lnwallet: expose optional account filter for several WalletController methods

This commit is contained in:
Wilmer Paulino committed 2021-04-05 15:41:06 -07:00
1 parent f38bf4d7fa
commit f91e7cde59
14 files changed
+138 -76

No files matched your search

+34 -15
View File
@@ -223,13 +223,19 @@ func (b *BtcWallet) Stop() error {
// ConfirmedBalance returns the sum of all the wallet's unspent outputs that
// have at least confs confirmations. If confs is set to zero, then all unspent
// outputs, including those currently in the mempool will be included in the
// final sum.
// final sum. The account parameter serves as a filter to retrieve the balance
// for a specific account. When empty, the confirmed balance of all wallet
// accounts is returned.
//
// This is a part of the WalletController interface.
func (b *BtcWallet) ConfirmedBalance(confs int32) (btcutil.Amount, error) {
func (b *BtcWallet) ConfirmedBalance(confs int32,
accountFilter string) (btcutil.Amount, error) {
var balance btcutil.Amount
witnessOutputs, err := b.ListUnspentWitness(confs, math.MaxInt32)
witnessOutputs, err := b.ListUnspentWitness(
confs, math.MaxInt32, accountFilter,
)
if err != nil {
return 0, err
}
@@ -463,16 +469,25 @@ func (b *BtcWallet) ReleaseOutput(id wtxmgr.LockID, op wire.OutPoint) error {
return b.wallet.ReleaseOutput(id, op)
}
// ListUnspentWitness returns a slice of all the unspent outputs the wallet
// controls which pay to witness programs either directly or indirectly.
// ListUnspentWitness returns all unspent outputs which are version 0 witness
// programs. The 'minconfirms' and 'maxconfirms' parameters indicate the minimum
// and maximum number of confirmations an output needs in order to be returned
// by this method. Passing -1 as 'minconfirms' indicates that even unconfirmed
// outputs should be returned. Using MaxInt32 as 'maxconfirms' implies returning
// all outputs with at least 'minconfirms'. The account parameter serves as a
// filter to retrieve the unspent outputs for a specific account. When empty,
// the unspent outputs of all wallet accounts are returned.
//
// NOTE: This method requires the global coin selection lock to be held.
//
// This is a part of the WalletController interface.
func (b *BtcWallet) ListUnspentWitness(minConfs, maxConfs int32) (
[]*lnwallet.Utxo, error) {
func (b *BtcWallet) ListUnspentWitness(minConfs, maxConfs int32,
accountFilter string) ([]*lnwallet.Utxo, error) {
// First, grab all the unfiltered currently unspent outputs.
unspentOutputs, err := b.wallet.ListUnspent(minConfs, maxConfs, "")
unspentOutputs, err := b.wallet.ListUnspent(
minConfs, maxConfs, accountFilter,
)
if err != nil {
return nil, err
}
@@ -690,14 +705,18 @@ func unminedTransactionsToDetail(
return txDetail, nil
}
// ListTransactionDetails returns a list of all transactions which are
// relevant to the wallet. It takes inclusive start and end height to allow
// paginated queries. Unconfirmed transactions can be included in the query
// by providing endHeight = UnconfirmedHeight (= -1).
// ListTransactionDetails returns a list of all transactions which are relevant
// to the wallet over [startHeight;endHeight]. If start height is greater than
// end height, the transactions will be retrieved in reverse order. To include
// unconfirmed transactions, endHeight should be set to the special value -1.
// This will return transactions from the tip of the chain until the start
// height (inclusive) and unconfirmed transactions. The account parameter serves
// as a filter to retrieve the transactions relevant to a specific account. When
// empty, transactions of all wallet accounts are returned.
//
// This is a part of the WalletController interface.
func (b *BtcWallet) ListTransactionDetails(startHeight,
endHeight int32) ([]*lnwallet.TransactionDetail, error) {
func (b *BtcWallet) ListTransactionDetails(startHeight, endHeight int32,
accountFilter string) ([]*lnwallet.TransactionDetail, error) {
// Grab the best block the wallet knows of, we'll use this to calculate
// # of confirmations shortly below.
@@ -707,7 +726,7 @@ func (b *BtcWallet) ListTransactionDetails(startHeight,
// We'll attempt to find all transactions from start to end height.
start := base.NewBlockIdentifierFromHeight(startHeight)
stop := base.NewBlockIdentifierFromHeight(endHeight)
txns, err := b.wallet.GetTransactions(start, stop, "", nil)
txns, err := b.wallet.GetTransactions(start, stop, accountFilter, nil)
if err != nil {
return nil, err
}
+14 -7
View File
@@ -159,12 +159,14 @@ type WalletController interface {
// ConfirmedBalance returns the sum of all the wallet's unspent outputs
// that have at least confs confirmations. If confs is set to zero,
// then all unspent outputs, including those currently in the mempool
// will be included in the final sum.
// will be included in the final sum. The account parameter serves as a
// filter to retrieve the balance for a specific account. When empty,
// the confirmed balance of all wallet accounts is returned.
//
// NOTE: Only witness outputs should be included in the computation of
// the total spendable balance of the wallet. We require this as only
// witness inputs can be used for funding channels.
ConfirmedBalance(confs int32) (btcutil.Amount, error)
ConfirmedBalance(confs int32, accountFilter string) (btcutil.Amount, error)
// NewAddress returns the next external or internal address for the
// wallet dictated by the value of the `change` parameter. If change is
@@ -221,10 +223,13 @@ type WalletController interface {
// needs in order to be returned by this method. Passing -1 as
// 'minconfirms' indicates that even unconfirmed outputs should be
// returned. Using MaxInt32 as 'maxconfirms' implies returning all
// outputs with at least 'minconfirms'.
// outputs with at least 'minconfirms'. The account parameter serves as
// a filter to retrieve the unspent outputs for a specific account.
// When empty, the unspent outputs of all wallet accounts are returned.
//
// NOTE: This method requires the global coin selection lock to be held.
ListUnspentWitness(minconfirms, maxconfirms int32) ([]*Utxo, error)
ListUnspentWitness(minconfirms, maxconfirms int32,
accountFilter string) ([]*Utxo, error)
// ListTransactionDetails returns a list of all transactions which are
// relevant to the wallet over [startHeight;endHeight]. If start height
@@ -232,9 +237,11 @@ type WalletController interface {
// reverse order. To include unconfirmed transactions, endHeight should
// be set to the special value -1. This will return transactions from
// the tip of the chain until the start height (inclusive) and
// unconfirmed transactions.
ListTransactionDetails(startHeight,
endHeight int32) ([]*TransactionDetail, error)
// unconfirmed transactions. The account parameter serves as a filter to
// retrieve the transactions relevant to a specific account. When
// empty, transactions of all wallet accounts are returned.
ListTransactionDetails(startHeight, endHeight int32,
accountFilter string) ([]*TransactionDetail, error)
// LockOutpoint marks an outpoint as locked meaning it will no longer
// be deemed as eligible for coin selection. Locking outputs are
+14 -18
View File
@@ -101,7 +101,7 @@ var (
func assertProperBalance(t *testing.T, lw *lnwallet.LightningWallet,
numConfirms int32, amount float64) {
balance, err := lw.ConfirmedBalance(numConfirms)
balance, err := lw.ConfirmedBalance(numConfirms, lnwallet.DefaultAccountName)
if err != nil {
t.Fatalf("unable to query for balance: %v", err)
}
@@ -208,7 +208,7 @@ func assertTxInWallet(t *testing.T, w *lnwallet.LightningWallet,
// We'll fetch all of our transaction and go through each one until
// finding the expected transaction with its expected confirmation
// status.
txs, err := w.ListTransactionDetails(0, btcwallet.UnconfirmedHeight)
txs, err := w.ListTransactionDetails(0, btcwallet.UnconfirmedHeight, "")
if err != nil {
t.Fatalf("unable to retrieve transactions: %v", err)
}
@@ -248,7 +248,7 @@ func loadTestCredits(miner *rpctest.Harness, w *lnwallet.LightningWallet,
if err != nil {
return fmt.Errorf("unable to create amt: %v", err)
}
expectedBalance, err := w.ConfirmedBalance(1)
expectedBalance, err := w.ConfirmedBalance(1, lnwallet.DefaultAccountName)
if err != nil {
return err
}
@@ -294,7 +294,7 @@ func loadTestCredits(miner *rpctest.Harness, w *lnwallet.LightningWallet,
timeout := time.After(30 * time.Second)
for range ticker.C {
balance, err := w.ConfirmedBalance(1)
balance, err := w.ConfirmedBalance(1, lnwallet.DefaultAccountName)
if err != nil {
return err
}
@@ -1191,7 +1191,7 @@ func testListTransactionDetails(miner *rpctest.Harness,
t.Fatalf("Couldn't sync Alice's wallet: %v", err)
}
txDetails, err := alice.ListTransactionDetails(
startHeight, chainTip,
startHeight, chainTip, "",
)
if err != nil {
t.Fatalf("unable to fetch tx details: %v", err)
@@ -1305,7 +1305,7 @@ func testListTransactionDetails(miner *rpctest.Harness,
// with a confirmation height of 0, indicating that it has not been
// mined yet.
txDetails, err = alice.ListTransactionDetails(
chainTip, btcwallet.UnconfirmedHeight,
chainTip, btcwallet.UnconfirmedHeight, "",
)
if err != nil {
t.Fatalf("unable to fetch tx details: %v", err)
@@ -1361,9 +1361,7 @@ func testListTransactionDetails(miner *rpctest.Harness,
if err != nil {
t.Fatalf("Couldn't sync Alice's wallet: %v", err)
}
txDetails, err = alice.ListTransactionDetails(
chainTip, chainTip,
)
txDetails, err = alice.ListTransactionDetails(chainTip, chainTip, "")
if err != nil {
t.Fatalf("unable to fetch tx details: %v", err)
}
@@ -1410,9 +1408,7 @@ func testListTransactionDetails(miner *rpctest.Harness,
// Query for transactions only in the latest block. We do not expect
// any transactions to be returned.
txDetails, err = alice.ListTransactionDetails(
chainTip, chainTip,
)
txDetails, err = alice.ListTransactionDetails(chainTip, chainTip, "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -2189,7 +2185,7 @@ func testReorgWalletBalance(r *rpctest.Harness, w *lnwallet.LightningWallet,
}
// Get the original balance.
origBalance, err := w.ConfirmedBalance(1)
origBalance, err := w.ConfirmedBalance(1, lnwallet.DefaultAccountName)
if err != nil {
t.Fatalf("unable to query for balance: %v", err)
}
@@ -2205,7 +2201,7 @@ func testReorgWalletBalance(r *rpctest.Harness, w *lnwallet.LightningWallet,
t.Fatalf("unable to set up mining node: %v", err)
}
defer r2.TearDown()
newBalance, err := w.ConfirmedBalance(1)
newBalance, err := w.ConfirmedBalance(1, lnwallet.DefaultAccountName)
if err != nil {
t.Fatalf("unable to query for balance: %v", err)
}
@@ -2297,7 +2293,7 @@ func testReorgWalletBalance(r *rpctest.Harness, w *lnwallet.LightningWallet,
}
// Now we check that the wallet balance stays the same.
newBalance, err = w.ConfirmedBalance(1)
newBalance, err = w.ConfirmedBalance(1, lnwallet.DefaultAccountName)
if err != nil {
t.Fatalf("unable to query for balance: %v", err)
}
@@ -2320,7 +2316,7 @@ func testChangeOutputSpendConfirmation(r *rpctest.Harness,
// Assuming a balance of 80 BTC and a transaction fee of 2500 sat/kw,
// we'll craft the following transaction so that Alice doesn't have any
// UTXOs left.
aliceBalance, err := alice.ConfirmedBalance(0)
aliceBalance, err := alice.ConfirmedBalance(0, lnwallet.DefaultAccountName)
if err != nil {
t.Fatalf("unable to retrieve alice's balance: %v", err)
}
@@ -2345,7 +2341,7 @@ func testChangeOutputSpendConfirmation(r *rpctest.Harness,
// With the transaction sent and confirmed, Alice's balance should now
// be 0.
aliceBalance, err = alice.ConfirmedBalance(0)
aliceBalance, err = alice.ConfirmedBalance(0, lnwallet.DefaultAccountName)
if err != nil {
t.Fatalf("unable to retrieve alice's balance: %v", err)
}
@@ -2401,7 +2397,7 @@ func testSpendUnconfirmed(miner *rpctest.Harness,
// First we will empty out bob's wallet, sending the entire balance
// to alice.
bobBalance, err := bob.ConfirmedBalance(0)
bobBalance, err := bob.ConfirmedBalance(0, lnwallet.DefaultAccountName)
if err != nil {
t.Fatalf("unable to retrieve bob's balance: %v", err)
}
+33 -8
View File
@@ -396,14 +396,33 @@ func (l *LightningWallet) Shutdown() error {
return nil
}
// ConfirmedBalance returns the current confirmed balance of the wallet. This
// methods wraps the interal WalletController method so we're able to properly
// hold the coin select mutex while we compute the balance.
func (l *LightningWallet) ConfirmedBalance(confs int32) (btcutil.Amount, error) {
// ConfirmedBalance returns the current confirmed balance of a wallet account.
// This methods wraps the internal WalletController method so we're able to
// properly hold the coin select mutex while we compute the balance.
func (l *LightningWallet) ConfirmedBalance(confs int32,
account string) (btcutil.Amount, error) {
l.coinSelectMtx.Lock()
defer l.coinSelectMtx.Unlock()
return l.WalletController.ConfirmedBalance(confs)
return l.WalletController.ConfirmedBalance(confs, account)
}
// ListUnspentWitnessFromDefaultAccount returns all unspent outputs from the
// default wallet account which are version 0 witness programs. The 'minConfs'
// and 'maxConfs' parameters indicate the minimum and maximum number of
// confirmations an output needs in order to be returned by this method. Passing
// -1 as 'minConfs' indicates that even unconfirmed outputs should be returned.
// Using MaxInt32 as 'maxConfs' implies returning all outputs with at least
// 'minConfs'.
//
// NOTE: This method requires the global coin selection lock to be held.
func (l *LightningWallet) ListUnspentWitnessFromDefaultAccount(
minConfs, maxConfs int32) ([]*Utxo, error) {
return l.WalletController.ListUnspentWitness(
minConfs, maxConfs, DefaultAccountName,
)
}
// LockedOutpoints returns a list of all currently locked outpoint.
@@ -922,8 +941,12 @@ func (l *LightningWallet) currentNumAnchorChans() (int, error) {
func (l *LightningWallet) CheckReservedValue(in []wire.OutPoint,
out []*wire.TxOut, numAnchorChans int) (btcutil.Amount, error) {
// Get all unspent coins in the wallet.
witnessOutputs, err := l.ListUnspentWitness(0, math.MaxInt32)
// Get all unspent coins in the wallet. We only care about those part of
// the wallet's default account as we know we can readily sign for those
// at any time.
witnessOutputs, err := l.ListUnspentWitnessFromDefaultAccount(
0, math.MaxInt32,
)
if err != nil {
return 0, err
}
@@ -2022,7 +2045,9 @@ func NewCoinSource(w *LightningWallet) *CoinSource {
func (c *CoinSource) ListCoins(minConfs int32,
maxConfs int32) ([]chanfunding.Coin, error) {
utxos, err := c.wallet.ListUnspentWitness(minConfs, maxConfs)
utxos, err := c.wallet.ListUnspentWitnessFromDefaultAccount(
minConfs, maxConfs,
)
if err != nil {
return nil, err
}