Skip to content

fix(drive)!: never serve a contract parsed under another protocol version from the cache - #5993

Merged
shumkov merged 1 commit into
v5.1-devfrom
shumkov/v51-contract-cache-version
Nov 19, 2026
Merged

shumkov merged 1 commit into
v5.1-devfrom
shumkov/v51-contract-cache-version

Conversation

@shumkov

@shumkov shumkov commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Issue being fixed or feature implemented

A data contract is parsed under the protocol version of whoever reads it, and two protocol versions can parse the same stored contract differently. Protocol version 15 already does for some contracts: since #5987 a document type whose summed property is not bounded within 2^27 keeps its sums in 128 bits. #5983 adds another difference: nested object members in position order.

Query threads read committed state at the protocol version of the platform state they loaded, and they fill the shared contract cache with what they parse. On the first block of a new protocol version that is still the previous version. The same holds for a query that loaded the state just before the block's state was published. A block whose transactional read fell back to such a cached copy could then write different state than a node with an empty cache, for example a sum tree of a different width, or a token configuration update storing differently compiled contract bytes. The emptying of the cache on the first block of a protocol change does not cover this: a query can put its copy back during that block or after the promotion.

This must merge before protocol version 15 ships. #5983 depends on it.

What was done?

  • DataContractFetchInfo records the protocol version that parsed it (parsed_under_protocol_version). It is set where the contract is parsed (fetch_contract_v0).
  • DataContractCache::get_parsed_under treats an entry parsed under another protocol version as a miss:
    • In the block cache, there is no fallback to the global cache.
    • In the global cache, the reader reads state, as a node with an empty cache does.
    • get is unchanged and version-blind.
  • get_contract_with_fetch_info generation 1, selected only by protocol version 15 (DRIVE_CONTRACT_METHOD_VERSIONS_V5 → DRIVE_VERSION_V10), differs from generation 0 in two ways:
    • It looks up through get_parsed_under.
    • A cache hit is billed its cost priced under the reader's protocol version, never a fee stored on the entry. Generation 0 can store a fee that a reader of another protocol version priced on an entry. That is latent today, because 14 and 15 price contract reads alike.
    • So a protocol 15 block reads and bills every contract exactly as a cold read does.
  • Generation 0 stays version-blind for protocol versions 14 and earlier, as their blocks were executed. Up to protocol version 11, and for user contracts at 13's activation, those blocks were served parses made under the previous version. An always-on check would make a replay reparse there, which cannot be shown to be inert.
  • v15 note 51, the drive version docs, the DataContractCache / DataContractFetchInfo docs, the token configuration update comment (which assumed 14 and 15 compile a contract alike) and the book's cache section (drive/finalize-tasks.md) describe the rule.
  • Precedent for a cache keyed by protocol version: SystemDataContracts keys on (protocol_version, contract).

In-place changes to shipped generations

Generation Selected by Change Why consensus cannot change
fetch_contract 0 (fetch_contract_v0) every protocol version sets parsed_under_protocol_version on the entry it builds Only get_contract_with_fetch_info 1, selected only by protocol version 15, reads the field.
get_contract_with_fetch_info 0 1–14 the fee-refresh copy of a cache hit copies the field Generation 0 never reads it, and generation 1 never bills a stored fee, so a fee generation 0 prices never reaches a reader of another version.
perform_events_on_first_block_of_protocol_change 2 14 (and 15 through 3) doc comment only No code change.
token_config_update_transition converter 0 (token_config_update_transition.rs) every protocol version comment only: the compiled bytes follow the version that parsed the cache entry, which from protocol version 15 is always the block's No code change.

How Has This Been Tested?

Red first, then green:

