chainreg: add Config, move chainparams.go

Creates a Config that initializes the chainregistry struct in the
lnd package. Also moves all of chainparams.go to the chainreg
package.
This commit is contained in:
Eugene 2020-09-29 10:00:19 -07:00 committed by eugene
parent 933b959aa8
commit f4fe76aaf1
8 changed files with 189 additions and 93 deletions

View File

@ -1,4 +1,4 @@
package lnd
package chainreg
import (
"github.com/btcsuite/btcd/chaincfg"
@ -10,91 +10,93 @@ import (
litecoinWire "github.com/ltcsuite/ltcd/wire"
)
// bitcoinNetParams couples the p2p parameters of a network with the
// BitcoinNetParams couples the p2p parameters of a network with the
// corresponding RPC port of a daemon running on the particular network.
type bitcoinNetParams struct {
type BitcoinNetParams struct {
*bitcoinCfg.Params
rpcPort string
RPCPort string
CoinType uint32
}
// litecoinNetParams couples the p2p parameters of a network with the
// LitecoinNetParams couples the p2p parameters of a network with the
// corresponding RPC port of a daemon running on the particular network.
type litecoinNetParams struct {
type LitecoinNetParams struct {
*litecoinCfg.Params
rpcPort string
RPCPort string
CoinType uint32
}
// bitcoinTestNetParams contains parameters specific to the 3rd version of the
// BitcoinTestNetParams contains parameters specific to the 3rd version of the
// test network.
var bitcoinTestNetParams = bitcoinNetParams{
var BitcoinTestNetParams = BitcoinNetParams{
Params: &bitcoinCfg.TestNet3Params,
rpcPort: "18334",
RPCPort: "18334",
CoinType: keychain.CoinTypeTestnet,
}
// bitcoinMainNetParams contains parameters specific to the current Bitcoin
// BitcoinMainNetParams contains parameters specific to the current Bitcoin
// mainnet.
var bitcoinMainNetParams = bitcoinNetParams{
var BitcoinMainNetParams = BitcoinNetParams{
Params: &bitcoinCfg.MainNetParams,
rpcPort: "8334",
RPCPort: "8334",
CoinType: keychain.CoinTypeBitcoin,
}
// bitcoinSimNetParams contains parameters specific to the simulation test
// BitcoinSimNetParams contains parameters specific to the simulation test
// network.
var bitcoinSimNetParams = bitcoinNetParams{
var BitcoinSimNetParams = BitcoinNetParams{
Params: &bitcoinCfg.SimNetParams,
rpcPort: "18556",
RPCPort: "18556",
CoinType: keychain.CoinTypeTestnet,
}
// litecoinSimNetParams contains parameters specific to the simulation test
// LitecoinSimNetParams contains parameters specific to the simulation test
// network.
var litecoinSimNetParams = litecoinNetParams{
Params: &litecoinCfg.SimNetParams,
rpcPort: "18556",
CoinType: keychain.CoinTypeTestnet,
}
// litecoinTestNetParams contains parameters specific to the 4th version of the
// test network.
var litecoinTestNetParams = litecoinNetParams{
var LitecoinSimNetParams = LitecoinNetParams{
Params: &litecoinCfg.TestNet4Params,
rpcPort: "19334",
RPCPort: "18556",
CoinType: keychain.CoinTypeTestnet,
}
// litecoinMainNetParams contains the parameters specific to the current
// LitecoinTestNetParams contains parameters specific to the 4th version of the
// test network.
var LitecoinTestNetParams = LitecoinNetParams{
Params: &litecoinCfg.TestNet4Params,
RPCPort: "19334",
CoinType: keychain.CoinTypeTestnet,
}
// LitecoinMainNetParams contains the parameters specific to the current
// Litecoin mainnet.
var litecoinMainNetParams = litecoinNetParams{
var LitecoinMainNetParams = LitecoinNetParams{
Params: &litecoinCfg.MainNetParams,
rpcPort: "9334",
RPCPort: "9334",
CoinType: keychain.CoinTypeLitecoin,
}
// litecoinRegTestNetParams contains parameters specific to a local litecoin
// LitecoinRegTestNetParams contains parameters specific to a local litecoin
// regtest network.
var litecoinRegTestNetParams = litecoinNetParams{
var LitecoinRegTestNetParams = LitecoinNetParams{
Params: &litecoinCfg.RegressionNetParams,
rpcPort: "18334",
RPCPort: "18334",
CoinType: keychain.CoinTypeTestnet,
}
// bitcoinRegTestNetParams contains parameters specific to a local bitcoin
// BitcoinRegTestNetParams contains parameters specific to a local bitcoin
// regtest network.
var bitcoinRegTestNetParams = bitcoinNetParams{
var BitcoinRegTestNetParams = BitcoinNetParams{
Params: &bitcoinCfg.RegressionNetParams,
rpcPort: "18334",
RPCPort: "18334",
CoinType: keychain.CoinTypeTestnet,
}
// applyLitecoinParams applies the relevant chain configuration parameters that
// ApplyLitecoinParams applies the relevant chain configuration parameters that
// differ for litecoin to the chain parameters typed for btcsuite derivation.
// This function is used in place of using something like interface{} to
// abstract over _which_ chain (or fork) the parameters are for.
func applyLitecoinParams(params *bitcoinNetParams, litecoinParams *litecoinNetParams) {
func ApplyLitecoinParams(params *BitcoinNetParams,
litecoinParams *LitecoinNetParams) {
params.Name = litecoinParams.Name
params.Net = bitcoinWire.BitcoinNet(litecoinParams.Net)
params.DefaultPort = litecoinParams.DefaultPort
@ -127,13 +129,13 @@ func applyLitecoinParams(params *bitcoinNetParams, litecoinParams *litecoinNetPa
}
params.Checkpoints = checkPoints
params.rpcPort = litecoinParams.rpcPort
params.RPCPort = litecoinParams.RPCPort
params.CoinType = litecoinParams.CoinType
}
// isTestnet tests if the given params correspond to a testnet
// IsTestnet tests if the givern params correspond to a testnet
// parameter configuration.
func isTestnet(params *bitcoinNetParams) bool {
func IsTestnet(params *BitcoinNetParams) bool {
switch params.Params.Net {
case bitcoinWire.TestNet3, bitcoinWire.BitcoinNet(litecoinWire.TestNet4):
return true

78
chainreg/chainregistry.go Normal file
View File

@ -0,0 +1,78 @@
package chainreg
import (
"time"
"github.com/btcsuite/btcwallet/wallet"
"github.com/lightninglabs/neutrino"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/lncfg"
)
// Config houses necessary fields that a chainControl instance needs to
// function.
type Config struct {
// Bitcoin defines settings for the Bitcoin chain.
Bitcoin *lncfg.Chain
// Litecoin defines settings for the Litecoin chain.
Litecoin *lncfg.Chain
// PrimaryChain is a function that returns our primary chain via its
// ChainCode.
PrimaryChain func() ChainCode
// HeightHintCacheQueryDisable is a boolean that disables height hint
// queries if true.
HeightHintCacheQueryDisable bool
// NeutrinoMode defines settings for connecting to a neutrino light-client.
NeutrinoMode *lncfg.Neutrino
// BitcoindMode defines settings for connecting to a bitcoind node.
BitcoindMode *lncfg.Bitcoind
// LitecoindMode defines settings for connecting to a litecoind node.
LitecoindMode *lncfg.Bitcoind
// BtcdMode defines settings for connecting to a btcd node.
BtcdMode *lncfg.Btcd
// LtcdMode defines settings for connecting to an ltcd node.
LtcdMode *lncfg.Btcd
// LocalChanDB is a pointer to the local backing channel database.
LocalChanDB *channeldb.DB
// RemoteChanDB is a pointer to the remote backing channel database.
RemoteChanDB *channeldb.DB
// PrivateWalletPw is the private wallet password to the underlying
// btcwallet instance.
PrivateWalletPw []byte
// PublicWalletPw is the public wallet password to the underlying btcwallet
// instance.
PublicWalletPw []byte
// Birthday specifies the time the wallet was initially created.
Birthday time.Time
// RecoveryWindow specifies the address look-ahead for which to scan when
// restoring a wallet.
RecoveryWindow uint32
// Wallet is a pointer to the backing wallet instance.
Wallet *wallet.Wallet
// NeutrinoCS is a pointer to a neutrino ChainService. Must be non-nil if
// using neutrino.
NeutrinoCS *neutrino.ChainService
// ActiveNetParams details the current chain we are on.
ActiveNetParams BitcoinNetParams
// FeeURL defines the URL for fee estimation we will use. This field is
// optional.
FeeURL string
}

View File

@ -16,8 +16,6 @@ import (
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwallet/chain"
"github.com/btcsuite/btcwallet/wallet"
"github.com/lightninglabs/neutrino"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/chainntnfs/bitcoindnotify"
"github.com/lightningnetwork/lnd/chainntnfs/btcdnotify"
@ -130,29 +128,26 @@ type chainControl struct {
minHtlcIn lnwire.MilliSatoshi
}
// newChainControlFromConfig attempts to create a chainControl instance
// according to the parameters in the passed lnd configuration. Currently three
// newChainControl attempts to create a chainControl instance according
// to the parameters in the passed configuration. Currently three
// branches of chainControl instances exist: one backed by a running btcd
// full-node, another backed by a running bitcoind full-node, and the other
// backed by a running neutrino light client instance. When running with a
// neutrino light client instance, `neutrinoCS` must be non-nil.
func newChainControlFromConfig(cfg *Config, localDB, remoteDB *channeldb.DB,
privateWalletPw, publicWalletPw []byte, birthday time.Time,
recoveryWindow uint32, wallet *wallet.Wallet,
neutrinoCS *neutrino.ChainService) (*chainControl, error) {
func newChainControl(cfg *chainreg.Config) (*chainControl, error) {
// 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 cfg.registeredChains.PrimaryChain() == chainreg.LitecoinChain {
if cfg.PrimaryChain() == chainreg.LitecoinChain {
homeChainConfig = cfg.Litecoin
}
ltndLog.Infof("Primary chain is set to: %v",
cfg.registeredChains.PrimaryChain())
cfg.PrimaryChain())
cc := &chainControl{}
switch cfg.registeredChains.PrimaryChain() {
switch cfg.PrimaryChain() {
case chainreg.BitcoinChain:
cc.routingPolicy = htlcswitch.ForwardingPolicy{
MinHTLCOut: cfg.Bitcoin.MinHTLCOut,
@ -178,18 +173,18 @@ func newChainControlFromConfig(cfg *Config, localDB, remoteDB *channeldb.DB,
)
default:
return nil, fmt.Errorf("default routing policy for chain %v is "+
"unknown", cfg.registeredChains.PrimaryChain())
"unknown", cfg.PrimaryChain())
}
walletConfig := &btcwallet.Config{
PrivatePass: privateWalletPw,
PublicPass: publicWalletPw,
Birthday: birthday,
RecoveryWindow: recoveryWindow,
PrivatePass: cfg.PrivateWalletPw,
PublicPass: cfg.PublicWalletPw,
Birthday: cfg.Birthday,
RecoveryWindow: cfg.RecoveryWindow,
DataDir: homeChainConfig.ChainDir,
NetParams: cfg.ActiveNetParams.Params,
CoinType: cfg.ActiveNetParams.CoinType,
Wallet: wallet,
Wallet: cfg.Wallet,
}
var err error
@ -202,7 +197,9 @@ func newChainControlFromConfig(cfg *Config, localDB, remoteDB *channeldb.DB,
}
// Initialize the height hint cache within the chain directory.
hintCache, err := chainntnfs.NewHeightHintCache(heightHintCacheConfig, localDB)
hintCache, err := chainntnfs.NewHeightHintCache(
heightHintCacheConfig, cfg.LocalChanDB,
)
if err != nil {
return nil, fmt.Errorf("unable to initialize height hint "+
"cache: %v", err)
@ -217,9 +214,9 @@ func newChainControlFromConfig(cfg *Config, localDB, remoteDB *channeldb.DB,
// along with the wallet's ChainSource, which are all backed by
// the neutrino light client.
cc.chainNotifier = neutrinonotify.New(
neutrinoCS, hintCache, hintCache,
cfg.NeutrinoCS, hintCache, hintCache,
)
cc.chainView, err = chainview.NewCfFilteredChainView(neutrinoCS)
cc.chainView, err = chainview.NewCfFilteredChainView(cfg.NeutrinoCS)
if err != nil {
return nil, err
}
@ -236,7 +233,7 @@ func newChainControlFromConfig(cfg *Config, localDB, remoteDB *channeldb.DB,
}
walletConfig.ChainSource = chain.NewNeutrinoClient(
cfg.ActiveNetParams.Params, neutrinoCS,
cfg.ActiveNetParams.Params, cfg.NeutrinoCS,
)
case "bitcoind", "litecoind":
@ -260,7 +257,7 @@ func newChainControlFromConfig(cfg *Config, localDB, remoteDB *channeldb.DB,
// btcd, which picks a different port so that btcwallet
// can use the same RPC port as bitcoind. We convert
// this back to the btcwallet/bitcoind port.
rpcPort, err := strconv.Atoi(cfg.ActiveNetParams.rpcPort)
rpcPort, err := strconv.Atoi(cfg.ActiveNetParams.RPCPort)
if err != nil {
return nil, err
}
@ -395,7 +392,7 @@ func newChainControlFromConfig(cfg *Config, localDB, remoteDB *channeldb.DB,
btcdHost = btcdMode.RPCHost
} else {
btcdHost = fmt.Sprintf("%v:%v", btcdMode.RPCHost,
cfg.ActiveNetParams.rpcPort)
cfg.ActiveNetParams.RPCPort)
}
btcdUser := btcdMode.RPCUser
@ -494,7 +491,7 @@ func newChainControlFromConfig(cfg *Config, localDB, remoteDB *channeldb.DB,
// Select the default channel constraints for the primary chain.
channelConstraints := defaultBtcChannelConstraints
if cfg.registeredChains.PrimaryChain() == chainreg.LitecoinChain {
if cfg.PrimaryChain() == chainreg.LitecoinChain {
channelConstraints = defaultLtcChannelConstraints
}
@ -506,7 +503,7 @@ func newChainControlFromConfig(cfg *Config, localDB, remoteDB *channeldb.DB,
// Create, and start the lnwallet, which handles the core payment
// channel logic, and exposes control via proxy state machines.
walletCfg := lnwallet.Config{
Database: remoteDB,
Database: cfg.RemoteChanDB,
Notifier: cc.chainNotifier,
WalletController: wc,
Signer: cc.signer,
@ -622,7 +619,7 @@ type chainRegistry struct {
sync.RWMutex
activeChains map[chainreg.ChainCode]*chainControl
netParams map[chainreg.ChainCode]*bitcoinNetParams
netParams map[chainreg.ChainCode]*chainreg.BitcoinNetParams
primaryChain chainreg.ChainCode
}
@ -631,7 +628,7 @@ type chainRegistry struct {
func newChainRegistry() *chainRegistry {
return &chainRegistry{
activeChains: make(map[chainreg.ChainCode]*chainControl),
netParams: make(map[chainreg.ChainCode]*bitcoinNetParams),
netParams: make(map[chainreg.ChainCode]*chainreg.BitcoinNetParams),
}
}

View File

@ -320,7 +320,7 @@ type Config struct {
networkDir string
// ActiveNetParams contains parameters of the target chain.
ActiveNetParams bitcoinNetParams
ActiveNetParams chainreg.BitcoinNetParams
}
// DefaultConfig returns all default values for the Config struct.
@ -453,7 +453,7 @@ func DefaultConfig() Config {
LogWriter: build.NewRotatingLogWriter(),
DB: lncfg.DefaultDB(),
registeredChains: newChainRegistry(),
ActiveNetParams: bitcoinTestNetParams,
ActiveNetParams: chainreg.BitcoinTestNetParams,
}
}
@ -830,22 +830,22 @@ func ValidateConfig(cfg Config, usageMessage string) (*Config, error) {
// number of network flags passed; assign active network params
// while we're at it.
numNets := 0
var ltcParams litecoinNetParams
var ltcParams chainreg.LitecoinNetParams
if cfg.Litecoin.MainNet {
numNets++
ltcParams = litecoinMainNetParams
ltcParams = chainreg.LitecoinMainNetParams
}
if cfg.Litecoin.TestNet3 {
numNets++
ltcParams = litecoinTestNetParams
ltcParams = chainreg.LitecoinTestNetParams
}
if cfg.Litecoin.RegTest {
numNets++
ltcParams = litecoinRegTestNetParams
ltcParams = chainreg.LitecoinRegTestNetParams
}
if cfg.Litecoin.SimNet {
numNets++
ltcParams = litecoinSimNetParams
ltcParams = chainreg.LitecoinSimNetParams
}
if numNets > 1 {
@ -869,7 +869,7 @@ func ValidateConfig(cfg Config, usageMessage string) (*Config, error) {
// 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.
applyLitecoinParams(&cfg.ActiveNetParams, &ltcParams)
chainreg.ApplyLitecoinParams(&cfg.ActiveNetParams, &ltcParams)
switch cfg.Litecoin.Node {
case "ltcd":
@ -914,19 +914,19 @@ func ValidateConfig(cfg Config, usageMessage string) (*Config, error) {
numNets := 0
if cfg.Bitcoin.MainNet {
numNets++
cfg.ActiveNetParams = bitcoinMainNetParams
cfg.ActiveNetParams = chainreg.BitcoinMainNetParams
}
if cfg.Bitcoin.TestNet3 {
numNets++
cfg.ActiveNetParams = bitcoinTestNetParams
cfg.ActiveNetParams = chainreg.BitcoinTestNetParams
}
if cfg.Bitcoin.RegTest {
numNets++
cfg.ActiveNetParams = bitcoinRegTestNetParams
cfg.ActiveNetParams = chainreg.BitcoinRegTestNetParams
}
if cfg.Bitcoin.SimNet {
numNets++
cfg.ActiveNetParams = bitcoinSimNetParams
cfg.ActiveNetParams = chainreg.BitcoinSimNetParams
}
if numNets > 1 {
str := "%s: The mainnet, testnet, regtest, and " +
@ -1315,7 +1315,7 @@ func CleanAndExpandPath(path string) string {
func parseRPCParams(cConfig *lncfg.Chain, nodeConfig interface{},
net chainreg.ChainCode, funcName string,
netParams bitcoinNetParams) error { // nolint:unparam
netParams chainreg.BitcoinNetParams) error { // nolint:unparam
// First, we'll check our node config to make sure the RPC parameters
// were set correctly. We'll also determine the path to the conf file

View File

@ -21,9 +21,8 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/chainreg"
"github.com/lightningnetwork/lnd/chanacceptor"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channelnotifier"
@ -31,6 +30,7 @@ import (
"github.com/lightningnetwork/lnd/htlcswitch"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/lnpeer"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntest/mock"
@ -104,7 +104,7 @@ var (
_, _ = testSig.R.SetString("63724406601629180062774974542967536251589935445068131219452686511677818569431", 10)
_, _ = testSig.S.SetString("18801056069249825825291287104931333862866033135609736119018462340006816851118", 10)
fundingNetParams = bitcoinTestNetParams
fundingNetParams = chainreg.BitcoinTestNetParams
)
type mockNotifier struct {

28
lnd.go
View File

@ -450,11 +450,29 @@ func Main(cfg *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error {
// When we create the chain control, we need storage for the height
// hints and also the wallet itself, for these two we want them to be
// replicated, so we'll pass in the remote channel DB instance.
activeChainControl, err := newChainControlFromConfig(
cfg, localChanDB, remoteChanDB, privateWalletPw, publicWalletPw,
walletInitParams.Birthday, walletInitParams.RecoveryWindow,
walletInitParams.Wallet, neutrinoCS,
)
chainControlCfg := &chainreg.Config{
Bitcoin: cfg.Bitcoin,
Litecoin: cfg.Litecoin,
PrimaryChain: cfg.registeredChains.PrimaryChain,
HeightHintCacheQueryDisable: cfg.HeightHintCacheQueryDisable,
NeutrinoMode: cfg.NeutrinoMode,
BitcoindMode: cfg.BitcoindMode,
LitecoindMode: cfg.LitecoindMode,
BtcdMode: cfg.BtcdMode,
LtcdMode: cfg.LtcdMode,
LocalChanDB: localChanDB,
RemoteChanDB: remoteChanDB,
PrivateWalletPw: privateWalletPw,
PublicWalletPw: publicWalletPw,
Birthday: walletInitParams.Birthday,
RecoveryWindow: walletInitParams.RecoveryWindow,
Wallet: walletInitParams.Wallet,
NeutrinoCS: neutrinoCS,
ActiveNetParams: cfg.ActiveNetParams,
FeeURL: cfg.FeeURL,
}
activeChainControl, err := newChainControl(chainControlCfg)
if err != nil {
err := fmt.Errorf("unable to create chain control: %v", err)
ltndLog.Error(err)

View File

@ -9,6 +9,7 @@ import (
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/autopilot"
"github.com/lightningnetwork/lnd/chainreg"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/tor"
@ -76,7 +77,7 @@ type chanController struct {
minConfs int32
confTarget uint32
chanMinHtlcIn lnwire.MilliSatoshi
netParams bitcoinNetParams
netParams chainreg.BitcoinNetParams
}
// OpenChannel opens a channel to a target peer, with a capacity of the
@ -134,8 +135,8 @@ var _ autopilot.ChannelController = (*chanController)(nil)
// interfaces needed to drive it won't be launched before the Manager's
// StartAgent method is called.
func initAutoPilot(svr *server, cfg *lncfg.AutoPilot,
chainCfg *lncfg.Chain, netParams bitcoinNetParams) (*autopilot.ManagerCfg,
error) {
chainCfg *lncfg.Chain, netParams chainreg.BitcoinNetParams) (
*autopilot.ManagerCfg, error) {
atplLog.Infof("Instantiating autopilot with active=%v, "+
"max_channels=%d, allocation=%f, min_chan_size=%d, "+

View File

@ -2584,7 +2584,7 @@ func (r *rpcServer) GetInfo(ctx context.Context,
BlockHeight: uint32(bestHeight),
BlockHash: bestHash.String(),
SyncedToChain: isSynced,
Testnet: isTestnet(&r.cfg.ActiveNetParams),
Testnet: chainreg.IsTestnet(&r.cfg.ActiveNetParams),
Chains: activeChains,
Uris: uris,
Alias: nodeAnn.Alias.String(),