Repository navigation
fix(drive): look up a single document at a block time (PV15) #5838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d0f7c07
fix(drive): verify a single document that may be contested (PV15)
QuantumExplorer 5b1033f
test(drive): pin the maybe contested query of protocol version 14
QuantumExplorer 6b8940f
docs(drive): say which lookups at a block time do not verify
QuantumExplorer 620918a
docs(drive): say when a maybe contested lookup refuses a block time
QuantumExplorer 8b8f5ef
fix(drive): look up a single document at a block time (PV15)
QuantumExplorer d52e3a4
Merge remote-tracking branch 'origin/claude/maybe-contested-single-do…
QuantumExplorer 938326c
docs(drive): say what the looping maybe contested tests show
QuantumExplorer b881f60
Merge remote-tracking branch 'origin/claude/maybe-contested-single-do…
QuantumExplorer 5948001
docs(drive): say what the looping block time tests show
QuantumExplorer 4c578c4
Merge remote-tracking branch 'origin/v5.1-dev' into claude/single-doc…
QuantumExplorer 14df42a
fix(drive): refuse a document with history that may be contested at a…
QuantumExplorer cfc146a
Merge remote-tracking branch 'origin/v5.1-dev' into claude/single-doc…
QuantumExplorer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
301 changes: 192 additions & 109 deletions
301
packages/rs-drive/src/query/single_document_drive_query.rs
Large diffs are not rendered by default.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
packages/rs-drive/src/query/single_document_drive_query/v0/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| use crate::drive::document::paths::contract_document_type_path_vec; | ||
| use crate::drive::votes; | ||
| use crate::error::Error; | ||
| use crate::query::{Query, SingleDocumentDriveQuery, SingleDocumentDriveQueryContestedStatus}; | ||
| use crate::util::common::encode::encode_u64; | ||
| use grovedb::{PathQuery, SizedQuery}; | ||
| use platform_version::version::PlatformVersion; | ||
|
|
||
| impl SingleDocumentDriveQuery { | ||
| /// The query protocol versions 1 to 14 build. | ||
| /// | ||
| /// In a document type that keeps history, a lookup at a block time ranges over every key | ||
| /// up to that time. That reaches the pointer to the latest version at key `[0]`, which | ||
| /// sorts below every time, so a time before the document was first stored answers with the | ||
| /// latest version. No proof of such a lookup verifies either: generation 0 of the verifier | ||
| /// asks GroveDB to name the absent keys, which a range that is open at one end does not | ||
| /// allow. | ||
| #[inline(always)] | ||
| pub(super) fn construct_path_query_v0( | ||
| &self, | ||
| platform_version: &PlatformVersion, | ||
| ) -> Result<PathQuery, Error> { | ||
| match self.contested_status { | ||
| SingleDocumentDriveQueryContestedStatus::NotContested => { | ||
| Ok(self.construct_non_contested_path_query(true)) | ||
| } | ||
| SingleDocumentDriveQueryContestedStatus::MaybeContested => { | ||
| let non_contested = self.construct_non_contested_path_query(true); | ||
| let contested = self.construct_contested_path_query(true); | ||
| PathQuery::merge( | ||
| vec![&non_contested, &contested], | ||
| &platform_version.drive.grove_version, | ||
| ) | ||
| .map_err(Error::from) | ||
| } | ||
| SingleDocumentDriveQueryContestedStatus::Contested => { | ||
| Ok(self.construct_contested_path_query(true)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Operations to construct the normal path query. | ||
| pub(super) fn construct_non_contested_path_query(&self, with_limit_1: bool) -> PathQuery { | ||
| // First we should get the overall document_type_path | ||
| let mut path = | ||
| contract_document_type_path_vec(&self.contract_id, self.document_type_name.as_str()); | ||
|
|
||
| path.push(vec![0]); | ||
|
|
||
| let mut query = Query::new(); | ||
| query.insert_key(self.document_id.to_vec()); | ||
|
|
||
| if self.document_type_keeps_history { | ||
| // if the documents keep history then we should insert a subquery | ||
| if let Some(block_time) = self.block_time_ms { | ||
| let encoded_block_time = encode_u64(block_time); | ||
| let mut sub_query = Query::new_with_direction(false); | ||
| sub_query.insert_range_to_inclusive(..=encoded_block_time); | ||
| query.set_subquery(sub_query); | ||
| } else { | ||
| query.set_subquery_key(vec![0]); | ||
| } | ||
| } | ||
|
|
||
| let limit = if with_limit_1 { Some(1) } else { None }; | ||
|
|
||
| PathQuery::new(path, SizedQuery::new(query, limit, None)) | ||
| } | ||
|
|
||
| /// Operations to construct the contested path query. | ||
| pub(super) fn construct_contested_path_query(&self, with_limit_1: bool) -> PathQuery { | ||
| // First we should get the overall document_type_path | ||
| let path = votes::paths::vote_contested_resource_contract_documents_storage_path_vec( | ||
| &self.contract_id, | ||
| self.document_type_name.as_str(), | ||
| ); | ||
|
|
||
| let mut query = Query::new(); | ||
| query.insert_key(self.document_id.to_vec()); | ||
|
|
||
| let limit = if with_limit_1 { Some(1) } else { None }; | ||
|
|
||
| PathQuery::new(path, SizedQuery::new(query, limit, None)) | ||
| } | ||
| } |
85 changes: 85 additions & 0 deletions
85
packages/rs-drive/src/query/single_document_drive_query/v1/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| use crate::error::query::QuerySyntaxError; | ||
| use crate::error::Error; | ||
| use crate::query::{Query, SingleDocumentDriveQuery, SingleDocumentDriveQueryContestedStatus}; | ||
| use crate::util::common::encode::encode_u64; | ||
| use grovedb::PathQuery; | ||
| use platform_version::version::PlatformVersion; | ||
|
|
||
| /// The last time a document's history can order. A time is stored with its top bit flipped, so | ||
| /// a time past this one would sort below every time before it. | ||
| const LAST_ORDERED_TIME_MS: u64 = i64::MAX as u64; | ||
|
|
||
| impl SingleDocumentDriveQuery { | ||
| /// Generation 0, except for two lookups in a document type that keeps history. | ||
| /// | ||
| /// A lookup at a block time stops above the pointer to the latest version, so it answers | ||
| /// with the newest version stored at or before that time, and with nothing for a time | ||
| /// before the document was first stored. | ||
| /// | ||
| /// The lookup of a document that may be contested states its cap itself. Its history lookup | ||
| /// has a subquery, above which GroveDB's merge refuses an overall limit from its generation | ||
| /// 2 (`GROVE_V5`, which protocol version 15 is the first to select), where the generation | ||
| /// before turned that limit into the cap of the merged branch. Written as that cap here, it | ||
| /// merges to the query generation 0 built. | ||
| /// | ||
| /// Such a lookup at a block time is refused. A contest's area keeps no history: it holds | ||
| /// each contender as it was submitted, with no time to compare, so a document submitted | ||
| /// after the block time would be found there. | ||
| #[inline(always)] | ||
| pub(super) fn construct_path_query_v1( | ||
| &self, | ||
| platform_version: &PlatformVersion, | ||
| ) -> Result<PathQuery, Error> { | ||
| match self.contested_status { | ||
| SingleDocumentDriveQueryContestedStatus::NotContested => { | ||
| Ok(self.construct_non_contested_path_query_v1()) | ||
| } | ||
| SingleDocumentDriveQueryContestedStatus::MaybeContested => { | ||
| if self.document_type_keeps_history && self.block_time_ms.is_some() { | ||
| return Err(Error::Query(QuerySyntaxError::Unsupported( | ||
| "a document with history that may be contested can not be looked up at \ | ||
| a block time" | ||
| .to_string(), | ||
| ))); | ||
| } | ||
| let mut non_contested = self.construct_non_contested_path_query_v1(); | ||
| let contested = self.construct_contested_path_query(true); | ||
| if self.document_type_keeps_history { | ||
| non_contested.query.query.limit = non_contested.query.limit.take(); | ||
| } | ||
| PathQuery::merge( | ||
| vec![&non_contested, &contested], | ||
| &platform_version.drive.grove_version, | ||
| ) | ||
| .map_err(Error::from) | ||
| } | ||
| SingleDocumentDriveQueryContestedStatus::Contested => { | ||
| Ok(self.construct_contested_path_query(true)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Generation 0's query of the document among the documents of its type, with the range of | ||
| /// a lookup at a block time replaced. | ||
| fn construct_non_contested_path_query_v1(&self) -> PathQuery { | ||
| let mut path_query = self.construct_non_contested_path_query(true); | ||
| if self.document_type_keeps_history { | ||
| if let Some(block_time_ms) = self.block_time_ms { | ||
| // A document's history holds one key per time a version was stored, and the | ||
| // pointer to the latest version at key `[0]`, below all of them. Walking down | ||
| // from the block time and stopping above the pointer, the first key met is the | ||
| // newest version stored at or before that time, and there is none when the | ||
| // document was first stored later. | ||
| let mut versions_up_to_block_time = Query::new_with_direction(false); | ||
| versions_up_to_block_time.insert_range_after_to_inclusive( | ||
| vec![0]..=encode_u64(block_time_ms.min(LAST_ORDERED_TIME_MS)), | ||
| ); | ||
| path_query | ||
| .query | ||
| .query | ||
| .set_subquery(versions_up_to_block_time); | ||
| } | ||
| } | ||
| path_query | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.