From f470946379fb3480e2f97b571d81de94ad24c8bd Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Thu, 18 Mar 2021 14:30:58 +0200 Subject: [PATCH] routing: add block cache to BtcdFilteredChainView This commit makes the block cache available to BtcdFilteredChainView and wraps its GetBlock method so that the block cache is used. --- chainreg/chainregistry.go | 4 +++- routing/chainview/btcd.go | 19 +++++++++++++++++-- routing/chainview/interface_test.go | 5 ++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/chainreg/chainregistry.go b/chainreg/chainregistry.go index e81258fd..dda18a80 100644 --- a/chainreg/chainregistry.go +++ b/chainreg/chainregistry.go @@ -555,7 +555,9 @@ func NewChainControl(cfg *Config) (*ChainControl, func(), error) { // Finally, we'll create an instance of the default chain view to be // used within the routing layer. - cc.ChainView, err = chainview.NewBtcdFilteredChainView(*rpcConfig) + cc.ChainView, err = chainview.NewBtcdFilteredChainView( + *rpcConfig, blockCache, + ) if err != nil { log.Errorf("unable to create chain view: %v", err) return nil, nil, err diff --git a/routing/chainview/btcd.go b/routing/chainview/btcd.go index c4d9c022..4e5a95b5 100644 --- a/routing/chainview/btcd.go +++ b/routing/chainview/btcd.go @@ -12,6 +12,7 @@ import ( "github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" + "github.com/lightningnetwork/lnd/blockcache" "github.com/lightningnetwork/lnd/channeldb" ) @@ -35,6 +36,9 @@ type BtcdFilteredChainView 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 @@ -58,11 +62,14 @@ var _ FilteredChainView = (*BtcdFilteredChainView)(nil) // NewBtcdFilteredChainView creates a new instance of a FilteredChainView from // RPC credentials for an active btcd instance. -func NewBtcdFilteredChainView(config rpcclient.ConnConfig) (*BtcdFilteredChainView, error) { +func NewBtcdFilteredChainView(config rpcclient.ConnConfig, + blockCache *blockcache.BlockCache) (*BtcdFilteredChainView, error) { + chainView := &BtcdFilteredChainView{ chainFilter: make(map[wire.OutPoint]struct{}), filterUpdates: make(chan filterUpdate), filterBlockReqs: make(chan *filterBlockReq), + blockCache: blockCache, quit: make(chan struct{}), } @@ -404,7 +411,7 @@ func (b *BtcdFilteredChainView) chainFilterer() { case req := <-b.filterBlockReqs: // First we'll fetch the block itself as well as some // additional information including its height. - block, err := b.btcdConn.GetBlock(req.blockHash) + block, err := b.GetBlock(req.blockHash) if err != nil { req.err <- err req.resp <- nil @@ -486,3 +493,11 @@ func (b *BtcdFilteredChainView) FilteredBlocks() <-chan *FilteredBlock { func (b *BtcdFilteredChainView) 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 *BtcdFilteredChainView) GetBlock(hash *chainhash.Hash) ( + *wire.MsgBlock, error) { + + return b.blockCache.GetBlock(hash, b.btcdConn.GetBlock) +} diff --git a/routing/chainview/interface_test.go b/routing/chainview/interface_test.go index 875c0601..439b8d7f 100644 --- a/routing/chainview/interface_test.go +++ b/routing/chainview/interface_test.go @@ -906,7 +906,10 @@ var interfaceImpls = []struct { { name: "btcd_websockets", chainViewInit: func(config rpcclient.ConnConfig, _ string) (func(), FilteredChainView, error) { - chainView, err := NewBtcdFilteredChainView(config) + blockCache := blockcache.NewBlockCache(10000) + chainView, err := NewBtcdFilteredChainView( + config, blockCache, + ) if err != nil { return nil, nil, err }