Skip to content

fix(platform-wallet): keep pre-derived lost funding rows ineligible after restart - #6035

Merged
lklimek merged 3 commits into
v5.2-devfrom
fix/funding-pool-lost-row-in-window
Nov 27, 2026
Merged

lklimek merged 3 commits into
v5.2-devfrom
fix/funding-pool-lost-row-in-window

Conversation

@Claudius-Maginificent

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

Copy link
Copy Markdown
Collaborator

TL;DR: After a restart, the wallet no longer hands out an invitation key that was already given away in an earlier voucher, even when the record of that key was lost from storage.

User story

As a wallet user sending identity invitations, I want every invitation to use a fresh key, so that someone holding an older invitation link cannot claim a new one.

Scenario

Base flow

  1. The wallet issues several invitation vouchers; each exports a one-time key.
  2. Storage loses the record of one of those keys (for example, a lost write on a mobile store).
  3. The app restarts and loads the wallet.

Actual behavior

If the lost record sits among the first few keys the wallet always prepares in advance, the wallet treats that key as unused and offers it for the next invitation, so two vouchers share a key.

Expected behavior

Any key at or below the highest one the wallet has recorded, but missing from storage, is treated as used and is never offered again.

Detailed discussion

What was done

  • restore_address_rows (rs-platform-wallet wallet/address_pool_restore.rs) records which indices persisted rows restored. With GapRows::Used (asset-lock funding pools), it then marks used every pool address at or below the cutoff that no row restored, whether pre-derived by ManagedWalletInfo::from_wallet or gap-filled.
  • The cutoff is the highest valid normal index among the persisted rows, counted even when its row is refused. It is computed once and also sets the funding issuance floor in restore_account_address_pools, so a stored row with an out-of-range index is refused without retiring the unused pre-derived addresses.
  • A refused row counts as missing (fail closed). Funds pools (GapRows::Available) and the gap-fill budget are unchanged.
  • The used-marking moved out of fill_gap_below; the final sweep alone marks the addresses a partially failed gap fill derived.

Stacked on #5951; this bug predates the rust-dashcore bump and was split out of it. Rows the FFI decoder rejects are dropped before restore and are not covered here (follow-up).

Testing

  • should_not_reissue_a_lost_invitation_row_below_the_highest_persisted_row: lost index inside and just above the pre-derived window (RED before the fix).
  • should_issue_the_next_invitation_key_despite_an_invalid_persisted_index: a used row plus a u32::MAX row; one refusal, no floor, next invitation key is index 1 (RED before the fix).
  • cargo test -p platform-wallet --lib address_pool_restore passes; clippy for the wallet crate passes with the shielded feature.

Breaking changes

None.

Checklist

  • I have performed a self-review of my own code
  • My Rust changes follow the coding conventions
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation if needed

🤖 Co-authored by Claudius the Magnificent AI Agent

🤖 Generated with Claude Code

https://claude.ai/code/session_01WunYYhpfoE88XJoyRt4hcR

PR Hygiene · 4f39040

  • Bots — thepastaclaw ✓
  • Self-review — not asked of a bot author
  • Build green
  • Approvals
    • rs-platform-wallet (packages/rs-platform-wallet/src/manager/load.rs, packages/rs-platform-wallet/src/wallet/address_pool_restore.rs) — approved by llbartekll

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.

Summary by CodeRabbit

  • Bug Fixes
    • Restored address pools now avoid reissuing missing addresses below the highest valid address already recorded, including addresses that need to be derived during recovery.
    • Invalid persisted invitation addresses are skipped without blocking issuance of the next valid invitation address.

@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Address-pool restoration now reports the highest valid normal persisted index, including refused rows, and marks missing addresses through that cutoff as used when configured. Manager restoration uses the reported index for funding-floor evaluation. New tests cover missing invitation rows and invalid persisted indices.

Changes: Address pool restoration

