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.
This commit is contained in:
Elle Mouton 2021-03-18 14:24:06 +02:00
parent 106f93a1b4
commit 0193669ed8
3 changed files with 25 additions and 4 deletions

@ -418,7 +418,9 @@ func NewChainControl(cfg *Config) (*ChainControl, func(), error) {
cc.ChainNotifier = bitcoindnotify.New( cc.ChainNotifier = bitcoindnotify.New(
bitcoindConn, cfg.ActiveNetParams.Params, hintCache, hintCache, bitcoindConn, cfg.ActiveNetParams.Params, hintCache, hintCache,
) )
cc.ChainView = chainview.NewBitcoindFilteredChainView(bitcoindConn) cc.ChainView = chainview.NewBitcoindFilteredChainView(
bitcoindConn, blockCache,
)
walletConfig.ChainSource = bitcoindConn.NewBitcoindClient() walletConfig.ChainSource = bitcoindConn.NewBitcoindClient()
// If we're not in regtest mode, then we'll attempt to use a // If we're not in regtest mode, then we'll attempt to use a

@ -12,6 +12,7 @@ import (
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/chain" "github.com/btcsuite/btcwallet/chain"
"github.com/btcsuite/btcwallet/wtxmgr" "github.com/btcsuite/btcwallet/wtxmgr"
"github.com/lightningnetwork/lnd/blockcache"
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
) )
@ -37,6 +38,9 @@ type BitcoindFilteredChainView struct {
// chainView. // chainView.
blockQueue *blockEventQueue blockQueue *blockEventQueue
// blockCache is an LRU block cache.
blockCache *blockcache.BlockCache
// filterUpdates is a channel in which updates to the utxo filter // filterUpdates is a channel in which updates to the utxo filter
// attached to this instance are sent over. // attached to this instance are sent over.
filterUpdates chan filterUpdate filterUpdates chan filterUpdate
@ -61,12 +65,14 @@ var _ FilteredChainView = (*BitcoindFilteredChainView)(nil)
// NewBitcoindFilteredChainView creates a new instance of a FilteredChainView // NewBitcoindFilteredChainView creates a new instance of a FilteredChainView
// from RPC credentials and a ZMQ socket address for a bitcoind instance. // from RPC credentials and a ZMQ socket address for a bitcoind instance.
func NewBitcoindFilteredChainView( func NewBitcoindFilteredChainView(
chainConn *chain.BitcoindConn) *BitcoindFilteredChainView { chainConn *chain.BitcoindConn,
blockCache *blockcache.BlockCache) *BitcoindFilteredChainView {
chainView := &BitcoindFilteredChainView{ chainView := &BitcoindFilteredChainView{
chainFilter: make(map[wire.OutPoint]struct{}), chainFilter: make(map[wire.OutPoint]struct{}),
filterUpdates: make(chan filterUpdate), filterUpdates: make(chan filterUpdate),
filterBlockReqs: make(chan *filterBlockReq), filterBlockReqs: make(chan *filterBlockReq),
blockCache: blockCache,
quit: make(chan struct{}), quit: make(chan struct{}),
} }
@ -390,7 +396,7 @@ func (b *BitcoindFilteredChainView) chainFilterer() {
case req := <-b.filterBlockReqs: case req := <-b.filterBlockReqs:
// First we'll fetch the block itself as well as some // First we'll fetch the block itself as well as some
// additional information including its height. // additional information including its height.
block, err := b.chainClient.GetBlock(req.blockHash) block, err := b.GetBlock(req.blockHash)
if err != nil { if err != nil {
req.err <- err req.err <- err
req.resp <- nil req.resp <- nil
@ -479,3 +485,11 @@ func (b *BitcoindFilteredChainView) FilteredBlocks() <-chan *FilteredBlock {
func (b *BitcoindFilteredChainView) DisconnectedBlocks() <-chan *FilteredBlock { func (b *BitcoindFilteredChainView) DisconnectedBlocks() <-chan *FilteredBlock {
return b.blockQueue.staleBlocks 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)
}

@ -26,6 +26,7 @@ import (
_ "github.com/btcsuite/btcwallet/walletdb/bdb" // Required to register the boltdb walletdb implementation. _ "github.com/btcsuite/btcwallet/walletdb/bdb" // Required to register the boltdb walletdb implementation.
"github.com/lightninglabs/neutrino" "github.com/lightninglabs/neutrino"
"github.com/lightningnetwork/lnd/blockcache"
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/kvdb" "github.com/lightningnetwork/lnd/channeldb/kvdb"
) )
@ -844,7 +845,11 @@ var interfaceImpls = []struct {
cleanUp2() cleanUp2()
} }
chainView := NewBitcoindFilteredChainView(chainConn) blockCache := blockcache.NewBlockCache(10000)
chainView := NewBitcoindFilteredChainView(
chainConn, blockCache,
)
return cleanUp3, chainView, nil return cleanUp3, chainView, nil
}, },