eth/downloader: fix ancestor searching for light syncing (#19136)

This commit is contained in:
gary rong 2019-03-14 18:19:03 +08:00 committed by Péter Szilágyi
parent 95b2ec7169
commit 6c312a24b6
1 changed files with 19 additions and 17 deletions

View File

@ -665,30 +665,32 @@ func (d *Downloader) findAncestor(p *peerConnection, remoteHeader *types.Header)
localHeight = d.lightchain.CurrentHeader().Number.Uint64() localHeight = d.lightchain.CurrentHeader().Number.Uint64()
} }
p.log.Debug("Looking for common ancestor", "local", localHeight, "remote", remoteHeight) p.log.Debug("Looking for common ancestor", "local", localHeight, "remote", remoteHeight)
// Recap floor value for binary search
if localHeight >= MaxForkAncestry { if localHeight >= MaxForkAncestry {
// We're above the max reorg threshold, find the earliest fork point // We're above the max reorg threshold, find the earliest fork point
floor = int64(localHeight - MaxForkAncestry) floor = int64(localHeight - MaxForkAncestry)
}
// If we're doing a light sync, ensure the floor doesn't go below the CHT, as // If we're doing a light sync, ensure the floor doesn't go below the CHT, as
// all headers before that point will be missing. // all headers before that point will be missing.
if d.mode == LightSync { if d.mode == LightSync {
// If we dont know the current CHT position, find it // If we dont know the current CHT position, find it
if d.genesis == 0 { if d.genesis == 0 {
header := d.lightchain.CurrentHeader() header := d.lightchain.CurrentHeader()
for header != nil { for header != nil {
d.genesis = header.Number.Uint64() d.genesis = header.Number.Uint64()
if floor >= int64(d.genesis)-1 { if floor >= int64(d.genesis)-1 {
break break
}
header = d.lightchain.GetHeaderByHash(header.ParentHash)
} }
} header = d.lightchain.GetHeaderByHash(header.ParentHash)
// We already know the "genesis" block number, cap floor to that
if floor < int64(d.genesis)-1 {
floor = int64(d.genesis) - 1
} }
} }
// We already know the "genesis" block number, cap floor to that
if floor < int64(d.genesis)-1 {
floor = int64(d.genesis) - 1
}
} }
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)