Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
301 changes: 192 additions & 109 deletions packages/rs-drive/src/query/single_document_drive_query.rs

Large diffs are not rendered by default.

85 changes: 85 additions & 0 deletions packages/rs-drive/src/query/single_document_drive_query/v0/mod.rs
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 packages/rs-drive/src/query/single_document_drive_query/v1/mod.rs
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],
Comment thread
QuantumExplorer marked this conversation as resolved.
Resolved
&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
}
}
Loading
Loading