Skip to content

Commit

Permalink
Fix pending block index during startup.
Browse files Browse the repository at this point in the history
  • Loading branch information
weiribao committed Dec 7, 2019
1 parent 296c034 commit c2c5c6b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions blockchain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ func (ch *Chain) addBlock(block *core.Block, isSnapshotRoot bool) (*core.Extende
return extendedBlock, nil
}

// FixBlockIndex fixes index for given block.
func (ch *Chain) FixBlockIndex(block *core.ExtendedBlock) {
ch.mu.Lock()
defer ch.mu.Unlock()

ch.AddBlockByHeightIndex(block.Height, block.Hash())
ch.AddTxsToIndex(block, false)
}

// blockByHeightIndexKey constructs the DB key for the given block height.
func blockByHeightIndexKey(height uint64) common.Bytes {
// convert uint64 to []byte
Expand Down
8 changes: 8 additions & 0 deletions consensus/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,14 @@ func (e *ConsensusEngine) validateBlock(block *core.Block, parent *core.Extended
return result.Error("Block epoch must be greater than parent epoch")
}
if !parent.Status.IsValid() {
if parent.Status.IsPending() {
// Should never happen
e.logger.WithFields(log.Fields{
"parent": block.Parent.Hex(),
"parent.status": parent.Status,
"block": block.Hash().Hex(),
}).Panic("Parent block is pending")
}
e.logger.WithFields(log.Fields{
"parent": block.Parent.Hex(),
"parent.status": parent.Status,
Expand Down
3 changes: 3 additions & 0 deletions netsync/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,9 @@ func (rm *RequestManager) resumePendingBlocks() {
block := queue[0]
queue = queue[1:]
if block.Status.IsPending() {
// In rare cases a block is saved but index is not yet saved before the process is
// killed.
rm.chain.FixBlockIndex(block)
rm.addBlock(block.Block)
}
for _, hash := range block.Children {
Expand Down

0 comments on commit c2c5c6b

Please sign in to comment.