lnd: register selected chain within chainRegistry on boot

This commit modifies the initialization logic within lnd.go to
recognize which chain was selected by the user and to set the
parameters accordingly. With this commit, lnd is now able to switch
between chains within nothing more than a toggle of config paramters!
This commit is contained in:
Olaoluwa Osuntokun 2017-05-02 19:49:14 -07:00
parent bf927eb507
commit c66c7473bf
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2

51
lnd.go
View File

@ -8,7 +8,6 @@ import (
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"path/filepath"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@ -29,8 +28,9 @@ import (
) )
var ( var (
cfg *config cfg *config
shutdownChannel = make(chan struct{}) shutdownChannel = make(chan struct{})
registeredChains = newChainRegistry()
) )
// lndMain is the true entry point for lnd. This function is required since // lndMain is the true entry point for lnd. This function is required since
@ -64,22 +64,31 @@ func lndMain() error {
// network related metadata. // network related metadata.
chanDB, err := channeldb.Open(cfg.DataDir) chanDB, err := channeldb.Open(cfg.DataDir)
if err != nil { if err != nil {
fmt.Println("unable to open channeldb: ", err) ltndLog.Errorf("unable to open channeldb: ", err)
return err return err
} }
defer chanDB.Close() defer chanDB.Close()
// Set the RPC config from the "home" chain. Multi-chain isn't yet
// active, so we'll restrict usage to a particular chain for now.
homeChainConfig := cfg.Bitcoin
if registeredChains.PrimaryChain() == litecoinChain {
homeChainConfig = cfg.Litecoin
}
ltndLog.Infof("Primary chain is set to: %v",
registeredChains.PrimaryChain())
// Next load btcd's TLS cert for the RPC connection. If a raw cert was // Next load btcd's TLS cert for the RPC connection. If a raw cert was
// specified in the config, then we'll set that directly. Otherwise, we // specified in the config, then we'll set that directly. Otherwise, we
// attempt to read the cert from the path specified in the config. // attempt to read the cert from the path specified in the config.
var rpcCert []byte var rpcCert []byte
if cfg.RawRPCCert != "" { if homeChainConfig.RawRPCCert != "" {
rpcCert, err = hex.DecodeString(cfg.RawRPCCert) rpcCert, err = hex.DecodeString(homeChainConfig.RawRPCCert)
if err != nil { if err != nil {
return err return err
} }
} else { } else {
certFile, err := os.Open(cfg.RPCCert) certFile, err := os.Open(homeChainConfig.RPCCert)
if err != nil { if err != nil {
return err return err
} }
@ -96,14 +105,14 @@ func lndMain() error {
// specified, then we use that directly. Otherwise, we assume the // specified, then we use that directly. Otherwise, we assume the
// default port according to the selected chain parameters. // default port according to the selected chain parameters.
var btcdHost string var btcdHost string
if strings.Contains(cfg.RPCHost, ":") { if strings.Contains(homeChainConfig.RPCHost, ":") {
btcdHost = cfg.RPCHost btcdHost = homeChainConfig.RPCHost
} else { } else {
btcdHost = fmt.Sprintf("%v:%v", cfg.RPCHost, activeNetParams.rpcPort) btcdHost = fmt.Sprintf("%v:%v", homeChainConfig.RPCHost, activeNetParams.rpcPort)
} }
btcdUser := cfg.RPCUser btcdUser := homeChainConfig.RPCUser
btcdPass := cfg.RPCPass btcdPass := homeChainConfig.RPCPass
// TODO(roasbeef): parse config here and select chosen notifier instead // TODO(roasbeef): parse config here and select chosen notifier instead
rpcConfig := &btcrpcclient.ConnConfig{ rpcConfig := &btcrpcclient.ConnConfig{
@ -124,10 +133,10 @@ func lndMain() error {
// TODO(roasbeef): parse config here select chosen WalletController // TODO(roasbeef): parse config here select chosen WalletController
walletConfig := &btcwallet.Config{ walletConfig := &btcwallet.Config{
PrivatePass: []byte("hello"), PrivatePass: []byte("hello"),
DataDir: filepath.Join(cfg.DataDir, "lnwallet"), DataDir: homeChainConfig.ChainDir,
RPCHost: btcdHost, RPCHost: btcdHost,
RPCUser: cfg.RPCUser, RPCUser: homeChainConfig.RPCUser,
RPCPass: cfg.RPCPass, RPCPass: homeChainConfig.RPCPass,
CACert: rpcCert, CACert: rpcCert,
NetParams: activeNetParams.Params, NetParams: activeNetParams.Params,
} }
@ -160,6 +169,18 @@ func lndMain() error {
net.JoinHostPort("", strconv.Itoa(cfg.PeerPort)), net.JoinHostPort("", strconv.Itoa(cfg.PeerPort)),
} }
// Finally before we start the server, we'll register the "holy
// trinity" of interface for our current "home chain" with the active
// chainRegistry interface.
primaryChain := registeredChains.PrimaryChain()
registeredChains.RegisterChain(primaryChain, &chainControl{
chainIO: bio,
chainNotifier: notifier,
wallet: wallet,
})
// With all the relevant chains initialized, we can finally start the
// server itself.
server, err := newServer(defaultListenAddrs, notifier, bio, server, err := newServer(defaultListenAddrs, notifier, bio,
fundingSigner, wallet, chanDB) fundingSigner, wallet, chanDB)
if err != nil { if err != nil {