Skip to content

key-wallet: expose which of the wallet's coins a finalized transaction spent #1376

Open

Description

@HashEngineering

TL;DR: Since #1326, a funds account keeps a FinalizedSummary (owned outputs and input_details) for each record
that ChainLock finality evicts, but only inside the crate. A consumer that needs to know "was this coin of mine spent
by a transaction no reorg can undo?" still cannot ask key-wallet, and has to rebuild the answer by watching records
before they are evicted.

Who needs it

dashpay/platform's rs-platform-wallet reconciles a host app's TXO store against the engine (dashpay/platform#5796).
A stored coin is marked spent only when its spender is ChainLocked, because a write based on a spender a fork could
drop would retire a live coin for good. To know that, it keeps its own finalized_spends set, filled by intercepting
the wallet before key-wallet evicts:

  • in apply_chain_lock, the owned inputs of every in-block record at or below the lock height, before delegating;
  • in check_core_transaction, the owned inputs of new and updated records whose own context is ChainLocked,
    including records handed back by late-input attribution;
  • a compile-time cfg!(not(feature = "keep-finalized-transactions")) standing in for "key-wallet evicts". That is
    wrong if another crate enables key-wallet/keep-finalized-transactions directly, through Cargo feature
    unification.

This ties platform-wallet to key-wallet's promotion and eviction paths, the record slices it returns, and its
late-input attribution. A missed path leaves a coin Unknown (safe, but never repaired); a misread one could mark a
live coin spent.

What key-wallet has today (dev 451c6cc)

  • ManagedCoreKeysAccount::drop_finalized_transaction stores FinalizedSummary::of(&record) in
    finalized_summaries when a record is evicted; FinalizedSummary keeps input_details. Both are pub(crate).
  • The summary's outputs are updated later (adopt_finalized_outputs, a rescan that finds more owned outputs). I
    found no path that adds an input to a summary when the coin it spent is funded late, so a spender finalized
    before its funding arrives would still be missing that input.
  • is_outpoint_spent on the funds account is pub(crate), per account, and does not say whether the spender is
    final.
  • With keep-finalized-transactions on, records stay and there are no summaries; nothing public tells a consumer
    which mode the crate was compiled in.

Ask

  1. A public, finality-aware query on the wallet (or each funds account), for example
    finalized_spender_of(&OutPoint) -> Option<Txid>, answered from retained records in the
    keep-finalized-transactions build and from finalized_summaries otherwise, so the answer doesn't depend on the
    feature.
  2. Keep it complete on every path that finalizes a spend: ChainLock promotion, a transaction first seen in a
    ChainLocked block, and funding that arrives after its spender was finalized (add the input to the summary).

With that, platform-wallet drops finalized_spends, both interceptions and the feature constant.

Related

#1303: re-delivery reaches only accounts that are still relevant, so an account whose only part in a transaction was
the coin it spent keeps its record in Mempool after the transaction is mined or ChainLocked. #1303 shows it for an
asset lock with no change. By the same root cause an ordinary payment whose change goes to another account of the
same wallet should leave the paying account's record unpromoted too (not tested). The query above has to answer for
that account's coins whether or not #1303 is fixed first.

Raised by thepastaclaw on dashpay/platform#5796 (packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs,
lines 296 and 319).

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