Cache `unspent` re-adds a spent output when its transaction is deleted and then seen again
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
The bug is in Cache::update_unspent in lwk_wollet/src/cache.rs, which adds outputs of re-seen txids without checking whether a known transaction already spends them. Start by reading update_unspent and how heights and the txs store interact on a delete-then-re-add. Add the issue's test_readded_tx_resurrects_spent_output to the tests module and run it; done means it passes and the existing cache tests still pass.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Summary
Since the cache keeps unspent incrementally, a wallet output that is already spent can become "unspent" again and stay that way. Wollet::utxos() then returns it, the transaction builder selects it, and the broadcast fails with bad-txns-inputs-missingorspent. Later scans do not correct it, and replaying the persisted updates reproduces it on every load.
Version: lwk_wollet 0.20.0. The same logic appears to be on master.
Cause
Cache::update_unspent (lwk_wollet/src/cache.rs) adds every wallet output of a txid in txid_height_new that was not known before the update. It removes only the inputs of the txids in txid_height_new. It never checks whether an output it adds is spent by a transaction the cache already knows.
That is correct for a transaction seen for the first time. It is wrong for a transaction that was deleted by an earlier update and now comes back:
- Scan 1:
Aconfirms and pays wallet outputA:0.unspent = {A:0}. - Scan 2:
Bconfirms and spendsA:0.unspent = {}. - Scan 3: the script history does not contain
A(but still containsB).full_scan_to_index_defaultputsAintxid_height_delete.Ais removed fromheights. It stays in the txs store and inunblinded. - Scan 4:
Ais in the history again. It is not inheights, so it goes intxid_height_newand is treated as new.A:0is added tounspent.Bis not intxid_height_new, so its inputs are not removed.
After step 4, A:0 is in unspent forever, unless the height of B changes.
Before 0.20 this could not happen, because the unspent set was derived from all transactions.
Reproduction
Add to the tests module in lwk_wollet/src/cache.rs:
#[test]
fn test_readded_tx_resurrects_spent_output() {
use crate::descriptor::Chain;
use elements::bitcoin::bip32::ChildNumber;
use elements::confidential::{AssetBlindingFactor, ValueBlindingFactor};
use elements::{AssetId, OutPoint, Script, 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 spends A:0.
let b = Transaction {
version: 2,
lock_time: elements::LockTime::ZERO,
input: vec![TxIn {
previous_output: a0,
..Default::default()
}],
output: vec![],
};
let b_txid = b.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 confirms and spends A:0.
cache
.update(&[(b_txid, Some(101))], &[], &[(b_txid, b)], false, vec![], false)
.unwrap();
assert!(!cache.unspent().contains_key(&a0));
// Scan 3: the history misses A one time. B is still there.
cache
.update(&[], &[a_txid], &[], false, vec![], false)
.unwrap();
assert!(!cache.unspent().contains_key(&a0));
// Scan 4: A is back in the history. It is already in the txs store.
cache
.update(&[(a_txid, Some(100))], &[], &[], false, vec![], false)
.unwrap();
// Fails on 0.20.0: A:0 is unspent again.
assert!(!cache.unspent().contains_key(&a0));
}
I ran this sequence against the 0.20.0 source. unspent contains A:0 after scans 1 and 4, and not after scans 2 and 3.
Open question
I have not found what makes a confirmed transaction drop out of the scanned history for one scan. The step from scan 2 to scan 3 above is the assumption. Is there a known way for a scan to return a history that misses a transaction that is still valid?
Possible fix
When update_unspent adds the outputs of a txid, skip any outpoint that is an input of a transaction in heights. Or rebuild unspent from all transactions when an update re-adds a txid that the txs store already holds. A transaction that comes back after a delete is a normal event (reorg, mempool eviction and re-broadcast), so it probably should not depend on the backend being consistent.
- Ngôn ngữ chính
- Rust
- Star
- 114
- Fork
- 69
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của Blockstream/lwk
-
Ignore entry for RUSTSEC-2020-0168 no longer matches anything (`mach` is no longer in `Cargo.lock`)Có thể đã có người làm @zyl71 đã nhận hôm nay. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
Blockstream/lwk#213 ·
-
wollet: Waterfalls scan sets `last_unused` to a page-relative index past the first pageCó thể đã có người làm @diorwave đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Blockstream/lwk#209 ·
-
C# bindings: all generated types are `internal`, `LiquidWalletKit` 0.8.2 has no usable public APICó thể đã có người làm @lisboaigor đã nhận hôm nay. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 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`Có thể đã có người làm @Jeon1691 đã nhận 4 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 78/100
Blockstream/lwk#205 ·
-
Cache `unspent` restores an input of a deleted transaction that a known replacement still spendsĐang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
Blockstream/lwk#204 ·
Tất cả issue của Blockstream/lwk
Issue tương tự
-
Ignore entry for RUSTSEC-2026-0002 no longer matches anything (`lru` is no longer in `Cargo.lock`)Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
influxdata/influxdb#27767 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
manuscript-lang/manuscript#6389 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 62/100
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 68/100
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày