Skip to content

Commit

Permalink
spv: Request headers from peer after initial sync
Browse files Browse the repository at this point in the history
This modifies the peer setup procedure to request any updated headers
from a newly connected peer if the connection is made after the initial
sync is completed.

This speeds up re-syncing after initial sync when the local client has
been offline for some time (for example, due to a flicking network
connection) by ensuring any and all updated headers are requested
immediately, instead of waiting until a new block is announced to begin
the catchup process.
  • Loading branch information
matheusd committed Dec 13, 2023
1 parent 705569f commit b9fd1a7
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion spv/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,11 @@ func (s *Syncer) initialSyncRescan(ctx context.Context) error {
// peerStartup performs initial startup operations with a recently connected
// peer.
func (s *Syncer) peerStartup(ctx context.Context, rp *p2p.RemotePeer) error {
// If the initial sync process has already completed, then immediately
// request any new headers from the peer at the end of the peer setup
// process.
requestHeaders := s.Synced()

// Only continue with peer startup after the initial sync process
// has completed.
select {
Expand Down Expand Up @@ -1649,7 +1654,23 @@ func (s *Syncer) peerStartup(ctx context.Context, rp *p2p.RemotePeer) error {
}

// Ask peer to send any new headers.
return rp.SendHeaders(ctx)
if err := rp.SendHeaders(ctx); err != nil {
return err
}

// If needed, request any updated headers from the peer.
if requestHeaders {
log.Debugf("Requesting updated headers from peer %v", rp)
locators, _, err := s.wallet.BlockLocators(ctx, nil)
if err != nil {
return err
}
if err := rp.HeadersAsync(ctx, locators, &hashStop); err != nil {
return err
}
}

return nil
}

// handleMempool handles eviction from the local mempool of non-wallet-backed
Expand Down

0 comments on commit b9fd1a7

Please sign in to comment.