From 6974a98aca4e258086990ea9f1849727699fe7e0 Mon Sep 17 00:00:00 2001 From: ledgerwatch Date: Sat, 21 May 2022 18:00:26 +0100 Subject: [PATCH] Clean anchors, forward sort of headers (#4225) Co-authored-by: Alexey Sharp --- cmd/sentry/sentry/sentry_multy_client.go | 3 ++- turbo/stages/headerdownload/header_algos.go | 30 ++++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/cmd/sentry/sentry/sentry_multy_client.go b/cmd/sentry/sentry/sentry_multy_client.go index 50cb40939..5fb8c1e21 100644 --- a/cmd/sentry/sentry/sentry_multy_client.go +++ b/cmd/sentry/sentry/sentry_multy_client.go @@ -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 { diff --git a/turbo/stages/headerdownload/header_algos.go b/turbo/stages/headerdownload/header_algos.go index 1d2de0f1e..d7c2fcf62 100644 --- a/turbo/stages/headerdownload/header_algos.go +++ b/turbo/stages/headerdownload/header_algos.go @@ -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) + } } } }