Skip to content

SDK nonce cache incorrectly retains stale values after failed state transitions #2737

Closed

Description

@thephez

Expected Behavior

When a state transition fails to broadcast, the SDK should reset its cached nonce to match the platform's actual nonce value on the next attempt. The SDK should always use the platform as the source of truth for nonces when it queries the platform.

Current Behavior

After a failed state transition, the SDK's cached nonce continues to increment locally while the platform nonce remains unchanged. Even when the SDK queries the platform and receives a lower nonce value, it incorrectly keeps the higher cached value, causing all subsequent state transitions to fail with "nonce too far in future" errors.

Example:

  • Platform nonce: 1
  • Failed transition attempts increment cached nonce: 42, 43, 44...
  • SDK queries platform, gets nonce = 1
  • SDK ignores this and keeps using 42+
  • Error: "Identity xyz... is trying to set an invalid identity nonce. The current identity nonce is 1, we are setting 42, error is nonce too far in future"

Possible Solution

Per Claude, the bug is in platform/packages/rs-sdk/src/sdk.rs in the get_identity_nonce function (lines 420-435) and similar logic in get_identity_contract_nonce (around line 512).

The problematic logic:

// Entry::Occupied branch when querying platform
let insert_nonce = if platform_nonce > *current_nonce {
    // Use platform nonce if higher
    if bump_first {
        platform_nonce + 1
    } else {
        platform_nonce
    }
} else if bump_first {
    // BUG: Keeps cached nonce even when platform says it's lower!
    *current_nonce + 1
} else {
    *current_nonce
};

The fix - always trust the platform as source of truth:

// Always use platform nonce when we query it
let insert_nonce = if bump_first {
    platform_nonce + 1
} else {
    platform_nonce
};

Steps to Reproduce (for bugs)

  1. Attempt a data contract update state transition on https://evo-sdk.dash.org/ that fails (e.g., network issue, validation error)
  2. Retry the same state transition without refreshing the page/SDK instance
  3. Observe that the nonce increments on each retry (42, 43, 44...)
  4. Check platform's actual nonce for the identity - it remains at expected value (e.g., 1)
  5. All subsequent state transitions fail with "nonce too far in future" error

Context

This issue prevents retrying failed state transitions without completely reinitializing the SDK. It particularly affects:

  • Data contract updates

Document and token operations use per-contract nonces (get_identity_contract_nonce) which has the same bug but may be less frequently triggered.

The issue makes development and testing difficult as any failed transaction requires restarting the application/SDK to clear the cache.

Your Environment

  • Version used: Current v2.1-dev branch
  • Environment name and version: WASM SDK in browser (Chrome 131), Dash Platform Testnet
  • Operating System and version: Linux/macOS/Windows (all affected)
  • Link to your project: /packages/wasm-sdk/test/ui-automation/tests/state-transitions.spec.js

Additional Notes

The cache staleness timeout (DEFAULT_IDENTITY_NONCE_STALE_TIME_S = 1200 seconds / 20 minutes) means the bad cache persists for a long time. The assumption that "nonces only go up" is invalid when transactions fail, as the platform doesn't increment its nonce for failed transactions.

This affects all state transitions using:

  • get_identity_nonce(..., true, ...) with bump_first: true
  • get_identity_contract_nonce(..., ..., true, ...) with bump_first: true

Activity

  1. added
    bug
    on Aug 27, 2025
  2. added theissue type on Aug 27, 2025
  3. lklimek commented on Nov 3, 2025

    @lklimek
    Contributor

    @codex check if this issue is still there in platform packages/rs-sdk crate; if so, prepare a fix.

  4. chatgpt-codex-connector commented on Nov 3, 2025

    @chatgpt-codex-connector

    Summary

    • Updated the cached identity and contract nonce refresh logic to always adopt the Platform-reported nonce before optionally bumping, preventing stale local counters. packages/rs-sdk/src/sdk.rsL403-L512
    • Added async regression tests to confirm both identity- and contract-level nonce caches reset after observing lower Platform values. packages/rs-sdk/src/sdk.rsL1180-L1384

    Testing

    • ⚠️ cargo test -p dash-sdk get_identity_nonce_resets_to_platform_value_after_refresh (fails: tenderdash-proto build cannot download Tenderdash archive because of an invalid TLS certificate)

    View task →

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugrs-sdk

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions