Skip to content

feat: ranked aggregate indexes with provable top-K queries (protocol v14) - #4266

Open
QuantumExplorer wants to merge 8 commits into
v4.2-devfrom
claude/grovedb-pr657-integration-b6f426
Open

feat: ranked aggregate indexes with provable top-K queries (protocol v14)#4266
QuantumExplorer wants to merge 8 commits into
v4.2-devfrom
claude/grovedb-pr657-integration-b6f426

Conversation

@QuantumExplorer

@QuantumExplorer QuantumExplorer commented Aug 2, 2026

Copy link
Copy Markdown
Member

Issue being fixed or feature implemented

Platform's 4.0 aggregates can compute counts, sums and averages over document groups, but answering "which groups rank highest?" (e.g. top 5 restaurants by average grade) requires scanning every group — O(n) work with an O(n) proof, so it was never offered.

GroveDB's indexed-tree family (dashpay/grovedb#657, follow-ups in dashpay/grovedb#781) pairs each aggregate-bearing tree with per-axis ordered secondaries, making provable top-K reads O(log n + k). This PR integrates that at the contract level and activates it at protocol v14.

What was done?

Contract grammar (rs-dpp, meta-schema v3, document_type_schema >= 3)

  • New index keywords rankedCountable / rankedSummable / rankedAverageable, each requiring its range* counterpart; parsed into Index / IndexLevelTypeInfo
  • Restricted to single-property, non-unique, non-contested indexes; immutable on contract update (find_first_ranked_change); pre-v14 contracts get the same "unexpected property name" rejection as before

Storage (rs-drive)

  • The index's terminal property-name tree upgrades to ProvableCountIndexedTree / ProvableSumIndexedTree / ProvableCountProvableSumIndexedTree(axes) — a byte-compatible mirror of the tree it replaces, so existing range-aggregate queries keep working on it
  • Resolver drive/document/ranked_index_tree_type.rs used by contract insert/update and all document index walkers; three new batch_insert_empty_provable_*_indexed_tree grove-op helpers; estimation maps indexed trees to their non-indexed mirror weights (estimated ≥ actual verified per variant)

Queries (rs-drive → rs-drive-abci → wire)

  • query/drive_document_ranked_query/: validation (detect_ranked_mode, versioned), executors over indexed_{count,sum,avg}_top_k and prove_indexed_axis_top_k, MAX_RANKED_LIMIT = 100; verify/document_ranked/ wraps GroveDb::verify_indexed_axis_top_k
  • drive-abci document_query/v1: compute_aggregate_mode_and_check_limit v1 routes a single HAVING ranking clause (TOP(n)/BOTTOM(n)/MAX/MIN) to dispatch_ranked_v1; protocol v13 keeps feature version 0 and still rejects every HAVING, so mixed-version networks agree
  • Proto: additive only — RankedEntry/RankedEntries messages and a ResultData.ranked arm; requests reuse the existing selects/group_by/having fields of GetDocumentsRequestV1. Avg entries carry the grovedb fixed-point i128 (scale AVG_FIXED_POINT_SCALE, currently 10^19) as 16 BE bytes; clients divide

Clients (rs-drive-proof-verifier, rs-sdk)

  • DocumentRankedEntries: Fetch with proof verification bound to the signed app hash via verify_tenderdash_proof; ranking_having constructor; empty-ranking-with-proof surfaces as an explicit error (grovedb has no absence envelope for indexed-axis proofs)

Versioning (rs-platform-version)

How Has This Been Tested?

  • rs-dpp index grammar/immutability/gating tests (28) — parsing, desugaring, per-flag rejections, PV13 vs PV14 acceptance
  • rs-drive insert_contract/v0/tests/ranked_index_e2e_tests.rs — all three variants through real batches: registration shape (axes TLV), ranking across inserts/updates/deletes, verify_grovedb integrity after every test; restaurants fixture added
  • rs-drive drive_document_ranked_query/tests.rs (28) — validation matrix, all axes/kinds, prove→verify round-trips against the live root hash, tamper rejection, tie ordering, multi-index prover/verifier index agreement
  • rs-drive-abci document_query/v1/tests.rs ranked module (13) — wire-level requests per axis, proof responses, PV13 rejection, empty-ranking-prove clean error, malformed-shape rejections
  • rs-drive-proof-verifier (12) + rs-sdk (7 unit + doctest) — wire round-trip against the abci ground-truth shape, 16-byte avg decode incl. negatives, order preservation
  • Full suites: cargo test -p drive --lib 3300 · -p dpp --lib 3834 · -p drive-abci --lib query:: 604 · -p platform-version 13 · -p drive-proof-verifier 257 · -p dash-sdk --lib/--doc 181/12 — all 0 failures; cargo check --workspace --all-targets, cargo check -p drive --no-default-features --features verify, clippy clean on all touched crates, cargo fmt clean

