Skip to content

Commit

Permalink
Merge pull request #529 from frankcrypto2023/wallet-rebase
Browse files Browse the repository at this point in the history
Wallet Module
  • Loading branch information
dindinw authored Sep 26, 2023
2 parents fd2aaff + ae2285a commit fb64d47
Show file tree
Hide file tree
Showing 23 changed files with 973 additions and 169 deletions.
13 changes: 7 additions & 6 deletions cmd/miner/symbols/lib/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/Qitmeer/qng/cmd/miner/common"
"github.com/Qitmeer/qng/cmd/miner/core"
"github.com/Qitmeer/qng/common/hash"
"github.com/Qitmeer/qng/core/types"
"github.com/Qitmeer/qng/rpc/client"
"github.com/Qitmeer/qng/rpc/client/cmds"
"io/ioutil"
"log"
"strconv"
"strings"
"sync"
"time"

"github.com/Qitmeer/qng/cmd/miner/common"
"github.com/Qitmeer/qng/cmd/miner/core"
"github.com/Qitmeer/qng/common/hash"
"github.com/Qitmeer/qng/core/types"
"github.com/Qitmeer/qng/rpc/client"
"github.com/Qitmeer/qng/rpc/client/cmds"
)

const (
Expand Down
9 changes: 5 additions & 4 deletions cmd/qng/qng.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ package main

import (
"fmt"
"os"
"runtime"
"runtime/debug"
"runtime/pprof"

"github.com/Qitmeer/qng/common/profiling"
"github.com/Qitmeer/qng/common/roughtime"
"github.com/Qitmeer/qng/common/system"
Expand All @@ -18,10 +23,6 @@ import (
"github.com/Qitmeer/qng/services/common"
"github.com/Qitmeer/qng/version"
"github.com/urfave/cli/v2"
"os"
"runtime"
"runtime/debug"
"runtime/pprof"
)

func main() {
Expand Down
7 changes: 6 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package config

import (
"github.com/Qitmeer/qng/core/types"
"path/filepath"

"github.com/Qitmeer/qng/core/types"
)

type Config struct {
Expand Down Expand Up @@ -39,6 +40,7 @@ type Config struct {
TrackHeapLimit int `long:"trackheaplimit" description:"track heap when limit in gigabytes (default:7G)"`
DebugLevel string `short:"d" long:"debuglevel" description:"Logging level {trace, debug, info, warn, error, critical} "`
DebugPrintOrigins bool `long:"printorigin" description:"Print log debug location (file:line) "`

// MemPool Config
NoRelayPriority bool `long:"norelaypriority" description:"Do not require free or low-fee transactions to have high priority for relaying"`
FreeTxRelayLimit float64 `long:"limitfreerelay" description:"Limit relay of transactions with no transaction fee to the given amount in thousands of bytes per minute"`
Expand Down Expand Up @@ -129,6 +131,9 @@ type Config struct {

// TODO: It will soon be discarded in the near future
DevNextGDB bool `long:"devnextgdb" description:"Enable next generation databases that only exist in development mode"`
// wallet
WalletPass string
AutoCollectEvm bool `long:"autocollectevm" description:"auto collect utxo to evm"`
}

func (c *Config) GetMinningAddrs() []types.Address {
Expand Down
7 changes: 7 additions & 0 deletions core/json/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,12 @@ type Amout struct {
CoinId uint16 `json:"coinid"`
Amount int64 `json:"amount"`
}
type AmountV3 struct {
CoinId uint16 `json:"coinid"`
Amount int64 `json:"amount"`
TargetLockTime int64 `json:"targetLockTime"`
}

type AdreesAmount map[string]Amout

type AddressAmountV3 map[string]AmountV3
6 changes: 6 additions & 0 deletions core/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -1136,3 +1136,9 @@ var scriptPool scriptFreeList = make(chan []byte, freeListMaxItems)
func IsSequenceLockTimeDisabled(sequence uint32) bool {
return sequence&SequenceLockTimeDisabled != 0 || sequence <= TxTypeInSequence
}

type AutoCollectUtxo struct {
Op TxOutPoint
Address string
Amount uint64
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb
github.com/google/gops v0.3.25
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d
github.com/howeyc/gopass v0.0.0-20210920133722-c8aef6fb66ef
github.com/ipfs/go-ds-leveldb v0.5.0
github.com/ipfs/go-log/v2 v2.5.1
github.com/jessevdk/go-flags v1.4.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZ
github.com/holiman/bloomfilter/v2 v2.0.3/go.mod h1:zpoh+gs7qcpqrHr3dB55AMiJwo0iURXE7ZOP9L9hSkA=
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c h1:DZfsyhDK1hnSS5lH8l+JggqzEleHteTYfutAiVlSUM8=
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c/go.mod h1:SC8Ryt4n+UBbPbIBKaG9zbbDlp4jOru9xFZmPzLUTxw=
github.com/howeyc/gopass v0.0.0-20210920133722-c8aef6fb66ef h1:A9HsByNhogrvm9cWb28sjiS3i7tcKCkflWFEkHfuAgM=
github.com/howeyc/gopass v0.0.0-20210920133722-c8aef6fb66ef/go.mod h1:lADxMC39cJJqL93Duh1xhAs4I2Zs8mKS89XWXFGp9cs=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huin/goupnp v1.2.0 h1:uOKW26NG1hsSSbXIZ1IR7XP9Gjd1U8pnLaCMgntmkmY=
github.com/huin/goupnp v1.2.0/go.mod h1:gnGPsThkYa7bFi/KWmEysQRf48l2dvR5bxr2OFckNX8=
Expand Down
5 changes: 3 additions & 2 deletions meerevm/cmd/accountcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ package cmd

import (
"fmt"
"io/ioutil"

"github.com/Qitmeer/qng/config"
"github.com/Qitmeer/qng/meerevm/eth"
"io/ioutil"

"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/cmd/utils"
Expand Down Expand Up @@ -288,6 +289,6 @@ func accountImport(ctx *cli.Context) error {
if err != nil {
utils.Fatalf("Could not create the account: %v", err)
}
fmt.Printf("Address: {%x}\n", acct.Address)
fmt.Printf("PKAAddress: {%x}\n", acct.Address)
return nil
}
3 changes: 2 additions & 1 deletion node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (api *PublicBlockChainAPI) GetNodeInfo() (interface{}, error) {
Network: params.ActiveNetParams.Name,
Confirmations: meerdag.StableConfirmations,
CoinbaseMaturity: int32(api.node.node.Params.CoinbaseMaturity),
Modules: []string{cmds.DefaultServiceNameSpace, cmds.MinerNameSpace, cmds.TestNameSpace, cmds.LogNameSpace, cmds.P2PNameSpace},
Modules: []string{cmds.DefaultServiceNameSpace, cmds.MinerNameSpace, cmds.TestNameSpace, cmds.LogNameSpace, cmds.P2PNameSpace, cmds.WalletNameSpace},
}
ret.GraphState = marshal.GetGraphStateResult(best.GraphState)
ret.StateRoot = best.StateRoot.String()
Expand Down Expand Up @@ -198,6 +198,7 @@ func (api *PublicBlockChainAPI) GetRpcModules() (interface{}, error) {
json.KV{Key: cmds.TestNameSpace, Val: false},
json.KV{Key: cmds.LogNameSpace, Val: false},
json.KV{Key: cmds.P2PNameSpace, Val: false},
json.KV{Key: cmds.WalletNameSpace, Val: false},
}

for _, m := range api.node.node.Config.Modules {
Expand Down
34 changes: 28 additions & 6 deletions node/full.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package node

import (
"fmt"
"path/filepath"
"reflect"
"time"

"github.com/Qitmeer/qng/common/system"
"github.com/Qitmeer/qng/common/system/disk"
"github.com/Qitmeer/qng/config"
Expand All @@ -25,10 +29,8 @@ import (
"github.com/Qitmeer/qng/services/mining"
"github.com/Qitmeer/qng/services/notifymgr"
"github.com/Qitmeer/qng/services/tx"
"github.com/Qitmeer/qng/services/wallet"
ecommon "github.com/ethereum/go-ethereum/common"
"path/filepath"
"reflect"
"time"
)

// QitmeerFull implements the qitmeer full node service.
Expand Down Expand Up @@ -138,7 +140,7 @@ func (qm *QitmeerFull) RegisterNotifyMgr() error {

func (qm *QitmeerFull) RegisterAccountService(cfg *config.Config) error {
// account manager
acctmgr, err := acct.New(qm.GetBlockChain(), cfg)
acctmgr, err := acct.New(qm.GetBlockChain(), cfg, qm.node.consensus.Events())
if err != nil {
return err
}
Expand All @@ -147,6 +149,16 @@ func (qm *QitmeerFull) RegisterAccountService(cfg *config.Config) error {
return nil
}

func (qm *QitmeerFull) RegisterWalletService(cfg *config.Config) error {
walletmgr, err := wallet.New(cfg, qm.node.consensus.BlockChain().MeerChain().(*meer.MeerChain),
qm.GetAccountManager(), qm.GetTxManager(), qm.node.consensus.Events())
if err != nil {
return err
}
qm.Services().RegisterService(walletmgr)
return nil
}

func (qm *QitmeerFull) RegisterAmana() error {
if !qm.node.Config.Amana ||
params.ActiveNetParams.Net == protocol.MainNet {
Expand Down Expand Up @@ -192,6 +204,15 @@ func (qm *QitmeerFull) GetTxManager() *tx.TxManager {
return service
}

func (qm *QitmeerFull) GetAccountManager() *acct.AccountManager {
var service *acct.AccountManager
if err := qm.Services().FetchService(&service); err != nil {
log.Error(err.Error())
return nil
}
return service
}

func (qm *QitmeerFull) GetMiner() *miner.Miner {
var service *miner.Miner
if err := qm.Services().FetchService(&service); err != nil {
Expand Down Expand Up @@ -286,16 +307,17 @@ func newQitmeerFullNode(node *Node) (*QitmeerFull, error) {
}
// init address api
qm.addressApi = address.NewAddressApi(cfg, node.Params, qm.GetBlockChain())

if err := qm.RegisterAccountService(cfg); err != nil {
return nil, err
}

if qm.node.consensus.AmanaService() != nil {
if err := qm.Services().RegisterService(qm.node.consensus.AmanaService()); err != nil {
return nil, err
}
}
if err := qm.RegisterWalletService(cfg); err != nil {
return nil, err
}

apis, err := qm.RegisterRpcService()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions rpc/client/cmds/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
LogNameSpace = "log"
NotifyNameSpace = ""
P2PNameSpace = "p2p"
WalletNameSpace = "wallet"
)

type RPCErrorCode int
Expand Down
41 changes: 41 additions & 0 deletions script/cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,30 @@ function get_balance_info() {
get_result "$data"
}



function unlock() {
local account=$1
local password=$2
local timeout=$3
local data='{"jsonrpc":"2.0","method":"wallet_unlock","params":["'$account'","'$password'",'$timeout'],"id":null}'
get_result "$data"
}

function lock() {
local address=$1
local data='{"jsonrpc":"2.0","method":"wallet_lock","params":["'$address'"],"id":null}'
get_result "$data"
}

function sendToAddress() {
local fromAddress=$1
local to=$2
local lockTime=$3
local data='{"jsonrpc":"2.0","method":"wallet_sendToAddress","params":["'$fromAddress'","'$to'",'$lockTime'],"id":null}'
get_result "$data"
}

function add_balance() {
local address=$1
local data='{"jsonrpc":"2.0","method":"addBalance","params":["'$address'"],"id":null}'
Expand Down Expand Up @@ -866,6 +890,11 @@ function usage(){
echo " iscurrent"
echo " consistency <hashOrOrder>"
echo " disablerelaytx <bool>"
echo "wallet :"
echo " unlock (accountIndex) password timeout"
echo " lock (address)"
echo " sendtoaddress fromAddress addressAmounts({\"RmN6q2ZdNaCtgpq2BE5ZaUbfQxXwRU1yTYf\":{\"amount\":100000000,\"coinid\":0}}) locktime"

}

# -------------------
Expand Down Expand Up @@ -1530,6 +1559,18 @@ elif [ "$1" == "stateroot" ]; then
elif [ "$1" == "config" ]; then
shift
get_config $@

## Wallet
elif [ "$1" == "unlock" ]; then
shift
unlock "$@"
elif [ "$1" == "lock" ]; then
shift
lock "$@"
elif [ "$1" == "sendtoaddress" ]; then
shift
sendToAddress "$@"


elif [ "$1" == "list_command" ]; then
usage
Expand Down
15 changes: 14 additions & 1 deletion services/acct/acctmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"bytes"
"encoding/hex"
"fmt"

"github.com/Qitmeer/qng/config"
"github.com/Qitmeer/qng/core/address"
"github.com/Qitmeer/qng/core/blockchain"
"github.com/Qitmeer/qng/core/blockchain/utxo"
"github.com/Qitmeer/qng/core/event"
"github.com/Qitmeer/qng/core/types"
"github.com/Qitmeer/qng/database/legacydb"
"github.com/Qitmeer/qng/engine/txscript"
Expand All @@ -26,6 +28,11 @@ type AccountManager struct {
info *AcctInfo
utxoops []*UTXOOP
watchers map[string]*AcctBalanceWatcher
events *event.Feed
}

func (a *AccountManager) SetEvents(evs *event.Feed) {
a.events = evs
}

func (a *AccountManager) Start() error {
Expand All @@ -50,6 +57,7 @@ func (a *AccountManager) Stop() error {
if a.db != nil {
if err := a.db.Close(); err != nil {
log.Error(err.Error())

}
}
return nil
Expand Down Expand Up @@ -247,6 +255,7 @@ func (a *AccountManager) checkUtxoEntry(entry *utxo.UtxoEntry, tracks []string)
}

func (a *AccountManager) apply(add bool, op *types.TxOutPoint, entry *utxo.UtxoEntry) error {

addrStr, scriptClass, err := a.checkUtxoEntry(entry, a.info.addrs)
if err != nil {
return err
Expand Down Expand Up @@ -610,6 +619,9 @@ func (a *AccountManager) AddAddress(addr string) error {
}
return a.rebuild([]string{addr})
}
func (a *AccountManager) GetChain() *blockchain.BlockChain {
return a.chain
}

func (a *AccountManager) cleanBalanceDB(dbTx legacydb.Tx, addr string) error {
er := DBDelACCTBalance(dbTx, addr)
Expand Down Expand Up @@ -640,13 +652,14 @@ func (a *AccountManager) APIs() []api.API {
}
}

func New(chain *blockchain.BlockChain, cfg *config.Config) (*AccountManager, error) {
func New(chain *blockchain.BlockChain, cfg *config.Config, _events *event.Feed) (*AccountManager, error) {
a := AccountManager{
chain: chain,
cfg: cfg,
info: NewAcctInfo(),
utxoops: []*UTXOOP{},
watchers: map[string]*AcctBalanceWatcher{},
events: _events,
}
return &a, nil
}
Loading

0 comments on commit fb64d47

Please sign in to comment.