Skip to content

fix(platform)!: refuse a by-id refersTo to an indexOnly document type at registration (PV14) - #5284

Merged
QuantumExplorer merged 1 commit into
v5.0-devfrom
fix/refers-to-index-only-target
Oct 5, 2026
Merged

QuantumExplorer merged 1 commit into
v5.0-devfrom
fix/refers-to-index-only-target

Conversation

@QuantumExplorer

@QuantumExplorer QuantumExplorer commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

Basic explanation

Some document types are indexOnly: their documents are stored only as index entries, with no stored document behind them. Because of that, Drive cannot fetch one of their documents by its id.

A refersTo reference that points at a document by its id (permanentDocument, deletableDocument or moderatedDocument without findBy) needs exactly that fetch every time a referring document is written. Until now a contract could register such a reference to an indexOnly type, and then every write to the referring type failed with an internal error and was dropped without being paid for. The referring type was unusable.

This PR refuses that reference when the contract is registered, with a new consensus error, ReferencedDocumentTypeIndexOnlyError (40146). A findBy reference to an indexOnly type was already refused and keeps its existing error.

Issue being fixed or feature implemented

Registration (validate_data_contract_references 0) only compared the declared reference kind with document_reference_kind() of the referenced type. An indexOnly type with canBeDeleted: false reports Permanent, and one that allows deletion reports Deletable, so a by-id reference of the matching kind passed. Only findBy refused indexOnly targets.

At write time the document reference validation calls fetch_document_with_id, which reaches Drive query code that returns QuerySyntaxError::Unsupported("indexOnly documents cannot be fetched by id ..."). process_raw_state_transitions turns that into StateTransitionExecutionResult::InternalError, and prepare_proposal drops the transition unpaid. The same happens in the deletable-target-gone check.

