Skip to content

fix(dpp)!: refuse token cost and unruled keyword changes on update with a consensus error (PV14) - #5069

Merged
QuantumExplorer merged 1 commit into
v4.2-devfrom
claude/charming-bell-c727b9
Sep 27, 2026
Merged

QuantumExplorer merged 1 commit into
v4.2-devfrom
claude/charming-bell-c727b9

Conversation

@QuantumExplorer

@QuantumExplorer QuantumExplorer commented Sep 27, 2026 •

Copy link
Copy Markdown
Member

Basic explanation

What this does: When a contract owner updates a contract, Platform compares each existing document type's old schema with its new one and refuses changes that would break documents already stored. In protocol version 14 that comparison had no rule for 19 schema keywords (tokenCost, ttl, actionFees and others). It stopped with an internal error whenever an update touched one of them. This PR gives each of those keywords a rule: any change to it is refused, with a proper consensus error. Token costs are also compared on their parsed values now, so changing a cost is refused with a clear message saying token costs are fixed.

Value: A contract update that touches one of these keywords now gets the consensus error code the same refusal gets elsewhere (40212 or 10246), not an internal error. The contract owner learns why the update was refused. The transition is handled like every other refused update, instead of being dropped as a node-side failure.

Risks: Low. Every update this PR affects was already refused, and it still is; only the kind of refusal changes. No update that failed before now passes, and none that passed now fails. The change is consensus-relevant: a refused update is now recorded as a failed transition, not left out of the block. It only touches the generations selected by protocol version 14, which is unreleased. Protocol version 13 and earlier keep their behaviour; a test pins that.

Issue being fixed or feature implemented

validate_schema_compatibility generation 1 walks the JSON diff between a document type's old and new schema. For a keyword with no rule in rs-json-schema-compatibility-validator's rule set, it returns UnsupportedSchemaKeywordError. That surfaces as ProtocolError::DataContractError(JsonSchema(SchemaCompatibilityValidationError)), not a consensus-invalid result. drive-abci then treats the update as an internal error in check_tx and in block processing, not as a consensus error.

Meta-schema v3 admits these keywords with no differ rule that prepared_for_diff does not strip:

  • top level: tokenCost, ttl, actionFees, indexOnly, entryPayload, keepsTransferHistory, keepsPurchaseHistory, keepsPricingHistory, documentsCountable, rangeCountable, documentsSummable, rangeSummable, documentsAverageable, rangeAverageable, canBeDeletedByModerators, canBeDeletedByModeratorsFor, minProperties, maxProperties
  • in a property's schema: minProperties, maxProperties, contains

Most of these are also compared on their parsed values by validate_update v1 or the shared config checks, which refuse a real change with DocumentTypeUpdateError (40212) before the differ runs. That left two gaps:

  1. tokenCost had no parsed comparison, so every tokenCost change on an existing document type reached the differ.
  2. An edit that leaves the parsed value as it was still changes the schema text, so it reached the differ too. Examples: writing out a default, or switching to the documentsAverageable shorthand.

What was done?

Frozen rules in the v1 differ. FROZEN_KEYWORDS_WITHOUT_A_SHARED_RULE lists the 19 keywords above. Each gets the frozen rule the property refersTo already gets, the way ownerRefersTo, transient and propertyConstraints do today. The rules go in generation 1's override rules, not the shared rule set, because generation 0 also reads the shared set. A new test reads meta-schema v3 and fails if any keyword it admits (top level, property schema, typed-array element) has no rule and is not stripped. A test at protocol version 13 pins that generation 0 still reports the old error.

Parsed token cost comparison in validate_update v1. validate_token_costs_unchanged compares the parsed cost of each of the six actions. An added, changed or removed cost gets DocumentTypeUpdateError (40212), as actionFees and ttl already do. A document type added by the update may still declare its own costs. No earlier protocol version let an update change token costs either, since the differ failed on any tokenCost diff.

