multi: remove ChainRegistry

This commit is contained in:
Elle Mouton 2023-08-03 18:12:49 +02:00 committed by Olaoluwa Osuntokun
parent 2a7fffe441
commit 031dbd7760
9 changed files with 24 additions and 197 deletions

View File

@ -1,25 +0,0 @@
package chainreg
// ChainCode is an enum-like structure for keeping track of the chains
// currently supported within lnd.
type ChainCode uint32
const (
// BitcoinChain is Bitcoin's chain.
BitcoinChain ChainCode = iota
// LitecoinChain is Litecoin's chain.
LitecoinChain
)
// String returns a string representation of the target ChainCode.
func (c ChainCode) String() string {
switch c {
case BitcoinChain:
return "bitcoin"
case LitecoinChain:
return "litecoin"
default:
return "kekcoin"
}
}

View File

@ -11,7 +11,6 @@ import (
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/btcsuite/btcd/chaincfg/chainhash"
@ -42,10 +41,6 @@ type Config struct {
// Bitcoin defines settings for the Bitcoin chain.
Bitcoin *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
@ -224,32 +219,19 @@ func GenDefaultBtcConstraints() channeldb.ChannelConstraints {
//
//nolint:lll
func NewPartialChainControl(cfg *Config) (*PartialChainControl, func(), 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
log.Infof("Primary chain is set to: %v", cfg.PrimaryChain())
cc := &PartialChainControl{
Cfg: cfg,
}
switch cfg.PrimaryChain() {
case BitcoinChain:
cc.RoutingPolicy = models.ForwardingPolicy{
RoutingPolicy: models.ForwardingPolicy{
MinHTLCOut: cfg.Bitcoin.MinHTLCOut,
BaseFee: cfg.Bitcoin.BaseFee,
FeeRate: cfg.Bitcoin.FeeRate,
TimeLockDelta: cfg.Bitcoin.TimeLockDelta,
}
cc.MinHtlcIn = cfg.Bitcoin.MinHTLCIn
cc.FeeEstimator = chainfee.NewStaticEstimator(
},
MinHtlcIn: cfg.Bitcoin.MinHTLCIn,
FeeEstimator: chainfee.NewStaticEstimator(
DefaultBitcoinStaticFeePerKW,
DefaultBitcoinStaticMinRelayFeeRate,
)
default:
return nil, nil, fmt.Errorf("default routing policy for chain "+
"%v is unknown", cfg.PrimaryChain())
),
}
var err error
@ -272,7 +254,7 @@ func NewPartialChainControl(cfg *Config) (*PartialChainControl, func(), error) {
// If spv mode is active, then we'll be using a distinct set of
// chainControl interfaces that interface directly with the p2p network
// of the selected chain.
switch homeChainConfig.Node {
switch cfg.Bitcoin.Node {
case "neutrino":
// We'll create ChainNotifier and FilteredChainView instances,
// along with the wallet's ChainSource, which are all backed by
@ -682,7 +664,7 @@ func NewPartialChainControl(cfg *Config) (*PartialChainControl, func(), error) {
default:
return nil, nil, fmt.Errorf("unknown node type: %s",
homeChainConfig.Node)
cfg.Bitcoin.Node)
}
switch {
@ -690,7 +672,7 @@ func NewPartialChainControl(cfg *Config) (*PartialChainControl, func(), error) {
// we'll return an error to instruct them to set a proper fee
// estimator.
case cfg.FeeURL == "" && cfg.Bitcoin.MainNet &&
homeChainConfig.Node == "neutrino":
cfg.Bitcoin.Node == "neutrino":
return nil, nil, fmt.Errorf("--feeurl parameter required " +
"when running neutrino on mainnet")
@ -834,13 +816,6 @@ var (
0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
})
// chainMap is a simple index that maps a chain's genesis hash to the
// ChainCode enum for that chain.
chainMap = map[chainhash.Hash]ChainCode{
BitcoinTestnetGenesis: BitcoinChain,
BitcoinMainnetGenesis: BitcoinChain,
}
// ChainDNSSeeds is a map of a chain's hash to the set of DNS seeds
// that will be use to bootstrap peers upon first startup.
//
@ -878,98 +853,3 @@ var (
},
}
)
// ChainRegistry keeps track of the current chains.
type ChainRegistry struct {
sync.RWMutex
activeChains map[ChainCode]*ChainControl
netParams map[ChainCode]*BitcoinNetParams
primaryChain ChainCode
}
// NewChainRegistry creates a new ChainRegistry.
func NewChainRegistry() *ChainRegistry {
return &ChainRegistry{
activeChains: make(map[ChainCode]*ChainControl),
netParams: make(map[ChainCode]*BitcoinNetParams),
}
}
// RegisterChain assigns an active ChainControl instance to a target chain
// identified by its ChainCode.
func (c *ChainRegistry) RegisterChain(newChain ChainCode,
cc *ChainControl) {
c.Lock()
c.activeChains[newChain] = cc
c.Unlock()
}
// LookupChain attempts to lookup an active ChainControl instance for the
// target chain.
func (c *ChainRegistry) LookupChain(targetChain ChainCode) (
*ChainControl, bool) {
c.RLock()
cc, ok := c.activeChains[targetChain]
c.RUnlock()
return cc, ok
}
// LookupChainByHash attempts to look up an active ChainControl which
// corresponds to the passed genesis hash.
func (c *ChainRegistry) LookupChainByHash(
chainHash chainhash.Hash) (*ChainControl, bool) {
c.RLock()
defer c.RUnlock()
targetChain, ok := chainMap[chainHash]
if !ok {
return nil, ok
}
cc, ok := c.activeChains[targetChain]
return cc, ok
}
// RegisterPrimaryChain sets a target chain as the "home chain" for lnd.
func (c *ChainRegistry) RegisterPrimaryChain(cc ChainCode) {
c.Lock()
defer c.Unlock()
c.primaryChain = cc
}
// PrimaryChain returns the primary chain for this running lnd instance. The
// primary chain is considered the "home base" while the other registered
// chains are treated as secondary chains.
func (c *ChainRegistry) PrimaryChain() ChainCode {
c.RLock()
defer c.RUnlock()
return c.primaryChain
}
// ActiveChains returns a slice containing the active chains.
func (c *ChainRegistry) ActiveChains() []ChainCode {
c.RLock()
defer c.RUnlock()
chains := make([]ChainCode, 0, len(c.activeChains))
for activeChain := range c.activeChains {
chains = append(chains, activeChain)
}
return chains
}
// NumActiveChains returns the total number of active chains.
func (c *ChainRegistry) NumActiveChains() uint32 {
c.RLock()
defer c.RUnlock()
return uint32(len(c.activeChains))
}

View File

@ -225,6 +225,9 @@ const (
// defaultGrpcClientPingMinWait is the default minimum amount of time a
// client should wait before sending a keepalive ping.
defaultGrpcClientPingMinWait = 5 * time.Second
// BitcoinChainName is a string that represents the Bitcoin blockchain.
BitcoinChainName = "bitcoin"
)
var (
@ -471,10 +474,6 @@ type Config struct {
// hooked up to.
LogWriter *build.RotatingLogWriter
// registeredChains keeps track of all chains that have been registered
// with the daemon.
registeredChains *chainreg.ChainRegistry
// networkDir is the path to the directory of the currently active
// network. This path will hold the files related to each different
// network.
@ -670,7 +669,6 @@ func DefaultConfig() Config {
DB: lncfg.DefaultDB(),
Cluster: lncfg.DefaultCluster(),
RPCMiddleware: lncfg.DefaultRPCMiddleware(),
registeredChains: chainreg.NewChainRegistry(),
ActiveNetParams: chainreg.BitcoinTestNetParams,
ChannelCommitInterval: defaultChannelCommitInterval,
PendingCommitInterval: defaultPendingCommitInterval,
@ -1218,8 +1216,7 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser,
switch cfg.Bitcoin.Node {
case "btcd":
err := parseRPCParams(
cfg.Bitcoin, cfg.BtcdMode,
chainreg.BitcoinChain, cfg.ActiveNetParams,
cfg.Bitcoin, cfg.BtcdMode, cfg.ActiveNetParams,
)
if err != nil {
return nil, mkErr("unable to load RPC "+
@ -1232,8 +1229,7 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser,
}
err := parseRPCParams(
cfg.Bitcoin, cfg.BitcoindMode,
chainreg.BitcoinChain, cfg.ActiveNetParams,
cfg.Bitcoin, cfg.BitcoindMode, cfg.ActiveNetParams,
)
if err != nil {
return nil, mkErr("unable to load RPC "+
@ -1253,14 +1249,9 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser,
}
cfg.Bitcoin.ChainDir = filepath.Join(
cfg.DataDir, defaultChainSubDirname,
chainreg.BitcoinChain.String(),
cfg.DataDir, defaultChainSubDirname, BitcoinChainName,
)
// Finally we'll register the bitcoin chain as our current
// primary chain.
cfg.registeredChains.RegisterPrimaryChain(chainreg.BitcoinChain)
// Ensure that the user didn't attempt to specify negative values for
// any of the autopilot params.
if cfg.Autopilot.MaxChannels < 0 {
@ -1317,8 +1308,7 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser,
// We'll now construct the network directory which will be where we
// store all the data specific to this chain/network.
cfg.networkDir = filepath.Join(
cfg.DataDir, defaultChainSubDirname,
cfg.registeredChains.PrimaryChain().String(),
cfg.DataDir, defaultChainSubDirname, BitcoinChainName,
lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name),
)
@ -1342,8 +1332,7 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser,
}
towerDir := filepath.Join(
cfg.Watchtower.TowerDir,
cfg.registeredChains.PrimaryChain().String(),
cfg.Watchtower.TowerDir, BitcoinChainName,
lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name),
)
@ -1376,7 +1365,7 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser,
// Append the network type to the log directory so it is "namespaced"
// per network in the same fashion as the data directory.
cfg.LogDir = filepath.Join(
cfg.LogDir, cfg.registeredChains.PrimaryChain().String(),
cfg.LogDir, BitcoinChainName,
lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name),
)
@ -1753,7 +1742,7 @@ func CleanAndExpandPath(path string) string {
}
func parseRPCParams(cConfig *lncfg.Chain, nodeConfig interface{},
net chainreg.ChainCode, netParams chainreg.BitcoinNetParams) error {
netParams chainreg.BitcoinNetParams) error {
// 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

@ -538,7 +538,6 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
// replicated, so we'll pass in the remote channel DB instance.
chainControlCfg := &chainreg.Config{
Bitcoin: d.cfg.Bitcoin,
PrimaryChain: d.cfg.registeredChains.PrimaryChain,
HeightHintCacheQueryDisable: d.cfg.HeightHintCacheQueryDisable,
NeutrinoMode: d.cfg.NeutrinoMode,
BitcoindMode: d.cfg.BitcoindMode,
@ -911,8 +910,7 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
databaseBackends, err := cfg.DB.GetBackends(
ctx, cfg.graphDatabaseDir(), cfg.networkDir, filepath.Join(
cfg.Watchtower.TowerDir,
cfg.registeredChains.PrimaryChain().String(),
cfg.Watchtower.TowerDir, BitcoinChainName,
lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name),
), cfg.WtClient.Active, cfg.Watchtower.Active, d.logger,
)

View File

@ -19,7 +19,6 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/go-errors/errors"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/chainreg"
"github.com/lightningnetwork/lnd/chanacceptor"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
@ -518,10 +517,6 @@ type Config struct {
// is enabled.
EnableUpfrontShutdown bool
// RegisteredChains keeps track of all chains that have been registered
// with the daemon.
RegisteredChains *chainreg.ChainRegistry
// MaxAnchorsCommitFeeRate is the max commitment fee rate we'll use as
// the initiator for channels of the anchor type.
MaxAnchorsCommitFeeRate chainfee.SatPerKWeight

View File

@ -549,7 +549,6 @@ func createTestFundingManager(t *testing.T, privKey *btcec.PrivateKey,
NotifyOpenChannelEvent: evt.NotifyOpenChannelEvent,
OpenChannelPredicate: chainedAcceptor,
NotifyPendingOpenChannelEvent: evt.NotifyPendingOpenChannelEvent,
RegisteredChains: chainreg.NewChainRegistry(),
DeleteAliasEdge: func(scid lnwire.ShortChannelID) (
*channeldb.ChannelEdgePolicy, error) {

9
lnd.go
View File

@ -177,8 +177,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg,
}
ltndLog.Infof("Active chain: %v (network=%v)",
strings.Title(cfg.registeredChains.PrimaryChain().String()),
network,
strings.Title(BitcoinChainName), network,
)
ctx := context.Background()
@ -453,12 +452,6 @@ func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg,
defer cleanUp()
// 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 := cfg.registeredChains.PrimaryChain()
cfg.registeredChains.RegisterChain(primaryChain, activeChainControl)
// TODO(roasbeef): add rotation
idKeyDesc, err := activeChainControl.KeyRing.DeriveKey(
keychain.KeyLocator{

View File

@ -2955,12 +2955,11 @@ func (r *rpcServer) GetInfo(_ context.Context,
}
network := lncfg.NormalizeNetwork(r.cfg.ActiveNetParams.Name)
activeChains := make([]*lnrpc.Chain, r.cfg.registeredChains.NumActiveChains())
for i, chain := range r.cfg.registeredChains.ActiveChains() {
activeChains[i] = &lnrpc.Chain{
Chain: chain.String(),
activeChains := []*lnrpc.Chain{
{
Chain: BitcoinChainName,
Network: network,
}
},
}
// Check if external IP addresses were provided to lnd and use them

View File

@ -1447,7 +1447,6 @@ func newServer(cfg *Config, listenAddrs []net.Addr,
OpenChannelPredicate: chanPredicate,
NotifyPendingOpenChannelEvent: s.channelNotifier.NotifyPendingOpenChannelEvent,
EnableUpfrontShutdown: cfg.EnableUpfrontShutdown,
RegisteredChains: cfg.registeredChains,
MaxAnchorsCommitFeeRate: chainfee.SatPerKVByte(
s.cfg.MaxCommitFeeRateAnchors * 1000).FeePerKWeight(),
DeleteAliasEdge: deleteAliasEdge,