Skip to content

Commit

Permalink
netsync: change checkHeadersList behavior
Browse files Browse the repository at this point in the history
2 things are changed:

1: We allow checkHeadersList to remove from the headerList even if the
   SyncManager is not in headers-first mode as we're now receiving
   headers for new block annoucements.

2: We remove headers when we no longer have checkpoints as the main
   reason we keep the last headerNode is to check that the next
   checkpoint connects.

   This is not needed on headers-first with no checkpoints as we don't
   perform that check.

   This is also not needed on new block annoucements as we will have the
   chain tip.
  • Loading branch information
kcalvinalvin committed Jan 20, 2025
1 parent fbbb32b commit bbd87f4
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions netsync/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,17 +811,14 @@ func (sm *SyncManager) current() bool {
func (sm *SyncManager) checkHeadersList(blockHash *chainhash.Hash) (
bool, blockchain.BehaviorFlags) {

// Nothing to check if we're not in headers-first mode.
if !sm.headersFirstMode {
return false, blockchain.BFNone
}

isCheckpointBlock := false
behaviorFlags := blockchain.BFNone

firstNodeEl := sm.headerList.Front()
if firstNodeEl == nil {
log.Warnf("headers-first mode is on but the headersList is empty")
if sm.headersFirstMode {
log.Warnf("headers-first mode is on but the headersList is empty")
}
return isCheckpointBlock, behaviorFlags
}

Expand All @@ -830,6 +827,8 @@ func (sm *SyncManager) checkHeadersList(blockHash *chainhash.Hash) (
return isCheckpointBlock, behaviorFlags
}

// When we still have checkpoints, don't remove the last header node
// as we need that to check if the next header connects to the list.
if sm.nextCheckpoint != nil {
behaviorFlags |= blockchain.BFFastAdd
if firstNode.hash.IsEqual(sm.nextCheckpoint.Hash) {
Expand All @@ -838,9 +837,11 @@ func (sm *SyncManager) checkHeadersList(blockHash *chainhash.Hash) (
sm.headerList.Remove(firstNodeEl)
}
} else {
if firstNode.height != sm.syncPeer.LastBlock() {
sm.headerList.Remove(firstNodeEl)
}
// Since we don't have any more checkpoints, it's ok
// to remove all the headers from the list as we'll
// reset the header state to include the tip for all
// the headers received when not in headers-first mode.
sm.headerList.Remove(firstNodeEl)
}

return isCheckpointBlock, behaviorFlags
Expand Down

0 comments on commit bbd87f4

Please sign in to comment.