Skip to content

fix(wallet-storage): validate transaction bodies during bulk history load - #5781

Open
Claudius-Maginificent wants to merge 231 commits into
v5.2-devfrom
fix/sqlite-bulk-history-identity
Open

Claudius-Maginificent wants to merge 231 commits into
v5.2-devfrom
fix/sqlite-bulk-history-identity

Conversation

@Claudius-Maginificent

@Claudius-Maginificent Claudius-Maginificent commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Basic explanation

TL;DR: Reject corrupted stored transaction bodies when loading wallet history.

Value: Wallet history uses consistent transaction contents and identifiers.

Risks: Corrupt records previously accepted by bulk loading are rejected. Existing Recovery handling may omit the affected wallet. No consensus change.

User story

As a wallet user, I want corrupted transaction records to be rejected so my history is not reconstructed from inconsistent data.

Scenario

A saved transaction's contents or recorded identifier changes after the database has already been upgraded. Currently, loading the wallet's complete history can accept that inconsistent record. The loader should reject it in both strict and recovery modes.

Detailed discussion

Issue being fixed or feature implemented

The bulk history loader bypasses the body-to-identifier check used by point reads and accounting migration. This is a focused follow-up stacked on #5150, based on its published f8d15b06e44c76820fcca91310bed45bd3e00161 head.

What was done?

Route the single core_state::load_state decoder call through existing decode_transaction_record. Valid-body typed-column drift retains its existing Strict/Recovery policy. The existing outer Recovery boundary can still omit a failed wallet and report degradation; it cannot reconstruct that wallet from a body-invalid record. No schema, migration, write-path, dependency or ABI change.

The shared decoder inherits #5150's narrow historical AssetUnlock read adapter. Bulk reads preserve those old identifiers and exact bytes; incoming writes still require current canonical identifiers. This PR changes only the bulk caller and its regression tests relative to #5150.

How Has This Been Tested?

Two regressions fail on the original caller and pass after the change. Each uses a migrated in-memory database, a valid-read control, body and embedded-identifier tampering, and unchanged-blob assertions after rejected reads. Five existing point/bulk column-drift tests also pass. A bulk-load test reads both immutable old-writer fixture records under Strict and Recovery and checks their exact bytes, bodies and identifiers. Parent migration, historical-tamper and canonical-write controls also pass (11/11 selected, offline).

Scoped formatting and whitespace checks pass. Scoped all-target/all-feature CI Clippy passes with --no-deps -D warnings (locked/offline).

Breaking Changes

No public API or serialized-format changes. Inconsistent record bodies now fail bulk decoding under both load policies.

Checklist:

  • I have performed a self-review of my own code
  • My Rust changes follow the repository coding conventions
  • I have added relevant tests
  • Independent review of the feature and updated parent integration

No GroveDB structure, system contract, versioned consensus generation or durable documentation changes apply. Milestone assignment remains with the coordinator.

🤖 Co-authored by Claudius the Magnificent AI Agent

PR Hygiene · b85a8a5

  • Bots — thepastaclaw requested changes — dismiss the review or push a fix, 1 thread unresolved — resolve it
  • Self-review — not asked of a bot author
  • Build failed
  • Approvals
    • wallet-storage (packages/rs-platform-wallet-storage/src/sqlite/schema/core_state.rs) — lklimek

When every merge requirement is met, the PR Hygiene check passes. Reviewer limits do not block merging; other required GitHub checks and protections still apply.

