lnd: properly switch RPC port with target net change

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.
This commit is contained in:
Olaoluwa Osuntokun 2016-07-13 18:37:50 -07:00
parent 98fae7f329
commit a56ab46e97
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
5 changed files with 44 additions and 19 deletions

@ -9,7 +9,6 @@ import (
"strings" "strings"
flags "github.com/btcsuite/go-flags" flags "github.com/btcsuite/go-flags"
"github.com/roasbeef/btcd/chaincfg"
"github.com/roasbeef/btcutil" "github.com/roasbeef/btcutil"
) )
@ -34,10 +33,6 @@ var (
defaultDataDir = filepath.Join(lndHomeDir, defaultDataDirname) defaultDataDir = filepath.Join(lndHomeDir, defaultDataDirname)
defaultLogDir = filepath.Join(lndHomeDir, defaultLogDirname) 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) btcdHomeDir = btcutil.AppDataDir("btcd", false)
defaultRPCKeyFile = filepath.Join(btcdHomeDir, "rpc.key") defaultRPCKeyFile = filepath.Join(btcdHomeDir, "rpc.key")
defaultRPCCertFile = filepath.Join(btcdHomeDir, "rpc.cert") defaultRPCCertFile = filepath.Join(btcdHomeDir, "rpc.cert")
@ -154,15 +149,11 @@ func loadConfig() (*config, error) {
numNets := 0 numNets := 0
if cfg.TestNet3 { if cfg.TestNet3 {
numNets++ numNets++
activeNetParams = &chaincfg.TestNet3Params activeNetParams = testNetParams
}
if cfg.SegNet {
numNets++
activeNetParams = &chaincfg.SegNet4Params
} }
if cfg.SimNet { if cfg.SimNet {
numNets++ numNets++
activeNetParams = &chaincfg.SimNetParams activeNetParams = simNetParams
} }
if numNets > 1 { if numNets > 1 {
str := "%s: The testnet, segnet, and simnet params can't be " + str := "%s: The testnet, segnet, and simnet params can't be " +

@ -331,7 +331,7 @@ func (f *fundingManager) handleFundingRequest(fmsg *fundingRequestMsg) {
// With our portion of the reservation initialied, process the // With our portion of the reservation initialied, process the
// initiators contribution to the channel. // initiators contribution to the channel.
_, addrs, _, err := txscript.ExtractPkScriptAddrs(msg.DeliveryPkScript, activeNetParams) _, addrs, _, err := txscript.ExtractPkScriptAddrs(msg.DeliveryPkScript, activeNetParams.Params)
if err != nil { if err != nil {
fndgLog.Errorf("Unable to extract addresses from script: %v", err) fndgLog.Errorf("Unable to extract addresses from script: %v", err)
return return
@ -390,7 +390,7 @@ func (f *fundingManager) handleFundingResponse(fmsg *fundingResponseMsg) {
// contribution. At this point, we can process their contribution which // contribution. At this point, we can process their contribution which
// allows us to construct and sign both the commitment transaction, and // allows us to construct and sign both the commitment transaction, and
// the funding transaction. // the funding transaction.
_, addrs, _, err := txscript.ExtractPkScriptAddrs(msg.DeliveryPkScript, activeNetParams) _, addrs, _, err := txscript.ExtractPkScriptAddrs(msg.DeliveryPkScript, activeNetParams.Params)
if err != nil { if err != nil {
fndgLog.Errorf("Unable to extract addresses from script: %v", err) fndgLog.Errorf("Unable to extract addresses from script: %v", err)
return return

9
lnd.go

@ -40,7 +40,7 @@ func lndMain() error {
ltndLog.Infof("Version %s", version()) ltndLog.Infof("Version %s", version())
if loadedConfig.SPVMode == true { if loadedConfig.SPVMode == true {
shell(loadedConfig.SPVHostAdr, activeNetParams) shell(loadedConfig.SPVHostAdr, activeNetParams.Params)
return err return err
} }
@ -57,7 +57,7 @@ func lndMain() error {
// Open the channeldb, which is dedicated to storing channel, and // Open the channeldb, which is dedicated to storing channel, and
// network related meta-data. // network related meta-data.
chanDB, err := channeldb.Open(loadedConfig.DataDir, activeNetParams) chanDB, err := channeldb.Open(loadedConfig.DataDir, activeNetParams.Params)
if err != nil { if err != nil {
fmt.Println("unable to open channeldb: ", err) fmt.Println("unable to open channeldb: ", err)
return err return err
@ -82,11 +82,11 @@ func lndMain() error {
config := &lnwallet.Config{ config := &lnwallet.Config{
PrivatePass: []byte("hello"), PrivatePass: []byte("hello"),
DataDir: filepath.Join(loadedConfig.DataDir, "lnwallet"), DataDir: filepath.Join(loadedConfig.DataDir, "lnwallet"),
RpcHost: loadedConfig.RPCHost, RpcHost: fmt.Sprintf("%v:%v", loadedConfig.RPCHost, activeNetParams.rpcPort),
RpcUser: loadedConfig.RPCUser, RpcUser: loadedConfig.RPCUser,
RpcPass: loadedConfig.RPCPass, RpcPass: loadedConfig.RPCPass,
CACert: cert, CACert: cert,
NetParams: activeNetParams, NetParams: activeNetParams.Params,
} }
wallet, err := lnwallet.NewLightningWallet(config, chanDB) wallet, err := lnwallet.NewLightningWallet(config, chanDB)
if err != nil { if err != nil {
@ -151,6 +151,7 @@ func main() {
// Call the "real" main in a nested manner so the defers will properly // Call the "real" main in a nested manner so the defers will properly
// be executed in the case of a graceful shutdown. // be executed in the case of a graceful shutdown.
if err := lndMain(); err != nil { if err := lndMain(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1) os.Exit(1)
} }
} }

33
params.go Normal file

@ -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",
}

@ -71,7 +71,7 @@ func (r *rpcServer) Stop() error {
func addrPairsToOutputs(addrPairs map[string]int64) ([]*wire.TxOut, error) { func addrPairsToOutputs(addrPairs map[string]int64) ([]*wire.TxOut, error) {
outputs := make([]*wire.TxOut, 0, len(addrPairs)) outputs := make([]*wire.TxOut, 0, len(addrPairs))
for addr, amt := range addrPairs { for addr, amt := range addrPairs {
addr, err := btcutil.DecodeAddress(addr, activeNetParams) addr, err := btcutil.DecodeAddress(addr, activeNetParams.Params)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -286,7 +286,7 @@ func (r *rpcServer) GetInfo(ctx context.Context,
pendingChannels := r.server.fundingMgr.NumPendingChannels() pendingChannels := r.server.fundingMgr.NumPendingChannels()
idPub := r.server.identityPriv.PubKey().SerializeCompressed() 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 { if err != nil {
return nil, err return nil, err
} }