• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 11219354629

07 Oct 2024 03:56PM UTC coverage: 58.585% (-0.2%) from 58.814%
11219354629

Pull #9147

github

ziggie1984
fixup! sqlc: migration up script for payments.
Pull Request #9147: [Part 1|3] Introduce SQL Payment schema into LND

130227 of 222287 relevant lines covered (58.59%)

29106.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.55
/config_builder.go
1
package lnd
2

3
import (
4
        "bytes"
5
        "context"
6
        "database/sql"
7
        "errors"
8
        "fmt"
9
        "net"
10
        "os"
11
        "path/filepath"
12
        "sort"
13
        "strconv"
14
        "strings"
15
        "sync/atomic"
16
        "time"
17

18
        "github.com/btcsuite/btcd/chaincfg"
19
        "github.com/btcsuite/btcd/chaincfg/chainhash"
20
        "github.com/btcsuite/btcd/wire"
21
        "github.com/btcsuite/btclog"
22
        "github.com/btcsuite/btcwallet/chain"
23
        "github.com/btcsuite/btcwallet/waddrmgr"
24
        "github.com/btcsuite/btcwallet/wallet"
25
        "github.com/btcsuite/btcwallet/walletdb"
26
        proxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
27
        "github.com/lightninglabs/neutrino"
28
        "github.com/lightninglabs/neutrino/blockntfns"
29
        "github.com/lightninglabs/neutrino/headerfs"
30
        "github.com/lightninglabs/neutrino/pushtx"
31
        "github.com/lightningnetwork/lnd/blockcache"
32
        "github.com/lightningnetwork/lnd/chainntnfs"
33
        "github.com/lightningnetwork/lnd/chainreg"
34
        "github.com/lightningnetwork/lnd/channeldb"
35
        "github.com/lightningnetwork/lnd/clock"
36
        "github.com/lightningnetwork/lnd/fn"
37
        "github.com/lightningnetwork/lnd/funding"
38
        "github.com/lightningnetwork/lnd/invoices"
39
        "github.com/lightningnetwork/lnd/keychain"
40
        "github.com/lightningnetwork/lnd/kvdb"
41
        "github.com/lightningnetwork/lnd/lncfg"
42
        "github.com/lightningnetwork/lnd/lnrpc"
43
        "github.com/lightningnetwork/lnd/lnwallet"
44
        "github.com/lightningnetwork/lnd/lnwallet/btcwallet"
45
        "github.com/lightningnetwork/lnd/lnwallet/chancloser"
46
        "github.com/lightningnetwork/lnd/lnwallet/rpcwallet"
47
        "github.com/lightningnetwork/lnd/macaroons"
48
        "github.com/lightningnetwork/lnd/msgmux"
49
        "github.com/lightningnetwork/lnd/routing"
50
        "github.com/lightningnetwork/lnd/rpcperms"
51
        "github.com/lightningnetwork/lnd/signal"
52
        "github.com/lightningnetwork/lnd/sqldb"
53
        "github.com/lightningnetwork/lnd/walletunlocker"
54
        "github.com/lightningnetwork/lnd/watchtower"
55
        "github.com/lightningnetwork/lnd/watchtower/wtclient"
56
        "github.com/lightningnetwork/lnd/watchtower/wtdb"
57
        "google.golang.org/grpc"
58
        "gopkg.in/macaroon-bakery.v2/bakery"
59
)
60

61
// GrpcRegistrar is an interface that must be satisfied by an external subserver
62
// that wants to be able to register its own gRPC server onto lnd's main
63
// grpc.Server instance.
64
type GrpcRegistrar interface {
65
        // RegisterGrpcSubserver is called for each net.Listener on which lnd
66
        // creates a grpc.Server instance. External subservers implementing this
67
        // method can then register their own gRPC server structs to the main
68
        // server instance.
69
        RegisterGrpcSubserver(*grpc.Server) error
70
}
71

72
// RestRegistrar is an interface that must be satisfied by an external subserver
73
// that wants to be able to register its own REST mux onto lnd's main
74
// proxy.ServeMux instance.
75
type RestRegistrar interface {
76
        // RegisterRestSubserver is called after lnd creates the main
77
        // proxy.ServeMux instance. External subservers implementing this method
78
        // can then register their own REST proxy stubs to the main server
79
        // instance.
80
        RegisterRestSubserver(context.Context, *proxy.ServeMux, string,
81
                []grpc.DialOption) error
82
}
83

84
// ExternalValidator is an interface that must be satisfied by an external
85
// macaroon validator.
86
type ExternalValidator interface {
87
        macaroons.MacaroonValidator
88

89
        // Permissions returns the permissions that the external validator is
90
        // validating. It is a map between the full HTTP URI of each RPC and its
91
        // required macaroon permissions. If multiple action/entity tuples are
92
        // specified per URI, they are all required. See rpcserver.go for a list
93
        // of valid action and entity values.
94
        Permissions() map[string][]bakery.Op
95
}
96

97
// DatabaseBuilder is an interface that must be satisfied by the implementation
98
// that provides lnd's main database backend instances.
99
type DatabaseBuilder interface {
100
        // BuildDatabase extracts the current databases that we'll use for
101
        // normal operation in the daemon. A function closure that closes all
102
        // opened databases is also returned.
103
        BuildDatabase(ctx context.Context) (*DatabaseInstances, func(), error)
104
}
105

106
// WalletConfigBuilder is an interface that must be satisfied by a custom wallet
107
// implementation.
108
type WalletConfigBuilder interface {
109
        // BuildWalletConfig is responsible for creating or unlocking and then
110
        // fully initializing a wallet.
111
        BuildWalletConfig(context.Context, *DatabaseInstances, *AuxComponents,
112
                *rpcperms.InterceptorChain,
113
                []*ListenerWithSignal) (*chainreg.PartialChainControl,
114
                *btcwallet.Config, func(), error)
115
}
116

117
// ChainControlBuilder is an interface that must be satisfied by a custom wallet
118
// implementation.
119
type ChainControlBuilder interface {
120
        // BuildChainControl is responsible for creating a fully populated chain
121
        // control instance from a wallet.
122
        BuildChainControl(*chainreg.PartialChainControl,
123
                *btcwallet.Config) (*chainreg.ChainControl, func(), error)
124
}
125

126
// ImplementationCfg is a struct that holds all configuration items for
127
// components that can be implemented outside lnd itself.
128
type ImplementationCfg struct {
129
        // GrpcRegistrar is a type that can register additional gRPC subservers
130
        // before the main gRPC server is started.
131
        GrpcRegistrar
132

133
        // RestRegistrar is a type that can register additional REST subservers
134
        // before the main REST proxy is started.
135
        RestRegistrar
136

137
        // ExternalValidator is a type that can provide external macaroon
138
        // validation.
139
        ExternalValidator
140

141
        // DatabaseBuilder is a type that can provide lnd's main database
142
        // backend instances.
143
        DatabaseBuilder
144

145
        // WalletConfigBuilder is a type that can provide a wallet configuration
146
        // with a fully loaded and unlocked wallet.
147
        WalletConfigBuilder
148

149
        // ChainControlBuilder is a type that can provide a custom wallet
150
        // implementation.
151
        ChainControlBuilder
152

153
        // AuxComponents is a set of auxiliary components that can be used by
154
        // lnd for certain custom channel types.
155
        AuxComponents
156
}
157

158
// AuxComponents is a set of auxiliary components that can be used by lnd for
159
// certain custom channel types.
160
type AuxComponents struct {
161
        // AuxLeafStore is an optional data source that can be used by custom
162
        // channels to fetch+store various data.
163
        AuxLeafStore fn.Option[lnwallet.AuxLeafStore]
164

165
        // TrafficShaper is an optional traffic shaper that can be used to
166
        // control the outgoing channel of a payment.
167
        TrafficShaper fn.Option[routing.TlvTrafficShaper]
168

169
        // MsgRouter is an optional message router that if set will be used in
170
        // place of a new blank default message router.
171
        MsgRouter fn.Option[msgmux.Router]
172

173
        // AuxFundingController is an optional controller that can be used to
174
        // modify the way we handle certain custom channel types. It's also
175
        // able to automatically handle new custom protocol messages related to
176
        // the funding process.
177
        AuxFundingController fn.Option[funding.AuxFundingController]
178

179
        // AuxSigner is an optional signer that can be used to sign auxiliary
180
        // leaves for certain custom channel types.
181
        AuxSigner fn.Option[lnwallet.AuxSigner]
182

183
        // AuxDataParser is an optional data parser that can be used to parse
184
        // auxiliary data for certain custom channel types.
185
        AuxDataParser fn.Option[AuxDataParser]
186

187
        // AuxChanCloser is an optional channel closer that can be used to
188
        // modify the way a coop-close transaction is constructed.
189
        AuxChanCloser fn.Option[chancloser.AuxChanCloser]
190
}
191

192
// DefaultWalletImpl is the default implementation of our normal, btcwallet
193
// backed configuration.
194
type DefaultWalletImpl struct {
195
        cfg         *Config
196
        logger      btclog.Logger
197
        interceptor signal.Interceptor
198

199
        watchOnly        bool
200
        migrateWatchOnly bool
201
        pwService        *walletunlocker.UnlockerService
202
}
203

