Cache `unspent` restores an input of a deleted transaction that a known replacement still spends
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 68/100
Research direction
Read lwk_wollet/src/cache.rs, focusing on Cache::update_unspent and the spent_by_known_txs helper mentioned in the issue. Add a regression test in that file’s tests module based on the reproduction. Done means deleted transactions’ inputs remain absent from unspent when a known replacement still spends them, and the test passes.
Written by the indexing model from the issue text.
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.
- Dominant language
- Rust
- Stars
- 114
- Forks
- 69
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Blockstream/lwk
-
Ignore entry for RUSTSEC-2020-0168 no longer matches anything (`mach` is no longer in `Cargo.lock`)Possibly taken @zyl71 claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 72/100
Blockstream/lwk#213 ·
-
wollet: Waterfalls scan sets `last_unused` to a page-relative index past the first pagePossibly taken @diorwave claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Blockstream/lwk#209 ·
-
C# bindings: all generated types are `internal`, `LiquidWalletKit` 0.8.2 has no usable public APIPossibly taken @lisboaigor claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 25/100
Blockstream/lwk#210 ·
-
Applying a merged update with a txid in both `txid_height_new` and `txid_height_delete` drops its outputs from `unspent`Possibly taken @Jeon1691 claimed this 4 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 78/100
Blockstream/lwk#205 ·
-
Cache `unspent` re-adds a spent output when its transaction is deleted and then seen againPossibly taken @Jeon1691 claimed this 6 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 35/100
Blockstream/lwk#201 ·
Similar issues
-
Ignore entry for RUSTSEC-2026-0002 no longer matches anything (`lru` is no longer in `Cargo.lock`)Open
Difficulty 1/5 Under an hour Newbie friendliness 72/100
influxdata/influxdb#27767 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
manuscript-lang/manuscript#6389 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 62/100
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
-
Difficulty 1/5 Under an hour Newbie friendliness 62/100
Maintainers usually reply within 1 day