From f2fb45707b788b8e422c9f1016f1f0cdd0a411ab Mon Sep 17 00:00:00 2001 From: Jimmy Hu Date: Wed, 3 Apr 2019 14:51:21 +0800 Subject: core: syncer: add deliver pending blocks (#546) * core: syncer: deliver pending blocks * fixup --- core/syncer/consensus.go | 31 +++++++++++++++++++++++++++++++ integration_test/consensus_test.go | 2 ++ 2 files changed, 33 insertions(+) diff --git a/core/syncer/consensus.go b/core/syncer/consensus.go index f777e35..f4681a2 100644 --- a/core/syncer/consensus.go +++ b/core/syncer/consensus.go @@ -86,6 +86,7 @@ type Consensus struct { // NewConsensus creates an instance for Consensus (syncer consensus). func NewConsensus( + initHeight uint64, dMoment time.Time, app core.Application, gov core.Governance, @@ -122,9 +123,39 @@ func NewConsensus( defer con.agreementWaitGroup.Done() con.agreementModule.run() }() + if err := con.deliverPendingBlocks(initHeight); err != nil { + panic(err) + } return con } +func (con *Consensus) deliverPendingBlocks(height uint64) error { + if height >= con.initChainTipHeight { + return nil + } + blocks := make([]*types.Block, 0, con.initChainTipHeight-height) + hash, _ := con.db.GetCompactionChainTipInfo() + for { + block, err := con.db.GetBlock(hash) + if err != nil { + return err + } + if block.Position.Height == height { + break + } + blocks = append(blocks, &block) + hash = block.ParentHash + } + sort.Sort(types.BlocksByPosition(blocks)) + for _, b := range blocks { + con.logger.Debug("Syncer BlockConfirmed", "block", b) + con.app.BlockConfirmed(*b) + con.logger.Debug("Syncer BlockDelivered", "block", b) + con.app.BlockDelivered(b.Hash, b.Position, b.Randomness) + } + return nil +} + func (con *Consensus) assureBuffering() { if func() bool { con.lock.RLock() diff --git a/integration_test/consensus_test.go b/integration_test/consensus_test.go index eab0c22..ae56af2 100644 --- a/integration_test/consensus_test.go +++ b/integration_test/consensus_test.go @@ -469,6 +469,7 @@ ReachAlive: } logger := common.NewCustomLogger(log.New(f, "", log.LstdFlags|log.Lmicroseconds)) syncerObj := syncer.NewConsensus( + 0, dMoment, syncNode.app, syncNode.gov, @@ -634,6 +635,7 @@ ReachStop: nID := types.NewNodeID(prvKey.PublicKey()) node := nodes[nID] syncerCon[nID] = syncer.NewConsensus( + latestHeight, dMoment, node.app, node.gov, -- cgit v1.2.3