From 050751f86e43b1fce77dc579b0fcdd1796b0e853 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Fri, 4 Oct 2024 20:23:36 +0200 Subject: [PATCH] chore: apply rabbit review --- packages/rs-drive-proof-verifier/src/types.rs | 40 ++++++++++++------- packages/rs-sdk/src/mock/requests.rs | 6 +-- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/packages/rs-drive-proof-verifier/src/types.rs b/packages/rs-drive-proof-verifier/src/types.rs index 1aebc864452..89b729851ec 100644 --- a/packages/rs-drive-proof-verifier/src/types.rs +++ b/packages/rs-drive-proof-verifier/src/types.rs @@ -51,9 +51,15 @@ pub use evonode_status::*; /// from a server using [`FetchMany`](dash_sdk::platform::FetchMany) or parsing a proof that contains multiple objects /// using [`FromProof`](crate::FromProof). /// -/// Each key in the `RetrievedObjects` corresponds to an object of generic type `O`. -/// If an object is found for a given key, the value is `Some(object)`. -/// If no object is found for a given key, the value is `None`. +/// Each key `K` in the `RetrievedObjects` corresponds to zero or one object of generic type `O`: +/// * if an object is found for a given key, the value is `Some(object)`, +/// * if no object is found for a given key, the value is `None`; this can be interpreted as a proof of absence. +/// +/// This data structure preserves order of objects insertion. However, actual order of objects depends on the order of +/// objects returned by Dash Drive, which is not always guaranteed to be correct. +/// You can sort the objects by key if you need a specific order; see [`IndexMap::sort_keys`] and similar methods. +/// +/// `RetrievedObjects` is a wrapper around the [`IndexMap`] type. /// /// # Generic Type Parameters /// @@ -61,26 +67,32 @@ pub use evonode_status::*; /// * `O`: The type of the objects in the map. pub type RetrievedObjects = IndexMap>; -/// A data structure that holds a set of objects of a generic type `O`, indexed by a key of type `K`. +/// A data structure that holds a set of values of a generic type `I`, indexed by a key of type `K`. /// /// This type is typically returned by functions that operate on multiple objects, such as fetching multiple objects /// from a server using [`FetchMany`](dash_sdk::platform::FetchMany) or parsing a proof that contains multiple objects /// using [`FromProof`](crate::FromProof). /// -/// Each key in the `RetrievedObjects` corresponds to an object of generic type `O`. -/// If a value is found for a given key, the value is `value`. -/// If no value is found for a given key, the value is `0`. +/// Each key in this data structure corresponds to an existing value of generic type `I`. It differs from +/// [`RetrievedObjects`] in that it does not contain `Option`, but only `I`, so it cannot be interpreted as a +/// proof of absence. +/// +/// This data structure preserves the order of object insertion. However, the actual order of objects depends on the +/// order of objects returned by Dash Drive, which is not always guaranteed to be correct. +/// You can sort the objects by key if you need a specific order; see [`IndexMap::sort_keys`] and similar methods. +/// +/// The ordering of the objects is guaranteed to be the same as the order of objects returned by Dash Drive. /// /// # Generic Type Parameters /// /// * `K`: The type of the keys in the map. -/// * `I`: The type of the integer in the map. -pub type RetrievedIntegerValue = IndexMap; +/// * `I`: The type of the integer values in the map. +pub type RetrievedValues = IndexMap; /// History of a data contract. /// /// Contains a map of data contract revisions to data contracts. -pub type DataContractHistory = IndexMap; +pub type DataContractHistory = RetrievedValues; /// Multiple data contracts. /// /// Mapping between data contract IDs and data contracts. @@ -114,14 +126,14 @@ impl Contenders { &self, document_type: &DocumentType, platform_version: &PlatformVersion, - ) -> Result, crate::Error> { + ) -> Result, crate::Error> { self.contenders .iter() .map(|(id, v)| { let contender = v.try_to_contender(document_type.as_ref(), platform_version)?; Ok((*id, contender)) }) - .collect::, dpp::ProtocolError>>() + .collect::, dpp::ProtocolError>>() .map_err(Into::into) } } @@ -450,7 +462,7 @@ impl FromIterator<(u64, Vec)> for VotePollsGroupedByTimestamp { // collect all vote polls for the same timestamp into a single vector let data = iter .into_iter() - .fold(IndexMap::new(), |mut acc, (timestamp, vote_poll)| { + .fold(BTreeMap::new(), |mut acc, (timestamp, vote_poll)| { let entry: &mut Vec = acc.entry(timestamp).or_default(); entry.extend(vote_poll); acc @@ -572,7 +584,7 @@ pub type MasternodeProtocolVotes = RetrievedObjects); +pub struct ProposerBlockCounts(pub RetrievedValues); impl FromIterator<(ProTxHash, Option)> for ProposerBlockCounts { fn from_iter)>>( diff --git a/packages/rs-sdk/src/mock/requests.rs b/packages/rs-sdk/src/mock/requests.rs index 605e5e96217..582c3628a05 100644 --- a/packages/rs-sdk/src/mock/requests.rs +++ b/packages/rs-sdk/src/mock/requests.rs @@ -19,8 +19,8 @@ use drive::grovedb::Element; use drive_proof_verifier::types::{ Contenders, ContestedResources, CurrentQuorumsInfo, ElementFetchRequestItem, EvoNodeStatus, IdentityBalanceAndRevision, IndexMap, MasternodeProtocolVote, PrefundedSpecializedBalance, - ProposerBlockCounts, RetrievedIntegerValue, TotalCreditsInPlatform, - VotePollsGroupedByTimestamp, Voters, + ProposerBlockCounts, RetrievedValues, TotalCreditsInPlatform, VotePollsGroupedByTimestamp, + Voters, }; use std::{collections::BTreeMap, hash::Hash}; @@ -268,7 +268,7 @@ impl MockResponse for ProposerBlockCounts { where Self: Sized, { - let data = RetrievedIntegerValue::::mock_deserialize(sdk, buf); + let data = RetrievedValues::::mock_deserialize(sdk, buf); ProposerBlockCounts(data) } }