Skip to content

wollet: cache: keep outputs of re-added txs - #207

Open
Jeon1691 wants to merge 2 commits into
Blockstream:masterfrom
Jeon1691:fix/205-merged-delete-readd
Open

Jeon1691 wants to merge 2 commits into
Blockstream:masterfrom
Jeon1691:fix/205-merged-delete-readd

Conversation

@Jeon1691

Copy link
Copy Markdown

Fixes #205.

Problem

Update::merge computes Um.insert = (U1.insert \ U2.delete) ∪ U2.insert and Um.delete = U1.delete ∪ U2.delete, so a merged update can list the same txid in both txid_height_new and txid_height_delete, for example the merge of "A new", "A deleted" and "A new" again.

Cache::update keeps A in heights, since update_heights deletes before inserting. Cache::update_unspent then removes the outputs of every txid in deleted_txids as its last step, including A. The wallet lists A but Wollet::utxos() and the balance miss its outputs.

This needs a v4 update going through update_unspent: a wallet with merged updates (WolletBuilder::with_merge_threshold(), or WolletBuilder::with_stores() with a store that is not persisted) and a non-persisted transaction store (the default MemoryStore), when the persisted updates are applied again, for example on reload. With a persisted transaction store the updates are upgraded to v5 and unspent comes from the stored snapshot.

Change

When removing the outputs of deleted txids, keep the ones of txids that the same update also adds. This gives unspent the same "delete, then insert" meaning that update_heights already gives heights.

I did not change Update::merge instead: merged updates that list a txid in both vectors may already be persisted, and fixing the cache also applies them correctly. I can also drop re-inserted txids from txid_height_delete in merge if you prefer both.

The inputs of such a tx are already consistent: inputs_to_restore adds them back as inputs of a deleted tx, and inputs_new removes them again as inputs of a new tx.

Tests

test_merged_delete_and_readd_keeps_output in cache.rs applies an update listing a tx both as new and as deleted, and checks that its output stays unspent. It fails on master at the last assertion.

  • cargo test -p lwk_wollet --lib: 141 passed.
  • cargo clippy -p lwk_wollet --all-targets -- -D warnings, cargo fmt --check: clean.
  • cargo test -p lwk_wollet --test e2e, on aarch64-linux with the same setup as wollet: cache: keep outputs spent by known txs #203: 129 passed, 19 ignored, 9 failed.
    • 5 failures are missing executables: AMP2_MOCK_EXEC (amp2::test_amp2_flow, amp2::test_elip153_flow, test_jade::emul_elip153_flow), LIGHTNINGD_EXEC, ASSET_REGISTRY_EXEC.
    • registry::test_registry_v2 times out against the emulated registry container, also on master, and test_zmq_endpoint is flaky in this VM on both branches, as reported in wollet: cache: keep outputs spent by known txs #203.
    • ct_discount and test_merge_tx_update failed while starting the test environment (test_env.rs:390, the electrs header subscription returned an invalid reply) and pass 3/3 when run alone; both passed in a full run before the last review change, which only builds the removed txids as a set.

Notes

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 Blockstream#205
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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Applying a merged update with a txid in both txid_height_new and txid_height_delete drops its outputs from unspent

1 participant