diff options
author | Mission Liao <mission.liao@dexon.org> | 2018-11-13 16:28:24 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-13 16:28:24 +0800 |
commit | 01642721a7768218e7f9a5be8f0829eb8ae7c7b1 (patch) | |
tree | a8ebe89570de317d7be047f32c8c35334e78c919 /core/test | |
parent | 86838fe70789292de0851f82426e5241c0f0cc96 (diff) | |
download | dexon-consensus-01642721a7768218e7f9a5be8f0829eb8ae7c7b1.tar dexon-consensus-01642721a7768218e7f9a5be8f0829eb8ae7c7b1.tar.gz dexon-consensus-01642721a7768218e7f9a5be8f0829eb8ae7c7b1.tar.bz2 dexon-consensus-01642721a7768218e7f9a5be8f0829eb8ae7c7b1.tar.lz dexon-consensus-01642721a7768218e7f9a5be8f0829eb8ae7c7b1.tar.xz dexon-consensus-01642721a7768218e7f9a5be8f0829eb8ae7c7b1.tar.zst dexon-consensus-01642721a7768218e7f9a5be8f0829eb8ae7c7b1.zip |
core: expose implicit round shift (#321)
Diffstat (limited to 'core/test')
-rw-r--r-- | core/test/governance.go | 20 | ||||
-rw-r--r-- | core/test/governance_test.go | 17 |
2 files changed, 26 insertions, 11 deletions
diff --git a/core/test/governance.go b/core/test/governance.go index f27139f..58b773a 100644 --- a/core/test/governance.go +++ b/core/test/governance.go @@ -35,6 +35,7 @@ import ( // Governance is an implementation of Goverance for testing purpose. type Governance struct { + roundShift uint64 configs []*types.Config nodeSets [][]crypto.PublicKey stateModule *State @@ -44,13 +45,14 @@ type Governance struct { } // NewGovernance constructs a Governance instance. -func NewGovernance(genesisNodes []crypto.PublicKey, - lambda time.Duration) (g *Governance, err error) { +func NewGovernance(genesisNodes []crypto.PublicKey, lambda time.Duration, + roundShift uint64) (g *Governance, err error) { // Setup a State instance. // TODO(mission): it's not a good idea to embed initialization of one // public class in another, I did this to make the range of // modification smaller. g = &Governance{ + roundShift: roundShift, pendingConfigChanges: make(map[uint64]map[StateChangeType]interface{}), stateModule: NewState(genesisNodes, lambda, true), } @@ -95,17 +97,20 @@ func (g *Governance) CRS(round uint64) common.Hash { // NotifyRoundHeight notifies governace contract to snapshot config. func (g *Governance) NotifyRoundHeight(round, height uint64) { - g.CatchUpWithRound(round) + // Snapshot configuration for the shifted round, this behavior is synced with + // full node's implementation. + shiftedRound := round + g.roundShift + g.CatchUpWithRound(shiftedRound) // Apply change request for next round. func() { g.lock.Lock() defer g.lock.Unlock() - for t, v := range g.pendingConfigChanges[round+1] { + for t, v := range g.pendingConfigChanges[shiftedRound+1] { if err := g.stateModule.RequestChange(t, v); err != nil { panic(err) } } - delete(g.pendingConfigChanges, round+1) + delete(g.pendingConfigChanges, shiftedRound+1) g.broadcastPendingStateChanges() }() } @@ -280,6 +285,7 @@ func (g *Governance) Clone() *Governance { } // Clone pending changes. return &Governance{ + roundShift: g.roundShift, configs: copiedConfigs, stateModule: copiedState, nodeSets: copiedNodeSets, @@ -289,6 +295,10 @@ func (g *Governance) Clone() *Governance { // Equal checks equality between two Governance instances. func (g *Governance) Equal(other *Governance, checkState bool) bool { + // Check roundShift. + if g.roundShift != other.roundShift { + return false + } // Check configs. if !reflect.DeepEqual(g.configs, other.configs) { return false diff --git a/core/test/governance_test.go b/core/test/governance_test.go index 1c6dbd2..07b0d46 100644 --- a/core/test/governance_test.go +++ b/core/test/governance_test.go @@ -33,10 +33,10 @@ func (s *GovernanceTestSuite) TestEqual() { // Setup a base governance. _, genesisNodes, err := NewKeys(20) req.NoError(err) - g1, err := NewGovernance(genesisNodes, 100*time.Millisecond) + g1, err := NewGovernance(genesisNodes, 100*time.Millisecond, 2) req.NoError(err) // Create a governance with different lambda. - g2, err := NewGovernance(genesisNodes, 50*time.Millisecond) + g2, err := NewGovernance(genesisNodes, 50*time.Millisecond, 2) req.NoError(err) req.False(g1.Equal(g2, true)) // Create configs for 3 rounds for g1. @@ -57,13 +57,18 @@ func (s *GovernanceTestSuite) TestEqual() { g1.CatchUpWithRound(5) g4.CatchUpWithRound(5) req.False(g1.Equal(g4, true)) + // Make a clone. + g5 := g1.Clone() + // Change its roundShift + g5.roundShift = 3 + req.False(g1.Equal(g5, true)) } func (s *GovernanceTestSuite) TestRegisterChange() { req := s.Require() _, genesisNodes, err := NewKeys(20) req.NoError(err) - g, err := NewGovernance(genesisNodes, 100*time.Millisecond) + g, err := NewGovernance(genesisNodes, 100*time.Millisecond, 2) req.NoError(err) // Unable to register change for genesis round. req.Error(g.RegisterConfigChange(0, StateChangeNumChains, uint32(32))) @@ -79,12 +84,12 @@ func (s *GovernanceTestSuite) TestRegisterChange() { req.NoError(g.RegisterConfigChange(7, StateChangeNumChains, uint32(40))) // In local mode, state for round 6 would be ready after notified with // round 5. - g.NotifyRoundHeight(5, 0) + g.NotifyRoundHeight(3, 0) // In local mode, state for round 7 would be ready after notified with // round 6. - g.NotifyRoundHeight(6, 0) + g.NotifyRoundHeight(4, 0) // Notify governance to take a snapshot for round 7's configuration. - g.NotifyRoundHeight(7, 0) + g.NotifyRoundHeight(5, 0) req.Equal(g.Configuration(6).NumChains, uint32(32)) req.Equal(g.Configuration(7).NumChains, uint32(40)) } |