Skip to content

Commit

Permalink
chain: Avoid early getHeaders on sidechains
Browse files Browse the repository at this point in the history
This adds a check for the presence of the parent block on a known
sidechain prior to re-requesting all headers on announced blocks during
RPC chain sync

Prior to this commit, the parent block was checked for presence only on
the main chain, which could cause getHeaders to be repeatedly executed
on reorgs of size greater than one.

This was mostly an issue for automated tests that generate large reorgs.
  • Loading branch information
matheusd committed Dec 12, 2023
1 parent ab15d98 commit 705569f
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions chain/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,17 +623,19 @@ func (s *Syncer) blockConnected(ctx context.Context, params json.RawMessage) err
return err
}

// Ensure the ancestor is in the main chain. If it is not, this means
// we missed some blocks and should perform a new round of initial
// header sync.
if inMainChain, _, _ := s.wallet.BlockInMainChain(ctx, &header.PrevBlock); !inMainChain {
if !inMainChain {
log.Infof("Received header for block %s (height %d) when "+
"parent %s not in main chain. Re-requesting "+
"missing headers.", header.BlockHash(),
header.Height, header.PrevBlock)
return s.getHeaders(ctx)
}
// Ensure the ancestor is known to be in the main or in a side chain.
// If it is not, this means we missed some blocks and should perform a
// new round of initial header sync.
s.sidechainsMu.Lock()
prevInMainChain, _, _ := s.wallet.BlockInMainChain(ctx, &header.PrevBlock)
prevInSideChain := s.sidechains.HasSideChainBlock(&header.PrevBlock)
s.sidechainsMu.Unlock()
if !(prevInMainChain || prevInSideChain) {
log.Infof("Received header for block %s (height %d) when "+
"parent %s not in main or side chain. Re-requesting "+
"missing headers.", header.BlockHash(),
header.Height, header.PrevBlock)
return s.getHeaders(ctx)
}

blockHash := header.BlockHash()
Expand Down

0 comments on commit 705569f

Please sign in to comment.