Skip to content

Conversation

thephez
Copy link
Collaborator

@thephez thephez commented Aug 27, 2025

Issue being fixed or feature implemented

Data contract update transitions require identity_contract_nonce instead of identity_nonce. This aligns with the DataContractUpdateTransition struct and matches how document/token operations work.

What was done?

Switched to identity contract nonce for contract update

How Has This Been Tested?

Locally

Breaking Changes

🤔 Technically maybe. But not really, because it was mostly non-functional before (it only worked when your identity nonce and identity contract nonce were the same)

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have added "!" to the title and described breaking changes in the corresponding section if my code contains any
  • I have made corresponding changes to the documentation if needed

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

Summary by CodeRabbit

  • Bug Fixes

    • Contract updates now use per‑contract nonces, preventing collisions across multiple contracts owned by the same identity and reducing failed or rejected updates.
  • Improvements

    • More reliable signing for contract updates by aligning nonce handling with per‑contract behavior.
    • Clearer error messaging when a contract-specific nonce cannot be retrieved.

Data contract update transitions require identity_contract_nonce instead of
identity_nonce. This aligns with the DataContractUpdateTransition struct and
matches how document/token operations work.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@thephez thephez added this to the v2.1 milestone Aug 27, 2025
Copy link
Contributor

coderabbitai bot commented Aug 27, 2025

Walkthrough

Switches contract update signing to use per-contract nonces by replacing identity-wide nonce retrieval with get_identity_contract_nonce(owner_identifier, contract_identifier, ...). Introduces identity_contract_nonce and updates related error messaging and DataContractUpdateTransition construction accordingly.

Changes

Cohort / File(s) Summary
Per-contract nonce for contract updates
packages/wasm-sdk/src/state_transitions/contracts/mod.rs
Replaced get_identity_nonce(...) with get_identity_contract_nonce(owner_identifier, contract_identifier, ...); introduced identity_contract_nonce variable and updated error message; used this nonce when creating DataContractUpdateTransition.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant App as App
    participant WasmSdk as WasmSdk::contract_update
    participant Platform as Platform API

    App->>WasmSdk: Request contract update (owner_id, contract_id, update)
    note right of WasmSdk: Contract updates use per-contract nonces
    WasmSdk->>Platform: get_identity_contract_nonce(owner_id, contract_id, proof=true)
    Platform-->>WasmSdk: identity_contract_nonce or error
    alt nonce retrieved
        WasmSdk->>WasmSdk: Build DataContractUpdateTransition with per-contract nonce
        WasmSdk-->>App: Signed update transition
    else error
        WasmSdk-->>App: Error fetching identity contract nonce
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Suggested reviewers

  • ktechmidas
  • pauldelucia

Poem

A nibble of nonce, a hop to refine,
Contracts now count per ID and line.
I twitch my nose at bugs that prance—
One contract, one neat nonce dance.
Thump-thump! Signed true, updates advance. 🥕🐇

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch wasm-sdk-fix-contract-update-nonce

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

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

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
packages/wasm-sdk/src/state_transitions/contracts/mod.rs (2)

245-260: Add sanity checks: ensure updated JSON targets the same contract and owner

Prevent mismatches early by verifying the updated contract’s $id and ownerId match the inputs before building the transition.

Apply after constructing updated_contract and before the version check:

   let updated_contract = DataContract::from_json(
       updates_json,
       true, // validate
       sdk.version(),
   )
   .map_err(|e| JsValue::from_str(&format!("Failed to create updated contract from JSON: {}", e)))?;
 
