-
Notifications
You must be signed in to change notification settings - Fork 20.2k
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
eth/downloader: resolve local header by hash for beacon sync #24691
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7abaf28
eth/downlaoder: resolve local header by hash for beacon sync
rjl493456442 c98ff83
eth/downloader: fix error message
rjl493456442 9986865
eth/downloader: cap the reverse header resolving
rjl493456442 4e5e436
eth/downloader: re-enable tests
rjl493456442 8ac432b
eth/downloader: add warning logs
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 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 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 |
---|---|---|
|
@@ -159,9 +159,6 @@ type LightChain interface { | |
// GetHeaderByHash retrieves a header from the local chain. | ||
GetHeaderByHash(common.Hash) *types.Header | ||
|
||
// GetHeaderByNumber retrieves a block header from the local chain by number. | ||
GetHeaderByNumber(number uint64) *types.Header | ||
|
||
// CurrentHeader retrieves the head header from the local chain. | ||
CurrentHeader() *types.Header | ||
|
||
|
@@ -486,7 +483,15 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td, ttd * | |
// Retrieve the pivot header from the skeleton chain segment but | ||
// fallback to local chain if it's not found in skeleton space. | ||
if pivot = d.skeleton.Header(number); pivot == nil { | ||
pivot = d.lightchain.GetHeaderByNumber(number) | ||
_, oldest, _ := d.skeleton.Bounds() // error is already checked | ||
if number < oldest.Number.Uint64() { | ||
count := int(oldest.Number.Uint64() - number) // it's capped by fsMinFullBlocks | ||
headers := d.readHeaderRange(oldest, count) | ||
if len(headers) == count { | ||
pivot = headers[len(headers)-1] | ||
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. In theory, we should check the number of retrieved pivot header. But I think it should always be correct unless we have the programming fault. |
||
log.Warn("Retrieved pivot header from local", "number", pivot.Number, "hash", pivot.Hash(), "latest", latest.Number, "oldest", oldest.Number) | ||
} | ||
} | ||
} | ||
// Print an error log and return directly in case the pivot header | ||
// is still not found. It means the skeleton chain is not linked | ||
|
@@ -1772,3 +1777,25 @@ func (d *Downloader) DeliverSnapPacket(peer *snap.Peer, packet snap.Packet) erro | |
return fmt.Errorf("unexpected snap packet type: %T", packet) | ||
} | ||
} | ||
|
||
// readHeaderRange returns a list of headers, using the given last header as the base, | ||
// and going backwards towards genesis. This method assumes that the caller already has | ||
// placed a reasonable cap on count. | ||
func (d *Downloader) readHeaderRange(last *types.Header, count int) []*types.Header { | ||
var ( | ||
current = last | ||
headers []*types.Header | ||
) | ||
for { | ||
parent := d.lightchain.GetHeaderByHash(current.ParentHash) | ||
if parent == nil { | ||
break // The chain is not continuous, or the chain is exhausted | ||
} | ||
headers = append(headers, parent) | ||
if len(headers) >= count { | ||
break | ||
} | ||
current = parent | ||
} | ||
return headers | ||
} |
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.
In theory, we should check the number of retrieved pivot header. But I think it should always be correct unless we have the programming fault.