diff --git a/Cargo.lock b/Cargo.lock index e2fcf55c579..4a05631bbaf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2956,8 +2956,8 @@ dependencies = [ [[package]] name = "grovedb" -version = "6.0.0" -source = "git+https://github.com/dashpay/grovedb?rev=6816457d2b2df98653f286bf9a374f236a17748f#6816457d2b2df98653f286bf9a374f236a17748f" +version = "6.0.1" +source = "git+https://github.com/dashpay/grovedb?rev=a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6#a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" dependencies = [ "axum 0.8.9", "blake3", @@ -3015,8 +3015,8 @@ dependencies = [ [[package]] name = "grovedb-bulk-append-tree" -version = "6.0.0" -source = "git+https://github.com/dashpay/grovedb?rev=6816457d2b2df98653f286bf9a374f236a17748f#6816457d2b2df98653f286bf9a374f236a17748f" +version = "6.0.1" +source = "git+https://github.com/dashpay/grovedb?rev=a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6#a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" dependencies = [ "blake3", "grovedb-bincode", @@ -3033,8 +3033,8 @@ dependencies = [ [[package]] name = "grovedb-commitment-tree" -version = "6.0.0" -source = "git+https://github.com/dashpay/grovedb?rev=6816457d2b2df98653f286bf9a374f236a17748f#6816457d2b2df98653f286bf9a374f236a17748f" +version = "6.0.1" +source = "git+https://github.com/dashpay/grovedb?rev=a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6#a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" dependencies = [ "blake3", "grovedb-bulk-append-tree", @@ -3050,8 +3050,8 @@ dependencies = [ [[package]] name = "grovedb-costs" -version = "6.0.0" -source = "git+https://github.com/dashpay/grovedb?rev=6816457d2b2df98653f286bf9a374f236a17748f#6816457d2b2df98653f286bf9a374f236a17748f" +version = "6.0.1" +source = "git+https://github.com/dashpay/grovedb?rev=a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6#a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" dependencies = [ "integer-encoding", "intmap", @@ -3060,8 +3060,8 @@ dependencies = [ [[package]] name = "grovedb-dense-fixed-sized-merkle-tree" -version = "6.0.0" -source = "git+https://github.com/dashpay/grovedb?rev=6816457d2b2df98653f286bf9a374f236a17748f#6816457d2b2df98653f286bf9a374f236a17748f" +version = "6.0.1" +source = "git+https://github.com/dashpay/grovedb?rev=a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6#a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" dependencies = [ "blake3", "grovedb-bincode", @@ -3074,8 +3074,8 @@ dependencies = [ [[package]] name = "grovedb-element" -version = "6.0.0" -source = "git+https://github.com/dashpay/grovedb?rev=6816457d2b2df98653f286bf9a374f236a17748f#6816457d2b2df98653f286bf9a374f236a17748f" +version = "6.0.1" +source = "git+https://github.com/dashpay/grovedb?rev=a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6#a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" dependencies = [ "grovedb-bincode", "grovedb-bincode-derive", @@ -3090,8 +3090,8 @@ dependencies = [ [[package]] name = "grovedb-epoch-based-storage-flags" -version = "6.0.0" -source = "git+https://github.com/dashpay/grovedb?rev=6816457d2b2df98653f286bf9a374f236a17748f#6816457d2b2df98653f286bf9a374f236a17748f" +version = "6.0.1" +source = "git+https://github.com/dashpay/grovedb?rev=a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6#a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" dependencies = [ "grovedb-costs", "hex", @@ -3102,8 +3102,8 @@ dependencies = [ [[package]] name = "grovedb-merk" -version = "6.0.0" -source = "git+https://github.com/dashpay/grovedb?rev=6816457d2b2df98653f286bf9a374f236a17748f#6816457d2b2df98653f286bf9a374f236a17748f" +version = "6.0.1" +source = "git+https://github.com/dashpay/grovedb?rev=a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6#a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" dependencies = [ "blake3", "byteorder", @@ -3128,8 +3128,8 @@ dependencies = [ [[package]] name = "grovedb-merkle-mountain-range" -version = "6.0.0" -source = "git+https://github.com/dashpay/grovedb?rev=6816457d2b2df98653f286bf9a374f236a17748f#6816457d2b2df98653f286bf9a374f236a17748f" +version = "6.0.1" +source = "git+https://github.com/dashpay/grovedb?rev=a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6#a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" dependencies = [ "blake3", "grovedb-bincode", @@ -3141,16 +3141,16 @@ dependencies = [ [[package]] name = "grovedb-path" -version = "6.0.0" -source = "git+https://github.com/dashpay/grovedb?rev=6816457d2b2df98653f286bf9a374f236a17748f#6816457d2b2df98653f286bf9a374f236a17748f" +version = "6.0.1" +source = "git+https://github.com/dashpay/grovedb?rev=a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6#a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" dependencies = [ "hex", ] [[package]] name = "grovedb-private-document-store" -version = "6.0.0" -source = "git+https://github.com/dashpay/grovedb?rev=6816457d2b2df98653f286bf9a374f236a17748f#6816457d2b2df98653f286bf9a374f236a17748f" +version = "6.0.1" +source = "git+https://github.com/dashpay/grovedb?rev=a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6#a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" dependencies = [ "blake3", "grovedb-bulk-append-tree", @@ -3162,8 +3162,8 @@ dependencies = [ [[package]] name = "grovedb-query" -version = "6.0.0" -source = "git+https://github.com/dashpay/grovedb?rev=6816457d2b2df98653f286bf9a374f236a17748f#6816457d2b2df98653f286bf9a374f236a17748f" +version = "6.0.1" +source = "git+https://github.com/dashpay/grovedb?rev=a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6#a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" dependencies = [ "byteorder", "ed", @@ -3178,8 +3178,8 @@ dependencies = [ [[package]] name = "grovedb-storage" -version = "6.0.0" -source = "git+https://github.com/dashpay/grovedb?rev=6816457d2b2df98653f286bf9a374f236a17748f#6816457d2b2df98653f286bf9a374f236a17748f" +version = "6.0.1" +source = "git+https://github.com/dashpay/grovedb?rev=a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6#a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" dependencies = [ "blake3", "grovedb-costs", @@ -3197,8 +3197,8 @@ dependencies = [ [[package]] name = "grovedb-version" -version = "6.0.0" -source = "git+https://github.com/dashpay/grovedb?rev=6816457d2b2df98653f286bf9a374f236a17748f#6816457d2b2df98653f286bf9a374f236a17748f" +version = "6.0.1" +source = "git+https://github.com/dashpay/grovedb?rev=a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6#a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" dependencies = [ "thiserror 2.0.18", "versioned-feature-core", @@ -3206,8 +3206,8 @@ dependencies = [ [[package]] name = "grovedb-visualize" -version = "6.0.0" -source = "git+https://github.com/dashpay/grovedb?rev=6816457d2b2df98653f286bf9a374f236a17748f#6816457d2b2df98653f286bf9a374f236a17748f" +version = "6.0.1" +source = "git+https://github.com/dashpay/grovedb?rev=a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6#a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" dependencies = [ "hex", "itertools 0.14.0", @@ -3215,8 +3215,8 @@ dependencies = [ [[package]] name = "grovedbg-types" -version = "6.0.0" -source = "git+https://github.com/dashpay/grovedb?rev=6816457d2b2df98653f286bf9a374f236a17748f#6816457d2b2df98653f286bf9a374f236a17748f" +version = "6.0.1" +source = "git+https://github.com/dashpay/grovedb?rev=a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6#a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" dependencies = [ "serde", "serde_with 3.21.0", @@ -5601,8 +5601,8 @@ version = "0.14.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "03da047801ff44bb6a4d407d4860c05fd70bb81714e6b2f3812603d5b145b042" dependencies = [ - "heck 0.5.0", - "itertools 0.14.0", + "heck 0.4.1", + "itertools 0.13.0", "log", "multimap", "petgraph", @@ -5623,7 +5623,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a56d757972c98b346a9b766e3f02746cde6dd1cd1d1d563472929fdd74bec4d" dependencies = [ "anyhow", - "itertools 0.14.0", + "itertools 0.13.0", "proc-macro2", "quote", "syn 2.0.117", @@ -5636,7 +5636,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b570b25f7617e43d59005d0990ccb79e950a423952cea19671b7a876da390adf" dependencies = [ "anyhow", - "itertools 0.14.0", + "itertools 0.13.0", "proc-macro2", "quote", "syn 2.0.117", diff --git a/Cargo.toml b/Cargo.toml index 8a28221ca82..9da02250909 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,6 +54,14 @@ members = [ [workspace.dependencies] bincode = { package = "grovedb-bincode", version = "=2.1.0" } +grovedb = { git = "https://github.com/dashpay/grovedb", rev = "a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6", default-features = false } +grovedb-costs = { git = "https://github.com/dashpay/grovedb", rev = "a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" } +grovedb-path = { git = "https://github.com/dashpay/grovedb", rev = "a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" } +grovedb-query = { git = "https://github.com/dashpay/grovedb", rev = "a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" } +grovedb-storage = { git = "https://github.com/dashpay/grovedb", rev = "a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" } +grovedb-version = { git = "https://github.com/dashpay/grovedb", rev = "a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" } +grovedb-epoch-based-storage-flags = { git = "https://github.com/dashpay/grovedb", rev = "a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" } +grovedb-commitment-tree = { git = "https://github.com/dashpay/grovedb", rev = "a579d52fcf68c3cbd798c5a94e73afdc41b5f5a6" } dashcore = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } dash-network-seeds = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } dash-spv = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } diff --git a/book/src/drive/document-ranked-trees.md b/book/src/drive/document-ranked-trees.md index 3c30f621dce..5e0d8f59bb5 100644 --- a/book/src/drive/document-ranked-trees.md +++ b/book/src/drive/document-ranked-trees.md @@ -403,7 +403,7 @@ Note that the fixture puts each shape on its **own document type**. That's not a | Top / bottom K groups by sum of a property | `rankedSummable: true` on an index with `summable: ""` + `rangeSummable: true` | | Top / bottom K groups by average of a property | `rankedAverageable: true` on an index with `averageable: ""` + `rangeAverageable: true` (or the count+sum longhand) | | Two rankings on one index (e.g. by count *and* by average) | Both keywords. The tree is a PCPSIT carrying both axes in its TLV; you pay one secondary Merk per axis on every write. | -| A ranking filtered by another property (`top 5 restaurants in London`) | A **compound ranked index** with the filter property leading: `[city, restaurantId]` with the ranked flags. Each city gets its own secondary; the query pins the prefix with an equality `where` (`WHERE city == "London" GROUP BY restaurantId ORDER BY DESC LIMIT 5`). Equality pins select one prefix; at most one pin may be an `IN` (2..=10 distinct elements; a never-written element — or one whose deeper pinned path was never written — contributes an empty branch), which walks one secondary per element and merges by `(aggregate, encoded prefix, group key)`, proved in one branched `PathQuery` envelope (shared ancestors proved once, per-element authenticated absence) — entries then carry `in_key`. A single-element `IN` normalizes to the equality pin; a `null` pin stays legal on its own but cannot combine with an `IN` (null addresses its prefix through an empty path segment the branched proof cannot express); a non-zero `OFFSET` is rejected together with `IN`; and branched proofs are generated from committed state only. A range operator on the prefix stays rejected, and there is still no global cross-prefix ordering beyond that merge. | +| A ranking filtered by another property (`top 5 restaurants in London`) | A **compound ranked index** with the filter property leading: `[city, restaurantId]` with the ranked flags. Each city gets its own secondary; the query pins the prefix with an equality `where` (`WHERE city == "London" GROUP BY restaurantId ORDER BY DESC LIMIT 5`). Equality pins select one prefix; at most one pin may be an `IN` (2..=10 distinct elements; a never-written element — or one whose deeper pinned path was never written — contributes an empty branch), which walks one secondary per element and merges by `(aggregate, encoded prefix, group key)`, proved in one branched `PathQuery` envelope (shared ancestors proved once, per-element authenticated absence) — entries then carry `in_key`. A single `==` pin on a prefix no document has written yet (a `timeRange` window before its first document, a hashtag nobody has used) is likewise an authenticated **empty page**, at any `OFFSET`: GroveDB answers a single-path axis read over a path that does not exist with the traversal's empty result, the absence proved by the layers the walk emits rather than reported as an error. A single-element `IN` normalizes to the equality pin; a `null` pin stays legal on its own but cannot combine with an `IN` (null addresses its prefix through an empty path segment the branched proof cannot express); a non-zero `OFFSET` is rejected together with `IN`; and branched proofs are generated from committed state only. A range operator on the prefix stays rejected, and there is still no global cross-prefix ordering beyond that merge. | | A ranking on a unique or contested index | Not available, and not meaningful: every group holds at most one document. | | Range aggregates without ranking (the 4.0 surface) | Just the `range*` flags. Ranking is strictly additive — adding it never changes what a range query returns. | | Nothing ranking-aware (default) | Don't set any `ranked*` flag. The terminal property-name tree keeps the type its range flags give it. | diff --git a/book/src/drive/time-range-ttl.md b/book/src/drive/time-range-ttl.md index 6e4b2d47850..da520e16fd3 100644 --- a/book/src/drive/time-range-ttl.md +++ b/book/src/drive/time-range-ttl.md @@ -38,7 +38,9 @@ buckets are drained **lazily, on write**: every state transition that writes into the index continues draining the oldest expired bucket, deepest-first, under a per-write operation budget. A fully drained window is provably absent, exactly like a window that never held -documents. An expired window is **not queryable at all** — `byStart` +documents: a count over it proves zero, and a ranked or having-range +query pinned to it proves an empty page (see the ranked chapter's +pinned-prefix rules). An expired window is **not queryable at all** — `byStart` rejects starts past the horizon, so the drainage lag is purely internal: drainage only ever touches expired buckets, which makes every window a query can address complete. Everything written under the index's diff --git a/packages/rs-dpp/Cargo.toml b/packages/rs-dpp/Cargo.toml index b1a6d6a8c61..bf9bba16c3c 100644 --- a/packages/rs-dpp/Cargo.toml +++ b/packages/rs-dpp/Cargo.toml @@ -76,7 +76,7 @@ strum = { version = "0.26", features = ["derive"] } json-schema-compatibility-validator = { path = '../rs-json-schema-compatibility-validator', optional = true } once_cell = "1.19.0" tracing = { version = "0.1.41" } -grovedb-commitment-tree = { git = "https://github.com/dashpay/grovedb", rev = "6816457d2b2df98653f286bf9a374f236a17748f", optional = true } +grovedb-commitment-tree = { workspace = true, optional = true } [dev-dependencies] tokio = { version = "1.40", features = ["full"] } diff --git a/packages/rs-drive-abci/Cargo.toml b/packages/rs-drive-abci/Cargo.toml index dccf7521b5c..3a109ac0789 100644 --- a/packages/rs-drive-abci/Cargo.toml +++ b/packages/rs-drive-abci/Cargo.toml @@ -82,7 +82,7 @@ derive_more = { version = "1.0", features = ["from", "deref", "deref_mut"] } async-trait = "0.1.77" console-subscriber = { version = "0.4", optional = true } bls-signatures = { git = "https://github.com/dashpay/bls-signatures", rev = "0842b17583888e8f46c252a4ee84cdfd58e0546f", optional = true } -grovedb-commitment-tree = { git = "https://github.com/dashpay/grovedb", rev = "6816457d2b2df98653f286bf9a374f236a17748f" } +grovedb-commitment-tree = { workspace = true } nonempty = "0.11" # Shielded-pool snapshot needs raw RocksDB SstFileWriter + ingest_external_file_cf # bindings, and blake3 for the snapshot-file checksum. @@ -108,7 +108,7 @@ dpp = { path = "../rs-dpp", default-features = false, features = [ drive = { path = "../rs-drive", features = ["fixtures-and-mocks"] } drive-proof-verifier = { path = "../rs-drive-proof-verifier" } strategy-tests = { path = "../strategy-tests" } -grovedb-commitment-tree = { git = "https://github.com/dashpay/grovedb", rev = "6816457d2b2df98653f286bf9a374f236a17748f", features = ["client"] } +grovedb-commitment-tree = { workspace = true, features = ["client"] } assert_matches = "1.5.0" drive-abci = { path = ".", features = ["testing-config", "mocks", "shielded_test_data"] } bls-signatures = { git = "https://github.com/dashpay/bls-signatures", rev = "0842b17583888e8f46c252a4ee84cdfd58e0546f" } @@ -122,8 +122,8 @@ integer-encoding = { version = "4.0.0" } # For dump_only_default_and_aux_cfs_under_shielded_subtree_prefix — same # subtree-prefix algorithm grovedb uses internally. -grovedb-path = { git = "https://github.com/dashpay/grovedb", rev = "6816457d2b2df98653f286bf9a374f236a17748f" } -grovedb-storage = { git = "https://github.com/dashpay/grovedb", rev = "6816457d2b2df98653f286bf9a374f236a17748f" } +grovedb-path = { workspace = true } +grovedb-storage = { workspace = true } [features] default = ["bls-signatures"] diff --git a/packages/rs-drive-abci/src/query/document_query/v1/tests.rs b/packages/rs-drive-abci/src/query/document_query/v1/tests.rs index cffb2c62c9a..1d8a8614932 100644 --- a/packages/rs-drive-abci/src/query/document_query/v1/tests.rs +++ b/packages/rs-drive-abci/src/query/document_query/v1/tests.rs @@ -5919,6 +5919,52 @@ mod time_range_proof_verification { assert_hashtag_counts(&page.entries, &[("ibiza", 3), ("berlin", 1)]); } + /// A window no document has landed in — every fresh window of a + /// `range == step` grid until its first post — ranks EMPTY through + /// the entry point, proof and all, rather than failing. The window's + /// bucket tree is created by the first write under it, so the pinned + /// path does not exist; grovedb answers the axis read over that + /// absent path with an empty page its proof's own layers + /// authenticate (grovedb #965), and the SDK verifies it at rank 0 + /// against the signed root hash. Before that the prover refused ("a + /// single-path axis read must produce exactly one axis descent") and + /// every such request surfaced as an internal error. The window + /// starting four steps back covers `[start − 8h, start − 2h)`, which + /// none of the fixture's posts fall into. + #[test] + fn an_unwritten_window_ranks_empty_through_the_entry_point() { + let (platform, base_state, version) = setup_platform(None, Network::Testnet, None); + let (contract, state) = setup_ranked_trending(&platform, &base_state, version); + let unwritten_start_ms = NEWEST_BUCKET_START_MS - 8 * HOUR_MS; + + let request = windowed_top_k_request( + contract.id().to_vec(), + by_start_selection(unwritten_start_ms), + ); + let (proof, mtd, provider) = prove_and_sign(&platform, &state, request, version); + + let (page, _mtd, _proof) = + >::maybe_from_proof_with_metadata( + windowed_top_k_query( + &contract, + TimeRangeSelector::ByStart { + start_ms: unwritten_start_ms, + }, + ), + signed_response(proof, &mtd), + Network::Testnet, + version, + &provider, + ) + .expect("a correctly signed empty window must verify"); + let page = page.expect("the ranked entry point always returns a page"); + assert_eq!(page.starting_rank, 0); + assert!( + page.entries.is_empty(), + "an unwritten window is an authenticated empty leaderboard" + ); + } + /// The window-binding property of the relative selectors, on the /// ranked surface: re-signing the response over a one-step-later /// metadata time makes the verifier re-derive a *different* bucket diff --git a/packages/rs-drive/Cargo.toml b/packages/rs-drive/Cargo.toml index b73a0ebaec5..3c5fac3a00b 100644 --- a/packages/rs-drive/Cargo.toml +++ b/packages/rs-drive/Cargo.toml @@ -52,13 +52,13 @@ enum-map = { version = "2.0.3", optional = true } intmap = { version = "3.0.1", features = ["serde"], optional = true } chrono = { version = "0.4.35", optional = true } itertools = { version = "0.13", optional = true } -grovedb = { git = "https://github.com/dashpay/grovedb", rev = "6816457d2b2df98653f286bf9a374f236a17748f", optional = true, default-features = false } -grovedb-costs = { git = "https://github.com/dashpay/grovedb", rev = "6816457d2b2df98653f286bf9a374f236a17748f", optional = true } -grovedb-path = { git = "https://github.com/dashpay/grovedb", rev = "6816457d2b2df98653f286bf9a374f236a17748f" } -grovedb-query = { git = "https://github.com/dashpay/grovedb", rev = "6816457d2b2df98653f286bf9a374f236a17748f" } -grovedb-storage = { git = "https://github.com/dashpay/grovedb", rev = "6816457d2b2df98653f286bf9a374f236a17748f", optional = true } -grovedb-version = { git = "https://github.com/dashpay/grovedb", rev = "6816457d2b2df98653f286bf9a374f236a17748f" } -grovedb-epoch-based-storage-flags = { git = "https://github.com/dashpay/grovedb", rev = "6816457d2b2df98653f286bf9a374f236a17748f" } +grovedb = { workspace = true, optional = true } +grovedb-costs = { workspace = true, optional = true } +grovedb-path = { workspace = true } +grovedb-query = { workspace = true } +grovedb-storage = { workspace = true, optional = true } +grovedb-version = { workspace = true } +grovedb-epoch-based-storage-flags = { workspace = true } [dev-dependencies] criterion = "0.5" diff --git a/packages/rs-drive/src/drive/document/insert/add_document_for_contract/time_range_index_e2e_tests.rs b/packages/rs-drive/src/drive/document/insert/add_document_for_contract/time_range_index_e2e_tests.rs index c65ca638db5..a92cc99af5a 100644 --- a/packages/rs-drive/src/drive/document/insert/add_document_for_contract/time_range_index_e2e_tests.rs +++ b/packages/rs-drive/src/drive/document/insert/add_document_for_contract/time_range_index_e2e_tests.rs @@ -2889,6 +2889,218 @@ fn ttl_drops_expired_buckets_and_walkers_skip_them() { } } +/// A ranked windowed index whose current window holds no documents +/// yet — every new day of a `range == step` grid until its first like — +/// ranks EMPTY, with a proof, rather than failing. The window's bucket +/// tree is created by the first write under it, so the pinned path the +/// resolved `IN_TIME_RANGE` equality descends into does not exist; +/// grovedb answers the single-path axis read over that absent path with +/// an empty page whose absence the proof's own layers authenticate +/// (grovedb #965). Before that the prover refused ("a single-path axis +/// read must produce exactly one axis descent"), which reached clients +/// as an internal error on every fresh window. Once a document lands in +/// the window the same query serves its leaderboard. +#[test] +fn an_unwritten_window_ranks_empty_and_proves_it() { + use crate::query::drive_document_ranked_query::index_picker::resolve_ranked_query_for_mode; + use crate::query::drive_document_ranked_query::PrefixPin; + use crate::query::{DocumentRankedMode, RankedAxis, RankedEntry, RankedEntryValue}; + + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + + // Tumbling 2h windows (range == step), ranked hashtags per window. + let factory = + DataContractFactory::new(PlatformVersion::latest().protocol_version).expect("factory"); + let index_map = vec![ + ( + Value::Text("name".to_string()), + Value::Text("trendingDaily".to_string()), + ), + ( + Value::Text("properties".to_string()), + Value::Array(vec![ + platform_value!({"$createdAt": "asc"}), + platform_value!({"hashtag": "asc"}), + ]), + ), + ( + Value::Text("timeRange".to_string()), + Value::Map(vec![ + ( + Value::Text("on".to_string()), + Value::Text("$createdAt".to_string()), + ), + ( + Value::Text("range".to_string()), + Value::U64(2 * HOUR_SECONDS), + ), + ( + Value::Text("step".to_string()), + Value::U64(2 * HOUR_SECONDS), + ), + ]), + ), + ( + Value::Text("countable".to_string()), + Value::Text("countable".to_string()), + ), + (Value::Text("rangeCountable".to_string()), Value::Bool(true)), + ( + Value::Text("rankedCountable".to_string()), + Value::Bool(true), + ), + ]; + let document_schema = platform_value!({ + "type": "object", + "properties": { + "hashtag": {"type": "string", "maxLength": 61, "position": 0}, + }, + "required": ["hashtag", "$createdAt"], + "indices": Value::Array(vec![Value::Map(index_map)]), + "additionalProperties": false, + }); + let schemas = platform_value!({ "post": document_schema }); + let contract = factory + .create_with_value_config(Identifier::from([205u8; 32]), 0, schemas, None, None) + .expect("a ranked windowed index registers") + .data_contract_owned(); + drive + .apply_contract( + &contract, + BlockInfo::default(), + true, + StorageFlags::optional_default_as_cow(), + None, + platform_version, + ) + .expect("apply contract"); + let document_type = contract.document_type_for_name("post").expect("post"); + + let insert_at = |created_at: u64, tag: &str| { + let owner_bytes = fixture_bytes(7, created_at, tag); + let document = Document::V0(DocumentV0 { + id: Identifier::from(fixture_bytes(8, created_at, tag)), + owner_id: Identifier::from(owner_bytes), + properties: BTreeMap::from([("hashtag".to_string(), Value::Text(tag.to_string()))]), + created_at: Some(created_at), + revision: Some(1), + ..Default::default() + }); + drive + .add_document_for_contract( + DocumentAndContractInfo { + owned_document_info: OwnedDocumentInfo { + document_info: DocumentRefInfo(( + &document, + StorageFlags::optional_default_as_cow(), + )), + owner_id: Some(owner_bytes), + }, + contract: &contract, + document_type, + }, + false, + BlockInfo { + time_ms: created_at, + ..Default::default() + }, + true, + None, + platform_version, + None, + ) + .expect("add document"); + }; + + let h = HOUR_MS; + let t0 = 1_000 * h; + // One post in window t0; window t0+2h — the "current" one a + // `newest` selection resolves to at t0+2h..t0+4h — never written. + insert_at(t0 + 10 * MINUTE_MS_TTL, "alpha"); + + let window_query = |window_start: u64, k: u16| { + let mode = DocumentRankedMode { + axis: RankedAxis::Count, + descending: true, + k, + offset: 0, + group_by_property: "hashtag".to_string(), + aggregate_field: String::new(), + prefix_pins: vec![PrefixPin { + field: "$createdAt".to_string(), + values: vec![Value::U64(window_start)], + }], + }; + resolve_ranked_query_for_mode( + contract.id().to_buffer(), + document_type, + "post".to_string(), + document_type.indexes(), + &mode, + &created_at_resolution(document_type), + platform_version, + ) + .expect("the ranked windowed index covers the pinned request") + }; + let root_hash = || { + drive + .grove + .root_hash(None, &platform_version.drive.grove_version) + .unwrap() + .expect("root hash must be readable") + }; + + let unwritten = window_query(t0 + 2 * h, 10); + let page = unwritten + .execute_top_k_no_proof(&drive, None, platform_version) + .expect("an unwritten window reads as an empty leaderboard"); + assert_eq!(page.skipped, 0); + assert!(page.entries.is_empty()); + let proof = unwritten + .execute_top_k_with_proof(&drive, None, platform_version) + .expect("an unwritten window proves as an empty leaderboard"); + let (proved_root, verified) = unwritten + .verify_ranked_top_k_proof(&proof, platform_version) + .expect("the envelope authenticates the absent window"); + assert_eq!(verified, page); + assert_eq!(proved_root, root_hash()); + + // The same read works under a caller transaction. + let transaction = drive.grove.start_transaction(); + let page_in_tx = unwritten + .execute_top_k_no_proof(&drive, Some(&transaction), platform_version) + .expect("an unwritten window reads under a transaction"); + assert!(page_in_tx.entries.is_empty()); + drop(transaction); + + // Once a document lands in the window, the same query shape serves + // its leaderboard — and the written window t0 still ranks its post. + insert_at(t0 + 2 * h + 5 * MINUTE_MS_TTL, "bravo"); + let written = window_query(t0 + 2 * h, 10); + let expected = vec![RankedEntry { + in_key: None, + key: b"bravo".to_vec(), + value: RankedEntryValue::Count(1), + }]; + let page = written + .execute_top_k_no_proof(&drive, None, platform_version) + .expect("the now-written window reads"); + assert_eq!(page.entries, expected); + let proof = written + .execute_top_k_with_proof(&drive, None, platform_version) + .expect("the now-written window proves"); + let (proved_root, verified) = written + .verify_ranked_top_k_proof(&proof, platform_version) + .expect("the envelope verifies the written window"); + assert_eq!(verified.entries, expected); + assert_eq!(proved_root, root_hash()); + let older = window_query(t0, 10) + .execute_top_k_no_proof(&drive, None, platform_version) + .expect("the first window still reads"); + assert_eq!(older.entries[0].key, b"alpha".to_vec()); +} + /// One minute in milliseconds, for the TTL lifecycle test's offsets. const MINUTE_MS_TTL: u64 = 60_000; diff --git a/packages/rs-drive/src/query/drive_document_having_query/execute_range.rs b/packages/rs-drive/src/query/drive_document_having_query/execute_range.rs index 12e732da890..21804b6cf81 100644 --- a/packages/rs-drive/src/query/drive_document_having_query/execute_range.rs +++ b/packages/rs-drive/src/query/drive_document_having_query/execute_range.rs @@ -34,18 +34,21 @@ impl DriveDocumentHavingQuery<'_> { /// not an error; exactly `limit` entries may mean the match set was /// cut. /// - /// Missing paths follow the ranked surface's rule. Under a single - /// `==` pin (or no pins) a missing path *is* an error rather than an - /// empty result: the indexed property-name tree is created at - /// contract registration, so its absence means the contract-level - /// state is not what the request claims. On an `IN`-pinned request, - /// an element whose branch chain is missing at ANY depth — the - /// branch key, or any deeper pinned segment under a *present* key — - /// contributes an **empty branch** instead (union semantics, exactly - /// as the proved envelope authenticates it), and the union is served - /// from one committed state (a `None` read runs under a grovedb - /// snapshot read transaction). An index with no documents has the - /// tree, with an empty secondary, and yields an empty entry list. + /// Missing paths follow the ranked surface's rule. A pinned prefix + /// no document has written yet (a `timeRange` bucket before its + /// first document, a `hashtag` nobody has used) is an empty match + /// set, not an error: grovedb answers a single-path axis read over + /// a path that does not exist with the traversal's empty result on + /// the read and the proof alike. On an `IN`-pinned request, an + /// element whose branch chain is missing at ANY depth — the branch + /// key, or any deeper pinned segment under a *present* key — + /// contributes an **empty branch** (union semantics, exactly as the + /// proved envelope authenticates it), and the union is served from + /// one committed state (a `None` read runs under a grovedb snapshot + /// read transaction). What stays an error is a path that exists but + /// does not lead to an indexed tree carrying the axis. An index with + /// no documents has the tree, with an empty secondary, and yields an + /// empty entry list. pub fn execute_range_no_proof( &self, drive: &Drive, diff --git a/packages/rs-drive/src/query/drive_document_having_query/tests.rs b/packages/rs-drive/src/query/drive_document_having_query/tests.rs index f01da90d657..8b70c0bdad3 100644 --- a/packages/rs-drive/src/query/drive_document_having_query/tests.rs +++ b/packages/rs-drive/src/query/drive_document_having_query/tests.rs @@ -1992,24 +1992,46 @@ mod pinned_prefix { } /// A pin on an identity that never inserted a document addresses a - /// prefix value tree that does not exist. The read and the prover - /// both surface an error rather than fabricating an empty page — - /// same contract as the empty-secondary limitation on the - /// single-property surface (the abci layer maps these to a - /// client-visible rejection). + /// prefix value tree that does not exist — the state every + /// `timeRange` window is in before its first document lands. That + /// is an EMPTY match set, not corrupted state: grovedb answers a + /// single-path axis read over an absent path with the empty result + /// on the read and the proof alike, the absence authenticated by the + /// layers the walk emits rather than fabricated — the verifier + /// reconstructs the live root hash from it. Sibling of the ranked + /// suite's `an_absent_equality_pin_reads_empty_and_proves_empty`. #[test] - fn unknown_prefix_value_errors_rather_than_fabricating_an_empty_page() { + fn unknown_prefix_value_reads_empty_and_proves_empty() { let (drive, contract) = setup_grades_compound_ranked(); insert_two_identities(&drive, &contract); let unknown = pin([9u8; 32]); - assert!( - run(&drive, &contract, &unknown, &[], false).is_err(), - "reading a never-written prefix value tree must error" + let entries = entries_of( + run(&drive, &contract, &unknown, &[], false) + .expect("a never-written prefix reads as an empty match set"), ); assert!( - run(&drive, &contract, &unknown, &[], true).is_err(), - "proving a never-written prefix value tree must error" + entries.is_empty(), + "nothing matches under an unwritten prefix" + ); + + let proof = match run(&drive, &contract, &unknown, &[], true) + .expect("a never-written prefix proves as an empty match set") + { + DocumentHavingResponse::Proof(proof) => proof, + DocumentHavingResponse::Entries(_) => panic!("expected a proof, got entries"), + }; + let (root_hash, verified) = client_side_query(&contract, &unknown, &[]) + .verify_having_range_proof(&proof, platform_version()) + .expect("the envelope authenticates the absent prefix"); + assert!(verified.is_empty()); + assert_eq!( + root_hash, + drive + .grove + .root_hash(None, &platform_version().drive.grove_version) + .unwrap() + .expect("root hash must be readable"), ); } diff --git a/packages/rs-drive/src/query/drive_document_ranked_query/execute_top_k.rs b/packages/rs-drive/src/query/drive_document_ranked_query/execute_top_k.rs index b049038174f..6fa1a28c121 100644 --- a/packages/rs-drive/src/query/drive_document_ranked_query/execute_top_k.rs +++ b/packages/rs-drive/src/query/drive_document_ranked_query/execute_top_k.rs @@ -28,22 +28,27 @@ impl DriveDocumentRankedQuery<'_> { /// the direction and the tie contract. /// /// Fewer than `k` entries is normal (the index simply has fewer - /// groups than `offset + k`) and is not an error. On an `IN`-pinned - /// request, an element whose branch chain is missing at ANY depth — - /// the branch key itself, or any deeper pinned segment under a - /// *present* key — contributes an **empty branch** (union + /// groups than `offset + k`) and is not an error. So is a pinned + /// prefix no document has written yet — this window's `timeRange` + /// bucket before its first like, a `hashtag` nobody has used, a + /// TTL-dropped bucket: a pinned value tree is created by the first + /// write under it (contract registration creates only the level + /// trees), and grovedb answers a single-path axis read over a path + /// that does not exist with the traversal's empty page — no + /// entries, `skipped` 0 — on the read and the proof alike, the + /// absence authenticated by the layers the walk did emit. On an + /// `IN`-pinned request, an element whose branch chain is missing at + /// ANY depth — the branch key itself, or any deeper pinned segment + /// under a *present* key — contributes an **empty branch** (union /// semantics), exactly as the proved envelope authenticates it, and /// the union is served from **one committed state**: the branched /// read always runs under a grovedb snapshot read transaction, so /// every per-branch probe and walk reads the same RocksDB snapshot /// (a caller transaction is rejected on this shape, mirroring the /// branched prover — read per prefix element under a transaction). - /// A missing path under a single `==` pin *is* an error rather than - /// an empty result: the indexed property-name tree is created when - /// the contract is registered, so its absence means the - /// contract-level state is not what the request claims, not that - /// the ranking is empty. (An index with no documents yet has the - /// tree, with an empty secondary, and yields an empty entry list.) + /// What stays an error is a path that exists but does not lead to + /// an indexed tree carrying the axis: contract-level state that is + /// not what the request claims. /// /// The paginated grovedb primitive is used unconditionally, with /// `offset = 0` standing in for an unpaginated request, so the @@ -219,7 +224,11 @@ impl DriveDocumentRankedQuery<'_> { /// unqueryable with `prove = true`; the paginated prover emits a /// guaranteed-empty range against the secondary instead, so the /// proved and unproven paths agree on empty state. Pinned by the - /// `ranking_an_empty_index_reads_empty_and_proves_empty` test. + /// `ranking_an_empty_index_reads_empty_and_proves_empty` test. A + /// pinned prefix whose value tree does not exist yet proves empty + /// too: grovedb authenticates the absent path and the verifier + /// reads it as an empty page (pinned by + /// `an_absent_equality_pin_reads_empty_and_proves_empty`). pub fn execute_top_k_with_proof( &self, drive: &Drive, diff --git a/packages/rs-drive/src/query/drive_document_ranked_query/tests.rs b/packages/rs-drive/src/query/drive_document_ranked_query/tests.rs index eb33c5ee22f..9c58eb5eada 100644 --- a/packages/rs-drive/src/query/drive_document_ranked_query/tests.rs +++ b/packages/rs-drive/src/query/drive_document_ranked_query/tests.rs @@ -2989,6 +2989,131 @@ mod pinned_prefix { ); } + /// A `==` pin on an identity that never inserted a document + /// addresses a prefix value tree that does not exist — the same + /// state a `timeRange` window is in before its first document lands + /// (a bucket is created by the first write under it, not at + /// contract registration). That is an EMPTY ranking, not corrupted + /// state: grovedb answers a single-path axis read over an absent + /// path with the empty page on the read and the proof alike, the + /// absence authenticated by the layers the walk emits — the + /// verifier reconstructs the live root hash from an empty page. + /// Before grovedb #965 both paths failed with "a single-path axis + /// read must produce exactly one axis descent", which reached + /// clients as an internal error on every fresh window. + #[test] + fn an_absent_equality_pin_reads_empty_and_proves_empty() { + let (drive, contract) = setup_grades_compound_ranked(); + insert_grades(&drive, &contract, &[(IDENTITY_X, "art", 90)]); + + let never_written = pin([9u8; 32]); + let page = match run(&drive, &contract, &never_written, 2, false) + .expect("a never-written prefix reads as an empty ranking") + { + DocumentRankedResponse::Entries(page) => page, + DocumentRankedResponse::Proof(_) => panic!("expected entries, got a proof"), + }; + assert_eq!(page.skipped, 0); + assert!( + page.entries.is_empty(), + "nothing is ranked under an unwritten prefix" + ); + + let proof = match run(&drive, &contract, &never_written, 2, true) + .expect("a never-written prefix proves as an empty ranking") + { + DocumentRankedResponse::Proof(proof) => proof, + DocumentRankedResponse::Entries(_) => panic!("expected a proof, got entries"), + }; + let (root_hash, verified) = client_side_query(&contract, &never_written, 2) + .verify_ranked_top_k_proof(&proof, platform_version()) + .expect("the envelope authenticates the absent prefix"); + assert_eq!(verified, page); + assert_eq!( + root_hash, + drive + .grove + .root_hash(None, &platform_version().drive.grove_version) + .unwrap() + .expect("root hash must be readable"), + ); + + // The written prefix still reads with its entry and no `in_key`: + // absence semantics change nothing about what a present prefix + // returns. + let present = match run(&drive, &contract, &pin(IDENTITY_X), 2, false) + .expect("a written prefix reads") + { + DocumentRankedResponse::Entries(page) => page, + DocumentRankedResponse::Proof(_) => panic!("expected entries, got a proof"), + }; + assert_eq!( + present.entries, + vec![RankedEntry { + in_key: None, + key: b"art".to_vec(), + value: RankedEntryValue::AvgFixedPoint(compute_avg_fixed_point(90, 1)), + }] + ); + + // A page past rank 0 over the unwritten prefix is the same empty + // answer: the population is zero, so nothing could be skipped + // and the attested skip is 0, not the requested offset. + let mode = detect_ranked_mode( + &SelectProjection::avg("grade"), + &[CLASS_PROPERTY.to_string()], + &[], + &[OrderClause { + field: "grade".to_string(), + ascending: false, + }], + &never_written, + RankedPaginationInputs { + limit: Some(2), + offset: Some(1), + has_start_at: false, + }, + platform_version(), + ) + .expect("an offset with a `==` pin is grammatical"); + let document_type = contract + .document_type_for_name(DOCUMENT_TYPE) + .expect("grade doctype exists"); + let paged = resolve_ranked_query_for_mode( + contract.id_ref().to_buffer(), + document_type, + DOCUMENT_TYPE.to_string(), + document_type.indexes(), + &mode, + &[], + platform_version(), + ) + .expect("the compound index covers the pinned request"); + let offset_page = paged + .execute_top_k_no_proof(&drive, None, platform_version()) + .expect("an offset page of an unwritten prefix reads as an empty ranking"); + assert_eq!( + offset_page.skipped, 0, + "nothing to skip in an empty population" + ); + assert!(offset_page.entries.is_empty()); + let proof = paged + .execute_top_k_with_proof(&drive, None, platform_version()) + .expect("an offset page of an unwritten prefix proves as an empty ranking"); + let (root_hash, verified) = paged + .verify_ranked_top_k_proof(&proof, platform_version()) + .expect("the absent-path proof verifies the offset page"); + assert_eq!(verified, offset_page); + assert_eq!( + root_hash, + drive + .grove + .root_hash(None, &platform_version().drive.grove_version) + .unwrap() + .expect("root hash must be readable"), + ); + } + /// An unpinned request over the compound-only contract has no /// covering index — there is no global cross-prefix ordering to /// serve, so the rejection names the missing coverage. diff --git a/packages/rs-platform-version/Cargo.toml b/packages/rs-platform-version/Cargo.toml index 94951daa007..6f04b0a77c4 100644 --- a/packages/rs-platform-version/Cargo.toml +++ b/packages/rs-platform-version/Cargo.toml @@ -11,7 +11,7 @@ license = "MIT" thiserror = { version = "2.0.12" } bincode = { workspace = true } versioned-feature-core = "1.0.0" -grovedb-version = { git = "https://github.com/dashpay/grovedb", rev = "6816457d2b2df98653f286bf9a374f236a17748f" } +grovedb-version = { workspace = true } [features] mock-versions = [] diff --git a/packages/rs-platform-wallet/Cargo.toml b/packages/rs-platform-wallet/Cargo.toml index f4e5d000787..d3da739bcba 100644 --- a/packages/rs-platform-wallet/Cargo.toml +++ b/packages/rs-platform-wallet/Cargo.toml @@ -61,7 +61,7 @@ zeroize = "1" log = "0.4" # Shielded pool (optional, behind `shielded` feature) -grovedb-commitment-tree = { git = "https://github.com/dashpay/grovedb", rev = "6816457d2b2df98653f286bf9a374f236a17748f", optional = true } +grovedb-commitment-tree = { workspace = true, optional = true } # Direct `rusqlite` access so `FileBackedShieldedStore::open_path` can set # WAL + synchronous=NORMAL pragmas before handing the connection to # `ClientPersistentCommitmentTree`. Version locked to match the rev grovedb diff --git a/packages/rs-sdk/Cargo.toml b/packages/rs-sdk/Cargo.toml index a307441b67a..30e6a1bae7b 100644 --- a/packages/rs-sdk/Cargo.toml +++ b/packages/rs-sdk/Cargo.toml @@ -18,7 +18,7 @@ drive = { path = "../rs-drive", default-features = false, features = [ ] } drive-proof-verifier = { path = "../rs-drive-proof-verifier", default-features = false } -grovedb-commitment-tree = { git = "https://github.com/dashpay/grovedb", rev = "6816457d2b2df98653f286bf9a374f236a17748f", features = [ +grovedb-commitment-tree = { workspace = true, features = [ "client", "sqlite", ], optional = true }