Skip to main content

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). Zero-count
31//!   ranges are simply absent — the range itself is unbounded so
32//!   there's no enumerable key set to ever-emit.
33
34use crate::documents::count_proof_helpers::{assert_select_is_count, verify_count_query};
35use crate::documents::document_query::DocumentQuery;
36use dapi_grpc::platform::v0::{GetDocumentsResponse, Proof, ResponseMetadata};
37use dash_context_provider::ContextProvider;
38use dpp::dashcore::Network;
39use dpp::version::PlatformVersion;
40use drive_proof_verifier::{DocumentSplitCounts, FromProof};
41
42impl FromProof<DocumentQuery> for DocumentSplitCounts {
43    type Request = DocumentQuery;
44    type Response = GetDocumentsResponse;
45
46    fn maybe_from_proof_with_metadata<'a, I: Into<Self::Request>, O: Into<Self::Response>>(
47        request: I,
48        response: O,
49        _network: Network,
50        platform_version: &PlatformVersion,
51        provider: &'a dyn ContextProvider,
52    ) -> Result<(Option<Self>, ResponseMetadata, Proof), drive_proof_verifier::Error>
53    where
54        Self: 'a,
55    {
56        let request: Self::Request = request.into();
57        assert_select_is_count(&request)?;
58        let response: Self::Response = response.into();
59        let (entries, mtd, proof) =
60            verify_count_query(request, response, platform_version, provider)?;
61        Ok((entries.map(DocumentSplitCounts::from_verified), mtd, proof))
62    }
63}