Skip to content

Commit e84fd92

Browse files
committed
doc+config: move network config to top-level struct
Since we know that only the Bitcoin network is used and that all components must operate on the same network, we can make the configuration a lot easier for the user by allowing them to only specify a single option for the network. We'll then go ahead and set that in all the necessary sub structs.
1 parent c917e91 commit e84fd92

File tree

4 files changed

+48
-52
lines changed

4 files changed

+48
-52
lines changed

config.go

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ type Config struct {
120120
LitDir string `long:"lit-dir" description:"The main directory where LiT looks for its configuration file. If LiT is running in 'remote' lnd mode, this is also the directory where the TLS certificates and log files are stored by default."`
121121
ConfigFile string `long:"configfile" description:"Path to LiT's configuration file."`
122122

123+
// Network is the Bitcoin network we're running on. This will be parsed
124+
// before the configuration is loaded and will set the correct flag on
125+
// `lnd.bitcoin.mainnet|testnet|regtest` and also for the other daemons.
126+
// That way only one global network flag is needed.
127+
Network string `long:"network" description:"The network the UI and all its components run on" choice:"regtest" choice:"testnet" choice:"mainnet" choice:"simnet"`
128+
123129
Remote *RemoteConfig `group:"Remote mode options (use when lnd-mode=remote)" namespace:"remote"`
124130

125131
Faraday *faraday.Config `group:"Faraday options" namespace:"faraday"`
@@ -131,11 +137,6 @@ type Config struct {
131137
// faradayRpcConfig is a subset of faraday's full configuration that is
132138
// passed into faraday's RPC server.
133139
faradayRpcConfig *frdrpc.Config
134-
135-
// network is the Bitcoin network we're running on. This will be parsed
136-
// and set when the configuration is loaded, either from
137-
// `lnd.bitcoin.mainnet|testnet|regtest` or from `remote.lnd.network`.
138-
network string
139140
}
140141

141142
// RemoteConfig holds the configuration parameters that are needed when running
@@ -156,8 +157,6 @@ type RemoteConfig struct {
156157
// RemoteDaemonConfig holds the configuration parameters that are needed to
157158
// connect to a remote daemon like lnd for example.
158159
type RemoteDaemonConfig struct {
159-
Network string `long:"network" description:"The network the remote daemon runs on" choice:"regtest" choice:"testnet" choice:"mainnet" choice:"simnet"`
160-
161160
// RPCServer is host:port that the remote daemon's RPC server is
162161
// listening on.
163162
RPCServer string `long:"rpcserver" description:"The host:port that the remote daemon is listening for RPC connections on."`
@@ -183,7 +182,7 @@ func (c *Config) lndConnectParams() (string, lndclient.Network, string,
183182
// remote section of the lnd config.
184183
if c.LndMode == ModeRemote {
185184
return c.Remote.Lnd.RPCServer,
186-
lndclient.Network(c.network),
185+
lndclient.Network(c.Network),
187186
lncfg.CleanAndExpandPath(c.Remote.Lnd.TLSCertPath),
188187
lncfg.CleanAndExpandPath(c.Remote.Lnd.MacaroonPath)
189188
}
@@ -206,7 +205,7 @@ func (c *Config) lndConnectParams() (string, lndclient.Network, string,
206205
)
207206
}
208207

209-
return lndDialAddr, lndclient.Network(c.network),
208+
return lndDialAddr, lndclient.Network(c.Network),
210209
c.Lnd.TLSCertPath, c.Lnd.AdminMacPath
211210
}
212211

@@ -222,12 +221,12 @@ func defaultConfig() *Config {
222221
LitMaxLogFiles: defaultMaxLogFiles,
223222
LitMaxLogFileSize: defaultMaxLogFileSize,
224223
Lnd: &RemoteDaemonConfig{
225-
Network: defaultNetwork,
226224
RPCServer: defaultRemoteLndRpcServer,
227225
MacaroonPath: lndDefaultConfig.AdminMacPath,
228226
TLSCertPath: lndDefaultConfig.TLSCertPath,
229227
},
230228
},
229+
Network: defaultNetwork,
231230
LndMode: defaultLndMode,
232231
Lnd: &lndDefaultConfig,
233232
LitDir: defaultLitDir,
@@ -317,17 +316,14 @@ func loadAndValidateConfig() (*Config, error) {
317316
// (like the log or lnd options) as they will be taken from lnd's config
318317
// struct. Others we want to force to be the same as lnd so the user
319318
// doesn't have to set them manually, like the network for example.
320-
cfg.Loop.Network = cfg.network
321319
if err := loopd.Validate(cfg.Loop); err != nil {
322320
return nil, err
323321
}
324322

325-
cfg.Pool.Network = cfg.network
326323
if err := pool.Validate(cfg.Pool); err != nil {
327324
return nil, err
328325
}
329326

330-
cfg.Faraday.Network = cfg.network
331327
if err := faraday.ValidateConfig(cfg.Faraday); err != nil {
332328
return nil, err
333329
}
@@ -389,6 +385,12 @@ func loadConfigFile(preCfg *Config, usageMessage string) (*Config, error) {
389385
return nil, err
390386
}
391387

388+
// Parse the global/top-level network and propagate it to all sub config
389+
// structs.
390+
if err := setNetwork(cfg); err != nil {
391+
return nil, err
392+
}
393+
392394
switch cfg.LndMode {
393395
// In case we are running lnd in-process, let's make sure its
394396
// configuration is fully valid. This also sets up the main logger that
@@ -399,10 +401,6 @@ func loadConfigFile(preCfg *Config, usageMessage string) (*Config, error) {
399401
if err != nil {
400402
return nil, err
401403
}
402-
cfg.network, err = getNetwork(cfg.Lnd.Bitcoin)
403-
if err != nil {
404-
return nil, err
405-
}
406404

407405
// In remote lnd mode we skip the validation of the lnd configuration
408406
// and instead just set up the logging (that would be done by lnd if it
@@ -431,13 +429,6 @@ func loadConfigFile(preCfg *Config, usageMessage string) (*Config, error) {
431429
func validateRemoteModeConfig(cfg *Config) error {
432430
r := cfg.Remote
433431

434-
// Validate the network as in the remote node it's provided as a string
435-
// instead of a series of boolean flags.
436-
if _, err := lndclient.Network(r.Lnd.Network).ChainParams(); err != nil {
437-
return fmt.Errorf("error validating lnd remote network: %v", err)
438-
}
439-
cfg.network = r.Lnd.Network
440-
441432
// When referring to the default lnd configuration later on, let's make
442433
// sure we use the actual default values and not the lndDefaultConfig
443434
// variable which could've been overwritten by the user. Otherwise this
@@ -448,12 +439,12 @@ func validateRemoteModeConfig(cfg *Config) error {
448439
// need to adjust the default macaroon directory so the user can only
449440
// specify --network=testnet for example if everything else is using
450441
// the defaults.
451-
if r.Lnd.Network != defaultNetwork &&
442+
if cfg.Network != defaultNetwork &&
452443
r.Lnd.MacaroonPath == defaultLndCfg.AdminMacPath {
453444

454445
r.Lnd.MacaroonPath = filepath.Join(
455446
defaultLndCfg.DataDir, defaultLndChainSubDir,
456-
defaultLndChain, r.Lnd.Network,
447+
defaultLndChain, cfg.Network,
457448
path.Base(defaultLndCfg.AdminMacPath),
458449
)
459450
}
@@ -486,7 +477,7 @@ func validateRemoteModeConfig(cfg *Config) error {
486477
logWriter := build.NewRotatingLogWriter()
487478
cfg.Lnd.LogWriter = logWriter
488479
err := logWriter.InitLogRotator(
489-
filepath.Join(r.LitLogDir, cfg.network, defaultLogFilename),
480+
filepath.Join(r.LitLogDir, cfg.Network, defaultLogFilename),
490481
r.LitMaxLogFileSize, r.LitMaxLogFiles,
491482
)
492483
if err != nil {
@@ -499,23 +490,33 @@ func validateRemoteModeConfig(cfg *Config) error {
499490
)
500491
}
501492

502-
func getNetwork(cfg *lncfg.Chain) (string, error) {
503-
switch {
504-
case cfg.MainNet:
505-
return "mainnet", nil
493+
// setNetwork parses the top-level network config options and, if valid, sets it
494+
// in all sub configuration structs. We also set the Bitcoin chain to active by
495+
// default as LiT won't support Litecoin in the foreseeable future.
496+
func setNetwork(cfg *Config) error {
497+
switch cfg.Network {
498+
case "mainnet":
499+
cfg.Lnd.Bitcoin.MainNet = true
506500

507-
case cfg.TestNet3:
508-
return "testnet", nil
501+
case "testnet", "testnet3":
502+
cfg.Lnd.Bitcoin.TestNet3 = true
509503

510-
case cfg.RegTest:
511-
return "regtest", nil
504+
case "regtest":
505+
cfg.Lnd.Bitcoin.RegTest = true
512506

513-
case cfg.SimNet:
514-
return "simnet", nil
507+
case "simnet":
508+
cfg.Lnd.Bitcoin.SimNet = true
515509

516510
default:
517-
return "", fmt.Errorf("no network selected")
511+
return fmt.Errorf("unknown network: %v", cfg.Network)
518512
}
513+
514+
cfg.Lnd.Bitcoin.Active = true
515+
cfg.Faraday.Network = cfg.Network
516+
cfg.Loop.Network = cfg.Network
517+
cfg.Pool.Network = cfg.Network
518+
519+
return nil
519520
}
520521

521522
// readUIPassword reads the password for the UI either from the command line

doc/config-lnd-integrated.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ node:
3232
--uipassword=My$trongP@ssword \
3333
--letsencrypt \
3434
--letsencrypthost=loop.merchant.com \
35+
--network=testnet \
3536
--lnd-mode=integrated \
3637
--lnd.lnddir=/root/.lnd \
3738
--lnd.alias=merchant \
3839
--lnd.externalip=loop.merchant.com \
3940
--lnd.rpclisten=0.0.0.0:10009 \
4041
--lnd.listen=0.0.0.0:9735 \
41-
--lnd.bitcoin.active \
42-
--lnd.bitcoin.testnet \
4342
--lnd.bitcoin.node=bitcoind \
4443
--lnd.bitcoind.rpchost=localhost \
4544
--lnd.bitcoind.rpcuser=testnetuser \
@@ -75,6 +74,7 @@ httpslisten=0.0.0.0:8443
7574
letsencrypt=true
7675
letsencrypthost=loop.merchant.com
7776
lnd-mode=integrated
77+
network=testnet
7878
7979
# Lnd
8080
lnd.lnddir=~/.lnd
@@ -85,8 +85,6 @@ lnd.listen=0.0.0.0:9735
8585
lnd.debuglevel=debug
8686
8787
# Lnd - bitcoin
88-
lnd.bitcoin.active=true
89-
lnd.bitcoin.testnet=true
9088
lnd.bitcoin.node=bitcoind
9189
9290
# Lnd - bitcoind
@@ -151,13 +149,12 @@ For `lnd`:
151149
```text
152150
# New flag to tell LiT to run its own lnd in integrated mode. We need to set
153151
# this because "remote" is the new default value if we don't specify anything.
152+
# We also don't need to explicitly activate the Bitcoin network anymore as
153+
# that is the default for LiT.
154154
lnd-mode=integrated
155155
156156
# Application Options
157157
lnd.alias=merchant
158-
159-
# bitcoin
160-
lnd.bitcoin.active=true
161158
```
162159

163160
- if you use command line arguments for configuration, add the `lnd.` prefix to
@@ -271,11 +268,10 @@ relevant parts shown here):
271268

272269
```text
273270
lnd-mode=integrated
271+
network=testnet
274272
275273
lnd.lnddir=~/.lnd
276274
lnd.rpclisten=0.0.0.0:10009
277-
278-
lnd.bitcoin.testnet=true
279275
```
280276

281277
Because all components listen on the same gRPC port and use the same TLS

doc/config-lnd-remote.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ and `faraday` (optional):
8989
--letsencrypt \
9090
--letsencrypthost=loop.merchant.com \
9191
--lit-dir=~/.lit \
92+
--network=testnet \
9293
--remote.lit-debuglevel=debug \
93-
--remote.lnd.network=testnet \
9494
--remote.lnd.rpcserver=some-other-host:10009 \
9595
--remote.lnd.macaroonpath=/some/folder/with/lnd/data/admin.macaroon \
9696
--remote.lnd.tlscertpath=/some/folder/with/lnd/data/tls.cert \
@@ -135,12 +135,12 @@ uipassword=My$trongP@ssword
135135
letsencrypt=true
136136
letsencrypthost=loop.merchant.com
137137
lit-dir=~/.lit
138+
network=testnet
138139
139140
# Remote options
140141
remote.lit-debuglevel=debug
141142
142143
# Remote lnd options
143-
remote.lnd.network=testnet
144144
remote.lnd.rpcserver=some-other-host:10009
145145
remote.lnd.macaroonpath=/some/folder/with/lnd/data/admin.macaroon
146146
remote.lnd.tlscertpath=/some/folder/with/lnd/data/tls.cert
@@ -180,8 +180,8 @@ configuration (only relevant parts shown here):
180180
```text
181181
httpslisten=0.0.0.0:8443
182182
lit-dir=~/.lit
183+
network=testnet
183184
184-
remote.lnd.network=testnet
185185
remote.lnd.rpcserver=some-other-host:10009
186186
remote.lnd.macaroonpath=/some/folder/with/lnd/data/admin.macaroon
187187
remote.lnd.tlscertpath=/some/folder/with/lnd/data/tls.cert

doc/letsencrypt.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ httpslisten=0.0.0.0:8443
7979
letsencrypt=1
8080
letsencrypthost=terminal.mydomain.com
8181
lnd-mode=integrated
82-
83-
lnd.bitcoin.testnet=true
82+
network=testnet
8483
```
8584

8685
### Example `lncli` command

0 commit comments

Comments
 (0)