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.
This commit is contained in:
Oliver Gugger 2021-02-16 13:39:35 +01:00
parent c917e91ffb
commit e84fd9257c
No known key found for this signature in database
GPG key ID: 8E4256593F177720
4 changed files with 48 additions and 52 deletions

View file

@ -120,6 +120,12 @@ type Config struct {
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."`
ConfigFile string `long:"configfile" description:"Path to LiT's configuration file."`
// Network is the Bitcoin network we're running on. This will be parsed
// before the configuration is loaded and will set the correct flag on
// `lnd.bitcoin.mainnet|testnet|regtest` and also for the other daemons.
// That way only one global network flag is needed.
Network string `long:"network" description:"The network the UI and all its components run on" choice:"regtest" choice:"testnet" choice:"mainnet" choice:"simnet"`
Remote *RemoteConfig `group:"Remote mode options (use when lnd-mode=remote)" namespace:"remote"`
Faraday *faraday.Config `group:"Faraday options" namespace:"faraday"`
@ -131,11 +137,6 @@ type Config struct {
// faradayRpcConfig is a subset of faraday's full configuration that is
// passed into faraday's RPC server.
faradayRpcConfig *frdrpc.Config
// network is the Bitcoin network we're running on. This will be parsed
// and set when the configuration is loaded, either from
// `lnd.bitcoin.mainnet|testnet|regtest` or from `remote.lnd.network`.
network string
}
// RemoteConfig holds the configuration parameters that are needed when running
@ -156,8 +157,6 @@ type RemoteConfig struct {
// RemoteDaemonConfig holds the configuration parameters that are needed to
// connect to a remote daemon like lnd for example.
type RemoteDaemonConfig struct {
Network string `long:"network" description:"The network the remote daemon runs on" choice:"regtest" choice:"testnet" choice:"mainnet" choice:"simnet"`
// RPCServer is host:port that the remote daemon's RPC server is
// listening on.
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,
// remote section of the lnd config.
if c.LndMode == ModeRemote {
return c.Remote.Lnd.RPCServer,
lndclient.Network(c.network),
lndclient.Network(c.Network),
lncfg.CleanAndExpandPath(c.Remote.Lnd.TLSCertPath),
lncfg.CleanAndExpandPath(c.Remote.Lnd.MacaroonPath)
}
@ -206,7 +205,7 @@ func (c *Config) lndConnectParams() (string, lndclient.Network, string,
)
}
return lndDialAddr, lndclient.Network(c.network),
return lndDialAddr, lndclient.Network(c.Network),
c.Lnd.TLSCertPath, c.Lnd.AdminMacPath
}
@ -222,12 +221,12 @@ func defaultConfig() *Config {
LitMaxLogFiles: defaultMaxLogFiles,
LitMaxLogFileSize: defaultMaxLogFileSize,
Lnd: &RemoteDaemonConfig{
Network: defaultNetwork,
RPCServer: defaultRemoteLndRpcServer,
MacaroonPath: lndDefaultConfig.AdminMacPath,
TLSCertPath: lndDefaultConfig.TLSCertPath,
},
},
Network: defaultNetwork,
LndMode: defaultLndMode,
Lnd: &lndDefaultConfig,
LitDir: defaultLitDir,
@ -317,17 +316,14 @@ func loadAndValidateConfig() (*Config, error) {
// (like the log or lnd options) as they will be taken from lnd's config
// struct. Others we want to force to be the same as lnd so the user
// doesn't have to set them manually, like the network for example.
cfg.Loop.Network = cfg.network
if err := loopd.Validate(cfg.Loop); err != nil {
return nil, err
}
cfg.Pool.Network = cfg.network
if err := pool.Validate(cfg.Pool); err != nil {
return nil, err
}
cfg.Faraday.Network = cfg.network
if err := faraday.ValidateConfig(cfg.Faraday); err != nil {
return nil, err
}
@ -389,6 +385,12 @@ func loadConfigFile(preCfg *Config, usageMessage string) (*Config, error) {
return nil, err
}
// Parse the global/top-level network and propagate it to all sub config
// structs.
if err := setNetwork(cfg); err != nil {
return nil, err
}
switch cfg.LndMode {
// In case we are running lnd in-process, let's make sure its
// configuration is fully valid. This also sets up the main logger that
@ -399,10 +401,6 @@ func loadConfigFile(preCfg *Config, usageMessage string) (*Config, error) {
if err != nil {
return nil, err
}
cfg.network, err = getNetwork(cfg.Lnd.Bitcoin)
if err != nil {
return nil, err
}
// In remote lnd mode we skip the validation of the lnd configuration
// 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) {
func validateRemoteModeConfig(cfg *Config) error {
r := cfg.Remote
// Validate the network as in the remote node it's provided as a string
// instead of a series of boolean flags.
if _, err := lndclient.Network(r.Lnd.Network).ChainParams(); err != nil {
return fmt.Errorf("error validating lnd remote network: %v", err)
}
cfg.network = r.Lnd.Network
// When referring to the default lnd configuration later on, let's make
// sure we use the actual default values and not the lndDefaultConfig
// variable which could've been overwritten by the user. Otherwise this
@ -448,12 +439,12 @@ func validateRemoteModeConfig(cfg *Config) error {
// need to adjust the default macaroon directory so the user can only
// specify --network=testnet for example if everything else is using
// the defaults.
if r.Lnd.Network != defaultNetwork &&
if cfg.Network != defaultNetwork &&
r.Lnd.MacaroonPath == defaultLndCfg.AdminMacPath {
r.Lnd.MacaroonPath = filepath.Join(
defaultLndCfg.DataDir, defaultLndChainSubDir,
defaultLndChain, r.Lnd.Network,
defaultLndChain, cfg.Network,
path.Base(defaultLndCfg.AdminMacPath),
)
}
@ -486,7 +477,7 @@ func validateRemoteModeConfig(cfg *Config) error {
logWriter := build.NewRotatingLogWriter()
cfg.Lnd.LogWriter = logWriter
err := logWriter.InitLogRotator(
filepath.Join(r.LitLogDir, cfg.network, defaultLogFilename),
filepath.Join(r.LitLogDir, cfg.Network, defaultLogFilename),
r.LitMaxLogFileSize, r.LitMaxLogFiles,
)
if err != nil {
@ -499,23 +490,33 @@ func validateRemoteModeConfig(cfg *Config) error {
)
}
func getNetwork(cfg *lncfg.Chain) (string, error) {
switch {
case cfg.MainNet:
return "mainnet", nil
// setNetwork parses the top-level network config options and, if valid, sets it
// in all sub configuration structs. We also set the Bitcoin chain to active by
// default as LiT won't support Litecoin in the foreseeable future.
func setNetwork(cfg *Config) error {
switch cfg.Network {
case "mainnet":
cfg.Lnd.Bitcoin.MainNet = true
case cfg.TestNet3:
return "testnet", nil
case "testnet", "testnet3":
cfg.Lnd.Bitcoin.TestNet3 = true
case cfg.RegTest:
return "regtest", nil
case "regtest":
cfg.Lnd.Bitcoin.RegTest = true
case cfg.SimNet:
return "simnet", nil
case "simnet":
cfg.Lnd.Bitcoin.SimNet = true
default:
return "", fmt.Errorf("no network selected")
return fmt.Errorf("unknown network: %v", cfg.Network)
}
cfg.Lnd.Bitcoin.Active = true
cfg.Faraday.Network = cfg.Network
cfg.Loop.Network = cfg.Network
cfg.Pool.Network = cfg.Network
return nil
}
// readUIPassword reads the password for the UI either from the command line

View file

@ -32,14 +32,13 @@ node:
--uipassword=My$trongP@ssword \
--letsencrypt \
--letsencrypthost=loop.merchant.com \
--network=testnet \
--lnd-mode=integrated \
--lnd.lnddir=/root/.lnd \
--lnd.alias=merchant \
--lnd.externalip=loop.merchant.com \
--lnd.rpclisten=0.0.0.0:10009 \
--lnd.listen=0.0.0.0:9735 \
--lnd.bitcoin.active \
--lnd.bitcoin.testnet \
--lnd.bitcoin.node=bitcoind \
--lnd.bitcoind.rpchost=localhost \
--lnd.bitcoind.rpcuser=testnetuser \
@ -75,6 +74,7 @@ httpslisten=0.0.0.0:8443
letsencrypt=true
letsencrypthost=loop.merchant.com
lnd-mode=integrated
network=testnet
# Lnd
lnd.lnddir=~/.lnd
@ -85,8 +85,6 @@ lnd.listen=0.0.0.0:9735
lnd.debuglevel=debug
# Lnd - bitcoin
lnd.bitcoin.active=true
lnd.bitcoin.testnet=true
lnd.bitcoin.node=bitcoind
# Lnd - bitcoind
@ -151,13 +149,12 @@ For `lnd`:
```text
# New flag to tell LiT to run its own lnd in integrated mode. We need to set
# this because "remote" is the new default value if we don't specify anything.
# We also don't need to explicitly activate the Bitcoin network anymore as
# that is the default for LiT.
lnd-mode=integrated
# Application Options
lnd.alias=merchant
# bitcoin
lnd.bitcoin.active=true
```
- if you use command line arguments for configuration, add the `lnd.` prefix to
@ -271,11 +268,10 @@ relevant parts shown here):
```text
lnd-mode=integrated
network=testnet
lnd.lnddir=~/.lnd
lnd.rpclisten=0.0.0.0:10009
lnd.bitcoin.testnet=true
```
Because all components listen on the same gRPC port and use the same TLS

View file

@ -89,8 +89,8 @@ and `faraday` (optional):
--letsencrypt \
--letsencrypthost=loop.merchant.com \
--lit-dir=~/.lit \
--network=testnet \
--remote.lit-debuglevel=debug \
--remote.lnd.network=testnet \
--remote.lnd.rpcserver=some-other-host:10009 \
--remote.lnd.macaroonpath=/some/folder/with/lnd/data/admin.macaroon \
--remote.lnd.tlscertpath=/some/folder/with/lnd/data/tls.cert \
@ -135,12 +135,12 @@ uipassword=My$trongP@ssword
letsencrypt=true
letsencrypthost=loop.merchant.com
lit-dir=~/.lit
network=testnet
# Remote options
remote.lit-debuglevel=debug
# Remote lnd options
remote.lnd.network=testnet
remote.lnd.rpcserver=some-other-host:10009
remote.lnd.macaroonpath=/some/folder/with/lnd/data/admin.macaroon
remote.lnd.tlscertpath=/some/folder/with/lnd/data/tls.cert
@ -180,8 +180,8 @@ configuration (only relevant parts shown here):
```text
httpslisten=0.0.0.0:8443
lit-dir=~/.lit
network=testnet
remote.lnd.network=testnet
remote.lnd.rpcserver=some-other-host:10009
remote.lnd.macaroonpath=/some/folder/with/lnd/data/admin.macaroon
remote.lnd.tlscertpath=/some/folder/with/lnd/data/tls.cert

View file

@ -79,8 +79,7 @@ httpslisten=0.0.0.0:8443
letsencrypt=1
letsencrypthost=terminal.mydomain.com
lnd-mode=integrated
lnd.bitcoin.testnet=true
network=testnet
```
### Example `lncli` command