204
// NewDefaultWalletImpl creates a new default wallet implementation.
205
func NewDefaultWalletImpl(cfg *Config, logger btclog.Logger,
206
        interceptor signal.Interceptor, watchOnly bool) *DefaultWalletImpl {
207

208
        return &DefaultWalletImpl{
209
                cfg:         cfg,
210
                logger:      logger,
211
                interceptor: interceptor,
212
                watchOnly:   watchOnly,
213
                pwService:   createWalletUnlockerService(cfg),
214
        }
215
}
2✔
216

2✔
217
// RegisterRestSubserver is called after lnd creates the main proxy.ServeMux
2✔
218
// instance. External subservers implementing this method can then register
2✔
219
// their own REST proxy stubs to the main server instance.
2✔
220
//
2✔
221
// NOTE: This is part of the GrpcRegistrar interface.
2✔
222
func (d *DefaultWalletImpl) RegisterRestSubserver(ctx context.Context,
2✔
223
        mux *proxy.ServeMux, restProxyDest string,
2✔
224
        restDialOpts []grpc.DialOption) error {
2✔
225

226
        return lnrpc.RegisterWalletUnlockerHandlerFromEndpoint(
227
                ctx, mux, restProxyDest, restDialOpts,
228
        )
229
}
230

231
// RegisterGrpcSubserver is called for each net.Listener on which lnd creates a
232
// grpc.Server instance. External subservers implementing this method can then
233
// register their own gRPC server structs to the main server instance.
2✔
234
//
2✔
235
// NOTE: This is part of the GrpcRegistrar interface.
2✔
236
func (d *DefaultWalletImpl) RegisterGrpcSubserver(s *grpc.Server) error {
2✔
237
        lnrpc.RegisterWalletUnlockerServer(s, d.pwService)
2✔
238

2✔
239
        return nil
240
}
241

242
// ValidateMacaroon extracts the macaroon from the context's gRPC metadata,
243
// checks its signature, makes sure all specified permissions for the called
244
// method are contained within and finally ensures all caveat conditions are
245
// met. A non-nil error is returned if any of the checks fail.
2✔
246
//
2✔
247
// NOTE: This is part of the ExternalValidator interface.
2✔
248
func (d *DefaultWalletImpl) ValidateMacaroon(ctx context.Context,
2✔
249
        requiredPermissions []bakery.Op, fullMethod string) error {
2✔
250

251
        // Because the default implementation does not return any permissions,
252
        // we shouldn't be registered as an external validator at all and this
253
        // should never be invoked.
254
        return fmt.Errorf("default implementation does not support external " +
255
                "macaroon validation")
256
}
257

258
// Permissions returns the permissions that the external validator is
×
259
// validating. It is a map between the full HTTP URI of each RPC and its
×
260
// required macaroon permissions. If multiple action/entity tuples are specified
×
261
// per URI, they are all required. See rpcserver.go for a list of valid action
×
262
// and entity values.
×
263
//
×
264
// NOTE: This is part of the ExternalValidator interface.
×
265
func (d *DefaultWalletImpl) Permissions() map[string][]bakery.Op {
×
266
        return nil
267
}
268

269
// BuildWalletConfig is responsible for creating or unlocking and then
270
// fully initializing a wallet.
271
//
272
// NOTE: This is part of the WalletConfigBuilder interface.
273
func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
274
        dbs *DatabaseInstances, aux *AuxComponents,
2✔
275
        interceptorChain *rpcperms.InterceptorChain,
