Fix header skeleton (#2945)

* Fix underflow only

* Rename queryRange to maxHeight

* Fix additional cases
This commit is contained in:
TBC Dev 2021-11-11 15:53:32 +08:00 committed by GitHub
parent c1cbb21f3d
commit 3df4197f2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -571,21 +571,22 @@ func (hd *HeaderDownload) RequestSkeleton() *HeaderRequest {
defer hd.lock.RUnlock()
log.Trace("Request skeleton", "anchors", len(hd.anchors), "top seen height", hd.topSeenHeight, "highestInDb", hd.highestInDb)
stride := uint64(8 * 192)
queryRange := hd.topSeenHeight
nextHeight := hd.highestInDb + stride
maxHeight := hd.topSeenHeight + 1 // Inclusive upper bound
if maxHeight <= nextHeight {
return nil
}
// Determine the query range as the height of lowest anchor
for _, anchor := range hd.anchors {
if anchor.blockHeight > hd.highestInDb && anchor.blockHeight < queryRange {
queryRange = anchor.blockHeight
if anchor.blockHeight > nextHeight && anchor.blockHeight < maxHeight {
maxHeight = anchor.blockHeight // Exclusive upper bound
}
}
length := (queryRange - hd.highestInDb) / stride
length := (maxHeight - nextHeight) / stride
if length > 192 {
length = 192
}
if length == 0 {
return nil
}
return &HeaderRequest{Number: hd.highestInDb + stride, Length: length, Skip: stride, Reverse: false}
return &HeaderRequest{Number: nextHeight, Length: length, Skip: stride - 1, Reverse: false}
}
// InsertHeaders attempts to insert headers into the database, verifying them first