Skip to content

Commit

Permalink
refactor the node setup function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricfung committed Feb 13, 2024
1 parent 941f90c commit e96338a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
4 changes: 3 additions & 1 deletion kernel/election_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ func setupTestNode(require *require.Assertions, dir string) *Node {

custom, err := config.Initialize(dir + "/config.toml")
require.Nil(err)
gns, err := common.ReadGenesis(dir + "/genesis.json")
require.Nil(err)

cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7, // number of keys to track frequency of (10M).
Expand All @@ -118,7 +120,7 @@ func setupTestNode(require *require.Assertions, dir string) *Node {
store, err := storage.NewBadgerStore(custom, dir)
require.Nil(err)
require.NotNil(store)
node, err := SetupNode(custom, store, cache, 7239, dir)
node, err := SetupNode(custom, store, cache, gns, 7239)
require.Nil(err)
require.Equal(mainnetId, node.networkId.String())
return node
Expand Down
18 changes: 6 additions & 12 deletions kernel/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ type Node struct {
persistStore storage.Store
cacheStore *ristretto.Cache
custom *config.Custom
configDir string

done chan struct{}
elc chan struct{}
Expand All @@ -77,17 +76,16 @@ type CNode struct {
ConsensusIndex int
}

func SetupNode(custom *config.Custom, persistStore storage.Store, cacheStore *ristretto.Cache, legacyPort int, dir string) (*Node, error) {
func SetupNode(custom *config.Custom, store storage.Store, cache *ristretto.Cache, gns *common.Genesis, legacyPort int) (*Node, error) {
node := &Node{
SyncPoints: &syncMap{mutex: new(sync.RWMutex), m: make(map[crypto.Hash]*p2p.SyncPoint)},
LegacySyncPoints: &legacySyncMap{mutex: new(sync.RWMutex), m: make(map[crypto.Hash]*network.SyncPoint)},
LegacyAddr: fmt.Sprintf(":%d", legacyPort),
chains: &chainsMap{m: make(map[crypto.Hash]*Chain)},
genesisNodesMap: make(map[crypto.Hash]bool),
persistStore: persistStore,
cacheStore: cacheStore,
persistStore: store,
cacheStore: cache,
custom: custom,
configDir: dir,
startAt: clock.Now(),
done: make(chan struct{}),
elc: make(chan struct{}),
Expand All @@ -100,15 +98,11 @@ func SetupNode(custom *config.Custom, persistStore storage.Store, cacheStore *ri
mint := node.lastMintDistribution()
node.LastMint = mint.Batch

gns, err := common.ReadGenesis(dir + "/genesis.json")
err := node.LoadGenesis(gns)
if err != nil {
return nil, fmt.Errorf("ReadGenesis(%s) => %v", dir, err)
return nil, fmt.Errorf("LoadGenesis(%v) => %v", gns, err)
}
err = node.LoadGenesis(gns)
if err != nil {
return nil, fmt.Errorf("LoadGenesis(%s) => %v", dir, err)
}
node.TopoCounter = node.getTopologyCounter(persistStore)
node.TopoCounter = node.getTopologyCounter(store)

logger.Println("Validating graph entries...")
start := clock.Now()
Expand Down
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"runtime"

"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/kernel"
"github.com/MixinNetwork/mixin/logger"
Expand Down Expand Up @@ -661,6 +662,12 @@ func kernelCmd(c *cli.Context) error {
if err != nil {
return err
}

gns, err := common.ReadGenesis(c.String("dir") + "/genesis.json")
if err != nil {
return err
}

custom, err := config.Initialize(c.String("dir") + "/config.toml")
if err != nil {
return err
Expand All @@ -677,7 +684,7 @@ func kernelCmd(c *cli.Context) error {
}
defer store.Close()

node, err := kernel.SetupNode(custom, store, cache, c.Int("port"), c.String("dir"))
node, err := kernel.SetupNode(custom, store, cache, gns, c.Int("port"))
if err != nil {
return err
}
Expand Down
11 changes: 8 additions & 3 deletions rpc/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func testConsensus(t *testing.T, withRelayers bool) {
instances := make([]*kernel.Node, 0)
for i := range accounts {
dir := fmt.Sprintf("%s/mixin-170%02d", root, i+1)
gns, err := common.ReadGenesis(dir + "/genesis.json")
require.Nil(err)
custom, err := config.Initialize(dir + "/config.toml")
require.Nil(err)
cache := newCache(custom)
Expand All @@ -72,7 +74,7 @@ func testConsensus(t *testing.T, withRelayers bool) {
if i == 0 {
kernel.TestMockDiff(epoch.Sub(time.Now()))
}
node, err := kernel.SetupNode(custom, store, cache, 0, dir)
node, err := kernel.SetupNode(custom, store, cache, gns, 0)
require.Nil(err)
require.NotNil(node)
instances = append(instances, node)
Expand Down Expand Up @@ -639,11 +641,13 @@ func testPledgeNewNode(t *testing.T, nodes []*Node, domain common.Address, genes

custom, err := config.Initialize(dir + "/config.toml")
require.Nil(err)
gns, err := common.ReadGenesis(dir + "/genesis.json")
require.Nil(err)
cache := newCache(custom)
store, err := storage.NewBadgerStore(custom, dir)
require.Nil(err)
require.NotNil(store)
pnode, err := kernel.SetupNode(custom, store, cache, 0, dir)
pnode, err := kernel.SetupNode(custom, store, cache, gns, 0)
require.Nil(err)
require.NotNil(pnode)
go pnode.Loop()
Expand Down Expand Up @@ -827,10 +831,11 @@ func setupTestNet(root string, withRelayers bool) ([]common.Address, []common.Ad
if err != nil {
panic(err)
}
gns, _ := common.ReadGenesis(dir + "/genesis.json")
custom, _ := config.Initialize(dir + "/config.toml")
cache := newCache(custom)
store, _ := storage.NewBadgerStore(custom, dir)
node, _ := kernel.SetupNode(custom, store, cache, 0, dir)
node, _ := kernel.SetupNode(custom, store, cache, gns, 0)
go node.Loop()
}
}
Expand Down

0 comments on commit e96338a

Please sign in to comment.