diff options
-rw-r--r-- | core/nodeset-cache.go | 34 | ||||
-rw-r--r-- | core/nodeset-cache_test.go | 39 |
2 files changed, 42 insertions, 31 deletions
diff --git a/core/nodeset-cache.go b/core/nodeset-cache.go index 4afec28..5b9f25c 100644 --- a/core/nodeset-cache.go +++ b/core/nodeset-cache.go @@ -21,12 +21,13 @@ import ( "errors" "sync" + "github.com/dexon-foundation/dexon-consensus-core/common" "github.com/dexon-foundation/dexon-consensus-core/core/crypto" "github.com/dexon-foundation/dexon-consensus-core/core/types" ) var ( - // ErrRoundNotReady means we got nil config from governance contract. + // ErrRoundNotReady means we got nil config. ErrRoundNotReady = errors.New("round is not ready") ) @@ -36,10 +37,25 @@ type sets struct { dkgSet map[types.NodeID]struct{} } -// NodeSetCache caches node set information from governance contract. +// NodeSetCacheInterface interface specifies interface used by NodeSetCache. +type NodeSetCacheInterface interface { + // Configuration returns the configuration at a given round. + // Return the genesis configuration if round == 0. + Configuration(round uint64) *types.Config + + // CRS returns the CRS for a given round. + // Return the genesis CRS if round == 0. + CRS(round uint64) common.Hash + + // NodeSet returns the node set at a given round. + // Return the genesis node set if round == 0. + NodeSet(round uint64) []crypto.PublicKey +} + +// NodeSetCache caches node set information. type NodeSetCache struct { lock sync.RWMutex - gov Governance + nsIntf NodeSetCacheInterface rounds map[uint64]*sets keyPool map[types.NodeID]*struct { pubKey crypto.PublicKey @@ -48,9 +64,9 @@ type NodeSetCache struct { } // NewNodeSetCache constructs an NodeSetCache instance. -func NewNodeSetCache(gov Governance) *NodeSetCache { +func NewNodeSetCache(nsIntf NodeSetCacheInterface) *NodeSetCache { return &NodeSetCache{ - gov: gov, + nsIntf: nsIntf, rounds: make(map[uint64]*sets), keyPool: make(map[types.NodeID]*struct { pubKey crypto.PublicKey @@ -154,8 +170,8 @@ func (cache *NodeSetCache) update( cache.lock.Lock() defer cache.lock.Unlock() - // Get the requested round from governance contract. - keySet := cache.gov.NodeSet(round) + // Get the requested round. + keySet := cache.nsIntf.NodeSet(round) if keySet == nil { // That round is not ready yet. err = ErrRoundNotReady @@ -175,8 +191,8 @@ func (cache *NodeSetCache) update( }{key, 1} } } - cfg := cache.gov.Configuration(round) - crs := cache.gov.CRS(round) + cfg := cache.nsIntf.Configuration(round) + crs := cache.nsIntf.CRS(round) nIDs = &sets{ nodeSet: nodeSet, notarySet: make([]map[types.NodeID]struct{}, cfg.NumChains), diff --git a/core/nodeset-cache_test.go b/core/nodeset-cache_test.go index 73f0dee..0b69ee3 100644 --- a/core/nodeset-cache_test.go +++ b/core/nodeset-cache_test.go @@ -19,31 +19,31 @@ package core import ( "testing" + "time" "github.com/dexon-foundation/dexon-consensus-core/common" "github.com/dexon-foundation/dexon-consensus-core/core/crypto" "github.com/dexon-foundation/dexon-consensus-core/core/crypto/ecdsa" + "github.com/dexon-foundation/dexon-consensus-core/core/test" "github.com/dexon-foundation/dexon-consensus-core/core/types" "github.com/stretchr/testify/suite" ) -type testGov struct { +type nsIntf struct { s *NodeSetCacheTestSuite crs common.Hash curKeys []crypto.PublicKey } -func (g *testGov) Configuration(round uint64) (cfg *types.Config) { +func (g *nsIntf) Configuration(round uint64) (cfg *types.Config) { return &types.Config{ NotarySetSize: 7, DKGSetSize: 7, NumChains: 4, } } -func (g *testGov) CRS(round uint64) (b common.Hash) { return g.crs } -func (g *testGov) ProposeCRS([]byte) {} -func (g *testGov) NotifyRoundHeight(round, height uint64) {} -func (g *testGov) NodeSet(round uint64) []crypto.PublicKey { +func (g *nsIntf) CRS(round uint64) (b common.Hash) { return g.crs } +func (g *nsIntf) NodeSet(round uint64) []crypto.PublicKey { // Randomly generating keys, and check them for verification. g.curKeys = []crypto.PublicKey{} for i := 0; i < 10; i++ { @@ -53,32 +53,27 @@ func (g *testGov) NodeSet(round uint64) []crypto.PublicKey { } return g.curKeys } -func (g *testGov) AddDKGComplaint(_ uint64, _ *types.DKGComplaint) {} -func (g *testGov) DKGComplaints( - round uint64) (cs []*types.DKGComplaint) { - return -} -func (g *testGov) AddDKGMasterPublicKey( - _ uint64, _ *types.DKGMasterPublicKey) { -} -func (g *testGov) DKGMasterPublicKeys( - round uint64) (keys []*types.DKGMasterPublicKey) { - return -} -func (g *testGov) AddDKGFinalize(_ uint64, _ *types.DKGFinalize) {} -func (g *testGov) IsDKGFinal(round uint64) bool { return true } type NodeSetCacheTestSuite struct { suite.Suite } +func (s *NodeSetCacheTestSuite) TestGovernanceIntf() { + // NodeSetCacheInterface should let Governance implement it. + var gov Governance + gov, err := test.NewGovernance(7, 250*time.Millisecond) + s.Require().NoError(err) + _, ok := gov.(NodeSetCacheInterface) + s.True(ok) +} + func (s *NodeSetCacheTestSuite) TestBasicUsage() { var ( - gov = &testGov{ + nsIntf = &nsIntf{ s: s, crs: common.NewRandomHash(), } - cache = NewNodeSetCache(gov) + cache = NewNodeSetCache(nsIntf) req = s.Require() ) |