Layer / File(s) Summary
Track and apply the restore cutoff
packages/rs-platform-wallet/src/wallet/address_pool_restore.rs
Restoration tracks the highest valid normal persisted index, including refused rows. When gap_rows is Used, missing addresses through that cutoff are marked used. Tests verify cutoff reporting, including when gap filling fails or exceeds its budget.
Use the cutoff in manager restoration
packages/rs-platform-wallet/src/manager/load.rs
Funding-floor evaluation uses the cutoff reported by address-row restoration. Invitation-pool tests cover missing rows below the highest used index and an invalid persisted index.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant PoolManager
  participant AddressPoolRestore
  participant AddressPool
  participant FundingFloorStats
  PoolManager->>AddressPoolRestore: restore persisted address rows
  AddressPoolRestore->>AddressPool: validate indices and derive gaps
  AddressPoolRestore->>AddressPool: mark missing addresses through cutoff as used
  AddressPoolRestore-->>PoolManager: return highest_valid_index and restore counts
  PoolManager->>FundingFloorStats: evaluate funding floor using highest_valid_index
Loading

Possibly related issues

  • Issue 6324: The changes cover the host restore cutoff for missing rows below a persisted index. The issue describes remaining cases in SQLite restoration and for a deleted tail row, which this change does not address.

Suggested reviewers: quantumexplorer, shumkov

Merge Risk: Minimal · up to 4f390

After a restart, the wallet no longer reissues an invitation key whose stored record was lost. Only a rare storage-loss state could leave the invitation pool with no available key, and in that case the wallet declines to issue a key instead of reusing an old one. The change is low risk to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the fix and its subject: keeping pre-derived lost funding rows ineligible after restart in platform-wallet. The changeset restores address rows, sets a highest valid index cutoff, and …
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.
Full details: Title check

Explanation

The title names the fix and its subject: keeping pre-derived lost funding rows ineligible after restart in platform-wallet. The changeset restores address rows, sets a highest valid index cutoff, and marks missing pre-derived rows at or below that cutoff as used, which matches the title's intent. Only the restart-related framing is slightly narrow, but the title still describes the main change.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
Fix failing CI checks

  • Commit to this branch
  • Create a new PR
  • 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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between a56fc48 and 4f39040.

Files selected for processing: 2

  • packages/rs-platform-wallet/src/manager/load.rs
  • packages/rs-platform-wallet/src/wallet/address_pool_restore.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@thepastaclaw

thepastaclaw commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Final review complete — no blockers (commit 4f39040) · triage: normal

@lklimek
lklimek added this pull request to stack #6217 November 24, 2026 11:38
@lklimek
lklimek force-pushed the fix/funding-pool-lost-row-in-window branch from a9f4c10 to 838f1a9 Compare November 24, 2026 11:57
Base automatically changed from chore/rust-dashcore-dev to v5.2-dev November 25, 2026 09:19
…fter restart

Preserve the final PR scope, including merge resolutions, on the updated native stack.

Co-Authored-By: Codex <[email protected]>
@lklimek
lklimek force-pushed the fix/funding-pool-lost-row-in-window branch from 838f1a9 to b30b4b5 Compare November 25, 2026 09:19
@github-actions github-actions Bot added this to the v5.2.0 milestone Nov 25, 2026
@lklimek
lklimek marked this pull request as ready for review November 25, 2026 09:19
@github-actions

github-actions Bot commented Nov 25, 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 25, 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

Changes requested: 1 blocker

  • A corrupt stored index can leave the wallet unable to issue invitations after restart — packages/rs-platform-wallet/src/wallet/address_pool_restore.rs:96-98 (new)
    Impact: A wallet user who restarts with an out-of-range invitation row and no valid unused rows in storage cannot send an invitation, even though the wallet pre-derived unused keys.

The patch retires missing funding addresses on restore, including addresses pre-derived at startup, so they cannot be handed out again. Swift and Kotlin SDK checks passed; the Linux Rust wallet tests did not run because GitHub Actions cancelled the original Tests run in favor of a queued run, which was skipped.

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

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

🟡 Suggestion: A partially failed gap fill marks the same addresses used twice
Impact: A wallet loading a partially derivable funding pool scans and marks its newly derived addresses twice, leaving two paths for maintainers to keep consistent.
packages/rs-platform-wallet/src/wallet/address_pool_restore.rs:103-110