PastaPastaPasta and others added 30 commits September 23, 2026 11:37
Bump every rust-dashcore git dependency from e4208c90 to 719de34b (dashpay/rust-dashcore dev, the merge of #1049). The breaking change in range is rust-dashcore #1042: secp256k1 0.30 -> 0.33, rand 0.9, getrandom 0.4, and the secp256k1 context argument dropped API-wide. The range also carries key-wallet fixes #1035 (DIP-15 contact pools extend as payments arrive), #1009 (provider transactions consult every fund-bearing account), #1048, and test-only #920/#1038.

Mechanical call-site migration: drop Secp256k1 contexts (derive_priv/from_priv/public_key/from_secret_key/Keypair::new take no context); secp.sign_ecdsa/recover_ecdsa/verify_ecdsa become methods on the key/signature; SecretKey::from_slice/from_byte_array become from_secret_bytes (slices go through <[u8; 32]>::try_from mapped to Error::InvalidSecretKey, the error from_slice returned); secret_bytes -> to_secret_bytes; SecretKey/SharedSecret lost AsRef<[u8]>, so use as_secret_bytes; thread_rng -> rng; StdRng::from_entropy -> from_os_rng. The secp256k1::hashes re-export is gone, so hex rendering uses the hex crate (same output).

Seeded RNG bridging (rand 0.8 caller -> secp's rand 0.9 StdRng) now fills a 32-byte seed and calls from_seed, which is what rand_core 0.6 from_rng did, so seeded keys are unchanged; drive-abci tests that feed seed_from_u64 into Keypair::new use the secp re-exported StdRng (seed_from_u64 and ChaCha12 are identical across rand_core 0.6/0.9).

rs-platform-encryption moves its own secp256k1 to 0.33.1 so only one secp256k1 is in the graph. simple-signer takes a direct rand 0.8 dep for the RngCore its callers pass. rs-dpp enables getrandom 0.3/0.4 wasm_js on wasm32-unknown-unknown, which the new transitive getrandom versions require there. Rust toolchain already at 1.98.1, matching rust-dashcore.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Build on rust-dashcore PR #979 and account-local correction/event fixes at ed4c02e119898f1bb510cf79abc8a125a9bc9bea. Cargo metadata --locked validates the pin; wallet integration checks follow with the storage changes.
Repair fully resolved history from durable TXOs at round commit and wallet
load, preserve funded asset-lock accounting during context-only recovery,
and scope history presentation to the selected wallet.

Swift tests require macOS tooling and were not executable on this host.

<sub>🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent</sub>
Use one ownership predicate for persisted inputs, outputs, and scoped
history amounts, including legacy watch-only TXOs. Preserve accountless
rows with known wallet ownership.

<sub>🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent</sub>
Index historical inputs to repair late funding and existing SQLite history,
preserve known ownership across partial replay, and project corrected bridge
topology without double-counting account snapshots. Keep unknown credit
verdicts from inventing spends or resurrecting coins.

Co-Authored-By: Codex GPT-6 <noreply@openai.com>

<sub>🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent</sub>
Replace the PR 979-based dependency with the isolated accounting correction
backported onto the original e4208c90 base. The identical upstream patch is
available on current rust-dashcore dev without SPV or address-pool changes.

Validated 24 Core storage and 132 changeset tests with the new pin; scoped
Clippy with CI flags and formatting checks pass.

Co-Authored-By: Codex <noreply@openai.com>
#5043 introduced PLATFORM_V15 and made it latest, but the committed
grovedb-structure.json and one structure test still expected protocol 14,
so `structure::tests` has failed on v4.3-dev since it merged:

- should_match_committed_grovedb_structure_json: regenerate the snapshot.
  Only the origin labels and latest_protocol_version change (14 -> 15);
  v15 has the same state structure as v14.
- should_record_a_contract_layer_with_its_documents_on_top: read the
  expected origin from PlatformVersion::latest() instead of a hard-coded
  14, so the next protocol bump does not break it again.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
infraclaw: install the reviewed publisher prerequisite only; no application or runner changes.
…sync

Replay persisted confirmed transactions through the wallet checker on load,
retain only persisted unspent outputs not consumed by confirmed history,
and restore finality before sync checkpoint pruning. Use dash-async for
synchronous persistence loading.

Cover restart, stale projections, funding redelivery, finality, same-block
spends, reserved inputs, and synchronous/current-thread/multi-thread callers.

Co-authored-by: Codex <noreply@openai.com>

<sub>🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent</sub>
…count

The load projection parks an unattributable UTXO in the first funds
account; replaying confirmed history then credits the same outpoint to
its real owner. The post-replay filter only checked a wallet-wide
unspent set, so both copies survived and update_balance double counted.

Drop the load-time fallback copy whenever replay credits the outpoint to
a different funds account, while still refusing to re-credit anything
persistence excluded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…dable

Pre-fix builds persisted DashPay contact (watch-only) outputs as unspent
core_utxos rows. V019 re-labels their history as Sent but left the rows
unspent, so load routed them to the first funds account: the balance
included coins the wallet cannot sign and coin selection could pick one.

load_state now skips unspent contact-only rows. The rows are kept in
SQLite (no destructive migration step), so the exclusion is reversible.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…eset

loadWalletList saved or rolled back the background context
unconditionally after reconciling transaction accounting. Inside an open
begin/endChangeset round that committed half of the round or silently
discarded its staged writes while the round still reported success.

Guard the pass with !inChangeset like every other load-time writer; the
round reconciles its own dirty rows and the next load runs the full pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…PV15

#5057 folded DRIVE_ABCI_QUERY_VERSIONS_V3 into V2 (identical values) and
deleted v3.rs; PV14 was updated but PV15, introduced on v4.3-dev, still
imported V3, so the v4.2-dev -> v4.3-dev merge stopped compiling.
Pure rename; the selected query versions are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The v4.2-dev -> v4.3-dev merge (704307e) left an unused import and
stray blank lines in dpns.rs, failing clippy -D warnings and rustfmt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The structure generator describes PlatformVersion::latest(), which is 15
once PLATFORM_V15 compiles. The committed JSON and a hard-coded
"@14" assertion were stale. Regenerated the JSON (version labels only)
and derive the expected fixture origin from PlatformVersion::latest().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
 review (#5208)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
…, document V019

- V019 `read_record` now queries `length(record_blob)` on its own and fetches
  the blob only after `check_size` passes. SQLite evaluates every result
  column before returning a row, so the old single `SELECT length(b), b`
  loaded an oversize blob into SQLite's heap before the check could run.
- SCHEMA.md: add the V019 migration row and sections for
  `core_transaction_inputs` and `core_transaction_record_originals`, list both
  among the not-yet-diagrammed tables, and name all three writers of
  `core_utxos.spent_in_txid` (apply_sweep, runtime history repair, V019 repair).
- Tests: import `migrations` at the top of the core_state and legacy_v019 test
  modules instead of calling `crate::sqlite::migrations::run` inline.
- Swift tests: build `PersistentTransaction` fixtures with `CoreDirectionCode`
  instead of raw direction integers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cing wholesale

coalesce_account_records kept a later record's body but copied in the
earlier record's higher-ranked context, fabricating a record whose
finality came from a different observation. The higher context_rank
record now wins intact; on equal rank the later one wins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…k fetch

The load path now runs the accounting reconcile before restoring unspent
TXOs, so the fault-injection test observes three more reads ahead of the
faulted PersistentAssetLock fetch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`upsertTransaction` writes a never-pruned pending input for every input
whose prevout has no local TXO, including every foreign input of an
ordinary incoming payment, so `netAmount(for:)` returned nil ("Amount
unavailable") for received payments and hid provider names and fees.

The stored scalar is the sole participating wallet's own accounting, so
it now answers ahead of the pending-input check; that check only makes
the TXO-computed multi-wallet amount provisional, and only for pending
inputs the queried wallet recorded. Participation includes
`involvedAccounts`, so a payload-only row recorded by one wallet keeps
its stored amount and a row recorded by two wallets never hands one of
them the other's scalar.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018LN7YPS4eaKdQv5XFUBPLH
Pin `direction(for:)` and `netAmount(for:)` for an A→B transfer between
two local wallets, and when "Amount unavailable" is expected for rows
without linked TXOs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018LN7YPS4eaKdQv5XFUBPLH
… a V019 rescan

V019's corruption recovery subtracted one from the raw stored
`birth_height`, so `i64::MIN` panicked with subtract-overflow inside
`migrations::run`. Gate it through `i64_to_u32` (typed IntegerOverflow,
the whole upgrade rolls back) and use `saturating_sub(1)`. Out-of-range
heights are no longer silently clamped to a rescan from genesis.

The runtime repair in `schema::core_history` never reads `birth_height`,
so it has no equivalent to fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018LN7YPS4eaKdQv5XFUBPLH
Upstream key-wallet classifies each account on its own. For an asset
lock with no change output, the funding account says `Outgoing`, because
the OP_RETURN burn is not an owned output. The keys account says
`Internal`. The live fold kept the funding verdict, while the SQLite
repair, the frozen V019 migration and Swift all write `Internal`. As a
result, the same row flipped whenever storage repaired it.

The fold also summed the keys account's `+credit` marker net into the
wallet net. Live asset-lock rows therefore read `-fee`, while every
repair path wrote `-(credit + fee)`, with or without change (ARCH-002).
A test reproduced this before the fix (folded net -1000 = -fee).

Changes:
- Add one wallet-level accounting rule in
  `changeset::wallet_accounting`:
  - `wallet_direction` (public) is the rule the repair already used.
  - `apply_wallet_accounting` sets net to owned outputs minus owned
    inputs, and sets direction via `wallet_direction`.
- `fold_same_txid_records` now applies that rule to every record,
  folded or single, after the per-txid merge. A funding slice that never
  meets its keys marker is normalised too.
- Records with no details (keys markers only) keep upstream's net and
  direction, mirroring repair's "empty metadata is not evidence".
- The storage runtime repair calls `platform_wallet::changeset::wallet_direction`
  instead of its own copy. The Swift-shared case table stays in storage
  under the same name.
- The frozen V019 logic is untouched.
- `test_support::fold_wallet_records` exposes the live fold, so storage
  can pin repair parity against it.

No existing test pinned the old live behaviour, so none needed updating.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018LN7YPS4eaKdQv5XFUBPLH
…ansactions

The wallet-level accounting pass runs on every projected record,
including singles, so this regression table shows that the live
projection's net and direction are unchanged for every other transaction
shape.

The table covers 14 cases:
- plain receive
- send with and without change
- self-transfer within one account and between two accounts
- CoinJoin
- provider registration, with an own and with an external collateral
- asset unlock
- coinbase
- payment to a contact, with the watch-only slice present
- payment from a contact into DashPay receiving funds
- a single keys marker
- a keys-only group

The expected values were derived by running the table against the
pre-pass fold (a70ee60); all 14 pass there.

One case changes on purpose. A group made only of keys-account markers,
with no details, used to re-derive `Incoming` from its empty details. It
now keeps upstream's `Internal`, which matches a single marker and the
SQLite repair (that repair skips detail-less records).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018LN7YPS4eaKdQv5XFUBPLH
The Rust repair's local `repaired_direction` was replaced by the shared
`platform_wallet::changeset::wallet_direction`, which both the live
projection and the SQLite repair use. The comment is the only change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018LN7YPS4eaKdQv5XFUBPLH
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Keep one transaction body and its computed identifier while varying the
snapshot metadata checked by the whole-record selection regression.

<sub>🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent</sub>
Inherit the published accounting fixes and historical identity decoder
before validating the separate bulk-history read path.

<sub>🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent</sub>
Exercise the inherited old-writer AssetUnlock fixture through bulk load
under Strict and Recovery without rekeying or rewriting record bytes.

<sub>🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent</sub>
@coderabbitai

coderabbitai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@thepastaclaw

thepastaclaw commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

⛔ Final review complete — 1 blocking finding(s) (commit b85a8a5) · triage: low

lklimek and others added 9 commits November 6, 2026 10:25
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ashcore bump

The rust-dashcore update merged from v5.1-dev makes the byte-array
constructors and accessors inherent on the hash newtypes, so the
`dashcore::hashes::Hash` imports added on this branch are unused and
fail clippy with -D warnings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ash import

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A new variant must break every downstream match that has not classified
it. #[non_exhaustive] forced a panicking wildcard arm into the external
classification test, which silently absorbed later variants. Restore the
wildcard-free match there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WunYYijwZNbw3EjHtdMAHC
Add a coding-conventions rule, mirrored in AGENTS.md: the workspace's own
error enums are not #[non_exhaustive], and per-variant matches over them
list every variant, so a new variant breaks the build at each caller.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WunYYijwZNbw3EjHtdMAHC
Brings in #5762, which takes migration V020 for the asset-lock shielded
recipient. The core transaction accounting migration moves to V021:
file, module, typed-hook version, test rewind and version asserts,
SCHEMA.md rows and schema pinning fingerprints.

Upstream bumps rusqlite to 0.41; the rusqlite dev-dependency used for
migration fault injection follows it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WunYYijwZNbw3EjHtdMAHC

<sub>🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent</sub>
Base automatically changed from fix/pr-5126 to v5.2-dev November 17, 2026 06:42
@github-actions github-actions Bot added this to the v5.2.0 milestone Nov 17, 2026
lklimek and others added 2 commits November 17, 2026 08:00
Conflicts:
- packages/rs-platform-wallet/src/changeset/core_bridge.rs: two test
  PlatformWalletInfo literals; took v5.2-dev's added
  `finalized_spends: Default::default()` field (#5954).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WunYYijwZNbw3EjHtdMAHC
No conflicts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WunYYijwZNbw3EjHtdMAHC
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Waiting for bot review — thepastaclaw not yet. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.

@github-actions github-actions Bot added the waiting-bots Waiting for the review bots to report on this head label Nov 19, 2026

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final validation — Phase 1 + Phase 2

The bulk loader now checks stored transaction bodies against their recorded IDs while preserving historical AssetUnlock IDs and the existing column-drift policy. One blocking case remains: a malformed record can deserialize successfully and then panic during hashing, bypassing Recovery’s per-wallet isolation. Rust wallet, Kotlin SDK, and Swift SDK checks passed; this review was static.

🔴 1 blocking | 🟡 1 suggestion(s) | 💬 1 nitpick(s)

1 finding(s) not shown inline (the lines are not part of this PR's diff)

💬 Nitpick: Document the difference between body validation and column drift
packages/rs-platform-wallet-storage/src/sqlite/schema/core_state.rs:919-921

The transaction-record bullet mentions only the typed-column comparison. A body-to-ID mismatch outside the historical AssetUnlock exception returns before ctx.tolerate, so it fails the wallet load under either policy even though Recovery can tolerate typed-column drift for a valid record. State that distinction alongside the other reconstruction failure semantics.

/// - **Transaction records**: height-only rows supply UTXO confirmation
///   metadata but are not emitted as records. Blob-bearing rows must decode
///   and carry an ID consistent with their body, allowing historical
///   AssetUnlock instance hashes. Decode or body-to-ID errors fail the wallet
///   load under either policy; typed txid and height drift follows the load
///   policy separately.

source: glm-5.3-flash (phase1-reviewer: general, rust-quality)

Review provenance

Source: reviewer 1: glm-5.3-flash (agent: phase1-reviewer, role: general); reviewer 2: glm-5.3-flash (agent: phase1-reviewer, role: rust-quality); reviewer 3: gpt-6.2-sol (agent: phase2-reviewer, role: general); reviewer 4: gpt-6.2-sol (agent: phase2-reviewer, role: architecture-layering); reviewer 5: gpt-6.2-sol (agent: phase2-reviewer, role: platform-versioning); reviewer 6: gpt-6.2-sol (agent: phase2-reviewer, role: rust-quality); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)

  • Triage: low by gpt-6.2-sol (effort low) — The only production change routes one bulk-history decoder call through an existing validator; the rest of the diff adds regression tests.
  • Phase 1 reviewers: glm-5.3-flash — general (completed, effort high); agent phase1-reviewer, glm-5.3-flash — rust-quality (completed, effort high); agent phase1-reviewer
  • Phase 1 model: glm-5.3-flash — zai quota: 5h 90% left, weekly 58% left; passed over gemini-3.8-flash-high (antigravity quota lookup failed)
  • Fresh verifier: gpt-6.2-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.2-sol — general (completed, effort medium); agent phase2-reviewer, gpt-6.2-sol — architecture-layering (completed, effort medium); agent phase2-reviewer, gpt-6.2-sol — platform-versioning (completed, effort medium); agent phase2-reviewer, gpt-6.2-sol — rust-quality (completed, effort medium); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review (PastaClaw). How to work through them:
- Verify each finding against the code; fix only what is real, as narrowly as the finding.
- If a finding is wrong, decline it on its thread in one or two sentences; the bot answers once and stops.
- To defer one, link a tracking issue on its thread (or reply asking the bot to file one).
- Suggestions don't block the merge, but most are worth fixing in this PR; judge each on its merits.
- Keep the PR's scope; don't grow it to satisfy hardening suggestions.
- Carried items were raised in an earlier round: read the thread first, and don't re-fix what you already fixed, declined or deferred there. Disputed and deferred items are already handled and are not listed.

In `packages/rs-platform-wallet-storage/src/sqlite/schema/core_state.rs`:
- [BLOCKING] packages/rs-platform-wallet-storage/src/sqlite/schema/core_state.rs:970: Bulk identity validation can panic before Recovery isolates the wallet
  The new bulk decode calls `ensure_current_transaction_identity`, which hashes the transaction before it can return `BlobDecode`. In both the pinned and default rust-dashcore revisions, Serde accepts a v3 provider registration payload with `payouts` or `shared_collateral` set to `None`, but the consensus encoder rejects that payload with `InvalidInput`. `Transaction::txid()` delegates to `instance_hash()` for this transaction, and `instance_hash()` panics on that error at `expect("engines don't error")`. The old bulk reader did not hash the record at this point. Neither load policy can handle the panic as a typed error, so Recovery cannot report the damaged wallet and continue loading healthy siblings. Add fallible transaction ID and instance-hash APIs in rust-dashcore, map their encoding errors to `BlobDecode` in the shared decoder, and add a regression for a record that deserializes but cannot be hashed.
- [SUGGESTION] packages/rs-platform-wallet-storage/src/sqlite/schema/core_state.rs:2523-2579: Test that Recovery isolates a wallet with a body-to-ID mismatch
  The new tests establish that `load_state` returns `BlobDecode` and leaves the rejected blob unchanged, but they do not exercise the user-visible result through `SqlitePersister::load()`. The existing integration test checks wallet isolation for a `BlobDecode` from a different reader. Add a two-wallet Recovery case with a body-to-ID mismatch in one wallet and assert that the damaged wallet is absent from the rehydrated wallet map, is reported as `blob_decode`, and does not prevent the healthy wallet from loading.
- [NITPICK] packages/rs-platform-wallet-storage/src/sqlite/schema/core_state.rs:919-921: Document the difference between body validation and column drift
  The transaction-record bullet mentions only the typed-column comparison. A body-to-ID mismatch outside the historical AssetUnlock exception returns before `ctx.tolerate`, so it fails the wallet load under either policy even though Recovery can tolerate typed-column drift for a valid record. State that distinction alongside the other reconstruction failure semantics.
Out-of-scope follow-up suggestions (1)

These are valid observations, but they are outside this PR's scope and should be handled in separate issues or author/maintainer-requested PRs rather than blocking this review.

  • Validate stored transaction bodies before applying spend sweeps — The unchanged sweep readers at core_state.rs:488 and :645 compare the SQL key with the embedded txid but do not validate it against the body. If a blob changes after startup while those IDs still agree, a sweep can use the wrong input set when updating which coins remain spent.
    • Follow-up: Track this separately in wallet storage. Once the shared decoder handles encoding failures without panicking, reuse it in both sweep readers while retaining their SQL-key checks and transactional rollback.

blob::check_size(record_blob_len)?;
let payload: Vec<u8> = row.get(4)?;
let record = blob::decode::<TransactionRecord>(&payload)?;
let record = decode_transaction_record(&payload)?;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Blocking: Bulk identity validation can panic before Recovery isolates the wallet

The new bulk decode calls ensure_current_transaction_identity, which hashes the transaction before it can return BlobDecode. In both the pinned and default rust-dashcore revisions, Serde accepts a v3 provider registration payload with payouts or shared_collateral set to None, but the consensus encoder rejects that payload with InvalidInput. Transaction::txid() delegates to instance_hash() for this transaction, and instance_hash() panics on that error at expect("engines don't error"). The old bulk reader did not hash the record at this point. Neither load policy can handle the panic as a typed error, so Recovery cannot report the damaged wallet and continue loading healthy siblings. Add fallible transaction ID and instance-hash APIs in rust-dashcore, map their encoding errors to BlobDecode in the shared decoder, and add a regression for a record that deserializes but cannot be hashed.

source: gpt-6.2-sol (phase2-reviewer: general, architecture-layering, platform-versioning, rust-quality)

Comment on lines +2523 to +2579
fn assert_bulk_load_rejects_body_identity_mismatch(ctx: &LoadCtx) {
let wallet_id = [0x72; 32];
let conn = wallet_db(&wallet_id);
let record = transaction_record(TransactionContext::Mempool);
conn.execute(
"INSERT INTO core_transactions (wallet_id, txid, record_blob, finalized) \
VALUES (?1, ?2, ?3, 0)",
params![
wallet_id.as_slice(),
record.txid.as_byte_array().as_slice(),
blob::encode(&record).unwrap()
],
)
.unwrap();
let (loaded, _) = load_state(&conn, &wallet_id, dashcore::Network::Testnet, ctx).unwrap();
assert_eq!(loaded.records[0].txid, record.txid);

for change_body in [true, false] {
let mut tampered = record.clone();
if change_body {
tampered.transaction.lock_time += 1;
} else {
tampered.txid = Txid::from_byte_array([0x73; 32]);
}
let payload = blob::encode(&tampered).unwrap();
conn.execute(
"UPDATE core_transactions SET txid = ?1, record_blob = ?2 WHERE wallet_id = ?3",
params![
tampered.txid.as_byte_array().as_slice(),
&payload,
wallet_id.as_slice()
],
)
.unwrap();
assert!(matches!(
load_state(&conn, &wallet_id, dashcore::Network::Testnet, ctx),
Err(WalletStorageError::BlobDecode { .. })
));
let stored: Vec<u8> = conn
.query_row(
"SELECT record_blob FROM core_transactions WHERE wallet_id = ?1",
params![wallet_id.as_slice()],
|row| row.get(0),
)
.unwrap();
assert_eq!(stored, payload, "a failed read must not rewrite history");
}
}

#[test]
fn should_reject_bulk_history_body_identity_mismatch_in_strict_mode() {
assert_bulk_load_rejects_body_identity_mismatch(&LoadCtx::strict());
}

#[test]
fn should_reject_bulk_history_body_identity_mismatch_in_recovery_mode() {
assert_bulk_load_rejects_body_identity_mismatch(&LoadCtx::recovery());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Suggestion: Test that Recovery isolates a wallet with a body-to-ID mismatch

The new tests establish that load_state returns BlobDecode and leaves the rejected blob unchanged, but they do not exercise the user-visible result through SqlitePersister::load(). The existing integration test checks wallet isolation for a BlobDecode from a different reader. Add a two-wallet Recovery case with a body-to-ID mismatch in one wallet and assert that the damaged wallet is absent from the rehydrated wallet map, is reported as blob_decode, and does not prevent the healthy wallet from loading.

source: glm-5.3-flash (phase1-reviewer: general, rust-quality)

@github-actions github-actions Bot added waiting-self-review Waiting for the author to post /self-reviewed and removed waiting-bots Waiting for the review bots to report on this head labels Nov 19, 2026

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

priority: low Low priority — nice to have waiting-self-review Waiting for the author to post /self-reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants