Skip to content

Commit 979af6e

Browse files
committed
refactor(core): use errors.Is for sentinel error comparisons in blockchain.go
Replace direct == / != comparisons against sentinel errors (consensus.ErrPrunedAncestor, consensus.ErrFutureBlock, consensus.ErrUnknownAncestor, ErrKnownBlock, ErrStopPreparingBlock) with errors.Is, which also matches wrapped errors.
1 parent 5501a1f commit 979af6e

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

core/blockchain.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,12 +1806,12 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
18061806
block, err := it.next()
18071807
switch {
18081808
// First block is pruned, insert as sidechain and reorg only if TD grows enough
1809-
case err == consensus.ErrPrunedAncestor:
1809+
case errors.Is(err, consensus.ErrPrunedAncestor):
18101810
return bc.insertSidechain(block, it)
18111811

18121812
// First block is future, shove it (and all children) to the future queue (unknown ancestor)
1813-
case err == consensus.ErrFutureBlock || (err == consensus.ErrUnknownAncestor && bc.futureBlocks.Contains(it.first().ParentHash())):
1814-
for block != nil && (it.index == 0 || err == consensus.ErrUnknownAncestor) {
1813+
case errors.Is(err, consensus.ErrFutureBlock) || (errors.Is(err, consensus.ErrUnknownAncestor) && bc.futureBlocks.Contains(it.first().ParentHash())):
1814+
for block != nil && (it.index == 0 || errors.Is(err, consensus.ErrUnknownAncestor)) {
18151815
if err := bc.addFutureBlock(block); err != nil {
18161816
return it.index, events, coalescedLogs, err
18171817
}
@@ -1827,11 +1827,11 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
18271827
// 1. We did a roll-back, and should now do a re-import
18281828
// 2. The block is stored as a sidechain, and is lying about it's stateroot, and passes a stateroot
18291829
// from the canonical chain, which has not been verified.
1830-
case err == ErrKnownBlock:
1830+
case errors.Is(err, ErrKnownBlock):
18311831
// Skip all known blocks that behind us
18321832
current := bc.CurrentBlock().Number.Uint64()
18331833

1834-
for block != nil && err == ErrKnownBlock && current >= block.NumberU64() {
1834+
for block != nil && errors.Is(err, ErrKnownBlock) && current >= block.NumberU64() {
18351835
stats.ignored++
18361836
block, err = it.next()
18371837
}
@@ -1934,13 +1934,13 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
19341934
}
19351935

19361936
// Any blocks remaining here? The only ones we care about are the future ones
1937-
if block != nil && err == consensus.ErrFutureBlock {
1937+
if block != nil && errors.Is(err, consensus.ErrFutureBlock) {
19381938
if err := bc.addFutureBlock(block); err != nil {
19391939
return it.index, events, coalescedLogs, err
19401940
}
19411941
block, err = it.next()
19421942

1943-
for ; block != nil && err == consensus.ErrUnknownAncestor; block, err = it.next() {
1943+
for ; block != nil && errors.Is(err, consensus.ErrUnknownAncestor); block, err = it.next() {
19441944
if err := bc.addFutureBlock(block); err != nil {
19451945
return it.index, events, coalescedLogs, err
19461946
}
@@ -2064,7 +2064,7 @@ func (bc *BlockChain) insertSidechain(block *types.Block, it *insertIterator) (i
20642064
// ones. Any other errors means that the block is invalid, and should not be written
20652065
// to disk.
20662066
err := consensus.ErrPrunedAncestor
2067-
for ; block != nil && (err == consensus.ErrPrunedAncestor); block, err = it.next() {
2067+
for ; block != nil && (errors.Is(err, consensus.ErrPrunedAncestor)); block, err = it.next() {
20682068
// Check the canonical state root for that number
20692069
if number := block.NumberU64(); current >= number {
20702070
if canonical := bc.GetBlockByNumber(number); canonical != nil && canonical.Root() == block.Root() {
@@ -2225,13 +2225,13 @@ func (bc *BlockChain) getResultBlock(block *types.Block, verifiedM2 bool) (*Resu
22252225
bstart := time.Now()
22262226
err := bc.validator.ValidateBody(block)
22272227
switch {
2228-
case err == ErrKnownBlock:
2228+
case errors.Is(err, ErrKnownBlock):
22292229
// Block and state both already known. However if the current block is below
22302230
// this number we did a rollback and we should reimport it nonetheless.
22312231
if bc.CurrentBlock().Number.Uint64() >= block.NumberU64() {
22322232
return nil, ErrKnownBlock
22332233
}
2234-
case err == consensus.ErrPrunedAncestor:
2234+
case errors.Is(err, consensus.ErrPrunedAncestor):
22352235
// Block competing with the canonical chain, store in the db, but don't process
22362236
// until the competitor TD goes above the canonical TD
22372237
currentBlock := bc.CurrentBlock()
@@ -2286,7 +2286,7 @@ func (bc *BlockChain) getResultBlock(block *types.Block, verifiedM2 bool) (*Resu
22862286
receipts, logs, usedGas, err := bc.processor.ProcessBlockNoValidator(calculatedBlock, statedb, tradingState, bc.vmConfig, feeCapacity)
22872287
process := time.Since(bstart)
22882288
if err != nil {
2289-
if err != ErrStopPreparingBlock {
2289+
if !errors.Is(err, ErrStopPreparingBlock) {
22902290
bc.reportBlock(block, receipts, err)
22912291
}
22922292
return nil, err

0 commit comments

Comments
 (0)