From a0f7bf8b2d6d54d5a3fd10f7d12427dbd02e838a Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Thu, 18 Mar 2021 14:54:36 +0200 Subject: [PATCH] chainntnfs: add block cache to BtcdNotifier This commit adds gives BtcdNotifier access to the block cache and wraps its GetBlock method so that it uses the block cache. --- chainntnfs/btcdnotify/btcd.go | 21 ++++++++++++++++++--- chainntnfs/btcdnotify/btcd_test.go | 6 +++++- chainntnfs/btcdnotify/driver.go | 15 ++++++++++++--- chainntnfs/test/test_interface.go | 2 +- chainreg/chainregistry.go | 3 ++- lnwallet/test/test_interface.go | 3 ++- 6 files changed, 40 insertions(+), 10 deletions(-) diff --git a/chainntnfs/btcdnotify/btcd.go b/chainntnfs/btcdnotify/btcd.go index dc36503e..8ad840e3 100644 --- a/chainntnfs/btcdnotify/btcd.go +++ b/chainntnfs/btcdnotify/btcd.go @@ -14,6 +14,7 @@ import ( "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" + "github.com/lightningnetwork/lnd/blockcache" "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/queue" ) @@ -69,6 +70,9 @@ type BtcdNotifier struct { bestBlock chainntnfs.BlockEpoch + // blockCache is a LRU block cache. + blockCache *blockcache.BlockCache + chainUpdates *queue.ConcurrentQueue txUpdates *queue.ConcurrentQueue @@ -94,7 +98,8 @@ var _ chainntnfs.ChainNotifier = (*BtcdNotifier)(nil) // accept new websockets clients. func New(config *rpcclient.ConnConfig, chainParams *chaincfg.Params, spendHintCache chainntnfs.SpendHintCache, - confirmHintCache chainntnfs.ConfirmHintCache) (*BtcdNotifier, error) { + confirmHintCache chainntnfs.ConfirmHintCache, + blockCache *blockcache.BlockCache) (*BtcdNotifier, error) { notifier := &BtcdNotifier{ chainParams: chainParams, @@ -110,6 +115,8 @@ func New(config *rpcclient.ConnConfig, chainParams *chaincfg.Params, spendHintCache: spendHintCache, confirmHintCache: confirmHintCache, + blockCache: blockCache, + quit: make(chan struct{}), } @@ -578,7 +585,7 @@ func (b *BtcdNotifier) confDetailsManually(confRequest chainntnfs.ConfRequest, } // TODO: fetch the neutrino filters instead. - block, err := b.chainConn.GetBlock(blockHash) + block, err := b.GetBlock(blockHash) if err != nil { return nil, chainntnfs.TxNotFoundManually, fmt.Errorf("unable to get block with hash "+ @@ -616,7 +623,7 @@ func (b *BtcdNotifier) handleBlockConnected(epoch chainntnfs.BlockEpoch) error { // First, we'll fetch the raw block as we'll need to gather all the // transactions to determine whether any are relevant to our registered // clients. - rawBlock, err := b.chainConn.GetBlock(epoch.Hash) + rawBlock, err := b.GetBlock(epoch.Hash) if err != nil { return fmt.Errorf("unable to get block: %v", err) } @@ -1012,3 +1019,11 @@ func (b *BtcdNotifier) RegisterBlockEpochNtfn( }, nil } } + +// GetBlock is used to retrieve the block with the given hash. This function +// wraps the blockCache's GetBlock function. +func (b *BtcdNotifier) GetBlock(hash *chainhash.Hash) (*wire.MsgBlock, + error) { + + return b.blockCache.GetBlock(hash, b.chainConn.GetBlock) +} diff --git a/chainntnfs/btcdnotify/btcd_test.go b/chainntnfs/btcdnotify/btcd_test.go index e5954f25..7302171c 100644 --- a/chainntnfs/btcdnotify/btcd_test.go +++ b/chainntnfs/btcdnotify/btcd_test.go @@ -9,6 +9,7 @@ import ( "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/integration/rpctest" + "github.com/lightningnetwork/lnd/blockcache" "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" ) @@ -53,9 +54,12 @@ func initHintCache(t *testing.T) *chainntnfs.HeightHintCache { // driver. func setUpNotifier(t *testing.T, h *rpctest.Harness) *BtcdNotifier { hintCache := initHintCache(t) + blockCache := blockcache.NewBlockCache(10000) rpcCfg := h.RPCConfig() - notifier, err := New(&rpcCfg, chainntnfs.NetParams, hintCache, hintCache) + notifier, err := New( + &rpcCfg, chainntnfs.NetParams, hintCache, hintCache, blockCache, + ) if err != nil { t.Fatalf("unable to create notifier: %v", err) } diff --git a/chainntnfs/btcdnotify/driver.go b/chainntnfs/btcdnotify/driver.go index 901426f2..067b48cf 100644 --- a/chainntnfs/btcdnotify/driver.go +++ b/chainntnfs/btcdnotify/driver.go @@ -6,15 +6,16 @@ import ( "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/rpcclient" + "github.com/lightningnetwork/lnd/blockcache" "github.com/lightningnetwork/lnd/chainntnfs" ) // createNewNotifier creates a new instance of the ChainNotifier interface // implemented by BtcdNotifier. func createNewNotifier(args ...interface{}) (chainntnfs.ChainNotifier, error) { - if len(args) != 4 { + if len(args) != 5 { return nil, fmt.Errorf("incorrect number of arguments to "+ - ".New(...), expected 4, instead passed %v", len(args)) + ".New(...), expected 5, instead passed %v", len(args)) } config, ok := args[0].(*rpcclient.ConnConfig) @@ -41,7 +42,15 @@ func createNewNotifier(args ...interface{}) (chainntnfs.ChainNotifier, error) { "is incorrect, expected a chainntnfs.ConfirmHintCache") } - return New(config, chainParams, spendHintCache, confirmHintCache) + blockCache, ok := args[4].(*blockcache.BlockCache) + if !ok { + return nil, errors.New("fifth argument to btcdnotify.New " + + "is incorrect, expected a *blockcache.BlockCache") + } + + return New( + config, chainParams, spendHintCache, confirmHintCache, blockCache, + ) } // init registers a driver for the BtcdNotifier concrete implementation of the diff --git a/chainntnfs/test/test_interface.go b/chainntnfs/test/test_interface.go index 4b5c69f3..bfa4d23c 100644 --- a/chainntnfs/test/test_interface.go +++ b/chainntnfs/test/test_interface.go @@ -1955,7 +1955,7 @@ func TestInterfaces(t *testing.T, targetBackEnd string) { newNotifier = func() (chainntnfs.TestChainNotifier, error) { return btcdnotify.New( &rpcConfig, chainntnfs.NetParams, - hintCache, hintCache, + hintCache, hintCache, blockCache, ) } diff --git a/chainreg/chainregistry.go b/chainreg/chainregistry.go index 28f7e386..db2d4b92 100644 --- a/chainreg/chainregistry.go +++ b/chainreg/chainregistry.go @@ -548,7 +548,8 @@ func NewChainControl(cfg *Config) (*ChainControl, func(), error) { DisableAutoReconnect: false, } cc.ChainNotifier, err = btcdnotify.New( - rpcConfig, cfg.ActiveNetParams.Params, hintCache, hintCache, + rpcConfig, cfg.ActiveNetParams.Params, hintCache, + hintCache, blockCache, ) if err != nil { return nil, nil, err diff --git a/lnwallet/test/test_interface.go b/lnwallet/test/test_interface.go index cd01232e..1b7e7e48 100644 --- a/lnwallet/test/test_interface.go +++ b/lnwallet/test/test_interface.go @@ -3205,8 +3205,9 @@ func TestLightningWallet(t *testing.T, targetBackEnd string) { if err != nil { t.Fatalf("unable to create height hint cache: %v", err) } + blockCache := blockcache.NewBlockCache(10000) chainNotifier, err := btcdnotify.New( - &rpcConfig, netParams, hintCache, hintCache, + &rpcConfig, netParams, hintCache, hintCache, blockCache, ) if err != nil { t.Fatalf("unable to create notifier: %v", err)