dash_platform_queries/documents/document_split_counts.rs
1//! `FromProof` + `Fetch` for [`DocumentSplitCounts`] — the
2//! per-group-entry view of the unified `getDocuments` endpoint.
3//!
4//! Backed by the same [`DocumentQuery`] as
5//! [`drive_proof_verifier::DocumentCount`]; the only difference
6//! is response shape — `DocumentSplitCounts` returns the full
7//! `entries` list keyed by the splitting property's serialized
8//! value, while `DocumentCount` returns the sum.
9//!
10//! Per-shape proof dispatch lives in
11//! [`super::count_proof_helpers::verify_count_query`] — this
12//! impl passes the verified entries through unchanged.
13//!
14//! Shapes emitted by `verify_count_query`:
15//! - `group_by = []` (aggregate): one entry with empty `key`
16//! carrying the verified total. `AggregateCountOnRange`,
17//! primary-key CountTree, and point-lookup-on-Equal-only paths
18//! all collapse to this shape.
19//! - `group_by = [in_field]` (per-In entries): one entry per
20//! **present** queried In value, with `count: Some(n)`. Absent
21//! In branches are omitted from `entries` — the current
22//! point-lookup path query doesn't request absence proofs,
23//! so grovedb's `verify_query` surfaces only present
24//! `(path, key, Some(Element))` triples. Callers that need to
25//! distinguish "verified zero" from "queried but absent" diff
26//! their request's In array against the returned entries by
27//! `key` (each entry's `key` is `serialize_value_for_key(in_field, v)`).
28//! - `group_by = [range_field]` / `[in_field, range_field]`
29//! (distinct walk): one entry per distinct value in the range
30//! (compound queries: per `(in_key, key)` pair). A value with no
31//! documents is absent, except on an index that can hold an empty
32//! group (Drive's `index_keeps_empty_groups`: one a preallocated or
33//! an `outlivesDelete` index shares its levels with, a
34//! `summableOffCountIndex` counter at zero included): there it comes
35//! back with `count: Some(0)`, counted in the page's limit. A short
36//! page is not proof the range ended: across an `IN`, grovedb also
37//! charges the limit for an `IN` value whose range holds nothing, and
38//! a `nullSearchable: false` index's empty null-key group is counted
39//! and left out when the range includes the empty key.
40
41use crate::documents::count_proof_helpers::{assert_select_is_count, verify_count_query};
42use crate::documents::document_query::DocumentQuery;
43use dapi_grpc::platform::v0::{GetDocumentsResponse, Proof, ResponseMetadata};
44use dash_context_provider::ContextProvider;
45use dpp::dashcore::Network;
46use dpp::version::PlatformVersion;
47use drive_proof_verifier::{DocumentSplitCounts, FromProof};
48
49impl FromProof<DocumentQuery> for DocumentSplitCounts {
50 type Request = DocumentQuery;
51 type Response = GetDocumentsResponse;
52
53 fn maybe_from_proof_with_metadata<'a, I: Into<Self::Request>, O: Into<Self::Response>>(
54 request: I,
55 response: O,
56 _network: Network,
57 platform_version: &PlatformVersion,
58 provider: &'a dyn ContextProvider,
59 ) -> Result<(Option<Self>, ResponseMetadata, Proof), drive_proof_verifier::Error>
60 where
61 Self: 'a,
62 {
63 let request: Self::Request = request.into();
64 assert_select_is_count(&request)?;
65 let response: Self::Response = response.into();
66 let (entries, mtd, proof) =
67 verify_count_query(request, response, platform_version, provider)?;
68 Ok((entries.map(DocumentSplitCounts::from_verified), mtd, proof))
69 }
70}