2✔
276
        grpcListeners []*ListenerWithSignal) (*chainreg.PartialChainControl,
2✔
277
        *btcwallet.Config, func(), error) {
278

279
        // Keep track of our various cleanup functions. We use a defer function
280
        // as well to not repeat ourselves with every return statement.
281
        var (
282
                cleanUpTasks []func()
283
                earlyExit    = true
284
                cleanUp      = func() {
285
                        for _, fn := range cleanUpTasks {
286
                                if fn == nil {
2✔
287
                                        continue
2✔
288
                                }
2✔
289

2✔
290
                                fn()
2✔
291
                        }
2✔
292
                }
2✔
293
        )
4✔
294
        defer func() {
4✔
295
                if earlyExit {
2✔
296
                        cleanUp()
×
297
                }
298
        }()
299

2✔
300
        // Initialize a new block cache.
301
        blockCache := blockcache.NewBlockCache(d.cfg.BlockCacheSize)
302

303
        // Before starting the wallet, we'll create and start our Neutrino
4✔
304
        // light client instance, if enabled, in order to allow it to sync
2✔
305
        // while the rest of the daemon continues startup.
×
306
        mainChain := d.cfg.Bitcoin
×
307
        var neutrinoCS *neutrino.ChainService
308
        if mainChain.Node == "neutrino" {
309
                neutrinoBackend, neutrinoCleanUp, err := initNeutrinoBackend(
310
                        ctx, d.cfg, mainChain.ChainDir, blockCache,
2✔
311
                )
2✔
312
                if err != nil {
2✔
313
                        err := fmt.Errorf("unable to initialize neutrino "+
2✔
314
                                "backend: %v", err)
2✔
315
                        d.logger.Error(err)
2✔
316
                        return nil, nil, nil, err
2✔
317
                }
2✔
318
                cleanUpTasks = append(cleanUpTasks, neutrinoCleanUp)
×
319
                neutrinoCS = neutrinoBackend
×
320
        }
×
321

×
322
        var (
×
323
                walletInitParams = walletunlocker.WalletUnlockParams{
×
324
                        // In case we do auto-unlock, we need to be able to send
×
325
                        // into the channel without blocking so we buffer it.
×
326
                        MacResponseChan: make(chan []byte, 1),
×
327
                }
×
328
                privateWalletPw = lnwallet.DefaultPrivatePassphrase
×
329
                publicWalletPw  = lnwallet.DefaultPublicPassphrase
330
        )
331

2✔
332
        // If the user didn't request a seed, then we'll manually assume a
2✔
333
        // wallet birthday of now, as otherwise the seed would've specified
2✔
334
        // this information.
2✔
335
        walletInitParams.Birthday = time.Now()
2✔
336

2✔
337
        d.pwService.SetLoaderOpts([]btcwallet.LoaderOption{dbs.WalletDB})
2✔
338
        d.pwService.SetMacaroonDB(dbs.MacaroonDB)
2✔
339
        walletExists, err := d.pwService.WalletExists()
2✔
340
        if err != nil {
2✔
341
                return nil, nil, nil, err
2✔
342
        }
2✔
343

2✔
344
        if !walletExists {
2✔
345
                interceptorChain.SetWalletNotCreated()
2✔
346
        } else {
2✔
347
                interceptorChain.SetWalletLocked()
2✔
348
        }
2✔
349

2✔
350
        // If we've started in auto unlock mode, then a wallet should already
×
351
        // exist because we don't want to enable the RPC unlocker in that case
×
352
        // for security reasons (an attacker could inject their seed since the
353
        // RPC is unauthenticated). Only if the user explicitly wants to allow
4✔
354
        // wallet creation we don't error out here.
2✔
355
        if d.cfg.WalletUnlockPasswordFile != "" && !walletExists &&
4✔
356
                !d.cfg.WalletUnlockAllowCreate {
2✔
357

2✔
358
                return nil, nil, nil, fmt.Errorf("wallet unlock password file " +
359
                        "was specified but wallet does not exist; initialize " +
360
                        "the wallet before using auto unlocking")
361
        }
362

363
        // What wallet mode are we running in? We've already made sure the no
364
        // seed backup and auto unlock aren't both set during config parsing.
2✔
365
        switch {
2✔
366
        // No seed backup means we're also using the default password.
×
367
        case d.cfg.NoSeedBackup:
×
368
                // We continue normally, the default password has already been
×
369
                // set above.
×
370

×
371
        // A password for unlocking is provided in a file.
372
        case d.cfg.WalletUnlockPasswordFile != "" && walletExists:
373
                d.logger.Infof("Attempting automatic wallet unlock with " +
374
                        "password provided in file")
2✔
375
                pwBytes, err := os.ReadFile(d.cfg.WalletUnlockPasswordFile)
376
                if err != nil {
2✔
377
                        return nil, nil, nil, fmt.Errorf("error reading "+
378
                                "password from file %s: %v",
379
                                d.cfg.WalletUnlockPasswordFile, err)
380
                }
381

×
382
                // Remove any newlines at the end of the file. The lndinit tool
×
383
                // won't ever write a newline but maybe the file was provisioned
×
384
                // by another process or user.
×
385
                pwBytes = bytes.TrimRight(pwBytes, "\r\n")
×
386

×
387
                // We have the password now, we can ask the unlocker service to
×
388
                // do the unlock for us.
×
389
                unlockedWallet, unloadWalletFn, err := d.pwService.LoadAndUnlock(
×
390
                        pwBytes, 0,
391
                )
392
                if err != nil {
393
                        return nil, nil, nil, fmt.Errorf("error unlocking "+
394
                                "wallet with password from file: %v", err)
×
395
                }
×
396

×
397
                cleanUpTasks = append(cleanUpTasks, func() {
×
398
                        if err := unloadWalletFn(); err != nil {
×
399
                                d.logger.Errorf("Could not unload wallet: %v",
×
400
                                        err)
×
401
                        }
×
402
                })
×
403

×
404
                privateWalletPw = pwBytes
×
405
                publicWalletPw = pwBytes
406
                walletInitParams.Wallet = unlockedWallet
×
407
                walletInitParams.UnloadWallet = unloadWalletFn
×
408

×
409
        // If none of the automatic startup options are selected, we fall back
×
410
        // to the default behavior of waiting for the wallet creation/unlocking
×
411
        // over RPC.
412
        default:
413
                if err := d.interceptor.Notifier.NotifyReady(false); err != nil {
×
414
                        return nil, nil, nil, err
×
415
                }
×
416

×
417
                params, err := waitForWalletPassword(
418
                        d.cfg, d.pwService, []btcwallet.LoaderOption{dbs.WalletDB},
419
                        d.interceptor.ShutdownChannel(),
420
                )
421
                if err != nil {
2✔
422
                        err := fmt.Errorf("unable to set up wallet password "+
2✔
423
                                "listeners: %v", err)
×
424
                        d.logger.Error(err)
×
425
                        return nil, nil, nil, err
426
                }
2✔
427

2✔
428
                walletInitParams = *params
2✔
429
                privateWalletPw = walletInitParams.Password
2✔
430
                publicWalletPw = walletInitParams.Password
2✔
431
                cleanUpTasks = append(cleanUpTasks, func() {
×
432
                        if err := walletInitParams.UnloadWallet(); err != nil {
×
433
                                d.logger.Errorf("Could not unload wallet: %v",
×
434
                                        err)
×
435
                        }
×
436
                })
437

2✔
438
                if walletInitParams.RecoveryWindow > 0 {
2✔
439
                        d.logger.Infof("Wallet recovery mode enabled with "+
2✔
440
                                "address lookahead of %d addresses",
4✔
441
                                walletInitParams.RecoveryWindow)
2✔
442
                }
×
443
        }
×
444

×
445
        var macaroonService *macaroons.Service
446
        if !d.cfg.NoMacaroons {
447
                // Create the macaroon authentication/authorization service.
4✔
448
                rootKeyStore, err := macaroons.NewRootKeyStorage(dbs.MacaroonDB)
2✔
449
                if err != nil {
2✔
450
                        return nil, nil, nil, err
2✔
451
                }
2✔
452
                macaroonService, err = macaroons.NewService(
453
                        rootKeyStore, "lnd", walletInitParams.StatelessInit,
454
                        macaroons.IPLockChecker,
2✔
455
                        macaroons.CustomChecker(interceptorChain),
4✔
456
                )
2✔
457
                if err != nil {
2✔
458
                        err := fmt.Errorf("unable to set up macaroon "+
2✔
459
                                "authentication: %v", err)
×
460
                        d.logger.Error(err)
×
461
                        return nil, nil, nil, err
2✔
462
                }
2✔
463
                cleanUpTasks = append(cleanUpTasks, func() {
2✔
464
                        if err := macaroonService.Close(); err != nil {
2✔
465
                                d.logger.Errorf("Could not close macaroon "+
2✔
466
                                        "service: %v", err)
2✔
467
                        }
×
468
                })
×
469

×
470
                // Try to unlock the macaroon store with the private password.
×
471
                // Ignore ErrAlreadyUnlocked since it could be unlocked by the
×
472
                // wallet unlocker.
4✔
473
                err = macaroonService.CreateUnlock(&privateWalletPw)
2✔
474
                if err != nil && err != macaroons.ErrAlreadyUnlocked {
×
475
                        err := fmt.Errorf("unable to unlock macaroons: %w", err)
×
476
                        d.logger.Error(err)
×
477
                        return nil, nil, nil, err
478
                }
479

480
                // If we have a macaroon root key from the init wallet params,
481
                // set the root key before baking any macaroons.
482
                if len(walletInitParams.MacRootKey) > 0 {
2✔
483
                        err := macaroonService.SetRootKey(
2✔
484
                                walletInitParams.MacRootKey,
×
485
                        )
×
486
                        if err != nil {
×
487
                                return nil, nil, nil, err
×
488
                        }
489
                }
490

491
                // Send an admin macaroon to all our listeners that requested
2✔
492
                // one by setting a non-nil macaroon channel.
×
493
                adminMacBytes, err := bakeMacaroon(
×
494
                        ctx, macaroonService, adminPermissions(),
×
495
                )
×
496
                if err != nil {
×
497
                        return nil, nil, nil, err
×
498
                }
499
                for _, lis := range grpcListeners {
500
                        if lis.MacChan != nil {
501
                                lis.MacChan <- adminMacBytes
502
                        }
2✔
503
                }
2✔
504

2✔
505
                // In case we actually needed to unlock the wallet, we now need
2✔
506
                // to create an instance of the admin macaroon and send it to
×
507
                // the unlocker so it can forward it to the user. In no seed
×
508
                // backup mode, there's nobody listening on the channel and we'd
4✔
509
                // block here forever.
2✔
510
                if !d.cfg.NoSeedBackup {
×
511
                        // The channel is buffered by one element so writing
×
512
                        // should not block here.
513
                        walletInitParams.MacResponseChan <- adminMacBytes
514
                }
515

516
                // If the user requested a stateless initialization, no macaroon
517
                // files should be created.
518
                if !walletInitParams.StatelessInit {
519
                        // Create default macaroon files for lncli to use if
4✔
520
                        // they don't exist.
2✔
521
                        err = genDefaultMacaroons(
2✔
522
                                ctx, macaroonService, d.cfg.AdminMacPath,
2✔
523
                                d.cfg.ReadMacPath, d.cfg.InvoiceMacPath,
2✔
524
                        )
525
                        if err != nil {
526
                                err := fmt.Errorf("unable to create macaroons "+
527
                                        "%v", err)
4✔
528
                                d.logger.Error(err)
2✔
529
                                return nil, nil, nil, err
2✔
530
                        }
2✔
531
                }
2✔
532

2✔
533
                // As a security service to the user, if they requested
2✔
534
                // stateless initialization and there are macaroon files on disk
2✔
535
                // we log a warning.
×
536
                if walletInitParams.StatelessInit {
×
537
                        msg := "Found %s macaroon on disk (%s) even though " +
×
538
                                "--stateless_init was requested. Unencrypted " +
×
539
                                "state is accessible by the host system. You " +
×
540
                                "should change the password and use " +
541
                                "--new_mac_root_key with --stateless_init to " +
542
                                "clean up and invalidate old macaroons."
543

544
                        if lnrpc.FileExists(d.cfg.AdminMacPath) {
545
                                d.logger.Warnf(msg, "admin", d.cfg.AdminMacPath)
4✔
546
                        }
2✔
547
                        if lnrpc.FileExists(d.cfg.ReadMacPath) {
2✔
548
                                d.logger.Warnf(msg, "readonly", d.cfg.ReadMacPath)
2✔
549
                        }
2✔
550
                        if lnrpc.FileExists(d.cfg.InvoiceMacPath) {
2✔
551
                                d.logger.Warnf(msg, "invoice", d.cfg.InvoiceMacPath)
2✔
552
                        }
2✔
553
                }
2✔
554

×
555
                // We add the macaroon service to our RPC interceptor. This
×
556
                // will start checking macaroons against permissions on every
2✔
557
                // RPC invocation.
×
558
                interceptorChain.AddMacaroonService(macaroonService)
×
559
        }
2✔
560

×
561
        // Now that the wallet password has been provided, transition the RPC
×
562
        // state into Unlocked.
563
        interceptorChain.SetWalletUnlocked()
564

565
        // Since calls to the WalletUnlocker service wait for a response on the
566
        // macaroon channel, we close it here to make sure they return in case
567
        // we did not return the admin macaroon above. This will be the case if
2✔
568
        // --no-macaroons is used.
569
        close(walletInitParams.MacResponseChan)
570

571
        // We'll also close all the macaroon channels since lnd is done sending
572
        // macaroon data over it.
2✔
573
        for _, lis := range grpcListeners {
2✔
574
                if lis.MacChan != nil {
2✔
575
                        close(lis.MacChan)
2✔
576
                }
2✔
577
        }
2✔
578

2✔
579
        // With the information parsed from the configuration, create valid
2✔
580
        // instances of the pertinent interfaces required to operate the
2✔
581
        // Lightning Network Daemon.
2✔
582
        //
4✔
583
        // When we create the chain control, we need storage for the height
2✔
584
        // hints and also the wallet itself, for these two we want them to be
×
585
        // replicated, so we'll pass in the remote channel DB instance.
×
586
        chainControlCfg := &chainreg.Config{
587
                Bitcoin:                     d.cfg.Bitcoin,
588
                HeightHintCacheQueryDisable: d.cfg.HeightHintCacheQueryDisable,
589
                NeutrinoMode:                d.cfg.NeutrinoMode,
590
                BitcoindMode:                d.cfg.BitcoindMode,
591
                BtcdMode:                    d.cfg.BtcdMode,
592
                HeightHintDB:                dbs.HeightHintDB,
593
                ChanStateDB:                 dbs.ChanStateDB.ChannelStateDB(),
594
                NeutrinoCS:                  neutrinoCS,
595
                AuxLeafStore:                aux.AuxLeafStore,
2✔
596
                AuxSigner:                   aux.AuxSigner,
2✔
597
                ActiveNetParams:             d.cfg.ActiveNetParams,
2✔
598
                FeeURL:                      d.cfg.FeeURL,
2✔
599
                Fee: &lncfg.Fee{
2✔
600
                        URL:              d.cfg.Fee.URL,
2✔
601
                        MinUpdateTimeout: d.cfg.Fee.MinUpdateTimeout,
2✔
602
                        MaxUpdateTimeout: d.cfg.Fee.MaxUpdateTimeout,
2✔
603
                },
2✔
604
                Dialer: func(addr string) (net.Conn, error) {
2✔
605
                        return d.cfg.net.Dial(
2✔
606
                                "tcp", addr, d.cfg.ConnectionTimeout,
2✔
607
                        )
2✔
608
                },
2✔
609
                BlockCache:         blockCache,
2✔
610
                WalletUnlockParams: &walletInitParams,
2✔
611
        }
2✔
612

2✔
613
        // Let's go ahead and create the partial chain control now that is only
2✔
614
        // dependent on our configuration and doesn't require any wallet
×
615
        // specific information.
×
616
        partialChainControl, pccCleanup, err := chainreg.NewPartialChainControl(
×
617
                chainControlCfg,
×
618
        )
619
        cleanUpTasks = append(cleanUpTasks, pccCleanup)
620
        if err != nil {
621
                err := fmt.Errorf("unable to create partial chain control: %w",
622
                        err)
623
                d.logger.Error(err)
624
                return nil, nil, nil, err
625
        }
2✔
626

2✔
627
        walletConfig := &btcwallet.Config{
2✔
628
                PrivatePass:      privateWalletPw,
2✔
629
                PublicPass:       publicWalletPw,
2✔
630
                Birthday:         walletInitParams.Birthday,
×
631
                RecoveryWindow:   walletInitParams.RecoveryWindow,
×
632
                NetParams:        d.cfg.ActiveNetParams.Params,
×
633
                CoinType:         d.cfg.ActiveNetParams.CoinType,
×
634
                Wallet:           walletInitParams.Wallet,
×
635
                LoaderOptions:    []btcwallet.LoaderOption{dbs.WalletDB},
636
                ChainSource:      partialChainControl.ChainSource,
2✔
637
                WatchOnly:        d.watchOnly,
2✔
638
                MigrateWatchOnly: d.migrateWatchOnly,
2✔
639
        }
2✔
640

2✔
641
        // Parse coin selection strategy.
2✔
642
        switch d.cfg.CoinSelectionStrategy {
2✔
643
        case "largest":
2✔
644
                walletConfig.CoinSelectionStrategy = wallet.CoinSelectionLargest
2✔
645

2✔
646
        case "random":
2✔
647
                walletConfig.CoinSelectionStrategy = wallet.CoinSelectionRandom
2✔
648

2✔
649
        default:
2✔
650
                return nil, nil, nil, fmt.Errorf("unknown coin selection "+
2✔
651
                        "strategy %v", d.cfg.CoinSelectionStrategy)
2✔
652
        }
2✔
653

2✔
654
        earlyExit = false
655
        return partialChainControl, walletConfig, cleanUp, nil
×
656
}
×
657

658
// proxyBlockEpoch proxies a block epoch subsections to the underlying neutrino
×
659
// rebroadcaster client.
×
660
func proxyBlockEpoch(
×
661
        notifier chainntnfs.ChainNotifier) func() (*blockntfns.Subscription,
662
        error) {
663

2✔
664
        return func() (*blockntfns.Subscription, error) {
2✔
665
                blockEpoch, err := notifier.RegisterBlockEpochNtfn(
666
                        nil,
667
                )
668
                if err != nil {
669
                        return nil, err
670
                }
671

2✔
672
                sub := blockntfns.Subscription{
2✔
673
                        Notifications: make(chan blockntfns.BlockNtfn, 6),
4✔
674
                        Cancel:        blockEpoch.Cancel,
2✔
675
                }
2✔
676
                go func() {
2✔
677
                        for blk := range blockEpoch.Epochs {
2✔
678
                                ntfn := blockntfns.NewBlockConnected(
×
679
                                        *blk.BlockHeader,
×
680
                                        uint32(blk.Height),
681
                                )
2✔
682

2✔
683
                                sub.Notifications <- ntfn
2✔
684
                        }
2✔
685
                }()
4✔
686

4✔
687
                return &sub, nil
2✔
688
        }
2✔
689
}
2✔
690

2✔
691
// walletReBroadcaster is a simple wrapper around the pushtx.Broadcaster
2✔
692
// interface to adhere to the expanded lnwallet.Rebroadcaster interface.
2✔
693
type walletReBroadcaster struct {
2✔
694
        started atomic.Bool
695

696
        *pushtx.Broadcaster
2✔
697
}
698

699
// newWalletReBroadcaster creates a new instance of the walletReBroadcaster.
700
func newWalletReBroadcaster(
701
        broadcaster *pushtx.Broadcaster) *walletReBroadcaster {
702

703
        return &walletReBroadcaster{
704
                Broadcaster: broadcaster,
705
        }
706
}
707

708
// Start launches all goroutines the rebroadcaster needs to operate.
709
func (w *walletReBroadcaster) Start() error {
710
        defer w.started.Store(true)
2✔
711

2✔
712
        return w.Broadcaster.Start()
2✔
713
}
2✔
714

2✔
715
// Started returns true if the broadcaster is already active.
2✔
716
func (w *walletReBroadcaster) Started() bool {
717
        return w.started.Load()
718
}
2✔
719

2✔
720
// BuildChainControl is responsible for creating a fully populated chain
2✔
721
// control instance from a wallet.
2✔
722
//
2✔
723
// NOTE: This is part of the ChainControlBuilder interface.
724
func (d *DefaultWalletImpl) BuildChainControl(
725
        partialChainControl *chainreg.PartialChainControl,
2✔
726
        walletConfig *btcwallet.Config) (*chainreg.ChainControl, func(), error) {
2✔
727

2✔
728
        walletController, err := btcwallet.New(
729
                *walletConfig, partialChainControl.Cfg.BlockCache,
730
        )
731
        if err != nil {
732
                err := fmt.Errorf("unable to create wallet controller: %w", err)
733
                d.logger.Error(err)
734
                return nil, nil, err
735
        }
2✔
736

2✔
737
        keyRing := keychain.NewBtcWalletKeyRing(
2✔
738
                walletController.InternalWallet(), walletConfig.CoinType,
2✔
739
        )
2✔
740

2✔
741
        // Create, and start the lnwallet, which handles the core payment
×
742
        // channel logic, and exposes control via proxy state machines.
×
743
        lnWalletConfig := lnwallet.Config{
×
744
                Database:              partialChainControl.Cfg.ChanStateDB,
×
745
                Notifier:              partialChainControl.ChainNotifier,
746
                WalletController:      walletController,
2✔
747
                Signer:                walletController,
2✔
748
                FeeEstimator:          partialChainControl.FeeEstimator,
2✔
749
                SecretKeyRing:         keyRing,
2✔
750
                ChainIO:               walletController,
2✔
751
                NetParams:             *walletConfig.NetParams,
2✔
752
                CoinSelectionStrategy: walletConfig.CoinSelectionStrategy,
2✔
753
                AuxLeafStore:          partialChainControl.Cfg.AuxLeafStore,
2✔
754
                AuxSigner:             partialChainControl.Cfg.AuxSigner,
2✔
755
        }
2✔
756

2✔
757
        // The broadcast is already always active for neutrino nodes, so we
2✔
758
        // don't want to create a rebroadcast loop.
2✔
759
        if partialChainControl.Cfg.NeutrinoCS == nil {
2✔
760
                cs := partialChainControl.ChainSource
2✔
761
                broadcastCfg := pushtx.Config{
2✔
762
                        Broadcast: func(tx *wire.MsgTx) error {
2✔
763
                                _, err := cs.SendRawTransaction(
2✔
764
                                        tx, true,
2✔
765
                                )
2✔
766

2✔
767
                                return err
2✔
768
                        },
4✔
769
                        SubscribeBlocks: proxyBlockEpoch(
2✔
770
                                partialChainControl.ChainNotifier,
2✔
771
                        ),
4✔
772
                        RebroadcastInterval: pushtx.DefaultRebroadcastInterval,
2✔
773
                        // In case the backend is different from neutrino we
2✔
774
                        // make sure that broadcast backend errors are mapped
2✔
775
                        // to the neutrino broadcastErr.
2✔
776
                        MapCustomBroadcastError: func(err error) error {
2✔
777
                                rpcErr := cs.MapRPCErr(err)
2✔
778
                                return broadcastErrorMapper(rpcErr)
779
                        },
780
                }
781

782
                lnWalletConfig.Rebroadcaster = newWalletReBroadcaster(
783
                        pushtx.NewBroadcaster(&broadcastCfg),
784
                )
785
        }
2✔
786

2✔
787
        // We've created the wallet configuration now, so we can finish
2✔
788
        // initializing the main chain control.
2✔
789
        activeChainControl, cleanUp, err := chainreg.NewChainControl(
790
                lnWalletConfig, walletController, partialChainControl,
791
        )
2✔
792
        if err != nil {
2✔
793
                err := fmt.Errorf("unable to create chain control: %w", err)
2✔
794
                d.logger.Error(err)
795
                return nil, nil, err
796
        }
797

798
        return activeChainControl, cleanUp, nil
2✔
799
}
2✔
800

2✔
801
// RPCSignerWalletImpl is a wallet implementation that uses a remote signer over
2✔
802
// an RPC interface.
×
803
type RPCSignerWalletImpl struct {
×
804
        // DefaultWalletImpl is the embedded instance of the default
×
805
        // implementation that the remote signer uses as its watch-only wallet
×
806
        // for keeping track of addresses and UTXOs.
807
        *DefaultWalletImpl
2✔
808
}
809

810
// NewRPCSignerWalletImpl creates a new instance of the remote signing wallet
811
// implementation.
812
func NewRPCSignerWalletImpl(cfg *Config, logger btclog.Logger,
813
        interceptor signal.Interceptor,
814
        migrateWatchOnly bool) *RPCSignerWalletImpl {
815

816
        return &RPCSignerWalletImpl{
817
                DefaultWalletImpl: &DefaultWalletImpl{
818
                        cfg:              cfg,
819
                        logger:           logger,
820
                        interceptor:      interceptor,
821
                        watchOnly:        true,
822
                        migrateWatchOnly: migrateWatchOnly,
823
                        pwService:        createWalletUnlockerService(cfg),
2✔
824
                },
2✔
825
        }
2✔
826
}
2✔
827

2✔
828
// BuildChainControl is responsible for creating or unlocking and then fully
2✔
829
// initializing a wallet and returning it as part of a fully populated chain
2✔
830
// control instance.
2✔
831
//
2✔
832
// NOTE: This is part of the ChainControlBuilder interface.
2✔
833
func (d *RPCSignerWalletImpl) BuildChainControl(
2✔
834
        partialChainControl *chainreg.PartialChainControl,
2✔
835
        walletConfig *btcwallet.Config) (*chainreg.ChainControl, func(), error) {
2✔
836

837
        walletController, err := btcwallet.New(
838
                *walletConfig, partialChainControl.Cfg.BlockCache,
839
        )
840
        if err != nil {
841
                err := fmt.Errorf("unable to create wallet controller: %w", err)
842
                d.logger.Error(err)
843
                return nil, nil, err
844
        }
2✔
845

2✔
846
        baseKeyRing := keychain.NewBtcWalletKeyRing(
2✔
847
                walletController.InternalWallet(), walletConfig.CoinType,
2✔
848
        )
2✔
849

2✔
850
        rpcKeyRing, err := rpcwallet.NewRPCKeyRing(
×
851
                baseKeyRing, walletController,
×
852
                d.DefaultWalletImpl.cfg.RemoteSigner, walletConfig.NetParams,
×
853
        )
×
854
        if err != nil {
855
                err := fmt.Errorf("unable to create RPC remote signing wallet "+
2✔
856
                        "%v", err)
2✔
857
                d.logger.Error(err)
2✔
858
                return nil, nil, err
2✔
859
        }
2✔
860

2✔
861
        // Create, and start the lnwallet, which handles the core payment
2✔
862
        // channel logic, and exposes control via proxy state machines.
2✔
863
        lnWalletConfig := lnwallet.Config{
2✔
864
                Database:              partialChainControl.Cfg.ChanStateDB,
×
865
                Notifier:              partialChainControl.ChainNotifier,
×
866
                WalletController:      rpcKeyRing,
×
867
                Signer:                rpcKeyRing,
×
868
                FeeEstimator:          partialChainControl.FeeEstimator,
×
869
                SecretKeyRing:         rpcKeyRing,
870
                ChainIO:               walletController,
871
                NetParams:             *walletConfig.NetParams,
872
                CoinSelectionStrategy: walletConfig.CoinSelectionStrategy,
2✔
873
        }
2✔
874

2✔
875
        // We've created the wallet configuration now, so we can finish
2✔
876
        // initializing the main chain control.
2✔
877
        activeChainControl, cleanUp, err := chainreg.NewChainControl(
2✔
878
                lnWalletConfig, rpcKeyRing, partialChainControl,
2✔
879
        )
2✔
880
        if err != nil {
2✔
881
                err := fmt.Errorf("unable to create chain control: %w", err)
2✔
882
                d.logger.Error(err)
2✔
883
                return nil, nil, err
2✔
884
        }
2✔
885

2✔
886
        return activeChainControl, cleanUp, nil
2✔
887
}
2✔
888

2✔
889
// DatabaseInstances is a struct that holds all instances to the actual
2✔
890
// databases that are used in lnd.
×
891
type DatabaseInstances struct {
×
892
        // GraphDB is the database that stores the channel graph used for path
×
893
        // finding.
×
894
        //
895
        // NOTE/TODO: This currently _needs_ to be the same instance as the
2✔
896
        // ChanStateDB below until the separation of the two databases is fully
897
        // complete!
898
        GraphDB *channeldb.DB
899

900
        // ChanStateDB is the database that stores all of our node's channel
901
        // state.
902
        //
903
        // NOTE/TODO: This currently _needs_ to be the same instance as the
904
        // GraphDB above until the separation of the two databases is fully
905
        // complete!
906
        ChanStateDB *channeldb.DB
907

908
        // HeightHintDB is the database that stores height hints for spends.
909
        HeightHintDB kvdb.Backend
910

911
        // InvoiceDB is the database that stores information about invoices.
912
        InvoiceDB invoices.InvoiceDB
913

914
        // MacaroonDB is the database that stores macaroon root keys.
915
        MacaroonDB kvdb.Backend
916

917
        // DecayedLogDB is the database that stores p2p related encryption
918
        // information.
919
        DecayedLogDB kvdb.Backend
920

921
        // TowerClientDB is the database that stores the watchtower client's
922
        // configuration.
923
        TowerClientDB wtclient.DB
924

925
        // TowerServerDB is the database that stores the watchtower server's
926
        // configuration.
927
        TowerServerDB watchtower.DB
928

929
        // WalletDB is the configuration for loading the wallet database using
930
        // the btcwallet's loader.
931
        WalletDB btcwallet.LoaderOption
932

933
        // NativeSQLStore is a pointer to a native SQL store that can be used
934
        // for native SQL queries for tables that already support it. This may
935
        // be nil if the use-native-sql flag was not set.
936
        NativeSQLStore *sqldb.BaseDB
937
}
938

939
// DefaultDatabaseBuilder is a type that builds the default database backends
940
// for lnd, using the given configuration to decide what actual implementation
941
// to use.
942
type DefaultDatabaseBuilder struct {
943
        cfg    *Config
944
        logger btclog.Logger
945
}
946

947
// NewDefaultDatabaseBuilder returns a new instance of the default database
948
// builder.
949
func NewDefaultDatabaseBuilder(cfg *Config,
950
        logger btclog.Logger) *DefaultDatabaseBuilder {
951

952
        return &DefaultDatabaseBuilder{
953
                cfg:    cfg,
954
                logger: logger,
955
        }
956
}
957

958
// BuildDatabase extracts the current databases that we'll use for normal
959
// operation in the daemon. A function closure that closes all opened databases
2✔
960
// is also returned.
2✔
961
func (d *DefaultDatabaseBuilder) BuildDatabase(
2✔
962
        ctx context.Context) (*DatabaseInstances, func(), error) {
2✔
963

2✔
964
        d.logger.Infof("Opening the main database, this might take a few " +
2✔
965
                "minutes...")
2✔
966

967
        cfg := d.cfg
968
        if cfg.DB.Backend == lncfg.BoltBackend {
969
                d.logger.Infof("Opening bbolt database, sync_freelist=%v, "+
970
                        "auto_compact=%v", !cfg.DB.Bolt.NoFreelistSync,
971
                        cfg.DB.Bolt.AutoCompact)
2✔
972
        }
2✔
973

2✔
974
        startOpenTime := time.Now()
2✔
975

2✔
976
        databaseBackends, err := cfg.DB.GetBackends(
2✔
977
                ctx, cfg.graphDatabaseDir(), cfg.networkDir, filepath.Join(
4✔
978
                        cfg.Watchtower.TowerDir, BitcoinChainName,
2✔
979
                        lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name),
2✔
980
                ), cfg.WtClient.Active, cfg.Watchtower.Active, d.logger,
2✔
981
        )
2✔
982
        if err != nil {
983
                return nil, nil, fmt.Errorf("unable to obtain database "+
2✔
984
                        "backends: %v", err)
2✔
985
        }
2✔
986

2✔
987
        // With the full remote mode we made sure both the graph and channel
2✔
988
        // state DB point to the same local or remote DB and the same namespace
2✔
989
        // within that DB.
2✔
990
        dbs := &DatabaseInstances{
2✔
991
                HeightHintDB:   databaseBackends.HeightHintDB,
2✔
992
                MacaroonDB:     databaseBackends.MacaroonDB,
×
993
                DecayedLogDB:   databaseBackends.DecayedLogDB,
×
994
                WalletDB:       databaseBackends.WalletDB,
×
995
                NativeSQLStore: databaseBackends.NativeSQLStore,
996
        }