So a real change to one of these keywords gets 40212 wherever the document type parse reads the keyword. An edit that only changes the text gets 10246, the same as writing out "documentsMutable": true today.

Before / after

A real token cost change:

// stored
"tokenCost": { "create": { "tokenPosition": 0, "amount": 1 } }
// update
"tokenCost": { "create": { "tokenPosition": 0, "amount": 2 } }
  • Before: internal error schema keyword 'tokenCost' at path '/tokenCost/create/amount' is not supported
  • After: DocumentTypeUpdateError (40212) document type can not change the token cost of its create action: token costs are fixed when the document type is published

Writing out a default, with the parsed document type unchanged:

// stored: no keepsTransferHistory
// update adds
"keepsTransferHistory": false
  • Before: internal error schema keyword 'keepsTransferHistory' at path '/keepsTransferHistory' is not supported
  • After: IncompatibleDocumentTypeSchemaError (10246), add at /keepsTransferHistory

Switching to the averageable shorthand:

// stored
"documentsCountable": true, "documentsSummable": "amount"
// update
"documentsAverageable": "amount"
  • Before: internal error schema keyword 'documentsAverageable' at path '/documentsAverageable' is not supported
  • After: IncompatibleDocumentTypeSchemaError (10246)

A keyword with no parsed value:

// stored
"maxProperties": 2
// update
"maxProperties": 3
  • Before: internal error schema keyword 'maxProperties' at path '/maxProperties' is not supported
  • After: IncompatibleDocumentTypeSchemaError (10246), replace at /maxProperties

Notes for reviewers

  • minProperties, maxProperties and contains are frozen outright, matching "Fixed" in the book's keyword reference. Loosening them (raising maxProperties, dropping a contains) would be safe for stored documents; that can be allowed later with dedicated rules if wanted.
  • entryPayload is parsed as a set, like transient, but only transient is sorted before the diff. A reordered entryPayload is therefore refused (10246), not accepted.
  • The frozen rules are keyed by keyword, so they also apply to a contract's $defs diff (validate_update_schema_defs). A $defs change under minProperties, maxProperties or contains now gets IncompatibleDataContractSchemaError, not the internal error.
  • The book's contract keywords reference page (#5067) lists tokenCost as "Fixed" with no code. Once both land, its Update column should read 40212, and minProperties, maxProperties and contains 10246.

How Has This Been Tested?

  • New rs-dpp tests:
    • should_report_every_change_to_a_keyword_without_a_shared_rule_as_incompatible: adding, removing and changing each of the 21 keyword placements is judged, not an error.
    • should_have_a_rule_for_every_keyword_meta_schema_v3_admits: the meta-schema guard. Checked by removing contains from the list, which made it fail.
    • should_hard_error_on_a_token_cost_diff_at_protocol_version_13: the generation 0 replay pin.
    • should_return_invalid_result_when_token_costs_are_changed: 40212 for add, remove and change of amount, effect, gasFeesPaidBy and optional.
    • should_refuse_a_schema_edit_that_leaves_the_parsed_document_type_unchanged: 13 text-only edits, each 10246.
  • Before the fix, the last two tests failed with SchemaCompatibilityValidationError on every case.
  • New drive-abci tests: should_refuse_an_update_adding_a_token_cost_as_a_document_type_update_error and should_refuse_an_update_writing_out_a_default_as_an_incompatible_schema. validate_state now returns a consensus error instead of Err.
  • cargo test -p dpp: 4881 passed, 6 ignored, plus integration and doc tests.
  • cargo test -p drive-abci --lib -- data_contract_update: 62 passed.
  • cargo clippy -p dpp -p drive-abci --tests: clean. rustfmt --check on the changed files: clean.

Breaking Changes

Consensus, protocol version 14 only. A contract update that changes a token cost, or touches any of the keywords above, used to end as an internal error. It now ends as a consensus error (40212 or 10246) and is recorded like any other refused update. Protocol versions up to 13 are unchanged.

