Skip to content

Commit 81bd998

Browse files
authored
core, eth/downloader: handle spurious junk bodies from racey rollbacks (#25578)
* eth/downloader: handle junkbodies/receipts in the beacon sync * core: check for header presence when checking for blocks
1 parent 6d711f0 commit 81bd998

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

core/blockchain_reader.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ func (bc *BlockChain) HasBlock(hash common.Hash, number uint64) bool {
137137
if bc.blockCache.Contains(hash) {
138138
return true
139139
}
140+
if !bc.HasHeader(hash, number) {
141+
return false
142+
}
140143
return rawdb.HasBody(bc.db, hash, number)
141144
}
142145

eth/downloader/skeleton.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ func (s *skeleton) sync(head *types.Header) (*types.Header, error) {
358358
// If the sync is already done, resume the backfiller. When the loop stops,
359359
// terminate the backfiller too.
360360
linked := len(s.progress.Subchains) == 1 &&
361+
rawdb.HasHeader(s.db, s.progress.Subchains[0].Next, s.scratchHead) &&
361362
rawdb.HasBody(s.db, s.progress.Subchains[0].Next, s.scratchHead) &&
362363
rawdb.HasReceipts(s.db, s.progress.Subchains[0].Next, s.scratchHead)
363364
if linked {
@@ -946,12 +947,12 @@ func (s *skeleton) processResponse(res *headerResponse) (linked bool, merged boo
946947
// In the case of full sync it would be enough to check for the body,
947948
// but even a full syncing node will generate a receipt once block
948949
// processing is done, so it's just one more "needless" check.
949-
var (
950-
hasBody = rawdb.HasBody(s.db, header.ParentHash, header.Number.Uint64()-1)
951-
hasReceipt = rawdb.HasReceipts(s.db, header.ParentHash, header.Number.Uint64()-1)
952-
)
953-
if hasBody && hasReceipt {
954-
linked = true
950+
//
951+
// The weird cascading checks are done to minimize the database reads.
952+
linked = rawdb.HasHeader(s.db, header.ParentHash, header.Number.Uint64()-1) &&
953+
rawdb.HasBody(s.db, header.ParentHash, header.Number.Uint64()-1) &&
954+
rawdb.HasReceipts(s.db, header.ParentHash, header.Number.Uint64()-1)
955+
if linked {
955956
break
956957
}
957958
}

0 commit comments

Comments
 (0)