Cache `unspent` restores an input of a deleted transaction that a known replacement still spends

Open
#204 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust
Domain
backend, database

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.

  1. Scan 1: A confirms and pays wallet output A:0. unspent = {A:0}.
  2. Scan 2: B is in the mempool and spends A:0. unspent = {}.
  3. B2 replaces B (fee bump) and is applied with Wollet::apply_transaction. unspent = {}.
  4. Scan 3: B was replaced, so it is in txid_height_delete. B2 is still unconfirmed, its height did not change, so it is not in txid_height_new. A:0 is 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Blockstream/lwk

All issues in Blockstream/lwk

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.