From 71a5fac9c7731a4c551250f9c3783b0c926965d1 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 15 Mar 2018 02:04:17 -0700 Subject: [PATCH] lnd+config: add flags for mainnet --- config.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++-------- lnd.go | 21 ++++++++++++++++ 2 files changed, 85 insertions(+), 10 deletions(-) diff --git a/config.go b/config.go index 3da18b27..afe0f1b4 100644 --- a/config.go +++ b/config.go @@ -82,6 +82,7 @@ type chainConfig struct { Node string `long:"node" description:"The blockchain interface to use." choice:"btcd" choice:"bitcoind" choice:"neutrino" choice:"ltcd" choice:"litecoind"` + MainNet bool `long:"mainnet" description:"Use the main network"` TestNet3 bool `long:"testnet" description:"Use the test network"` SimNet bool `long:"simnet" description:"Use the simulation test network"` RegTest bool `long:"regtest" description:"Use the regression test network"` @@ -405,13 +406,41 @@ func loadConfig() (*config, error) { minTimeLockDelta) } + // Multiple networks can't be selected simultaneously. Count + // number of network flags passed; assign active network params + // while we're at it. + numNets := 0 + var ltcParams litecoinNetParams + if cfg.Litecoin.MainNet { + numNets++ + ltcParams = litecoinMainNetParams + } + if cfg.Litecoin.TestNet3 { + numNets++ + ltcParams = litecoinTestNetParams + } + if numNets > 1 { + str := "%s: The mainnet, testnet, and simnet params " + + "can't be used together -- choose one of the " + + "three" + err := fmt.Errorf(str, funcName) + return nil, err + } + + // The target network must be provided, otherwise, we won't + // know how to initialize the daemon. + if numNets == 0 { + str := "%s: either --litecoin.mainnet, or " + + "litecoin.testnet must be specified" + err := fmt.Errorf(str, funcName) + return nil, err + } + // The litecoin chain is the current active chain. However // throughout the codebase we required chaincfg.Params. So as a // temporary hack, we'll mutate the default net params for // bitcoin with the litecoin specific information. - paramCopy := bitcoinTestNetParams - applyLitecoinParams(¶mCopy) - activeNetParams = paramCopy + applyLitecoinParams(&activeNetParams, <cParams) switch cfg.Litecoin.Node { case "ltcd": @@ -453,6 +482,10 @@ func loadConfig() (*config, error) { // number of network flags passed; assign active network params // while we're at it. numNets := 0 + if cfg.Bitcoin.MainNet { + numNets++ + activeNetParams = bitcoinMainNetParams + } if cfg.Bitcoin.TestNet3 { numNets++ activeNetParams = bitcoinTestNetParams @@ -466,8 +499,26 @@ func loadConfig() (*config, error) { activeNetParams = bitcoinSimNetParams } if numNets > 1 { - str := "%s: The testnet, segnet, and simnet params can't be " + - "used together -- choose one of the three" + str := "%s: The mainnet, testnet, regtest, and " + + "simnet params can't be used together -- " + + "choose one of the four" + err := fmt.Errorf(str, funcName) + return nil, err + } + + // The target network must be provided, otherwise, we won't + // know how to initialize the daemon. + if numNets == 0 { + str := "%s: either --bitcoin.mainnet, or " + + "bitcoin.testnet, bitcoin.simnet, or bitcoin.regtest " + + "must be specified" + err := fmt.Errorf(str, funcName) + return nil, err + } + + if cfg.Bitcoin.Node == "neutrino" && cfg.Bitcoin.MainNet { + str := "%s: neutrino isn't yet supported for " + + "bitcoin's mainnet" err := fmt.Errorf(str, funcName) return nil, err } @@ -479,8 +530,9 @@ func loadConfig() (*config, error) { switch cfg.Bitcoin.Node { case "btcd": - err := parseRPCParams(cfg.Bitcoin, cfg.BtcdMode, - bitcoinChain, funcName) + err := parseRPCParams( + cfg.Bitcoin, cfg.BtcdMode, bitcoinChain, funcName, + ) if err != nil { err := fmt.Errorf("unable to load RPC "+ "credentials for btcd: %v", err) @@ -491,8 +543,10 @@ func loadConfig() (*config, error) { return nil, fmt.Errorf("%s: bitcoind does not "+ "support simnet", funcName) } - err := parseRPCParams(cfg.Bitcoin, cfg.BitcoindMode, - bitcoinChain, funcName) + + err := parseRPCParams( + cfg.Bitcoin, cfg.BitcoindMode, bitcoinChain, funcName, + ) if err != nil { err := fmt.Errorf("unable to load RPC "+ "credentials for bitcoind: %v", err) @@ -997,7 +1051,7 @@ func normalizeAddresses(addrs []string, defaultPort string) []string { // interface. func enforceSafeAuthentication(addrs []string, macaroonsActive bool) error { isLoopback := func(addr string) bool { - loopBackAddrs := []string{"localhost", "127.0.0.1"} + loopBackAddrs := []string{"localhost", "127.0.0.1", "[::1]"} for _, loopback := range loopBackAddrs { if strings.Contains(addr, loopback) { return true diff --git a/lnd.go b/lnd.go index f12b6010..9e498275 100644 --- a/lnd.go +++ b/lnd.go @@ -23,6 +23,7 @@ import ( "path/filepath" "runtime" "runtime/pprof" + "strings" "sync" "time" @@ -105,6 +106,26 @@ func lndMain() error { // Show version at startup. ltndLog.Infof("Version %s", version()) + var network string + switch { + case cfg.Bitcoin.TestNet3 || cfg.Litecoin.TestNet3: + network = "testnet" + + case cfg.Bitcoin.MainNet || cfg.Litecoin.MainNet: + network = "mainnet" + + case cfg.Bitcoin.SimNet: + network = "simmnet" + + case cfg.Bitcoin.RegTest: + network = "regtest" + } + + ltndLog.Infof("Active chain: %v (network=%v)", + strings.Title(registeredChains.PrimaryChain().String()), + network, + ) + // Enable http profiling server if requested. if cfg.Profile != "" { go func() {