Skip to content

Commit 69686fa

Browse files
authored
core, core/rawdb: fix transaction indexing (ethereum#24024)
This PR fixes a special corner case in transaction indexing. When the chain is rewound by SetHead to a historical point which is even lower than the transaction indexes tail, then system will report Failed to decode block body error all the time, because the relevant blocks are already deleted. In order to avoid this "non-critical-but-annoying" issue, we can recap the indexing target to head+1(to is excluded, so it means indexing transactions from 0 to head).
1 parent 619a3e7 commit 69686fa

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

core/blockchain.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -2204,7 +2204,14 @@ func (bc *BlockChain) maintainTxIndex(ancients uint64) {
22042204
// If a previous indexing existed, make sure that we fill in any missing entries
22052205
if bc.txLookupLimit == 0 || head < bc.txLookupLimit {
22062206
if *tail > 0 {
2207-
rawdb.IndexTransactions(bc.db, 0, *tail, bc.quit)
2207+
// It can happen when chain is rewound to a historical point which
2208+
// is even lower than the indexes tail, recap the indexing target
2209+
// to new head to avoid reading non-existent block bodies.
2210+
end := *tail
2211+
if end > head+1 {
2212+
end = head + 1
2213+
}
2214+
rawdb.IndexTransactions(bc.db, 0, end, bc.quit)
22082215
}
22092216
return
22102217
}

core/rawdb/chain_iterator.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ func indexTransactions(db ethdb.Database, from uint64, to uint64, interrupt chan
247247
}
248248
}
249249

250-
// IndexTransactions creates txlookup indices of the specified block range.
250+
// IndexTransactions creates txlookup indices of the specified block range. The from
251+
// is included while to is excluded.
251252
//
252253
// This function iterates canonical chain in reverse order, it has one main advantage:
253254
// We can write tx index tail flag periodically even without the whole indexing
@@ -339,6 +340,7 @@ func unindexTransactions(db ethdb.Database, from uint64, to uint64, interrupt ch
339340
}
340341

341342
// UnindexTransactions removes txlookup indices of the specified block range.
343+
// The from is included while to is excluded.
342344
//
343345
// There is a passed channel, the whole procedure will be interrupted if any
344346
// signal received.

0 commit comments

Comments
 (0)