997
        cleanUp := func() {
998
                // We can just close the returned close functions directly. Even
999
                // if we decorate the channel DB with an additional struct, its
2✔
1000
                // close function still just points to the kvdb backend.
2✔
1001
                for name, closeFunc := range databaseBackends.CloseFuncs {
2✔
1002
                        if err := closeFunc(); err != nil {
2✔
1003
                                d.logger.Errorf("Error closing %s "+
2✔
1004
                                        "database: %v", name, err)
2✔
1005
                        }
2✔
1006
                }
4✔
1007
        }
2✔
1008
        if databaseBackends.Remote {
2✔
1009
                d.logger.Infof("Using remote %v database! Creating "+
2✔
1010
                        "graph and channel state DB instances", cfg.DB.Backend)
4✔
1011
        } else {
2✔
1012
                d.logger.Infof("Creating local graph and channel state DB " +
×
1013
                        "instances")
×
1014
        }
×
1015

1016
        dbOptions := []channeldb.OptionModifier{
1017
                channeldb.OptionSetRejectCacheSize(cfg.Caches.RejectCacheSize),
2✔
1018
                channeldb.OptionSetChannelCacheSize(
×
1019
                        cfg.Caches.ChannelCacheSize,
×
1020
                ),
2✔
1021
                channeldb.OptionSetBatchCommitInterval(
2✔
1022
                        cfg.DB.BatchCommitInterval,
2✔
1023
                ),
2✔
1024
                channeldb.OptionDryRunMigration(cfg.DryRunMigration),
1025
                channeldb.OptionSetUseGraphCache(!cfg.DB.NoGraphCache),
2✔
1026
                channeldb.OptionKeepFailedPaymentAttempts(
2✔
1027
                        cfg.KeepFailedPaymentAttempts,
2✔
1028
                ),
2✔
1029
                channeldb.OptionStoreFinalHtlcResolutions(
2✔
1030
                        cfg.StoreFinalHtlcResolutions,
2✔
1031
                ),
2✔
1032
                channeldb.OptionPruneRevocationLog(cfg.DB.PruneRevocation),
2✔
1033
                channeldb.OptionNoRevLogAmtData(cfg.DB.NoRevLogAmtData),
2✔
1034
        }
2✔
1035

2✔
1036
        // We want to pre-allocate the channel graph cache according to what we
2✔
1037
        // expect for mainnet to speed up memory allocation.
2✔
1038
        if cfg.ActiveNetParams.Name == chaincfg.MainNetParams.Name {
2✔
1039
                dbOptions = append(
2✔
1040
                        dbOptions, channeldb.OptionSetPreAllocCacheNumNodes(
2✔
1041
                                channeldb.DefaultPreAllocCacheNumNodes,
2✔
1042
                        ),
2✔
1043
                )
2✔
1044
        }
2✔
1045

2✔
1046
        // Otherwise, we'll open two instances, one for the state we only need
2✔
1047
        // locally, and the other for things we want to ensure are replicated.
2✔
1048
        dbs.GraphDB, err = channeldb.CreateWithBackend(
×
1049
                databaseBackends.GraphDB, dbOptions...,
×
1050
        )
×
1051
        switch {
×
1052
        // Give the DB a chance to dry run the migration. Since we know that
×
1053
        // both the channel state and graph DBs are still always behind the same
×
1054
        // backend, we know this would be applied to both of those DBs.
1055
        case err == channeldb.ErrDryRunMigrationOK:
1056
                d.logger.Infof("Graph DB dry run migration successful")
1057
                return nil, nil, err
2✔
1058

2✔
1059
        case err != nil:
2✔
1060
                cleanUp()
2✔
1061

1062
                err := fmt.Errorf("unable to open graph DB: %w", err)
1063
                d.logger.Error(err)
1064
                return nil, nil, err
×
1065
        }
×
1066

×
1067
        // For now, we don't _actually_ split the graph and channel state DBs on
1068
        // the code level. Since they both are based upon the *channeldb.DB
×
1069
        // struct it will require more refactoring to fully separate them. With
×
1070
        // the full remote mode we at least know for now that they both point to
×
1071
        // the same DB backend (and also namespace within that) so we only need
×
1072
        // to apply any migration once.
×
1073
        //
×
1074
        // TODO(guggero): Once the full separation of anything graph related
1075
        // from the channeldb.DB is complete, the decorated instance of the
1076
        // channel state DB should be created here individually instead of just
1077
        // using the same struct (and DB backend) instance.
1078
        dbs.ChanStateDB = dbs.GraphDB
1079

1080
        // Instantiate a native SQL invoice store if the flag is set.
1081
        if d.cfg.DB.UseNativeSQL {
1082
                // KV invoice db resides in the same database as the graph and
1083
                // channel state DB. Let's query the database to see if we have
1084
                // any invoices there. If we do, we won't allow the user to
1085
                // start lnd with native SQL enabled, as we don't currently
1086
                // migrate the invoices to the new database schema.
1087
                invoiceSlice, err := dbs.GraphDB.QueryInvoices(
2✔
1088
                        ctx, invoices.InvoiceQuery{
2✔
1089
                                NumMaxInvoices: 1,
2✔
1090
                        },
2✔
1091
                )
×
1092
                if err != nil {
×
1093
                        cleanUp()
×
1094
                        d.logger.Errorf("Unable to query KV invoice DB: %v",
×
1095
                                err)
×
1096

×
1097
                        return nil, nil, err
×
1098
                }
×
1099

×
1100
                if len(invoiceSlice.Invoices) > 0 {
×
1101
                        cleanUp()
×
1102
                        err := fmt.Errorf("found invoices in the KV invoice " +
×
1103
                                "DB, migration to native SQL is not yet " +
×
1104
                                "supported")
×
1105
                        d.logger.Error(err)
×
1106

×
1107
                        return nil, nil, err
×
1108
                }
1109

×
1110
                executor := sqldb.NewTransactionExecutor(
×
1111
                        dbs.NativeSQLStore,
×
1112
                        func(tx *sql.Tx) invoices.SQLInvoiceQueries {
×
1113
                                return dbs.NativeSQLStore.WithTx(tx)
×
1114
                        },
×
1115
                )
×
1116

×
1117
                dbs.InvoiceDB = invoices.NewSQLStore(
×
1118
                        executor, clock.NewDefaultClock(),
1119
                )
×
1120
        } else {
×
1121
                dbs.InvoiceDB = dbs.GraphDB
×
1122
        }
×
1123

×
1124
        // Wrap the watchtower client DB and make sure we clean up.
1125
        if cfg.WtClient.Active {
1126
                dbs.TowerClientDB, err = wtdb.OpenClientDB(
×
1127
                        databaseBackends.TowerClientDB,
×
1128
                )
×
1129
                if err != nil {
2✔
1130
                        cleanUp()
2✔
1131

2✔
1132
                        err := fmt.Errorf("unable to open %s database: %w",
1133
                                lncfg.NSTowerClientDB, err)
1134
                        d.logger.Error(err)
4✔
1135
                        return nil, nil, err
2✔
1136
                }
2✔
1137
        }
2✔
1138

2✔
1139
        // Wrap the watchtower server DB and make sure we clean up.
×
1140
        if cfg.Watchtower.Active {
×
1141
                dbs.TowerServerDB, err = wtdb.OpenTowerDB(
×
1142
                        databaseBackends.TowerServerDB,
×
1143
                )
×
1144
                if err != nil {
×
1145
                        cleanUp()
×
1146

1147
                        err := fmt.Errorf("unable to open %s database: %w",
1148
                                lncfg.NSTowerServerDB, err)
1149
                        d.logger.Error(err)
4✔
1150
                        return nil, nil, err
2✔
1151
                }
2✔
1152
        }
2✔
1153

2✔
1154
        openTime := time.Since(startOpenTime)
×
1155
        d.logger.Infof("Database(s) now open (time_to_open=%v)!", openTime)
×
1156

×
1157
        return dbs, cleanUp, nil
×
1158
}
×
1159

