Clean anchors, forward sort of headers (#4225)

Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
This commit is contained in:
ledgerwatch 2022-05-21 18:00:26 +01:00 committed by GitHub
parent e6cb086c90
commit 6974a98aca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 5 deletions

View File

@ -488,7 +488,7 @@ func (cs *MultyClient) blockHeaders(ctx context.Context, pkt eth.BlockHeadersPac
})
}
if cs.Hd.POSSync() {
sort.Sort(headerdownload.HeadersByHeightAndHash(csHeaders)) // Sorting by reverse order of block heights
sort.Sort(headerdownload.HeadersReverseSort(csHeaders)) // Sorting by reverse order of block heights
tx, err := cs.db.BeginRo(ctx)
defer tx.Rollback()
if err != nil {
@ -502,6 +502,7 @@ func (cs *MultyClient) blockHeaders(ctx context.Context, pkt eth.BlockHeadersPac
cs.Penalize(ctx, penalties)
}
} else {
sort.Sort(headerdownload.HeadersSort(csHeaders)) // Sorting by order of block heights
canRequestMore := cs.Hd.ProcessHeaders(csHeaders, false /* newBlock */, ConvertH512ToPeerID(peerID))
if canRequestMore {

View File

@ -56,13 +56,13 @@ const POSPandaBanner = `
`
// Implements sort.Interface so we can sort the incoming header in the message by block height
type HeadersByHeightAndHash []ChainSegmentHeader
type HeadersReverseSort []ChainSegmentHeader
func (h HeadersByHeightAndHash) Len() int {
func (h HeadersReverseSort) Len() int {
return len(h)
}
func (h HeadersByHeightAndHash) Less(i, j int) bool {
func (h HeadersReverseSort) Less(i, j int) bool {
// Note - the ordering is the inverse ordering of the block heights
if h[i].Number != h[j].Number {
return h[i].Number > h[j].Number
@ -70,7 +70,26 @@ func (h HeadersByHeightAndHash) Less(i, j int) bool {
return bytes.Compare(h[i].Hash[:], h[j].Hash[:]) > 0
}
func (h HeadersByHeightAndHash) Swap(i, j int) {
func (h HeadersReverseSort) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
// Implements sort.Interface so we can sort the incoming header in the message by block height
type HeadersSort []ChainSegmentHeader
func (h HeadersSort) Len() int {
return len(h)
}
func (h HeadersSort) Less(i, j int) bool {
// Note - the ordering is the inverse ordering of the block heights
if h[i].Number != h[j].Number {
return h[i].Number < h[j].Number
}
return bytes.Compare(h[i].Hash[:], h[j].Hash[:]) < 0
}
func (h HeadersSort) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
@ -208,6 +227,9 @@ func (hd *HeaderDownload) pruneLinkQueue() {
} else {
prevChild.next = link.next
}
if anchor.fLink == nil {
hd.removeAnchor(anchor)
}
}
}
}