Skip to content

Commit b9fd1a7

Browse files
committed
spv: Request headers from peer after initial sync
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.
1 parent 705569f commit b9fd1a7

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

spv/sync.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,11 @@ func (s *Syncer) initialSyncRescan(ctx context.Context) error {
16181618
// peerStartup performs initial startup operations with a recently connected
16191619
// peer.
16201620
func (s *Syncer) peerStartup(ctx context.Context, rp *p2p.RemotePeer) error {
1621+
// If the initial sync process has already completed, then immediately
1622+
// request any new headers from the peer at the end of the peer setup
1623+
// process.
1624+
requestHeaders := s.Synced()
1625+
16211626
// Only continue with peer startup after the initial sync process
16221627
// has completed.
16231628
select {
@@ -1649,7 +1654,23 @@ func (s *Syncer) peerStartup(ctx context.Context, rp *p2p.RemotePeer) error {
16491654
}
16501655

16511656
// Ask peer to send any new headers.
1652-
return rp.SendHeaders(ctx)
1657+
if err := rp.SendHeaders(ctx); err != nil {
1658+
return err
1659+
}
1660+
1661+
// If needed, request any updated headers from the peer.
1662+
if requestHeaders {
1663+
log.Debugf("Requesting updated headers from peer %v", rp)
1664+
locators, _, err := s.wallet.BlockLocators(ctx, nil)
1665+
if err != nil {
1666+
return err
1667+
}
1668+
if err := rp.HeadersAsync(ctx, locators, &hashStop); err != nil {
1669+
return err
1670+
}
1671+
}
1672+
1673+
return nil
16531674
}
16541675

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

0 commit comments

Comments
 (0)