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

@ -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

@ -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)
}

@ -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
}