sweep: remove all unconfirmed descendant transactions when a sweep conflicts

Before this commit, we we were trying to sweep an anchor output, and
that output was spent by someone else (not the sweeper), then we would
report this back to the original resolver (allowing it to be cleaned
up), and also remove the set of inputs spent by that transaction from
the set we need to sweep.

However, it's possible that if a user is spending unconfirmed outputs,
then the wallet is holding onto an invalid transaction, as the outputs
that were used as inputs have been double spent elsewhere.

In this commit, we fix this issue by recursively removing all descendant
transactions of our past sweeps that have an intersecting input set as
the spending transaction. In cases where a user spent an unconfirmed
output to funding a channel, and that output was a descendant of the now
swept anchor output, the funds will now properly be marked as available.

Fixes #6241
This commit is contained in:
Olaoluwa Osuntokun
2022-02-17 18:25:30 -08:00
parent 4ecd153be2
commit e1e9de24df
7 changed files with 185 additions and 7 deletions

View File

@@ -56,6 +56,10 @@ var (
// stored within the top-level waleltdb buckets of btcwallet.
waddrmgrNamespaceKey = []byte("waddrmgr")
// wtxmgrNamespaceKey is the namespace key that the wtxmgr state is
// stored within the top-level waleltdb buckets of btcwallet.
wtxmgrNamespaceKey = []byte("wtxmgr")
// lightningAddrSchema is the scope addr schema for all keys that we
// derive. We'll treat them all as p2wkh addresses, as atm we must
// specify a particular type.
@@ -1400,3 +1404,46 @@ func (b *BtcWallet) GetRecoveryInfo() (bool, float64, error) {
return isRecoveryMode, progress, nil
}
// FetchTx attempts to fetch a transaction in the wallet's database identified
// by the passed transaction hash. If the transaction can't be found, then a
// nil pointer is returned.
func (b *BtcWallet) FetchTx(txHash chainhash.Hash) (*wire.MsgTx, error) {
var targetTx *wtxmgr.TxDetails
err := walletdb.View(b.db, func(tx walletdb.ReadTx) error {
wtxmgrNs := tx.ReadBucket(wtxmgrNamespaceKey)
txDetails, err := b.wallet.TxStore.TxDetails(wtxmgrNs, &txHash)
if err != nil {
return err
}
targetTx = txDetails
return nil
})
if err != nil {
return nil, err
}
if targetTx == nil {
return nil, nil
}
return &targetTx.TxRecord.MsgTx, nil
}
// RemoveDescendants attempts to remove any transaction from the wallet's tx
// store (that may be unconfirmed) that spends outputs created by the passed
// transaction. This remove propagates recursively down the chain of descendent
// transactions.
func (b *BtcWallet) RemoveDescendants(tx *wire.MsgTx) error {
txRecord, err := wtxmgr.NewTxRecordFromMsgTx(tx, time.Now())
if err != nil {
return err
}
return walletdb.Update(b.db, func(tx walletdb.ReadWriteTx) error {
wtxmgrNs := tx.ReadWriteBucket(wtxmgrNamespaceKey)
return b.wallet.TxStore.RemoveUnminedTx(wtxmgrNs, txRecord)
})
}