Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean headers pointers when removing links #4222

Merged
merged 2 commits into from
May 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions turbo/stages/headerdownload/header_algos.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,7 @@ func (hd *HeaderDownload) ReportBadHeader(headerHash common.Hash) {
hd.badHeaders[headerHash] = struct{}{}
// Find the link, remove it and all its descendands from all the queues
if link, ok := hd.links[headerHash]; ok {
removeList := []*Link{link}
for len(removeList) > 0 {
removal := removeList[len(removeList)-1]
removeList = removeList[:len(removeList)-1]
hd.moveLinkToQueue(removal, NoQueue)
delete(hd.links, removal.hash)
for child := removal.fChild; child != nil; child = child.next {
removeList = append(removeList, child)
}
}
hd.removeUpwards(link)
}
}

Expand Down Expand Up @@ -146,16 +137,16 @@ func (hd *HeaderDownload) IsBadHeaderPoS(tipHash common.Hash) (bad bool, lastVal
}

func (hd *HeaderDownload) removeUpwards(link *Link) {
var toRemove []*Link
for child := link; child != nil; child = child.next {
toRemove = append(toRemove, child)
if link == nil {
return
}
var toRemove []*Link = []*Link{link}
for len(toRemove) > 0 {
removal := toRemove[len(toRemove)-1]
toRemove = toRemove[:len(toRemove)-1]
delete(hd.links, removal.hash)
hd.moveLinkToQueue(removal, NoQueue)
for child := removal.fChild; child != nil; child = child.next {
for child := removal.fChild; child != nil; child, child.next = child.next, nil {
toRemove = append(toRemove, child)
}
}
Expand Down Expand Up @@ -185,13 +176,17 @@ func (hd *HeaderDownload) removeAnchor(anchor *Anchor) {
// Anchor is removed from the map, and from the priority queue
delete(hd.anchors, anchor.parentHash)
heap.Remove(hd.anchorQueue, anchor.idx)
for child := anchor.fLink; child != nil; child, child.next = child.next, nil {
}
anchor.idx = -1
}

func (hd *HeaderDownload) pruneLinkQueue() {
for hd.linkQueue.Len() > hd.linkLimit {
link := heap.Pop(&hd.linkQueue).(*Link)
delete(hd.links, link.hash)
for child := link.fChild; child != nil; child, child.next = child.next, nil {
}
if parentLink, ok := hd.links[link.header.ParentHash]; ok {
var prevChild *Link
for child := parentLink.fChild; child != nil && child != link; child = child.next {
Expand Down Expand Up @@ -295,6 +290,8 @@ func (hd *HeaderDownload) RecoverFromDb(db kv.RoDB) error {
for hd.persistedLinkQueue.Len() > 0 {
link := heap.Pop(&hd.persistedLinkQueue).(*Link)
delete(hd.links, link.hash)
for child := link.fChild; child != nil; child, child.next = child.next, nil {
}
}
err := db.View(context.Background(), func(tx kv.Tx) error {
c, err := tx.Cursor(kv.Headers)
Expand Down Expand Up @@ -461,6 +458,7 @@ func (hd *HeaderDownload) InsertHeader(hf FeedHeaderFunc, terminalTotalDifficult
// If the link or its parent is marked bad, throw it out
hd.moveLinkToQueue(link, NoQueue)
delete(hd.links, link.hash)
hd.removeUpwards(link)
return true, false, nil
}
if !link.verified {
Expand All @@ -473,6 +471,7 @@ func (hd *HeaderDownload) InsertHeader(hf FeedHeaderFunc, terminalTotalDifficult
log.Debug("Verification failed for header", "hash", link.hash, "height", link.blockHeight, "err", err)
hd.moveLinkToQueue(link, NoQueue)
delete(hd.links, link.hash)
hd.removeUpwards(link)
return true, false, nil
}
}
Expand Down Expand Up @@ -519,6 +518,8 @@ func (hd *HeaderDownload) InsertHeader(hf FeedHeaderFunc, terminalTotalDifficult
for hd.persistedLinkQueue.Len() > hd.persistedLinkLimit {
link := heap.Pop(&hd.persistedLinkQueue).(*Link)
delete(hd.links, link.hash)
for child := link.fChild; child != nil; child, child.next = child.next, nil {
}
}
return hd.insertQueue.Len() > 0 && hd.insertQueue[0].blockHeight <= hd.highestInDb+1, false, nil
}
Expand Down Expand Up @@ -930,6 +931,8 @@ func (hd *HeaderDownload) ProcessHeaders(csHeaders []ChainSegmentHeader, newBloc
requestMore = true
}
}
hd.lock.Lock()
defer hd.lock.Unlock()
log.Trace("Link queue", "size", hd.linkQueue.Len())
if hd.linkQueue.Len() > hd.linkLimit {
log.Trace("Too many links, cutting down", "count", hd.linkQueue.Len(), "tried to add", len(csHeaders), "limit", hd.linkLimit)
Expand Down