What was done?

  • New state error ReferencedDocumentTypeIndexOnlyError (code 40146, appended to StateError at discriminant 171), carrying the referenced contract id, the document type name and the declaration path, like its 40122/40131/40143/40144 siblings.
  • validate_data_contract_references_v0 refuses any document reference without a lookup (by id, or with inList, whose list's document is also fetched by id) whose referenced type is indexOnly. It runs before the kind check, since no kind fixes it, and covers same-contract and cross-contract targets alike.
  • wasm-dpp maps the new error; wasm-dpp2 adds DocumentReferenceErrorCode.ReferencedDocumentTypeIndexOnly = 40146 with its from_code arm and the mirror test.
  • Book: the error-codes range is now 40100-40146; refers-to.md gets the registration rule and the error table row; index-only.md says no document reference can target such a type.
  • Protocol version 14 note 75 in v14.rs.

Before (registration of this contract succeeded; every note create then failed with an internal error):

"note": {
  "properties": {
    "pinId": {
      "type": "array", "byteArray": true, "minItems": 32, "maxItems": 32,
      "contentMediaType": "application/x.dash.dpp.identifier", "position": 1,
      "refersTo": { "type": "permanentDocument", "documentType": "pin" }
    }
  }
},
"pin": { "type": "object", "indexOnly": true, "documentsMutable": false, "canBeDeleted": false, ... }

After: the contract create is refused, paid:

PaidConsensusError: ReferencedDocumentTypeIndexOnlyError (40146)
referenced document type pin in contract <id> is indexOnly: its documents exist only as index entries and can not be fetched by id, so the document reference at path note.pinId could never be checked; an indexOnly document type can not be the target of a document reference

A findBy into an indexOnly type of another contract still fails as before, with ReferencedDocumentLookupInvalidError (40137), because an indexOnly type has no unique index.

In-place changes to shipped generations

  • validate_data_contract_references v0 (data_contract_reference_validation: 0, selected by every protocol version's table) gains the indexOnly refusal in place. This cannot change consensus on any released version: only parser generation 3, selected from protocol version 14 (unreleased), admits an indexOnly document type, so before protocol version 14 no referenced type is ever indexOnly and the new branch is never taken. The reason is also stated in a note at the line.

How Has This Been Tested?

New registration tests in data_contract_create (permanent_document_reference_declarations), with a foreign fixture contract holding two indexOnly types:

  • should_reject_a_permanent_reference_by_id_to_an_index_only_type_of_the_same_contract: 40146 at note.pinId.
  • should_reject_a_deletable_reference_by_id_to_an_index_only_type_of_another_contract: 40146 at note.likeId, naming the other contract.
  • should_keep_refusing_a_find_by_into_an_index_only_type_as_an_invalid_lookup: still 40137 ("index "byBallot" of "vote" over (ballotId) is not unique").

Also: the StateError discriminant test pins 171, and the wasm-dpp2 code mirror test covers 40146.

Run locally:

  • cargo test -p drive-abci --lib -- permanent_document_reference_declarations index_only: 87 passed (the whole reference registration module and every indexOnly test).
  • cargo test -p dpp --lib -- errors::consensus::state::state_error: 2 passed.
  • cargo test -p wasm-dpp2 --lib -- consensus_error: 10 passed.
  • cargo check -p wasm-dpp: clean.

Breaking Changes

None on released protocol versions. On protocol version 14 (unreleased) a contract declaring a by-id document reference to an indexOnly type is now refused at registration.

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 · 1c31082

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

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

Summary by CodeRabbit

  • New Features
    • Document references by ID, including list-based references, to index-only document types are now rejected during contract registration with error code 40146.
    • References using findBy continue to follow their existing validation rules.
  • Documentation
    • Documented the index-only reference restriction and its registration-time error.

… at registration (PV14)

An indexOnly document type keeps its documents only as index entries, and
Drive refuses to fetch one by id. A permanentDocument, deletableDocument or
moderatedDocument reference without findBy (or with inList) to such a type
passed registration, since only the reference kind was compared, and every
write resolving it then failed with an internal error, dropped unpaid.

The contract reference validation 0 now refuses it, for same-contract and
cross-contract targets, with ReferencedDocumentTypeIndexOnlyError (40146,
StateError discriminant 171). In place: only parser generation 3, selected
from protocol version 14, admits an indexOnly document type. A findBy into
one keeps its own refusal (40137).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added this to the v5.0.0 milestone Oct 5, 2026
@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

📖 Book Preview built successfully.

Download the preview from the workflow artifacts.
To view locally: download the artifact, unzip, and open index.html.

Updated at 2026-10-05T12:12:35.069Z

@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Waiting for bot review — coderabbitai ✓ · 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 Oct 5, 2026
@coderabbitai

coderabbitai Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

🧰 Additional context used
📚 Code guidelines (1)
AGENTS.md — auto-discovered

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: dashpay/platform/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 555e46e6-139d-4447-9719-c92cb3c41630
📥 Commits

Reviewing files that changed from the base of the PR and between 3f029d8 and 1c31082.

📒 Files selected for processing (17)
  • book/src/contract-keywords/index-only.md
  • book/src/contract-keywords/refers-to.md
  • book/src/error-handling/error-codes.md
  • packages/rs-dpp/src/errors/consensus/codes.rs
  • packages/rs-dpp/src/errors/consensus/state/document/mod.rs
  • packages/rs-dpp/src/errors/consensus/state/document/referenced_document_type_index_only_error.rs
  • packages/rs-dpp/src/errors/consensus/state/state_error.rs
  • packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/data_contract_common/data_contract_reference_validation/v0/mod.rs
  • packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/data_contract_create/mod.rs
  • packages/rs-drive-abci/tests/supporting_files/contract/reference-validation/reference-validation-contract-deletable-doc-registration-index-only.json
  • packages/rs-drive-abci/tests/supporting_files/contract/reference-validation/reference-validation-contract-index-only-foreign.json
  • packages/rs-drive-abci/tests/supporting_files/contract/reference-validation/reference-validation-contract-lookup-registration-index-only.json
  • packages/rs-drive-abci/tests/supporting_files/contract/reference-validation/reference-validation-contract-permanent-doc-registration-index-only.json
  • packages/rs-platform-version/src/version/v14.rs
  • packages/wasm-dpp/src/errors/consensus/consensus_error.rs
  • packages/wasm-dpp2/src/consensus_error.rs
  • packages/wasm-dpp2/tests/unit/DocumentPropertyReference.spec.ts

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The change rejects ID-based document references to index-only types during contract registration with consensus error 40146. It adds the error to consensus and WASM mappings, tests the validation cases, and documents the rule and its protocol version.

Changes

Index-only reference validation

Layer / File(s) Summary
Error 40146 definition and registration
packages/rs-dpp/src/errors/consensus/state/document/..., packages/rs-dpp/src/errors/consensus/state/state_error.rs, packages/rs-dpp/src/errors/consensus/codes.rs
Adds ReferencedDocumentTypeIndexOnlyError with contract ID, document type, and reference path details. Registers it as a StateError, assigns code 40146, and pins its discriminant to 171.
Reference validation and test coverage
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/data_contract_common/..., packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/data_contract_create/mod.rs, packages/rs-drive-abci/tests/supporting_files/contract/reference-validation/*
Rejects references without a lookup when the target type is index-only. Tests cover permanent and deletable ID references and confirm that findBy retains its existing lookup validation. Adds contract fixtures for these cases.
WASM error mappings
packages/wasm-dpp/src/errors/consensus/consensus_error.rs, packages/wasm-dpp2/src/consensus_error.rs, packages/wasm-dpp2/tests/unit/DocumentPropertyReference.spec.ts
Adds conversion and code mappings for error 40146, with tests for the exported mapping.
Protocol and user documentation
packages/rs-platform-version/src/version/v14.rs, book/src/contract-keywords/*, book/src/error-handling/error-codes.md
Documents the v14 validation rule, the distinction between ID and findBy references, and error code 40146.

Priority: ➖ Normal

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

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 1c310

This change adds a registration-time refusal for by-id references to index-only document types, with consistent error mapping and documentation. No concrete merge-blocking risk was identified.

Security Architecture Review

Security architecture risk: 🔵 Low · up to 1c310

The change rejects unusable references earlier without adding permissions. No introduced security concern was established in the inspected registration paths. Protocol rollout and recovery behavior remain only partially assessed.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The directly affected surface is authenticated contract creation and update, including references to another contract's document-type metadata. The inspected change narrows accepted schemas; it does not add cross-contract write authority or access to document contents.

Trust Boundaries and Controls

  • observed — Signature authorization precedes state validation. Invalid references do not retain contract-create or contract-update actions; they retain the prescribed nonce-bump action and consensus errors. Rejection therefore is not side-effect-free: signer nonce and fee accounting remain intentional shared-state effects.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 58.82% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 17 functions across 10 files. (7 skipped:… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: rejecting by-ID references to indexOnly document types at registration.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 58.82% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 17 functions across 10 files. (7 skipped: 7 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · 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

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

@thepastaclaw

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 1c31082. Normal review starts when eligible; priority review starts as soon as a slot is available.

@QuantumExplorer

Copy link
Copy Markdown
Member Author

Reviewed

@QuantumExplorer
QuantumExplorer merged commit 8061667 into v5.0-dev Oct 5, 2026
23 of 24 checks passed
@QuantumExplorer
QuantumExplorer deleted the fix/refers-to-index-only-target branch October 5, 2026 12:22
@PastaPastaPasta PastaPastaPasta mentioned this pull request Oct 5, 2026
1 of 22 tasks
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