btcwallet: make blockcache available to BtcWallet

This commit makes the blockcache available to BtcWallet so that any
GetBlock call made to BtcWallet is wrapped by the blockcache GetBlock
call.
This commit is contained in:
Elle Mouton committed 2021-04-28 09:46:11 +02:00
1 parent 6702c79216
commit 106f93a1b4
5 files changed
+30 -8

No files matched your search

+1 -1
View File
@@ -131,7 +131,7 @@ func (b *BtcWallet) GetUtxo(op *wire.OutPoint, pkScript []byte,
//
// This method is a part of the lnwallet.BlockChainIO interface.
func (b *BtcWallet) GetBlock(blockHash *chainhash.Hash) (*wire.MsgBlock, error) {
return b.chain.GetBlock(blockHash)
return b.blockCache.GetBlock(blockHash, b.chain.GetBlock)
}
// GetBlockHash returns the hash of the block in the best blockchain at the
+5 -1
View File
@@ -24,6 +24,7 @@ import (
"github.com/btcsuite/btcwallet/wallet/txrules"
"github.com/btcsuite/btcwallet/walletdb"
"github.com/btcsuite/btcwallet/wtxmgr"
"github.com/lightningnetwork/lnd/blockcache"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
@@ -74,6 +75,8 @@ type BtcWallet struct {
netParams *chaincfg.Params
chainKeyScope waddrmgr.KeyScope
blockCache *blockcache.BlockCache
}
// A compile time check to ensure that BtcWallet implements the
@@ -83,7 +86,7 @@ var _ lnwallet.BlockChainIO = (*BtcWallet)(nil)
// New returns a new fully initialized instance of BtcWallet given a valid
// configuration struct.
func New(cfg Config) (*BtcWallet, error) {
func New(cfg Config, blockCache *blockcache.BlockCache) (*BtcWallet, error) {
// Ensure the wallet exists or create it when the create flag is set.
netDir := NetworkDir(cfg.DataDir, cfg.NetParams)
@@ -142,6 +145,7 @@ func New(cfg Config) (*BtcWallet, error) {
chain: cfg.ChainSource,
netParams: cfg.NetParams,
chainKeyScope: chainKeyScope,
blockCache: blockCache,
}, nil
}
+10 -3
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"github.com/btcsuite/btcwallet/chain"
"github.com/lightningnetwork/lnd/blockcache"
"github.com/lightningnetwork/lnd/lnwallet"
)
@@ -16,9 +17,9 @@ const (
// properly create an instance of the lnwallet.WalletDriver struct for
// BtcWallet.
func createNewWallet(args ...interface{}) (lnwallet.WalletController, error) {
if len(args) != 1 {
if len(args) != 2 {
return nil, fmt.Errorf("incorrect number of arguments to .New(...), "+
"expected 1, instead passed %v", len(args))
"expected 2, instead passed %v", len(args))
}
config, ok := args[0].(*Config)
@@ -27,7 +28,13 @@ func createNewWallet(args ...interface{}) (lnwallet.WalletController, error) {
"incorrect, expected a *rpcclient.ConnConfig")
}
return New(*config)
blockCache, ok := args[1].(*blockcache.BlockCache)
if !ok {
return nil, fmt.Errorf("second argument to btcdnotifier.New is " +
"incorrect, expected a *blockcache.BlockCache")
}
return New(*config, blockCache)
}
// init registers a driver for the BtcWallet concrete implementation of the
+9 -2
View File
@@ -32,6 +32,7 @@ import (
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
"github.com/davecgh/go-spew/spew"
"github.com/lightninglabs/neutrino"
"github.com/lightningnetwork/lnd/blockcache"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/chainntnfs/btcdnotify"
"github.com/lightningnetwork/lnd/channeldb"
@@ -3262,6 +3263,8 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver,
}
defer os.RemoveAll(tempTestDirBob)
blockCache := blockcache.NewBlockCache(10000)
walletType := walletDriver.WalletType
switch walletType {
case "btcwallet":
@@ -3430,7 +3433,9 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver,
// wallet starts in recovery mode
RecoveryWindow: 2,
}
aliceWalletController, err = walletDriver.New(aliceWalletConfig)
aliceWalletController, err = walletDriver.New(
aliceWalletConfig, blockCache,
)
if err != nil {
t.Fatalf("unable to create btcwallet: %v", err)
}
@@ -3455,7 +3460,9 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver,
// wallet starts without recovery mode
RecoveryWindow: 0,
}
bobWalletController, err = walletDriver.New(bobWalletConfig)
bobWalletController, err = walletDriver.New(
bobWalletConfig, blockCache,
)
if err != nil {
t.Fatalf("unable to create btcwallet: %v", err)
}