2018-12-07 09:06:36 +01:00
|
|
|
package sweep
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2019-12-10 15:32:57 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
2018-12-07 09:06:36 +01:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
|
|
|
)
|
|
|
|
|
|
|
|
// mockBackend simulates a chain backend for realistic behaviour in unit tests
|
|
|
|
// around double spends.
|
|
|
|
type mockBackend struct {
|
2019-12-10 15:32:57 +01:00
|
|
|
t *testing.T
|
|
|
|
|
2018-12-07 09:06:36 +01:00
|
|
|
lock sync.Mutex
|
|
|
|
|
|
|
|
notifier *MockNotifier
|
|
|
|
|
|
|
|
confirmedSpendInputs map[wire.OutPoint]struct{}
|
|
|
|
|
|
|
|
unconfirmedTxes map[chainhash.Hash]*wire.MsgTx
|
|
|
|
unconfirmedSpendInputs map[wire.OutPoint]struct{}
|
2019-12-10 15:32:57 +01:00
|
|
|
|
|
|
|
publishChan chan wire.MsgTx
|
2019-12-10 16:06:45 +01:00
|
|
|
|
|
|
|
walletUtxos []*lnwallet.Utxo
|
2020-11-09 18:49:44 +01:00
|
|
|
utxoCnt int
|
2018-12-07 09:06:36 +01:00
|
|
|
}
|
|
|
|
|
2019-12-10 15:32:57 +01:00
|
|
|
func newMockBackend(t *testing.T, notifier *MockNotifier) *mockBackend {
|
2018-12-07 09:06:36 +01:00
|
|
|
return &mockBackend{
|
2019-12-10 15:32:57 +01:00
|
|
|
t: t,
|
2018-12-07 09:06:36 +01:00
|
|
|
notifier: notifier,
|
|
|
|
unconfirmedTxes: make(map[chainhash.Hash]*wire.MsgTx),
|
|
|
|
confirmedSpendInputs: make(map[wire.OutPoint]struct{}),
|
|
|
|
unconfirmedSpendInputs: make(map[wire.OutPoint]struct{}),
|
2019-12-10 15:32:57 +01:00
|
|
|
publishChan: make(chan wire.MsgTx, 2),
|
2018-12-07 09:06:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *mockBackend) publishTransaction(tx *wire.MsgTx) error {
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
|
|
|
txHash := tx.TxHash()
|
|
|
|
if _, ok := b.unconfirmedTxes[txHash]; ok {
|
|
|
|
// Tx already exists
|
|
|
|
testLog.Tracef("mockBackend duplicate tx %v", tx.TxHash())
|
|
|
|
return lnwallet.ErrDoubleSpend
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, in := range tx.TxIn {
|
|
|
|
if _, ok := b.unconfirmedSpendInputs[in.PreviousOutPoint]; ok {
|
|
|
|
// Double spend
|
|
|
|
testLog.Tracef("mockBackend double spend tx %v", tx.TxHash())
|
|
|
|
return lnwallet.ErrDoubleSpend
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := b.confirmedSpendInputs[in.PreviousOutPoint]; ok {
|
|
|
|
// Already included in block
|
|
|
|
testLog.Tracef("mockBackend already in block tx %v", tx.TxHash())
|
|
|
|
return lnwallet.ErrDoubleSpend
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.unconfirmedTxes[txHash] = tx
|
|
|
|
for _, in := range tx.TxIn {
|
|
|
|
b.unconfirmedSpendInputs[in.PreviousOutPoint] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
testLog.Tracef("mockBackend publish tx %v", tx.TxHash())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-18 14:13:23 +02:00
|
|
|
func (b *mockBackend) PublishTransaction(tx *wire.MsgTx, _ string) error {
|
2019-12-10 15:32:57 +01:00
|
|
|
log.Tracef("Publishing tx %v", tx.TxHash())
|
|
|
|
err := b.publishTransaction(tx)
|
|
|
|
select {
|
|
|
|
case b.publishChan <- *tx:
|
|
|
|
case <-time.After(defaultTestTimeout):
|
|
|
|
b.t.Fatalf("unexpected tx published")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-22 19:04:00 +02:00
|
|
|
func (b *mockBackend) ListUnspentWitnessFromDefaultAccount(minConfs, maxConfs int32) (
|
2019-12-10 16:06:45 +01:00
|
|
|
[]*lnwallet.Utxo, error) {
|
2022-02-07 13:58:28 +01:00
|
|
|
|
2020-11-09 18:49:44 +01:00
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
|
|
|
// Each time we list output, we increment the utxo counter, to
|
|
|
|
// ensure we don't return the same outpoint every time.
|
|
|
|
b.utxoCnt++
|
|
|
|
|
|
|
|
for i := range b.walletUtxos {
|
|
|
|
b.walletUtxos[i].OutPoint.Hash[0] = byte(b.utxoCnt)
|
|
|
|
}
|
2019-12-10 16:06:45 +01:00
|
|
|
|
|
|
|
return b.walletUtxos, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *mockBackend) WithCoinSelectLock(f func() error) error {
|
|
|
|
return f()
|
|
|
|
}
|
|
|
|
|
2018-12-07 09:06:36 +01:00
|
|
|
func (b *mockBackend) deleteUnconfirmed(txHash chainhash.Hash) {
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
|
|
|
tx, ok := b.unconfirmedTxes[txHash]
|
|
|
|
if !ok {
|
|
|
|
// Tx already exists
|
|
|
|
testLog.Errorf("mockBackend delete tx not existing %v", txHash)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
testLog.Tracef("mockBackend delete tx %v", tx.TxHash())
|
|
|
|
delete(b.unconfirmedTxes, txHash)
|
|
|
|
for _, in := range tx.TxIn {
|
|
|
|
delete(b.unconfirmedSpendInputs, in.PreviousOutPoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *mockBackend) mine() {
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
|
|
|
notifications := make(map[wire.OutPoint]*wire.MsgTx)
|
|
|
|
for _, tx := range b.unconfirmedTxes {
|
|
|
|
testLog.Tracef("mockBackend mining tx %v", tx.TxHash())
|
|
|
|
for _, in := range tx.TxIn {
|
|
|
|
b.confirmedSpendInputs[in.PreviousOutPoint] = struct{}{}
|
|
|
|
notifications[in.PreviousOutPoint] = tx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.unconfirmedSpendInputs = make(map[wire.OutPoint]struct{})
|
|
|
|
b.unconfirmedTxes = make(map[chainhash.Hash]*wire.MsgTx)
|
|
|
|
|
|
|
|
for outpoint, tx := range notifications {
|
|
|
|
testLog.Tracef("mockBackend delivering spend ntfn for %v",
|
|
|
|
outpoint)
|
|
|
|
b.notifier.SpendOutpoint(outpoint, *tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *mockBackend) isDone() bool {
|
|
|
|
return len(b.unconfirmedTxes) == 0
|
|
|
|
}
|
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
2022-02-18 03:25:30 +01:00
|
|
|
|
|
|
|
func (b *mockBackend) RemoveDescendants(*wire.MsgTx) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *mockBackend) FetchTx(chainhash.Hash) (*wire.MsgTx, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2023-04-14 23:58:13 +02:00
|
|
|
|
|
|
|
func (b *mockBackend) CancelRebroadcast(tx chainhash.Hash) {
|
|
|
|
}
|