The early mark_used_from call no longer changes the result. A partial fill derives only indices below the failed row, starting above the pool's previous highest index. None of those indices was restored earlier, and later rows cannot restore them because the input is sorted. The final sweep therefore marks the same entries, and mark_used is idempotent. Remove the early marking and its helper so the final sweep owns the rule; the existing partial-fill regression checks that these addresses still end up used.

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: rust-quality); reviewer 6: gpt-6.2-sol (agent: phase2-reviewer, role: security-auditor); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)

  • Triage: low by gpt-6.2-sol (effort low) — The fix is confined to wallet address-pool restoration, where one eligibility rule now covers both pre-derived and gap-filled rows, with a regression test for each case.
  • 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 96% left, weekly 23% left; passed over gemini-3.8-flash-high (lane 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 — rust-quality (completed, effort medium); agent phase2-reviewer, gpt-6.2-sol — security-auditor (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/src/wallet/address_pool_restore.rs`:
- [BLOCKING] packages/rs-platform-wallet/src/wallet/address_pool_restore.rs:96-98: A corrupt stored index can leave the wallet unable to issue invitations after restart
  Impact: A wallet user who restarts with an out-of-range invitation row and no valid unused rows in storage cannot send an invitation, even though the wallet pre-derived unused keys.
  The next invitation fails before the wallet builds its asset lock. The Swift loader forwards the stored `UInt32` index unchanged, and the FFI decoder preserves it if the rest of the row decodes. With a valid used row at index 0 and a corrupt row at `u32::MAX`, gap filling refuses the corrupt row, but `rows.last()` has already made it the cutoff. The final sweep marks pre-derived indices 1–4 used; the base left them available. The manager already excludes non-normal indices from the issuance floor, but that does not help because the invitation builder calls `next_address(None)` and cannot derive a replacement. Use one highest valid normal index for both the sweep and the floor. Keep counting invalid rows as refused and keep valid indices in the cutoff even when their rows are refused. Add a restore regression that checks both the refusal count and the next available invitation key.
- [SUGGESTION] packages/rs-platform-wallet/src/wallet/address_pool_restore.rs:103-110: A partially failed gap fill marks the same addresses used twice
  Impact: A wallet loading a partially derivable funding pool scans and marks its newly derived addresses twice, leaving two paths for maintainers to keep consistent.
  The early `mark_used_from` call no longer changes the result. A partial fill derives only indices below the failed row, starting above the pool's previous highest index. None of those indices was restored earlier, and later rows cannot restore them because the input is sorted. The final sweep therefore marks the same entries, and `mark_used` is idempotent. Remove the early marking and its helper so the final sweep owns the rule; the existing partial-fill regression checks that these addresses still end up used.
- [NITPICK] packages/rs-platform-wallet/src/wallet/address_pool_restore.rs:82-85: The restore docs can be read as stopping at the highest refused row
  Impact: A maintainer reading the restore contract can mistake a refused row for the sweep boundary and miss that a successfully restored higher row also establishes that boundary.
  The phrase “the highest row that no row restored” reads as though the cutoff is itself a refused row. In the code, the cutoff comes from the input rows; the successful-restore check applies separately to each address below or at that cutoff. State those two conditions separately here, on `GapRows`, and in the manager's restore docs.
Out-of-scope follow-up suggestions (2)

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.

  • An unreadable highest invitation row can hide an issued key — The existing FFI decoder counts unreadable rows as dropped without forwarding their indices. If a row with a valid index and malformed payload is above every decoded row, neither the sweep nor the issuance floor sees it, and an exported key can remain available after restart. A rejected row below a later decoded row is covered by this patch.
    • Follow-up: Open a follow-up PR to forward identifiable account, pool, and index information for unreadable rows to rs-platform-wallet, where the restore policy belongs. Cover an unreadable highest used row in a load-path regression.
  • A later refill can hide the gap that established an issuance floor — The issuance floor is recomputed on load. If a refill persists available rows that obscure the original gap, a later restart can accept them as restored and lose the earlier protection.
    • Follow-up: Cover the refill-and-restart sequence in #6102.

lklimek and others added 2 commits November 27, 2026 07:29
…valid row index

- Derive one cutoff, the highest valid normal index among the persisted rows
  (counted even when its row is refused), and use it for both the final
  sweep and the funding issuance floor, so an out-of-range stored index no
  longer marks the pre-derived unused window used.
- Refuse rows with an invalid index before any gap fill.
- Drop the early marking after a partial gap fill; the final sweep covers it.
- Clarify the restore documentation of the cutoff.

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

Co-Authored-By: Codex (gpt-6.2-sol) <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Nov 27, 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 waiting-bots Waiting for the review bots to report on this head and removed waiting-self-review Waiting for the author to post /self-reviewed labels Nov 27, 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.

Re-review — Final validation — Phase 1 + Phase 2

New on this head: The follow-up uses the highest valid normal persisted index for both the restore sweep and the funding issuance floor, and the final sweep alone marks missing addresses used. Linux wallet CI passed, including the lost-row and invalid-index invitation regressions. The Swift SDK check failed when the simulator could not launch the example-app test runner, and the Kotlin emulator check failed on a voting-key authentication assertion outside the restore path.

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

Review provenance

Source: reviewer 1: muse-spark-1.3-contributor (agent: phase1-reviewer, role: general); reviewer 2: muse-spark-1.3-contributor (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: rust-quality); reviewer 6: gpt-6.2-sol (agent: phase2-reviewer, role: security-auditor); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)

  • Triage: normal by gpt-6.2-sol (effort low) — The fix is localized, but verifying invitation-key eligibility requires tracing how index validation, refused rows, and partial gap fills affect both the restore sweep and the issuance floor.
  • Phase 1 reviewers: muse-spark-1.3-contributor — general (completed, effort xhigh); agent phase1-reviewer, muse-spark-1.3-contributor — rust-quality (completed, effort xhigh); agent phase1-reviewer
  • Phase 1 model: muse-spark-1.3-contributor — not quota-gated; passed over gemini-3.8-flash-high (antigravity quota lookup failed), glm-5.3-flash (not used above high effort; tier asks max)
  • Fresh verifier: gpt-6.2-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.2-sol — general (completed, effort high); agent phase2-reviewer, gpt-6.2-sol — architecture-layering (completed, effort high); agent phase2-reviewer, gpt-6.2-sol — rust-quality (completed, effort high); agent phase2-reviewer, gpt-6.2-sol — security-auditor (completed, effort high); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify the current code and confirm that no unresolved issues remain.

No unresolved findings remain from the prior review on this head.
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.

  • An unreadable funding row can hide the highest recorded invitation index — The existing FFI loader counts an unreadable row as dropped but passes neither the row nor its index to wallet restore. If that row holds the highest recorded invitation index, the wallet can still offer a key already exported in a voucher. This change does not alter the decoder.
    • Follow-up: In a separate PR, pass recoverable pool and index metadata from unreadable rows to rs-platform-wallet so it can apply the issuance cutoff.

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

Copy link
Copy Markdown
Contributor

Ready for review — rs-platform-wallet: HashEngineering or ZocoLini or llbartekll or romchornyi.
Full checklist in the description.

@github-actions github-actions Bot added the ready-for-human Bots have reported, the author has self-reviewed, and the build is green: this needs a human. label Nov 27, 2026
@Claudius-Maginificent

Claudius-Maginificent commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator Author

Re: review summary, finding "🔴 Blocking: A corrupt stored index can leave the wallet unable to issue invitations after restart"

Duplicate of #6035 (comment)
Tracked in 4f39040

🤖 Co-authored by Claudius the Magnificent AI Agent

@llbartekll llbartekll left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed at 4f39040. The sweep covers the whole pool (entries are merged per pool before restore), funding pools are persisted as whole-pool snapshots so a healthy store is dense below its highest row, and the early index check matches what AddressPool::insert already refused. No issues found.

@github-actions

Copy link
Copy Markdown
Contributor

Policy satisfied — this can merge.
Full checklist in the description.

@github-actions github-actions Bot removed the ready-for-human Bots have reported, the author has self-reviewed, and the build is green: this needs a human. label Nov 27, 2026
@lklimek
lklimek merged commit dbbfb45 into v5.2-dev Nov 27, 2026
54 of 56 checks passed
@lklimek
lklimek deleted the fix/funding-pool-lost-row-in-window branch November 27, 2026 14:11
HashEngineering added a commit that referenced this pull request Nov 27, 2026
…latform-wallet-top-up-someone-elses-identity-fr

Registry: v5.2-dev's rows for 77-81 (#5865) taken; #5931 no longer holds
81, and rows 89 and 90 say the acknowledgement covers every recipient-less
lock. The legacy funding-rows test expects index 0 used: #6035 keeps an
address no row restored below the highest stored row ineligible.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

4 participants