Skip to content

Commit

Permalink
spv: Ask for sendheaders after initial sync
Browse files Browse the repository at this point in the history
This makes the remote peers configure to send block announcements via
sendheaders only after the initial sync is completed. This ensures the
initial sync process can fetch all the needed headers from any and all
peers before they are configured to send header annoucements.
  • Loading branch information
matheusd committed Oct 31, 2023
1 parent fbb33ad commit d630a42
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions spv/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package spv

import (
"context"
"fmt"
"runtime"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -444,8 +445,18 @@ func (s *Syncer) Run(ctx context.Context) error {
}
s.rescanFinished()

log.Infof("Initial sync completed. Switching to header announcements sync.")
s.synced()

// Perform peer startup (publish txs, sendheaders, etc) on
// every connected peer.
err = s.forRemotes(func(rp *p2p.RemotePeer) error {
return s.startupPeerSync(ctx, rp)
})
if err != nil {
return err
}

// Rescan done.
return nil
})
Expand Down Expand Up @@ -547,16 +558,19 @@ func (s *Syncer) connectAndRunPeer(ctx context.Context, raddr string) {
s.peerDisconnected(n, raddr)
}()

// Perform peer startup.
err = s.startupSync(ctx, rp)
if err != nil {
if !errors.Is(err, context.Canceled) {
log.Warnf("Unable to complete startup sync with peer %v: %v", raddr, err)
} else {
log.Infof("Lost peer %v", raddr)
// Perform peer sync if the initial sync process has completed. This
// configures the peer to send new headers.
if s.Synced() {
err = s.startupPeerSync(ctx, rp)
if err != nil {
if !errors.Is(err, context.Canceled) {
log.Warnf("Unable to complete startup sync with peer %v: %v", raddr, err)
} else {
log.Infof("Lost peer %v", raddr)
}
rp.Disconnect(err)
return
}
rp.Disconnect(err)
return
}

// Finally, block until the peer disconnects.
Expand Down Expand Up @@ -1548,7 +1562,9 @@ nextbatch:
}
}

func (s *Syncer) startupSync(ctx context.Context, rp *p2p.RemotePeer) error {
// startupPeerSync performs peer startup procedures after connection. This should
// only be called after the wallet has had its initial sync complete.
func (s *Syncer) startupPeerSync(ctx context.Context, rp *p2p.RemotePeer) error {
var err error

if rp.Pver() >= wire.InitStateVersion {
Expand All @@ -1558,6 +1574,11 @@ func (s *Syncer) startupSync(ctx context.Context, rp *p2p.RemotePeer) error {
}
}

err = rp.SendHeaders(ctx)
if err != nil {
return fmt.Errorf("unable ask for sendheaders from peer: %w", err)
}

unminedTxs, err := s.wallet.UnminedTransactions(ctx)
if err != nil {
log.Errorf("Cannot load unmined transactions for resending: %v", err)
Expand Down

0 comments on commit d630a42

Please sign in to comment.