Repository navigation
Description
Summary
When a scan deletes a transaction, Cache::update_unspent puts its wallet inputs back in unspent. It does not check whether another known transaction still spends them. After a fee bump this makes a spent output unspent again: Wollet::utxos() returns it, the transaction builder can select it, and the broadcast fails with bad-txns-inputs-missingorspent.
Version: lwk_wollet 0.20.0, still on master (ea5321528).
This is close to #201, which covers outputs added back by a re-added transaction (fix in #203). Here the output comes back through the inputs of a deleted transaction, which #203 does not change.
Cause
update_unspent (lwk_wollet/src/cache.rs) computes inputs_to_restore from the inputs of every txid in deleted_txids and adds them to unspent. Then it removes only the inputs of the txids in txid_height_new. A known transaction that spends the same outpoint, but whose height did not change, is not in txid_height_new, so its input is not removed again.
- Scan 1:
Aconfirms and pays wallet outputA:0.unspent = {A:0}. - Scan 2:
Bis in the mempool and spendsA:0.unspent = {}. B2replacesB(fee bump) and is applied withWollet::apply_transaction.unspent = {}.- Scan 3:
Bwas replaced, so it is intxid_height_delete.B2is still unconfirmed, its height did not change, so it is not intxid_height_new.A:0is restored.
After step 4, A:0 stays in unspent until B2 changes height, even though B2 spends it.
Reproduction
Add to the tests module in lwk_wollet/src/cache.rs:
#[test]
fn test_deleted_tx_restores_input_spent_by_replacement() {
use crate::descriptor::Chain;
use elements::bitcoin::bip32::ChildNumber;
use elements::confidential::{AssetBlindingFactor, ValueBlindingFactor};
use elements::{AssetId, OutPoint, Script, Sequence, TxIn, TxOut, TxOutSecrets};
let script = Script::from(vec![0x51]);
// A pays one output to a wallet script.
let a = Transaction {
version: 2,
lock_time: elements::LockTime::ZERO,
input: vec![],
output: vec![TxOut {
script_pubkey: script.clone(),
..Default::default()
}],
};
let a_txid = a.txid();
let a0 = OutPoint::new(a_txid, 0);
// B and its replacement B2 both spend A:0.
let spend = |sequence| Transaction {
version: 2,
lock_time: elements::LockTime::ZERO,
input: vec![TxIn {
previous_output: a0,
sequence: Sequence(sequence),
..Default::default()
}],
output: vec![],
};
let b = spend(0xfffffffd);
let b2 = spend(0xfffffffe);
let (b_txid, b2_txid) = (b.txid(), b2.txid());
let mut cache = Cache::default();
cache
.paths
.insert(script.clone(), (Chain::External, ChildNumber::from(0)));
let secrets = TxOutSecrets::new(
AssetId::from_byte_array([1u8; 32]),
AssetBlindingFactor::zero(),
1000,
ValueBlindingFactor::zero(),
);
cache.extend_unblinded([(a0, secrets)]);
// Scan 1: A confirms. A:0 is unspent.
cache
.update(&[(a_txid, Some(100))], &[], &[(a_txid, a)], false, vec![], false)
.unwrap();
assert!(cache.unspent().contains_key(&a0));
// Scan 2: B is in the mempool and spends A:0.
cache
.update(&[(b_txid, None)], &[], &[(b_txid, b)], false, vec![], false)
.unwrap();
assert!(!cache.unspent().contains_key(&a0));
// B2 replaces B and is applied locally, as `Wollet::apply_transaction` does.
cache
.update(&[(b2_txid, None)], &[], &[(b2_txid, b2)], false, vec![], false)
.unwrap();
assert!(!cache.unspent().contains_key(&a0));
// Scan 3: B was replaced and is deleted. B2 is still unconfirmed, its height
// did not change, so it is not in the update.
cache
.update(&[], &[b_txid], &[], false, vec![], false)
.unwrap();
// Fails: A:0 is unspent again, although B2 spends it.
assert!(!cache.unspent().contains_key(&a0));
}On master the last assertion fails; the earlier ones pass.
Possible fix
Before restoring the inputs of a deleted transaction, skip the ones spent by a transaction that is still in heights. A spending transaction cannot confirm before the transaction it spends, so only the known transactions at or above the height of the restored output need to be read. #203 adds Cache::spent_by_known_txs for the same check on added outputs, so it could be reused here.
Activity