From 0193669ed8a959d9f38f9728083bc11de1f518aa Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Thu, 18 Mar 2021 14:24:06 +0200 Subject: [PATCH] routing: add block cache to BitcoindFilteredChainView This commit adds the block cache to the BitcoindFilteredChainView struct and wraps its GetBlock function so that block cache is used. --- chainreg/chainregistry.go | 4 +++- routing/chainview/bitcoind.go | 18 ++++++++++++++++-- routing/chainview/interface_test.go | 7 ++++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/chainreg/chainregistry.go b/chainreg/chainregistry.go index d3b84130..e81258fd 100644 --- a/chainreg/chainregistry.go +++ b/chainreg/chainregistry.go @@ -418,7 +418,9 @@ func NewChainControl(cfg *Config) (*ChainControl, func(), error) { cc.ChainNotifier = bitcoindnotify.New( bitcoindConn, cfg.ActiveNetParams.Params, hintCache, hintCache, ) - cc.ChainView = chainview.NewBitcoindFilteredChainView(bitcoindConn) + cc.ChainView = chainview.NewBitcoindFilteredChainView( + bitcoindConn, blockCache, + ) walletConfig.ChainSource = bitcoindConn.NewBitcoindClient() // If we're not in regtest mode, then we'll attempt to use a diff --git a/routing/chainview/bitcoind.go b/routing/chainview/bitcoind.go index d793dfb7..9a298edd 100644 --- a/routing/chainview/bitcoind.go +++ b/routing/chainview/bitcoind.go @@ -12,6 +12,7 @@ import ( "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcwallet/chain" "github.com/btcsuite/btcwallet/wtxmgr" + "github.com/lightningnetwork/lnd/blockcache" "github.com/lightningnetwork/lnd/channeldb" ) @@ -37,6 +38,9 @@ type BitcoindFilteredChainView struct { // chainView. blockQueue *blockEventQueue + // blockCache is an LRU block cache. + blockCache *blockcache.BlockCache + // filterUpdates is a channel in which updates to the utxo filter // attached to this instance are sent over. filterUpdates chan filterUpdate @@ -61,12 +65,14 @@ var _ FilteredChainView = (*BitcoindFilteredChainView)(nil) // NewBitcoindFilteredChainView creates a new instance of a FilteredChainView // from RPC credentials and a ZMQ socket address for a bitcoind instance. func NewBitcoindFilteredChainView( - chainConn *chain.BitcoindConn) *BitcoindFilteredChainView { + chainConn *chain.BitcoindConn, + blockCache *blockcache.BlockCache) *BitcoindFilteredChainView { chainView := &BitcoindFilteredChainView{ chainFilter: make(map[wire.OutPoint]struct{}), filterUpdates: make(chan filterUpdate), filterBlockReqs: make(chan *filterBlockReq), + blockCache: blockCache, quit: make(chan struct{}), } @@ -390,7 +396,7 @@ func (b *BitcoindFilteredChainView) chainFilterer() { case req := <-b.filterBlockReqs: // First we'll fetch the block itself as well as some // additional information including its height. - block, err := b.chainClient.GetBlock(req.blockHash) + block, err := b.GetBlock(req.blockHash) if err != nil { req.err <- err req.resp <- nil @@ -479,3 +485,11 @@ func (b *BitcoindFilteredChainView) FilteredBlocks() <-chan *FilteredBlock { func (b *BitcoindFilteredChainView) DisconnectedBlocks() <-chan *FilteredBlock { return b.blockQueue.staleBlocks } + +// GetBlock is used to retrieve the block with the given hash. This function +// wraps the blockCache's GetBlock function. +func (b *BitcoindFilteredChainView) GetBlock(hash *chainhash.Hash) ( + *wire.MsgBlock, error) { + + return b.blockCache.GetBlock(hash, b.chainClient.GetBlock) +} diff --git a/routing/chainview/interface_test.go b/routing/chainview/interface_test.go index 33975e0a..875c0601 100644 --- a/routing/chainview/interface_test.go +++ b/routing/chainview/interface_test.go @@ -26,6 +26,7 @@ import ( _ "github.com/btcsuite/btcwallet/walletdb/bdb" // Required to register the boltdb walletdb implementation. "github.com/lightninglabs/neutrino" + "github.com/lightningnetwork/lnd/blockcache" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb/kvdb" ) @@ -844,7 +845,11 @@ var interfaceImpls = []struct { cleanUp2() } - chainView := NewBitcoindFilteredChainView(chainConn) + blockCache := blockcache.NewBlockCache(10000) + + chainView := NewBitcoindFilteredChainView( + chainConn, blockCache, + ) return cleanUp3, chainView, nil },