Skip to content

Commit 5b6571f

Browse files
committed
eth/downloader: polish the logic in cleanup
1 parent de234df commit 5b6571f

File tree

1 file changed

+31
-37
lines changed

1 file changed

+31
-37
lines changed

eth/downloader/skeleton.go

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,52 +1120,46 @@ func (s *skeleton) cleanStales(filled *types.Header) error {
11201120
number := filled.Number.Uint64()
11211121
log.Trace("Cleaning stale beacon headers", "filled", number, "hash", filled.Hash())
11221122

1123-
// If the filled header is below and discontinuous with the linked subchain,
1124-
// something's corrupted internally. Report and error and refuse to do anything.
1123+
// If the filled header is below the linked subchain, something's corrupted
1124+
// internally. Report and error and refuse to do anything.
11251125
if number+1 < s.progress.Subchains[0].Tail {
11261126
return fmt.Errorf("filled header below beacon header tail: %d < %d", number, s.progress.Subchains[0].Tail)
11271127
}
1128-
// If nothing is filled, don't bother to do cleanup.
1128+
// If nothing in subchain is filled, don't bother to do cleanup.
11291129
if number+1 == s.progress.Subchains[0].Tail {
11301130
return nil
11311131
}
1132-
// Determine the new tail based on the given filled header. If it's still
1133-
// in the range of skeleton chain, use the next header adjacent to the
1134-
// filled one (it's expected to link with the filled one, otherwise
1135-
// something corrupted); otherwise use the filled one if the whole skeleton
1136-
// chain is consumed.
1137-
newTail := filled
1138-
if number < s.progress.Subchains[0].Head {
1139-
newTail = rawdb.ReadSkeletonHeader(s.db, number+1)
1140-
if newTail.ParentHash != filled.Hash() {
1141-
return fmt.Errorf("filled header is discontinuous with subchain: %d %s", number, filled.Hash())
1142-
}
1143-
}
1144-
// Subchain seems trimmable, push the tail forward up to the last
1145-
// filled header and delete everything before it - if available. In
1146-
// case we filled past the head, recreate the subchain with a new
1147-
// head to keep it consistent with the data on disk.
11481132
var (
1149-
start = s.progress.Subchains[0].Tail // start deleting from the first known header
1150-
end = newTail.Number.Uint64() // delete skeleton headers before the new tail
1133+
start uint64
1134+
end uint64
11511135
batch = s.db.NewBatch()
11521136
)
1153-
s.progress.Subchains[0].Tail = newTail.Number.Uint64()
1154-
s.progress.Subchains[0].Next = newTail.ParentHash
1155-
1156-
// If more headers were filled than available, push the entire subchain
1157-
// forward to keep tracking the node's block imports.
1158-
//
1159-
// Note that the new tail will be the filled one in this case, which is
1160-
// unexpected, but we cannot do anything else to improve the situation.
1161-
if s.progress.Subchains[0].Head < number {
1162-
end = s.progress.Subchains[0].Head + 1 // delete the entire original range, including the head
1163-
s.progress.Subchains[0].Head = number // assign a new head (tail is already assigned to this)
1164-
1165-
// The entire original skeleton chain was deleted and a new one
1166-
// defined. Make sure the new single-header chain gets pushed to
1167-
// disk to keep internal state consistent.
1168-
rawdb.WriteSkeletonHeader(batch, filled)
1137+
if number < s.progress.Subchains[0].Head {
1138+
// The skeleton chain is partially consumed, set the new tail as filled+1.
1139+
tail := rawdb.ReadSkeletonHeader(s.db, number+1)
1140+
if tail.ParentHash != filled.Hash() {
1141+
return fmt.Errorf("filled header is discontinuous with subchain: %d %s", number, filled.Hash())
1142+
}
1143+
start, end = s.progress.Subchains[0].Tail, number+1 // remove headers in [tail, filled]
1144+
s.progress.Subchains[0].Tail = tail.Number.Uint64()
1145+
s.progress.Subchains[0].Next = tail.ParentHash
1146+
} else {
1147+
// The skeleton chain is fully consumed, set both head and tail as filled.
1148+
start, end = s.progress.Subchains[0].Tail, filled.Number.Uint64() // remove headers in [tail, filled)
1149+
s.progress.Subchains[0].Tail = filled.Number.Uint64()
1150+
s.progress.Subchains[0].Next = filled.ParentHash
1151+
1152+
// If more headers were filled than available, push the entire subchain
1153+
// forward to keep tracking the node's block imports.
1154+
if number > s.progress.Subchains[0].Head {
1155+
end = s.progress.Subchains[0].Head + 1 // delete the entire original range, including the head
1156+
s.progress.Subchains[0].Head = number // assign a new head (tail is already assigned to this)
1157+
1158+
// The entire original skeleton chain was deleted and a new one
1159+
// defined. Make sure the new single-header chain gets pushed to
1160+
// disk to keep internal state consistent.
1161+
rawdb.WriteSkeletonHeader(batch, filled)
1162+
}
11691163
}
11701164
// Execute the trimming and the potential rewiring of the progress
11711165
s.saveSyncStatus(batch)

0 commit comments

Comments
 (0)