Skip to content

Commit 1b5a867

Browse files
authored
core: do less lookups when writing fast-sync block bodies (#21468)
1 parent 87c0ba9 commit 1b5a867

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

core/blockchain.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
12771277
}
12781278
// writeLive writes blockchain and corresponding receipt chain into active store.
12791279
writeLive := func(blockChain types.Blocks, receiptChain []types.Receipts) (int, error) {
1280+
skipPresenceCheck := false
12801281
batch := bc.db.NewBatch()
12811282
for i, block := range blockChain {
12821283
// Short circuit insertion if shutting down or processing failed
@@ -1287,9 +1288,17 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
12871288
if !bc.HasHeader(block.Hash(), block.NumberU64()) {
12881289
return i, fmt.Errorf("containing header #%d [%x…] unknown", block.Number(), block.Hash().Bytes()[:4])
12891290
}
1290-
if bc.HasBlock(block.Hash(), block.NumberU64()) {
1291-
stats.ignored++
1292-
continue
1291+
if !skipPresenceCheck {
1292+
// Ignore if the entire data is already known
1293+
if bc.HasBlock(block.Hash(), block.NumberU64()) {
1294+
stats.ignored++
1295+
continue
1296+
} else {
1297+
// If block N is not present, neither are the later blocks.
1298+
// This should be true, but if we are mistaken, the shortcut
1299+
// here will only cause overwriting of some existing data
1300+
skipPresenceCheck = true
1301+
}
12931302
}
12941303
// Write all the data out into the database
12951304
rawdb.WriteBody(batch, block.Hash(), block.NumberU64(), block.Body())

0 commit comments

Comments
 (0)