Skip to content

Commit

Permalink
spv: Drop syncer-global block locators
Browse files Browse the repository at this point in the history
The locators are now only needed during the initial getHeaders sync
stage, therefore they are not needed in the global syncer struct.

Since they are accessed only from a single goroutine now, the mutex is
also unneeded.
  • Loading branch information
matheusd committed Nov 17, 2023
1 parent f46ab46 commit cc42b80
Showing 1 changed file with 6 additions and 60 deletions.
66 changes: 6 additions & 60 deletions spv/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ type Syncer struct {
sidechains wallet.SidechainForest
sidechainMu sync.Mutex

currentLocators []*chainhash.Hash
locatorGeneration uint
locatorMu sync.Mutex

// Holds all potential callbacks used to notify clients
notifications *Notifications

Expand Down Expand Up @@ -333,12 +329,6 @@ func (s *Syncer) Run(ctx context.Context) error {
log.Infof("Transactions synced through block %v height %d", &tipHash, tipHeight)
}

locators, err := s.wallet.BlockLocators(ctx, nil)
if err != nil {
return err
}
s.currentLocators = locators

s.lp.AddrManager().Start()
defer func() {
err := s.lp.AddrManager().Stop()
Expand Down Expand Up @@ -1299,13 +1289,6 @@ func (s *Syncer) handleBlockAnnouncements(ctx context.Context, rp *p2p.RemotePee
return err
}

if len(bestChain) != 0 {
s.locatorMu.Lock()
s.currentLocators = nil
s.locatorGeneration++
s.locatorMu.Unlock()
}

// Log connected blocks.
for _, n := range bestChain {
log.Infof("Connected block %v, height %d, %d wallet transaction(s)",
Expand Down Expand Up @@ -1358,22 +1341,6 @@ var hashStop chainhash.Hash
// getHeaders fetches headers from peers until the wallet is up to date with
// all connected peers. This is part of the startup sync process.
func (s *Syncer) getHeaders(ctx context.Context) error {
var locators []*chainhash.Hash
var generation uint
var err error
s.locatorMu.Lock()
locators = s.currentLocators
generation = s.locatorGeneration
if len(locators) == 0 {
locators, err = s.wallet.BlockLocators(ctx, nil)
if err != nil {
s.locatorMu.Unlock()
return err
}
s.currentLocators = locators
s.locatorGeneration++
}
s.locatorMu.Unlock()

cnet := s.wallet.ChainParams().Net

Expand All @@ -1396,6 +1363,11 @@ nextbatch:
return nil
}

// Request headers from the selected peer.
locators, err := s.wallet.BlockLocators(ctx, nil)
if err != nil {
return err
}
headers, err := rp.Headers(ctx, locators, &hashStop)
if err != nil {
continue nextbatch
Expand Down Expand Up @@ -1502,21 +1474,6 @@ nextbatch:
if added == 0 {
s.sidechainMu.Unlock()

s.locatorMu.Lock()
if s.locatorGeneration > generation {
locators = s.currentLocators
}
if len(locators) == 0 {
locators, err = s.wallet.BlockLocators(ctx, nil)
if err != nil {
s.locatorMu.Unlock()
return err
}
s.currentLocators = locators
s.locatorGeneration++
generation = s.locatorGeneration
}
s.locatorMu.Unlock()
continue nextbatch
}
s.fetchHeadersProgress(headers[len(headers)-1])
Expand All @@ -1531,7 +1488,7 @@ nextbatch:
}
if len(bestChain) == 0 {
s.sidechainMu.Unlock()
continue
continue nextbatch
}

prevChain, err := s.wallet.ChainSwitch(ctx, &s.sidechains, bestChain, nil)
Expand Down Expand Up @@ -1561,17 +1518,6 @@ nextbatch:
// Peers should not be significantly behind the new tip.
s.setRequiredHeight(int32(tip.Header.Height))
s.disconnectStragglers(int32(tip.Header.Height))

// Generate new locators
s.locatorMu.Lock()
locators, err = s.wallet.BlockLocators(ctx, nil)
if err != nil {
s.locatorMu.Unlock()
return err
}
s.currentLocators = locators
s.locatorGeneration++
s.locatorMu.Unlock()
}

return ctx.Err()
Expand Down

0 comments on commit cc42b80

Please sign in to comment.