From ca43bd9f99deead21dae71a749297dc6aa361898 Mon Sep 17 00:00:00 2001 From: Mission Liao Date: Mon, 21 Jan 2019 17:51:26 +0800 Subject: core: fix issue (#427) * Dropped block should not be added to db - Here "dropped" means a block is older than current tip of that chain. * "Acking blocks doesn't exist" should be treated as an error when adding a block --- core/lattice-data.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'core/lattice-data.go') diff --git a/core/lattice-data.go b/core/lattice-data.go index cf81a11..b9ad699 100644 --- a/core/lattice-data.go +++ b/core/lattice-data.go @@ -106,7 +106,7 @@ func newLatticeDataConfig( // latticeData is a module for storing lattice. type latticeData struct { // DB for getting blocks purged in memory. - db db.Reader + db db.Database // chains stores chains' blocks and other info. chains []*chainStatus // blockByHash stores blocks, indexed by block hash. @@ -119,7 +119,7 @@ type latticeData struct { // newLatticeData creates a new latticeData instance. func newLatticeData( - db db.Reader, + db db.Database, dMoment time.Time, round uint64, config *types.Config) (data *latticeData) { @@ -291,21 +291,26 @@ func (data *latticeData) addBlock( bAck *types.Block updated bool ) + if err = data.db.PutBlock(*block); err != nil { + if err == db.ErrBlockExists { + // If a node is crashed and restarted, we might encounter some + // blocks that already confirmed but not delivered yet. Then + // syncer might still try to add that block in this way. + err = nil + } else { + return + } + } data.chains[block.Position.ChainID].addBlock(block) data.blockByHash[block.Hash] = block // Update lastAckPos. for _, ack := range block.Acks { if bAck, err = data.findBlock(ack); err != nil { - if err == db.ErrBlockDoesNotExist { - err = nil - continue - } return } data.chains[bAck.Position.ChainID].lastAckPos[block.Position.ChainID] = bAck.Position.Clone() } - // Extract deliverable blocks to total ordering. A block is deliverable to // total ordering iff all its ackings blocks were delivered to total ordering. for { -- cgit v1.2.3