Skip to content

feat(key-wallet): stamp retained transaction records with their in-block position - #891

Merged
QuantumExplorer merged 1 commit into
devfrom
fix/in-block-position-retained-records
Jul 14, 2026
Merged

feat(key-wallet): stamp retained transaction records with their in-block position#891
QuantumExplorer merged 1 commit into
devfrom
fix/in-block-position-retained-records

Conversation

@QuantumExplorer

@QuantumExplorer QuantumExplorer commented Jul 14, 2026

Copy link
Copy Markdown
Member

Issue being fixed or feature implemented

Fixes #890. Retained TransactionRecords carried no in-block position: BlockInfo held only {height, block_hash, timestamp}, account.transactions() iterates in txid order, and the index visible during block processing was discarded. Downstream consumers that aggregate a wallet's retained provider special transactions (dashpay/platform#4120's masternode list) therefore cannot replay Core's same-block apply order — RebuildListFromBlock applies same-block provider updates in block.vtx order, so with two updates for one proTxHash in a single block, the winner per field must be the later-positioned one, not whichever txid sorts higher.

What was done?

  • BlockInfo gains position: Option<u32> (serde(default); None for contexts built without the containing block — FFI-supplied contexts, legacy persisted records), a position() accessor, and a with_position builder. BlockInfo::new keeps its 3-arg signature, so existing call sites are untouched.
  • process_block_for_wallets enumerates block.txdata and stamps each record's context with its index. Chainlock promotion (apply_chain_lock) already copies the whole BlockInfo, so the position survives InBlockInChainLockedBlock.
  • New test test_block_processing_stamps_in_block_position: a two-tx block yields records stamped positions 0 and 1.

How Has This Been Tested?

cargo test -p key-wallet -p key-wallet-manager -p key-wallet-ffi — 548 + 233 + FFI suites, 0 failures.

Breaking Changes

None — additive field with a defaulted deserialization and an unchanged constructor signature.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Corrected transaction processing so each transaction retains its position within the block.
    • Block-processed wallet events now report accurate transaction indexes for all transactions.
    • Preserved transaction position information across processing contexts and serialized data.

…ock position

Fixes #890.

Consumers aggregating a wallet's retained provider special transactions
need to replay Core's same-block apply order (RebuildListFromBlock
applies provider updates in block.vtx order), but the retained records
carried no in-block index: BlockInfo held only height/hash/timestamp,
account.transactions() iterates in txid order, and the index visible
during block processing was discarded.

- BlockInfo gains `position: Option<u32>` (serde default; None for
  contexts built without the containing block, e.g. FFI-supplied
  contexts and legacy persisted records), with a `position()` accessor
  and a `with_position` builder. `new()` keeps its 3-arg signature.
- process_block_for_wallets enumerates block.txdata and stamps each
  record's context with its index. Chainlock promotion already copies
  the whole BlockInfo, so the position survives InBlock →
  InChainLockedBlock transitions.
- Test pins that a two-tx block yields records stamped 0 and 1.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 34325e21-8618-4c20-a5b8-1979289b05e4

📥 Commits

Reviewing files that changed from the base of the PR and between 7221d86 and 4943dcd.

📒 Files selected for processing (3)
  • key-wallet-manager/src/event_tests.rs
  • key-wallet-manager/src/process_block.rs
  • key-wallet/src/transaction_checking/transaction_context.rs

📝 Walkthrough

Walkthrough

Block processing now stamps each wallet-relevant transaction with its index in the block. BlockInfo retains this optional position through transaction context handling, and a new test verifies positions in emitted wallet records.

Changes

Block Position Tracking

Layer / File(s) Summary
Position metadata contract
key-wallet/src/transaction_checking/transaction_context.rs
BlockInfo adds an optional position field, initializes it, serializes it with a default, and exposes builder/accessor methods.
Indexed block processing and validation
key-wallet-manager/src/process_block.rs, key-wallet-manager/src/event_tests.rs
Block transactions are processed with per-transaction contexts carrying their indexes, and emitted records are tested for matching positions.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related issues

