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.
This commit is contained in:
Elle Mouton 2021-03-18 14:54:36 +02:00
parent 8a33fbf11a
commit a0f7bf8b2d
6 changed files with 40 additions and 10 deletions

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

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

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

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

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

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