From a56ab46e97ff7ee9dc98a5933a8fd771555af0d6 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 13 Jul 2016 18:37:50 -0700 Subject: [PATCH] lnd: properly switch RPC port with target net change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With this commit, support for changing the target network (testnet, simnet, etc) has been finalized. Previously a command line option was present to swap networks, but the RPC port wouldn’t automatically be updated to reflect the network. --- config.go | 13 ++----------- fundingmanager.go | 4 ++-- lnd.go | 9 +++++---- params.go | 33 +++++++++++++++++++++++++++++++++ rpcserver.go | 4 ++-- 5 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 params.go diff --git a/config.go b/config.go index 91355d5c..5f4e9d41 100644 --- a/config.go +++ b/config.go @@ -9,7 +9,6 @@ import ( "strings" flags "github.com/btcsuite/go-flags" - "github.com/roasbeef/btcd/chaincfg" "github.com/roasbeef/btcutil" ) @@ -34,10 +33,6 @@ var ( defaultDataDir = filepath.Join(lndHomeDir, defaultDataDirname) defaultLogDir = filepath.Join(lndHomeDir, defaultLogDirname) - // activeNetParams is a pointer to the parameters specific to the - // currently active bitcoin network. - activeNetParams = &chaincfg.SegNet4Params - btcdHomeDir = btcutil.AppDataDir("btcd", false) defaultRPCKeyFile = filepath.Join(btcdHomeDir, "rpc.key") defaultRPCCertFile = filepath.Join(btcdHomeDir, "rpc.cert") @@ -154,15 +149,11 @@ func loadConfig() (*config, error) { numNets := 0 if cfg.TestNet3 { numNets++ - activeNetParams = &chaincfg.TestNet3Params - } - if cfg.SegNet { - numNets++ - activeNetParams = &chaincfg.SegNet4Params + activeNetParams = testNetParams } if cfg.SimNet { numNets++ - activeNetParams = &chaincfg.SimNetParams + activeNetParams = simNetParams } if numNets > 1 { str := "%s: The testnet, segnet, and simnet params can't be " + diff --git a/fundingmanager.go b/fundingmanager.go index 3ff7979c..66e399fe 100644 --- a/fundingmanager.go +++ b/fundingmanager.go @@ -331,7 +331,7 @@ func (f *fundingManager) handleFundingRequest(fmsg *fundingRequestMsg) { // With our portion of the reservation initialied, process the // initiators contribution to the channel. - _, addrs, _, err := txscript.ExtractPkScriptAddrs(msg.DeliveryPkScript, activeNetParams) + _, addrs, _, err := txscript.ExtractPkScriptAddrs(msg.DeliveryPkScript, activeNetParams.Params) if err != nil { fndgLog.Errorf("Unable to extract addresses from script: %v", err) return @@ -390,7 +390,7 @@ func (f *fundingManager) handleFundingResponse(fmsg *fundingResponseMsg) { // contribution. At this point, we can process their contribution which // allows us to construct and sign both the commitment transaction, and // the funding transaction. - _, addrs, _, err := txscript.ExtractPkScriptAddrs(msg.DeliveryPkScript, activeNetParams) + _, addrs, _, err := txscript.ExtractPkScriptAddrs(msg.DeliveryPkScript, activeNetParams.Params) if err != nil { fndgLog.Errorf("Unable to extract addresses from script: %v", err) return diff --git a/lnd.go b/lnd.go index 659199a9..ea97bb31 100644 --- a/lnd.go +++ b/lnd.go @@ -40,7 +40,7 @@ func lndMain() error { ltndLog.Infof("Version %s", version()) if loadedConfig.SPVMode == true { - shell(loadedConfig.SPVHostAdr, activeNetParams) + shell(loadedConfig.SPVHostAdr, activeNetParams.Params) return err } @@ -57,7 +57,7 @@ func lndMain() error { // Open the channeldb, which is dedicated to storing channel, and // network related meta-data. - chanDB, err := channeldb.Open(loadedConfig.DataDir, activeNetParams) + chanDB, err := channeldb.Open(loadedConfig.DataDir, activeNetParams.Params) if err != nil { fmt.Println("unable to open channeldb: ", err) return err @@ -82,11 +82,11 @@ func lndMain() error { config := &lnwallet.Config{ PrivatePass: []byte("hello"), DataDir: filepath.Join(loadedConfig.DataDir, "lnwallet"), - RpcHost: loadedConfig.RPCHost, + RpcHost: fmt.Sprintf("%v:%v", loadedConfig.RPCHost, activeNetParams.rpcPort), RpcUser: loadedConfig.RPCUser, RpcPass: loadedConfig.RPCPass, CACert: cert, - NetParams: activeNetParams, + NetParams: activeNetParams.Params, } wallet, err := lnwallet.NewLightningWallet(config, chanDB) if err != nil { @@ -151,6 +151,7 @@ func main() { // Call the "real" main in a nested manner so the defers will properly // be executed in the case of a graceful shutdown. if err := lndMain(); err != nil { + fmt.Fprintln(os.Stderr, err) os.Exit(1) } } diff --git a/params.go b/params.go new file mode 100644 index 00000000..92a99119 --- /dev/null +++ b/params.go @@ -0,0 +1,33 @@ +package main + +import "github.com/roasbeef/btcd/chaincfg" + +// activeNetParams is a pointer to the parameters specific to the currently +// active bitcoin network. +var activeNetParams = segNetParams + +// netParams couples the p2p parameters of a network with the corresponding RPC +// port of a daemon running on the particular network. +type netParams struct { + *chaincfg.Params + rpcPort string +} + +// testNetParams contains parameters specific to the 3rd version of the test network. +var testNetParams = netParams{ + Params: &chaincfg.TestNet3Params, + rpcPort: "18334", +} + +// segNetParams contains parameters specific to the segregated witness test +// network. +var segNetParams = netParams{ + Params: &chaincfg.SegNet4Params, + rpcPort: "28902", +} + +// simNetParams contains parameters specific to the simulation test network. +var simNetParams = netParams{ + Params: &chaincfg.SimNetParams, + rpcPort: "18556", +} diff --git a/rpcserver.go b/rpcserver.go index 981500f1..d66b578e 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -71,7 +71,7 @@ func (r *rpcServer) Stop() error { func addrPairsToOutputs(addrPairs map[string]int64) ([]*wire.TxOut, error) { outputs := make([]*wire.TxOut, 0, len(addrPairs)) for addr, amt := range addrPairs { - addr, err := btcutil.DecodeAddress(addr, activeNetParams) + addr, err := btcutil.DecodeAddress(addr, activeNetParams.Params) if err != nil { return nil, err } @@ -286,7 +286,7 @@ func (r *rpcServer) GetInfo(ctx context.Context, pendingChannels := r.server.fundingMgr.NumPendingChannels() idPub := r.server.identityPriv.PubKey().SerializeCompressed() - idAddr, err := btcutil.NewAddressPubKeyHash(btcutil.Hash160(idPub), activeNetParams) + idAddr, err := btcutil.NewAddressPubKeyHash(btcutil.Hash160(idPub), activeNetParams.Params) if err != nil { return nil, err }