From 3d1275d2bb41cf7547b4ec27952cce68bfb4f4e3 Mon Sep 17 00:00:00 2001 From: Shawn Hsu Date: Sun, 5 May 2024 17:11:56 +0800 Subject: [PATCH] fix(grpc): introduce wallet manager config --- cmd/cmd.go | 5 ++++- config/config.go | 47 ++++++++++++++++++++++------------------- node/node.go | 4 ++-- node/node_test.go | 2 +- tests/main_test.go | 1 - wallet/config.go | 15 +++++++++++++ wallet/manager.go | 6 +++--- www/grpc/server_test.go | 6 +++++- www/http/http_test.go | 5 ++++- 9 files changed, 59 insertions(+), 32 deletions(-) create mode 100644 wallet/config.go diff --git a/cmd/cmd.go b/cmd/cmd.go index 9689928bc..f820727f6 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -410,7 +410,7 @@ func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, return nil, nil, err } - nodeInstance, err := node.NewNode(walletsDir, gen, conf, valKeys, rewardAddrs) + nodeInstance, err := node.NewNode(gen, conf, valKeys, rewardAddrs) if err != nil { return nil, nil, err } @@ -483,6 +483,9 @@ func MakeConfig(genDoc *genesis.Genesis, confPath, walletsDir string) (*config.C conf.GRPC.DefaultWalletName = DefaultWalletName conf.GRPC.WalletsDir = walletsDir + conf.WalletManager.ChainType = chainType + conf.WalletManager.WalletsDir = walletsDir + return conf, nil } diff --git a/config/config.go b/config/config.go index 4f711b295..33f73b627 100644 --- a/config/config.go +++ b/config/config.go @@ -15,6 +15,7 @@ import ( "github.com/pactus-project/pactus/txpool" "github.com/pactus-project/pactus/util" "github.com/pactus-project/pactus/util/logger" + "github.com/pactus-project/pactus/wallet" "github.com/pactus-project/pactus/www/grpc" "github.com/pactus-project/pactus/www/http" "github.com/pactus-project/pactus/www/jsonrpc" @@ -34,17 +35,18 @@ var ( ) type Config struct { - Node *NodeConfig `toml:"node"` - Store *store.Config `toml:"store"` - Network *network.Config `toml:"network"` - Sync *sync.Config `toml:"sync"` - TxPool *txpool.Config `toml:"tx_pool"` - Consensus *consensus.Config `toml:"-"` - Logger *logger.Config `toml:"logger"` - GRPC *grpc.Config `toml:"grpc"` - JSONRPC *jsonrpc.Config `toml:"jsonrpc"` - HTTP *http.Config `toml:"http"` - Nanomsg *nanomsg.Config `toml:"nanomsg"` + Node *NodeConfig `toml:"node"` + Store *store.Config `toml:"store"` + Network *network.Config `toml:"network"` + Sync *sync.Config `toml:"sync"` + TxPool *txpool.Config `toml:"tx_pool"` + Consensus *consensus.Config `toml:"-"` + Logger *logger.Config `toml:"logger"` + GRPC *grpc.Config `toml:"grpc"` + JSONRPC *jsonrpc.Config `toml:"jsonrpc"` + HTTP *http.Config `toml:"http"` + WalletManager *wallet.Config `toml:"-"` + Nanomsg *nanomsg.Config `toml:"nanomsg"` } type BootstrapInfo struct { @@ -86,17 +88,18 @@ func (conf *NodeConfig) BasicCheck() error { func defaultConfig() *Config { conf := &Config{ - Node: DefaultNodeConfig(), - Store: store.DefaultConfig(), - Network: network.DefaultConfig(), - Sync: sync.DefaultConfig(), - TxPool: txpool.DefaultConfig(), - Consensus: consensus.DefaultConfig(), - Logger: logger.DefaultConfig(), - GRPC: grpc.DefaultConfig(), - JSONRPC: jsonrpc.DefaultConfig(), - HTTP: http.DefaultConfig(), - Nanomsg: nanomsg.DefaultConfig(), + Node: DefaultNodeConfig(), + Store: store.DefaultConfig(), + Network: network.DefaultConfig(), + Sync: sync.DefaultConfig(), + TxPool: txpool.DefaultConfig(), + Consensus: consensus.DefaultConfig(), + Logger: logger.DefaultConfig(), + GRPC: grpc.DefaultConfig(), + JSONRPC: jsonrpc.DefaultConfig(), + HTTP: http.DefaultConfig(), + Nanomsg: nanomsg.DefaultConfig(), + WalletManager: wallet.DefaultConfig(), } return conf diff --git a/node/node.go b/node/node.go index cf79232ab..cdca3dfba 100644 --- a/node/node.go +++ b/node/node.go @@ -41,7 +41,7 @@ type Node struct { nanomsg *nanomsg.Server } -func NewNode(walletDir string, genDoc *genesis.Genesis, conf *config.Config, +func NewNode(genDoc *genesis.Genesis, conf *config.Config, valKeys []*bls.ValidatorKey, rewardAddrs []crypto.Address, ) (*Node, error) { // Initialize the logger @@ -77,7 +77,7 @@ func NewNode(walletDir string, genDoc *genesis.Genesis, conf *config.Config, } consMgr := consensus.NewManager(conf.Consensus, st, valKeys, rewardAddrs, messageCh) - walletMgr := wallet.NewWalletManager(chainType, walletDir) + walletMgr := wallet.NewWalletManager(conf.WalletManager) syn, err := sync.NewSynchronizer(conf.Sync, valKeys, st, consMgr, net, messageCh) if err != nil { diff --git a/node/node_test.go b/node/node_test.go index 60e6a0556..dbfccb14c 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -39,7 +39,7 @@ func TestRunningNode(t *testing.T) { valKeys := []*bls.ValidatorKey{ts.RandValKey(), ts.RandValKey()} rewardAddrs := []crypto.Address{ts.RandAccAddress(), ts.RandAccAddress()} - n, err := NewNode(util.TempDirPath(), gen, conf, valKeys, rewardAddrs) + n, err := NewNode(gen, conf, valKeys, rewardAddrs) require.NoError(t, err) assert.Equal(t, n.state.LastBlockHash(), hash.UndefHash) diff --git a/tests/main_test.go b/tests/main_test.go index 4e1f1ea6e..780fea4c2 100644 --- a/tests/main_test.go +++ b/tests/main_test.go @@ -129,7 +129,6 @@ func TestMain(m *testing.M) { for i := 0; i < tTotalNodes; i++ { tNodes[i], _ = node.NewNode( - util.TempDirPath(), tGenDoc, tConfigs[i], tValKeys[i], []crypto.Address{ diff --git a/wallet/config.go b/wallet/config.go new file mode 100644 index 000000000..afa308cce --- /dev/null +++ b/wallet/config.go @@ -0,0 +1,15 @@ +package wallet + +import ( + "github.com/pactus-project/pactus/genesis" +) + +type Config struct { + // private config + WalletsDir string `toml:"-"` + ChainType genesis.ChainType `toml:"-"` +} + +func DefaultConfig() *Config { + return &Config{} +} diff --git a/wallet/manager.go b/wallet/manager.go index 37cc44059..af792b5eb 100644 --- a/wallet/manager.go +++ b/wallet/manager.go @@ -19,11 +19,11 @@ type Manager struct { walletDirectory string } -func NewWalletManager(chainType genesis.ChainType, walletDir string) *Manager { +func NewWalletManager(conf *Config) *Manager { return &Manager{ wallets: make(map[string]*Wallet), - chainType: chainType, - walletDirectory: walletDir, + chainType: conf.ChainType, + walletDirectory: conf.WalletsDir, } } diff --git a/www/grpc/server_test.go b/www/grpc/server_test.go index 6a6139a17..05d0cdf91 100644 --- a/www/grpc/server_test.go +++ b/www/grpc/server_test.go @@ -77,10 +77,14 @@ func setup(t *testing.T, conf *Config) *testData { require.NoError(t, err) require.NoError(t, defaultWallet.Save()) + mockWalletMgrConf := wallet.DefaultConfig() + mockWalletMgrConf.WalletsDir = conf.WalletsDir + mockWalletMgrConf.ChainType = mockState.Genesis().ChainType() + server := NewServer( conf, mockState, mockSync, mockNet, - mockConsMgr, wallet.NewWalletManager(mockState.Genesis().ChainType(), conf.WalletsDir), + mockConsMgr, wallet.NewWalletManager(mockWalletMgrConf), ) err = server.startListening(listener) assert.NoError(t, err) diff --git a/www/http/http_test.go b/www/http/http_test.go index e4fbff89c..fbaf4a7a1 100644 --- a/www/http/http_test.go +++ b/www/http/http_test.go @@ -60,10 +60,13 @@ func setup(t *testing.T) *testData { Listen: "[::]:0", } + mockWalletMgrConf := wallet.DefaultConfig() + mockWalletMgrConf.ChainType = mockState.Genesis().ChainType() + gRPCServer := grpc.NewServer( grpcConf, mockState, mockSync, mockNet, - mockConsMgr, wallet.NewWalletManager(mockState.Genesis().ChainType(), ""), + mockConsMgr, wallet.NewWalletManager(mockWalletMgrConf), ) assert.NoError(t, gRPCServer.StartServer())