diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-10-18 09:45:05 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-18 09:45:05 +0800 |
commit | 76b84a2bbf8fcab928e8528aa3decb0cb31411e3 (patch) | |
tree | 250772ca5df40e1ab14dd3ad511e72a8b97248ce /core | |
parent | 29f38589a29e434282a783433d9fbb565ce4231b (diff) | |
download | tangerine-consensus-76b84a2bbf8fcab928e8528aa3decb0cb31411e3.tar tangerine-consensus-76b84a2bbf8fcab928e8528aa3decb0cb31411e3.tar.gz tangerine-consensus-76b84a2bbf8fcab928e8528aa3decb0cb31411e3.tar.bz2 tangerine-consensus-76b84a2bbf8fcab928e8528aa3decb0cb31411e3.tar.lz tangerine-consensus-76b84a2bbf8fcab928e8528aa3decb0cb31411e3.tar.xz tangerine-consensus-76b84a2bbf8fcab928e8528aa3decb0cb31411e3.tar.zst tangerine-consensus-76b84a2bbf8fcab928e8528aa3decb0cb31411e3.zip |
core: Add init block to Run() (#221)
Diffstat (limited to 'core')
-rw-r--r-- | core/compaction-chain.go | 15 | ||||
-rw-r--r-- | core/compaction-chain_test.go | 12 | ||||
-rw-r--r-- | core/consensus.go | 3 | ||||
-rw-r--r-- | core/consensus_test.go | 1 | ||||
-rw-r--r-- | core/lattice_test.go | 5 |
5 files changed, 23 insertions, 13 deletions
diff --git a/core/compaction-chain.go b/core/compaction-chain.go index ea26562..ac8c458 100644 --- a/core/compaction-chain.go +++ b/core/compaction-chain.go @@ -30,6 +30,8 @@ import ( var ( ErrBlockNotRegistered = fmt.Errorf( "block not registered") + ErrNotInitiazlied = fmt.Errorf( + "not initialized") ) type compactionChain struct { @@ -49,6 +51,12 @@ func newCompactionChain(gov Governance) *compactionChain { } } +func (cc *compactionChain) init(initBlock *types.Block) { + cc.prevBlockLock.Lock() + defer cc.prevBlockLock.Unlock() + cc.prevBlock = initBlock +} + func (cc *compactionChain) registerBlock(block *types.Block) { if cc.blockRegistered(block.Hash) { return @@ -67,11 +75,10 @@ func (cc *compactionChain) blockRegistered(hash common.Hash) (exist bool) { func (cc *compactionChain) processBlock(block *types.Block) error { prevBlock := cc.lastBlock() - if prevBlock != nil { - block.Finalization.Height = prevBlock.Finalization.Height + 1 - } else { - block.Finalization.Height = 1 + if prevBlock == nil { + return ErrNotInitiazlied } + block.Finalization.Height = prevBlock.Finalization.Height + 1 cc.prevBlockLock.Lock() defer cc.prevBlockLock.Unlock() cc.prevBlock = block diff --git a/core/compaction-chain_test.go b/core/compaction-chain_test.go index 6931674..bffe57a 100644 --- a/core/compaction-chain_test.go +++ b/core/compaction-chain_test.go @@ -37,7 +37,9 @@ func (s *CompactionChainTestSuite) SetupTest() { func (s *CompactionChainTestSuite) newCompactionChain() *compactionChain { gov, err := test.NewGovernance(4, 100*time.Millisecond) s.Require().NoError(err) - return newCompactionChain(gov) + cc := newCompactionChain(gov) + cc.init(&types.Block{}) + return cc } func (s *CompactionChainTestSuite) generateBlocks( @@ -73,15 +75,11 @@ func (s *CompactionChainTestSuite) TestProcessBlock() { } now = now.Add(100 * time.Millisecond) } - var prevBlock *types.Block + prevBlock := &types.Block{} for _, block := range blocks { s.Equal(cc.prevBlock, prevBlock) s.Require().NoError(cc.processBlock(block)) - if prevBlock != nil { - s.Equal(prevBlock.Finalization.Height+1, block.Finalization.Height) - } else { - s.Equal(uint64(1), block.Finalization.Height) - } + s.Equal(prevBlock.Finalization.Height+1, block.Finalization.Height) prevBlock = block } } diff --git a/core/consensus.go b/core/consensus.go index f4e3295..3601720 100644 --- a/core/consensus.go +++ b/core/consensus.go @@ -310,9 +310,10 @@ func NewConsensus( } // Run starts running DEXON Consensus. -func (con *Consensus) Run() { +func (con *Consensus) Run(initBlock *types.Block) { // Setup context. con.ctx, con.ctxCancel = context.WithCancel(context.Background()) + con.ccModule.init(initBlock) go con.processMsg(con.network.ReceiveChan()) con.cfgModule.registerDKG(con.round, int(con.currentConfig.DKGSetSize)/3+1) con.event.RegisterTime(con.dMoment.Add(con.currentConfig.RoundInterval/4), diff --git a/core/consensus_test.go b/core/consensus_test.go index 39aac7a..94c5018 100644 --- a/core/consensus_test.go +++ b/core/consensus_test.go @@ -170,6 +170,7 @@ func (s *ConsensusTestSuite) prepareConsensus( network := conn.newNetwork(nID) con := NewConsensus(dMoment, app, gov, db, network, prvKey) + con.ccModule.init(&types.Block{}) conn.setCon(nID, con) return app, con } diff --git a/core/lattice_test.go b/core/lattice_test.go index 603424b..5c06b92 100644 --- a/core/lattice_test.go +++ b/core/lattice_test.go @@ -107,9 +107,12 @@ func (s *LatticeTestSuite) newTestLatticeMgr( // Setup governance. gov, err := test.NewGovernance(int(cfg.NotarySetSize), cfg.LambdaBA) req.NoError(err) + // Setup compaction chain. + cc := newCompactionChain(gov) + cc.init(&types.Block{}) // Setup lattice. return &testLatticeMgr{ - ccModule: newCompactionChain(gov), + ccModule: cc, app: app, db: db, lattice: NewLattice( |