Not yet covered (needs a live PV14 network, which doesn't exist): recorded SDK tests/vectors fixtures and live e2e; JS/Java/ObjC/Python generated gRPC clients still need regeneration (yarn workspace @dashevo/dapi-grpc build, docker).

Breaking Changes

None for existing networks or clients: the new consensus behavior is entirely gated behind protocol v14 (a v13→v14 upgrade is a no-op until a contract uses the new grammar), the wire change is additive, and existing aggregate queries are unaffected. Merging does make nodes signal v14 as their desired version.

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

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added ranked aggregate queries using HAVING TOP(n) and BOTTOM(n) for count, sum, and average results.
    • Ranked responses preserve ordering and support proof generation and verification.
    • Added ranked-index configuration for document schemas and compatible response formats.
  • Bug Fixes
    • Improved validation for unsupported ranking forms, malformed queries, and incompatible indexes.
    • Fixed aggregate index handling for shared-prefix and ranked layouts.
  • Documentation
    • Clarified ranking limits, ordering, protocol version 14 requirements, and MIN/MAX limitations.

…l v14

Contracts can now declare that an index's groups are rankable by an
aggregate (rankedCountable / rankedSummable / rankedAverageable, each
requiring its range* counterpart), upgrading the index's terminal
property-name tree to a GroveDB indexed tree (ProvableCountIndexedTree /
ProvableSumIndexedTree / ProvableCountProvableSumIndexedTree) whose
per-axis ordered secondaries are maintained on every document write.
Queries of the form

    SELECT AVG(grade) GROUP BY restaurantId HAVING AVG(grade) IN TOP(5)

are served in O(log n + k) with proofs, over the existing
GetDocumentsRequestV1 wire surface (the only proto change is the
additive RankedEntries response message).

- rs-dpp: meta-schema v3 grammar + validation (single-property,
  non-unique indexes only; flags immutable on contract update), gated
  on document_type_schema >= 3 so pre-v14 contracts cannot declare it
- rs-drive: indexed-tree construction at contract registration,
  secondary maintenance through the normal batch write path, cost
  estimation, the drive_document_ranked_query family and
  verify/document_ranked
- rs-drive-abci: HAVING ranking routing, feature-gated so protocol v13
  nodes keep rejecting HAVING and mixed-version networks agree
- rs-drive-proof-verifier + rs-sdk: DocumentRankedEntries::fetch with
  proof verification bound to the signed app hash
- rs-platform-version: protocol v14, behaviorally identical to v13
  until a contract uses the new grammar
- grovedb pinned to develop 4c6720b, which includes the indexed-tree
  family (grovedb #657) plus the subset-verification regression fix and
  verify-only proof gating shipped via grovedb #781

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@github-actions github-actions Bot added this to the v4.2.0 milestone Aug 2, 2026
@coderabbitai

coderabbitai Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Protocol v14 adds ranked aggregate indexes and HAVING ... TOP/BOTTOM(n) queries for COUNT, SUM, and AVG. The change adds indexed storage, query routing, proof verification, SDK support, protobuf responses, compatibility validation, and generated bindings.

Changes

Ranked aggregate contracts and versioning

Layer / File(s) Summary
Schema and index validation
packages/rs-dpp/..., packages/rs-platform-version/...
Meta-schema v3 and ranked index capabilities are gated by protocol v14. Ranking flags require matching range capabilities and remain immutable during updates.
Wire response contract
packages/dapi-grpc/protos/..., packages/dapi-grpc/clients/...
RankedEntry, RankedEntries, and ResultData.ranked add ordered count, signed sum, and fixed-point average responses.

Ranked index storage

Layer / File(s) Summary
Tree resolution and propagation
packages/rs-drive/src/drive/..., packages/rs-drive/src/fees/...
Drive resolves Count, Sum, and Avg ranking axes and propagates indexed tree metadata through insert, update, delete, continuation, and fee paths.
Indexed tree operations
packages/rs-drive/src/util/grove_operations/...
New operations create single-axis and multi-axis indexed trees. Unsupported wrapper and missing-axis combinations return errors.
Storage validation
packages/rs-drive/src/drive/contract/.../ranked_index_e2e_tests.rs, packages/rs-drive/tests/...
End-to-end tests cover ranking, updates, deletes, shared prefixes, compound continuations, fees, and GroveDB integrity.

Ranked query execution

Layer / File(s) Summary
ABCI routing
packages/rs-drive-abci/src/query/document_query/...
Decoded HAVING clauses route aggregate requests to grouped or ranked execution. Protocol v14 enables the ranked route.
Drive mode detection and execution
packages/rs-drive/src/query/drive_document_ranked_query/...
Drive validates grouping, aggregate projection, ranking operator, limits, filters, and pagination. It then selects a compatible index and executes top-k reads or proofs.
Query validation tests
packages/rs-drive/src/query/drive_document_ranked_query/tests.rs, packages/rs-drive-abci/src/query/document_query/v1/tests.rs
Tests cover COUNT, SUM, AVG, TOP, BOTTOM, ordering, ties, invalid shapes, empty rankings, version gates, and proof tampering.

Proofs and SDK integration

Layer / File(s) Summary
Ranked proof verification
packages/rs-drive/src/verify/..., packages/rs-drive-proof-verifier/...
Ranked proofs bind the indexed path, axis, direction, and limit to the verified root hash. Ranked entries decode count, signed sum, and fixed-point average values.
SDK ranked results
packages/rs-sdk/src/platform/documents/..., packages/rs-sdk/src/mock/...
The SDK constructs ranking clauses, validates ranked requests, verifies ranked proofs, and exposes ordered ranked entries through Fetch and FromProof.

Compatibility and generated support

Layer / File(s) Summary
Compatibility updates
packages/*/Cargo.toml, packages/rs-drive/src/cache/..., packages/rs-sdk-ffi/..., packages/wasm-sdk/...
Grovedb dependencies use the pinned revision. Indexed tree types and aggregate mappings are exposed across FFI and WASM support code.
Generated bindings and fixtures
packages/dapi-grpc/clients/..., packages/rs-drive/tests/supporting_files/...
Generated bindings and restaurant fixtures represent ranked entries and ranked aggregate indexes.

Estimated code review effort: 5 (Critical) | ~120 minutes

Possibly related PRs

  • dashpay/platform#4265: Adds the shared-prefix aggregate-index tree layout used by the ranked-axis propagation and storage changes.

Suggested reviewers: lklimek, llbartekll, zocolini

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the PR's main change: ranked aggregate indexes and provable top-K queries for protocol v14.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/grovedb-pr657-integration-b6f426

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

thepastaclaw commented Aug 2, 2026

Copy link
Copy Markdown
Collaborator

🔍 Review in progress — actively reviewing now (commit 2404e74)
Stage: Codex precheck starting
ETA: complete ~02:05 UTC (median 19m across 30 recent reviews)
Running 19m · Last checked: 2026-08-03 02:00 UTC

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 8

🧹 Nitpick comments (7)
packages/rs-drive/src/query/drive_document_ranked_query/tests.rs (1)

1234-1280: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low value

Confirm the exhaustive bit sweep keeps an acceptable test runtime.

The loop runs 8 * proof.len() full proof verifications. For a proof of a few hundred bytes this is several thousand verifications, and debug-profile CI amplifies the cost. If the measured runtime is high, reduce the sweep to a deterministic sample of byte offsets and keep the "some mutations verify with a diverged root" sanity check.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/rs-drive/src/query/drive_document_ranked_query/tests.rs` around
lines 1234 - 1280, Measure the runtime of the exhaustive mutation loop in
a_tampered_proof_never_verifies_to_the_honest_root_hash; if it is excessive,
replace the full 8 * proof.len() sweep with a deterministic sample of byte
offsets and bit positions. Preserve tampering coverage and the verified_at_all >
0 sanity check so at least some mutations must verify to a divergent root.
packages/rs-drive/src/query/drive_document_ranked_query/mode_detection.rs (1)

44-56: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use DriveError::UnknownVersionMismatch for the unknown method version.

The unknown-version arm returns QuerySyntaxError::Unsupported. In rs-drive-abci that maps to a client-visible query rejection, so a node misconfiguration is reported as a malformed request. The sibling ranked dispatcher DriveDocumentRankedQuery::verify_ranked_top_k_proof uses DriveError::UnknownVersionMismatch for the same condition. Align the two so version faults stay internal errors.

♻️ Proposed change
         0 => detect_ranked_mode_v0(select, group_by, having, where_clauses, pagination),
-        version => Err(Error::Query(QuerySyntaxError::Unsupported(format!(
-            "detect_ranked_mode: unknown method version {version}; only 0 is supported"
-        )))),
+        version => Err(Error::Drive(DriveError::UnknownVersionMismatch {
+            method: "detect_ranked_mode".to_string(),
+            known_versions: vec![0],
+            received: version,
+        })),

Add use crate::error::drive::DriveError; to the imports.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/rs-drive/src/query/drive_document_ranked_query/mode_detection.rs`
around lines 44 - 56, Update the unknown-version arm in the ranked-mode
dispatcher to return DriveError::UnknownVersionMismatch instead of
QuerySyntaxError::Unsupported, matching
DriveDocumentRankedQuery::verify_ranked_top_k_proof. Add the required
crate::error::drive::DriveError import and preserve the supported version-0
dispatch.
packages/rs-drive/src/query/mod.rs (1)

886-898: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Preserve the underlying OrderClause parse error.

QuerySyntaxError::InvalidOrderByProperties only takes &'static str, but OrderClause::from_components returns detailed failures such as non-text fields or invalid asc/desc values. Add a String payload variant here so each rejected clause can report its actual reason and index.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/rs-drive/src/query/mod.rs` around lines 886 - 898, Update the
order_by parsing flow around OrderClause::from_components to preserve its
detailed parse error, including the rejected clause’s reason and index, instead
of replacing it with a static InvalidOrderByProperties message. Add a
String-carrying QuerySyntaxError variant and map the underlying error into it
while keeping the existing non-array validation unchanged.
packages/dapi-grpc/protos/platform/v0/platform.proto (1)

1349-1357: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Add the ranked shapes to the GetDocumentsResponseV1 wire-shape table.

The message-level docstring lists one row per select × group_by × prove combination. It has no row for the ranked path. Add the ranked rows so the table stays the single reference for response routing.

📝 Suggested docstring rows
//   - `having=RANKING` (no prove)               → `result.data.ranked.entries`.
//   - `having=RANKING` (prove)                  → `result.proof`.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/dapi-grpc/protos/platform/v0/platform.proto` around lines 1349 -
1357, Update the GetDocumentsResponseV1 message-level wire-shape docstring to
add rows for having=RANKING without prove routing to result.data.ranked.entries
and having=RANKING with prove routing to result.proof. Keep the existing table
entries unchanged and place the new rows with the other select/group_by/prove
response combinations.
packages/rs-drive/src/fees/op.rs (1)

1095-1102: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider canonicalizing ranked_axes before building the TLV.

The mapped list is passed to grovedb exactly as the caller supplied it. grovedb's validate_pcpsit_axes then rejects an unsorted or duplicated list with an error. Sorting by tag and de-duplicating here would make the helper tolerant of caller ordering, at negligible cost for a list of at most three entries.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/rs-drive/src/fees/op.rs` around lines 1095 - 1102, Canonicalize the
axes in the TreeType::ProvableCountProvableSumIndexedTree branch before passing
them to Self::for_known_path_key_empty_provable_count_provable_sum_indexed_tree:
sort the mapped entries by axis tag and remove duplicate tags. Preserve the
existing None values and storage_flags while ensuring the resulting ranked_axes
list is ordered and unique for grovedb validation.
packages/rs-drive/src/drive/contract/update/update_contract/v0/mod.rs (1)

484-537: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Reuse the generic indexed-tree helper to remove the duplicated dispatch.

The existing-doctype branch above (Lines 358-368) already resolves the same pair and delegates to batch_insert_empty_index_tree_if_not_exists, which handles every tree variant and carries ranked_axes. This branch repeats the whole match, and it is a near-copy of the dispatch in packages/rs-drive/src/drive/contract/insert/insert_contract/v0/mod.rs. Three copies of the same table must stay in lock-step, and a missed arm produces an on-disk layout that differs between fresh insert and contract update. Consider collapsing this branch onto the same helper.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/rs-drive/src/drive/contract/update/update_contract/v0/mod.rs` around
lines 484 - 537, Replace the TreeType match in the new-doctype branch with the
existing generic indexed-tree helper used by the existing-doctype path,
`batch_insert_empty_index_tree_if_not_exists`. Pass through `type_path`,
`KeyRef(index_bytes)`, `ranked_axes`, storage flags, batch operations, and
`drive_version`, preserving the helper’s handling for every tree variant.
packages/rs-drive/src/drive/contract/insert/insert_contract/v0/mod.rs (1)

440-455: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider routing the single-axis arms through the validating dispatcher.

The ProvableCountIndexedTree and ProvableSumIndexedTree arms discard ranked_axes. The element constructors do not need the TLV, so the written tree is correct today. However LowLevelDriveOperation::for_known_path_key_empty_indexed_tree additionally asserts that the resolved axis set matches the variant ([IndexAxis::Count] / [IndexAxis::Sum]). Calling the per-variant helpers directly skips that assertion, so a future resolver change that pairs, for example, ProvableCountIndexedTree with [IndexAxis::Avg] would silently create a tree whose secondary is keyed on the wrong aggregate. Routing all three arms through the dispatcher keeps the invariant enforced in one place.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/rs-drive/src/drive/contract/insert/insert_contract/v0/mod.rs` around
lines 440 - 455, Update the ProvableCountIndexedTree and ProvableSumIndexedTree
arms in the tree insertion dispatch to call
LowLevelDriveOperation::for_known_path_key_empty_indexed_tree, passing
ranked_axes and the existing insertion context. Route all three indexed-tree
variants through this validating dispatcher, preserving the current per-variant
construction behavior while enforcing axis-to-variant validation.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/rs-dpp/schema/meta_schemas/document/v3/document-meta.json`:
- Around line 3-4: Update the v3 meta-schema header’s $comment to identify it as
the v3 document meta-schema, state that it activates with protocol v14 (the
corresponding contract version), and correct the release-specific freeze
wording. Verify the $id compatibility requirement separately; preserve the
existing v1 URL if it is intentionally frozen, and document that it is a
compatibility identifier rather than the file path.

In `@packages/rs-dpp/src/validation/meta_validators/mod.rs`:
- Around line 247-254: Correct the stale v3 document meta-schema comments in
packages/rs-dpp/src/validation/meta_validators/mod.rs (lines 247-254) and
packages/rs-platform-version/src/version/dpp_versions/dpp_contract_versions/v6.rs
(line 54) so they accurately describe the existing rankedCountable,
rankedSummable, rankedAverageable, and dependentRequired validation; remove the
incorrect v6 claim rather than changing schema behavior.

In `@packages/rs-drive-proof-verifier/src/lib.rs`:
- Around line 17-23: Update the rustdoc link in the public re-export
documentation for DocumentRankedEntries to reference the public
verify_ranked_top_k_proof re-export rather than the private
proof::document_ranked path, using the existing public symbol without changing
the surrounding documentation.

In
`@packages/rs-drive/src/drive/contract/insert/insert_contract/v0/tests/ranked_index_e2e_tests.rs`:
- Around line 232-238: Update expected_avg_fixed_point to use Euclidean division
via div_euclid on the scaled sum and count, matching the documented
toward-negative-infinity rounding for negative sums while preserving the
existing calculation and types.

In `@packages/rs-drive/src/drive/document/mod.rs`:
- Around line 56-58: Gate all imports or nearest parent module declarations that
reference ranked_index_tree_type behind the server feature, including
estimated_sum_trees_for_value_tree_type, non-server contract index
insert/update/delete code, and
batch_insert_empty_provable_count_provable_sum_indexed_tree. Ensure non-server
feature builds no longer compile these dependent paths while preserving their
existing server-only behavior.

In
`@packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/v0/mod.rs`:
- Around line 495-503: Update the re-materialization logic around
batch_insert_empty_index_tree_if_not_exists to call
property_name_tree_type_and_ranked_axes(current_index_level) instead of
property_name_tree_type_for_index_level. Ensure the terminal property-name level
of a single-property ranked index is recreated with its indexed tree type and
ranked_axes, while preserving normal-tree behavior for non-ranked levels.

In `@packages/rs-drive/src/query/drive_document_ranked_query/execute_top_k.rs`:
- Around line 135-167: Update execute_top_k_with_proof to detect grovedb’s
dedicated empty-tree error variant, when available, and map it to the
appropriate typed Drive-specific error before the generic Error::GroveDB
conversion. Preserve generic grovedb failures through Error::GroveDB, and keep
the existing empty-ranking limitation documentation without matching
error-message text.

In `@packages/rs-platform-version/Cargo.toml`:
- Line 14: Update the grovedb-version dependency revision in Cargo.toml to a
valid commit or tag resolvable from the configured GroveDB repository, while
retaining the intended GroveDB changes.

---

Nitpick comments:
In `@packages/dapi-grpc/protos/platform/v0/platform.proto`:
- Around line 1349-1357: Update the GetDocumentsResponseV1 message-level
wire-shape docstring to add rows for having=RANKING without prove routing to
result.data.ranked.entries and having=RANKING with prove routing to
result.proof. Keep the existing table entries unchanged and place the new rows
with the other select/group_by/prove response combinations.

In `@packages/rs-drive/src/drive/contract/insert/insert_contract/v0/mod.rs`:
- Around line 440-455: Update the ProvableCountIndexedTree and
ProvableSumIndexedTree arms in the tree insertion dispatch to call
LowLevelDriveOperation::for_known_path_key_empty_indexed_tree, passing
ranked_axes and the existing insertion context. Route all three indexed-tree
variants through this validating dispatcher, preserving the current per-variant
construction behavior while enforcing axis-to-variant validation.

In `@packages/rs-drive/src/drive/contract/update/update_contract/v0/mod.rs`:
- Around line 484-537: Replace the TreeType match in the new-doctype branch with
the existing generic indexed-tree helper used by the existing-doctype path,
`batch_insert_empty_index_tree_if_not_exists`. Pass through `type_path`,
`KeyRef(index_bytes)`, `ranked_axes`, storage flags, batch operations, and
`drive_version`, preserving the helper’s handling for every tree variant.

In `@packages/rs-drive/src/fees/op.rs`:
- Around line 1095-1102: Canonicalize the axes in the
TreeType::ProvableCountProvableSumIndexedTree branch before passing them to
Self::for_known_path_key_empty_provable_count_provable_sum_indexed_tree: sort
the mapped entries by axis tag and remove duplicate tags. Preserve the existing
None values and storage_flags while ensuring the resulting ranked_axes list is
ordered and unique for grovedb validation.

In `@packages/rs-drive/src/query/drive_document_ranked_query/mode_detection.rs`:
- Around line 44-56: Update the unknown-version arm in the ranked-mode
dispatcher to return DriveError::UnknownVersionMismatch instead of
QuerySyntaxError::Unsupported, matching
DriveDocumentRankedQuery::verify_ranked_top_k_proof. Add the required
crate::error::drive::DriveError import and preserve the supported version-0
dispatch.

In `@packages/rs-drive/src/query/drive_document_ranked_query/tests.rs`:
- Around line 1234-1280: Measure the runtime of the exhaustive mutation loop in
a_tampered_proof_never_verifies_to_the_honest_root_hash; if it is excessive,
replace the full 8 * proof.len() sweep with a deterministic sample of byte
offsets and bit positions. Preserve tampering coverage and the verified_at_all >
0 sanity check so at least some mutations must verify to a divergent root.

In `@packages/rs-drive/src/query/mod.rs`:
- Around line 886-898: Update the order_by parsing flow around
OrderClause::from_components to preserve its detailed parse error, including the
rejected clause’s reason and index, instead of replacing it with a static
InvalidOrderByProperties message. Add a String-carrying QuerySyntaxError variant
and map the underlying error into it while keeping the existing non-array
validation unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 6654c4f5-c86c-499b-b898-399c5707474d

📥 Commits

Reviewing files that changed from the base of the PR and between ed4116b and d2af6ae.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (94)
  • packages/dapi-grpc/protos/platform/v0/platform.proto
  • packages/rs-dpp/Cargo.toml
  • packages/rs-dpp/schema/meta_schemas/document/v3/document-meta.json
  • packages/rs-dpp/src/data_contract/document_type/class_methods/try_from_schema/v0/mod.rs
  • packages/rs-dpp/src/data_contract/document_type/class_methods/try_from_schema/v1/mod.rs
  • packages/rs-dpp/src/data_contract/document_type/class_methods/try_from_schema/v2/mod.rs
  • packages/rs-dpp/src/data_contract/document_type/index/mod.rs
  • packages/rs-dpp/src/data_contract/document_type/index/random_index.rs
  • packages/rs-dpp/src/data_contract/document_type/index_level/find_first_change.rs
  • packages/rs-dpp/src/data_contract/document_type/index_level/mod.rs
  • packages/rs-dpp/src/data_contract/document_type/methods/validate_update/v0/mod.rs
  • packages/rs-dpp/src/data_contract/factory/v0/mod.rs
  • packages/rs-dpp/src/data_contract/methods/registration_cost/v1/mod.rs
  • packages/rs-dpp/src/validation/meta_validators/mod.rs
  • packages/rs-drive-abci/Cargo.toml
  • packages/rs-drive-abci/src/query/document_query/v1/compute_aggregate_mode_and_check_limit/mod.rs
  • packages/rs-drive-abci/src/query/document_query/v1/compute_aggregate_mode_and_check_limit/v0/mod.rs
  • packages/rs-drive-abci/src/query/document_query/v1/compute_aggregate_mode_and_check_limit/v1/mod.rs
  • packages/rs-drive-abci/src/query/document_query/v1/mod.rs
  • packages/rs-drive-abci/src/query/document_query/v1/tests.rs
  • packages/rs-drive-proof-verifier/src/lib.rs
  • packages/rs-drive-proof-verifier/src/proof.rs
  • packages/rs-drive-proof-verifier/src/proof/document_ranked.rs
  • packages/rs-drive/Cargo.toml
  • packages/rs-drive/src/cache/system_contracts.rs
  • packages/rs-drive/src/drive/contract/estimation_costs/mod.rs
  • packages/rs-drive/src/drive/contract/insert/insert_contract/v0/mod.rs
  • packages/rs-drive/src/drive/contract/insert/insert_contract/v0/tests/mod.rs
  • packages/rs-drive/src/drive/contract/insert/insert_contract/v0/tests/ranked_index_e2e_tests.rs
  • packages/rs-drive/src/drive/contract/update/update_contract/v0/mod.rs
  • packages/rs-drive/src/drive/document/delete/remove_indices_for_index_level_for_contract_operations/v1/mod.rs
  • packages/rs-drive/src/drive/document/delete/remove_indices_for_top_index_level_for_contract_operations/v1/mod.rs
  • packages/rs-drive/src/drive/document/estimation_costs/estimated_sum_trees_for_value_tree_type.rs
  • packages/rs-drive/src/drive/document/insert/add_indices_for_index_level_for_contract_operations/v1/mod.rs
  • packages/rs-drive/src/drive/document/insert/add_indices_for_top_index_level_for_contract_operations/v1/mod.rs
  • packages/rs-drive/src/drive/document/mod.rs
  • packages/rs-drive/src/drive/document/ranked_index_tree_type.rs
  • packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/v0/mod.rs
  • packages/rs-drive/src/fees/op.rs
  • packages/rs-drive/src/query/drive_document_count_query/tests.rs
  • packages/rs-drive/src/query/drive_document_ranked_query/drive_dispatcher.rs
  • packages/rs-drive/src/query/drive_document_ranked_query/execute_top_k.rs
  • packages/rs-drive/src/query/drive_document_ranked_query/executors/mod.rs
  • packages/rs-drive/src/query/drive_document_ranked_query/executors/top_k_no_proof.rs
  • packages/rs-drive/src/query/drive_document_ranked_query/executors/top_k_proof.rs
  • packages/rs-drive/src/query/drive_document_ranked_query/index_picker.rs
  • packages/rs-drive/src/query/drive_document_ranked_query/mod.rs
  • packages/rs-drive/src/query/drive_document_ranked_query/mode_detection.rs
  • packages/rs-drive/src/query/drive_document_ranked_query/path.rs
  • packages/rs-drive/src/query/drive_document_ranked_query/tests.rs
  • packages/rs-drive/src/query/drive_document_sum_query/tests.rs
  • packages/rs-drive/src/query/mod.rs
  • packages/rs-drive/src/util/grove_operations/batch_insert_empty_provable_count_indexed_tree/mod.rs
  • packages/rs-drive/src/util/grove_operations/batch_insert_empty_provable_count_indexed_tree/v0/mod.rs
  • packages/rs-drive/src/util/grove_operations/batch_insert_empty_provable_count_provable_sum_indexed_tree/mod.rs
  • packages/rs-drive/src/util/grove_operations/batch_insert_empty_provable_count_provable_sum_indexed_tree/v0/mod.rs
  • packages/rs-drive/src/util/grove_operations/batch_insert_empty_provable_sum_indexed_tree/mod.rs
  • packages/rs-drive/src/util/grove_operations/batch_insert_empty_provable_sum_indexed_tree/v0/mod.rs
  • packages/rs-drive/src/util/grove_operations/batch_insert_empty_tree_if_not_exists/mod.rs
  • packages/rs-drive/src/util/grove_operations/batch_insert_empty_tree_if_not_exists/v0/mod.rs
  • packages/rs-drive/src/util/grove_operations/grove_insert_empty_tree/v0/mod.rs
  • packages/rs-drive/src/util/grove_operations/mod.rs
  • packages/rs-drive/src/verify/document_ranked/mod.rs
  • packages/rs-drive/src/verify/document_ranked/verify_ranked_top_k_proof/mod.rs
  • packages/rs-drive/src/verify/document_ranked/verify_ranked_top_k_proof/v0/mod.rs
  • packages/rs-drive/src/verify/mod.rs
  • packages/rs-drive/tests/supporting_files/contract/restaurants/restaurants-contract.json
  • packages/rs-platform-version/Cargo.toml
  • packages/rs-platform-version/src/version/dpp_versions/dpp_contract_versions/mod.rs
  • packages/rs-platform-version/src/version/dpp_versions/dpp_contract_versions/v6.rs
  • packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_query_versions/mod.rs
  • packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_query_versions/v2.rs
  • packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/mod.rs
  • packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v1.rs
  • packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v2.rs
  • packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v3.rs
  • packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v4.rs
  • packages/rs-platform-version/src/version/drive_versions/drive_grove_method_versions/mod.rs
  • packages/rs-platform-version/src/version/drive_versions/drive_grove_method_versions/v1.rs
  • packages/rs-platform-version/src/version/drive_versions/drive_verify_method_versions/mod.rs
  • packages/rs-platform-version/src/version/drive_versions/drive_verify_method_versions/v1.rs
  • packages/rs-platform-version/src/version/drive_versions/mod.rs
  • packages/rs-platform-version/src/version/drive_versions/v9.rs
  • packages/rs-platform-version/src/version/mod.rs
  • packages/rs-platform-version/src/version/protocol_version.rs
  • packages/rs-platform-version/src/version/v14.rs
  • packages/rs-platform-wallet/Cargo.toml
  • packages/rs-sdk-ffi/src/system/queries/path_elements.rs
  • packages/rs-sdk/Cargo.toml
  • packages/rs-sdk/src/mock/requests.rs
  • packages/rs-sdk/src/platform/documents/document_ranked_entries.rs
  • packages/rs-sdk/src/platform/documents/mod.rs
  • packages/rs-sdk/src/platform/documents/ranked_proof_helpers.rs
  • packages/wasm-sdk/src/queries/system.rs

Comment thread packages/rs-dpp/schema/meta_schemas/document/v3/document-meta.json Outdated
Comment thread packages/rs-dpp/src/validation/meta_validators/mod.rs
Comment thread packages/rs-drive-proof-verifier/src/lib.rs Outdated
Comment thread packages/rs-drive/src/drive/document/mod.rs
Comment thread packages/rs-platform-version/Cargo.toml Outdated

@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.

Preliminary review — Codex only

The ranked storage and proof paths are extensively implemented, but three correctness issues block merge: MAX/MIN omit tied extrema, explicit false ranked flags are rejected by the meta-schema, and the committed generated gRPC clients cannot decode ranked responses. The WASM projection also needs two compatibility fixes, while several smaller documentation and test-helper issues remain.

Validated blockers were found in the Codex precheck. Sonnet is deferred until a fresh Codex revalidation clears the blocker gate.

Review provenance

  • Codex reviewers: gpt-5.6-sol — general (completed), gpt-5.6-sol — security-auditor (completed), gpt-5.6-sol — rust-quality (completed), gpt-5.6-sol — ffi-engineer (completed)
  • Verifier: gpt-5.6-sol — verifier
  • Sonnet: not run (deferred by blocker gate)

🔴 3 blocking | 🟡 2 suggestion(s) | 💬 3 nitpick(s)

2 additional finding(s) omitted (not in diff).

🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify each finding against the current code and only fix it if needed.

In `packages/rs-drive/src/query/drive_document_ranked_query/mode_detection.rs`:
- [BLOCKING] packages/rs-drive/src/query/drive_document_ranked_query/mode_detection.rs:187-205: MAX and MIN omit groups tied at the extreme
  The wire and Drive contracts define `HAVING COUNT(*) EQ MAX` as selecting every group whose aggregate equals the maximum scalar. This branch instead converts MAX/MIN into a directional top-K read with `k = 1`. The indexed secondary breaks equal aggregates by group key, so only one tied group is returned and the other groups satisfying the predicate are silently omitted. Reject MAX/MIN until a tie-aware primitive is available, or retrieve and prove every entry tied at the extreme.

In `packages/dapi-grpc/protos/platform/v0/platform.proto`:
- [BLOCKING] packages/dapi-grpc/protos/platform/v0/platform.proto:1349-1357: Regenerate the committed gRPC clients for ranked results
  The protobuf adds `RankedEntry`, `RankedEntries`, and oneof field 5, but the maintained bindings under `packages/dapi-grpc/clients/platform/v0/` contain no ranked symbols. For example, the Node decoder's oneof still lists only `documents|counts|sums|averages` and skips field 5 as unknown, while the Web, Python, and Objective-C APIs expose no ranked accessor. A non-proof ranked response therefore loses its result when decoded by the shipped clients. Run the existing dapi-grpc generation script and commit every maintained binding.

In `packages/rs-dpp/schema/meta_schemas/document/v3/document-meta.json`:
- [BLOCKING] packages/rs-dpp/schema/meta_schemas/document/v3/document-meta.json:486-493: Explicit false ranked flags incorrectly require range axes
  JSON Schema's `dependentRequired` is triggered by property presence rather than its boolean value. Consequently, `"rankedCountable": false` still requires `rangeCountable`, and `"rankedAverageable": false` still requires `rangeAverageable`. This contradicts the fields' “When true” semantics and `Index::try_from_value_map`, which parses false as an ordinary opt-out and only enforces the range prerequisite when the resolved ranked flag is true. Replace these dependencies with value-sensitive `if`/`then` conditions, or declare the optional ranked properties as `const: true` if explicit false is intentionally unsupported.

In `packages/wasm-sdk/src/queries/system.rs`:
- [SUGGESTION] packages/wasm-sdk/src/queries/system.rs:861-875: Expose indexed-tree sums through the WASM PathElement API
  `PathElementWasm::from_element` obtains its JavaScript `sum` property through this function. Both `ProvableSumIndexedTree` and `ProvableCountProvableSumIndexedTree` carry an `i64` sum, but they currently fall through to `None`, so JavaScript sees a recognized indexed element type with an unexpectedly undefined aggregate. Project these sums in the same way as the corresponding non-indexed variants.
- [SUGGESTION] packages/wasm-sdk/src/queries/system.rs:20-68: Add indexed-tree runtime values to the WASM TypeScript union
  The `elementType` getter now emits six values absent from `GroveElementType`: `provableSumIndexedTree`, `provableCountIndexedTree`, `provableCountProvableSumIndexedTree`, and their three `nonCounted` forms. JavaScript receives these strings, but generated TypeScript declarations claim they are impossible, making exhaustive switches and validators unsound. Add all six strings to the custom TypeScript section.

Comment thread packages/rs-drive/src/query/drive_document_ranked_query/mode_detection.rs Outdated
Comment thread packages/dapi-grpc/protos/platform/v0/platform.proto
Comment thread packages/rs-dpp/schema/meta_schemas/document/v3/document-meta.json
Comment thread packages/rs-dpp/schema/meta_schemas/document/v3/document-meta.json Outdated
Comment thread packages/rs-platform-version/src/version/dpp_versions/dpp_contract_versions/v6.rs Outdated
@codecov

codecov Bot commented Aug 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.84%. Comparing base (f43c883) to head (2404e74).
⚠️ Report is 1 commits behind head on v4.2-dev.

Additional details and impacted files
@@              Coverage Diff              @@
##           v4.2-dev    #4266       +/-   ##
=============================================
- Coverage     87.52%   66.84%   -20.69%     
=============================================
  Files          2678       27     -2651     
  Lines        341047     2835   -338212     
=============================================
- Hits         298516     1895   -296621     
+ Misses        42531      940    -41591     
Components Coverage Δ
dpp ∅ <ø> (∅)
drive ∅ <ø> (∅)
drive-abci ∅ <ø> (∅)
sdk ∅ <ø> (∅)
dapi-client ∅ <ø> (∅)
platform-version ∅ <ø> (∅)
platform-value ∅ <ø> (∅)
platform-wallet ∅ <ø> (∅)
drive-proof-verifier ∅ <ø> (∅)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

QuantumExplorer and others added 2 commits August 3, 2026 01:45
- Reject MAX / MIN rankings instead of mapping them to TOP(1) /
  BOTTOM(1): '= MAX' selects every group tied at the extreme, which the
  axis secondary cannot prove (ties break by group key), so a k = 1
  read would silently omit tied groups. Positional TOP(1) / BOTTOM(1)
  remain the single-best-ranked forms.
- Meta-schema v3: the three ranked prerequisite rules are now
  value-sensitive if/then conditionals, so an explicit
  'rankedCountable: false' no longer demands its range axis, matching
  the structural parser's opt-out semantics.
- wasm-sdk: project the i64 sum of ProvableSumIndexedTree /
  ProvableCountProvableSumIndexedTree path elements and add the six
  indexed element-type strings to the TypeScript union.
- Average fixed-point test helper now mirrors grovedb's euclidean
  floor; new signed-sum doctype in the restaurants fixture exercises
  negative averages where truncating division would disagree.
- Stale v3 meta-schema header and version-table comments corrected.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…657-integration-b6f426

# Conflicts:
#	packages/rs-platform-version/src/version/v14.rs
Pulls in the terminal non-Merk proof-binding fix (dashpay/grovedb#782):
CommitmentTree / MmrTree / BulkAppendTree / DenseTree elements reported
as a query's final result are now bound to the parent value_hash, so a
malicious node can no longer serve forged aggregates (e.g. a wrong
shielded note count) under a genuine root hash.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
QuantumExplorer and others added 3 commits August 3, 2026 04:02
…657-integration-b6f426

# Conflicts:
#	packages/rs-drive/src/util/grove_operations/batch_insert_empty_tree_if_not_exists/mod.rs
#	packages/rs-drive/src/util/grove_operations/batch_insert_empty_tree_if_not_exists/v0/mod.rs
#	packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v4.rs
#	packages/rs-platform-version/src/version/drive_versions/v9.rs
#	packages/rs-platform-version/src/version/v14.rs
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Regenerated with the repo's pinned docker toolchain
(yarn workspace @dashevo/dapi-grpc build): RankedEntry / RankedEntries
and the ResultData.ranked oneof arm now exist in the Node, Web, Python
and Objective-C bindings; drive_pbjs.js picks the messages up through
its platform.proto import. Diff is additive — the only removals are the
four-variant oneof lists becoming five-variant and proto doc comments
flowing into generated jsdoc.

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
packages/rs-drive/src/query/drive_document_ranked_query/mode_detection.rs (1)

136-178: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Remove MAX / MIN from these error messages.

Both messages still advertise MAX and MIN as accepted ranking operands. Lines 191-204 now reject both kinds. A user who follows either message gets a second rejection.

  • Line 142: "(IN TOP(n), IN BOTTOM(n), EQ MAX or EQ MIN)".
  • Line 173: "(TOP(n) / BOTTOM(n) / MAX / MIN)".
📝 Proposed message fixes
-                "ranked queries require exactly one `having` clause carrying the ranking \
-                 (`IN TOP(n)`, `IN BOTTOM(n)`, `EQ MAX` or `EQ MIN`); got {}. Multiple \
+                "ranked queries require exactly one `having` clause carrying the ranking \
+                 (`IN TOP(n)` or `IN BOTTOM(n)`); got {}. Multiple \
-                 (`TOP(n)` / `BOTTOM(n)` / `MAX` / `MIN`). Thresholds need a range walk \
+                 (`TOP(n)` / `BOTTOM(n)`). Thresholds need a range walk \
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/rs-drive/src/query/drive_document_ranked_query/mode_detection.rs`
around lines 136 - 178, Update the two error messages in the ranked-query
validation around the single-clause check and ranking operand check to advertise
only the supported TOP(n) and BOTTOM(n) ranking forms; remove MAX and MIN
references while preserving the existing validation behavior.
packages/rs-drive/src/fees/op.rs (1)

655-694: 📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Resolve the contradiction about indexed parents between the two dispatchers.

wrap_in_non_aggregated_for_parent_tree_type now accepts ProvableCountIndexedTree, ProvableSumIndexedTree, and ProvableCountProvableSumIndexedTree as aggregating parents. The new zero-contribution dispatcher states the opposite at Line 865-882: indexed trees are property-name trees, never value trees, so they cannot host zero-contributing children, and it rejects them explicitly.

Both functions answer the same question for the same parent role. Only one answer can be right.

If indexed parents are structurally impossible, remove the three indexed variants from these arms and let them fall to the _ arm. If they are reachable, correct the rejection text and reasoning in for_known_path_key_empty_tree_contributing_zero_to_parent. Note also that this function is described as the frozen v0 dispatcher, and pre-v14 contracts never produce indexed trees, so the added arms are unreachable for v0/v1 walkers.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/rs-drive/src/fees/op.rs` around lines 655 - 694, Resolve the
inconsistent indexed-parent handling between
wrap_in_non_aggregated_for_parent_tree_type and
for_known_path_key_empty_tree_contributing_zero_to_parent. Treat
ProvableCountIndexedTree, ProvableSumIndexedTree, and
ProvableCountProvableSumIndexedTree consistently in both dispatchers; because
the frozen v0/v1 path cannot produce indexed trees and the zero-contribution
dispatcher rejects them, remove these variants from the aggregating arms so they
fall through to the existing fallback handling.
🧹 Nitpick comments (2)
packages/rs-drive/src/query/drive_document_ranked_query/mode_detection.rs (1)

191-253: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Fold the direction and k resolution into one match to drop the unreachable!.

The second match repeats the Max | Min arm only to panic. The arm is unreachable today, because the first match returns early. A future edit that moves or removes the early return turns a validation error into a panic on a request-handling path.

One match over ranking.kind that yields (descending, k) removes the panic and keeps exhaustiveness, which is the stated goal of the current arm.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/rs-drive/src/query/drive_document_ranked_query/mode_detection.rs`
around lines 191 - 253, Combine the two ranking.kind matches into one exhaustive
match that returns both descending and k. Preserve the existing Max/Min
validation error, Top/Bottom n validation, operator checks, and limit checks,
while removing the unreachable! arm and destructuring the resulting tuple for
downstream use.
packages/rs-drive/src/fees/op.rs (1)

2439-2617: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Extend the matrix test to pin wrap_in_non_aggregated_for_parent_tree_type as well.

The test pins every cell of for_known_path_key_empty_tree_contributing_zero_to_parent, including the indexed rejections. It does not cover the v0 dispatcher, whose indexed-parent arms changed in this PR. Add a small loop that asserts the intended result for wrap_in_non_aggregated_for_parent_tree_type with each indexed parent, once the contradiction noted at Line 655-694 is resolved.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/rs-drive/src/fees/op.rs` around lines 2439 - 2617, Extend
zero_contribution_dispatcher_full_matrix with a focused loop covering
wrap_in_non_aggregated_for_parent_tree_type for every indexed parent type,
asserting the intended result for each. Resolve the existing contradiction
around the indexed-parent behavior first, then use the established
dispatch/assertion patterns to pin the v0 dispatcher’s expected acceptance or
rejection.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/rs-drive-abci/Cargo.toml`:
- Line 85: Update the protocol v14 version mapping centered on DRIVE_VERSION_V9
so its grove_version uses GROVE_V4 rather than GROVE_V3, and update any related
protocol-v14 configuration or mapping consistently. Ensure PLATFORM_V14.drive
selects GroveDB v4 behavior at activation while leaving other protocol versions
unchanged.

In `@packages/rs-drive/src/query/having.rs`:
- Around line 146-151: Update the cross-group ranking documentation near
HavingRankingKind to state that only Equal works with Top(1)/Bottom(1), while In
works with Top(N)/Bottom(N); remove the broader scalar-comparison claim and
preserve the existing Min/Max rejection note.

---

Outside diff comments:
In `@packages/rs-drive/src/fees/op.rs`:
- Around line 655-694: Resolve the inconsistent indexed-parent handling between
wrap_in_non_aggregated_for_parent_tree_type and
for_known_path_key_empty_tree_contributing_zero_to_parent. Treat
ProvableCountIndexedTree, ProvableSumIndexedTree, and
ProvableCountProvableSumIndexedTree consistently in both dispatchers; because
the frozen v0/v1 path cannot produce indexed trees and the zero-contribution
dispatcher rejects them, remove these variants from the aggregating arms so they
fall through to the existing fallback handling.

In `@packages/rs-drive/src/query/drive_document_ranked_query/mode_detection.rs`:
- Around line 136-178: Update the two error messages in the ranked-query
validation around the single-clause check and ranking operand check to advertise
only the supported TOP(n) and BOTTOM(n) ranking forms; remove MAX and MIN
references while preserving the existing validation behavior.

---

Nitpick comments:
In `@packages/rs-drive/src/fees/op.rs`:
- Around line 2439-2617: Extend zero_contribution_dispatcher_full_matrix with a
focused loop covering wrap_in_non_aggregated_for_parent_tree_type for every
indexed parent type, asserting the intended result for each. Resolve the
existing contradiction around the indexed-parent behavior first, then use the
established dispatch/assertion patterns to pin the v0 dispatcher’s expected
acceptance or rejection.

In `@packages/rs-drive/src/query/drive_document_ranked_query/mode_detection.rs`:
- Around line 191-253: Combine the two ranking.kind matches into one exhaustive
match that returns both descending and k. Preserve the existing Max/Min
validation error, Top/Bottom n validation, operator checks, and limit checks,
while removing the unreachable! arm and destructuring the resulting tuple for
downstream use.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: b07c4bf9-773d-4b85-987f-6748f7652dd5

📥 Commits

Reviewing files that changed from the base of the PR and between d2af6ae and 63f91a3.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (50)
  • packages/dapi-grpc/clients/drive/v0/nodejs/drive_pbjs.js
  • packages/dapi-grpc/clients/platform/v0/nodejs/platform_pbjs.js
  • packages/dapi-grpc/clients/platform/v0/nodejs/platform_protoc.js
  • packages/dapi-grpc/clients/platform/v0/objective-c/Platform.pbobjc.h
  • packages/dapi-grpc/clients/platform/v0/objective-c/Platform.pbobjc.m
  • packages/dapi-grpc/clients/platform/v0/python/platform_pb2.py
  • packages/dapi-grpc/clients/platform/v0/web/platform_pb.d.ts
  • packages/dapi-grpc/clients/platform/v0/web/platform_pb.js
  • packages/dapi-grpc/protos/platform/v0/platform.proto
  • packages/rs-dpp/Cargo.toml
  • packages/rs-dpp/schema/meta_schemas/document/v3/document-meta.json
  • packages/rs-dpp/src/data_contract/document_type/class_methods/try_from_schema/v2/mod.rs
  • packages/rs-drive-abci/Cargo.toml
  • packages/rs-drive-abci/src/query/document_query/v1/compute_aggregate_mode_and_check_limit/mod.rs
  • packages/rs-drive-abci/src/query/document_query/v1/compute_aggregate_mode_and_check_limit/v1/mod.rs
  • packages/rs-drive-abci/src/query/document_query/v1/conversions.rs
  • packages/rs-drive-abci/src/query/document_query/v1/mod.rs
  • packages/rs-drive-abci/src/query/document_query/v1/tests.rs
  • packages/rs-drive-proof-verifier/src/lib.rs
  • packages/rs-drive-proof-verifier/src/proof.rs
  • packages/rs-drive-proof-verifier/src/proof/document_ranked.rs
  • packages/rs-drive/Cargo.toml
  • packages/rs-drive/src/drive/contract/insert/insert_contract/v0/tests/ranked_index_e2e_tests.rs
  • packages/rs-drive/src/drive/document/delete/remove_indices_for_index_level_for_contract_operations/v2/mod.rs
  • packages/rs-drive/src/drive/document/delete/remove_indices_for_top_index_level_for_contract_operations/v2/mod.rs
  • packages/rs-drive/src/drive/document/index_level_tree_types.rs
  • packages/rs-drive/src/drive/document/insert/add_indices_for_index_level_for_contract_operations/v2/mod.rs
  • packages/rs-drive/src/drive/document/insert/add_indices_for_top_index_level_for_contract_operations/v2/mod.rs
  • packages/rs-drive/src/drive/document/mod.rs
  • packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/v1/mod.rs
  • packages/rs-drive/src/fees/op.rs
  • packages/rs-drive/src/query/drive_document_ranked_query/drive_dispatcher.rs
  • packages/rs-drive/src/query/drive_document_ranked_query/mod.rs
  • packages/rs-drive/src/query/drive_document_ranked_query/mode_detection.rs
  • packages/rs-drive/src/query/drive_document_ranked_query/tests.rs
  • packages/rs-drive/src/query/having.rs
  • packages/rs-drive/src/util/grove_operations/batch_insert_empty_tree_if_not_exists/mod.rs
  • packages/rs-drive/src/util/grove_operations/batch_insert_empty_tree_if_not_exists/v0/mod.rs
  • packages/rs-drive/tests/supporting_files/contract/restaurants/restaurants-contract.json
  • packages/rs-platform-version/Cargo.toml
  • packages/rs-platform-version/src/version/dpp_versions/dpp_contract_versions/v6.rs
  • packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v4.rs
  • packages/rs-platform-version/src/version/drive_versions/v9.rs
  • packages/rs-platform-version/src/version/v14.rs
  • packages/rs-platform-wallet/Cargo.toml
  • packages/rs-sdk/Cargo.toml
  • packages/rs-sdk/src/platform/documents/document_query.rs
  • packages/rs-sdk/src/platform/documents/document_ranked_entries.rs
  • packages/rs-sdk/src/platform/documents/ranked_proof_helpers.rs
  • packages/wasm-sdk/src/queries/system.rs
🚧 Files skipped from review as they are similar to previous changes (17)
  • packages/rs-drive/Cargo.toml
  • packages/rs-platform-version/src/version/dpp_versions/dpp_contract_versions/v6.rs
  • packages/rs-drive/src/drive/document/mod.rs
  • packages/rs-platform-version/src/version/drive_versions/v9.rs
  • packages/rs-platform-version/Cargo.toml
  • packages/rs-sdk/Cargo.toml
  • packages/rs-platform-wallet/Cargo.toml
  • packages/rs-drive-abci/src/query/document_query/v1/compute_aggregate_mode_and_check_limit/v1/mod.rs
  • packages/rs-drive-abci/src/query/document_query/v1/compute_aggregate_mode_and_check_limit/mod.rs
  • packages/rs-sdk/src/platform/documents/ranked_proof_helpers.rs
  • packages/rs-dpp/Cargo.toml
  • packages/rs-drive-proof-verifier/src/lib.rs
  • packages/rs-drive-proof-verifier/src/proof/document_ranked.rs
  • packages/dapi-grpc/protos/platform/v0/platform.proto
  • packages/rs-drive-abci/src/query/document_query/v1/mod.rs
  • packages/rs-drive-abci/src/query/document_query/v1/tests.rs
  • packages/rs-sdk/src/platform/documents/document_ranked_entries.rs

Comment thread packages/rs-drive-abci/Cargo.toml
Comment thread packages/rs-drive/src/query/having.rs Outdated
GROVE_V4 gates the indexed-tree batch cleanup behaviors (overwrite
inspection + delete-tree actual-type cleanup namespaces). Indexed trees
only exist from protocol v14, so staying on GROVE_V3 would let a batch
overwrite of a ranked index orphan its per-axis secondary storage.
The gate charges one extra stored-element read per overwrite-capable
op, so the latest-version fee-constant tests move up by that read while
the first-version tests keep the released values — pinning that the
cost change activates exactly at v14.

Also narrows the HavingRightOperand ranking-operator doc to what
evaluation actually accepts (Equal with Top(1)/Bottom(1), In with
Top(N)/Bottom(N)).

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/rs-platform-version/src/version/drive_versions/v9.rs (1)

145-151: 🗄️ Data Integrity & Integration | 🔵 Trivial | 🏗️ Heavy lift

Add a regression test for the Grove version gate.

DRIVE_VERSION_V9 now selects GROVE_V4, which changes cleanup behavior for ranked-index secondary storage. Add an integration test that overwrites a ranked index in a batch and verifies that obsolete secondary storage is deleted. Also verify that the protocol v13 version does not select GROVE_V4.

As per coding guidelines, the pull request must include tests. The PR objectives state that live protocol v14 tests remain pending.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/rs-platform-version/src/version/drive_versions/v9.rs` around lines
145 - 151, Add an integration regression test covering the DRIVE_VERSION_V9
Grove-version gate: perform a batch overwrite of a ranked index and verify
obsolete per-axis secondary storage is removed. Also assert that the protocol
v13 version selects a Grove version earlier than GROVE_V4, while
DRIVE_VERSION_V9 selects GROVE_V4; keep live protocol v14 coverage unchanged or
pending.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/rs-drive/src/drive/identity/update/mod.rs`:
- Line 62: Align the processing_fee expectation near the identity update test
with its explanation: either update the nearby comment to account for the full
4,040-credit increase, remove the insufficient explanation, or correct
processing_fee if the increase is unintended.

---

Nitpick comments:
In `@packages/rs-platform-version/src/version/drive_versions/v9.rs`:
- Around line 145-151: Add an integration regression test covering the
DRIVE_VERSION_V9 Grove-version gate: perform a batch overwrite of a ranked index
and verify obsolete per-axis secondary storage is removed. Also assert that the
protocol v13 version selects a Grove version earlier than GROVE_V4, while
DRIVE_VERSION_V9 selects GROVE_V4; keep live protocol v14 coverage unchanged or
pending.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 13e93bf3-4f42-4286-9369-de1333490a08

📥 Commits

Reviewing files that changed from the base of the PR and between 63f91a3 and 2404e74.

📒 Files selected for processing (5)
  • packages/rs-drive/src/drive/identity/balance/update.rs
  • packages/rs-drive/src/drive/identity/update/mod.rs
  • packages/rs-drive/src/drive/tokens/balance/update.rs
  • packages/rs-drive/src/query/having.rs
  • packages/rs-platform-version/src/version/drive_versions/v9.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/rs-drive/src/query/having.rs

Comment thread packages/rs-drive/src/drive/identity/update/mod.rs
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