diff options
author | Mission Liao <mission.liao@dexon.org> | 2019-02-20 12:53:18 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-20 12:53:18 +0800 |
commit | 8ef4fc213703620fbfa13890dee042d40eea8545 (patch) | |
tree | ba9a07d2423314396e5677b7294122caa505ae9a /core/test | |
parent | 2cf18fd299ea0fc270b213343314cab652cac271 (diff) | |
download | tangerine-consensus-8ef4fc213703620fbfa13890dee042d40eea8545.tar.gz tangerine-consensus-8ef4fc213703620fbfa13890dee042d40eea8545.tar.zst tangerine-consensus-8ef4fc213703620fbfa13890dee042d40eea8545.zip |
core: switch round by block height (#450)
Diffstat (limited to 'core/test')
-rw-r--r-- | core/test/app.go | 24 | ||||
-rw-r--r-- | core/test/governance.go | 2 | ||||
-rw-r--r-- | core/test/governance_test.go | 16 | ||||
-rw-r--r-- | core/test/network.go | 2 | ||||
-rw-r--r-- | core/test/network_test.go | 14 | ||||
-rw-r--r-- | core/test/state-change-request.go | 16 | ||||
-rw-r--r-- | core/test/state-change-request_test.go | 6 | ||||
-rw-r--r-- | core/test/state.go | 47 | ||||
-rw-r--r-- | core/test/state_test.go | 17 |
9 files changed, 36 insertions, 108 deletions
diff --git a/core/test/app.go b/core/test/app.go index 1ce5b84..516974c 100644 --- a/core/test/app.go +++ b/core/test/app.go @@ -77,7 +77,7 @@ type AppDeliveredRecord struct { // App implements Application interface for testing purpose. type App struct { Confirmed map[common.Hash]*types.Block - LastConfirmedHeights map[uint32]uint64 + LastConfirmedHeight uint64 confirmedLock sync.RWMutex Delivered map[common.Hash]*AppDeliveredRecord DeliverSequence common.Hashes @@ -92,12 +92,11 @@ type App struct { // NewApp constructs a TestApp instance. func NewApp(initRound uint64, gov *Governance) (app *App) { app = &App{ - Confirmed: make(map[common.Hash]*types.Block), - LastConfirmedHeights: make(map[uint32]uint64), - Delivered: make(map[common.Hash]*AppDeliveredRecord), - DeliverSequence: common.Hashes{}, - gov: gov, - roundToNotify: initRound, + Confirmed: make(map[common.Hash]*types.Block), + Delivered: make(map[common.Hash]*AppDeliveredRecord), + DeliverSequence: common.Hashes{}, + gov: gov, + roundToNotify: initRound, } if gov != nil { app.state = gov.State() @@ -171,8 +170,7 @@ func (app *App) VerifyBlock(block *types.Block) types.BlockVerifyStatus { // verify the next block in a given chain. app.confirmedLock.RLock() defer app.confirmedLock.RUnlock() - if app.LastConfirmedHeights[block.Position.ChainID]+1 != - block.Position.Height { + if app.LastConfirmedHeight+1 != block.Position.Height { return types.VerifyRetryLater } } @@ -185,13 +183,11 @@ func (app *App) BlockConfirmed(b types.Block) { defer app.confirmedLock.Unlock() app.Confirmed[b.Hash] = &b if b.Position.Height != 0 { - if h, exists := app.LastConfirmedHeights[b.Position.ChainID]; exists { - if h+1 != b.Position.Height { - panic(ErrConfirmedHeightNotIncreasing) - } + if app.LastConfirmedHeight+1 != b.Position.Height { + panic(ErrConfirmedHeightNotIncreasing) } } - app.LastConfirmedHeights[b.Position.ChainID] = b.Position.Height + app.LastConfirmedHeight = b.Position.Height } // BlockDelivered implements Application interface. diff --git a/core/test/governance.go b/core/test/governance.go index 81ced54..d540280 100644 --- a/core/test/governance.go +++ b/core/test/governance.go @@ -382,7 +382,7 @@ func (g *Governance) Equal(other *Governance, checkState bool) bool { // NOTE: this function should be called before running. func (g *Governance) RegisterConfigChange( round uint64, t StateChangeType, v interface{}) (err error) { - if t < StateChangeNumChains || t > StateChangeDKGSetSize { + if t < StateAddCRS || t > StateChangeDKGSetSize { return fmt.Errorf("state changes to register is not supported: %v", t) } if round < 2 { diff --git a/core/test/governance_test.go b/core/test/governance_test.go index cef2aea..e0a2365 100644 --- a/core/test/governance_test.go +++ b/core/test/governance_test.go @@ -75,16 +75,16 @@ func (s *GovernanceTestSuite) TestRegisterChange() { genesisNodes, 100*time.Millisecond, &common.NullLogger{}, true), 2) req.NoError(err) // Unable to register change for genesis round. - req.Error(g.RegisterConfigChange(0, StateChangeNumChains, uint32(32))) + req.Error(g.RegisterConfigChange(0, StateChangeDKGSetSize, uint32(32))) // Make some round prepared. g.CatchUpWithRound(4) - req.Equal(g.Configuration(4).NumChains, uint32(20)) + req.Equal(g.Configuration(4).DKGSetSize, uint32(20)) // Unable to register change for prepared round. - req.Error(g.RegisterConfigChange(4, StateChangeNumChains, uint32(32))) + req.Error(g.RegisterConfigChange(4, StateChangeDKGSetSize, uint32(32))) // It's ok to make some change when condition is met. - req.NoError(g.RegisterConfigChange(5, StateChangeNumChains, uint32(32))) - req.NoError(g.RegisterConfigChange(6, StateChangeNumChains, uint32(32))) - req.NoError(g.RegisterConfigChange(7, StateChangeNumChains, uint32(40))) + req.NoError(g.RegisterConfigChange(5, StateChangeDKGSetSize, uint32(32))) + req.NoError(g.RegisterConfigChange(6, StateChangeDKGSetSize, uint32(32))) + req.NoError(g.RegisterConfigChange(7, StateChangeDKGSetSize, uint32(40))) // In local mode, state for round 6 would be ready after notified with // round 2. g.NotifyRound(2) @@ -94,8 +94,8 @@ func (s *GovernanceTestSuite) TestRegisterChange() { g.NotifyRound(4) // Notify governance to take a snapshot for round 7's configuration. g.NotifyRound(5) - req.Equal(g.Configuration(6).NumChains, uint32(32)) - req.Equal(g.Configuration(7).NumChains, uint32(40)) + req.Equal(g.Configuration(6).DKGSetSize, uint32(32)) + req.Equal(g.Configuration(7).DKGSetSize, uint32(40)) } func TestGovernance(t *testing.T) { diff --git a/core/test/network.go b/core/test/network.go index f5d1c6e..0bbb12e 100644 --- a/core/test/network.go +++ b/core/test/network.go @@ -735,7 +735,7 @@ func (n *Network) getNotarySet(round uint64) map[types.NodeID]struct{} { set, exists := n.notarySetCaches[round] if !exists { var err error - set, err = n.cache.GetNotarySet(round, 0) + set, err = n.cache.GetNotarySet(round) if err != nil { panic(err) } diff --git a/core/test/network_test.go b/core/test/network_test.go index fd5f97f..22c31a8 100644 --- a/core/test/network_test.go +++ b/core/test/network_test.go @@ -90,9 +90,8 @@ func (s *NetworkTestSuite) TestPullRequestMarshaling() { Requester: GenerateRandomNodeIDs(1)[0], Type: "vote", Identity: types.Position{ - Round: 1, - ChainID: 2, - Height: 3, + Round: 1, + Height: 3, }} b, err = json.Marshal(req) s.Require().NoError(err) @@ -102,8 +101,6 @@ func (s *NetworkTestSuite) TestPullRequestMarshaling() { s.Require().Equal(req.Type, req2.Type) s.Require().Equal(req.Identity.(types.Position).Round, req.Identity.(types.Position).Round) - s.Require().Equal(req.Identity.(types.Position).ChainID, - req.Identity.(types.Position).ChainID) s.Require().Equal(req.Identity.(types.Position).Height, req.Identity.(types.Position).Height) } @@ -197,9 +194,8 @@ func (s *NetworkTestSuite) TestPullVotes() { v := types.NewVote( types.VoteInit, common.NewRandomHash(), randObj.Uint64()) v.Position = types.Position{ - ChainID: randObj.Uint32(), - Height: randObj.Uint64(), - Round: uint64(randObj.Intn(int(maxRound + 1))), + Height: randObj.Uint64(), + Round: uint64(randObj.Intn(int(maxRound + 1))), } req.NoError(master.trans.Send(n.ID, v)) votes[v.VoteHeader] = v @@ -262,7 +258,7 @@ func (s *NetworkTestSuite) TestBroadcastToSet() { dkgSet, err := cache.GetDKGSet(0) req.NoError(err) req.Len(dkgSet, 1) - notarySet, err := cache.GetNotarySet(0, 0) + notarySet, err := cache.GetNotarySet(0) req.NoError(err) req.Len(notarySet, 1) var ( diff --git a/core/test/state-change-request.go b/core/test/state-change-request.go index 21e623b..5b19859 100644 --- a/core/test/state-change-request.go +++ b/core/test/state-change-request.go @@ -19,7 +19,6 @@ package test import ( "fmt" - "math" "time" "github.com/dexon-foundation/dexon-consensus/common" @@ -42,13 +41,10 @@ const ( StateAddDKGMPKReady StateAddDKGFinal // Configuration related. - StateChangeNumChains StateChangeLambdaBA StateChangeLambdaDKG StateChangeRoundInterval StateChangeMinBlockInterval - StateChangeK - StateChangePhiRatio StateChangeNotarySetSize StateChangeDKGSetSize // Node set related. @@ -69,8 +65,6 @@ func (t StateChangeType) String() string { return "AddDKGMPKReady" case StateAddDKGFinal: return "AddDKGFinal" - case StateChangeNumChains: - return "ChangeNumChains" case StateChangeLambdaBA: return "ChangeLambdaBA" case StateChangeLambdaDKG: @@ -79,10 +73,6 @@ func (t StateChangeType) String() string { return "ChangeRoundInterval" case StateChangeMinBlockInterval: return "ChangeMinBlockInterval" - case StateChangeK: - return "ChangeK" - case StateChangePhiRatio: - return "ChangePhiRatio" case StateChangeNotarySetSize: return "ChangeNotarySetSize" case StateChangeDKGSetSize: @@ -194,8 +184,6 @@ func (req *StateChangeRequest) String() (ret string) { ret += fmt.Sprintf("%s", req.Payload.(*typesDKG.MPKReady)) case StateAddDKGFinal: ret += fmt.Sprintf("%s", req.Payload.(*typesDKG.Finalize)) - case StateChangeNumChains: - ret += fmt.Sprintf("%v", req.Payload.(uint32)) case StateChangeLambdaBA: ret += fmt.Sprintf("%v", time.Duration(req.Payload.(uint64))) case StateChangeLambdaDKG: @@ -204,10 +192,6 @@ func (req *StateChangeRequest) String() (ret string) { ret += fmt.Sprintf("%v", time.Duration(req.Payload.(uint64))) case StateChangeMinBlockInterval: ret += fmt.Sprintf("%v", time.Duration(req.Payload.(uint64))) - case StateChangeK: - ret += fmt.Sprintf("%v", req.Payload.(uint64)) - case StateChangePhiRatio: - ret += fmt.Sprintf("%v", math.Float32frombits(req.Payload.(uint32))) case StateChangeNotarySetSize: ret += fmt.Sprintf("%v", req.Payload.(uint32)) case StateChangeDKGSetSize: diff --git a/core/test/state-change-request_test.go b/core/test/state-change-request_test.go index 658a9fb..eeba4c4 100644 --- a/core/test/state-change-request_test.go +++ b/core/test/state-change-request_test.go @@ -30,8 +30,8 @@ type StateChangeRequestTestSuite struct { func (s *StateChangeRequestTestSuite) TestEqual() { // Basically, only the cloned one would be equal. - st00 := NewStateChangeRequest(StateChangeNumChains, uint32(4)) - st01 := NewStateChangeRequest(StateChangeNumChains, uint32(4)) + st00 := NewStateChangeRequest(StateChangeNotarySetSize, uint32(4)) + st01 := NewStateChangeRequest(StateChangeNotarySetSize, uint32(4)) s.Error(ErrStatePendingChangesNotEqual, st00.Equal(st01)) // Even with identical payload, they would be different. mKey := typesDKG.NewMasterPublicKey() @@ -42,7 +42,7 @@ func (s *StateChangeRequestTestSuite) TestEqual() { func (s *StateChangeRequestTestSuite) TestClone() { // The cloned one should be no error when compared with 'Equal' method. - st00 := NewStateChangeRequest(StateChangeNumChains, uint32(7)) + st00 := NewStateChangeRequest(StateChangeDKGSetSize, uint32(7)) s.NoError(st00.Equal(st00.Clone())) st10 := NewStateChangeRequest( StateAddDKGMasterPublicKey, typesDKG.NewMasterPublicKey()) diff --git a/core/test/state.go b/core/test/state.go index a5a285b..27ff87a 100644 --- a/core/test/state.go +++ b/core/test/state.go @@ -20,7 +20,6 @@ package test import ( "bytes" "errors" - "math" "sort" "sync" "time" @@ -86,14 +85,11 @@ type crsAdditionRequest struct { // State emulates what the global state in governace contract on a fullnode. type State struct { // Configuration related. - numChains uint32 lambdaBA time.Duration lambdaDKG time.Duration - k int - phiRatio float32 notarySetSize uint32 dkgSetSize uint32 - roundInterval time.Duration + roundInterval uint64 minBlockInterval time.Duration // Nodes nodes map[types.NodeID]crypto.PublicKey @@ -129,15 +125,12 @@ func NewState( return &State{ local: local, logger: logger, - numChains: uint32(len(nodes)), lambdaBA: lambda, lambdaDKG: lambda * 10, - roundInterval: lambda * 10000, + roundInterval: 1000, minBlockInterval: 4 * lambda, crs: []common.Hash{genesisCRS}, nodes: nodes, - phiRatio: 0.667, - k: 0, notarySetSize: uint32(len(nodes)), dkgSetSize: uint32(len(nodes)), ownRequests: make(map[common.Hash]*StateChangeRequest), @@ -173,11 +166,8 @@ func (s *State) Snapshot() (*types.Config, []crypto.PublicKey) { nodes = append(nodes, key) } cfg := &types.Config{ - NumChains: s.numChains, LambdaBA: s.lambdaBA, LambdaDKG: s.lambdaDKG, - K: s.k, - PhiRatio: s.phiRatio, NotarySetSize: s.notarySetSize, DKGSetSize: s.dkgSetSize, RoundInterval: s.roundInterval, @@ -210,10 +200,6 @@ func (s *State) unpackPayload( case StateAddDKGFinal: v = &typesDKG.Finalize{} err = rlp.DecodeBytes(raw.Payload, v) - case StateChangeNumChains: - var tmp uint32 - err = rlp.DecodeBytes(raw.Payload, &tmp) - v = tmp case StateChangeLambdaBA: var tmp uint64 err = rlp.DecodeBytes(raw.Payload, &tmp) @@ -230,14 +216,6 @@ func (s *State) unpackPayload( var tmp uint64 err = rlp.DecodeBytes(raw.Payload, &tmp) v = tmp - case StateChangeK: - var tmp uint64 - err = rlp.DecodeBytes(raw.Payload, &tmp) - v = tmp - case StateChangePhiRatio: - var tmp uint32 - err = rlp.DecodeBytes(raw.Payload, &tmp) - v = tmp case StateChangeNotarySetSize: var tmp uint32 err = rlp.DecodeBytes(raw.Payload, &tmp) @@ -284,11 +262,8 @@ func (s *State) unpackRequests( // Equal checks equality between State instance. func (s *State) Equal(other *State) error { // Check configuration part. - configEqual := s.numChains == other.numChains && - s.lambdaBA == other.lambdaBA && + configEqual := s.lambdaBA == other.lambdaBA && s.lambdaDKG == other.lambdaDKG && - s.k == other.k && - s.phiRatio == other.phiRatio && s.notarySetSize == other.notarySetSize && s.dkgSetSize == other.dkgSetSize && s.roundInterval == other.roundInterval && @@ -447,11 +422,8 @@ func (s *State) Equal(other *State) error { func (s *State) Clone() (copied *State) { // Clone configuration parts. copied = &State{ - numChains: s.numChains, lambdaBA: s.lambdaBA, lambdaDKG: s.lambdaDKG, - k: s.k, - phiRatio: s.phiRatio, notarySetSize: s.notarySetSize, dkgSetSize: s.dkgSetSize, roundInterval: s.roundInterval, @@ -730,20 +702,14 @@ func (s *State) applyRequest(req *StateChangeRequest) error { s.dkgFinals[final.Round] = make(map[types.NodeID]*typesDKG.Finalize) } s.dkgFinals[final.Round][final.ProposerID] = final - case StateChangeNumChains: - s.numChains = req.Payload.(uint32) case StateChangeLambdaBA: s.lambdaBA = time.Duration(req.Payload.(uint64)) case StateChangeLambdaDKG: s.lambdaDKG = time.Duration(req.Payload.(uint64)) case StateChangeRoundInterval: - s.roundInterval = time.Duration(req.Payload.(uint64)) + s.roundInterval = req.Payload.(uint64) case StateChangeMinBlockInterval: s.minBlockInterval = time.Duration(req.Payload.(uint64)) - case StateChangeK: - s.k = int(req.Payload.(uint64)) - case StateChangePhiRatio: - s.phiRatio = math.Float32frombits(req.Payload.(uint32)) case StateChangeNotarySetSize: s.notarySetSize = req.Payload.(uint32) case StateChangeDKGSetSize: @@ -773,13 +739,8 @@ func (s *State) RequestChange( payload = payload.(crypto.PublicKey).Bytes() case StateChangeLambdaBA, StateChangeLambdaDKG, - StateChangeRoundInterval, StateChangeMinBlockInterval: payload = uint64(payload.(time.Duration)) - case StateChangeK: - payload = uint64(payload.(int)) - case StateChangePhiRatio: - payload = math.Float32bits(payload.(float32)) // These cases for for type assertion, make sure callers pass expected types. case StateAddCRS: payload = payload.(*crsAdditionRequest) diff --git a/core/test/state_test.go b/core/test/state_test.go index 79a7a85..d92a8c1 100644 --- a/core/test/state_test.go +++ b/core/test/state_test.go @@ -135,26 +135,20 @@ func (s *StateTestSuite) makeDKGChanges( } func (s *StateTestSuite) makeConfigChanges(st *State) { - st.RequestChange(StateChangeNumChains, uint32(7)) st.RequestChange(StateChangeLambdaBA, time.Nanosecond) st.RequestChange(StateChangeLambdaDKG, time.Millisecond) - st.RequestChange(StateChangeRoundInterval, time.Hour) + st.RequestChange(StateChangeRoundInterval, uint64(1001)) st.RequestChange(StateChangeMinBlockInterval, time.Second) - st.RequestChange(StateChangeK, 1) - st.RequestChange(StateChangePhiRatio, float32(0.5)) st.RequestChange(StateChangeNotarySetSize, uint32(5)) st.RequestChange(StateChangeDKGSetSize, uint32(6)) } func (s *StateTestSuite) checkConfigChanges(config *types.Config) { req := s.Require() - req.Equal(config.NumChains, uint32(7)) req.Equal(config.LambdaBA, time.Nanosecond) req.Equal(config.LambdaDKG, time.Millisecond) - req.Equal(config.RoundInterval, time.Hour) + req.Equal(config.RoundInterval, uint64(1001)) req.Equal(config.MinBlockInterval, time.Second) - req.Equal(config.K, 1) - req.Equal(config.PhiRatio, float32(0.5)) req.Equal(config.NotarySetSize, uint32(5)) req.Equal(config.DKGSetSize, uint32(6)) } @@ -210,7 +204,7 @@ func (s *StateTestSuite) TestEqual() { // Switch to remote mode. st.SwitchToRemoteMode() // Make some change. - req.NoError(st.RequestChange(StateChangeK, int(5))) + req.NoError(st.RequestChange(StateChangeNotarySetSize, uint32(100))) st6 := st.Clone() req.NoError(st.Equal(st6)) // Remove the pending change, should not be equal. @@ -254,14 +248,11 @@ func (s *StateTestSuite) TestLocalMode() { config1, nodes1 := st.Snapshot() req.True(s.compareNodes(genesisNodes, nodes1)) // Check settings of config1 affected by genesisNodes and lambda. - req.Equal(config1.NumChains, uint32(len(genesisNodes))) req.Equal(config1.LambdaBA, lambda) req.Equal(config1.LambdaDKG, lambda*10) - req.Equal(config1.RoundInterval, lambda*10000) + req.Equal(config1.RoundInterval, uint64(1000)) req.Equal(config1.NotarySetSize, uint32(len(genesisNodes))) req.Equal(config1.DKGSetSize, uint32(len(genesisNodes))) - req.Equal(config1.K, 0) - req.Equal(config1.PhiRatio, float32(0.667)) // Request some changes, every fields for config should be affected. s.makeConfigChanges(st) // Add new node. |