Skip to content

fix(multisig-client): keep local state when GUARDIAN nonce lags instead of throwing#344

Open
WiktorStarczewski wants to merge 2 commits into
OpenZeppelin:mainfrom
WiktorStarczewski:wiktor/fix-syncstate-guardian-lag
Open

fix(multisig-client): keep local state when GUARDIAN nonce lags instead of throwing#344
WiktorStarczewski wants to merge 2 commits into
OpenZeppelin:mainfrom
WiktorStarczewski:wiktor/fix-syncstate-guardian-lag

Conversation

@WiktorStarczewski

@WiktorStarczewski WiktorStarczewski commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

What

Multisig.syncState() threw — and left Multisig.account frozen at its previous snapshot — whenever the GUARDIAN's nonce was <= the local nonce, i.e. whenever the GUARDIAN was simply behind local. refreshConfigFromAccount() runs only after the overwrite branch, so the throw in ensureSafeToOverwriteLocalState() unwound syncState() before this.account was ever refreshed. The UI (which reads Multisig.account) then stayed frozen on the pre-execute state even though the local account was already correct and independently on-chain-verifiable.

This is the normal state during the post-execute canonicalization window (#316) and permanent if the candidate is discarded (#312 / #319).

Change

  • Rename ensureSafeToOverwriteLocalStateisSafeToOverwriteLocalState, returning boolean instead of throwing on the nonce guard.
  • When the GUARDIAN is strictly behind local (incomingNonce < localNonce): return falsesyncState() skips the overwrite, keeps local, and still calls refreshConfigFromAccount(localAccount) so this.account advances and the UI unfreezes.
  • Genuine divergence still throws: same nonce + different commitment, and incoming-vs-on-chain commitment mismatch.

The guard "don't clobber good local state with a lower-nonce GUARDIAN state" is preserved — it is now a skip, not a fatal error. This is the client-side complement to #316: #316 removes the server-side lag; this stops the client freezing during whatever lag remains and handles the permanent-behind (discarded) case gracefully.

Test plan

packages/miden-multisig-client/src/multisig.test.ts (86/86 pass):

  • Rewrote the "incoming nonce lower than local" test: syncState() now resolves, does not call newAccount, needs no on-chain round-trip, and refreshes config from the local account.
  • Updated the equal-nonce / divergent-commitment test: still throws (new message).
  • Added a regression test for the exact repro (local nonce 1, GUARDIAN nonce 0 → Multisig.account advances to the local nonce-1 account).

tsc --noEmit adds no new errors from this change; multisig.ts appears in zero type errors. (The pre-existing guardian-client .code / GuardianErrorMeta errors are unrelated — see #318 — and reproduce on main.)

Closes #343

Summary by CodeRabbit

  • Bug Fixes
    • Improved account synchronization when guardian-provided state is behind the local state.
    • Preserved the local account state and configuration without unnecessary network requests in supported cases.
    • Prevented accounts from remaining frozen after execution when synchronization detects an outdated guardian nonce.
    • Continued to detect and report genuine state conflicts, including commitment mismatches.

…ad of throwing

syncState() threw in ensureSafeToOverwriteLocalState whenever the GUARDIAN's
nonce was <= local, and refreshConfigFromAccount ran only after the overwrite
branch — so a GUARDIAN that is merely behind (its pushed delta not yet
canonicalized) left Multisig.account frozen at the pre-execute snapshot even
though the local account was already correct and on-chain-verifiable.

Rename the guard to isSafeToOverwriteLocalState: return false (skip overwrite,
keep local) when the GUARDIAN is strictly behind, so config is still refreshed
from the authoritative local account. Same-nonce/divergent-commitment and
incoming-vs-on-chain mismatches still throw.
@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

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

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2cc10636-1f89-48f5-b9b2-787f6069ec4f

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

Walkthrough

syncState() now treats lower-nonce GUARDIAN state as non-fatal, preserves the local account, refreshes configuration, and retains errors for genuine commitment divergence. Tests cover lagging state before and after execution.

Changes

syncState lag handling

Layer / File(s) Summary
Non-fatal state lag decision
packages/miden-multisig-client/src/multisig.ts
syncState() uses a boolean overwrite decision, keeps local state when GUARDIAN lags, refreshes configuration, and preserves divergence checks.
Lag and divergence regression coverage
packages/miden-multisig-client/src/multisig.test.ts
Tests verify local account retention, refreshed thresholds, skipped account replacement, post-execution recovery, and updated divergence errors.

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

Sequence Diagram(s)

sequenceDiagram
  participant GUARDIAN
  participant Multisig
  participant MidenClientStore
  GUARDIAN->>Multisig: provide account state
  Multisig->>MidenClientStore: compare incoming and local nonce
  MidenClientStore-->>Multisig: keep local or allow overwrite
  Multisig->>MidenClientStore: refresh configuration from selected account
Loading

Suggested labels: enhancement

Suggested reviewers: zeljkox, haseebrabbani

Poem

A bunny checks the nonce with care,
GUARDIAN lags—but local’s there.
No frozen hops, no needless fright,
Config blooms from state kept right.
Divergent roots still raise alarm.

🚥 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: syncState now keeps local state when GUARDIAN nonce lags instead of throwing.
Linked Issues check ✅ Passed The code matches the issue goals by skipping fatal handling for lagging GUARDIAN state, preserving local state, refreshing config, and still throwing on divergence.
Out of Scope Changes check ✅ Passed The changes stay within the syncState lag-handling fix and its tests, with no obvious unrelated functionality added.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/miden-multisig-client/src/multisig.ts (1)

379-385: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Remove the inline implementation comments.

The surrounding JSDoc already documents both branches.

Suggested cleanup
-      // GUARDIAN is behind local: keep the (on-chain-verifiable) local state.
       if (incomingNonce < localNonce) {
         return false;
       }

-      // Same nonce but divergent state — this method is only reached when the
-      // commitments already differ — is a genuine fork, not GUARDIAN lag.
       if (incomingNonce === localNonce) {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/miden-multisig-client/src/multisig.ts` around lines 379 - 385,
Remove the inline comments surrounding the incomingNonce < localNonce branch and
the same-nonce divergent-state path in the relevant multisig method, while
preserving the existing logic and relying on the surrounding JSDoc.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/miden-multisig-client/src/multisig.ts`:
- Around line 379-385: Remove the inline comments surrounding the incomingNonce
< localNonce branch and the same-nonce divergent-state path in the relevant
multisig method, while preserving the existing logic and relying on the
surrounding JSDoc.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0e28359e-4770-422e-b324-725b265d5786

📥 Commits

Reviewing files that changed from the base of the PR and between e0d9c49 and 135b609.

📒 Files selected for processing (2)
  • packages/miden-multisig-client/src/multisig.test.ts
  • packages/miden-multisig-client/src/multisig.ts

…OverwriteLocalState

The surrounding JSDoc already documents both the behind-local (skip) and
same-nonce-divergence (throw) branches. Addresses CodeRabbit review nit on OpenZeppelin#344.

@zeljkoX zeljkoX 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.

Thanks for contribution <3.

LGTM

This PR introduces small divergence with Rust client

crates/miden-multisig-client/src/client/account.rs

We should update Rust client logic to match TS client.

We can open an issue for later if not handled within this PR.

Copilot AI 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.

Pull request overview

This PR fixes a UX-stalling behavior in the TypeScript Miden multisig SDK (packages/miden-multisig-client) where Multisig.syncState() could throw when the GUARDIAN-reported nonce lagged behind local state, preventing Multisig.account (and derived config) from being refreshed and leaving UI consumers stuck on a stale snapshot.

Changes:

  • Changed the overwrite guard from a throwing helper (ensureSafeToOverwriteLocalState) to a boolean decision helper (isSafeToOverwriteLocalState), treating “GUARDIAN behind local” as a non-fatal skip.
  • Ensured syncState() always refreshes multisig config from the authoritative account (local or imported), so Multisig.account advances even when GUARDIAN lags.
  • Updated and expanded tests to cover “GUARDIAN behind” non-throw behavior and added a regression test for the frozen-account repro.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
packages/miden-multisig-client/src/multisig.ts Updates syncState() overwrite decision flow to skip (not throw) when GUARDIAN nonce is behind, while still refreshing config from the chosen authoritative account.
packages/miden-multisig-client/src/multisig.test.ts Adjusts nonce-behind test to expect no throw/no overwrite/no RPC call, and adds a regression test ensuring Multisig.account “unfreezes” when GUARDIAN lags post-execute.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.01%. Comparing base (e0d9c49) to head (3b0e3ed).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #344   +/-   ##
=======================================
  Coverage   78.01%   78.01%           
=======================================
  Files         167      167           
  Lines       31065    31065           
=======================================
  Hits        24236    24236           
  Misses       6829     6829           

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e0d9c49...3b0e3ed. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

multisig-client: syncState() throws and freezes Multisig.account when GUARDIAN nonce lags behind local

4 participants