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 2021-03-18 14:08:53 +02:00
parent 6702c79216
commit 106f93a1b4
5 changed files with 30 additions and 8 deletions

@ -19,6 +19,7 @@ import (
"github.com/btcsuite/btcwallet/chain" "github.com/btcsuite/btcwallet/chain"
"github.com/btcsuite/btcwallet/wallet" "github.com/btcsuite/btcwallet/wallet"
"github.com/lightninglabs/neutrino" "github.com/lightninglabs/neutrino"
"github.com/lightningnetwork/lnd/blockcache"
"github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/chainntnfs/bitcoindnotify" "github.com/lightningnetwork/lnd/chainntnfs/bitcoindnotify"
"github.com/lightningnetwork/lnd/chainntnfs/btcdnotify" "github.com/lightningnetwork/lnd/chainntnfs/btcdnotify"
@ -306,6 +307,9 @@ func NewChainControl(cfg *Config) (*ChainControl, func(), error) {
"cache: %v", err) "cache: %v", err)
} }
// Initialize a new block cache.
blockCache := blockcache.NewBlockCache(cfg.BlockCacheSize)
// If spv mode is active, then we'll be using a distinct set of // If spv mode is active, then we'll be using a distinct set of
// chainControl interfaces that interface directly with the p2p network // chainControl interfaces that interface directly with the p2p network
// of the selected chain. // of the selected chain.
@ -641,7 +645,7 @@ func NewChainControl(cfg *Config) (*ChainControl, func(), error) {
return nil, nil, err return nil, nil, err
} }
wc, err := btcwallet.New(*walletConfig) wc, err := btcwallet.New(*walletConfig, blockCache)
if err != nil { if err != nil {
fmt.Printf("unable to create wallet controller: %v\n", err) fmt.Printf("unable to create wallet controller: %v\n", err)
return nil, ccCleanup, err return nil, ccCleanup, err

@ -131,7 +131,7 @@ func (b *BtcWallet) GetUtxo(op *wire.OutPoint, pkScript []byte,
// //
// This method is a part of the lnwallet.BlockChainIO interface. // This method is a part of the lnwallet.BlockChainIO interface.
func (b *BtcWallet) GetBlock(blockHash *chainhash.Hash) (*wire.MsgBlock, error) { 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 // GetBlockHash returns the hash of the block in the best blockchain at the

@ -24,6 +24,7 @@ import (
"github.com/btcsuite/btcwallet/wallet/txrules" "github.com/btcsuite/btcwallet/wallet/txrules"
"github.com/btcsuite/btcwallet/walletdb" "github.com/btcsuite/btcwallet/walletdb"
"github.com/btcsuite/btcwallet/wtxmgr" "github.com/btcsuite/btcwallet/wtxmgr"
"github.com/lightningnetwork/lnd/blockcache"
"github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwallet/chainfee" "github.com/lightningnetwork/lnd/lnwallet/chainfee"
@ -74,6 +75,8 @@ type BtcWallet struct {
netParams *chaincfg.Params netParams *chaincfg.Params
chainKeyScope waddrmgr.KeyScope chainKeyScope waddrmgr.KeyScope
blockCache *blockcache.BlockCache
} }
// A compile time check to ensure that BtcWallet implements the // 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 // New returns a new fully initialized instance of BtcWallet given a valid
// configuration struct. // 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. // Ensure the wallet exists or create it when the create flag is set.
netDir := NetworkDir(cfg.DataDir, cfg.NetParams) netDir := NetworkDir(cfg.DataDir, cfg.NetParams)
@ -142,6 +145,7 @@ func New(cfg Config) (*BtcWallet, error) {
chain: cfg.ChainSource, chain: cfg.ChainSource,
netParams: cfg.NetParams, netParams: cfg.NetParams,
chainKeyScope: chainKeyScope, chainKeyScope: chainKeyScope,
blockCache: blockCache,
}, nil }, nil
} }

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/btcsuite/btcwallet/chain" "github.com/btcsuite/btcwallet/chain"
"github.com/lightningnetwork/lnd/blockcache"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
) )
@ -16,9 +17,9 @@ const (
// properly create an instance of the lnwallet.WalletDriver struct for // properly create an instance of the lnwallet.WalletDriver struct for
// BtcWallet. // BtcWallet.
func createNewWallet(args ...interface{}) (lnwallet.WalletController, error) { 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(...), "+ 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) config, ok := args[0].(*Config)
@ -27,7 +28,13 @@ func createNewWallet(args ...interface{}) (lnwallet.WalletController, error) {
"incorrect, expected a *rpcclient.ConnConfig") "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 // init registers a driver for the BtcWallet concrete implementation of the

@ -32,6 +32,7 @@ import (
_ "github.com/btcsuite/btcwallet/walletdb/bdb" _ "github.com/btcsuite/btcwallet/walletdb/bdb"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/lightninglabs/neutrino" "github.com/lightninglabs/neutrino"
"github.com/lightningnetwork/lnd/blockcache"
"github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/chainntnfs/btcdnotify" "github.com/lightningnetwork/lnd/chainntnfs/btcdnotify"
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
@ -3262,6 +3263,8 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver,
} }
defer os.RemoveAll(tempTestDirBob) defer os.RemoveAll(tempTestDirBob)
blockCache := blockcache.NewBlockCache(10000)
walletType := walletDriver.WalletType walletType := walletDriver.WalletType
switch walletType { switch walletType {
case "btcwallet": case "btcwallet":
@ -3430,7 +3433,9 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver,
// wallet starts in recovery mode // wallet starts in recovery mode
RecoveryWindow: 2, RecoveryWindow: 2,
} }
aliceWalletController, err = walletDriver.New(aliceWalletConfig) aliceWalletController, err = walletDriver.New(
aliceWalletConfig, blockCache,
)
if err != nil { if err != nil {
t.Fatalf("unable to create btcwallet: %v", err) 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 // wallet starts without recovery mode
RecoveryWindow: 0, RecoveryWindow: 0,
} }
bobWalletController, err = walletDriver.New(bobWalletConfig) bobWalletController, err = walletDriver.New(
bobWalletConfig, blockCache,
)
if err != nil { if err != nil {
t.Fatalf("unable to create btcwallet: %v", err) t.Fatalf("unable to create btcwallet: %v", err)
} }