In-place changes to shipped generations

None. Both generations edited here, validate_schema_compatibility v1 and validate_update v1, are selected only by protocol version 14, which is unreleased. Generation 0 of each, used up to protocol version 13, is byte-identical; should_hard_error_on_a_token_cost_diff_at_protocol_version_13 pins its outcome.

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
  • 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

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

🤖 Generated with Claude Code

PR Hygiene · 29fc033

  • Bots — coderabbitai not yet · thepastaclaw not yet — /skip-bots proceeds without the ones not yet reported
  • Self-review — post /self-reviewed once the bots are done
  • Within your 5 open PRs
  • Build failed
  • Approvals — you own every area touched; none needed

When every box is checked the PR Hygiene check passes and this can merge.

…th a consensus error (PV14)

The protocol version 14 schema differ (validate_schema_compatibility v1)
fails as an unsupported keyword, a ProtocolError, on any diff under a
keyword the shared rule set has no rule for. Meta-schema v3 admits
nineteen such keywords that are not stripped before the diff, among them
tokenCost, ttl, actionFees, indexOnly, entryPayload, the keeps*History
and aggregate flags, canBeDeletedByModerators(For), minProperties,
maxProperties and contains. A contract update touching one was refused as
an internal error instead of a consensus error. That held for any
tokenCost change, which nothing compared on its parsed value, and for any
edit that leaves a parsed value unchanged, such as writing out a default
or switching to the documentsAverageable shorthand.

- The v1 differ gives each of those keywords the frozen rule the property
  refersTo has, so any change to one is an incompatible schema change
  (IncompatibleDocumentTypeSchemaError, 10246). A test fails when
  meta-schema v3 admits a keyword the differ has no rule for.
- validate_update v1 compares the parsed token costs of every action and
  refuses an added, changed or removed cost with DocumentTypeUpdateError
  (40212), as it does for actionFees and ttl.

Both generations are selected only by protocol version 14. Generation 0,
used up to protocol version 13, is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 5 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Repository: dashpay/platform/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: e13e474a-2040-4ff1-ae90-983fb4b4e289

📥 Commits

Reviewing files that changed from the base of the PR and between b60bcdb and 29fc033.

📒 Files selected for processing (3)
  • packages/rs-dpp/src/data_contract/document_type/methods/validate_update/v1/mod.rs
  • packages/rs-dpp/src/data_contract/document_type/schema/validate_schema_compatibility/v1/mod.rs
  • packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/data_contract_update/mod.rs

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.

@github-actions github-actions Bot added this to the v4.2.0 milestone Sep 27, 2026
@github-actions github-actions Bot added the waiting-bots Waiting for the review bots to report on this head label Sep 27, 2026
@thepastaclaw

thepastaclaw commented Sep 27, 2026 •

Copy link
Copy Markdown
Collaborator

🕓 Queued for automated review — 1st in line, estimated start in ~40 min (commit 29fc033)
Estimated review time once started: ~1.4 h (two-phase automated review; median of recent runs).

  • Request priority review — click to move this review to the front of the queue.

@QuantumExplorer
QuantumExplorer merged commit e090ae2 into v4.2-dev Sep 27, 2026
31 of 35 checks passed
@QuantumExplorer
QuantumExplorer deleted the claude/charming-bell-c727b9 branch September 27, 2026 17:31
QuantumExplorer added a commit that referenced this pull request Sep 27, 2026
…chapters

#5069 and #5074 gave the keywords the schema comparison had no rule
for a consensus error. The chapters now say so: a tokenCost change is
refused with DocumentTypeUpdateError (40212); minProperties,
maxProperties and contains changes with
IncompatibleDocumentTypeSchemaError (10246); a text-only edit of a
keyword with 10246; and a reordered entryPayload is no change.

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

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