Suggested reviewers: xdustinface, zocolini

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: stamping retained transaction records with their in-block position.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/in-block-position-retained-records

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@QuantumExplorer

Copy link
Copy Markdown
Member Author

Reviewed

@QuantumExplorer
QuantumExplorer merged commit 38c689d into dev Jul 14, 2026
34 of 35 checks passed
@QuantumExplorer
QuantumExplorer deleted the fix/in-block-position-retained-records branch July 14, 2026 12:16
@codecov

codecov Bot commented Jul 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.32%. Comparing base (7221d86) to head (4943dcd).
⚠️ Report is 1 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #891      +/-   ##
==========================================
- Coverage   74.34%   74.32%   -0.02%     
==========================================
  Files         327      327              
  Lines       74771    74779       +8     
==========================================
- Hits        55591    55582       -9     
- Misses      19180    19197      +17     
Flag Coverage Δ
core 77.29% <ø> (ø)
ffi 50.30% <ø> (-0.01%) ⬇️
rpc 20.00% <ø> (ø)
spv 90.87% <ø> (-0.10%) ⬇️
wallet 74.41% <100.00%> (+<0.01%) ⬆️
Files with missing lines Coverage Δ
key-wallet-manager/src/process_block.rs 90.82% <ø> (ø)
...et/src/transaction_checking/transaction_context.rs 70.21% <100.00%> (+6.11%) ⬆️

... and 5 files with indirect coverage changes

