From cc42b806ef088279caaab97ac2f748a71e0e6121 Mon Sep 17 00:00:00 2001 From: Matheus Degiovani Date: Tue, 31 Oct 2023 10:02:15 -0300 Subject: [PATCH] spv: Drop syncer-global block locators 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. --- spv/sync.go | 66 +++++------------------------------------------------ 1 file changed, 6 insertions(+), 60 deletions(-) diff --git a/spv/sync.go b/spv/sync.go index 6d23bd32f..5bbdcfda9 100644 --- a/spv/sync.go +++ b/spv/sync.go @@ -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 @@ -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() @@ -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)", @@ -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 @@ -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 @@ -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]) @@ -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) @@ -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()