Test Red (failure)
big_sums_tests::should_write_a_tip_after_the_activation_as_a_node_without_a_lagging_query_does: after the activation block (conversion, commit, promotion), the entry is evicted and a protocol 14 query caches its reading; the next protocol 15 block adds a tip with the contract the cache serves. The state must match a node without that query. Different root hashes.
parse_version::should_not_serve_a_block_at_protocol_version_15_the_reading_a_protocol_version_14_query_cached The block is served the 64-bit sums reading.
parse_version::should_never_serve_a_block_at_protocol_version_15_a_reading_a_concurrent_protocol_version_14_query_caches: a query thread at 14 runs next to 20 blocks at 15, each evicting, waiting for the query's insert, then reading, committing and promoting. 20 of 20 block reads got the protocol 14 reading.
parse_version::should_check_the_protocol_version_of_a_cached_copy_only_from_generation_1: generation 0 at 14 still serves a cross-version copy (pin); generation 1 at 15 does not.
parse_version::should_bill_a_cache_hit_at_protocol_version_15_what_a_cold_read_bills_whatever_fee_the_entry_carries The stored fee was billed (1 vs 29120).
  • The first four were red on unchanged v5.1-dev, or with generation 0 selected at protocol version 15. The fee test was red with the first draft of generation 1.
  • Cache unit tests for get_parsed_under.
  • cargo test -p drive -p drive-abci -p platform-version --all-features --lib --tests -- --skip shield (CI's filter): all pass. That is drive 4419, drive-abci 4561, strategy tests 125, platform-version 27, plus the integration test binaries. The shield-named tests of drive and drive-abci separately: 622 passed. The two shield characterizations in nullifier_batch_guard hash the Debug output of the validated action, which now names the protocol version its cached contract was parsed under. They strip that field, as they strip the other incidental Debug fields, and keep their original pins: no action, charge, operation, fee or root changed.
  • cargo clippy -p drive -p drive-abci -p platform-version --all-features --all-targets: clean.

Breaking Changes

From protocol version 15, a cached data contract is served only to a reader at the protocol version that parsed it, and a cache hit is billed its cost priced at the reader's protocol version. DataContractFetchInfo has a new crate-private field. Protocol versions 14 and earlier are unchanged.

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 · 67d558e

  • Bots — thepastaclaw ✓
  • 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

  • New Features
    • From protocol version 15, cached contracts are used only when they were parsed under the reader’s protocol version. Otherwise, the contract is read from state.
    • Cache-hit costs are calculated using the reader’s protocol version.
  • Bug Fixes
    • Prevented contract queries made during a protocol upgrade from causing later reads to use a contract parsed under the previous version.
    • Added regression coverage confirming consistent results when a query uses a lagging protocol version.

@shumkov shumkov added this to the v5.1.0 milestone Nov 19, 2026
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Contract cache entries now record the protocol version used to parse them. From protocol version 15, generation 1 serves cached contracts only to readers at the matching version and prices cache hits under the reader’s version.

Changes: Protocol-version-scoped contract cache

Layer / File(s) Summary
Record and check contract parse versions
packages/rs-drive/src/drive/contract/contract_fetch_info.rs, packages/rs-drive/src/cache/data_contract.rs, packages/rs-drive/src/drive/contract/get_fetch/fetch_contract/v0/mod.rs
DataContractFetchInfo records the parsing protocol version. DataContractCache adds a lookup that returns an entry only when its recorded version matches the reader’s version. Contract fetch paths populate the field.
Implement generation 1 cache reads
packages/rs-drive/src/drive/contract/get_fetch/get_contract_with_fetch_info/*
Generation 1 routes reads through a version-matching cache lookup. A miss fetches from state. A cache hit with an epoch is repriced under the requested platform version. Tests cover version isolation, generation 0 behavior, concurrent cache fills, and fee calculation.
Select generation 1 for protocol 15
packages/rs-platform-version/src/version/drive_versions/*, packages/rs-platform-version/src/version/v15.rs, book/src/drive/finalize-tasks.md, packages/rs-drive-abci/src/execution/platform_events/.../v2/mod.rs, packages/rs-drive/src/state_transition_action/.../token_config_update_transition.rs
Protocol 15 selects get_contract_with_fetch_info generation 1. Documentation describes the version-matching cache rule and the difference from protocol 14 and earlier.
Update fixtures and validate versioned reads
packages/rs-drive/src/drive/document/migration/big_sums_tests.rs, packages/rs-drive/src/state_transition_action/*, packages/rs-drive/src/drive/contract/refresh_cache/mod.rs, packages/rs-drive/src/drive/document/index_uniqueness/mod.rs, packages/rs-drive-abci/src/execution/validation/.../nullifier_batch_guard.rs
Test fixtures initialize the parsing-version field. Validation snapshots exclude that field from hashes. A regression test compares root hashes with and without a lagging protocol-14 query between protocol-15 blocks.

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

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Reader
  participant Drive
  participant DataContractCache
  participant ContractState
  Reader->>Drive: request contract for platform version
  Drive->>DataContractCache: get_parsed_under(contract_id, platform_version)
  DataContractCache-->>Drive: matching entry or cache miss
  Drive->>ContractState: fetch contract on cache miss
  ContractState-->>Drive: contract fetch information
  Drive->>DataContractCache: cache fetched entry when permitted
  Drive-->>Reader: contract information and optional repriced fee
Loading

Possibly related PRs

  • dashpay/platform#5579: Stores protocol-15 contracts in a new format with compiled document types, related to this change’s protocol-version-specific contract parsing.

Suggested reviewers: quantumexplorer, lklimek, thepastaclaw

Merge Risk: Minimal · up to 67d55

From protocol version 15, contract cache reads are limited to readers at the version that parsed the entry. A lagging older-version query can no longer change what a later block reads. Versions 14 and earlier keep their existing behavior. No merge-blocking risk is evident from the supplied changes.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title says the cache no longer serves a contract parsed under another protocol version. The changeset matches this. DataContractCache gains get_parsed_under, which returns an entry only when its p…
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.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title says the cache no longer serves a contract parsed under another protocol version. The changeset matches this. DataContractCache gains get_parsed_under, which returns an entry only when its parsed_under_protocol_version matches the reader's version. DataContractFetchInfo stores that version. get_contract_with_fetch_info generation 1 is selected for protocol version 15, and a mismatched entry is treated as a miss. The title names the main behavior change, and the fix (!) marker matches the breaking change from protocol version 15.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • 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 f3bdf54 and 67d558e.

Files selected for processing: 26

  • book/src/drive/finalize-tasks.md
  • packages/rs-drive-abci/src/execution/platform_events/protocol_upgrade/perform_events_on_first_block_of_protocol_change/v2/mod.rs
  • packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/shielded/nullifier_batch_guard.rs
  • packages/rs-drive/src/cache/data_contract.rs
  • packages/rs-drive/src/drive/contract/contract_fetch_info.rs
  • packages/rs-drive/src/drive/contract/get_fetch/fetch_contract/v0/mod.rs
  • packages/rs-drive/src/drive/contract/get_fetch/get_contract_with_fetch_info/mod.rs
  • packages/rs-drive/src/drive/contract/get_fetch/get_contract_with_fetch_info/v0/mod.rs
  • packages/rs-drive/src/drive/contract/get_fetch/get_contract_with_fetch_info/v1/mod.rs
  • packages/rs-drive/src/drive/contract/refresh_cache/mod.rs
  • packages/rs-drive/src/drive/document/index_uniqueness/mod.rs
  • packages/rs-drive/src/drive/document/migration/big_sums_tests.rs
  • packages/rs-drive/src/state_transition_action/action_convert_to_operations/batch/token/token_config_update_transition.rs
  • packages/rs-drive/src/state_transition_action/action_convert_to_operations/shielded/private_document_create_from_shielded_pool_transition.rs
  • packages/rs-drive/src/state_transition_action/batch/batched_transition/token_transition/token_base_transition_action/mod.rs
  • packages/rs-drive/src/state_transition_action/batch/batched_transition/token_transition/token_base_transition_action/v0/mod.rs
  • packages/rs-drive/src/state_transition_action/batch/batched_transition/token_transition/token_burn_transition_action/mod.rs
  • packages/rs-drive/src/state_transition_action/batch/batched_transition/token_transition/token_config_update_transition_action/mod.rs
  • packages/rs-drive/src/state_transition_action/batch/batched_transition/token_transition/token_destroy_frozen_funds_transition_action/mod.rs
  • packages/rs-drive/src/state_transition_action/batch/batched_transition/token_transition/token_mint_transition_action/mod.rs
  • packages/rs-drive/src/state_transition_action/batch/batched_transition/token_transition/token_mint_transition_action/v0/mod.rs
  • packages/rs-drive/src/state_transition_action/system/bump_identity_data_contract_nonce_action/transformer.rs
  • packages/rs-drive/src/state_transition_action/system/bump_identity_data_contract_nonce_action/v0/transformer.rs
  • packages/rs-platform-version/src/version/drive_versions/drive_contract_method_versions/v5.rs
  • packages/rs-platform-version/src/version/drive_versions/v10.rs
  • packages/rs-platform-version/src/version/v15.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 19, 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-11-19T14:16:08.692Z

@github-actions

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

thepastaclaw commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Final review complete — no blockers (commit 67d558e) · triage: critical

…sion from the cache

A contract is parsed under the protocol version of its reader, and protocol
version 15 parses some stored contracts differently from 14 (a document type
whose summed property is not bounded within 2^27 keeps its sums in 128 bits).
Query threads read committed state at the protocol version of the platform
state they loaded, which on the first block of a version, and for a query that
loaded it just before, is the previous one, and they cache what they parsed. A
block that read such a cached copy wrote state another node, with an empty
cache, would not.

Every DataContractFetchInfo now records the protocol version that parsed it
(set where the contract is parsed, fetch_contract_v0). get_contract_with_fetch_info
generation 1, selected only by protocol version 15 (DRIVE_CONTRACT_METHOD_VERSIONS_V5):
- looks up through DataContractCache::get_parsed_under, which treats an entry
  parsed under another protocol version as a miss (no fallback to the global
  cache from the block cache);
- bills a cache hit its cost priced under the reader's protocol version, never
  a fee stored on the entry, since generation 0 can store a fee a reader of
  another version priced (check_tx estimation writes the cache through
  fetch_bound_root_with_fee).
Generation 0 keeps the version-blind lookup for protocol versions 14 and
earlier, as their blocks were executed. v15 note 51.

In-place changes to shipped generations (inert): fetch_contract_v0 records the
new field, read only by generation 1; the fee-refresh copy in
get_contract_with_fetch_info_v0 copies it, and generation 1 never bills a
stored fee.

The shield characterizations in nullifier_batch_guard hash the Debug output of
the validated action, which now names the protocol version its cached contract
was parsed under. They strip that field, as they strip the other incidental
Debug fields, and keep their original pins: with only that field removed every
pinned hash still matches (actions, errors, context charges, lowered
operations, exact fees, roots and nonces), so no charge changed.

Tests would have caught this in CI: ✖ before the fix (on unchanged v5.1-dev,
and again with generation 0 selected at protocol version 15), ✔ after:
- big_sums_tests::should_write_a_tip_after_the_activation_as_a_node_without_a_lagging_query_does
  (different root hashes before the fix)
- get_contract_with_fetch_info parse_version::should_not_serve_a_block_at_protocol_version_15_the_reading_a_protocol_version_14_query_cached
- parse_version::should_never_serve_a_block_at_protocol_version_15_a_reading_a_concurrent_protocol_version_14_query_caches
  (threaded; 20 of 20 block reads got the protocol version 14 parse before the fix)
- parse_version::should_check_the_protocol_version_of_a_cached_copy_only_from_generation_1
- parse_version::should_bill_a_cache_hit_at_protocol_version_15_what_a_cold_read_bills_whatever_fee_the_entry_carries
  (✖ with a generation 1 that trusted the stored fee: billed 1 instead of 29120)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@shumkov
shumkov force-pushed the shumkov/v51-contract-cache-version branch from aecd199 to 67d558e Compare November 19, 2026 14:15
@github-actions

github-actions Bot commented Nov 19, 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 1 + Phase 2

The protocol-versioned contract cache changes are consistent with the existing cache lifecycle and versioned dispatch architecture. Generation 1 is selected only by protocol version 15, rejects entries parsed under another version, and reprices cache hits from the reader’s cost; earlier generations retain their historical behavior. No in-scope correctness, consensus, security, layering, or Rust-quality findings were identified. The Rust workspace test check was still in progress in the supplied CI snapshot, with no failed code checks caused by this change.

Review provenance

Source: reviewer 1: muse-spark-1.3-contributor (agent: phase1-reviewer, role: general); reviewer 2: muse-spark-1.3-contributor (agent: phase1-reviewer, role: rust-quality); reviewer 3: gpt-6.2-astra (agent: phase2-reviewer, role: general); reviewer 4: gpt-6.2-astra (agent: phase2-reviewer, role: architecture-layering); reviewer 5: gpt-6.2-astra (agent: phase2-reviewer, role: platform-versioning); reviewer 6: gpt-6.2-astra (agent: phase2-reviewer, role: rust-quality); reviewer 7: gpt-6.2-astra (agent: phase2-reviewer, role: security-auditor); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)

  • Triage: critical by gpt-6.2-sol (effort low) — This is a large, intricate change to consensus-critical contract parsing, protocol-version dispatch, cache behavior, persisted state interpretation, and fee calculation in rs-drive, including migration-sensitive logic that can change committed state across protocol activation.
  • Phase 1 reviewers: muse-spark-1.3-contributor — general (completed, effort xhigh); agent phase1-reviewer, muse-spark-1.3-contributor — rust-quality (completed, effort xhigh); agent phase1-reviewer
  • Phase 1 model: muse-spark-1.3-contributor — not quota-gated; passed over gemini-3.8-flash-high (antigravity quota lookup failed), glm-5.3-flash (not used above high effort; tier asks max)
  • Single stage: Phase 1 and Phase 2 reviewed this head side by side, with no blocker gate between them (triage tier)
  • Fresh verifier: gpt-6.2-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.2-astra — general (completed, effort high); agent phase2-reviewer, gpt-6.2-astra — architecture-layering (completed, effort high); agent phase2-reviewer, gpt-6.2-astra — platform-versioning (completed, effort high); agent phase2-reviewer, gpt-6.2-astra — rust-quality (completed, effort high); agent phase2-reviewer, gpt-6.2-astra — security-auditor (completed, effort high); agent phase2-reviewer

@github-actions

Copy link
Copy Markdown
Contributor

Bots are done — your move: post /self-reviewed.
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 19, 2026
@shumkov
shumkov merged commit 23c3e37 into v5.1-dev Nov 19, 2026
35 of 36 checks passed
@shumkov
shumkov deleted the shumkov/v51-contract-cache-version branch November 19, 2026 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting-self-review Waiting for the author to post /self-reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants