Skip to content

Commit bc38270

Browse files
authored
Merge pull request #111 from lightninglabs/lnd-sync-fix
Wait for lnd to be synced before starting subservers
2 parents c58baef + f69d9a9 commit bc38270

File tree

3 files changed

+45
-62
lines changed

3 files changed

+45
-62
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ require (
66
github.com/grpc-ecosystem/grpc-gateway v1.14.3
77
github.com/improbable-eng/grpc-web v0.12.0
88
github.com/jessevdk/go-flags v1.4.0
9-
github.com/lightninglabs/faraday v0.2.0-alpha.0.20200811090810-93547d433a66
10-
github.com/lightninglabs/lndclient v1.0.1-0.20200629081038-bb0726595df9
11-
github.com/lightninglabs/loop v0.8.0-beta
9+
github.com/lightninglabs/faraday v0.2.0-alpha.0.20200901065614-d72fdaeff888
10+
github.com/lightninglabs/lndclient v0.11.0-0
11+
github.com/lightninglabs/loop v0.8.1-beta.0.20200831113239-d4d9aa102455
1212
github.com/lightningnetwork/lnd v0.11.0-beta
1313
github.com/lightningnetwork/lnd/cert v1.0.2
1414
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f

go.sum

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ github.com/btcsuite/btcd v0.0.0-20190629003639-c26ffa870fd8/go.mod h1:3J08xEfcug
2929
github.com/btcsuite/btcd v0.0.0-20190824003749-130ea5bddde3/go.mod h1:3J08xEfcugPacsc34/LKRU2yO7YmuT8yt28J8k2+rrI=
3030
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
3131
github.com/btcsuite/btcd v0.20.1-beta.0.20200513120220-b470eee47728/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
32-
github.com/btcsuite/btcd v0.20.1-beta.0.20200515232429-9f0179fd2c46 h1:QyTpiR5nQe94vza2qkvf7Ns8XX2Rjh/vdIhO3RzGj4o=
33-
github.com/btcsuite/btcd v0.20.1-beta.0.20200515232429-9f0179fd2c46/go.mod h1:Yktc19YNjh/Iz2//CX0vfRTS4IJKM/RKO5YZ9Fn+Pgo=
3432
github.com/btcsuite/btcd v0.20.1-beta.0.20200730232343-1db1b6f8217f h1:m/GhMTvDQLbID616c4TYdHyt0MZ9lH5B/nf9Lu3okCY=
3533
github.com/btcsuite/btcd v0.20.1-beta.0.20200730232343-1db1b6f8217f/go.mod h1:ZSWyehm27aAuS9bvkATT+Xte3hjHZ+MRgMY/8NJ7K94=
3634
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f h1:bAs4lUbRJpnnkd9VhRV3jjAVU7DJVjMaK+IsvSeZvFo=
@@ -40,7 +38,6 @@ github.com/btcsuite/btcutil v1.0.2 h1:9iZ1Terx9fMIOtq1VrwdqfsATL9MC2l8ZrUY6YZ2ut
4038
github.com/btcsuite/btcutil v1.0.2/go.mod h1:j9HUFwoQRsZL3V4n+qG+CUnEGHOarIxfC3Le2Yhbcts=
4139
github.com/btcsuite/btcutil/psbt v1.0.2 h1:gCVY3KxdoEVU7Q6TjusPO+GANIwVgr9yTLqM+a6CZr8=
4240
github.com/btcsuite/btcutil/psbt v1.0.2/go.mod h1:LVveMu4VaNSkIRTZu2+ut0HDBRuYjqGocxDMNS1KuGQ=
43-
github.com/btcsuite/btcwallet v0.11.1-0.20200604005347-6390f167e5f8/go.mod h1:9+AH3V5mcTtNXTKe+fe63fDLKGOwQbZqmvOVUef+JFE=
4441
github.com/btcsuite/btcwallet v0.11.1-0.20200814001439-1d31f4ea6fc5 h1:1We7EuizBnX/17Q6O2dkeToyehxzUHo62Wv1c0ncr7c=
4542
github.com/btcsuite/btcwallet v0.11.1-0.20200814001439-1d31f4ea6fc5/go.mod h1:YkEbJaCyN6yncq5gEp2xG0OKDwus2QxGCEXTNF27w5I=
4643
github.com/btcsuite/btcwallet/wallet/txauthor v1.0.0 h1:KGHMW5sd7yDdDMkCZ/JpP0KltolFsQcB973brBnfj4c=
@@ -51,13 +48,10 @@ github.com/btcsuite/btcwallet/wallet/txsizes v1.0.0 h1:6DxkcoMnCPY4E9cUDPB5tbuuf
5148
github.com/btcsuite/btcwallet/wallet/txsizes v1.0.0/go.mod h1:pauEU8UuMFiThe5PB3EO+gO5kx87Me5NvdQDsTuq6cs=
5249
github.com/btcsuite/btcwallet/walletdb v1.0.0/go.mod h1:bZTy9RyYZh9fLnSua+/CD48TJtYJSHjjYcSaszuxCCk=
5350
github.com/btcsuite/btcwallet/walletdb v1.2.0/go.mod h1:9cwc1Yyg4uvd4ZdfdoMnALji+V9gfWSMfxEdLdR5Vwc=
54-
github.com/btcsuite/btcwallet/walletdb v1.3.1 h1:lW1Ac3F1jJY4K11P+YQtRNcP5jFk27ASfrV7C6mvRU0=
55-
github.com/btcsuite/btcwallet/walletdb v1.3.1/go.mod h1:9cwc1Yyg4uvd4ZdfdoMnALji+V9gfWSMfxEdLdR5Vwc=
5651
github.com/btcsuite/btcwallet/walletdb v1.3.2/go.mod h1:GZCMPNpUu5KE3ASoVd+k06p/1OW8OwNGCCaNWRto2cQ=
5752
github.com/btcsuite/btcwallet/walletdb v1.3.3 h1:u6e7vRIKBF++cJy+hOHaMGg+88ZTwvpaY27AFvtB668=
5853
github.com/btcsuite/btcwallet/walletdb v1.3.3/go.mod h1:oJDxAEUHVtnmIIBaa22wSBPTVcs6hUp5NKWmI8xDwwU=
5954
github.com/btcsuite/btcwallet/wtxmgr v1.0.0/go.mod h1:vc4gBprll6BP0UJ+AIGDaySoc7MdAmZf8kelfNb8CFY=
60-
github.com/btcsuite/btcwallet/wtxmgr v1.1.1-0.20200604005347-6390f167e5f8/go.mod h1:cJGqxXtqQbmXuL7RlXjIM18x0bGHy1407/85mQCLca4=
6155
github.com/btcsuite/btcwallet/wtxmgr v1.2.0 h1:ZUYPsSv8GjF9KK7lboB2OVHF0uYEcHxgrCfFWqPd9NA=
6256
github.com/btcsuite/btcwallet/wtxmgr v1.2.0/go.mod h1:h8hkcKUE3X7lMPzTUoGnNiw5g7VhGrKEW3KpR2r0VnY=
6357
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd h1:R/opQEbFEy9JGkIguV40SvRY1uliPX8ifOvi6ICsFCw=
@@ -149,7 +143,6 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
149143
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
150144
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
151145
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
152-
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
153146
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
154147
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
155148
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 h1:Iju5GlWwrvL6UBg4zJJt3btmonfrMlCDdsejg4CZE7c=
@@ -205,24 +198,21 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
205198
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
206199
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
207200
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
208-
github.com/lightninglabs/faraday v0.2.0-alpha.0.20200811090810-93547d433a66 h1:UKRSbfATcskm++YY4SC1wt9cNANDN5lE4x/uUh7HX2o=
209-
github.com/lightninglabs/faraday v0.2.0-alpha.0.20200811090810-93547d433a66/go.mod h1:ewf8srwFhhz2JpTtZkosWncnfd+0WW1f9koWWe/a1Hg=
201+
github.com/lightninglabs/faraday v0.2.0-alpha.0.20200901065614-d72fdaeff888 h1:kKruI0ZGCW0Ky4q2pt6jO7oSArNBgE+Ps3k6n7j/J6Y=
202+
github.com/lightninglabs/faraday v0.2.0-alpha.0.20200901065614-d72fdaeff888/go.mod h1:tFhh0ScstVI9GgYpJ4Tp+1Bw/DjUOJxlfIuZnfvThe8=
210203
github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf h1:HZKvJUHlcXI/f/O0Avg7t8sqkPo78HFzjmeYFl6DPnc=
211204
github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf/go.mod h1:vxmQPeIQxPf6Jf9rM8R+B4rKBqLA2AjttNxkFBL2Plk=
212-
github.com/lightninglabs/lndclient v0.0.0-20200618122423-5d815058a719 h1:RzyO65pK78xxSaekERq2V3g32GMBPc0MfxmM8MDMVmQ=
213-
github.com/lightninglabs/lndclient v0.0.0-20200618122423-5d815058a719/go.mod h1:WQU2oE0eJIp7jtBOUnL7CO5/YoWrAqyH23A6M1r31i8=
214-
github.com/lightninglabs/lndclient v1.0.1-0.20200629081038-bb0726595df9 h1:wfAFJKim+6pRMbCOGQD9xqZ63PLOR2UYIVrLP08w3js=
215-
github.com/lightninglabs/lndclient v1.0.1-0.20200629081038-bb0726595df9/go.mod h1:WQU2oE0eJIp7jtBOUnL7CO5/YoWrAqyH23A6M1r31i8=
216-
github.com/lightninglabs/loop v0.8.0-beta h1:wgAADHjTLAV5YZ2IgVa4zft3Z4XIulqjOcMuKkC0678=
217-
github.com/lightninglabs/loop v0.8.0-beta/go.mod h1:Pgj7xTC6T3wbCCBxKyOVWBpvDv9mas4zpvW8jZiwG0U=
205+
github.com/lightninglabs/lndclient v0.11.0-0 h1:tCvhlN/NNC/PeCjifONq/f8vU/6ZJS1ivXhpIfw6JfY=
206+
github.com/lightninglabs/lndclient v0.11.0-0/go.mod h1:8/cTKNwgL87NX123gmlv3Xh6p1a7pvzu+40Un3PhHiI=
207+
github.com/lightninglabs/loop v0.8.1-beta.0.20200831113239-d4d9aa102455 h1:RWIxymisZYA02aqEAIunvZxNgcSu+hS3LG4KAxvk2SU=
208+
github.com/lightninglabs/loop v0.8.1-beta.0.20200831113239-d4d9aa102455/go.mod h1:b1cG1mLbPUjWEN5ZQi6pfNeNWCYm5H48NdjkycyifS8=
218209
github.com/lightninglabs/neutrino v0.11.0/go.mod h1:CuhF0iuzg9Sp2HO6ZgXgayviFTn1QHdSTJlMncK80wg=
219210
github.com/lightninglabs/neutrino v0.11.1-0.20200316235139-bffc52e8f200 h1:j4iZ1XlUAPQmW6oSzMcJGILYsRHNs+4O3Gk+2Ms5Dww=
220211
github.com/lightninglabs/neutrino v0.11.1-0.20200316235139-bffc52e8f200/go.mod h1:MlZmoKa7CJP3eR1s5yB7Rm5aSyadpKkxqAwLQmog7N0=
221212
github.com/lightninglabs/protobuf-hex-display v1.3.3-0.20191212020323-b444784ce75d h1:QWD/5MPnaZfUVP7P8wLa4M8Td2DI7XXHXt2vhVtUgGI=
222213
github.com/lightninglabs/protobuf-hex-display v1.3.3-0.20191212020323-b444784ce75d/go.mod h1:KDb67YMzoh4eudnzClmvs2FbiLG9vxISmLApUkCa4uI=
223214
github.com/lightningnetwork/lightning-onion v1.0.2-0.20200501022730-3c8c8d0b89ea h1:oCj48NQ8u7Vz+MmzHqt0db6mxcFZo3Ho7M5gCJauY/k=
224215
github.com/lightningnetwork/lightning-onion v1.0.2-0.20200501022730-3c8c8d0b89ea/go.mod h1:rigfi6Af/KqsF7Za0hOgcyq2PNH4AN70AaMRxcJkff4=
225-
github.com/lightningnetwork/lnd v0.10.0-beta.rc6.0.20200615174244-103c59a4889f/go.mod h1:a2ejAHgjuwQ9YQJBzlMD8ZJHUcyWxXb8HRGe5QlJh9Y=
226216
github.com/lightningnetwork/lnd v0.11.0-beta h1:pUAT7FMHqS+iarNxyRtgj96XKCGAWwmb6ZdiUBy78ts=
227217
github.com/lightningnetwork/lnd v0.11.0-beta/go.mod h1:CzArvT7NFDLhVyW06+NJWSuWFmE6Ea+AjjA3txUBqTM=
228218
github.com/lightningnetwork/lnd/cert v1.0.2 h1:g2rEu+sM2Uyz0bpfuvwri/ks6R/26H5iY1NcGbpDJ+c=
@@ -387,7 +377,6 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
387377
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
388378
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
389379
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
390-
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
391380
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
392381
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
393382
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

terminal.go

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,10 @@ func (g *LightningTerminal) startSubservers(network string) error {
257257
var basicClient lnrpc.LightningClient
258258

259259
// The main RPC listener of lnd might need some time to start, it could
260-
// be that we run into a connection refused a few times.
260+
// be that we run into a connection refused a few times. We use the
261+
// basic client connection to find out if the RPC server is started yet
262+
// because that doesn't do anything else than just connect. We'll check
263+
// if lnd is also ready to be used in the next step.
261264
err := wait.NoError(func() error {
262265
// Create an lnd client now that we have the full configuration.
263266
// We'll need a basic client and a full client because not all
@@ -270,61 +273,52 @@ func (g *LightningTerminal) startSubservers(network string) error {
270273
g.cfg.Lnd.AdminMacPath,
271274
)),
272275
)
273-
if err != nil {
274-
return err
275-
}
276-
g.lndClient, err = lndclient.NewLndServices(
277-
&lndclient.LndServicesConfig{
278-
LndAddress: g.lndAddr,
279-
Network: lndclient.Network(network),
280-
MacaroonDir: filepath.Dir(g.cfg.Lnd.AdminMacPath),
281-
TLSPath: g.cfg.Lnd.TLSCertPath,
282-
},
283-
)
284276
return err
285-
286277
}, defaultStartupTimeout)
287278
if err != nil {
288279
return err
289280
}
290281

291-
// The chain notifier also needs some time to start. Loop will subscribe
292-
// to the notifier and crash if it isn't ready yet, so we need to wait
293-
// here as a workaround.
294-
//
295-
// TODO(guggero): Remove once loop can retry itself.
296-
err = wait.NoError(func() error {
297-
ctxt, cancel := context.WithTimeout(
298-
context.Background(), defaultStartupTimeout,
299-
)
300-
defer cancel()
282+
// Now we know that the connection itself is ready. But we also need to
283+
// wait for two things: The chain notifier to be ready and the lnd
284+
// wallet being fully synced to its chain backend. The chain notifier
285+
// will always be ready first so if we instruct the lndclient to wait
286+
// for the wallet sync, we should be fully ready to start all our
287+
// subservers. This will just block until lnd signals readiness. But we
288+
// still want to react to shutdown requests, so we need to listen for
289+
// those.
290+
ctxc, cancel := context.WithCancel(context.Background())
291+
defer cancel()
301292

302-
notifier := g.lndClient.ChainNotifier
303-
resChan, errChan, err := notifier.RegisterBlockEpochNtfn(
304-
ctxt,
305-
)
306-
if err != nil {
307-
return err
308-
}
309-
310-
// Block until we get a positive/negative answer or the timeout
311-
// is reached.
293+
// Make sure the context is canceled if the user requests shutdown.
294+
go func() {
312295
select {
313-
case <-resChan:
314-
return nil
296+
// Client requests shutdown, cancel the wait.
297+
case <-signal.ShutdownChannel():
298+
cancel()
315299

316-
case err := <-errChan:
317-
return err
318-
319-
case <-ctxt.Done():
320-
return fmt.Errorf("wait for chain notifier to be " +
321-
"ready timed out")
300+
// The check was completed and the above defer canceled the
301+
// context. We can just exit the goroutine, nothing more to do.
302+
case <-ctxc.Done():
322303
}
323-
}, defaultStartupTimeout)
304+
}()
305+
g.lndClient, err = lndclient.NewLndServices(
306+
&lndclient.LndServicesConfig{
307+
LndAddress: g.lndAddr,
308+
Network: lndclient.Network(network),
309+
MacaroonDir: filepath.Dir(
310+
g.cfg.Lnd.AdminMacPath,
311+
),
312+
TLSPath: g.cfg.Lnd.TLSCertPath,
313+
BlockUntilChainSynced: true,
314+
ChainSyncCtx: ctxc,
315+
},
316+
)
324317
if err != nil {
325318
return err
326319
}
327320

321+
// Both connection types are ready now, let's start our subservers.
328322
err = g.faradayServer.StartAsSubserver(g.lndClient.LndServices)
329323
if err != nil {
330324
return err

0 commit comments

Comments
 (0)