forked from cerc-io/plugeth
downloader: extract findAncestor search functions (#21744)
This is a simple refactoring, extracting common ancestor negotiation logic to named function
This commit is contained in:
parent
7da8f75d5b
commit
81bf9f97c9
@ -90,6 +90,7 @@ var (
|
|||||||
errCanceled = errors.New("syncing canceled (requested)")
|
errCanceled = errors.New("syncing canceled (requested)")
|
||||||
errNoSyncActive = errors.New("no sync active")
|
errNoSyncActive = errors.New("no sync active")
|
||||||
errTooOld = errors.New("peer's protocol version too old")
|
errTooOld = errors.New("peer's protocol version too old")
|
||||||
|
errNoAncestorFound = errors.New("no common ancestor found")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Downloader struct {
|
type Downloader struct {
|
||||||
@ -814,6 +815,26 @@ func (d *Downloader) findAncestor(p *peerConnection, remoteHeader *types.Header)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ancestor, err := d.findAncestorSpanSearch(p, mode, remoteHeight, localHeight, floor)
|
||||||
|
if err == nil {
|
||||||
|
return ancestor, nil
|
||||||
|
}
|
||||||
|
// The returned error was not nil.
|
||||||
|
// If the error returned does not reflect that a common ancestor was not found, return it.
|
||||||
|
// If the error reflects that a common ancestor was not found, continue to binary search,
|
||||||
|
// where the error value will be reassigned.
|
||||||
|
if !errors.Is(err, errNoAncestorFound) {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ancestor, err = d.findAncestorBinarySearch(p, mode, remoteHeight, floor)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return ancestor, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Downloader) findAncestorSpanSearch(p *peerConnection, mode SyncMode, remoteHeight, localHeight uint64, floor int64) (commonAncestor uint64, err error) {
|
||||||
from, count, skip, max := calculateRequestSpan(remoteHeight, localHeight)
|
from, count, skip, max := calculateRequestSpan(remoteHeight, localHeight)
|
||||||
|
|
||||||
p.log.Trace("Span searching for common ancestor", "count", count, "from", from, "skip", skip)
|
p.log.Trace("Span searching for common ancestor", "count", count, "from", from, "skip", skip)
|
||||||
@ -894,6 +915,12 @@ func (d *Downloader) findAncestor(p *peerConnection, remoteHeader *types.Header)
|
|||||||
p.log.Debug("Found common ancestor", "number", number, "hash", hash)
|
p.log.Debug("Found common ancestor", "number", number, "hash", hash)
|
||||||
return number, nil
|
return number, nil
|
||||||
}
|
}
|
||||||
|
return 0, errNoAncestorFound
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Downloader) findAncestorBinarySearch(p *peerConnection, mode SyncMode, remoteHeight uint64, floor int64) (commonAncestor uint64, err error) {
|
||||||
|
hash := common.Hash{}
|
||||||
|
|
||||||
// Ancestor not found, we need to binary search over our chain
|
// Ancestor not found, we need to binary search over our chain
|
||||||
start, end := uint64(0), remoteHeight
|
start, end := uint64(0), remoteHeight
|
||||||
if floor > 0 {
|
if floor > 0 {
|
||||||
|
Loading…
Reference in New Issue
Block a user