From ecf20ed35080d352bd7b7993303a3172dcc9b370 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Wed, 24 Mar 2021 08:48:33 +0200 Subject: [PATCH] multi: init neutrino backend with block cache This commit initializes the nwutrino backend with the lnd blockcache so that the two can share a block cache instead of each having its own. --- chainreg/chainregistry.go | 6 ++---- lnd.go | 14 +++++++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/chainreg/chainregistry.go b/chainreg/chainregistry.go index db2d4b92..5093f788 100644 --- a/chainreg/chainregistry.go +++ b/chainreg/chainregistry.go @@ -235,7 +235,8 @@ type ChainControl struct { // 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 NewChainControl(cfg *Config) (*ChainControl, func(), error) { +func NewChainControl(cfg *Config, blockCache *blockcache.BlockCache) ( + *ChainControl, 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. @@ -307,9 +308,6 @@ func NewChainControl(cfg *Config) (*ChainControl, func(), error) { "cache: %v", err) } - // Initialize a new block cache. - blockCache := blockcache.NewBlockCache(cfg.BlockCacheSize) - // 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. diff --git a/lnd.go b/lnd.go index 8e05ee5d..2559d0aa 100644 --- a/lnd.go +++ b/lnd.go @@ -34,6 +34,7 @@ import ( "gopkg.in/macaroon.v2" "github.com/lightningnetwork/lnd/autopilot" + "github.com/lightningnetwork/lnd/blockcache" "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/cert" "github.com/lightningnetwork/lnd/chainreg" @@ -254,6 +255,9 @@ func Main(cfg *Config, lisCfg ListenerCfg, interceptor signal.Interceptor) error defer cleanUp() + // Initialize a new block cache. + blockCache := blockcache.NewBlockCache(cfg.BlockCacheSize) + // Before starting the wallet, we'll create and start our Neutrino // light client instance, if enabled, in order to allow it to sync // while the rest of the daemon continues startup. @@ -264,7 +268,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, interceptor signal.Interceptor) error var neutrinoCS *neutrino.ChainService if mainChain.Node == "neutrino" { neutrinoBackend, neutrinoCleanUp, err := initNeutrinoBackend( - cfg, mainChain.ChainDir, + cfg, mainChain.ChainDir, blockCache, ) if err != nil { err := fmt.Errorf("unable to initialize neutrino "+ @@ -549,7 +553,9 @@ func Main(cfg *Config, lisCfg ListenerCfg, interceptor signal.Interceptor) error BlockCacheSize: cfg.BlockCacheSize, } - activeChainControl, cleanup, err := chainreg.NewChainControl(chainControlCfg) + activeChainControl, cleanup, err := chainreg.NewChainControl( + chainControlCfg, blockCache, + ) if cleanup != nil { defer cleanup() } @@ -1554,7 +1560,8 @@ func initializeDatabases(ctx context.Context, // initNeutrinoBackend inits a new instance of the neutrino light client // backend given a target chain directory to store the chain state. -func initNeutrinoBackend(cfg *Config, chainDir string) (*neutrino.ChainService, +func initNeutrinoBackend(cfg *Config, chainDir string, + blockCache *blockcache.BlockCache) (*neutrino.ChainService, func(), error) { // Both channel validation flags are false by default but their meaning @@ -1662,6 +1669,7 @@ func initNeutrinoBackend(cfg *Config, chainDir string) (*neutrino.ChainService, return ips, nil }, AssertFilterHeader: headerStateAssertion, + BlockCache: blockCache.Cache, } neutrino.MaxPeers = 8