Skip to content

Commit

Permalink
fix(grpc): introduce wallet manager config
Browse files Browse the repository at this point in the history
  • Loading branch information
ambersun1234 committed May 5, 2024
1 parent c3f47d1 commit 3d1275d
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 32 deletions.
5 changes: 4 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
47 changes: 25 additions & 22 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
15 changes: 15 additions & 0 deletions wallet/config.go
Original file line number Diff line number Diff line change
@@ -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{}
}
6 changes: 3 additions & 3 deletions wallet/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
6 changes: 5 additions & 1 deletion www/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion www/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down

0 comments on commit 3d1275d

Please sign in to comment.