diff --git a/packages/dash-platform-queries/src/documents/array_element_clauses.rs b/packages/dash-platform-queries/src/documents/array_element_clauses.rs index 6262ce15253..7a556097145 100644 --- a/packages/dash-platform-queries/src/documents/array_element_clauses.rs +++ b/packages/dash-platform-queries/src/documents/array_element_clauses.rs @@ -39,11 +39,10 @@ pub fn key_element_clauses( ) } -/// [`aggregate_where_clauses`] of `request` before it is sent, its pending -/// window selections left unresolved: a relative time selection resolves only -/// against the answer's signed block time. Resolving a selection adds an -/// equality pin alone, which can add a refusal but never lift one, so whatever -/// this refuses the verifier refuses too. +/// [`aggregate_where_clauses`] of `request` before it is sent. Callers pass the +/// request with its pending window selections resolved, as the verifier resolves +/// them (`DocumentQuery::resolved_for_checking`), so whatever this refuses the +/// verifier refuses too. pub(super) fn aggregate_request_where_clauses( request: &DocumentQuery, folds_entries: bool, diff --git a/packages/dash-platform-queries/src/documents/average_proof_helpers.rs b/packages/dash-platform-queries/src/documents/average_proof_helpers.rs index 79e592b38c2..675500fda37 100644 --- a/packages/dash-platform-queries/src/documents/average_proof_helpers.rs +++ b/packages/dash-platform-queries/src/documents/average_proof_helpers.rs @@ -81,8 +81,11 @@ pub(super) fn check_average_request( ) -> Result<(), drive_proof_verifier::Error> { request.ensure_no_sub_queries()?; assert_select_is_avg(request)?; + let (resolved, _) = request.resolved_for_checking()?; + let request = &resolved; let where_clauses = aggregate_request_where_clauses(request, folds_entries, platform_version)?; - resolve_sum_mode(&request.group_by, &where_clauses).map(|_| ()) + resolve_sum_mode(&request.group_by, &where_clauses)?; + request.ensure_no_having() } /// Verify an AVG-shape proof and return per-branch `AverageEntry`s. diff --git a/packages/dash-platform-queries/src/documents/check_request_tests.rs b/packages/dash-platform-queries/src/documents/check_request_tests.rs index a80c9e44ef3..f4cda7aca6e 100644 --- a/packages/dash-platform-queries/src/documents/check_request_tests.rs +++ b/packages/dash-platform-queries/src/documents/check_request_tests.rs @@ -42,9 +42,11 @@ fn check>( T::check_request(query, PlatformVersion::latest()) } -/// The grammars these views check are shape-only, so the fixture's document -/// type serves every view; whether an index covers the query is the server's -/// call, and the server refuses a query no index covers. +/// The count, sum and average views check shapes only, so the fixture's +/// document type serves them; whether an index covers the query is the server's +/// call, and the server refuses a query no index covers. The ranked and +/// having-range views also check that an index serves the query, so their +/// queries use [`ranked_contract`]. fn contract() -> Arc { Arc::new( get_data_contract_fixture(None, 0, PlatformVersion::latest().protocol_version) @@ -56,9 +58,47 @@ fn query() -> DocumentQuery { DocumentQuery::new(contract(), "niceDocument").expect("the fixture has this document type") } +/// A `review` whose indexes rank restaurants by their average grade and hashtags by how many +/// reviews carry them, so ranked and having-range queries over it are served. +fn ranked_contract() -> Arc { + let schemas = platform_value!({"review": { + "type": "object", + "properties": { + "restaurantId": {"type": "string", "minLength": 1, "maxLength": 59, "position": 0}, + "grade": {"type": "integer", "minimum": 0, "maximum": 100, "position": 1}, + "hashtag": {"type": "string", "minLength": 1, "maxLength": 59, "position": 2} + }, + "required": ["restaurantId", "grade", "hashtag"], + "indices": [ + {"name": "byRestaurant", + "properties": [{"restaurantId": "asc"}], + "averageable": "grade", + "rangeAverageable": true, + "rankedAverageable": true}, + {"name": "byHashtag", + "properties": [{"hashtag": "asc"}], + "countable": "countable", + "rangeCountable": true, + "rankedCountable": true} + ], + "additionalProperties": false + }}); + Arc::new( + DataContractFactory::new(PlatformVersion::latest().protocol_version) + .expect("factory") + .create_with_value_config(Identifier::new([9u8; 32]), 0, schemas, None, None) + .expect("the review contract is well-formed") + .data_contract_owned(), + ) +} + +fn reviews() -> DocumentQuery { + DocumentQuery::new(ranked_contract(), "review").expect("the contract has this document type") +} + /// `SELECT AVG(grade) GROUP BY restaurantId ORDER BY grade DESC LIMIT 5`. fn ranked() -> DocumentQuery { - query() + reviews() .with_select(SelectProjection::avg("grade")) .with_group_by("restaurantId") .order_by_selected_aggregate(RankingDirection::Descending) @@ -67,7 +107,7 @@ fn ranked() -> DocumentQuery { /// `SELECT COUNT(*) GROUP BY hashtag HAVING $count > 100 LIMIT 100`. fn having() -> DocumentQuery { - query() + reviews() .with_select(SelectProjection::count_star()) .with_group_by("hashtag") .with_having(vec![HavingClause { @@ -489,33 +529,106 @@ fn should_refuse_a_composite_sub_query_outside_the_composite_shape() { assert!(error.contains("sub-query 0: limit"), "{error}"); } -/// Window selections stay pending until the answer arrives: a relative -/// time-range selection resolves only against the answer's signed block time. -/// Resolving any selection also needs the contract's bucketing index, which -/// the fixture lacks, so a check that resolved them would refuse these. +/// A window that names no window of a grid the contract declares (a start off the grid or +/// not an integer, or a field no index buckets) is refused by the verifier for every answer, +/// so it is refused before sending, in the verifier's words. An integer-range selection, and a +/// time-range selection by start, resolve from the query alone. A newest or oldest window +/// resolves at the answer's block time, but at any block time past the grid's phase, which +/// contract validation keeps under a year, it fails only for the grid. #[test] -fn should_accept_pending_window_selections_before_they_are_resolved() { - let cases: [(&str, Check, DocumentQuery); 2] = [ +fn should_refuse_a_window_that_does_not_resolve_before_sending() { + let integer = "failed to resolve integer range clause"; + let time = "failed to resolve time range clause"; + let off_grid_day = TimeRangeSelector::ByStart { + start_ms: 1_700_006_400_001, + }; + let cases: Vec<(&str, Check, DocumentQuery, &str)> = vec![ + ( + "DocumentCount", + check::, + listings() + .with_select(SelectProjection::count_star()) + .with_integer_range("price", Value::U64(150)), + integer, + ), + ( + "DocumentSum", + check::, + listings() + .with_select(SelectProjection::sum("price")) + .with_integer_range("price", Value::Text("200".to_string())), + integer, + ), + ( + "DocumentAverage", + check::, + listings() + .with_select(SelectProjection::avg("price")) + .with_time_range("$createdAt", off_grid_day), + time, + ), + ( + "DocumentCount", + check::, + query() + .with_select(SelectProjection::count_star()) + .with_time_range( + "$createdAt", + TimeRangeSelector::ByStart { + start_ms: 1_700_006_400_000, + }, + ), + time, + ), ( "DocumentRankedEntries", check::, - ranked(), + ranked().with_integer_range("price", Value::U64(600)), + integer, + ), + ( + "DocumentRankedEntries", + check::, + trending_ranked(off_grid_day), + time, ), ( "DocumentHavingEntries", check::, - having(), + having().with_integer_range("grade", Value::U64(50)), + integer, + ), + ( + "DocumentHavingEntries", + check::, + trending_having(off_grid_day), + time, + ), + ( + "DocumentRankedEntries", + check::, + ranked().with_time_range("$createdAt", TimeRangeSelector::Newest), + time, + ), + ( + "DocumentHavingEntries", + check::, + having().with_time_range("$createdAt", TimeRangeSelector::Oldest), + time, + ), + ( + "DocumentCount", + check::, + listings() + .with_select(SelectProjection::count_star()) + .with_time_range("$updatedAt", TimeRangeSelector::Newest) + .with_integer_range("price", Value::U64(150)), + time, ), ]; - for (view, check, query) in cases { - for query in [ - query - .clone() - .with_time_range("$createdAt", TimeRangeSelector::Newest), - query.with_integer_range("price", Value::U64(600)), - ] { - assert!(check(&query).is_ok(), "{view}: {:?}", check(&query)); - } + for (view, check, query, words) in cases { + let error = refusal(view, check, &query); + assert!(error.contains(words), "{view} {query:?}: {error}"); } } @@ -680,3 +793,719 @@ fn should_leave_a_documents_projection_field_to_the_encoder_and_the_server() { assert!(check(&query).is_ok(), "{view}: {:?}", check(&query)); } } + +/// A `listing` whose `price` is bucketed in windows of 100 and whose `$createdAt` is bucketed +/// in daily windows, each under a countable index, so window selections on it resolve. +fn windowed_contract() -> Arc { + let schemas = platform_value!({"listing": { + "type": "object", + "properties": { + "price": {"type": "integer", "minimum": 0, "maximum": 1_000_000, "position": 0}, + "category": {"type": "string", "minLength": 1, "maxLength": 63, "position": 1} + }, + "required": ["price", "category", "$createdAt"], + "indices": [ + {"name": "byPriceBand", + "properties": [{"price": "asc"}, {"category": "asc"}], + "integerRange": {"on": "price", "range": 100u64, "step": 100u64}, + "countable": "countable"}, + {"name": "byDay", + "properties": [{"$createdAt": "asc"}, {"category": "asc"}], + "timeRange": {"on": "$createdAt", "range": 86400, "step": 86400}, + "countable": "countable"} + ], + "additionalProperties": false + }}); + Arc::new( + DataContractFactory::new(PlatformVersion::latest().protocol_version) + .expect("factory") + .create_with_value_config(Identifier::new([11u8; 32]), 0, schemas, None, None) + .expect("the listing contract is well-formed") + .data_contract_owned(), + ) +} + +fn listings() -> DocumentQuery { + DocumentQuery::new(windowed_contract(), "listing").expect("the contract has this type") +} + +/// A documents select resolves its windows before sending as its verifier resolves them from +/// the answer: a window that does not resolve, or an `In` or range beside it on the field it +/// buckets, is refused for every answer, so it is refused before sending. The window alone is +/// sent. +#[test] +fn should_refuse_a_documents_select_whose_window_every_verifier_refuses_before_sending() { + let price_above_one = WhereClause { + field: "price".to_string(), + operator: WhereOperator::GreaterThan, + value: Value::U64(1), + }; + let refused = [ + ( + listings().with_integer_range("price", Value::U64(150)), + "failed to resolve integer range clause", + ), + ( + listings().with_time_range( + "$createdAt", + TimeRangeSelector::ByStart { + start_ms: 1_700_006_400_001, + }, + ), + "failed to resolve time range clause", + ), + ( + query().with_time_range("$createdAt", TimeRangeSelector::Newest), + "failed to resolve time range clause", + ), + ( + listings() + .with_integer_range("price", Value::U64(200)) + .with_where(price_above_one), + "invalid window selection query shape", + ), + ]; + let views: [(&str, Check); 2] = [ + ("Document", check::), + ("Documents", check::), + ]; + for (view, check) in views { + for (query, words) in &refused { + let error = refusal(view, check, query); + assert!(error.contains(words), "{view} {query:?}: {error}"); + } + for window in [ + listings().with_integer_range("price", Value::U64(200)), + listings().with_time_range("$createdAt", TimeRangeSelector::Newest), + ] { + assert!(check(&window).is_ok(), "{view}: {:?}", check(&window)); + } + } +} + +/// An index buckets one field, so no index serves windows on two fields, even when each +/// window resolves: every node refuses such a query, and so does every verifier's index pick. +/// It is refused before sending. +#[test] +fn should_refuse_windows_on_two_fields_before_sending() { + let two_windows = listings() + .with_integer_range("price", Value::U64(200)) + .with_time_range("$createdAt", TimeRangeSelector::Newest); + let views: [(&str, Check, SelectProjection); 4] = [ + ( + "Documents", + check::, + SelectProjection::documents(), + ), + ( + "DocumentCount", + check::, + SelectProjection::count_star(), + ), + ( + "DocumentSum", + check::, + SelectProjection::sum("price"), + ), + ( + "DocumentAverage", + check::, + SelectProjection::avg("price"), + ), + ]; + for (view, check, select) in views { + let error = refusal(view, check, &two_windows.clone().with_select(select)); + assert!( + error.contains("at most one window selection"), + "{view}: {error}" + ); + } +} + +/// A query whose contract lacks its document type is refused by the verifier for every answer, +/// in the same words, when it carries a window to resolve. +#[test] +fn should_refuse_a_window_on_a_document_type_the_contract_lacks_before_sending() { + let mut query = listings().with_integer_range("price", Value::U64(200)); + query.document_type_name = "missing".to_string(); + let views: [(&str, Check, SelectProjection); 3] = [ + ("Document", check::, SelectProjection::documents()), + ( + "Documents", + check::, + SelectProjection::documents(), + ), + ( + "DocumentCount", + check::, + SelectProjection::count_star(), + ), + ]; + for (view, check, select) in views { + let error = refusal(view, check, &query.clone().with_select(select)); + assert!( + error.contains("document type not found for window selection query"), + "{view}: {error}" + ); + } +} + +/// A count, sum or average view verifies grouped totals, not a `having` clause. A grouped query +/// with one is answered as a having-range page, which only the having view verifies, and one +/// without a grouping is refused by every node. So these views, and their split views, refuse +/// any `having` clause before sending, even one whose group an `In` constrains. +#[test] +fn should_refuse_a_having_clause_in_an_aggregate_view_before_sending() { + let having = vec![HavingClause { + aggregate: HavingAggregate { + function: HavingAggregateFunction::Count, + field: String::new(), + }, + operator: HavingOperator::GreaterThan, + right: HavingRightOperand::Value(Value::U64(1)), + }]; + let views: [(&str, Check, SelectProjection); 6] = [ + ( + "DocumentCount", + check::, + SelectProjection::count_star(), + ), + ( + "DocumentSplitCounts", + check::, + SelectProjection::count_star(), + ), + ( + "DocumentSum", + check::, + SelectProjection::sum("grade"), + ), + ( + "DocumentSplitSums", + check::, + SelectProjection::sum("grade"), + ), + ( + "DocumentAverage", + check::, + SelectProjection::avg("grade"), + ), + ( + "DocumentSplitAverages", + check::, + SelectProjection::avg("grade"), + ), + ]; + for (view, check, select) in views { + let ungrouped = query().with_select(select.clone()); + let grouped_by_in = query() + .with_select(select) + .with_where(restaurants_in()) + .with_group_by("restaurantId"); + for aggregate in [ungrouped, grouped_by_in] { + assert!( + check(&aggregate).is_ok(), + "{view}: the aggregate without a having clause is sent: {:?}", + check(&aggregate) + ); + let error = refusal(view, check, &aggregate.with_having(having.clone())); + assert!(error.contains("DocumentHavingEntries"), "{view}: {error}"); + } + } +} + +/// A documents select is rebuilt by its verifier into Drive's query before the proof is read, +/// so a where clause Drive's grammar refuses, such as a second `In` on one field, is refused +/// for every answer. It is refused before sending, in the verifier's words. +#[test] +fn should_refuse_a_documents_select_drive_cannot_build_before_sending() { + let in_on = |field: &str, values: Value| WhereClause { + field: field.to_string(), + operator: WhereOperator::In, + value: values, + }; + let refused = [ + query() + .with_where(in_on("restaurantId", platform_value!(["a", "b"]))) + .with_where(in_on("restaurantId", platform_value!(["c", "d"]))), + query() + .with_where(in_on("restaurantId", platform_value!(["a", "b"]))) + .with_where(in_on("restaurantId", platform_value!(["c", "d"]))) + .with_where(in_on("grade", platform_value!([1u64, 2u64]))), + ]; + let views: [(&str, Check); 2] = [ + ("Document", check::), + ("Documents", check::), + ]; + for (view, check) in views { + for query in &refused { + let error = refusal(view, check, query); + assert!( + error.contains("Failed to convert DocumentQuery to DriveQuery"), + "{view}: {error}" + ); + } + } +} + +/// A field a window selection buckets may carry only the equality the selection resolves to. +/// An `In` or a range beside it is refused by every verifier, at whatever block time the +/// answer is signed, so it is refused before sending too. +#[test] +fn should_refuse_an_in_or_range_beside_a_window_on_the_same_field_before_sending() { + let beside = |query: DocumentQuery, field: &str, operator: WhereOperator, value: Value| { + query.with_where(WhereClause { + field: field.to_string(), + operator, + value, + }) + }; + let windows = [ + ( + listings().with_integer_range("price", Value::U64(200)), + "price", + ), + ( + listings().with_time_range("$createdAt", TimeRangeSelector::Newest), + "$createdAt", + ), + ]; + let views: [(&str, Check, SelectProjection); 3] = [ + ( + "DocumentCount", + check::, + SelectProjection::count_star(), + ), + ( + "DocumentSum", + check::, + SelectProjection::sum("price"), + ), + ( + "DocumentAverage", + check::, + SelectProjection::avg("price"), + ), + ]; + for (window, field) in windows { + for (view, check, select) in &views { + for (operator, value) in [ + (WhereOperator::GreaterThan, Value::U64(1)), + (WhereOperator::In, platform_value!([1u64, 2u64])), + ] { + let query = + beside(window.clone(), field, operator, value).with_select(select.clone()); + let error = refusal(view, *check, &query); + assert!( + error.contains("invalid window selection query shape"), + "{view} {field} {operator:?}: {error}" + ); + } + assert!( + check(&window.clone().with_select(select.clone())).is_ok(), + "{view} {field}: the window alone is sent" + ); + } + } +} + +/// A ranked or having-range query no index of the contract serves is refused by every node +/// and every verifier, so it is refused before sending. +#[test] +fn should_refuse_a_ranked_or_having_query_no_index_serves_before_sending() { + let uncovered_ranked = query() + .with_select(SelectProjection::avg("grade")) + .with_group_by("restaurantId") + .order_by_selected_aggregate(RankingDirection::Descending) + .with_limit(5); + let error = refusal( + "DocumentRankedEntries", + check::, + &uncovered_ranked, + ); + assert!(error.contains("cannot serve this ranked query"), "{error}"); + let mut uncovered_having = having(); + uncovered_having.group_by = vec!["restaurantId".to_string()]; + let error = refusal( + "DocumentHavingEntries", + check::, + &uncovered_having, + ); + assert!( + error.contains("cannot serve this having-range query"), + "{error}" + ); +} + +/// A `post` whose index ranks hashtags by how many posts carry them within each day's window +/// of `$createdAt`, so ranked and having-range queries pinned to a window are served. +fn windowed_ranked_contract() -> Arc { + trending_contract(platform_value!({"on": "$createdAt", "range": 86400, "step": 86400})) +} + +/// Seconds after a day's window starts that [`expiring_ranked_contract`]'s index drops it. +const TRENDING_TTL_SECONDS: u64 = 172_800; + +/// [`windowed_ranked_contract`] with an index that drops each day's window +/// [`TRENDING_TTL_SECONDS`] after it starts. +fn expiring_ranked_contract() -> Arc { + trending_contract(platform_value!({ + "on": "$createdAt", + "range": 86400, + "step": 86400, + "ttl": TRENDING_TTL_SECONDS + })) +} + +fn trending_contract(time_range: Value) -> Arc { + let schemas = platform_value!({"post": { + "type": "object", + "properties": { + "hashtag": {"type": "string", "minLength": 1, "maxLength": 59, "position": 0} + }, + "required": ["hashtag", "$createdAt"], + "indices": [ + {"name": "trending", + "properties": [{"$createdAt": "asc"}, {"hashtag": "asc"}], + "timeRange": time_range, + "countable": "countable", + "rangeCountable": true, + "rankedCountable": true} + ], + "additionalProperties": false + }}); + Arc::new( + DataContractFactory::new(PlatformVersion::latest().protocol_version) + .expect("factory") + .create_with_value_config(Identifier::new([12u8; 32]), 0, schemas, None, None) + .expect("the post contract is well-formed") + .data_contract_owned(), + ) +} + +fn trending(selector: TimeRangeSelector) -> DocumentQuery { + DocumentQuery::new(windowed_ranked_contract(), "post") + .expect("the contract has this document type") + .with_select(SelectProjection::count_star()) + .with_group_by("hashtag") + .with_time_range("$createdAt", selector) +} + +fn trending_ranked(selector: TimeRangeSelector) -> DocumentQuery { + trending(selector) + .order_by_selected_aggregate(RankingDirection::Descending) + .with_limit(5) +} + +fn trending_having(selector: TimeRangeSelector) -> DocumentQuery { + trending(selector) + .with_having(vec![HavingClause { + aggregate: HavingAggregate { + function: HavingAggregateFunction::Count, + field: String::new(), + }, + operator: HavingOperator::GreaterThan, + right: HavingRightOperand::Value(Value::U64(1)), + }]) + .with_limit(5) +} + +/// A ranked or having-range query pinned to a window is served by the index that buckets the +/// window, so it is sent: the check resolves the window and hands its provenance to the index +/// pick, as the verifier does. +#[test] +fn should_send_a_ranked_or_having_query_pinned_to_a_window_its_index_buckets() { + for selector in [ + TimeRangeSelector::Newest, + TimeRangeSelector::Oldest, + TimeRangeSelector::ByStart { + start_ms: 1_700_006_400_000, + }, + ] { + let ranked = trending_ranked(selector); + assert!( + check::(&ranked).is_ok(), + "{selector:?}: {:?}", + check::(&ranked) + ); + let having = trending_having(selector); + assert!( + check::(&having).is_ok(), + "{selector:?}: {:?}", + check::(&having) + ); + } +} + +/// A ranked query whose window resolves is still checked against the ranked grammar. +#[test] +fn should_refuse_a_malformed_ranked_query_pinned_to_a_window_before_sending() { + let error = refusal( + "DocumentRankedEntries", + check::, + &trending_ranked(TimeRangeSelector::Newest).with_limit(0), + ); + assert!(error.contains("not a well-formed ranked query"), "{error}"); +} + +/// A verifier answer to `query`, signed at `time_ms`, whose proof is empty: a verifier that +/// accepts the request fails on the proof, so a request error is a refusal of the request. +fn verifier_refuses< + T: FromProof, +>( + query: &DocumentQuery, + time_ms: u64, +) -> bool { + let response = GetDocumentsResponse { + version: Some(get_documents_response::Version::V0( + GetDocumentsResponseV0 { + result: Some(get_documents_response_v0::Result::Proof(Proof::default())), + metadata: Some(ResponseMetadata { + time_ms, + protocol_version: PlatformVersion::latest().protocol_version, + ..Default::default() + }), + }, + )), + }; + matches!( + T::maybe_from_proof_with_metadata( + query.clone(), + response, + Network::Testnet, + PlatformVersion::latest(), + &NoProvider, + ), + Err(Error::RequestError { .. }) + ) +} + +type Verifies = fn(&DocumentQuery, u64) -> bool; + +/// What a check refuses before sending, the verifier refuses for every answer, whatever block +/// time the answer is signed at. And a query the check accepts is one the verifier accepts at +/// some block time, so the comparison can tell the two apart. +#[test] +fn should_refuse_before_sending_only_what_the_verifier_refuses_at_any_block_time() { + let block_times = [ + 86_400_000, + 1_600_000_000_000, + 1_700_000_000_000, + 1_800_000_123_456, + 4_000_000_000_000, + ]; + let shapes: Vec<(&str, Check, Verifies, DocumentQuery)> = vec![ + ( + "DocumentCount", + check::, + verifier_refuses::, + listings() + .with_select(SelectProjection::count_star()) + .with_integer_range("price", Value::U64(200)), + ), + ( + "DocumentCount", + check::, + verifier_refuses::, + listings() + .with_select(SelectProjection::count_star()) + .with_integer_range("price", Value::U64(200)) + .with_where(WhereClause { + field: "price".to_string(), + operator: WhereOperator::GreaterThan, + value: Value::U64(1), + }), + ), + ( + "DocumentSum", + check::, + verifier_refuses::, + listings() + .with_select(SelectProjection::sum("price")) + .with_time_range("$createdAt", TimeRangeSelector::Newest) + .with_where(WhereClause { + field: "$createdAt".to_string(), + operator: WhereOperator::In, + value: platform_value!([1u64, 2u64]), + }), + ), + ( + "DocumentAverage", + check::, + verifier_refuses::, + listings() + .with_select(SelectProjection::avg("price")) + .with_time_range("$createdAt", TimeRangeSelector::Oldest), + ), + ( + "DocumentSplitCounts", + check::, + verifier_refuses::, + listings() + .with_select(SelectProjection::count_star()) + .with_integer_range("price", Value::U64(200)) + .with_group_by("category"), + ), + ( + "DocumentRankedEntries", + check::, + verifier_refuses::, + trending_ranked(TimeRangeSelector::Newest), + ), + ( + "DocumentRankedEntries", + check::, + verifier_refuses::, + trending_ranked(TimeRangeSelector::Newest).with_where(WhereClause { + field: "$createdAt".to_string(), + operator: WhereOperator::GreaterThan, + value: Value::U64(1), + }), + ), + ( + "DocumentHavingEntries", + check::, + verifier_refuses::, + trending_having(TimeRangeSelector::ByStart { + start_ms: 1_700_006_400_000, + }), + ), + ( + "DocumentRankedEntries", + check::, + verifier_refuses::, + query() + .with_select(SelectProjection::avg("grade")) + .with_group_by("restaurantId") + .order_by_selected_aggregate(RankingDirection::Descending) + .with_limit(5), + ), + ( + "DocumentCount", + check::, + verifier_refuses::, + listings() + .with_select(SelectProjection::count_star()) + .with_integer_range("price", Value::U64(150)), + ), + ( + "DocumentHavingEntries", + check::, + verifier_refuses::, + trending_having(TimeRangeSelector::ByStart { + start_ms: 1_700_006_400_001, + }), + ), + ( + "DocumentCount", + check::, + verifier_refuses::, + query() + .with_select(SelectProjection::count_star()) + .with_time_range("$createdAt", TimeRangeSelector::Newest), + ), + ( + "Documents", + check::, + verifier_refuses::, + listings().with_integer_range("price", Value::U64(150)), + ), + ( + "Documents", + check::, + verifier_refuses::, + listings().with_integer_range("price", Value::U64(200)), + ), + ( + "Documents", + check::, + verifier_refuses::, + query() + .with_where(WhereClause { + field: "restaurantId".to_string(), + operator: WhereOperator::In, + value: platform_value!(["a", "b"]), + }) + .with_where(WhereClause { + field: "restaurantId".to_string(), + operator: WhereOperator::In, + value: platform_value!(["c", "d"]), + }), + ), + ( + "DocumentCount", + check::, + verifier_refuses::, + listings() + .with_select(SelectProjection::count_star()) + .with_integer_range("price", Value::U64(200)) + .with_time_range("$createdAt", TimeRangeSelector::Newest), + ), + ]; + let (mut refused, mut accepted_by_both) = (0, 0); + for (view, check, verifier_refuses, query) in &shapes { + if check(query).is_ok() { + if block_times + .iter() + .any(|time_ms| !verifier_refuses(query, *time_ms)) + { + accepted_by_both += 1; + } + continue; + } + refused += 1; + for time_ms in block_times { + assert!( + verifier_refuses(query, time_ms), + "{view} refused {query:?} before sending, but the verifier accepts it at block \ + time {time_ms}" + ); + } + } + assert!( + refused >= 3 && accepted_by_both >= 2, + "{refused} refused, {accepted_by_both} accepted by both" + ); +} + +/// Whether a window by start has passed its index's TTL depends on the answer's block time, so +/// the check before sending leaves it to the verifier. A window the TTL dropped long ago is +/// sent; the verifier accepts it from an answer signed at its horizon and refuses it from one +/// signed a millisecond later. +#[test] +fn should_leave_a_window_past_its_ttl_to_the_answers_block_time() { + // A day's window in September 2020: past its TTL at any block time a check could stand in + // for the answer's. + let start_ms = 1_600_041_600_000; + let horizon_ms = start_ms + TRENDING_TTL_SECONDS * 1_000; + let selector = TimeRangeSelector::ByStart { start_ms }; + let expiring = |query: DocumentQuery| DocumentQuery { + data_contract: expiring_ranked_contract(), + ..query + }; + let shapes: [(&str, Check, Verifies, DocumentQuery); 2] = [ + ( + "DocumentRankedEntries", + check::, + verifier_refuses::, + expiring(trending_ranked(selector)), + ), + ( + "DocumentHavingEntries", + check::, + verifier_refuses::, + expiring(trending_having(selector)), + ), + ]; + for (view, check, verifier_refuses, query) in &shapes { + assert!(check(query).is_ok(), "{view}: {:?}", check(query)); + assert!( + !verifier_refuses(query, horizon_ms), + "{view}: a window is not expired at its horizon" + ); + assert!( + verifier_refuses(query, horizon_ms + 1), + "{view}: a window is expired a millisecond past its horizon" + ); + } +} diff --git a/packages/dash-platform-queries/src/documents/count_proof_helpers.rs b/packages/dash-platform-queries/src/documents/count_proof_helpers.rs index 1454a84f4d0..1fe58cc7f6a 100644 --- a/packages/dash-platform-queries/src/documents/count_proof_helpers.rs +++ b/packages/dash-platform-queries/src/documents/count_proof_helpers.rs @@ -76,6 +76,9 @@ pub(super) fn assert_select_is_count( /// serves one only when nothing constrains its group: its where clauses may /// only pin the index properties before the grouped one. Older nodes refuse /// those shapes, so refusing them here is right at every protocol version. +/// +/// Pending window selections are resolved first, as the verifier resolves them +/// ([`DocumentQuery::resolved_for_checking`]), so the checks read the clauses the verifier reads. pub(super) fn check_count_request( request: &DocumentQuery, folds_entries: bool, @@ -83,8 +86,11 @@ pub(super) fn check_count_request( ) -> Result<(), drive_proof_verifier::Error> { request.ensure_no_sub_queries()?; assert_select_is_count(request)?; + let (resolved, _) = request.resolved_for_checking()?; + let request = &resolved; let where_clauses = aggregate_request_where_clauses(request, folds_entries, platform_version)?; - resolve_count_mode(&request.group_by, &where_clauses).map(|_| ()) + resolve_count_mode(&request.group_by, &where_clauses)?; + request.ensure_no_having() } /// Translate the SDK's `u32`-with-`0`-sentinel limit into the diff --git a/packages/dash-platform-queries/src/documents/document_count.rs b/packages/dash-platform-queries/src/documents/document_count.rs index 16bed87c19b..05d6e427fc9 100644 --- a/packages/dash-platform-queries/src/documents/document_count.rs +++ b/packages/dash-platform-queries/src/documents/document_count.rs @@ -26,7 +26,25 @@ use dapi_grpc::platform::v0::{GetDocumentsResponse, Proof, ResponseMetadata}; use dash_context_provider::ContextProvider; use dpp::dashcore::Network; use dpp::version::PlatformVersion; -use drive_proof_verifier::{DocumentCount, FromProof}; +use drive_proof_verifier::{DocumentCount, FromProof, SplitCountEntry}; + +/// Fold per-branch counts into one `u64`. A total that does not fit is an +/// `UnusableVerifiedResult` rather than a panic (debug) or a wrapped count +/// (release): the entries are verified, so every honest node's answer +/// overflows alike. +fn fold_count_entries(entries: &[SplitCountEntry]) -> Result { + entries + .iter() + .filter_map(|e| e.count) + .try_fold(0u64, u64::checked_add) + .ok_or_else(|| drive_proof_verifier::Error::UnusableVerifiedResult { + reason: "DocumentCount: u64 overflow folding per-branch counts into a single \ + aggregate. The proof itself verified, but the requested total count \ + doesn't fit in u64. Use DocumentSplitCounts to receive per-branch \ + counts and fold them with your own arithmetic." + .to_string(), + }) +} impl FromProof for DocumentCount { type Request = DocumentQuery; @@ -58,8 +76,44 @@ impl FromProof for DocumentCount { let (entries, mtd, proof) = verify_count_query(request, response, true, platform_version, provider)?; let count = entries - .map(|es| es.iter().filter_map(|e| e.count).sum::()) + .map(|es| fold_count_entries(&es)) + .transpose()? .map(DocumentCount); Ok((count, mtd, proof)) } } + +#[cfg(test)] +mod tests { + use super::*; + + fn entry(in_key: u8, count: Option) -> SplitCountEntry { + SplitCountEntry { + in_key: Some(vec![in_key]), + key: vec![0u8], + count, + } + } + + /// The total adds up every verified branch; a branch the verifier proved absent adds 0. + #[test] + fn should_add_up_every_verified_branch_count() { + let entries = [entry(1, Some(100)), entry(2, None), entry(3, Some(50))]; + assert_eq!(fold_count_entries(&entries).expect("fits in u64"), 150); + } + + /// Branch counts whose total does not fit in `u64` are an unusable verified result, as for + /// the sum and average views: every honest node's answer overflows alike. The total must + /// neither panic nor wrap to a wrong count. + #[test] + fn should_report_an_overflowing_count_as_an_unusable_verified_result() { + let entries = [entry(1, Some(u64::MAX)), entry(2, Some(1))]; + match fold_count_entries(&entries) { + Err(drive_proof_verifier::Error::UnusableVerifiedResult { reason }) => assert!( + reason.contains("u64 overflow") && reason.contains("DocumentSplitCounts"), + "{reason}" + ), + other => panic!("expected an unusable verified result, got {other:?}"), + } + } +} diff --git a/packages/dash-platform-queries/src/documents/document_having_entries.rs b/packages/dash-platform-queries/src/documents/document_having_entries.rs index 9f4c12b2244..3bd2eaf8d31 100644 --- a/packages/dash-platform-queries/src/documents/document_having_entries.rs +++ b/packages/dash-platform-queries/src/documents/document_having_entries.rs @@ -104,7 +104,7 @@ //! ``` use crate::documents::document_query::DocumentQuery; -use crate::documents::having_proof_helpers::{assert_having_shape, verify_having_query}; +use crate::documents::having_proof_helpers::{check_having_request, verify_having_query}; use dapi_grpc::platform::v0::{GetDocumentsResponse, Proof, ResponseMetadata}; use dash_context_provider::ContextProvider; use dpp::dashcore::Network; @@ -116,15 +116,15 @@ impl FromProof for DocumentHavingEntries { type Response = GetDocumentsResponse; /// A request without a `having` clause is an ordinary grouped aggregate, which nodes - /// serve and the having verifier then refuses. Its pending time-range selections only - /// ever add equality pins, which the grammar cannot refuse a request for lacking, so - /// checking before they are resolved refuses nothing the verifier accepts. + /// serve and the having verifier then refuses. The check resolves the pending window + /// selections as the verifier does, then runs the grammar and picks the covering index + /// with the verifier's own code, so it refuses a window that does not resolve, a resolved + /// shape no verifier accepts, and a query no index serves. fn check_request( request: &Self::Request, platform_version: &PlatformVersion, ) -> Result<(), drive_proof_verifier::Error> { - request.ensure_no_sub_queries()?; - assert_having_shape(request, platform_version).map(|_| ()) + check_having_request(request, platform_version) } fn maybe_from_proof_with_metadata<'a, I: Into, O: Into>( diff --git a/packages/dash-platform-queries/src/documents/document_query.rs b/packages/dash-platform-queries/src/documents/document_query.rs index 616794b576a..53da152b92e 100644 --- a/packages/dash-platform-queries/src/documents/document_query.rs +++ b/packages/dash-platform-queries/src/documents/document_query.rs @@ -37,6 +37,7 @@ use dpp::{ InvalidVectorSizeError, ProtocolError, }; use drive::config::DEFAULT_QUERY_LIMIT; +use drive::error::Error as DriveError; use drive::query::drive_document_ranked_query::mode_detection::ranked_order_key; use drive::query::{ resolve_integer_range_bucket_clause, resolve_time_range_bucket_clause, @@ -266,6 +267,21 @@ impl DocumentQuery { Ok(()) } + /// A count, sum or average view verifies grouped totals, not a `having` clause. Nodes from + /// protocol version 14 answer a grouped query with one as a having-range page, which only + /// the having view verifies, and every node refuses one without a grouping. + pub(super) fn ensure_no_having(&self) -> Result<(), drive_proof_verifier::Error> { + if !self.having.is_empty() { + return Err(drive_proof_verifier::Error::RequestError { + error: "a having clause is answered as a having-range page, which a count, sum \ + or average view cannot verify; fetch a grouped query with one as \ + DocumentHavingEntries" + .to_string(), + }); + } + Ok(()) + } + /// Create new document query based on a [DriveDocumentQuery]. /// /// Preserves sub-queries, including their contracts and bindings. @@ -638,6 +654,8 @@ impl FromProof for drive_proof_verifier::types::Documents { /// A grouping or a `having` clause has no meaning for a documents select, and every node /// refuses it. A projection field on the documents select is left alone: the server /// refuses it on the V1 wire, and the V0 encoder drops it, so the documents served verify. + /// The window selections are resolved as the verifier resolves them, which refuses one that + /// does not resolve, or an `In` or range clause beside it on the field it buckets. fn check_request( request: &Self::Request, _platform_version: &PlatformVersion, @@ -670,7 +688,11 @@ impl FromProof for drive_proof_verifier::types::Documents { .to_string(), }); } - Ok(()) + // The verifier rebuilds the resolved query as Drive's before it reads the proof. + let (resolved, _) = request.resolved_for_checking()?; + DriveDocumentQuery::try_from(&resolved) + .map(drop) + .map_err(unconvertible_documents_query) } fn maybe_from_proof_with_metadata<'a, I: Into, O: Into>( @@ -711,12 +733,9 @@ impl FromProof for drive_proof_verifier::types::Documents { normalize_time_range_clauses_with_metadata_time(&mut request, time_ms)?; } - let mut drive_query: DriveDocumentQuery = - (&request) - .try_into() - .map_err(|e| drive_proof_verifier::Error::RequestError { - error: format!("Failed to convert DocumentQuery to DriveQuery: {}", e), - })?; + let mut drive_query: DriveDocumentQuery = (&request) + .try_into() + .map_err(unconvertible_documents_query)?; // The conversion cannot recover which equalities came from resolution, // so the provenance is carried across here; index selection reads it // to pin the query to the index that buckets the field. @@ -775,6 +794,92 @@ pub(super) fn normalize_time_range_clauses_with_metadata_time( Ok(resolved_time_ranges) } +/// A block time well inside any network's lifetime, at which a check before sending resolves +/// a relative time-range selection (newest or oldest window). The checks read that the +/// resolved clause is one equality on the bucketed field, and a ranked or having-range check +/// encodes its value as a timestamp key, which every window start encodes alike, so any block +/// time an answer can carry gives them the same outcome. It is past every grid's phase, which +/// contract validation keeps under a year, so a window resolves here exactly when it resolves +/// at the block time of a real answer. +const CHECKING_BLOCK_TIME_MS: u64 = 1_700_000_000_000; + +impl DocumentQuery { + /// This query with its pending window selections resolved as a verifier resolves them + /// from an answer's block time, and their provenance, for checking it before it is sent. + /// + /// A selection that does not resolve here does not resolve for any answer, so it is + /// refused here as the verifier refuses it. An integer-range selection, and a time-range + /// one by start, resolve from the query alone. A newest or oldest window resolves at the + /// answer's block time, but at any block time past the grid's phase it fails only for the + /// grid: no index buckets the field, or none declares the grid it names. A window by start + /// is resolved at its own start, where it is never past its TTL: whether it has expired + /// depends on the answer's block time, so the server and the verifier decide that. + /// + /// Refused as well, as every verifier refuses them for any answer: a document type the + /// contract lacks, a resolved query whose shape no verifier accepts (an `In` or range + /// clause on a field a selection buckets), and windows on two fields, which no index + /// serves since an index buckets one field. + pub(super) fn resolved_for_checking( + &self, + ) -> Result<(DocumentQuery, Vec), drive_proof_verifier::Error> { + let mut query = self.clone(); + if query.time_range_clauses.is_empty() && query.integer_range_clauses.is_empty() { + return Ok((query, Vec::new())); + } + let document_type = self + .data_contract + .document_type_for_name(&self.document_type_name) + .map_err(unknown_window_document_type)?; + let mut resolved_time_ranges = Vec::new(); + for TimeRangeClause { + field, + selector, + grid, + } in std::mem::take(&mut query.time_range_clauses) + { + // An absolute selection names its window, and is never expired at its own start. + let block_time_ms = match selector { + TimeRangeSelector::ByStart { start_ms } => start_ms, + TimeRangeSelector::Newest | TimeRangeSelector::Oldest => CHECKING_BLOCK_TIME_MS, + }; + let (clause, resolved) = resolve_time_range_bucket_clause( + &field, + selector, + grid, + document_type, + block_time_ms, + ) + .map_err(unresolved_time_range)?; + query.where_clauses.push(clause); + resolved_time_ranges.push(resolved); + } + for IntegerRangeClause { field, start, grid } in + std::mem::take(&mut query.integer_range_clauses) + { + let (clause, resolved) = + resolve_integer_range_bucket_clause(&field, &start, grid, document_type) + .map_err(unresolved_integer_range)?; + query.where_clauses.push(clause); + resolved_time_ranges.push(resolved); + } + validate_resolved_time_range_clause_shapes(&query.where_clauses, &resolved_time_ranges) + .map_err(|e| drive_proof_verifier::Error::RequestError { + error: format!("invalid window selection query shape: {}", e), + })?; + if resolved_time_ranges.len() > 1 { + return Err(drive_proof_verifier::Error::RequestError { + error: format!( + "at most one window selection (IN_TIME_RANGE or IN_INTEGER_RANGE) is \ + supported per query; this one resolves {}, and no single index can \ + bucket more than one field", + resolved_time_ranges.len() + ), + }); + } + Ok((query, resolved_time_ranges)) + } +} + pub(super) fn resolve_time_range_clauses_with_metadata_time( request: &mut DocumentQuery, time_ms: u64, @@ -785,9 +890,7 @@ pub(super) fn resolve_time_range_clauses_with_metadata_time( let data_contract = Arc::clone(&request.data_contract); let document_type = data_contract .document_type_for_name(&request.document_type_name) - .map_err(|e| drive_proof_verifier::Error::RequestError { - error: format!("document type not found for window selection query: {}", e), - })?; + .map_err(unknown_window_document_type)?; let time_range_clauses = std::mem::take(&mut request.time_range_clauses); let mut resolved_time_ranges = Vec::with_capacity(time_range_clauses.len()); for TimeRangeClause { @@ -798,9 +901,7 @@ pub(super) fn resolve_time_range_clauses_with_metadata_time( { let (clause, resolved) = resolve_time_range_bucket_clause(&field, selector, grid, document_type, time_ms) - .map_err(|e| drive_proof_verifier::Error::RequestError { - error: format!("failed to resolve time range clause: {}", e), - })?; + .map_err(unresolved_time_range)?; request.where_clauses.push(clause); resolved_time_ranges.push(resolved); } @@ -809,17 +910,45 @@ pub(super) fn resolve_time_range_clauses_with_metadata_time( let integer_range_clauses = std::mem::take(&mut request.integer_range_clauses); for IntegerRangeClause { field, start, grid } in integer_range_clauses { let (clause, resolved) = - resolve_integer_range_bucket_clause(&field, &start, grid, document_type).map_err( - |e| drive_proof_verifier::Error::RequestError { - error: format!("failed to resolve integer range clause: {}", e), - }, - )?; + resolve_integer_range_bucket_clause(&field, &start, grid, document_type) + .map_err(unresolved_integer_range)?; request.where_clauses.push(clause); resolved_time_ranges.push(resolved); } Ok(resolved_time_ranges) } +/// The refusal of a documents select Drive's query cannot be built from. +fn unconvertible_documents_query(error: impl std::fmt::Display) -> drive_proof_verifier::Error { + drive_proof_verifier::Error::RequestError { + error: format!("Failed to convert DocumentQuery to DriveQuery: {}", error), + } +} + +/// The refusal of a window selection on a document type the contract lacks. +fn unknown_window_document_type(error: impl std::fmt::Display) -> drive_proof_verifier::Error { + drive_proof_verifier::Error::RequestError { + error: format!( + "document type not found for window selection query: {}", + error + ), + } +} + +/// The refusal of a time-range selection that does not resolve. +fn unresolved_time_range(error: DriveError) -> drive_proof_verifier::Error { + drive_proof_verifier::Error::RequestError { + error: format!("failed to resolve time range clause: {}", error), + } +} + +/// The refusal of an integer-range selection that does not resolve. +fn unresolved_integer_range(error: DriveError) -> drive_proof_verifier::Error { + drive_proof_verifier::Error::RequestError { + error: format!("failed to resolve integer range clause: {}", error), + } +} + /// Version-aware encoder. The dispatch is driven by the /// `drive_abci.query.document_query` feature-version on /// [`PlatformVersion`]: `0` → V0 wire (used by v3.0 testnet), `1` → diff --git a/packages/dash-platform-queries/src/documents/document_ranked_entries.rs b/packages/dash-platform-queries/src/documents/document_ranked_entries.rs index 5334fdfe559..ed2d22e9d43 100644 --- a/packages/dash-platform-queries/src/documents/document_ranked_entries.rs +++ b/packages/dash-platform-queries/src/documents/document_ranked_entries.rs @@ -155,7 +155,7 @@ //! ``` use crate::documents::document_query::DocumentQuery; -use crate::documents::ranked_proof_helpers::{assert_ranked_shape, verify_ranked_query}; +use crate::documents::ranked_proof_helpers::{check_ranked_request, verify_ranked_query}; use dapi_grpc::platform::v0::{GetDocumentsResponse, Proof, ResponseMetadata}; use dash_context_provider::ContextProvider; use dpp::dashcore::Network; @@ -168,15 +168,15 @@ impl FromProof for DocumentRankedEntries { /// A request outside the ranked grammar is not refused by every node: a node routing /// with an older query table serves it as a plain grouped aggregate, whose proof the - /// ranked verifier then refuses. Its pending time-range selections only ever add - /// equality pins, which the grammar cannot refuse a request for lacking, so checking - /// before they are resolved refuses nothing the verifier accepts. + /// ranked verifier then refuses. The check resolves the pending window selections as the + /// verifier does, then runs the grammar and picks the covering index with the verifier's + /// own code, so it refuses a window that does not resolve, a resolved shape no verifier + /// accepts, and a query no index serves. fn check_request( request: &Self::Request, platform_version: &PlatformVersion, ) -> Result<(), drive_proof_verifier::Error> { - request.ensure_no_sub_queries()?; - assert_ranked_shape(request, platform_version).map(|_| ()) + check_ranked_request(request, platform_version) } fn maybe_from_proof_with_metadata<'a, I: Into, O: Into>( diff --git a/packages/dash-platform-queries/src/documents/having_proof_helpers.rs b/packages/dash-platform-queries/src/documents/having_proof_helpers.rs index 767aad05a5b..5efe65370d9 100644 --- a/packages/dash-platform-queries/src/documents/having_proof_helpers.rs +++ b/packages/dash-platform-queries/src/documents/having_proof_helpers.rs @@ -16,12 +16,13 @@ //! //! [`DocumentHavingEntries`]: drive_proof_verifier::DocumentHavingEntries -use crate::documents::array_element_clauses::key_element_clauses; +use crate::documents::array_element_clauses::{document_type_named, key_element_clauses}; use crate::documents::document_query::normalize_time_range_clauses_with_metadata_time; use crate::documents::document_query::DocumentQuery; use dapi_grpc::platform::v0::{GetDocumentsResponse, Proof, ResponseMetadata}; use dapi_grpc::platform::VersionedGrpcResponse; use dash_context_provider::ContextProvider; +use dpp::data_contract::document_type::{DocumentTypeRef, Index}; use dpp::version::PlatformVersion; use dpp::{ data_contract::accessors::v0::DataContractV0Getters, @@ -29,8 +30,10 @@ use dpp::{ }; use drive::query::drive_document_having_query::mode_detection::detect_having_mode; use drive::query::drive_document_having_query::resolve_having_query_for_mode; -use drive::query::{DocumentHavingMode, RankedEntry, RankedPaginationInputs}; +use drive::query::drive_document_having_query::DriveDocumentHavingQuery; +use drive::query::{DocumentHavingMode, RankedEntry, RankedPaginationInputs, ResolvedTimeRange}; use drive_proof_verifier::verify_having_range_proof; +use std::collections::BTreeMap; /// Validate that the caller-built [`DocumentQuery`] really describes a /// having-range query, and resolve it into the `(bounds, descending, @@ -90,6 +93,62 @@ pub(super) fn assert_having_shape( }) } +/// The having-range query a node runs for `request`, resolved exactly as the prover resolves +/// it with rs-drive's own resolution: the covering index pick, shared with the ranked surface, +/// and the encoding of the pinned prefix into path segments. +fn covering_having_query<'a>( + request: &DocumentQuery, + document_type: DocumentTypeRef<'a>, + indexes: &'a BTreeMap, + mode: &DocumentHavingMode, + resolved_time_ranges: &[ResolvedTimeRange], + platform_version: &PlatformVersion, +) -> Result, drive_proof_verifier::Error> { + resolve_having_query_for_mode( + request.data_contract.id().to_buffer(), + document_type, + request.document_type_name.clone(), + indexes, + mode, + resolved_time_ranges, + platform_version, + ) + .map_err(|e| drive_proof_verifier::Error::RequestError { + error: format!( + "document type `{}` cannot serve this having-range query: {e}. Ranked indexes \ + are opt-in contract grammar (meta-schema v3, protocol version 14+); a pinned \ + (compound-index) bound additionally needs every leading index property pinned \ + by a where clause — equality pins, of which at most one may be an `IN` of \ + 2..=10 elements.", + request.document_type_name, + ), + }) +} + +/// The refusals the having-range verifier makes from the request alone, made before it is +/// sent: the having grammar, and whether an index of the contract serves the bound. Pending +/// window selections are resolved first, as the verifier resolves them +/// ([`DocumentQuery::resolved_for_checking`]), which refuses a query whose selections do +/// not resolve. +pub(super) fn check_having_request( + request: &DocumentQuery, + platform_version: &PlatformVersion, +) -> Result<(), drive_proof_verifier::Error> { + request.ensure_no_sub_queries()?; + let (resolved, resolved_time_ranges) = request.resolved_for_checking()?; + let mode = assert_having_shape(&resolved, platform_version)?; + let document_type = document_type_named(&resolved.data_contract, &resolved.document_type_name)?; + covering_having_query( + &resolved, + document_type, + document_type.indexes(), + &mode, + &resolved_time_ranges, + platform_version, + ) + .map(drop) +} + /// Verify a having-range proof and return the verified entries — the /// matching groups **in axis order in the walk direction**. /// @@ -126,41 +185,16 @@ pub(super) fn verify_having_query( let resolved_time_ranges = normalize_time_range_clauses_with_metadata_time(&mut request, mtd.time_ms)?; - let document_type = request - .data_contract - .document_type_for_name(&request.document_type_name) - .map_err(|e| drive_proof_verifier::Error::RequestError { - error: format!( - "document type {} not found in contract: {}", - request.document_type_name, e - ), - })?; - let mode = assert_having_shape(&request, platform_version)?; - - // Resolve the query exactly as the prover did — rs-drive's own - // resolution (covering-index pick, shared with the ranked surface - // because both read the same indexed tree, plus the equality-pin - // encoding into prefix path segments for pinned compound requests). - let having_query = resolve_having_query_for_mode( - request.data_contract.id().to_buffer(), + let document_type = document_type_named(&request.data_contract, &request.document_type_name)?; + let having_query = covering_having_query( + &request, document_type, - request.document_type_name.clone(), document_type.indexes(), &mode, &resolved_time_ranges, platform_version, - ) - .map_err(|e| drive_proof_verifier::Error::RequestError { - error: format!( - "document type `{}` cannot serve this having-range query: {e}. Ranked indexes \ - are opt-in contract grammar (meta-schema v3, protocol version 14+); a pinned \ - (compound-index) bound additionally needs every leading index property pinned \ - by a where clause — equality pins, of which at most one may be an `IN` of \ - 2..=10 elements.", - request.document_type_name, - ), - })?; + )?; // Binds the reconstructed grovedb root hash to the quorum-signed // app hash before returning — see the module docs. diff --git a/packages/dash-platform-queries/src/documents/ranked_proof_helpers.rs b/packages/dash-platform-queries/src/documents/ranked_proof_helpers.rs index 576e178e593..15ea9f4a5a2 100644 --- a/packages/dash-platform-queries/src/documents/ranked_proof_helpers.rs +++ b/packages/dash-platform-queries/src/documents/ranked_proof_helpers.rs @@ -19,12 +19,13 @@ //! //! [`DocumentRankedEntries`]: drive_proof_verifier::DocumentRankedEntries -use crate::documents::array_element_clauses::key_element_clauses; +use crate::documents::array_element_clauses::{document_type_named, key_element_clauses}; use crate::documents::document_query::normalize_time_range_clauses_with_metadata_time; use crate::documents::document_query::DocumentQuery; use dapi_grpc::platform::v0::{GetDocumentsResponse, Proof, ResponseMetadata}; use dapi_grpc::platform::VersionedGrpcResponse; use dash_context_provider::ContextProvider; +use dpp::data_contract::document_type::{DocumentTypeRef, Index}; use dpp::version::PlatformVersion; use dpp::{ data_contract::accessors::v0::DataContractV0Getters, @@ -32,8 +33,10 @@ use dpp::{ }; use drive::query::drive_document_ranked_query::index_picker::resolve_ranked_query_for_mode; use drive::query::drive_document_ranked_query::mode_detection::detect_ranked_mode; -use drive::query::{DocumentRankedMode, RankedPage, RankedPaginationInputs}; +use drive::query::drive_document_ranked_query::DriveDocumentRankedQuery; +use drive::query::{DocumentRankedMode, RankedPage, RankedPaginationInputs, ResolvedTimeRange}; use drive_proof_verifier::verify_ranked_top_k_proof; +use std::collections::BTreeMap; /// Validate that the caller-built [`DocumentQuery`] really describes a /// ranked query, and resolve it into the @@ -107,6 +110,63 @@ pub(super) fn assert_ranked_shape( }) } +/// The ranked query a node runs for `request`, resolved exactly as the prover resolves it +/// with rs-drive's own resolution: the covering index pick and the encoding of the pinned +/// prefix into path segments. A second implementation could pick another index on a +/// contract with several candidates, or encode a pin differently. +fn covering_ranked_query<'a>( + request: &DocumentQuery, + document_type: DocumentTypeRef<'a>, + indexes: &'a BTreeMap, + mode: &DocumentRankedMode, + resolved_time_ranges: &[ResolvedTimeRange], + platform_version: &PlatformVersion, +) -> Result, drive_proof_verifier::Error> { + resolve_ranked_query_for_mode( + request.data_contract.id().to_buffer(), + document_type, + request.document_type_name.clone(), + indexes, + mode, + resolved_time_ranges, + platform_version, + ) + .map_err(|e| drive_proof_verifier::Error::RequestError { + error: format!( + "document type `{}` cannot serve this ranked query: {e}. Ranked indexes are \ + opt-in contract grammar (meta-schema v3, protocol version 14+); a pinned \ + (compound-index) ranking additionally needs every leading index property \ + pinned by a where clause — equality pins, of which at most one may be an \ + `IN` of 2..=10 elements.", + request.document_type_name, + ), + }) +} + +/// The refusals the ranked verifier makes from the request alone, made before it is sent: +/// the ranked grammar, and whether an index of the contract serves the ranking. Pending +/// window selections are resolved first, as the verifier resolves them +/// ([`DocumentQuery::resolved_for_checking`]), which refuses a query whose selections do +/// not resolve. +pub(super) fn check_ranked_request( + request: &DocumentQuery, + platform_version: &PlatformVersion, +) -> Result<(), drive_proof_verifier::Error> { + request.ensure_no_sub_queries()?; + let (resolved, resolved_time_ranges) = request.resolved_for_checking()?; + let mode = assert_ranked_shape(&resolved, platform_version)?; + let document_type = document_type_named(&resolved.data_contract, &resolved.document_type_name)?; + covering_ranked_query( + &resolved, + document_type, + document_type.indexes(), + &mode, + &resolved_time_ranges, + platform_version, + ) + .map(drop) +} + /// Verify a ranked-shape proof and return the verified [`RankedPage`] — /// the entries **in ranking order**, plus the attested rank the page /// starts at. @@ -155,44 +215,16 @@ pub(super) fn verify_ranked_query( let resolved_time_ranges = normalize_time_range_clauses_with_metadata_time(&mut request, mtd.time_ms)?; - let document_type = request - .data_contract - .document_type_for_name(&request.document_type_name) - .map_err(|e| drive_proof_verifier::Error::RequestError { - error: format!( - "document type {} not found in contract: {}", - request.document_type_name, e - ), - })?; - let mode = assert_ranked_shape(&request, platform_version)?; - - // Resolve the query exactly as the prover did — rs-drive's own - // resolution (covering-index pick + equality-pin encoding into - // prefix path segments). A second implementation here could choose - // a different index on a contract with several candidates, or - // encode a pinned prefix value differently, and then verify a proof - // of the wrong subtree's ranking (or, more likely, fail to verify - // at all with a confusing merk error). - let ranked_query = resolve_ranked_query_for_mode( - request.data_contract.id().to_buffer(), + let document_type = document_type_named(&request.data_contract, &request.document_type_name)?; + let ranked_query = covering_ranked_query( + &request, document_type, - request.document_type_name.clone(), document_type.indexes(), &mode, &resolved_time_ranges, platform_version, - ) - .map_err(|e| drive_proof_verifier::Error::RequestError { - error: format!( - "document type `{}` cannot serve this ranked query: {e}. Ranked indexes are \ - opt-in contract grammar (meta-schema v3, protocol version 14+); a pinned \ - (compound-index) ranking additionally needs every leading index property \ - pinned by a where clause — equality pins, of which at most one may be an \ - `IN` of 2..=10 elements.", - request.document_type_name, - ), - })?; + )?; // Binds the reconstructed grovedb root hash to the quorum-signed // app hash before returning — see the module docs. diff --git a/packages/dash-platform-queries/src/documents/sum_proof_helpers.rs b/packages/dash-platform-queries/src/documents/sum_proof_helpers.rs index f1d5c203e69..5ea776a0825 100644 --- a/packages/dash-platform-queries/src/documents/sum_proof_helpers.rs +++ b/packages/dash-platform-queries/src/documents/sum_proof_helpers.rs @@ -94,8 +94,11 @@ pub(super) fn check_sum_request( ), }, )?; + let (resolved, _) = request.resolved_for_checking()?; + let request = &resolved; let where_clauses = aggregate_request_where_clauses(request, folds_entries, platform_version)?; - resolve_sum_mode(&request.group_by, &where_clauses).map(|_| ()) + resolve_sum_mode(&request.group_by, &where_clauses)?; + request.ensure_no_having() } /// Verify a SUM-shape proof and return per-branch `SumEntry`s. diff --git a/packages/rs-sdk/src/sdk/document_query_shape_tests.rs b/packages/rs-sdk/src/sdk/document_query_shape_tests.rs index 6ebb918a297..f10eeb06408 100644 --- a/packages/rs-sdk/src/sdk/document_query_shape_tests.rs +++ b/packages/rs-sdk/src/sdk/document_query_shape_tests.rs @@ -42,9 +42,37 @@ fn query() -> DocumentQuery { DocumentQuery::new(contract, "niceDocument").expect("the fixture has this document type") } -/// `SELECT AVG(grade) GROUP BY restaurantId ORDER BY grade DESC LIMIT 5`. +/// A `review` whose index ranks restaurants by their average grade, so a ranked query over it +/// is served. +fn reviews() -> DocumentQuery { + let schemas = platform_value!({"review": { + "type": "object", + "properties": { + "restaurantId": {"type": "string", "minLength": 1, "maxLength": 59, "position": 0}, + "grade": {"type": "integer", "minimum": 0, "maximum": 100, "position": 1} + }, + "required": ["restaurantId", "grade"], + "indices": [ + {"name": "byRestaurant", + "properties": [{"restaurantId": "asc"}], + "averageable": "grade", + "rangeAverageable": true, + "rankedAverageable": true} + ], + "additionalProperties": false + }}); + let contract = DataContractFactory::new(PlatformVersion::latest().protocol_version) + .expect("factory") + .create_with_value_config(Identifier::new([9u8; 32]), 0, schemas, None, None) + .expect("the review contract is well-formed") + .data_contract_owned(); + DocumentQuery::new(contract, "review").expect("the contract has this document type") +} + +/// `SELECT AVG(grade) GROUP BY restaurantId ORDER BY grade DESC LIMIT 5`, over a contract that +/// ranks restaurants by it. fn ranked() -> DocumentQuery { - query() + reviews() .with_select(SelectProjection::avg("grade")) .with_group_by("restaurantId") .order_by_selected_aggregate(RankingDirection::Descending) diff --git a/packages/wasm-sdk/src/queries/document_ranked.rs b/packages/wasm-sdk/src/queries/document_ranked.rs index 59cc3f0602f..2193f998095 100644 --- a/packages/wasm-sdk/src/queries/document_ranked.rs +++ b/packages/wasm-sdk/src/queries/document_ranked.rs @@ -1433,9 +1433,13 @@ mod wasm_tests { #[cfg(test)] mod tests { use super::*; + use dash_sdk::dash_platform_queries::documents::array_element_clauses::key_element_clauses; use dash_sdk::dpp::prelude::DataContract; use dash_sdk::dpp::system_data_contracts::{load_system_data_contract, SystemDataContract}; + use drive::query::drive_document_having_query::mode_detection::detect_having_mode; + use drive::query::drive_document_ranked_query::mode_detection::detect_ranked_mode; use drive::query::drive_document_ranked_query::MAX_PREFIX_IN_BRANCHES; + use drive::query::RankedPaginationInputs; use drive::query::{WhereOperator, MAX_HAVING_LIMIT, MAX_RANKED_LIMIT, RANKED_COUNT_ORDER_KEY}; use drive_proof_verifier::FromProof; use serde_json::json; @@ -1529,25 +1533,58 @@ mod tests { } } - /// The query the JS input builds, checked as the SDK checks it before sending. + /// The pagination triple the grammar reads, off a built query. + fn pagination(query: &DocumentQuery) -> RankedPaginationInputs { + RankedPaginationInputs { + limit: (query.limit != 0).then_some(query.limit), + offset: query.offset, + has_start_at: query.start.is_some(), + } + } + + /// The where clauses as the grammar reads them: a typed array's `contains` pin keyed. + fn keyed(query: &DocumentQuery) -> Result, String> { + key_element_clauses( + &query.data_contract, + &query.document_type_name, + query.where_clauses.clone(), + false, + platform_version(), + ) + .map_err(|e| e.to_string()) + } + + /// The query the JS input builds, checked against the ranked grammar the SDK checks it + /// against before sending (rs-drive's own classifier). Whether an index serves it is the + /// SDK's other check, tested there: this fixture declares no ranked index. fn build_ranked(input: DocumentsRankedQueryInput) -> Result { let query = apply_ranked_shape(base_query(), &input).map_err(|e| e.to_string())?; - >::check_request( - &query, + detect_ranked_mode( + &query.select, + &query.group_by, + &query.having, + &query.order_by_clauses, + &keyed(&query)?, + pagination(&query), platform_version(), ) - .map_err(|e| e.to_string())?; + .map_err(|e| format!("not a well-formed ranked query: {e}"))?; Ok(query) } - /// The query the JS input builds, checked as the SDK checks it before sending. + /// The having-range counterpart of [`build_ranked`]. fn build_having(input: DocumentsHavingQueryInput) -> Result { let query = apply_having_shape(base_query(), &input).map_err(|e| e.to_string())?; - >::check_request( - &query, + detect_having_mode( + &query.select, + &query.group_by, + &query.having, + &query.order_by_clauses, + &keyed(&query)?, + pagination(&query), platform_version(), ) - .map_err(|e| e.to_string())?; + .map_err(|e| format!("not a well-formed having-range query: {e}"))?; Ok(query) }