Skip to content

Commit

Permalink
blockchain: Fix blockCache for rejected blocks (#1326)
Browse files Browse the repository at this point in the history
  • Loading branch information
darioush authored Aug 30, 2024
1 parent ef2f4fd commit 252592a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 26 deletions.
29 changes: 3 additions & 26 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,9 @@ func (bc *BlockChain) Reject(block *types.Block) error {
return fmt.Errorf("failed to write delete block batch: %w", err)
}

// Remove the block from the block cache (ignore return value of whether it was in the cache)
_ = bc.blockCache.Remove(block.Hash())

return nil
}

Expand Down Expand Up @@ -1782,32 +1785,6 @@ func (bc *BlockChain) reportBlock(block *types.Block, receipts types.Receipts, e
log.Debug(reason.String())
}

func (bc *BlockChain) RemoveRejectedBlocks(start, end uint64) error {
batch := bc.db.NewBatch()

for i := start; i < end; i++ {
hashes := rawdb.ReadAllHashes(bc.db, i)
canonicalBlock := bc.GetBlockByNumber((i))
if canonicalBlock == nil {
return fmt.Errorf("failed to retrieve block by number at height %d", i)
}
canonicalHash := canonicalBlock.Hash()
for _, hash := range hashes {
if hash == canonicalHash {
continue
}
rawdb.DeleteBlock(batch, hash, i)
}

if err := batch.Write(); err != nil {
return fmt.Errorf("failed to write delete rejected block batch at height %d", i)
}
batch.Reset()
}

return nil
}

// reprocessBlock reprocesses a previously accepted block. This is often used
// to regenerate previously pruned state tries.
func (bc *BlockChain) reprocessBlock(parent *types.Block, current *types.Block) (common.Hash, error) {
Expand Down
1 change: 1 addition & 0 deletions core/test_blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ func TestAcceptNonCanonicalBlock(t *testing.T, create func(db ethdb.Database, gs
if err := blockchain.Reject(chain1[i]); err != nil {
t.Fatal(err)
}
require.False(t, blockchain.HasBlock(chain1[i].Hash(), chain1[i].NumberU64()))
}

lastAcceptedBlock := blockchain.LastConsensusAcceptedBlock()
Expand Down

0 comments on commit 252592a

Please sign in to comment.