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.
This commit is contained in:
Elle Mouton 2021-03-24 08:48:33 +02:00
parent a0f7bf8b2d
commit ecf20ed350
2 changed files with 13 additions and 7 deletions

View File

@ -235,7 +235,8 @@ type ChainControl struct {
// full-node, another backed by a running bitcoind full-node, and the other // 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 // backed by a running neutrino light client instance. When running with a
// neutrino light client instance, `neutrinoCS` must be non-nil. // 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 // 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. // 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) "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 // If spv mode is active, then we'll be using a distinct set of
// chainControl interfaces that interface directly with the p2p network // chainControl interfaces that interface directly with the p2p network
// of the selected chain. // of the selected chain.

14
lnd.go
View File

@ -34,6 +34,7 @@ import (
"gopkg.in/macaroon.v2" "gopkg.in/macaroon.v2"
"github.com/lightningnetwork/lnd/autopilot" "github.com/lightningnetwork/lnd/autopilot"
"github.com/lightningnetwork/lnd/blockcache"
"github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/cert" "github.com/lightningnetwork/lnd/cert"
"github.com/lightningnetwork/lnd/chainreg" "github.com/lightningnetwork/lnd/chainreg"
@ -254,6 +255,9 @@ func Main(cfg *Config, lisCfg ListenerCfg, interceptor signal.Interceptor) error
defer cleanUp() defer cleanUp()
// Initialize a new block cache.
blockCache := blockcache.NewBlockCache(cfg.BlockCacheSize)
// Before starting the wallet, we'll create and start our Neutrino // Before starting the wallet, we'll create and start our Neutrino
// light client instance, if enabled, in order to allow it to sync // light client instance, if enabled, in order to allow it to sync
// while the rest of the daemon continues startup. // 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 var neutrinoCS *neutrino.ChainService
if mainChain.Node == "neutrino" { if mainChain.Node == "neutrino" {
neutrinoBackend, neutrinoCleanUp, err := initNeutrinoBackend( neutrinoBackend, neutrinoCleanUp, err := initNeutrinoBackend(
cfg, mainChain.ChainDir, cfg, mainChain.ChainDir, blockCache,
) )
if err != nil { if err != nil {
err := fmt.Errorf("unable to initialize neutrino "+ err := fmt.Errorf("unable to initialize neutrino "+
@ -549,7 +553,9 @@ func Main(cfg *Config, lisCfg ListenerCfg, interceptor signal.Interceptor) error
BlockCacheSize: cfg.BlockCacheSize, BlockCacheSize: cfg.BlockCacheSize,
} }
activeChainControl, cleanup, err := chainreg.NewChainControl(chainControlCfg) activeChainControl, cleanup, err := chainreg.NewChainControl(
chainControlCfg, blockCache,
)
if cleanup != nil { if cleanup != nil {
defer cleanup() defer cleanup()
} }
@ -1554,7 +1560,8 @@ func initializeDatabases(ctx context.Context,
// initNeutrinoBackend inits a new instance of the neutrino light client // initNeutrinoBackend inits a new instance of the neutrino light client
// backend given a target chain directory to store the chain state. // 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) { func(), error) {
// Both channel validation flags are false by default but their meaning // 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 return ips, nil
}, },
AssertFilterHeader: headerStateAssertion, AssertFilterHeader: headerStateAssertion,
BlockCache: blockCache.Cache,
} }
neutrino.MaxPeers = 8 neutrino.MaxPeers = 8