Skip to content

Commit

Permalink
[release-v1.7] netsync: Improve sync height tracking.
Browse files Browse the repository at this point in the history
This modifies the sync manager to update the sync height in the
following additional cases to improve its accuracy:

- When a block that is higher than the current known sync heighter is
  processed and connected to the main chain
- When a header fails to connect during the initial header sync process
  • Loading branch information
davecgh committed Aug 3, 2022
1 parent f595014 commit 3b5bdc8
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions internal/netsync/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,19 @@ func (m *SyncManager) processBlock(block *dcrutil.Block) (int64, error) {
if err != nil {
return 0, err
}

// Update the sync height when the block is higher than the currently best
// known value and it extends the main chain.
onMainChain := forkLen == 0
if onMainChain {
m.syncHeightMtx.Lock()
blockHeight := int64(block.MsgBlock().Header.Height)
if blockHeight > m.syncHeight {
m.syncHeight = blockHeight
}
m.syncHeightMtx.Unlock()
}

m.isCurrentMtx.Lock()
m.maybeUpdateIsCurrent()
m.isCurrentMtx.Unlock()
Expand Down Expand Up @@ -1043,6 +1056,18 @@ func (m *SyncManager) handleHeadersMsg(hmsg *headersMsg) {
for _, header := range headers {
err := chain.ProcessBlockHeader(header)
if err != nil {
// Update the sync height when the sync peer fails to process any
// headers since that chain is invalid from the local point of view
// and thus whatever the best known good header is becomes the new
// sync height unless a better one is discovered from the new sync
// peer.
if peer == m.syncPeer && !headersSynced {
_, newBestHeaderHeight := chain.BestHeader()
m.syncHeightMtx.Lock()
m.syncHeight = newBestHeaderHeight
m.syncHeightMtx.Unlock()
}

// Note that there is no need to check for an orphan header here
// because they were already verified to connect above.

Expand Down

0 comments on commit 3b5bdc8

Please sign in to comment.