Skip to content

Commit c6f458e

Browse files
lnd.go: sync headers before chain notifier startup to prevent premature rescan
This commit addresses a regression where Neutrino rescanning starts from an outdated height (~100k blocks behind) instead of using the current synced height. Root Cause: In commit 16a8b62, the initialization order was changed so that Chain Notifier starts before wallet syncing completes. This means the rescan begins using the stale height from BuildChainControl rather than the fully synced height. Old behavior (commit 1dfb5a0): 1. RPC server starts 2. Headers sync as part of daemon server 3. Chain Notifier starts after sync completes 4. Rescan begins from current (synced) height Current behavior (regression): 1. Chain Notifier starts in newServer (before RPC) 2. Wallet sync happens after RPC server starts 3. Rescan uses outdated height from BuildChainControl Solution: - Ensure headers are fully synced before starting the chain notifier, and after starting the RPC server. - Move chain notifier startup to its correct location after headers are fully synced. - Make sure the starting beat is lazily called after chain notifier started and before that starting beat result is used.
1 parent 6ade31d commit c6f458e

File tree

1 file changed

+12
-40
lines changed

1 file changed

+12
-40
lines changed

server.go

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -733,17 +733,6 @@ func newServer(ctx context.Context, cfg *Config, listenAddrs []net.Addr,
733733
quit: make(chan struct{}),
734734
}
735735

736-
// Start the low-level services once they are initialized.
737-
//
738-
// TODO(yy): break the server startup into four steps,
739-
// 1. init the low-level services.
740-
// 2. start the low-level services.
741-
// 3. init the high-level services.
742-
// 4. start the high-level services.
743-
if err := s.startLowLevelServices(); err != nil {
744-
return nil, err
745-
}
746-
747736
currentHash, currentHeight, err := s.cc.ChainIO.GetBestBlock()
748737
if err != nil {
749738
return nil, err
@@ -2125,41 +2114,12 @@ func (c cleaner) run() {
21252114
}
21262115
}
21272116

2128-
// startLowLevelServices starts the low-level services of the server. These
2129-
// services must be started successfully before running the main server. The
2130-
// services are,
2131-
// 1. the chain notifier.
2132-
//
2133-
// TODO(yy): identify and add more low-level services here.
2134-
func (s *server) startLowLevelServices() error {
2135-
var startErr error
2136-
2137-
cleanup := cleaner{}
2138-
2139-
cleanup = cleanup.add(s.cc.ChainNotifier.Stop)
2140-
if err := s.cc.ChainNotifier.Start(); err != nil {
2141-
startErr = err
2142-
}
2143-
2144-
if startErr != nil {
2145-
cleanup.run()
2146-
}
2147-
2148-
return startErr
2149-
}
2150-
21512117
// Start starts the main daemon server, all requested listeners, and any helper
21522118
// goroutines.
21532119
// NOTE: This function is safe for concurrent access.
21542120
//
21552121
//nolint:funlen
21562122
func (s *server) Start(ctx context.Context) error {
2157-
// Get the current blockbeat.
2158-
beat, err := s.getStartingBeat()
2159-
if err != nil {
2160-
return err
2161-
}
2162-
21632123
var startErr error
21642124

21652125
// If one sub system fails to start, the following code ensures that the
@@ -2213,6 +2173,12 @@ func (s *server) Start(ctx context.Context) error {
22132173
return
22142174
}
22152175

2176+
cleanup = cleanup.add(s.cc.ChainNotifier.Stop)
2177+
if err := s.cc.ChainNotifier.Start(); err != nil {
2178+
startErr = err
2179+
return
2180+
}
2181+
22162182
cleanup = cleanup.add(s.cc.BestBlockTracker.Stop)
22172183
if err := s.cc.BestBlockTracker.Start(); err != nil {
22182184
startErr = err
@@ -2247,6 +2213,12 @@ func (s *server) Start(ctx context.Context) error {
22472213
}
22482214
}
22492215

2216+
beat, err := s.getStartingBeat()
2217+
if err != nil {
2218+
startErr = err
2219+
return
2220+
}
2221+
22502222
cleanup = cleanup.add(s.txPublisher.Stop)
22512223
if err := s.txPublisher.Start(beat); err != nil {
22522224
startErr = err

0 commit comments

Comments
 (0)