+  // Sanity checks: ensure the update targets the same contract and owner
+  if updated_contract.id() != contract_identifier {
+      return Err(JsValue::from_str("Updated contract ID does not match provided contract_id"));
+  }
+  if updated_contract.owner_id() != owner_identifier {
+      return Err(JsValue::from_str("Updated contract ownerId does not match provided owner_id"));
+  }
 
   // Verify the version was incremented
   if updated_contract.version() <= existing_contract.version() {

262-267: All contract-update paths uniformly use per-contract nonces with bump=true

I’ve verified across both wasm-sdk and rs-sdk that every contract update transition calls

get_identity_contract_nonce(..., true, None)

and there are no remaining calls to the identity-wide get_identity_nonce within any contract_update or similar modules. The third parameter (true) correctly bumps the nonce on fetch (per prior learnings), ensuring consistency with document/token operations.

Bullet points of findings:

  • No instances of get_identity_nonce in any contract-update path (e.g. in state_transitions/contracts, tokens, documents in wasm-sdk, nor in platform/transition/* in rs-sdk).
  • All per-contract nonce fetches use true for bump semantics.
  • The bump behavior (“increment before storing”) aligns with the SDK’s caching logic documented in sdk.rs.

Recommendations:

  • Document in the SDK README (or inline docs) that callers must serialize calls per (owner, contract) to avoid nonce-bumping races, or implement a lightweight mutex in the SDK around get_identity_contract_nonce.
  • Nit: update the JS error string for clarity, for example:
    - .map_err(|e| JsValue::from_str(&format!("Failed to get identity contract nonce: {}", e)))?;
    + .map_err(|e| JsValue::from_str(&format!("Failed to get per-contract identity nonce: {}", e)))?;
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between cfd9b57 and 352f819.

📒 Files selected for processing (1)
  • packages/wasm-sdk/src/state_transitions/contracts/mod.rs (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
packages/wasm-sdk/src/**/*.rs

📄 CodeRabbit inference engine (packages/wasm-sdk/CLAUDE.md)

packages/wasm-sdk/src/**/*.rs: When implementing WASM SDK functionality, always refer to AI_REFERENCE.md first for accurate method signatures and examples.
For WASM builds, fix 'time not implemented on this platform' errors by using js_sys::Date::now().
Token functions are methods on WasmSdk, not standalone functions; avoid importing them as standalone.
The WASM SDK now fully supports where and orderBy clauses for document queries; use the specified JSON array formats and supported operators.

Files:

  • packages/wasm-sdk/src/state_transitions/contracts/mod.rs
🧠 Learnings (2)
📓 Common learnings
Learnt from: shumkov
PR: dashpay/platform#2449
File: packages/rs-sdk/src/platform/transition/fungible_tokens/unfreeze.rs:148-148
Timestamp: 2025-02-14T04:31:57.245Z
Learning: The third boolean parameter in `get_identity_contract_nonce` method controls whether to bump (increment) the nonce when set to `true`.
📚 Learning: 2024-10-03T11:51:06.980Z
Learnt from: shumkov
PR: dashpay/platform#2201
File: packages/rs-platform-version/src/version/v2.rs:1186-1188
Timestamp: 2024-10-03T11:51:06.980Z
Learning: In the `IdentityTransitionVersions` structure within `packages/rs-platform-version/src/version/v2.rs`, the field `credit_withdrawal` does not need the `identity_` prefix since it is already encompassed within identity state transitions.

Applied to files:

  • packages/wasm-sdk/src/state_transitions/contracts/mod.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Build JS packages / Build JS
  • GitHub Check: build-and-test-wasm-sdk
  • GitHub Check: build-wasm-sdk
🔇 Additional comments (1)
packages/wasm-sdk/src/state_transitions/contracts/mod.rs (1)

286-287: Nonce correctly plumbed into DataContractUpdateTransition

Passing identity_contract_nonce into DataContractUpdateTransition::new_from_data_contract is the intended wiring for per-contract nonces. Looks good.

@QuantumExplorer QuantumExplorer merged commit 23125f8 into v2.1-dev Aug 28, 2025
18 of 22 checks passed
@QuantumExplorer QuantumExplorer deleted the wasm-sdk-fix-contract-update-nonce branch August 28, 2025 07:29
lklimek pushed a commit that referenced this pull request Sep 1, 2025
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.

2 participants