-
Notifications
You must be signed in to change notification settings - Fork 20.8k
eth/downloader: fix skeleton cleanup #28581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
75929ca
eth/downloader: fix skeleton cleanup
rjl493456442 de234df
eth/downloader: short circuit if nothing to delete
rjl493456442 5b6571f
eth/downloader: polish the logic in cleanup
rjl493456442 eead5a2
eth/downloader: address comments
rjl493456442 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,7 +161,7 @@ type backfiller interface { | |
// on initial startup. | ||
// | ||
// The method should return the last block header that has been successfully | ||
// backfilled, or nil if the backfiller was not resumed. | ||
// backfilled (in the current or a previous run), falling back to the genesis. | ||
suspend() *types.Header | ||
|
||
// resume requests the backfiller to start running fill or snap sync based on | ||
|
@@ -382,14 +382,17 @@ func (s *skeleton) sync(head *types.Header) (*types.Header, error) { | |
done := make(chan struct{}) | ||
go func() { | ||
defer close(done) | ||
if filled := s.filler.suspend(); filled != nil { | ||
// If something was filled, try to delete stale sync helpers. If | ||
// unsuccessful, warn the user, but not much else we can do (it's | ||
// a programming error, just let users report an issue and don't | ||
// choke in the meantime). | ||
if err := s.cleanStales(filled); err != nil { | ||
log.Error("Failed to clean stale beacon headers", "err", err) | ||
} | ||
filled := s.filler.suspend() | ||
if filled == nil { | ||
log.Error("Latest filled block is not available") | ||
return | ||
} | ||
// If something was filled, try to delete stale sync helpers. If | ||
// unsuccessful, warn the user, but not much else we can do (it's | ||
// a programming error, just let users report an issue and don't | ||
// choke in the meantime). | ||
if err := s.cleanStales(filled); err != nil { | ||
log.Error("Failed to clean stale beacon headers", "err", err) | ||
} | ||
}() | ||
// Wait for the suspend to finish, consuming head events in the meantime | ||
|
@@ -1120,33 +1123,46 @@ func (s *skeleton) cleanStales(filled *types.Header) error { | |
number := filled.Number.Uint64() | ||
log.Trace("Cleaning stale beacon headers", "filled", number, "hash", filled.Hash()) | ||
|
||
// If the filled header is below the linked subchain, something's | ||
// corrupted internally. Report and error and refuse to do anything. | ||
if number < s.progress.Subchains[0].Tail { | ||
// If the filled header is below the linked subchain, something's corrupted | ||
// internally. Report and error and refuse to do anything. | ||
if number+1 < s.progress.Subchains[0].Tail { | ||
return fmt.Errorf("filled header below beacon header tail: %d < %d", number, s.progress.Subchains[0].Tail) | ||
} | ||
// Subchain seems trimmable, push the tail forward up to the last | ||
// filled header and delete everything before it - if available. In | ||
// case we filled past the head, recreate the subchain with a new | ||
// head to keep it consistent with the data on disk. | ||
// If nothing in subchain is filled, don't bother to do cleanup. | ||
if number+1 == s.progress.Subchains[0].Tail { | ||
return nil | ||
} | ||
var ( | ||
start = s.progress.Subchains[0].Tail // start deleting from the first known header | ||
end = number // delete until the requested threshold | ||
start uint64 | ||
end uint64 | ||
batch = s.db.NewBatch() | ||
) | ||
s.progress.Subchains[0].Tail = number | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah hmm, indeed this is wonky. If the tail is the first "missing" header, then it should be filled+1 |
||
s.progress.Subchains[0].Next = filled.ParentHash | ||
|
||
if s.progress.Subchains[0].Head < number { | ||
// If more headers were filled than available, push the entire | ||
// subchain forward to keep tracking the node's block imports | ||
end = s.progress.Subchains[0].Head + 1 // delete the entire original range, including the head | ||
s.progress.Subchains[0].Head = number // assign a new head (tail is already assigned to this) | ||
|
||
// The entire original skeleton chain was deleted and a new one | ||
// defined. Make sure the new single-header chain gets pushed to | ||
// disk to keep internal state consistent. | ||
rawdb.WriteSkeletonHeader(batch, filled) | ||
if number < s.progress.Subchains[0].Head { | ||
// The skeleton chain is partially consumed, set the new tail as filled+1. | ||
tail := rawdb.ReadSkeletonHeader(s.db, number+1) | ||
if tail.ParentHash != filled.Hash() { | ||
return fmt.Errorf("filled header is discontinuous with subchain: %d %s, please file an issue", number, filled.Hash()) | ||
} | ||
start, end = s.progress.Subchains[0].Tail, number+1 // remove headers in [tail, filled] | ||
s.progress.Subchains[0].Tail = tail.Number.Uint64() | ||
s.progress.Subchains[0].Next = tail.ParentHash | ||
} else { | ||
// The skeleton chain is fully consumed, set both head and tail as filled. | ||
start, end = s.progress.Subchains[0].Tail, filled.Number.Uint64() // remove headers in [tail, filled) | ||
s.progress.Subchains[0].Tail = filled.Number.Uint64() | ||
s.progress.Subchains[0].Next = filled.ParentHash | ||
|
||
// If more headers were filled than available, push the entire subchain | ||
// forward to keep tracking the node's block imports. | ||
if number > s.progress.Subchains[0].Head { | ||
end = s.progress.Subchains[0].Head + 1 // delete the entire original range, including the head | ||
s.progress.Subchains[0].Head = number // assign a new head (tail is already assigned to this) | ||
|
||
// The entire original skeleton chain was deleted and a new one | ||
// defined. Make sure the new single-header chain gets pushed to | ||
// disk to keep internal state consistent. | ||
rawdb.WriteSkeletonHeader(batch, filled) | ||
} | ||
} | ||
// Execute the trimming and the potential rewiring of the progress | ||
s.saveSyncStatus(batch) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure that this is correct.
Number is the last item newly filled. When I start a sync loop, lets say I have N blocks in my chain. Then I skeleton sync, and will reconstruct headers from HEAD to N+1, and which point I have my subchain linked to the local chain.
I start backfilling. If I fail to fill anything,
cleanStales
does not get called. If I successfully fill 1 block, thenfilled
will be N+1 (the newly filled one block).In that case
number
==filled
==N+1
, which is ==s.progress.Subchains[0].Tail
. The only way to hit this error would be to backfill something we already had in our chain.With the proposed modification however, backfilling my local head block would make the check pass, but that should not be possible, because I can only backfill stuff from my subchain, not 1 block below it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure about it.
cleanStales
will be invoked anyway regardless if we successfully fill something or not. TheCurrentSnapBlock()
will be returned as the newly filled block.Thus it's theoretically possible (1) the tail is
N+1
and (2) filled isN
, the error will be reported.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According the below, the will only call cleanStales if something was filled.
That said, you are also right that resuming the filler will always return the head snap block (or genesis I guess in case we're brand new).
Interesting, I guess this was a refactor after I designed the original idea. It kind of makes the filled check in my first code segment moot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I agree with this +1, but we need to fix some comments that mention that it cannot happen. Namely,