Skip to content

Commit

Permalink
spv: Validate header chain diff earlier
Browse files Browse the repository at this point in the history
This moves the header chain difficulty validation to an earlier point in
time of the initial sync process.

This is preferable so that less work is performed in case invalid
headers are received.
  • Loading branch information
matheusd committed Nov 13, 2023
1 parent 389bbe2 commit d9d3f17
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions spv/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,22 @@ func (s *Syncer) getHeaders(ctx context.Context, rp *p2p.RemotePeer) error {
}
}

// Verify the sidechain that includes the received headers has
// the correct difficulty.
s.sidechainMu.Lock()
fullsc, err := s.sidechains.FullSideChain(nodes)
if err != nil {
s.sidechainMu.Unlock()
return err
}
_, err = s.wallet.ValidateHeaderChainDifficulties(ctx, fullsc, 0)
if err != nil {
s.sidechainMu.Unlock()
rp.Disconnect(err)
return err
}
s.sidechainMu.Unlock()

g, ctx := errgroup.WithContext(ctx)
for i := range headers {
i := i
Expand Down Expand Up @@ -1408,12 +1424,6 @@ func (s *Syncer) getHeaders(ctx context.Context, rp *p2p.RemotePeer) error {
continue
}

_, err = s.wallet.ValidateHeaderChainDifficulties(ctx, bestChain, 0)
if err != nil {
s.sidechainMu.Unlock()
return err
}

prevChain, err := s.wallet.ChainSwitch(ctx, &s.sidechains, bestChain, nil)
if err != nil {
s.sidechainMu.Unlock()
Expand Down

0 comments on commit d9d3f17

Please sign in to comment.