bfoss765 added a commit to bfoss765/rust-dashcore that referenced this pull request Aug 5, 2026
…efore-funding (dashpay#649)

A spend processed BEFORE the transaction that funded the UTXO it spends
(out-of-order block delivery during a cold rescan) leaves that UTXO
permanently in the wallet's tracked set, producing phantom spendable
balance.

Device evidence (testnet): output
2febe5d7e8ad1dd0fb633004a82a24783d9b2e9095883541576e5f1344eb9975:0
(1,000,000 duffs) is counted unspent by the SDK while dashj has it spent,
and the phantom +0.01 survives a full wallet rebuild from seed (fresh
re-derivation + rescan reproduces it deterministically), proving the miss
lives in the scan/processing path, not just live mempool ingestion.

This test models that scenario at the WalletManager level: fund 1,000,000
duffs to a wallet address, then deliver the spending block (height 200)
BEFORE the funding block (height 100). It asserts the funding outpoint is
NOT still tracked afterward. FAILS on the current pin (which already
contains dashpay#837/dashpay#864/dashpay#891/dashpay#893) — those do not address this defect.

Refs: dashpay#649

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
QuantumExplorer added a commit that referenced this pull request Aug 6, 2026
…nspent balance (#649) (#909)

* test(key-wallet-manager): deterministic repro of out-of-order spend-before-funding (#649)

A spend processed BEFORE the transaction that funded the UTXO it spends
(out-of-order block delivery during a cold rescan) leaves that UTXO
permanently in the wallet's tracked set, producing phantom spendable
balance.

Device evidence (testnet): output
2febe5d7e8ad1dd0fb633004a82a24783d9b2e9095883541576e5f1344eb9975:0
(1,000,000 duffs) is counted unspent by the SDK while dashj has it spent,
and the phantom +0.01 survives a full wallet rebuild from seed (fresh
re-derivation + rescan reproduces it deterministically), proving the miss
lives in the scan/processing path, not just live mempool ingestion.

This test models that scenario at the WalletManager level: fund 1,000,000
duffs to a wallet address, then deliver the spending block (height 200)
BEFORE the funding block (height 100). It asserts the funding outpoint is
NOT still tracked afterward. FAILS on the current pin (which already
contains #837/#864/#891/#893) — those do not address this defect.

Refs: #649

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(key-wallet): track wallet-level observed_spent_outpoints to fix #649

Root cause: the "already spent" guard in
managed_core_funds_account.rs::update_utxos keys off the ACCOUNT-LOCAL
`spent_outpoints` set, which is only populated when the account itself
processes the spending transaction. When a spend is delivered before its
funding tx (out-of-order rescan), the wallet does not yet own the input,
so the spend is classified as irrelevant, update_utxos never runs for it,
and nothing records the spend. When the funding tx is processed later, the
output is (re-)inserted as a fresh, spendable UTXO -> phantom balance that
survives a full from-seed rescan.

Fix (adapted from #851): record every spend observed
in a block into a new wallet-level `observed_spent_outpoints` map
(ManagedWalletInfo), independent of the spending tx's classification or
account attribution. update_utxos and record_transaction consult this map:

  - update_utxos skips any output already observed spent (spend-first
    ordering: funding arrives after the spend).
  - remove_spent_from_accounts drops a coin the matched-account path
    missed (funding-first ordering: spend routed to another account).
  - TransactionRecord::compensate_for_observed_spends keeps net_amount /
    output_details consistent with the observed spend (declarative, so
    it is idempotent across rescan replays).

The set is bounded-permanent: entries are evicted by
prune_finalized_observed_spends once the spend height is provably final
(<= min(chainlock height, synced_height)); add-account rewinds the sync
checkpoint so a late account gets filter coverage before pruning can run.
A dash-spv commit-time contiguity guard keeps a mid-flight account-add
rescan from being silently clobbered forward.

Also fixes AddressPool::prune_unused to clear script_pubkey_index
alongside address_index.

Adds manager-level regression tests (multi-wallet, large-block stress)
that exercise the fix through the public WalletManager API.

The repro test from the previous commit now passes; key-wallet (549),
key-wallet-manager (all) and dash-spv lib (482) suites are green. The
pre-existing masternode-network integration failures
(test_utils/masternode_network.rs:106) are unrelated and fail identically
on the clean pin.

Refs: #649
Adapted-from: #851

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test(key-wallet): cover observed_spent guard branches for #649 patch coverage

Codecov flagged the #649 fix's previously-uncovered branches: the wallet-level
`observed_spent_outpoints` serde adapter, finality-boundary pruning, the
funding-first removal guard, the account-add sync rewind, and the AddressPool
`script_pubkey_index` prune fix. The manager-level integration tests only drive
the spend-first ordering end-to-end, leaving these reachable only from the
crate-internal `pub(crate)` surface.

Add `key-wallet/src/tests/observed_spent_outpoints_tests.rs` (the sibling file
already referenced by observed_spent_large_block_stress_test.rs) with five
white-box tests, plus one AddressPool prune test:

  - observed_spent_outpoints_survive_serde_round_trip: exercises the
    (OutPoint, height) sequence serde adapter (serialize + deserialize visitor)
    and the empty-map / `#[serde(default)]` path, isolated on an account-less
    wallet so the populated-account `script_pubkey_index` JSON-key blocker does
    not apply.
  - prune_finalized_observed_spends_respects_finality_boundary: no-op without a
    chainlock; otherwise evicts exactly entries at/below
    min(chainlock height, synced_height), keeping the rescan case (chainlock
    above sync checkpoint) from over-pruning.
  - funding_first_guard_removes_held_coin_and_compensates_record: the un-gated
    remove_spent_from_accounts / finalize_guard_removed_utxo path — coin dropped,
    reservation released, funding record compensated to net 0; idempotent;
    coinbase skipped.
  - wallet_level_set_outlives_account_local_reload: the account-local
    spent_outpoints derived set (rebuilt from recorded txs via
    simulate_reload_rebuild_spent_outpoints) forgets an unrecorded spend, but the
    persisted wallet-level set still prevents resurrection on funding re-delivery.
  - adding_account_from_xpub_rewinds_sync_checkpoint: standalone-account add
    collapses synced_height to birth_height - 1; a still-behind checkpoint is
    left untouched.
  - prune_unused_clears_script_pubkey_index (address_pool_tests.rs): regression
    guard for the missing script_pubkey_index.remove in AddressPool::prune_unused.

key-wallet lib (554) and key-wallet-manager (all) suites green.

Refs: #649

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* review fixes: gate net_amount recompute, surface observed-spend state modification, tighten deser cap

- compensate_for_observed_spends: only replace the match-derived net_amount
  when the compensation actually dropped an output detail, keeping the
  no-observed-spend path byte-identical to pre-#649 behavior; pinned by a
  new unit test.
- record_observed_spends: report whether the persisted observed-spent map
  actually changed, and surface that as state_modified in
  check_core_transaction — a consumer persisting only on reported
  modifications must not lose a recorded spend across a restart; pinned by
  a new regression test (new spend reports, unchanged redelivery and
  mempool spends do not).
- Replace a comment reference to a nonexistent test with the inline
  rationale for why input_details and account_match.sent populate together.
- Tighten MAX_OBSERVED_SPENT_OUTPOINTS 10M -> 1M (load-time allocation cap
  from a few hundred MB to a few tens of MB), still far above any
  legitimate size.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* review fixes round 2: generation guard for mid-flight account adds, surface guard-rewritten records, restore public account API

Addresses three external review findings on top of PR #909:

1. [P1] The commit-time contiguity guard could still certify unscanned
   coverage for a newly added account: a rewind landing INSIDE a scanned
   batch's range passed the height check, and an account add that moved
   no heights (checkpoint already at the birth floor) was undetectable
   by any height comparison. ManagedWalletInfo now carries an in-memory
   account_generation counter bumped on every account add (even
   height-invisible ones); filter scan snapshots it per wallet and
   commit refuses to advance a wallet whose generation changed since
   scan. Pinned by three new dash-spv tests including the mid-batch
   rewind repro (9000 -> scan [5000..9999] -> rewind 7499 -> commit
   keeps 7499) and the unmoved-checkpoint case.

2. [P2] The funding-first guard rewrote funding records without ever
   surfacing them: remove_spent_from_accounts now returns
   post-compensation clones of every rewritten record, the checker adds
   them to updated_records on both the relevant and irrelevant paths,
   and the manager propagates updated_records independent of
   is_relevant, so consumers persisting per-record updates see the
   rewrite.

3. [P2] ManagedAccountRefMut::record_transaction/confirm_transaction had
   silently gone pub -> pub(crate) with changed signatures. The public
   methods are restored with their original signatures (recording with
   no observed-spend context, the pre-#649 behavior); the checker uses
   new pub(crate) *_with_observed_spends variants.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test(key-wallet): pin born-fully-spent recovered transactions stay in history (#649/#846)

HashEngineering reported (against #851/#866) that a funding transaction
recovered after its spend was already observed -- every wallet-relevant
output already in observed_spent_outpoints before the funding is applied --
was dropped from history entirely: the spend-first path made it come out
not-relevant, so no TransactionRecord and no detection event were produced,
while balance and UTXO set stayed exact. That record-loss "belongs in #851";
#851 is superseded by #909.

Investigation of #909 shows its core commit (ebcd40a) already implements the
suggested remedy, so no behavior change is needed:

  - relevance in check_transaction_for_match is address-membership based and
    is never gated on spent-status, so a fully-spent funding tx is still
    classified relevant;
  - ManagedCoreFundsAccount::record_transaction unconditionally inserts the
    record after TransactionRecord::compensate_for_observed_spends zeroes the
    already-spent outputs (net 0, no UTXO);
  - the "never insert already-spent value" guard lives in update_utxos (UTXO
    insertion only), not in recording.

The QuantumExplorer "surface updated records independent of relevance" review
fix covers the separate funding-first UPDATE case (remove_spent_from_accounts
rewriting an existing funding record); the born-fully-spent NEW-record
insertion is covered independently by the record_transaction compensate path.

The existing observed-spent tests assert only UTXO/balance, leaving the
history-record guarantee uncovered. This adds that coverage: two tests pin
that a born-fully-spent recovered tx -- a plain funding tx, and a CoinJoin-
style intermediate hop that spends a live coin -- is surfaced as a new record
and recorded in the account's transaction history, while balance and UTXOs
stay at zero. Verified across InBlock and InChainLockedBlock (chainlocked
recovery) contexts and the WalletManager block path during investigation.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs(key-wallet): fix private intra-doc links so the Documentation CI job passes

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test(key-wallet-manager): parameterize network in the spend_tx test helper

CodeRabbit: the shared `spend_tx` helper hardcoded `Address::dummy(Network::Testnet, ..)`,
forcing every observed-spend test onto Testnet (coding guideline: never hardcode
network parameters in a shared helper). Add a `network: Network` parameter and
thread it into `Address::dummy`; each caller passes the network its manager uses.

All key-wallet-manager tests pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* refactor(key-wallet): reduce #649 fix to the two-piece observed-spend mechanism

Applies ZocoLini's requested simplification (PR #909 review): the #649
out-of-order-spend fix is the two-piece mechanism only —

  (a) record every input seen in a block-context tx into
      `observed_spent_outpoints`, independent of classification
      (`wallet_checker.rs`), and
  (b) in `update_utxos`, skip inserting an output whose outpoint is already
      in that map (`managed_core_funds_account.rs`).

Removes the separate unattributable-spend compensation machinery that was
bundled in, which also made transaction history order-dependent (a receive
delivered before its spend was rewritten to a 0-value entry, erasing it from
history — a spend delivered first leaves it intact):

- `TransactionRecord::compensate_for_observed_spends` and its unit tests
- `ManagedWalletInfo::remove_spent_from_accounts` (both call sites in
  `check_core_transaction`) and its `finalize_guard_removed_utxo` helper
- the now-dead account-local helpers `mark_outpoint_spent`,
  `release_reservation_for`, and the test-only
  `simulate_reload_rebuild_spent_outpoints`
- the `updated_records`-independent-of-relevance change in
  `WalletManager` (its only source was the removed funding-first guard)

The `record_transaction_with_observed_spends` / `confirm_transaction_with_observed_spends`
pair is kept: it is the plumbing that delivers the wallet-level observed map to
`update_utxos`, i.e. piece (b) itself.

A born-fully-spent funding tx is still recorded in history; its already-spent
output is simply never (re-)tracked as a UTXO (balance/UTXO correctness comes
from piece (b), not from rewriting the record). Tests updated to pin that the
receive is preserved in history rather than erased.

Test-helper cleanup (`key-wallet-manager/tests/common/mod.rs`): drop the
superfluous `Network` parameter from `spend_tx` (every caller passed Testnet and
the payee's network is irrelevant to observed-spend logic) and build the external
payee script directly, so the helper needs no network at all.

All three observed-spend integration tests (incl. the deterministic repro) and
`cargo test -p key-wallet --lib` pass; workspace clippy (`-D warnings`, debug +
release) and rustfmt are clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* test(key-wallet-manager): name the out-of-order repro after the invariant it pins

The test asserted `!still_tracked` — the funding UTXO must NOT remain in the
tracked set once its spend was observed first — but was named
`..._leaves_utxo_permanently_tracked`, i.e. after the #649 bug rather than
after the pinned behaviour. A failure therefore read as the expected outcome.
Rename to `..._does_not_leave_utxo_tracked`; assertions and scenario are
unchanged.

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: Quantum Explorer <quantum@dash.org>
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.

key-wallet: retained TransactionRecord/BlockInfo carries no in-block position — same-block provider updates can't be ordered

1 participant