Repository navigation
feat(dpp)!: count documents matching any element of a typed array - #5995
Conversation
A countOf or sumOf filter of a propertyConstraints or deleteConstraints
rule matched its documents by one value per key, so a rule could not
check a list element by element: "no tag of this post is banned" had no
spelling.
From protocol version 15 (parse_property_constraints 1) a key may be
matched by any element of a typed array of the document written,
{ "tag": { "in": "tags" } }. The total is over the counted documents
whose key is one of the list's distinct elements: one total read from
state per element, added up, and 0 for a list left out or empty.
Registration holds the elements to the key's kind, refuses the form in
a total of the rule's own type, admits one per filter, and counts the
total as the array's maxItems toward max_property_constraint_aggregates,
the state reads it can make.
read_aggregates of the shipped batch transformer now loops over the
value sets of a read. Under protocol version 14 no rule can hold the
new binding (its parser refuses the object), so every read has the one
set, or none, it had before, and reads and bills the same.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 16 minutes. View limit details
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. |
|
📖 Book Preview built successfully. Download the preview from the workflow artifacts. Updated at 2026-11-19T15:25:30.042Z |
|
Waiting for bot review — thepastaclaw not yet. Wait for the missing reviews, or a writer can post |
|
✅ Final review complete — no blockers (commit 1589642) |
thepastaclaw
left a comment
There was a problem hiding this comment.
Final validation — Phase 1 + Phase 2
The typed-array aggregate feature is integrated consistently across parsing, registration, storage, and consensus evaluation. However, the new per-element read loop runs before document schema validation, so malformed arrays can exceed their declared maxItems and amplify state-query work; execution coverage also misses sumOf and the delete/restore paths. Rust CI is successful, while the Swift SDK build remains in progress.
🔴 1 blocking | 🟡 2 suggestion(s)
Review provenance
Source: reviewer 1: muse-spark-1.3-contributor (agent: phase1-reviewer, role: general); reviewer 2: gpt-6.2-astra (agent: phase2-reviewer, role: general); reviewer 3: gpt-6.2-astra (agent: phase2-reviewer, role: architecture-layering); reviewer 4: muse-spark-1.3-contributor (agent: phase1-reviewer, role: rust-quality); 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:
criticalbygpt-6.2-sol(effort low) — This is a large, intricate change to consensus-critical document validation and aggregate computation in rs-drive-abci and rs-dpp, specifically the property-constraint parsing, registration, serialization, and transformer read paths. - Phase 1 reviewers:
muse-spark-1.3-contributor— general (completed, effort xhigh); agentphase1-reviewer,muse-spark-1.3-contributor— rust-quality (completed, effort xhigh); agentphase1-reviewer - Phase 1 model:
muse-spark-1.3-contributor— not quota-gated; passed overgemini-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; agentsol-verifier - Phase 2 reviewers:
gpt-6.2-astra— general (completed, effort high); agentphase2-reviewer,gpt-6.2-astra— architecture-layering (completed, effort high); agentphase2-reviewer,gpt-6.2-astra— platform-versioning (completed, effort high); agentphase2-reviewer,gpt-6.2-astra— rust-quality (completed, effort high); agentphase2-reviewer,gpt-6.2-astra— security-auditor (completed, effort high); agentphase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review (PastaClaw). How to work through them:
- Verify each finding against the code; fix only what is real, as narrowly as the finding.
- If a finding is wrong, decline it on its thread in one or two sentences; the bot answers once and stops.
- To defer one, link a tracking issue on its thread (or reply asking the bot to file one).
- Suggestions don't block the merge, but most are worth fixing in this PR; judge each on its merits.
- Keep the PR's scope; don't grow it to satisfy hardening suggestions.
- Carried items were raised in an earlier round: read the thread first, and don't re-fix what you already fixed, declined or deferred there. Disputed and deferred items are already handled and are not listed.
In `packages/rs-dpp/src/data_contract/document_type/property_constraints/aggregate.rs`:
- [BLOCKING] packages/rs-dpp/src/data_contract/document_type/property_constraints/aggregate.rs:118-143: Validate array bounds before performing per-element state reads
`filter_value_sets` iterates every submitted array element and the caller performs one aggregate lookup for each distinct element before `validate_document_properties` enforces the document type's `maxItems`. A transition can therefore submit an array much larger than the registered bound, causing substantially more serialization, GroveDB queries, and cost-accounting work before the transition is rejected. This defeats the `maxItems`-based `max_property_constraint_aggregates` resource bound for malformed documents and can be triggered repeatedly during check-TX and block validation. Validate the array length or reject/cap overlong arrays before entering this loop, using the declaring document type's registered typed-array bound.
In `packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/property_constraints.rs`:
- [SUGGESTION] packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/property_constraints.rs:3210-3214: Add execution coverage for sumOf over array elements
The new execution tests exercise only `countOf` with an `Elements` binding. The `sumOf` path uses different answering-index selection and contribution arithmetic, so the current tests would pass if per-element summed totals were selected or accumulated incorrectly. Add an execution test that creates counted documents with distinct summed values and verifies a `sumOf` array-element rule accepts or rejects based on the total.
- [SUGGESTION] packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/property_constraints.rs:3210-3214: Add execution coverage for delete and restore array-element aggregates
The new execution coverage is limited to create and replace. The delete path binds filter values from the stored document, while the moderator restore path binds them from the restored document with no removed contribution; both differ from create/replace despite sharing `read_aggregates`. Add tests that delete a document whose array contains a counted element and that refuse a moderator restore while one of its elements is banned.
| let Ok(Some(Value::Array(elements))) = data.get_optional_value_at_path(path) else { | ||
| return Ok(Vec::new()); | ||
| }; | ||
| let mut seen = BTreeSet::new(); | ||
| let mut sets = Vec::new(); | ||
| for element in elements { | ||
| // An element the key cannot hold matches no document | ||
| let Ok(serialized) = counted.serialize_value_for_key(key, element, platform_version) | ||
| else { | ||
| continue; | ||
| }; | ||
| if !seen.insert(serialized) { | ||
| continue; | ||
| } | ||
| if let Some(values) = self.bound_values( | ||
| counted, | ||
| document_id, | ||
| data, | ||
| owner_id, | ||
| Some(element), | ||
| platform_version, | ||
| )? { | ||
| sets.push(values); | ||
| } | ||
| } | ||
| Ok(sets) |
There was a problem hiding this comment.
✅ Resolved at
15896423; see the replies below.
🔴 Blocking: Validate array bounds before performing per-element state reads
filter_value_sets iterates every submitted array element and the caller performs one aggregate lookup for each distinct element before validate_document_properties enforces the document type's maxItems. A transition can therefore submit an array much larger than the registered bound, causing substantially more serialization, GroveDB queries, and cost-accounting work before the transition is rejected. This defeats the maxItems-based max_property_constraint_aggregates resource bound for malformed documents and can be triggered repeatedly during check-TX and block validation. Validate the array length or reject/cap overlong arrays before entering this loop, using the declaring document type's registered typed-array bound.
source: gpt-6.2-astra (phase2-reviewer: security-auditor)
There was a problem hiding this comment.
Confirmed and fixed in 1589642: filter_value_sets takes the type declaring the rule and gives no set for a list longer than the array's maxItems, so the totals read before schema validation stay within what registration counted. should_read_no_total_for_a_list_longer_than_the_array_holds covers it and fails with the bound taken out.
There was a problem hiding this comment.
Resolved (re-reviewed at 15896423): filter_value_sets now checks the declaring typed array's maxItems before iterating elements or performing aggregate lookups, and the regression coverage verifies that overlong arrays do not incur per-element aggregate-read fees.
| /// `countOf` matching a key by any element of an array of the document | ||
| /// (`{ "in": "tags" }`, protocol version 15): an offer holds no banned tag, | ||
| /// on a create and on a replace, and one without tags reads nothing. | ||
| #[tokio::test] | ||
| async fn should_refuse_a_document_holding_a_banned_element() { |
There was a problem hiding this comment.
✅ Resolved at
15896423; see the replies below.
🟡 Suggestion: Add execution coverage for sumOf over array elements
The new execution tests exercise only countOf with an Elements binding. The sumOf path uses different answering-index selection and contribution arithmetic, so the current tests would pass if per-element summed totals were selected or accumulated incorrectly. Add an execution test that creates counted documents with distinct summed values and verifies a sumOf array-element rule accepts or rejects based on the total.
source: muse-spark-1.3-contributor (phase1-reviewer: general, rust-quality); gpt-6.2-astra (phase2-reviewer: general, architecture-layering, platform-versioning, rust-quality, security-auditor)
There was a problem hiding this comment.
Added in 1589642: should_sum_the_documents_matching_each_distinct_element.
There was a problem hiding this comment.
Resolved (re-reviewed at 15896423): The added should_sum_the_documents_matching_each_distinct_element test uses distinct summed values and verifies that the array-element sum is accumulated and enforced correctly.
| /// `countOf` matching a key by any element of an array of the document | ||
| /// (`{ "in": "tags" }`, protocol version 15): an offer holds no banned tag, | ||
| /// on a create and on a replace, and one without tags reads nothing. | ||
| #[tokio::test] | ||
| async fn should_refuse_a_document_holding_a_banned_element() { |
There was a problem hiding this comment.
✅ Resolved at
15896423; see the replies below.
🟡 Suggestion: Add execution coverage for delete and restore array-element aggregates
The new execution coverage is limited to create and replace. The delete path binds filter values from the stored document, while the moderator restore path binds them from the restored document with no removed contribution; both differ from create/replace despite sharing read_aggregates. Add tests that delete a document whose array contains a counted element and that refuse a moderator restore while one of its elements is banned.
source: muse-spark-1.3-contributor (phase1-reviewer: general, rust-quality); gpt-6.2-astra (phase2-reviewer: general, architecture-layering, platform-versioning, rust-quality, security-auditor)
There was a problem hiding this comment.
Added in 1589642: should_judge_a_delete_rule_by_the_elements_of_the_stored_array and should_refuse_restore_while_an_element_of_the_restored_array_is_banned.
There was a problem hiding this comment.
Resolved (re-reviewed at 15896423): The new execution tests now cover both paths identified in the finding: delete constraints bind elements from the stored document, and moderator restore constraints evaluate the restored document while a matching element is banned.
|
Your move: thepastaclaw requested changes on this head; dismiss the review or push a fix; thepastaclaw left review threads unresolved; resolve them. |
The totals a rule reads are fetched when the action is built, before the document's schema is validated. A total matched by the elements of an array made one state read per distinct element of the submitted list, so a list past the array's maxItems read more than registration counted for the rule before the schema refused the document. filter_value_sets now takes the type declaring the rule and gives no set for a list longer than the array's maxItems. Adds execution tests for sumOf over elements, a delete rule and a moderator's restore. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
Your move: thepastaclaw left review threads unresolved; resolve them. |
thepastaclaw
left a comment
There was a problem hiding this comment.
Re-review — Final validation — Phase 1 + Phase 2
The current head correctly bounds array-element aggregate expansion by the declaring typed array's maxItems before per-element serialization or aggregate reads, while preserving protocol-14 behavior and protocol-15 gating. The added execution and round-trip coverage exercises sumOf, delete, restore, repeated elements, and overlong arrays; no failed code checks are reported, though several CI and review-policy checks remain pending.
🔴 0 blocking | 🟡 0 suggestion(s) | 💬 0 nitpick(s)
Review provenance
Source: reviewer 1: muse-spark-1.3-contributor (agent: phase1-reviewer, role: general); reviewer 2: gpt-6.2-astra (agent: phase2-reviewer, role: general); reviewer 3: gpt-6.2-astra (agent: phase2-reviewer, role: architecture-layering); reviewer 4: gpt-6.2-astra (agent: phase2-reviewer, role: platform-versioning); reviewer 5: gpt-6.2-astra (agent: phase2-reviewer, role: rust-quality); reviewer 6: muse-spark-1.3-contributor (agent: phase1-reviewer, role: rust-quality); reviewer 7: gpt-6.2-astra (agent: phase2-reviewer, role: security-auditor); reviewer 8: gpt-6.2-astra (agent: phase2-reviewer, role: general); reviewer 9: gpt-6.2-astra (agent: phase2-reviewer, role: architecture-layering); reviewer 10: gpt-6.2-astra (agent: phase2-reviewer, role: platform-versioning); reviewer 11: gpt-6.2-astra (agent: phase2-reviewer, role: rust-quality); reviewer 12: gpt-6.2-astra (agent: phase2-reviewer, role: security-auditor); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)
- Triage:
criticalbygpt-6.2-sol(effort low) — This is a large, intricate change to consensus-critical document constraint evaluation, aggregate reads, protocol-version dispatch, and persisted contract serialization in rs-dpp and rs-drive-abci. - Phase 1 reviewers:
muse-spark-1.3-contributor— general (completed, effort xhigh); agentphase1-reviewer,muse-spark-1.3-contributor— rust-quality (completed, effort xhigh); agentphase1-reviewer - Phase 1 model:
muse-spark-1.3-contributor— not quota-gated; passed overgemini-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 final gate: an independent Phase-2 review ran after iterative findings were reconciled
- Fresh verifier:
gpt-6.2-sol— final-verifier; agentsol-verifier - Phase 2 reviewers:
gpt-6.2-astra— general (completed, effort high); agentphase2-reviewer,gpt-6.2-astra— architecture-layering (completed, effort high); agentphase2-reviewer,gpt-6.2-astra— platform-versioning (completed, effort high); agentphase2-reviewer,gpt-6.2-astra— rust-quality (completed, effort high); agentphase2-reviewer,gpt-6.2-astra— security-auditor (completed, effort high); agentphase2-reviewer,gpt-6.2-astra— general (completed, effort high); agentphase2-reviewer,gpt-6.2-astra— architecture-layering (completed, effort high); agentphase2-reviewer,gpt-6.2-astra— platform-versioning (completed, effort high); agentphase2-reviewer,gpt-6.2-astra— rust-quality (completed, effort high); agentphase2-reviewer,gpt-6.2-astra— security-auditor (completed, effort high); agentphase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify the current code and confirm that no unresolved issues remain.
No unresolved findings remain from the prior review on this head.
|
Your move: thepastaclaw left review threads unresolved; resolve them. |
Basic explanation
A
propertyConstraintsrule can count the documents of another type that match a value of the document being written, but only by one property. A list (a typed array, such as a post's hashtags) could not be checked element by element, so a contract had no way to say "no tag of this post is banned".From protocol version 15 a
countOforsumOffilter may match a key by any element of a typed array of the document, written{ "in": "<array property>" }. The total is over the counted documents whose key is one of the list's distinct elements.Issue being fixed or feature implemented
Asked for by a contract that wants a moderator-kept list of banned hashtags: a
bannedTagdocument type, and posts refused when one of theirtagsis in it. References cannot say it (a reference requires a document to exist, never that none does), and a count rule read a single property.Before and after
Before, a filter value was one property,
$ownerId,$id, an integer or a constant. Naming the array was refused at registration, since a list is no single value:After (protocol version 15):
tagsbannedTagdocuments["dash", "news"]spam["dash", "spam"]spamDocumentPropertyConstraintViolatedError(10422)[]spam["spam", "spam"]spam["spam", "scam"]spam,scammaxItemsis 4)It is an integer operand like every other total, so any comparison works on it (
lessThan 2allows one banned tag), andsumOftakes the same form.What was done?
property_constraints/mod.rs):AggregateBinding::Elements { path, kind }, parsed from{ "in": path }underparse_property_constraints1 only, at most one per filter. The array is read as acontainsreads one (PropertyRead::Elements), so the existing check refuses a path that is no typed array.property_constraints/aggregate.rs):AggregateRead::filter_value_setsgives one set of key values per distinct element (told apart by their serialized key, as the index tells them apart), none for a list left out, and the one set (or none) offilter_valuesfor every other filter. A list longer than the array'smaxItemsgives none: the totals are read before the schema refuses the document, so a write never reads more totals than registration counted for the rule.try_from_schema/mod.rs): the elements are integers, strings or identifiers of the key's kind; the form is refused in a total of the rule's own type, whose contribution arithmetic matches every key by one value; and the total counts as the array'smaxItemstowardSystemLimits::max_property_constraint_aggregates(4), the state reads it can make. No new limit.native_schema/constraints.rs): admits the shape. Only protocol version 15 runs it; meta-schema v3 is untouched.serialized_version/v0/rules.rs):StoredAggregateBinding::Elements, appended.transformer/v0/property_constraint_aggregates.rs):read_aggregatesfetches one total per value set and adds them, with the existing overflow check. The map handed to the action staysAggregateRead -> i128, so the actions, the rule evaluation, the moderator-change, restore and delete paths and the SDKs are unchanged.contract-keywords/property-constraints.mdandtyped-arrays.md; the protocol version 15 notes.What a contract author should know (in the book): the rule is judged when its own type is written, so banning a tag later leaves stored posts; it is judged on every replace, so a type whose authors retract by replacing should let the retraction through with an
anyOf; and a moderator's restore of a removed post is refused while one of its tags is banned.In-place changes to shipped generations
batch/transformer/v0/property_constraint_aggregates.rs,read_aggregates(selected by protocol versions 14 and 15; inert before 14). The single read became a loop overfilter_value_sets, and the function takes the type declaring the rules, which only anElementsbinding reads (for the array'smaxItems). Under protocol version 14 no rule can hold anElementsbinding:parse_property_constraints0 refuses{ "in": ... }as no value a filter takes, so every read has exactly the one value set, or none, thatfilter_valuesgave, and the loop runs the same fetch, the same contribution arithmetic and the same billed operations once, or reads nothing and yields 0, as before. Commented at the edited lines, andshould_give_the_one_set_of_values_of_a_filter_without_an_arrayshows the two methods agree at protocol version 14.property_constraints/mod.rsandtry_from_schema/mod.rsdispatch onparse_property_constraintsas the byte array keys of generation 1 already do: generation 0 keeps its refusal, message included, and its limit count (every total weighs 1 when no filter holds anElementsbinding).parse_property_constraints1, the native schema check and stored contract form 0 with its rules are selected by protocol version 15 only, which is not released or activated, so they are edited in place.How Has This Been Tested?
cargo test -p dpp --all-features --lib -- try_from_schema property_constraints serialized_version: 1206 pass. New:array_element_aggregate_tests(8: the form at 15 and its refusal at 14, a delete rule, one value set per distinct element and none for an absent or empty list or one longer thanmaxItems,filter_value_setsagreeing withfilter_valuesat protocol versions 14 and 15 for a filter without an array, the refusals for no array, no property, another element kind, the rule's own type and twoinin one filter, and the limit weight), three parse refusals inproperty_constraints::tests, and a round trip of the stored rule.cargo test -p drive-abci --lib -- property_constraints delete_constraints restore_constraints: 69 pass. New (7): a create and a replace refused for a banned tag and accepted without one, a tag banned later refusing the next write and leaving stored documents, two banned tags totalling 2, a repeated tag counted once, one state read billed per distinct element, a list pastmaxItemsrefused for the same fee whether its elements are distinct or repeated (it fails with the bound taken out),sumOfover the distinct elements, a delete rule reading the stored document's elements, and a moderator's restore refused while a tag of the restored document is banned.cargo clippy -p dpp --all-features --all-targets -- -D warningsand the same fordrive-abci: clean.cargo fmt --all -- --check: clean.cargo check --workspace --all-targets(no crate outsidedppanddrive-abcinamesAggregateBinding) and the full suites.Breaking Changes
Consensus at protocol version 15 only, which is not released or activated: a contract using
{ "in": ... }registers where earlier builds of 15 refuse it, so nodes of a version 15 devnet must run the same build. Nothing changes at protocol version 14 or earlier.AggregateBinding(public indpp) gains theElementsvariant, andAggregateReadgainsfilter_value_sets(which takes the type declaring the rule and the type counted) andelements_path;filter_valueskeeps its signature and returnsNonefor a read holding anElementsbinding. No SDK, wasm or FFI surface changes.Checklist:
structure.rs, regeneratedgrovedb-structure.json, and checked the structure viewer link posted on this pull request🤖 Generated with Claude Code
PR Hygiene ·
1589642/self-reviewedWhen every merge requirement is met, the
PR Hygienecheck passes. Reviewer limits do not block merging; other required GitHub checks and protections still apply.