Repository navigation
Conversation
|
Tested this with a randomized model of scans that compares One case is still open. If the scan that brings a tx back started before the block confirming it and its spender, the tx comes back unconfirmed while the spender is cached as confirmed. As a row of (
// The scan bringing A back started before the block confirming A and B.
"re-added tx reported unconfirmed, spender confirmed",
vec![
a_seen(None),
b_seen(None),
(vec![(b_txid, Some(101))], vec![a_txid], vec![]),
a_back(None),
],
false,
),For an unconfirmed tx, also reading the txs at the highest cached height fixes it, and the model passes with it: let min_height =
min_height.or_else(|| self.sorted_txids.iter().find_map(|txid| self.heights[txid]));It only adds the txs of the top block, and covers a scan spanning one block. |
`update_unspent` adds the wallet outputs of every tx new to `heights`, but only removes the inputs of the txs in the same update. An output already spent by a known tx was added back to `unspent`, so `Wollet::utxos()` returned it, the balance counted it and the tx builder could select it, making the broadcast fail. This happens when: * a tx is removed by a scan and found again, while its spending tx stays in the history; * a tx is found after its spending tx, for example through another wallet script. Before adding the outputs, drop the ones spent by a known tx. A spending tx cannot confirm before the tx it spends, so only the known txs at the same height or above, and the unconfirmed ones, are read. A scan can read servers at different heights, so a tx can be reported unconfirmed while its spending tx is cached as confirmed, and the spending tx is then not read: * for a tx found again unconfirmed, all the known txs are read; a server reporting it as confirmed has seen its block, so the height bound reads its spending tx; * when the height of a known tx changes, its unspent outputs are checked again, so an output added with an inconsistent height is removed once a scan reports the right height. This also covers a tx found for the first time. Updates that neither add a wallet output nor change the height of a tx with unspent outputs read no tx, so updates still do not read every stored tx. Fixes Blockstream#201
Cover the cases where `update_unspent` added an output already spent by a known tx: a tx found again after a scan removed it, with a confirmed or unconfirmed spending tx, and a tx found after its spending tx, also in the same block. A re-added tx with no spending tx must keep its output unspent. Also cover a tx reported unconfirmed by a server that has not seen the block of its cached spending tx: found again, also when another wallet tx is in a later block, and found for the first time, then reported with its height.
7c6ebe2 to
f83999e
Compare
|
Thanks for testing this against a model of scans, and for the failing row. I added the row, together with a variant where another wallet tx is confirmed at 102 after B at 101. Reading the top cached block misses B in that variant, so I used two other checks:
Updates that neither add a wallet output nor change the height of a tx with unspent outputs still read no tx. About how a scan reaches it: step 3 already caches B at 101, so I think step 4 needs a server answering from an older chain state than the previous scan saw, for example backends at different heights. Esplora fetches the mempool and the chain history of each address separately, so the same thing could explain a confirmed tx dropping out of one scan in #201. I have not reproduced it against a live server. |
Fixes #201.
Problem
Cache::update_unspentadds the wallet outputs of every txid new toheights, and removes only the inputs of the txids in the same update. If a known transaction already spends one of those outputs, the output is added back tounspent.Wollet::utxos()then returns it, the balance counts it, and the transaction builder can select it, so the broadcast fails withbad-txns-inputs-missingorspent.Besides the case in the issue (a transaction deleted by a scan and found again while its spending transaction stays), the same happens when a transaction is found after its spending transaction, for example through another wallet script. The second case does not involve a deletion, so a fix limited to re-added transactions would not cover it.
Change
Before adding the outputs, drop the ones spent by a known transaction (in
heights, not in this update; spenders in this update are still handled byinputs_new).A spending transaction cannot confirm before the transaction it spends, so only the known transactions at the lowest candidate height or above, and the unconfirmed ones, are read.
sorted_txidsis sorted by descending height with unconfirmed first, so the iteration stops at the first lower transaction:A scan can read servers at different heights, so a transaction can be reported unconfirmed while its spending transaction is cached as confirmed, and the bound then skips the spender (reported by @diorwave below). Two checks cover this:
heights) reads all known transactions; a server reporting it as confirmed has seen its block, so the bound reads the spender;Wollet::apply_transactionwhile its spending transaction is cached as confirmed.An update that neither adds a wallet output nor changes the height of a transaction with unspent outputs reads nothing, so updates still do not read every stored transaction. The
utxo_onlyand v5 snapshot paths are unchanged.height_sort_keyreplaces theunwrap_or(u32::MAX)inrebuild_sorted_txids, so both use the same ordering.A transaction seen for the first time with an inconsistent height still shows its spent output until a scan reports its height.
Tests
test_output_spent_by_known_tx_is_not_unspentis one table-driven test incache.rs:The same-block row fails if the bound is changed from
>=to>. The three rows with an unconfirmed transaction fail without, in order, the full read for an unconfirmed re-added transaction, the full read instead of also reading only the highest cached block, and the check on height change. The last row checks that a re-added output with no spending transaction is still added.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 (an OrbStack NixOS VM, sincenix developdoes not evaluate on aarch64-darwin): 131 passed, 19 ignored, 7 failed.elementsd23.3.3 andbitcoindfrom the pinned nixpkgs (1c3fe55), Liquidelectrsbuilt from the pinnede60ca890(the flake build fails on aarch64-linux because theelementsdcrate's build script does not support it),waterfallsbuilt from the pinnedb2b31351, and Docker for the Jade and Ledger emulators.AMP2_MOCK_EXEC(amp2::test_amp2_flow,amp2::test_elip153_flow,test_jade::emul_elip153_flow),LIGHTNINGD_EXEC,ASSET_REGISTRY_EXEC.registry::test_registry_v2times out against the emulated registry container, also onmaster.ct_discountfailed while starting the test environment (test_env.rs:390, the electrs header subscription returned an empty reply) and passes 3/3 alone.jade_sign_wollet_psetfailed to pull its image andtest_zmq_endpointwas flaky in this VM on both branches; both passed in this run.Notes
unspentsnapshot as is. A wallet that already has a resurrected output keeps it until it is scanned again from an empty store; the CHANGELOG entry says so. A one-time check on restore could repair it, but it would read the transactions that the snapshot avoids reading, so I left it out. Happy to add it if you prefer.unspentrestores an input of a deleted transaction that a known replacement still spends #204 (inputs of a deleted transaction restored although a replacement spends them) and Applying a merged update with a txid in bothtxid_height_newandtxid_height_deletedrops its outputs fromunspent#205 (a merged update with a txid both added and deleted drops its outputs).