From e9a1d3bca8353ee206d262ab1fad2d7e3e0b24a5 Mon Sep 17 00:00:00 2001 From: Jimmy Hu Date: Mon, 15 Apr 2019 12:12:22 +0800 Subject: core: start next BA only when previous block delivered (#571) --- core/agreement-mgr.go | 12 +++++++----- core/blockchain.go | 7 +++++++ core/blockchain_test.go | 1 + core/consensus.go | 4 ++++ 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/core/agreement-mgr.go b/core/agreement-mgr.go index 4597fe9..f65903d 100644 --- a/core/agreement-mgr.go +++ b/core/agreement-mgr.go @@ -564,11 +564,13 @@ func (mgr *agreementMgr) baRoutineForOneRound( default: } nextHeight, nextTime = mgr.bcModule.nextBlock() - if isStop(restartPos) { - break - } - if nextHeight > restartPos.Height { - break + if nextHeight != notReadyHeight { + if isStop(restartPos) { + break + } + if nextHeight > restartPos.Height { + break + } } mgr.logger.Debug("BlockChain not ready!!!", "old", oldPos, "restart", restartPos, "next", nextHeight) diff --git a/core/blockchain.go b/core/blockchain.go index 1efd867..4fae221 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -21,6 +21,7 @@ import ( "bytes" "errors" "fmt" + "math" "sort" "sync" "time" @@ -49,6 +50,8 @@ var ( ErrMissingRandomness = errors.New("missing block randomness") ) +const notReadyHeight uint64 = math.MaxUint64 + type pendingBlockRecord struct { position types.Position block *types.Block @@ -384,6 +387,10 @@ func (bc *blockChain) nextBlock() (uint64, time.Time) { if tip == nil { return types.GenesisHeight, bc.dMoment } + if tip != bc.lastDelivered { + // If tip is not delivered, we should not proceed to next block. + return notReadyHeight, time.Time{} + } return tip.Position.Height + 1, tip.Timestamp.Add(config.minBlockInterval) } diff --git a/core/blockchain_test.go b/core/blockchain_test.go index ea604a2..c3d2cb7 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -415,6 +415,7 @@ func (s *BlockChainTestSuite) TestNextBlockAndTipRound() { s.Require().Equal(nextT, s.dMoment) // Add one block. s.Require().NoError(bc.addBlock(blocks[0])) + s.Require().Len(bc.extractBlocks(), 1) nextH, nextT = bc.nextBlock() s.Require().Equal(nextH, uint64(2)) s.Require().Equal( diff --git a/core/consensus.go b/core/consensus.go index 7c44bdf..ba7d8fd 100644 --- a/core/consensus.go +++ b/core/consensus.go @@ -1148,6 +1148,10 @@ func (con *Consensus) generateBlockRandomness(blocks []*types.Block) { "block", block, "result", result) con.network.BroadcastAgreementResult(result) + if err := con.deliverFinalizedBlocks(); err != nil { + con.logger.Error("Failed to deliver finalized block", + "error", err) + } } }(block) } -- cgit v1.2.3