Skip to content

fix(dpp)!: refuse updating a contract whose stored schema repeats a key (PV15) - #5947

Merged
QuantumExplorer merged 4 commits into
v5.1-devfrom
claude/repeated-schema-keys-pv15-912582
Nov 16, 2026
Merged

QuantumExplorer merged 4 commits into
v5.1-devfrom
claude/repeated-schema-keys-pv15-912582

Conversation

@QuantumExplorer

@QuantumExplorer QuantumExplorer commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Basic explanation

From protocol version 15, registration refuses a document type schema, or contract $defs, that names a key twice in one object (#5702). A contract stored before protocol version 15 may still hold such a schema. This PR completes the rule: from protocol version 15 a data contract update of a contract whose stored schema or stored $defs repeat a key is refused, so no document type can be registered with a repeated schema key and none stored with one can have its schema replaced.

Scope: the rule covers the $defs a document type's parse reads, so the $defs of a contract without document types are stored as sent, as before. A token configuration update submits no schema and stores the stored schemas as they are, so it is not refused.

No JSON can express a repeated key, so only a contract built from a raw value map can hold one. None of the 81 contract create and update transitions on mainnet, or the 5,026 on testnet, carries such a schema (checked 2026-11-16), so the rule touches no existing contract.

What was done?

  • DataContract::validate_update generation 2, selected by CONTRACT_VERSIONS_V7 (protocol version 15) only: it walks the stored document type schemas and the stored $defs once, refuses the update when one repeats a key, then runs generation 1.
  • The walk registration uses is split into first_repeated_key_of_schema and first_repeated_key_of_definitions, shared by both; registration's refusal and its message are unchanged.
  • The protocol version 15 notes in rs-platform-version name the rule.

Issue being fixed: before and after (PlatformVersion::latest())

A contract stored with a document type whose schema names properties twice, then any update of it:

Before After
Stored document type schema repeats a key judged by the usual update rules refused in the block, paid: DocumentTypeUpdateError (40212), "its stored schema declares properties more than once at its root, and a contract whose schema repeats a key can not be updated"
Stored $defs repeat a key judged by the usual update rules refused in the block, paid: DataContractUpdateActionNotAllowedError (40004), "update a contract whose stored $defs declare properties more than once at $defs.word"
Stored schema names each key once judged by the usual update rules unchanged

At protocol version 14 and earlier nothing changes.

Public interface changes

  • A data contract update can now fail, from protocol version 15, with

    • DocumentTypeUpdateError (40212) when a stored document type schema repeats a key, or
    • DataContractUpdateActionNotAllowedError (40004) when the stored $defs do.

    Both codes exist already; only a new cause is added.

  • DataContract.validateUpdate in wasm-dpp2 returns the same errors (it calls the same check); its doc comment names the rule.

  • No wire format, storage format or SDK signature changes.

In-place changes to shipped generations

None. Generations 0 and 1 of validate_update are untouched; generation 1's tests are now pinned to protocol version 14.

Breaking Changes

Consensus change at protocol version 15 (unreleased): an update that earlier versions judge by the usual update rules is refused when the stored contract repeats a schema key.

How Has This Been Tested?

  • cargo test -p dpp --lib -- validate_update repeated_doctype_key (154 passed): generation 2 refuses a stored repeat at the root, nested and in $defs, reports a type's schema before the $defs, admits a stored schema naming each key once, and the dispatcher admits the same update at protocol version 14 and refuses it at the latest.
  • cargo test -p drive-abci --lib -- data_contract_update repeated_properties_update (87 passed), including a block test that registers the contract under protocol version 14, runs the first block of the latest version, and updates it: the update is refused and paid, and the stored contract is unchanged.
  • cargo clippy -p dpp -p drive-abci --lib --tests clean.

Not run locally: the strategy tests and the full suites.

Checklist:

  • I have performed a self-review of my own code
  • My Rust, FFI, WASM, Swift or Kotlin 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 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
  • If I added or changed GroveDB structure, I described it in the area's structure.rs, regenerated grovedb-structure.json, and checked the structure viewer link posted on this pull request
  • If I added or changed a system contract's document schemas, I checked the contract viewer link posted on this pull request

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

🤖 Generated with Claude Code

PR Hygiene · 1da8a51

  • Bots — thepastaclaw not yet — /skip-bots proceeds without the ones not yet reported
  • Self-review — post /self-reviewed
  • Build running
  • Approvals — you own every area touched; none needed

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
    • From protocol version 15, contracts with duplicate schema keys are rejected, including duplicates in nested schemas or $defs.
    • Updates are refused when the stored contract contains duplicate schema keys, regardless of the new contract’s contents.
    • Update validation reports the first duplicate key and its location, with errors reflecting whether it appears in a document schema or $defs.
  • Documentation
    • Updated protocol and API guidance to describe duplicate-key validation and update restrictions.

QuantumExplorer and others added 2 commits November 16, 2026 13:40
…ey (PV15)

From protocol version 15 registration already refuses a document type
schema, or the contract's `$defs`, that names a key twice in any object.
A contract stored earlier may still hold one. `DataContract::validate_update`
generation 2, selected only by protocol version 15, runs generation 1 once
no stored document type schema (with the stored `$defs` its parse reads)
repeats a key, using the same walk as registration, and refuses the update
with `DocumentTypeUpdateError` otherwise.

Also adds regression tests for the registration refusal: a repeated key in
either order at the root, in a nested object, in an array's `items`, in an
index and in a definition, and contract create and update in a block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ad it once (PV15)

Follow-up to the refusal of an update of a contract whose stored schema
repeats a key (`validate_update` 2, protocol version 15 only):

- the stored `$defs` are walked once, not once per document type, and a
  key they repeat is reported on the contract
  (`DataContractUpdateActionNotAllowedError`) instead of on whichever
  document type sorts first; a type's own schema is still reported on the
  type (`DocumentTypeUpdateError`);
- the walk is split into `first_repeated_key_of_schema` and
  `first_repeated_key_of_definitions`, which share the wording of the
  place with registration's refusal;
- generation 1's tests are pinned to protocol version 14;
- the version gate is tested by behaviour on both sides of it;
- a block test registers the contract under protocol version 14, runs the
  first block of the latest version and updates it;
- the block test fixture registers its starting schema under full
  validation again;
- the wasm `validateUpdate` doc names the rule.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@github-actions github-actions Bot added this to the v5.1.0 milestone Nov 16, 2026
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adds repeated-key detection to document schema validation and introduces update validation version 2. At protocol version 15, updates are rejected when stored document schemas or $defs contain repeated keys. Tests cover validation errors and contract state-transition outcomes.

Changes: Contract update validation

Layer / File(s) Summary
Repeated-key detection and schema validation
packages/rs-dpp/src/data_contract/document_type/class_methods/..., packages/rs-dpp/src/data_contract/document_type/class_methods/try_from_schema/v3/repeated_doctype_key_tests.rs
The schema walker returns the first repeated key and its location. Validation-gated exports expose the detection functions. Tests cover repeated keys in document schemas and $defs.
Versioned update checks and documentation
packages/rs-dpp/src/data_contract/methods/validate_update/..., packages/rs-platform-version/src/version/..., packages/wasm-dpp2/src/data_contract/model.rs
Update validation version 2 scans stored document schemas and $defs before delegating to version 1. Dispatch and tests are updated, and version 14 tests are pinned to that version. Protocol-version and API documentation describe the update rule.
Contract state-transition tests
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/repeated_properties_update.rs
Tests cover contract creation and updates with repeated keys, stored contracts, and updates after a protocol-version upgrade. They assert errors and verify that rejected transitions do not change stored state.

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

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant UpdateDispatcher
  participant UpdateValidator
  participant SchemaScanner
  participant DefinitionsScanner
  participant VersionOneValidator
  UpdateDispatcher->>UpdateValidator: Dispatch version 2
  UpdateValidator->>SchemaScanner: Scan stored document schemas
  alt No repeated schema key
    UpdateValidator->>DefinitionsScanner: Scan stored definitions
    alt No repeated definition key
      UpdateValidator->>VersionOneValidator: Validate update
    end
  end
Loading

Possibly related PRs

  • dashpay/platform#5702: Adds protocol-version-15 registration refusal for repeated keys in document schemas and contract $defs; this change adds checks for updates to stored contracts.

Suggested reviewers: shumkov, thepastaclaw

Merge Risk: Minimal · up to 1da8a

The change refuses updates to contracts whose stored schemas repeat a key, starting at protocol version 15. Earlier protocol versions are unaffected, and no merge-blocking issue was found.

Pre-merge checks: Passed 2

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: a PV15 consensus rule that refuses updates to a stored contract whose schema repeats a key. It uses the conventional type(scope)! prefix and marks the breaking change.…
Full details: Title check

Explanation

The title names the main change: a PV15 consensus rule that refuses updates to a stored contract whose schema repeats a key. It uses the conventional type(scope)! prefix and marks the breaking change. The changeset adds validate_update v2, the repeated-key scan of stored document-type schemas and $defs, and the matching tests and docs, so the title matches the primary change.

  • 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 28c5c14 and 1758991.

Files selected for processing: 11

  • packages/rs-dpp/src/data_contract/document_type/class_methods/mod.rs
  • packages/rs-dpp/src/data_contract/document_type/class_methods/try_from_schema/mod.rs
  • packages/rs-dpp/src/data_contract/document_type/class_methods/try_from_schema/native_schema/mod.rs
  • packages/rs-dpp/src/data_contract/document_type/class_methods/try_from_schema/v3/repeated_doctype_key_tests.rs
  • packages/rs-dpp/src/data_contract/methods/validate_update/mod.rs
  • packages/rs-dpp/src/data_contract/methods/validate_update/v1/mod.rs
  • packages/rs-dpp/src/data_contract/methods/validate_update/v2/mod.rs
  • packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/repeated_properties_update.rs
  • packages/rs-platform-version/src/version/dpp_versions/dpp_contract_versions/v7.rs
  • packages/rs-platform-version/src/version/v15.rs
  • packages/wasm-dpp2/src/data_contract/model.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.

@github-actions

github-actions Bot commented Nov 16, 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 16, 2026
@thepastaclaw

thepastaclaw commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

🕓 Review not started yet because the new head is waiting for the 30-minute push debounce.

  • Request normal review — click when the PR is ready for review.
  • Request priority review — click to move this review to the front of the queue.

Commit 1da8a51. Normal review starts when eligible; priority review starts as soon as a slot is available.

@github-actions

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

@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 2 only (queue backlog)

The protocol-15 validation generation correctly preserves earlier behavior, but the duplicate-key invariant is incomplete and the new stored-schema scan is not included in processing-fee accounting. A version-snapshot comment also omits the newly selected validation generation. CI has no reported code failures; Kotlin SDK and PR hygiene checks remain pending.

🔴 3 blocking | 🟡 1 suggestion(s)

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

🔴 Blocking: Token-only contract creation bypasses the duplicate-key check
packages/rs-dpp/src/data_contract/document_type/class_methods/create_document_types_from_document_schemas/v1/mod.rs:44-48

Protocol 15 permits an empty documentSchemas map when has_tokens is true. The new duplicate-key check is invoked from the per-document-type parser, so the loop beginning at line 65 is skipped for a token-only contract and the contract-level $defs are never traversed. validate_schema_patterns does not reject repeated map keys; it only counts pattern values. Consequently, a raw token-only contract with a repeated key inside a $defs value can pass full validation and be stored, even though the PR claims registration refuses duplicate keys in the contract's $defs. Run the shared contract-level definitions walk before the document-type loop and account for its reads consistently.

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

🟡 Suggestion: Snapshot comment omits the newly selected validate_update generation
packages/rs-platform-version/src/version/v15.rs:957

CONTRACT_VERSIONS_V7.methods.validate_update changes from generation 1 to generation 2 in this PR, but the PLATFORM_V15.contract_versions // changed: comment lists the other contract-version changes without mentioning validate_update. The version selection is correct and this does not affect consensus, but the snapshot comment is incomplete for auditing the protocol-15 behavior. Add the new validate_update 2 change and its note reference to this comment.

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

Review provenance

Source: reviewer 1: gpt-6.1-sol (agent: phase2-reviewer, role: general); reviewer 2: gpt-6.1-sol (agent: phase2-reviewer, role: architecture-layering); reviewer 3: gpt-6.1-sol (agent: phase2-reviewer, role: platform-versioning); reviewer 4: gpt-6.1-sol (agent: phase2-reviewer, role: rust-quality); reviewer 5: gpt-6.1-sol (agent: phase2-reviewer, role: security-auditor); reviewer 6: gpt-6.1-sol (agent: phase2-reviewer, role: general); reviewer 7: gpt-6.1-sol (agent: phase2-reviewer, role: architecture-layering); reviewer 8: gpt-6.1-sol (agent: phase2-reviewer, role: platform-versioning); reviewer 9: gpt-6.1-sol (agent: phase2-reviewer, role: rust-quality); reviewer 10: gpt-6.1-sol (agent: phase2-reviewer, role: security-auditor); final verifier: gpt-6.1-sol (agent: sol-verifier, role: final-verifier)

  • Triage: critical by gpt-6.1-sol (effort low) — This is a large change to consensus-critical contract validation and protocol-version dispatch in rs-dpp and rs-drive-abci, changing which state transitions are accepted at protocol version 15.
  • Phase 1 reviewers: not run (skipped for throughput: 12 PRs queued, above the 10 limit)
  • Second Phase-2 pass: on this first full review every Phase-2 reviewer also ran a second, independent time, and the verifier weighed both sets
  • Fresh verifier: gpt-6.1-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.1-sol — general (completed, effort xhigh); agent phase2-reviewer, gpt-6.1-sol — architecture-layering (completed, effort xhigh); agent phase2-reviewer, gpt-6.1-sol — platform-versioning (completed, effort xhigh); agent phase2-reviewer, gpt-6.1-sol — rust-quality (completed, effort xhigh); agent phase2-reviewer, gpt-6.1-sol — security-auditor (completed, effort xhigh); agent phase2-reviewer, gpt-6.1-sol — general (completed, effort xhigh); agent phase2-reviewer, gpt-6.1-sol — architecture-layering (completed, effort xhigh); agent phase2-reviewer, gpt-6.1-sol — platform-versioning (completed, effort xhigh); agent phase2-reviewer, gpt-6.1-sol — rust-quality (completed, effort xhigh); agent phase2-reviewer, gpt-6.1-sol — security-auditor (completed, effort xhigh); 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-dpp/src/data_contract/methods/validate_update/v2/mod.rs`:
- [BLOCKING] packages/rs-dpp/src/data_contract/methods/validate_update/v2/mod.rs:37-42: Token configuration updates bypass the stored-schema check
  `validate_update_v2` is reached by the ordinary `DataContractUpdateTransition` state-validation path, but token configuration updates use a separate batch-action path. That path validates token-specific authorization and state rules, then `updated_contract()` increments the contract version and changes the token configuration; the operation converter subsequently emits `ApplyContract` for that copy. It never calls this stored-schema gate. Therefore, on protocol 15, a legacy contract whose stored document schema or `$defs` contains a repeated key can still be changed and re-stored through a token configuration update, contradicting the PR's stated invariant that no such contract can be changed. Apply the shared, versioned stored-schema check to the token-update validation path without duplicating the traversal.
- [BLOCKING] packages/rs-dpp/src/data_contract/methods/validate_update/v2/mod.rs:50-56: The stored-schema validation walk is not charged
  Every protocol-15 update now recursively scans all stored document schemas and stored `$defs`, but `validate_update_stored_schemas_repeat_no_key` only increments `read` and then discards it. The production ordinary-update caller adds the GroveDB fetch fee and later charges validation operations for parsing and compiling the submitted contract, but it emits no operation for this additional in-memory traversal of the old contract. The fetch fee accounts for storage access, not the CPU work of walking the stored values, so a large valid legacy contract can impose an uncharged O(stored-contract-size) scan; the paid refusal path is likewise undercharged. Extend the versioned validation/cost interface with an explicit operation for this walk and propagate it through the execution context so estimation and execution charge the same work.

In `packages/rs-dpp/src/data_contract/document_type/class_methods/create_document_types_from_document_schemas/v1/mod.rs`:
- [BLOCKING] packages/rs-dpp/src/data_contract/document_type/class_methods/create_document_types_from_document_schemas/v1/mod.rs:44-48: Token-only contract creation bypasses the duplicate-key check
  Protocol 15 permits an empty `documentSchemas` map when `has_tokens` is true. The new duplicate-key check is invoked from the per-document-type parser, so the loop beginning at line 65 is skipped for a token-only contract and the contract-level `$defs` are never traversed. `validate_schema_patterns` does not reject repeated map keys; it only counts pattern values. Consequently, a raw token-only contract with a repeated key inside a `$defs` value can pass full validation and be stored, even though the PR claims registration refuses duplicate keys in the contract's `$defs`. Run the shared contract-level definitions walk before the document-type loop and account for its reads consistently.

In `packages/rs-platform-version/src/version/v15.rs`:
- [SUGGESTION] packages/rs-platform-version/src/version/v15.rs:957: Snapshot comment omits the newly selected validate_update generation
  `CONTRACT_VERSIONS_V7.methods.validate_update` changes from generation 1 to generation 2 in this PR, but the `PLATFORM_V15.contract_versions` `// changed:` comment lists the other contract-version changes without mentioning `validate_update`. The version selection is correct and this does not affect consensus, but the snapshot comment is incomplete for auditing the protocol-15 behavior. Add the new `validate_update 2` change and its note reference to this comment.

@github-actions

Copy link
Copy Markdown
Contributor

Your move: thepastaclaw requested changes on this head; dismiss the review or push a fix; thepastaclaw left review threads unresolved; resolve them.
Full checklist in the description.

@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 16, 2026
…, scope the rule to updates that submit schemas

The `contract_versions` comment of `PLATFORM_V15` listed every change of
`CONTRACT_VERSIONS_V7` but `validate_update` 2. Generation 2's doc comment said a
contract stored with a repeated schema key stays as it was stored: its schemas do, a
token configuration update still changes its token configuration.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@QuantumExplorer

Copy link
Copy Markdown
Member Author

Replies to the two findings of the PastaClaw review that have no inline thread:

  • Token-only contract creation: declined. It is outside this PR's diff and is how the registration rule of fix(dpp)!: refuse a document type schema naming a key twice (PV15) #5702 is built: only a document type's parse reads the contract's $defs, so the $defs of a contract without document types are stored as sent, unchecked, as the comment in try_from_schema generation 3 says. Nothing parses them, and generation 2 of validate_update refuses a later update of a contract whose stored $defs repeat a key, so no document type can come to read them. The PR description now states that scope.
  • Snapshot comment: fixed in 1da8a51, the contract_versions comment of PLATFORM_V15 now names validate_update 2 with note 7.

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Your move: thepastaclaw left review threads unresolved; resolve them.
Full checklist in the description.

@github-actions

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 16, 2026
@QuantumExplorer
QuantumExplorer merged commit cbd1842 into v5.1-dev Nov 16, 2026
17 of 19 checks passed
@QuantumExplorer
QuantumExplorer deleted the claude/repeated-schema-keys-pv15-912582 branch November 16, 2026 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting-bots Waiting for the review bots to report on this head

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants