Skip to content

Spender of a claim-guarded coin loses its input match when the same transaction is seen again #1403

Open

Description

@manki-review

What goes wrong: Yes, the spender's input can sit in claim_guarded_outputs, and the existing fallback at line 754 resolves it the first time the spender is processed. Recording the spender then removes the entry and does not park it under the spend (managed_core_funds_account.rs:541, comment: "Not kept under the spend"). When the same transaction arrives again (for example mempool then block, with no fork), the new unsettled_spends lookup has nothing for that input, so sent is 0 for it. A spender with a change output is then rebuilt as incoming with the wrong net amount, and one without change drops out of the account.

// Check inputs (sent) - rely on tracked UTXOs to determine spends
if !tx.is_coin_base() {
let spent_by_tx = self.unsettled_spends.get(&tx.txid());
for input in &tx.input {
if let Some(utxo) = self

How to fix it
Park claim-guarded inputs under the spender in a store that a fork does not credit back to utxos. Alternatively, resolve a re-seen transaction's inputs from its existing record's input_details. Add a test that delivers the spender of a claim-guarded coin via mempool and then via block.

Checked in the code
On the flagged line: The input fallback chain is utxos, spent_before_funded, claim_guarded_outputs, then spent_by_tx from unsettled_spends.get(&tx.txid()).

self.claim_guarded_outputs.remove(&input.previous_output); with no push into unsettled_spends.

// a coin that only a claim kept out of `utxos`.
self.claim_guarded_outputs.remove(&input.previous_output);
if let Some(spent) = self.utxos.remove(&input.previous_output) {
tracing::debug!(

And 549: spent_before_funded and utxos entries are moved into unsettled_spends.entry(txid), but claim-guarded entries are not.

// too, so a fork dropping this spend gives it back as well.
if let Some(held) = self.spent_before_funded.remove(&input.previous_output) {
self.unsettled_spends.entry(txid).or_default().push(held);
}
// Not kept under the spend: a fork dropping it must not credit

keep_recorded_output_for_claim refills the entry only after truncate_above, and it returns early while spent_outpoints still holds the outpoint.

// so the spend is still matched when it is mined again.
for outpoint in spent_claims.keys() {
self.keep_recorded_output_for_claim(outpoint);
}
if !restored.is_empty() {

Double-checked
Manki tried to disprove this by reading the code and could not. Recording the spender removes the claim-guarded coin (line 541) without moving it into unsettled_spends, unlike lines 537 and 549. The claim-refill path return…

Found by Manki in #1399 at key-wallet/src/transaction_checking/account_checker.rs:748. See the review.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions