diff options
author | haoping-ku <haoping.ku@dexon.org> | 2018-11-08 14:41:47 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-08 14:41:47 +0800 |
commit | 57e2b93fc2fe7fb04374c9e9b6381791aeeffa34 (patch) | |
tree | 3c110d4364ad9e916ea5953873794d49a62a76e7 | |
parent | 6934243d0690384363a3cf9cd11d8a45c2ce15ac (diff) | |
download | tangerine-consensus-57e2b93fc2fe7fb04374c9e9b6381791aeeffa34.tar.gz tangerine-consensus-57e2b93fc2fe7fb04374c9e9b6381791aeeffa34.tar.zst tangerine-consensus-57e2b93fc2fe7fb04374c9e9b6381791aeeffa34.zip |
core: lattice, total-ordering: remove newGenesisConfig (#308)
* core: lattice, total-ordering: remove newGenesisConfig
* fixup
-rw-r--r-- | core/lattice-data.go | 20 | ||||
-rw-r--r-- | core/lattice-data_test.go | 17 | ||||
-rw-r--r-- | core/lattice.go | 6 | ||||
-rw-r--r-- | core/lattice_test.go | 23 | ||||
-rw-r--r-- | core/total-ordering-syncer_test.go | 18 | ||||
-rw-r--r-- | core/total-ordering.go | 18 | ||||
-rw-r--r-- | core/total-ordering_test.go | 174 |
7 files changed, 117 insertions, 159 deletions
diff --git a/core/lattice-data.go b/core/lattice-data.go index e671c63..ce6d32b 100644 --- a/core/lattice-data.go +++ b/core/lattice-data.go @@ -94,18 +94,6 @@ func (config *latticeDataConfig) isValidGenesisBlockTime(b *types.Block) bool { return !b.Timestamp.Before(config.roundBeginTime) } -// newGenesisLatticeDataConfig constructs a latticeDataConfig instance. -func newGenesisLatticeDataConfig( - dMoment time.Time, - round uint64, - config *types.Config) *latticeDataConfig { - - c := &latticeDataConfig{} - c.fromConfig(round, config) - c.setRoundBeginTime(dMoment) - return c -} - // newLatticeDataConfig constructs a latticeDataConfig instance. func newLatticeDataConfig( prev *latticeDataConfig, cur *types.Config) *latticeDataConfig { @@ -129,8 +117,14 @@ type latticeData struct { // newLatticeData creates a new latticeData instance. func newLatticeData( - db blockdb.Reader, genesisConfig *latticeDataConfig) (data *latticeData) { + db blockdb.Reader, + dMoment time.Time, + round uint64, + config *types.Config) (data *latticeData) { + genesisConfig := &latticeDataConfig{} + genesisConfig.fromConfig(round, config) + genesisConfig.setRoundBeginTime(dMoment) data = &latticeData{ db: db, chains: make([]*chainStatus, genesisConfig.numChains), diff --git a/core/lattice-data_test.go b/core/lattice-data_test.go index 6a53c18..e4a894f 100644 --- a/core/lattice-data_test.go +++ b/core/lattice-data_test.go @@ -61,7 +61,7 @@ func (s *LatticeDataTestSuite) genTestCase1() ( } db, err := blockdb.NewMemBackedBlockDB() req.NoError(err) - data = newLatticeData(db, newGenesisLatticeDataConfig(now, 0, genesisConfig)) + data = newLatticeData(db, now, 0, genesisConfig) config := &types.Config{ RoundInterval: 1000 * time.Second, NumChains: chainNum, @@ -387,8 +387,7 @@ func (s *LatticeDataTestSuite) TestRandomlyGeneratedBlocks() { for i := 0; i < repeat; i++ { db, err := blockdb.NewMemBackedBlockDB() req.NoError(err) - data := newLatticeData( - db, newGenesisLatticeDataConfig(genesisTime, 0, genesisConfig)) + data := newLatticeData(db, genesisTime, 0, genesisConfig) deliveredHashes := common.Hashes{} revealedHashes := common.Hashes{} revealer.Reset() @@ -477,8 +476,7 @@ func (s *LatticeDataTestSuite) TestPrepareBlock() { } db, err := blockdb.NewMemBackedBlockDB() req.NoError(err) - data := newLatticeData( - db, newGenesisLatticeDataConfig(time.Now().UTC(), 0, genesisConfig)) + data := newLatticeData(db, time.Now().UTC(), 0, genesisConfig) // Setup genesis blocks. b00 := s.prepareGenesisBlock(0) time.Sleep(minInterval) @@ -563,8 +561,7 @@ func (s *LatticeDataTestSuite) TestNextPosition() { NumChains: 4, MinBlockInterval: 1 * time.Second, } - data = newLatticeData( - nil, newGenesisLatticeDataConfig(time.Now().UTC(), 0, genesisConfig)) + data = newLatticeData(nil, time.Now().UTC(), 0, genesisConfig) s.Equal(data.nextPosition(0), types.Position{ChainID: 0, Height: 0}) } @@ -617,8 +614,7 @@ func (s *LatticeDataTestSuite) TestNumChainsChange() { db, err := blockdb.NewMemBackedBlockDB() req.NoError(err) // Set up latticeData instance. - lattice := newLatticeData(db, newGenesisLatticeDataConfig( - time.Now().UTC(), 0, configs[0])) + lattice := newLatticeData(db, time.Now().UTC(), 0, configs[0]) req.NoError(lattice.appendConfig(1, configs[1])) req.NoError(lattice.appendConfig(2, configs[2])) req.NoError(lattice.appendConfig(3, configs[3])) @@ -690,8 +686,7 @@ func (s *LatticeDataTestSuite) TestAppendConfig() { ) db, err := blockdb.NewMemBackedBlockDB() req.NoError(err) - dataConfig := newGenesisLatticeDataConfig(now, round, cfg) - latticeData := newLatticeData(db, dataConfig) + latticeData := newLatticeData(db, now, round, cfg) err = latticeData.appendConfig(6, cfg) req.NoError(err) err = latticeData.appendConfig(10, cfg) diff --git a/core/lattice.go b/core/lattice.go index 6c69d52..20e16f2 100644 --- a/core/lattice.go +++ b/core/lattice.go @@ -58,15 +58,13 @@ func NewLattice( logger common.Logger) *Lattice { // Create genesis latticeDataConfig. - dataConfig := newGenesisLatticeDataConfig(dMoment, round, cfg) - toConfig := newGenesisTotalOrderingConfig(dMoment, cfg) return &Lattice{ authModule: authModule, app: app, debug: debug, pool: newBlockPool(cfg.NumChains), - data: newLatticeData(db, dataConfig), - toModule: newTotalOrdering(toConfig), + data: newLatticeData(db, dMoment, round, cfg), + toModule: newTotalOrdering(dMoment, cfg), ctModule: newConsensusTimestamp(dMoment, round, cfg.NumChains), logger: logger, } diff --git a/core/lattice_test.go b/core/lattice_test.go index c06ac57..23a2814 100644 --- a/core/lattice_test.go +++ b/core/lattice_test.go @@ -156,19 +156,19 @@ func (s *LatticeTestSuite) TestBasicUsage() { ) // Master-lattice generates blocks. for i := uint32(0); i < chainNum; i++ { - // Produce genesis blocks should be delivered before all other blocks, + // Produced genesis blocks should be delivered before all other blocks, // or the consensus time would be wrong. b, err := master.prepareBlock(i) req.NotNil(b) req.NoError(err) - // We've ignored the error for "acking blocks don't exist". + // Ignore error "acking blocks don't exist". req.NoError(master.processBlock(b)) } for i := 0; i < (blockNum - int(chainNum)); i++ { b, err := master.prepareBlock(uint32(rand.Intn(int(chainNum)))) req.NotNil(b) req.NoError(err) - // We've ignored the error for "acking blocks don't exist". + // Ignore error "acking blocks don't exist". req.NoError(master.processBlock(b)) } // Now we have some blocks, replay them on different lattices. @@ -212,9 +212,9 @@ func (s *LatticeTestSuite) TestBasicUsage() { } func (s *LatticeTestSuite) TestSync() { - // One Lattice prepare blocks on chains randomly selected each time - // and process it. Those generated blocks and kept into a buffer, and - // process by other Lattice instances with random order. + // A Lattice prepares blocks on chains randomly selected each time and + // processes them. Those generated blocks are kept into a buffer, and + // processed by other Lattice instances with random order. var ( chainNum = uint32(19) otherLatticeNum = 50 @@ -241,28 +241,27 @@ func (s *LatticeTestSuite) TestSync() { MinBlockInterval: 0, RoundInterval: time.Hour, } - dMoment = time.Now().UTC() - master = s.newTestLatticeMgr(&cfg, dMoment) - //apps = []*test.App{master.app} + dMoment = time.Now().UTC() + master = s.newTestLatticeMgr(&cfg, dMoment) revealSeq = map[string]struct{}{} ) // Make sure the test setup is correct. req.True(syncNum > desyncNum) // Master-lattice generates blocks. for i := uint32(0); i < chainNum; i++ { - // Produce genesis blocks should be delivered before all other blocks, + // Produced genesis blocks should be delivered before all other blocks, // or the consensus time would be wrong. b, err := master.prepareBlock(i) req.NotNil(b) req.NoError(err) - // We've ignored the error for "acking blocks don't exist". + // Ignore error "acking blocks don't exist". req.NoError(master.processBlock(b)) } for i := 0; i < (blockNum - int(chainNum)); i++ { b, err := master.prepareBlock(uint32(rand.Intn(int(chainNum)))) req.NotNil(b) req.NoError(err) - // We've ignored the error for "acking blocks don't exist". + // Ignore error "acking blocks don't exist". req.NoError(master.processBlock(b)) } req.NoError(master.app.Verify()) diff --git a/core/total-ordering-syncer_test.go b/core/total-ordering-syncer_test.go index e20b438..5f9fd0f 100644 --- a/core/total-ordering-syncer_test.go +++ b/core/total-ordering-syncer_test.go @@ -35,20 +35,16 @@ type TotalOrderingSyncerTestSuite struct { func (s *TotalOrderingSyncerTestSuite) genDeliverySet(numChains uint32) ( deliverySet [][]*types.Block, revealer *test.RandomDAGRevealer) { - phi := uint64(numChains) / 2 genesisTime := time.Now().UTC() - - genesisConfig := &totalOrderingConfig{ - roundBasedConfig: roundBasedConfig{ - roundInterval: 1000 * time.Second, - }, - k: 0, - phi: phi, - numChains: numChains, + genesisConfig := &types.Config{ + K: 0, + PhiRatio: 0.5, + NumChains: numChains, + RoundInterval: 1000 * time.Second, } - genesisConfig.setRoundBeginTime(genesisTime) - to := newTotalOrdering(genesisConfig) + + to := newTotalOrdering(genesisTime, genesisConfig) gen := test.NewBlocksGenerator(&test.BlocksGeneratorConfig{ NumChains: numChains, diff --git a/core/total-ordering.go b/core/total-ordering.go index 9b980c0..90848ce 100644 --- a/core/total-ordering.go +++ b/core/total-ordering.go @@ -58,6 +58,8 @@ var ( ErrForwardAck = errors.New("forward ack") // ErrUnexpected means general (I'm lazy) errors. ErrUnexpected = errors.New("unexpected") + // ErrTotalOrderingPhiRatio means invalid phi ratio + ErrTotalOrderingPhiRatio = errors.New("invalid total ordering phi ratio") ) // totalOrderingConfig is the configuration for total ordering. @@ -83,14 +85,6 @@ func (config *totalOrderingConfig) fromConfig(round uint64, cfg *types.Config) { config.setupRoundBasedFields(round, cfg) } -func newGenesisTotalOrderingConfig( - dMoment time.Time, config *types.Config) *totalOrderingConfig { - c := &totalOrderingConfig{} - c.fromConfig(0, config) - c.setRoundBeginTime(dMoment) - return c -} - func newTotalOrderingConfig( prev *totalOrderingConfig, cur *types.Config) *totalOrderingConfig { c := &totalOrderingConfig{} @@ -798,7 +792,10 @@ type totalOrdering struct { } // newTotalOrdering constructs an totalOrdering instance. -func newTotalOrdering(config *totalOrderingConfig) *totalOrdering { +func newTotalOrdering(dMoment time.Time, cfg *types.Config) *totalOrdering { + config := &totalOrderingConfig{} + config.fromConfig(0, cfg) + config.setRoundBeginTime(dMoment) candidates := make([]*totalOrderingCandidateInfo, config.numChains) to := &totalOrdering{ pendings: make(map[common.Hash]*types.Block), @@ -822,6 +819,9 @@ func (to *totalOrdering) appendConfig( if round != uint64(len(to.configs))+to.configs[0].roundID { return ErrRoundNotIncreasing } + if config.PhiRatio < 0.5 || config.PhiRatio > 1.0 { + return ErrTotalOrderingPhiRatio + } to.configs = append( to.configs, newTotalOrderingConfig(to.configs[len(to.configs)-1], config)) diff --git a/core/total-ordering_test.go b/core/total-ordering_test.go index d127301..ac8956a 100644 --- a/core/total-ordering_test.go +++ b/core/total-ordering_test.go @@ -150,15 +150,14 @@ func (s *TotalOrderingTestSuite) TestBlockRelation() { Acks: common.NewSortedHashes(common.Hashes{blockB.Hash}), } - genesisConfig := &totalOrderingConfig{ - roundBasedConfig: roundBasedConfig{ - roundInterval: 1000 * time.Second, - }, - k: 1, - phi: 3, - numChains: uint32(len(nodes)), + genesisConfig := &types.Config{ + RoundInterval: 1000 * time.Second, + K: 1, + PhiRatio: 0.6, + NumChains: uint32(len(nodes)), } - to := newTotalOrdering(genesisConfig) + genesisTime := time.Now().UTC() + to := newTotalOrdering(genesisTime, genesisConfig) s.checkNotDeliver(to, blockA) s.checkNotDeliver(to, blockB) s.checkNotDeliver(to, blockC) @@ -339,15 +338,14 @@ func (s *TotalOrderingTestSuite) TestCycleDetection() { b10.Acks = append(b10.Acks, b10.Hash) // Make sure we won't hang when cycle exists. - genesisConfig := &totalOrderingConfig{ - roundBasedConfig: roundBasedConfig{ - roundInterval: 1000 * time.Second, - }, - k: 1, - phi: 3, - numChains: uint32(len(nodes)), + genesisConfig := &types.Config{ + RoundInterval: 1000 * time.Second, + K: 1, + PhiRatio: 0.6, + NumChains: uint32(len(nodes)), } - to := newTotalOrdering(genesisConfig) + genesisTime := time.Now().UTC() + to := newTotalOrdering(genesisTime, genesisConfig) s.checkNotDeliver(to, b00) s.checkNotDeliver(to, b01) s.checkNotDeliver(to, b02) @@ -370,15 +368,14 @@ func (s *TotalOrderingTestSuite) TestEarlyDeliver() { // Even when B is not received, A should // be able to be delivered. nodes := test.GenerateRandomNodeIDs(5) - genesisConfig := &totalOrderingConfig{ - roundBasedConfig: roundBasedConfig{ - roundInterval: 1000 * time.Second, - }, - k: 2, - phi: 3, - numChains: uint32(len(nodes)), + genesisConfig := &types.Config{ + RoundInterval: 1000 * time.Second, + K: 2, + PhiRatio: 0.6, + NumChains: uint32(len(nodes)), } - to := newTotalOrdering(genesisConfig) + genesisTime := time.Now().UTC() + to := newTotalOrdering(genesisTime, genesisConfig) genNextBlock := func(b *types.Block) *types.Block { return &types.Block{ ProposerID: b.ProposerID, @@ -483,15 +480,14 @@ func (s *TotalOrderingTestSuite) TestEarlyDeliver() { func (s *TotalOrderingTestSuite) TestBasicCaseForK2() { // It's a handcrafted test case. nodes := test.GenerateRandomNodeIDs(5) - genesisConfig := &totalOrderingConfig{ - roundBasedConfig: roundBasedConfig{ - roundInterval: 1000 * time.Second, - }, - k: 2, - phi: 3, - numChains: uint32(len(nodes)), + genesisConfig := &types.Config{ + RoundInterval: 1000 * time.Second, + K: 2, + PhiRatio: 0.6, + NumChains: uint32(len(nodes)), } - to := newTotalOrdering(genesisConfig) + genesisTime := time.Now().UTC() + to := newTotalOrdering(genesisTime, genesisConfig) // Setup blocks. b00 := s.genGenesisBlock(nodes, 0, common.Hashes{}) b10 := s.genGenesisBlock(nodes, 1, common.Hashes{}) @@ -826,16 +822,15 @@ func (s *TotalOrderingTestSuite) TestBasicCaseForK0() { // o o o <- o Height: 0 var ( nodes = test.GenerateRandomNodeIDs(5) - genesisConfig = &totalOrderingConfig{ - roundBasedConfig: roundBasedConfig{ - roundInterval: 1000 * time.Second, - }, - k: 0, - phi: 3, - numChains: uint32(len(nodes)), + genesisConfig = &types.Config{ + RoundInterval: 1000 * time.Second, + K: 0, + PhiRatio: 0.6, + NumChains: uint32(len(nodes)), } - req = s.Require() - to = newTotalOrdering(genesisConfig) + req = s.Require() + genesisTime = time.Now().UTC() + to = newTotalOrdering(genesisTime, genesisConfig) ) // Setup blocks. b00 := s.genGenesisBlock(nodes, 0, common.Hashes{}) @@ -970,13 +965,14 @@ func (s *TotalOrderingTestSuite) baseTestRandomlyGeneratedBlocks( func (s *TotalOrderingTestSuite) TestRandomlyGeneratedBlocks() { var ( - numChains = uint32(20) - phi = uint64(10) - repeat = 15 + numChains = uint32(20) + phi = float32(0.5) + repeat = 15 + genesisTime = time.Now().UTC() ) if testing.Short() { numChains = 10 - phi = 5 + phi = 0.5 repeat = 3 } @@ -990,15 +986,13 @@ func (s *TotalOrderingTestSuite) TestRandomlyGeneratedBlocks() { for _, gen := range ackingCountGenerators { // Test for K=0. constructor := func(numChains uint32) *totalOrdering { - genesisConfig := &totalOrderingConfig{ - roundBasedConfig: roundBasedConfig{ - roundInterval: 1000 * time.Second, - }, - k: 0, - phi: phi, - numChains: numChains, + genesisConfig := &types.Config{ + RoundInterval: 1000 * time.Second, + K: 0, + PhiRatio: phi, + NumChains: numChains, } - to := newTotalOrdering(genesisConfig) + to := newTotalOrdering(genesisTime, genesisConfig) // Add config for next round. s.Require().NoError(to.appendConfig(1, &types.Config{ K: 0, @@ -1010,15 +1004,13 @@ func (s *TotalOrderingTestSuite) TestRandomlyGeneratedBlocks() { s.baseTestRandomlyGeneratedBlocks(constructor, numChains, gen, repeat) // Test for K=1. constructor = func(numChains uint32) *totalOrdering { - genesisConfig := &totalOrderingConfig{ - roundBasedConfig: roundBasedConfig{ - roundInterval: 1000 * time.Second, - }, - k: 1, - phi: phi, - numChains: numChains, + genesisConfig := &types.Config{ + RoundInterval: 1000 * time.Second, + K: 1, + PhiRatio: phi, + NumChains: numChains, } - to := newTotalOrdering(genesisConfig) + to := newTotalOrdering(genesisTime, genesisConfig) // Add config for next round. s.Require().NoError(to.appendConfig(1, &types.Config{ K: 1, @@ -1030,15 +1022,13 @@ func (s *TotalOrderingTestSuite) TestRandomlyGeneratedBlocks() { s.baseTestRandomlyGeneratedBlocks(constructor, numChains, gen, repeat) // Test for K=2. constructor = func(numChains uint32) *totalOrdering { - genesisConfig := &totalOrderingConfig{ - roundBasedConfig: roundBasedConfig{ - roundInterval: 1000 * time.Second, - }, - k: 2, - phi: phi, - numChains: numChains, + genesisConfig := &types.Config{ + RoundInterval: 1000 * time.Second, + K: 2, + PhiRatio: phi, + NumChains: numChains, } - to := newTotalOrdering(genesisConfig) + to := newTotalOrdering(genesisTime, genesisConfig) s.Require().NoError(to.appendConfig(1, &types.Config{ K: 2, PhiRatio: 0.5, @@ -1049,15 +1039,13 @@ func (s *TotalOrderingTestSuite) TestRandomlyGeneratedBlocks() { s.baseTestRandomlyGeneratedBlocks(constructor, numChains, gen, repeat) // Test for K=3. constructor = func(numChains uint32) *totalOrdering { - genesisConfig := &totalOrderingConfig{ - roundBasedConfig: roundBasedConfig{ - roundInterval: 1000 * time.Second, - }, - k: 3, - phi: phi, - numChains: numChains, + genesisConfig := &types.Config{ + RoundInterval: 1000 * time.Second, + K: 3, + PhiRatio: phi, + NumChains: numChains, } - to := newTotalOrdering(genesisConfig) + to := newTotalOrdering(genesisTime, genesisConfig) s.Require().NoError(to.appendConfig(1, &types.Config{ K: 3, PhiRatio: 0.5, @@ -1097,8 +1085,7 @@ func (s *TotalOrderingTestSuite) baseTestForRoundChange( revealingSequence := make(map[string]struct{}) orderingSequence := make(map[string]struct{}) for i := 0; i < repeat; i++ { - to := newTotalOrdering( - newGenesisTotalOrderingConfig(genesisTime, configs[0])) + to := newTotalOrdering(genesisTime, configs[0]) for roundID, config := range configs[1:] { req.NoError(to.appendConfig(uint64(roundID+1), config)) } @@ -1219,16 +1206,13 @@ func (s *TotalOrderingTestSuite) TestSync() { revealer, err := test.NewRandomDAGRevealer(iter) req.NoError(err) - genesisConfig := &totalOrderingConfig{ - roundBasedConfig: roundBasedConfig{ - roundInterval: 1000 * time.Second, - }, - k: 0, - phi: uint64(numChains * 2 / 3), - numChains: numChains, + genesisConfig := &types.Config{ + RoundInterval: 1000 * time.Second, + K: 0, + PhiRatio: 0.67, + NumChains: numChains, } - genesisConfig.setRoundBeginTime(genesisTime) - to1 := newTotalOrdering(genesisConfig) + to1 := newTotalOrdering(genesisTime, genesisConfig) s.Require().NoError(to1.appendConfig(1, &types.Config{ K: 0, PhiRatio: 0.5, @@ -1252,7 +1236,7 @@ func (s *TotalOrderingTestSuite) TestSync() { } // Run new total ordering again. offset := len(deliveredBlockSets1) / 2 - to2 := newTotalOrdering(genesisConfig) + to2 := newTotalOrdering(genesisTime, genesisConfig) s.Require().NoError(to2.appendConfig(1, &types.Config{ K: 0, PhiRatio: 0.5, @@ -1284,14 +1268,6 @@ func (s *TotalOrderingTestSuite) TestSyncWithConfigChange() { roundInterval = 30 * time.Second ) - genesisConfig := &totalOrderingConfig{ - roundBasedConfig: roundBasedConfig{roundInterval: roundInterval}, - k: 0, - phi: uint64(19 * 2 / 3), - numChains: uint32(19), - } - genesisConfig.setRoundBeginTime(genesisTime) - // Configs for round change, notice configs[0] is the same as genesisConfig. configs := []*types.Config{ &types.Config{ @@ -1362,7 +1338,7 @@ func (s *TotalOrderingTestSuite) TestSyncWithConfigChange() { blocks = append(blocks, &b) } - to1 := newTotalOrdering(genesisConfig) + to1 := newTotalOrdering(genesisTime, configs[0]) for i, cfg := range configs[1:] { req.NoError(to1.appendConfig(uint64(i+1), cfg)) } @@ -1390,7 +1366,7 @@ func (s *TotalOrderingTestSuite) TestSyncWithConfigChange() { // or nothing is tested. req.True(uint64(0) < offsetRound && offsetRound < uint64(len(configs)-1)) - to2 := newTotalOrdering(genesisConfig) + to2 := newTotalOrdering(genesisTime, configs[0]) for i, cfg := range configs[1:] { req.NoError(to2.appendConfig(uint64(i+1), cfg)) } |