From b445db457e3e540dcef78a8416de9297b330a7ca Mon Sep 17 00:00:00 2001 From: Jeon1691 <16333003+Jeon1691@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:12:55 +0900 Subject: [PATCH 1/2] wollet: cache: keep outputs of re-added txs A merged update can list a txid both in `txid_height_new` and in `txid_height_delete`, for example the merge of "A new", "A deleted" and "A new" again. `update_heights` deletes before inserting, so the tx stays in the wallet, but `update_unspent` then removed the outputs of every deleted txid, including the re-added one. `Wollet::utxos()` and the balance missed them while the tx was listed. Keep the outputs of the txids that the update also adds. Fixes https://github.com/Blockstream/lwk/issues/205 --- lwk_wollet/CHANGELOG.md | 1 + lwk_wollet/src/cache.rs | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lwk_wollet/CHANGELOG.md b/lwk_wollet/CHANGELOG.md index a50bfb6d7..f899374c7 100644 --- a/lwk_wollet/CHANGELOG.md +++ b/lwk_wollet/CHANGELOG.md @@ -6,6 +6,7 @@ * Removed `Amp2::new_testnet()`, use `Amp2::new()`; also removed old AMP2 testnet consts. * Breaking: update the re-exported `age` crate to 0.12. Its `fl!` localization macro dependency emitted arguments in random order, making builds non-deterministic. See the [age 0.12 changelog](https://github.com/str4d/rage/blob/main/age/CHANGELOG.md) for API changes. * Breaking: `AddressResult::new()` takes the full derivation path of the address, `None` if unknown, readable with the new `AddressResult::derivation_path()`. +* Fix `Wollet::utxos()` missing the outputs of a transaction that a merged update deletes and adds back, for example a transaction applied with `Wollet::apply_transaction`, missing from the next scan and returned by a later one. The transaction was listed but its outputs were not counted in the balance. This affected wallets with merged updates (`WolletBuilder::with_merge_threshold()` or `WolletBuilder::with_stores()`) and a non-persisted transaction store, when the persisted updates were applied again, for example on reload. ## 0.20.0 diff --git a/lwk_wollet/src/cache.rs b/lwk_wollet/src/cache.rs index 57e70ff70..8e5e4bbd6 100644 --- a/lwk_wollet/src/cache.rs +++ b/lwk_wollet/src/cache.rs @@ -374,9 +374,14 @@ impl Cache { // Remove inputs of new txs (they are spent now) self.unspent.retain(|o, _| !inputs_new.contains(o)); // Remove outputs of deleted txs (after adding inputs, so that an output spent - // by another deleted tx does not remain in unspent) - self.unspent - .retain(|o, _| deleted_txids.iter().all(|txid| txid != &o.txid)); + // by another deleted tx does not remain in unspent). A merged update can delete + // a tx and add it back: `update_heights` deletes before inserting, so the tx + // stays in the wallet and its outputs are kept. + let removed: HashSet<&Txid> = deleted_txids + .iter() + .filter(|txid| !txids_new.contains(txid)) + .collect(); + self.unspent.retain(|o, _| !removed.contains(&o.txid)); } fn update_unspent_from_snapshot( From 6557efd3a1d93b8a5f908741ea87963d891aac1c Mon Sep 17 00:00:00 2001 From: Jeon1691 <16333003+Jeon1691@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:12:59 +0900 Subject: [PATCH 2/2] wollet: cache: test outputs of re-added txs Apply an update that lists a tx both as new and as deleted, as a merged update can do, and check that the tx output stays unspent. --- lwk_wollet/src/cache.rs | 49 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/lwk_wollet/src/cache.rs b/lwk_wollet/src/cache.rs index 8e5e4bbd6..da2f434f8 100644 --- a/lwk_wollet/src/cache.rs +++ b/lwk_wollet/src/cache.rs @@ -641,4 +641,53 @@ mod tests { assert_eq!(live.all_txids(), restored.all_txids()); } + + #[test] + fn test_merged_delete_and_readd_keeps_output() { + use crate::descriptor::Chain; + use elements::bitcoin::bip32::ChildNumber; + use elements::confidential::{AssetBlindingFactor, ValueBlindingFactor}; + use elements::{AssetId, OutPoint, Script, 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); + + let mut cache = Cache::default(); + cache + .paths + .insert(script, (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)]); + + // `Update::merge` of "A new", "A deleted" and "A new" again lists A both as new + // and as deleted. + cache + .update( + &[(a_txid, Some(100))], + &[a_txid], + &[(a_txid, a)], + false, + vec![], + false, + ) + .unwrap(); + assert!(cache.heights().contains_key(&a_txid)); + assert!(cache.unspent().contains_key(&a0)); + } }