Skip to content

Commit

Permalink
Merge pull request #586 from lochjin/dev1.2
Browse files Browse the repository at this point in the history
testutils
  • Loading branch information
dindinw authored Dec 18, 2023
2 parents 641b796 + 1c8a27b commit 96c9bdf
Show file tree
Hide file tree
Showing 25 changed files with 1,464 additions and 841 deletions.
3 changes: 1 addition & 2 deletions cmd/qng/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,9 @@ func consensusCmd() *cli.Command {
}

func loadConfig(ctx *cli.Context) error {
cfg, err := common.LoadConfig(ctx, false)
_, err := common.LoadConfig(ctx, false)
if err != nil {
return err
}
config.Cfg = cfg
return nil
}
1 change: 0 additions & 1 deletion cmd/qng/qng.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func qitmeerd(ctx *cli.Context) error {
if err != nil {
return err
}
config.Cfg = cfg
defer func() {
if log.LogWrite() != nil {
log.LogWrite().Close()
Expand Down
4 changes: 3 additions & 1 deletion meerevm/meer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ func MakeConfig(cfg *config.Config) (*eth.Config, error) {
nodeConf.HTTPModules = append(nodeConf.HTTPModules, "eth")
nodeConf.WSModules = append(nodeConf.WSModules, "eth")
nodeConf.IPCPath = ""
nodeConf.KeyStoreDir = filepath.Join(datadir, "keystore")
if len(datadir) > 0 {
nodeConf.KeyStoreDir = filepath.Join(datadir, "keystore")
}
//nodeConf.HTTPHost = node.DefaultHTTPHost
//nodeConf.WSHost = node.DefaultWSHost
nodeConf.HTTPPort, nodeConf.WSPort, nodeConf.AuthPort = getDefaultRPCPort()
Expand Down
11 changes: 10 additions & 1 deletion node/full.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (qm *QitmeerFull) GetPeerServer() *p2p.Service {
func (qm *QitmeerFull) GetRpcServer() *rpc.RpcServer {
var service *rpc.RpcServer
if err := qm.Services().FetchService(&service); err != nil {
log.Error(err.Error())
log.Warn(err.Error())
return nil
}
return service
Expand All @@ -213,6 +213,15 @@ func (qm *QitmeerFull) GetAccountManager() *acct.AccountManager {
return service
}

func (qm *QitmeerFull) GetWalletManager() *wallet.WalletManager {
var service *wallet.WalletManager
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
46 changes: 26 additions & 20 deletions p2p/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ package p2p
import (
"fmt"
"github.com/Qitmeer/qng/version"
ds "github.com/ipfs/go-ds-leveldb"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoreds"
"net"
"path"
"time"

ds "github.com/ipfs/go-ds-leveldb"
"github.com/libp2p/go-libp2p"
ma "github.com/multiformats/go-multiaddr"
)
Expand Down Expand Up @@ -41,17 +41,19 @@ const (
// buildOptions for the libp2p host.
func (s *Service) buildOptions(ip net.IP, priKey crypto.PrivKey) []libp2p.Option {
cfg := s.cfg
listen, err := multiAddressBuilder(ip.String(), cfg.TCPPort)
if err != nil {
log.Error(fmt.Sprintf("Failed to p2p listen: %v", err))
return nil
}
options := []libp2p.Option{
privKeyOption(priKey),
libp2p.ListenAddrs(listen),
libp2p.UserAgent(s.cfg.UserAgent),
libp2p.ConnectionGater(s),
}
if !s.cfg.DisableListen {
listen, err := multiAddressBuilder(ip.String(), cfg.TCPPort)
if err != nil {
log.Error(fmt.Sprintf("Failed to p2p listen: %v", err))
return nil
}
options = append(options, libp2p.ListenAddrs(listen))
}
if !s.cfg.NoDiscovery {
options = append(options, s.KademliaDHTOption())
}
Expand Down Expand Up @@ -101,28 +103,32 @@ func (s *Service) buildOptions(ip net.IP, priKey crypto.PrivKey) []libp2p.Option
log.Error(fmt.Sprintf("Invalid local ip provided: %s", cfg.LocalIP))
return options
}
listen, err = multiAddressBuilder(cfg.LocalIP, cfg.TCPPort)
listen, err := multiAddressBuilder(cfg.LocalIP, cfg.TCPPort)
if err != nil {
log.Error(fmt.Sprintf("Failed to p2p listen: %v", err))
return nil
}
options = append(options, libp2p.ListenAddrs(listen))
}

dsPath := path.Join(s.cfg.DataDir, PeerStore)
peerDS, err := ds.NewDatastore(dsPath, nil)
if err != nil {
log.Error(err.Error())
return nil
}
log.Info(fmt.Sprintf("Start Peers from:%s", dsPath))
if len(s.cfg.DataDir) > 0 {
dsPath := path.Join(s.cfg.DataDir, PeerStore)
log.Info(fmt.Sprintf("Start Peers from:%s", dsPath))
peerDS, err := ds.NewDatastore(dsPath, nil)
if err != nil {
log.Error(err.Error())
return nil
}

ps, err := pstoreds.NewPeerstore(s.Context(), peerDS, pstoreds.DefaultOpts())
if err != nil {
log.Error(err.Error())
return nil
ps, err := pstoreds.NewPeerstore(s.Context(), peerDS, pstoreds.DefaultOpts())
if err != nil {
log.Error(err.Error())
return nil
}
options = append(options, libp2p.Peerstore(ps))
} else {
options = append(options, libp2p.DefaultPeerstore)
}
options = append(options, libp2p.Peerstore(ps))

return options
}
Expand Down
15 changes: 10 additions & 5 deletions p2p/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ func NewService(cfg *config.Config, consensus model.Consensus, param *params.Par
if len(cfg.Listener) > 0 {
ipAddr = net.ParseIP(cfg.Listener)
}
if ipAddr == nil {
if ipAddr == nil && !cfg.DisableListen {
ipAddr = IpAddr()
}

Expand All @@ -697,15 +697,18 @@ func NewService(cfg *config.Config, consensus model.Consensus, param *params.Par
return nil, err
}
opts := s.buildOptions(ipAddr, s.privKey)
h, err := libp2p.New(opts...)
if cfg.DisableListen {
opts = append(opts, libp2p.DisableMetrics())
s.host, err = libp2p.NewWithoutDefaults(opts...)
} else {
s.host, err = libp2p.New(opts...)
}
if err != nil {
log.Error("Failed to create p2p host")
return nil, err
}

s.host = h

s.cfg.BootstrapNodeAddr = filterBootStrapAddrs(h.ID().String(), s.cfg.BootstrapNodeAddr)
s.cfg.BootstrapNodeAddr = filterBootStrapAddrs(s.host.ID().String(), s.cfg.BootstrapNodeAddr)

psOpts := []pubsub.Option{
pubsub.WithMessageSigning(false),
Expand Down Expand Up @@ -753,6 +756,8 @@ func logIPAddr(id peer.ID, addrs ...multiaddr.Multiaddr) {
}
if correctAddr != nil {
log.Info(fmt.Sprintf("Node started p2p server:multiAddr=%s", correctAddr.String()+"/p2p/"+id.String()))
} else {
log.Warn("QNG P2P server will be useless, neither dialing nor listening")
}
}

Expand Down
13 changes: 13 additions & 0 deletions p2p/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ func privKey(cfg *common.Config) (crypto.PrivKey, error) {
// Determines a private key for p2p networking from the p2p service's
// configuration struct. If no key is found, it generates a new one.
func PrivateKey(dataDir string, privateKeyPath string, readWritePermissions os.FileMode) (crypto.PrivKey, error) {
if len(dataDir) <= 0 && len(privateKeyPath) <= 0 {
priv, _, err := crypto.GenerateSecp256k1Key(rand.Reader)
if err != nil {
return nil, err
}
return priv, nil
}
defaultKeyPath := path.Join(dataDir, keyPath)

_, err := os.Stat(defaultKeyPath)
Expand Down Expand Up @@ -121,6 +128,12 @@ func retrievePrivKeyFromFile(path string) (crypto.PrivKey, error) {
// Retrieves node p2p metadata from a set of configuration values
// from the p2p service.
func metaDataFromConfig(cfg *common.Config) (*pb.MetaData, error) {
if len(cfg.DataDir) <= 0 && len(cfg.MetaDataDir) <= 0 {
return &pb.MetaData{
SeqNumber: 0,
Subnets: bitfield.NewBitvector64(),
}, nil
}
defaultKeyPath := path.Join(cfg.DataDir, metaDataPath)
metaDataPath := cfg.MetaDataDir

Expand Down
24 changes: 22 additions & 2 deletions script/cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,20 @@ function sendToAddress() {
get_result "$data"
}

function importRawKey() {
local privkey=$1
local password=$2
local data='{"jsonrpc":"2.0","method":"wallet_importRawKey","params":["'$privkey'","'$password'"],"id":null}'
get_result "$data"
}

function listAccount() {
local privkey=$1
local password=$2
local data='{"jsonrpc":"2.0","method":"wallet_listAccount","params":[],"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 @@ -900,7 +914,8 @@ function usage(){
echo " unlock (accountIndex) password timeout"
echo " lock (address)"
echo " sendtoaddress fromAddress addressAmounts({\"RmN6q2ZdNaCtgpq2BE5ZaUbfQxXwRU1yTYf\":{\"amount\":100000000,\"coinid\":0}}) locktime"

echo " importrawkey(privkey password)"
echo " listaccount"
}

# -------------------
Expand Down Expand Up @@ -1580,7 +1595,12 @@ elif [ "$1" == "lock" ]; then
elif [ "$1" == "sendtoaddress" ]; then
shift
sendToAddress "$@"

elif [ "$1" == "importrawkey" ]; then
shift
importRawKey "$@"
elif [ "$1" == "listaccount" ]; then
shift
listAccount "$@"

elif [ "$1" == "list_command" ]; then
usage
Expand Down
8 changes: 4 additions & 4 deletions services/acct/acctmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (a *AccountManager) Start() error {
if a.cfg.AcctMode {
err := a.initDB(true)
if err != nil {
return fmt.Errorf("Serious error, you can try to delete the data file(%s):%s", getDBPath(a.cfg.DataDir), err.Error())
return fmt.Errorf("Serious error, you can try to delete the data file(%s):%s", getDBPath(getDataDir(a.cfg)), err.Error())
}
} else {
a.cleanDB()
Expand All @@ -66,7 +66,7 @@ func (a *AccountManager) Stop() error {
func (a *AccountManager) initDB(first bool) error {
log.Info("AccountManager enable account mode")
var err error
a.db, err = loadDB("ffldb", a.cfg.DataDir, true)
a.db, err = loadDB("ffldb", getDataDir(a.cfg), true)
if err != nil {
return err
}
Expand Down Expand Up @@ -132,7 +132,7 @@ func (a *AccountManager) initDB(first bool) error {

func (a *AccountManager) cleanDB() {
if a.db == nil {
db, err := loadDB("ffldb", a.cfg.DataDir, false)
db, err := loadDB("ffldb", getDataDir(a.cfg), false)
if err != nil {
return
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func (a *AccountManager) cleanDB() {
}
}

err := removeDB(getDBPath(a.cfg.DataDir))
err := removeDB(getDBPath(getDataDir(a.cfg)))
if err != nil {
log.Error(err.Error())
}
Expand Down
9 changes: 9 additions & 0 deletions services/acct/dbhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"fmt"
"github.com/Qitmeer/qng/common/hash"
"github.com/Qitmeer/qng/config"
"github.com/Qitmeer/qng/core/serialization"
"github.com/Qitmeer/qng/core/types"
"github.com/Qitmeer/qng/database/legacydb"
Expand Down Expand Up @@ -71,6 +72,14 @@ func getDBPath(dataDir string) string {
return filepath.Join(dataDir, dbNamePrefix)
}

func getDataDir(cfg *config.Config) string {
dataDir := cfg.DataDir
if len(dataDir) <= 0 {
dataDir = cfg.HomeDir
}
return dataDir
}

// info
func DBGetACCTInfo(dbTx legacydb.Tx) (*AcctInfo, error) {
meta := dbTx.Metadata()
Expand Down
26 changes: 26 additions & 0 deletions services/address/address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package address

import (
"encoding/hex"
"github.com/Qitmeer/qng/params"
"github.com/Qitmeer/qng/testutils/simulator/testprivatekey"
"testing"
)

func TestNewAddresses(t *testing.T) {
params.ActiveNetParams = &params.PrivNetParam
pb, err := testprivatekey.NewBuilder(0)
if err != nil {
t.Fatal(err)
}
privateKeyHex := hex.EncodeToString(pb.Get(0))
privateKey, addr, eaddr, err := NewAddresses(privateKeyHex)
if err != nil {
t.Fatal(err)
}
pkHex := hex.EncodeToString(privateKey.Serialize())
if pkHex != privateKeyHex {
t.Fatalf("%s != %s (expect)", pkHex, privateKeyHex)
}
t.Logf("privateKey:%s addr:%s pkAddr:%s evmAddr:%s", pkHex, addr.PKHAddress().String(), addr.String(), eaddr.String())
}
33 changes: 20 additions & 13 deletions services/address/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/Qitmeer/qng/rpc"
"github.com/Qitmeer/qng/rpc/api"
"github.com/Qitmeer/qng/rpc/client/cmds"
ecommon "github.com/ethereum/go-ethereum/common"
"sync"
)

Expand Down Expand Up @@ -94,30 +95,36 @@ func NewPrivateAddressAPI(ai *AddressApi) *PrivateAddressAPI {
}

func (api *PrivateAddressAPI) GetAddresses(privateKeyHex string) (interface{}, error) {
privkeyByte, err := hex.DecodeString(privateKeyHex)
privateKey, addr, eaddr, err := NewAddresses(privateKeyHex)
if err != nil {
return nil, err
}
result := qjson.OrderedResult{
qjson.KV{Key: "PrivateKey", Val: hex.EncodeToString(privateKey.Serialize())},
qjson.KV{Key: "PKHAddress", Val: addr.PKHAddress().String()},
qjson.KV{Key: "PKAddress", Val: addr.String()},
qjson.KV{Key: "MeerEVM Address", Val: eaddr.String()},
}
return result, nil
}

func NewAddresses(privateKeyHex string) (ecc.PrivateKey, *address.SecpPubKeyAddress, ecommon.Address, error) {
privkeyByte, err := hex.DecodeString(privateKeyHex)
if err != nil {
return nil, nil, ecommon.Address{}, err
}
if len(privkeyByte) != 32 {
return nil, fmt.Errorf("error length:%d", len(privkeyByte))
return nil, nil, ecommon.Address{}, fmt.Errorf("error length:%d", len(privkeyByte))
}
privateKey, pubKey := ecc.Secp256k1.PrivKeyFromBytes(privkeyByte)

serializedKey := pubKey.SerializeCompressed()
addr, err := address.NewSecpPubKeyAddress(serializedKey, params.ActiveNetParams.Params)
if err != nil {
return nil, err
return nil, nil, ecommon.Address{}, err
}
eaddr, err := common.NewMeerEVMAddress(pubKey.SerializeUncompressed())
if err != nil {
return nil, err
}
result := qjson.OrderedResult{
qjson.KV{Key: "PrivateKey", Val: hex.EncodeToString(privateKey.Serialize())},
qjson.KV{Key: "PKHAddress", Val: addr.PKHAddress().String()},
qjson.KV{Key: "PKAddress", Val: addr.String()},
qjson.KV{Key: "MeerEVM Address", Val: eaddr.String()},
return nil, nil, ecommon.Address{}, err
}

return result, nil
return privateKey, addr, eaddr, nil
}
Loading

0 comments on commit 96c9bdf

Please sign in to comment.