×
1160
// waitForWalletPassword blocks until a password is provided by the user to
×
1161
// this RPC server.
1162
func waitForWalletPassword(cfg *Config,
1163
        pwService *walletunlocker.UnlockerService,
2✔
1164
        loaderOpts []btcwallet.LoaderOption, shutdownChan <-chan struct{}) (
2✔
1165
        *walletunlocker.WalletUnlockParams, error) {
2✔
1166

2✔
1167
        // Wait for user to provide the password.
1168
        ltndLog.Infof("Waiting for wallet encryption password. Use `lncli " +
1169
                "create` to create a wallet, `lncli unlock` to unlock an " +
1170
                "existing wallet, or `lncli changepassword` to change the " +
1171
                "password of an existing wallet and unlock it.")
1172

1173
        // We currently don't distinguish between getting a password to be used
1174
        // for creation or unlocking, as a new wallet db will be created if
2✔
1175
        // none exists when creating the chain control.
2✔
1176
        select {
2✔
1177
        // The wallet is being created for the first time, we'll check to see
2✔
1178
        // if the user provided any entropy for seed creation. If so, then
2✔
1179
        // we'll create the wallet early to load the seed.
2✔
1180
        case initMsg := <-pwService.InitMsgs:
2✔
1181
                password := initMsg.Passphrase
2✔
1182
                cipherSeed := initMsg.WalletSeed
2✔
1183
                extendedKey := initMsg.WalletExtendedKey
2✔
1184
                watchOnlyAccounts := initMsg.WatchOnlyAccounts
2✔
1185
                recoveryWindow := initMsg.RecoveryWindow
2✔
1186

1187
                // Before we proceed, we'll check the internal version of the
1188
                // seed. If it's greater than the current key derivation
1189
                // version, then we'll return an error as we don't understand
2✔
1190
                // this.
2✔
1191
                if cipherSeed != nil &&
2✔
1192
                        !keychain.IsKnownVersion(cipherSeed.InternalVersion) {
2✔
1193

2✔
1194
                        return nil, fmt.Errorf("invalid internal "+
2✔
1195
                                "seed version %v, current max version is %v",
2✔
1196
                                cipherSeed.InternalVersion,
2✔
1197
                                keychain.CurrentKeyDerivationVersion)
2✔
1198
                }
2✔
1199

2✔
1200
                loader, err := btcwallet.NewWalletLoader(
2✔
1201
                        cfg.ActiveNetParams.Params, recoveryWindow,
2✔
1202
                        loaderOpts...,
×
1203
                )
×
1204
                if err != nil {
×
1205
                        return nil, err
×
1206
                }
×
1207

×
1208
                // With the seed, we can now use the wallet loader to create
1209
                // the wallet, then pass it back to avoid unlocking it again.
2✔
1210
                var (
2✔
1211
                        birthday  time.Time
2✔
1212
                        newWallet *wallet.Wallet
2✔
1213
                )
2✔
1214
                switch {
×
1215
                // A normal cipher seed was given, use the birthday encoded in
×
1216
                // it and create the wallet from that.
1217
                case cipherSeed != nil:
1218
                        birthday = cipherSeed.BirthdayTime()
1219
                        newWallet, err = loader.CreateNewWallet(
2✔
1220
                                password, password, cipherSeed.Entropy[:],
2✔
1221
                                birthday,
2✔
1222
                        )
2✔
1223

2✔
1224
                // No seed was given, we're importing a wallet from its extended
1225
                // private key.
1226
                case extendedKey != nil:
2✔
1227
                        birthday = initMsg.ExtendedKeyBirthday
2✔
1228
                        newWallet, err = loader.CreateNewWalletExtendedKey(
2✔
1229
                                password, password, extendedKey, birthday,
2✔
1230
                        )
2✔
1231

2✔
1232
                // Neither seed nor extended private key was given, so maybe the
1233
                // third option was chosen, the watch-only initialization. In
1234
                // this case we need to import each of the xpubs individually.
1235
                case watchOnlyAccounts != nil:
2✔
1236
                        if !cfg.RemoteSigner.Enable {
2✔
1237
                                return nil, fmt.Errorf("cannot initialize " +
2✔
1238
                                        "watch only wallet with remote " +
2✔
1239
                                        "signer config disabled")
2✔
1240
                        }
1241

1242
                        birthday = initMsg.WatchOnlyBirthday
1243
                        newWallet, err = loader.CreateNewWatchingOnlyWallet(
1244
                                password, birthday,
2✔
1245
                        )
2✔
1246
                        if err != nil {
×
1247
                                break
×
1248
                        }
×
1249

×
1250
                        err = importWatchOnlyAccounts(newWallet, initMsg)
1251

2✔
1252
                default:
2✔
1253
                        // The unlocker service made sure either the cipher seed
2✔
1254
                        // or the extended key is set so, we shouldn't get here.
2✔
1255
                        // The default case is just here for readability and
2✔
1256
                        // completeness.
×
1257
                        err = fmt.Errorf("cannot create wallet, neither seed " +
1258
                                "nor extended key was given")
1259
                }
2✔
1260
                if err != nil {
1261
                        // Don't leave the file open in case the new wallet
×
1262
                        // could not be created for whatever reason.
×
1263
                        if err := loader.UnloadWallet(); err != nil {
×
1264
                                ltndLog.Errorf("Could not unload new "+
×
1265
                                        "wallet: %v", err)
×
1266
                        }
×
1267
                        return nil, err
×
1268
                }
1269

2✔
1270
                // For new wallets, the ResetWalletTransactions flag is a no-op.
×
1271
                if cfg.ResetWalletTransactions {
×
1272
                        ltndLog.Warnf("Ignoring reset-wallet-transactions " +
×
1273
                                "flag for new wallet as it has no effect")
×
1274
                }
×
1275

×
1276
                return &walletunlocker.WalletUnlockParams{
×
1277
                        Password:        password,
1278
                        Birthday:        birthday,
1279
                        RecoveryWindow:  recoveryWindow,
1280
                        Wallet:          newWallet,
4✔
1281
                        ChansToRestore:  initMsg.ChanBackups,
2✔
1282
                        UnloadWallet:    loader.UnloadWallet,
2✔
1283
                        StatelessInit:   initMsg.StatelessInit,
2✔
1284
                        MacResponseChan: pwService.MacResponseChan,
1285
                        MacRootKey:      initMsg.MacRootKey,
2✔
1286
                }, nil
2✔
1287

2✔
1288
        // The wallet has already been created in the past, and is simply being
2✔
1289
        // unlocked. So we'll just return these passphrases.
2✔
1290
        case unlockMsg := <-pwService.UnlockMsgs:
2✔
1291
                // Resetting the transactions is something the user likely only
2✔
1292
                // wants to do once so we add a prominent warning to the log to
2✔
1293
                // remind the user to turn off the setting again after
2✔
1294
                // successful completion.
2✔
1295
                if cfg.ResetWalletTransactions {
2✔
1296
                        ltndLog.Warnf("Dropped all transaction history from " +
1297
                                "on-chain wallet. Remember to disable " +
1298
                                "reset-wallet-transactions flag for next " +
1299
                                "start of lnd")
2✔
1300
                }
2✔
1301

2✔
1302
                return &walletunlocker.WalletUnlockParams{
2✔
1303
                        Password:        unlockMsg.Passphrase,
2✔
1304
                        RecoveryWindow:  unlockMsg.RecoveryWindow,
4✔
1305
                        Wallet:          unlockMsg.Wallet,
2✔
1306
                        ChansToRestore:  unlockMsg.ChanBackups,
2✔
1307
                        UnloadWallet:    unlockMsg.UnloadWallet,
2✔
1308
                        StatelessInit:   unlockMsg.StatelessInit,
2✔
1309
                        MacResponseChan: pwService.MacResponseChan,
2✔
1310
                }, nil
1311

2✔
1312
        // If we got a shutdown signal we just return with an error immediately
2✔
1313
        case <-shutdownChan:
2✔
1314
                return nil, fmt.Errorf("shutting down")
2✔
1315
        }
2✔
1316
}
2✔
1317

2✔
1318
// importWatchOnlyAccounts imports all individual account xpubs into our wallet
2✔
1319
// which we created as watch-only.
2✔
1320
func importWatchOnlyAccounts(wallet *wallet.Wallet,
1321
        initMsg *walletunlocker.WalletInitMsg) error {
1322

×
1323
        scopes := make([]waddrmgr.ScopedIndex, 0, len(initMsg.WatchOnlyAccounts))
×
1324
        for scope := range initMsg.WatchOnlyAccounts {
1325
                scopes = append(scopes, scope)
1326
        }
1327

1328
        // We need to import the accounts in the correct order, otherwise the
1329
        // indices will be incorrect.
1330
        sort.Slice(scopes, func(i, j int) bool {
2✔
1331
                return scopes[i].Scope.Purpose < scopes[j].Scope.Purpose ||
2✔
1332
                        scopes[i].Index < scopes[j].Index
2✔
1333
        })
4✔
1334

2✔
1335
        for _, scope := range scopes {
2✔
1336
                addrSchema := waddrmgr.ScopeAddrMap[waddrmgr.KeyScopeBIP0084]
1337

1338
                // We want witness pubkey hash by default, except for BIP49
1339
                // where we want mixed and BIP86 where we want taproot address
4✔
1340
                // formats.
2✔
1341
                switch scope.Scope.Purpose {
2✔
1342
                case waddrmgr.KeyScopeBIP0049Plus.Purpose,
2✔
1343
                        waddrmgr.KeyScopeBIP0086.Purpose:
1344

4✔
1345
                        addrSchema = waddrmgr.ScopeAddrMap[scope.Scope]
2✔
1346
                }
2✔
1347

2✔
1348
                // We want a human-readable account name. But for the default
2✔
1349
                // on-chain wallet we actually need to call it "default" to make
2✔
1350
                // sure everything works correctly.
2✔
1351
                name := fmt.Sprintf("%s/%d'", scope.Scope.String(), scope.Index)
1352
                if scope.Index == 0 {
2✔
1353
                        name = "default"
2✔
1354
                }
2✔
1355

1356
                _, err := wallet.ImportAccountWithScope(
1357
                        name, initMsg.WatchOnlyAccounts[scope],
1358
                        initMsg.WatchOnlyMasterFingerprint, scope.Scope,
1359
                        addrSchema,
1360
                )
2✔
1361
                if err != nil {
4✔
1362
                        return fmt.Errorf("could not import account %v: %w",
2✔
1363
                                name, err)
2✔
1364
                }
1365
        }
2✔
1366

2✔
1367
        return nil
2✔
1368
}
2✔
1369

2✔
1370
// initNeutrinoBackend inits a new instance of the neutrino light client
2✔
1371
// backend given a target chain directory to store the chain state.
×
1372
func initNeutrinoBackend(ctx context.Context, cfg *Config, chainDir string,
×
1373
        blockCache *blockcache.BlockCache) (*neutrino.ChainService,
×
1374
        func(), error) {
1375

1376
        // Both channel validation flags are false by default but their meaning
2✔
1377
        // is the inverse of each other. Therefore both cannot be true. For
1378
        // every other case, the neutrino.validatechannels overwrites the
1379
        // routing.assumechanvalid value.
1380
        if cfg.NeutrinoMode.ValidateChannels && cfg.Routing.AssumeChannelValid {
1381
                return nil, nil, fmt.Errorf("can't set both " +
1382
                        "neutrino.validatechannels and routing." +
1383
                        "assumechanvalid to true at the same time")
×
1384
        }
×
1385
        cfg.Routing.AssumeChannelValid = !cfg.NeutrinoMode.ValidateChannels
×
1386

×
1387
        // First we'll open the database file for neutrino, creating the
×
1388
        // database if needed. We append the normalized network name here to
×
1389
        // match the behavior of btcwallet.
×
1390
        dbPath := filepath.Join(
×
1391
                chainDir, lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name),
×
1392
        )
×
1393

×
1394
        // Ensure that the neutrino db path exists.
×
1395
        if err := os.MkdirAll(dbPath, 0700); err != nil {
×
1396
                return nil, nil, err
×
1397
        }
×
1398

×
1399
        var (
×
1400
                db  walletdb.DB
×
1401
                err error
×
1402
        )
×
1403
        switch {
×
1404
        case cfg.DB.Backend == kvdb.SqliteBackendName:
×
1405
                sqliteConfig := lncfg.GetSqliteConfigKVDB(cfg.DB.Sqlite)
×
1406
                db, err = kvdb.Open(
×
1407
                        kvdb.SqliteBackendName, ctx, sqliteConfig, dbPath,
1408
                        lncfg.SqliteNeutrinoDBName, lncfg.NSNeutrinoDB,
×
1409
                )
×
1410

×
1411
        default:
×
1412
                dbName := filepath.Join(dbPath, "neutrino.db")
×
1413
                db, err = walletdb.Create(
×
1414
                        "bdb", dbName, !cfg.SyncFreelist, cfg.DB.Bolt.DBTimeout,
×
1415
                )
×
1416
        }
×
1417
        if err != nil {
×
1418
                return nil, nil, fmt.Errorf("unable to create "+
×
1419
                        "neutrino database: %v", err)
1420
        }
×
1421

×
1422
        headerStateAssertion, err := parseHeaderStateAssertion(
×
1423
                cfg.NeutrinoMode.AssertFilterHeader,
×
1424
        )
×
1425
        if err != nil {
1426
                db.Close()
×
1427
                return nil, nil, err
×
1428
        }
×
1429

×
1430
        // With the database open, we can now create an instance of the
1431
        // neutrino light client. We pass in relevant configuration parameters
×
1432
        // required.
×
1433
        config := neutrino.Config{
×
1434
                DataDir:      dbPath,
×
1435
                Database:     db,
×
1436
                ChainParams:  *cfg.ActiveNetParams.Params,
×
1437
                AddPeers:     cfg.NeutrinoMode.AddPeers,
×
1438
                ConnectPeers: cfg.NeutrinoMode.ConnectPeers,
1439
                Dialer: func(addr net.Addr) (net.Conn, error) {
1440
                        return cfg.net.Dial(
1441
                                addr.Network(), addr.String(),
1442
                                cfg.ConnectionTimeout,
×
1443
                        )
×
1444
                },
×
1445
                NameResolver: func(host string) ([]net.IP, error) {
×
1446
                        addrs, err := cfg.net.LookupHost(host)
×
1447
                        if err != nil {
×
1448
                                return nil, err
×
1449
                        }
×
1450

×
1451
                        ips := make([]net.IP, 0, len(addrs))
×
1452
                        for _, strIP := range addrs {
×
1453
                                ip := net.ParseIP(strIP)
×
1454
                                if ip == nil {
×
1455
                                        continue
×
1456
                                }
×
1457

×
1458
                                ips = append(ips, ip)
×
1459
                        }
1460

×
1461
                        return ips, nil
×
1462
                },
×
1463
                AssertFilterHeader: headerStateAssertion,
×
1464
                BlockCache:         blockCache.Cache,
×
1465
                BroadcastTimeout:   cfg.NeutrinoMode.BroadcastTimeout,
1466
                PersistToDisk:      cfg.NeutrinoMode.PersistFilters,
1467
        }
×
1468

1469
        neutrino.MaxPeers = 8
1470
        neutrino.BanDuration = time.Hour * 48
×
1471
        neutrino.UserAgentName = cfg.NeutrinoMode.UserAgentName
1472
        neutrino.UserAgentVersion = cfg.NeutrinoMode.UserAgentVersion
1473

1474
        neutrinoCS, err := neutrino.NewChainService(config)
1475
        if err != nil {
1476
                db.Close()
1477
                return nil, nil, fmt.Errorf("unable to create neutrino light "+
1478
                        "client: %v", err)
×
1479
        }
×
1480

×
1481
        if err := neutrinoCS.Start(); err != nil {
×
1482
                db.Close()
×
1483
                return nil, nil, err
×
1484
        }
×
1485

×
1486
        cleanUp := func() {
×
1487
                if err := neutrinoCS.Stop(); err != nil {
×
1488
                        ltndLog.Infof("Unable to stop neutrino light client: "+
×
1489
                                "%v", err)
1490
                }
×
1491
                db.Close()
×
1492
        }
×
1493

×
1494
        return neutrinoCS, cleanUp, nil
1495
}
×
1496

×
1497
// parseHeaderStateAssertion parses the user-specified neutrino header state
×
1498
// into a headerfs.FilterHeader.
×
1499
func parseHeaderStateAssertion(state string) (*headerfs.FilterHeader, error) {
×
1500
        if len(state) == 0 {
×
1501
                return nil, nil
1502
        }
1503

×
1504
        split := strings.Split(state, ":")
1505
        if len(split) != 2 {
1506
                return nil, fmt.Errorf("header state assertion %v in "+
1507
                        "unexpected format, expected format height:hash", state)
1508
        }
×
1509

×
1510
        height, err := strconv.ParseUint(split[0], 10, 32)
×
1511
        if err != nil {
×
1512
                return nil, fmt.Errorf("invalid filter header height: %w", err)
1513
        }
×
1514

×
1515
        hash, err := chainhash.NewHashFromStr(split[1])
×
1516
        if err != nil {
×
1517
                return nil, fmt.Errorf("invalid filter header hash: %w", err)
×
1518
        }
1519

×
1520
        return &headerfs.FilterHeader{
×
1521
                Height:     uint32(height),
×
1522
                FilterHash: *hash,
×
1523
        }, nil
1524
}
×
1525

×
1526
// broadcastErrorMapper maps errors from bitcoin backends other than neutrino to
×
1527
// the neutrino BroadcastError which allows the Rebroadcaster which currently
×
1528
// resides in the neutrino package to use all of its functionalities.
1529
func broadcastErrorMapper(err error) error {
×
1530
        var returnErr error
×
1531

×
1532
        // We only filter for specific backend errors which are relevant for the
×
1533
        // Rebroadcaster.
1534
        switch {
1535
        // This makes sure the tx is removed from the rebroadcaster once it is
1536
        // confirmed.
1537
        case errors.Is(err, chain.ErrTxAlreadyKnown),
1538
                errors.Is(err, chain.ErrTxAlreadyConfirmed):
2✔
1539

2✔
1540
                returnErr = &pushtx.BroadcastError{
2✔
1541
                        Code:   pushtx.Confirmed,
2✔
1542
                        Reason: err.Error(),
2✔
1543
                }
2✔
1544

1545
        // Transactions which are still in mempool but might fall out because
1546
        // of low fees are rebroadcasted despite of their backend error.
1547
        case errors.Is(err, chain.ErrTxAlreadyInMempool):
2✔
1548
                returnErr = &pushtx.BroadcastError{
2✔
1549
                        Code:   pushtx.Mempool,
2✔
1550
                        Reason: err.Error(),
2✔
1551
                }
2✔
1552

2✔
1553
        // Transactions which are not accepted into mempool because of low fees
1554
        // in the first place are rebroadcasted despite of their backend error.
1555
        // Mempool conditions change over time so it makes sense to retry
1556
        // publishing the transaction. Moreover we log the detailed error so the
×
1557
        // user can intervene and increase the size of his mempool.
×
1558
        case errors.Is(err, chain.ErrMempoolMinFeeNotMet):
×
1559
                ltndLog.Warnf("Error while broadcasting transaction: %v", err)
×
1560

×
1561
                returnErr = &pushtx.BroadcastError{
1562
                        Code:   pushtx.Mempool,
1563
                        Reason: err.Error(),
1564
                }
1565
        }
1566

1567
        return returnErr
×
1568
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc