Skip to content

Commit

Permalink
refactor(sdk): contested resource as struct type (#2225)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukasz Klimek <[email protected]>
  • Loading branch information
QuantumExplorer and lklimek authored Oct 9, 2024
1 parent de5236e commit a646669
Show file tree
Hide file tree
Showing 36 changed files with 38 additions and 33 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions packages/rs-dapi-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[features]
default = ["mocks", "offline-testing"]
tokio-sleep = ["backon/tokio-sleep"]
mocks = [
"dep:sha2",
"dep:hex",
Expand All @@ -19,9 +20,7 @@ dump = ["mocks"]
offline-testing = []

[dependencies]
backon = { version = "1.2", default-features = false, features = [
"tokio-sleep",
] }
backon = { version = "1.2"}
dapi-grpc = { path = "../dapi-grpc", features = [
"core",
"platform",
Expand Down
2 changes: 1 addition & 1 deletion packages/rs-drive-proof-verifier/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ impl FromProof<platform::GetContestedResourcesRequest> for ContestedResources {
verify_tenderdash_proof(proof, mtd, &root_hash, provider)?;

let resources: ContestedResources =
items.into_iter().map(ContestedResource::Value).collect();
items.into_iter().map(|v| ContestedResource(v)).collect();

Ok((resources.into_option(), mtd.clone(), proof.clone()))
}
Expand Down
23 changes: 7 additions & 16 deletions packages/rs-drive-proof-verifier/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,8 @@ pub struct ElementFetchRequestItem(pub Element);
pub type IdentityBalanceAndRevision = (u64, Revision);

/// Contested resource values.
#[derive(Debug, derive_more::From, Clone, PartialEq)]
pub enum ContestedResource {
/// Generic [Value]
Value(Value),
}
#[derive(Debug, Clone, PartialEq)]
pub struct ContestedResource(pub Value);

impl ContestedResource {
/// Get the value.
Expand All @@ -244,13 +241,9 @@ impl ContestedResource {
}
}

impl TryInto<Value> for ContestedResource {
type Error = crate::Error;

fn try_into(self) -> Result<Value, Self::Error> {
match self {
ContestedResource::Value(value) => Ok(value),
}
impl Into<Value> for ContestedResource {
fn into(self) -> Value {
self.0
}
}

Expand All @@ -261,9 +254,7 @@ impl PlatformVersionEncode for ContestedResource {
encoder: &mut E,
_platform_version: &platform_version::PlatformVersion,
) -> Result<(), bincode::error::EncodeError> {
match self {
ContestedResource::Value(value) => value.encode(encoder),
}
self.0.encode(encoder)
}
}

Expand All @@ -273,7 +264,7 @@ impl PlatformVersionedDecode for ContestedResource {
decoder: &mut D,
_platform_version: &platform_version::PlatformVersion,
) -> Result<Self, bincode::error::DecodeError> {
Ok(ContestedResource::Value(Value::decode(decoder)?))
Ok(ContestedResource(Value::decode(decoder)?))
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/rs-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ test-case = { version = "3.3.1" }

[features]
default = ["mocks", "offline-testing"]
tokio-sleep = ["rs-dapi-client/tokio-sleep"]

mocks = [
"dep:serde",
Expand Down
1 change: 1 addition & 0 deletions packages/rs-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub use sdk::{RequestSettings, Sdk, SdkBuilder};
pub use dashcore_rpc;
pub use dpp;
pub use drive;
pub use drive_proof_verifier::types as query_types;
pub use rs_dapi_client as dapi_client;

/// Version of the SDK
Expand Down
12 changes: 6 additions & 6 deletions packages/rs-sdk/tests/fetch/contested_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async fn contested_resources_start_at_value() {
for inclusive in [true, false] {
// when I set start_at_value to some value,
for (i, start) in all.0.iter().enumerate() {
let ContestedResource::Value(start_value) = start.clone();
let ContestedResource(start_value) = start.clone();

let query = VotePollsByDocumentTypeQuery {
start_at_value: Some((start_value, inclusive)),
Expand Down Expand Up @@ -217,7 +217,7 @@ async fn contested_resources_limit_PLAN_656() {
);
}

let ContestedResource::Value(last) =
let ContestedResource(last) =
rss.0.into_iter().last().expect("last contested resource");
start_at_value = Some((last, false));

Expand All @@ -231,14 +231,14 @@ async fn contested_resources_limit_PLAN_656() {
/// ## Preconditions
///
/// None
#[test_case::test_case(|_q| {}, Ok("ContestedResources([Value(Text(".into()); "unmodified base query is Ok")]
#[test_case::test_case(|_q| {}, Ok("ContestedResources([ContestedResource(Text(".into()); "unmodified base query is Ok")]
#[test_case::test_case(|q| q.start_index_values = vec![Value::Text("".to_string())], Ok("".into()); "index value empty string is Ok")]
#[test_case::test_case(|q| q.document_type_name = "some random non-existing name".to_string(), Err(r#"code: InvalidArgument, message: "document type some random non-existing name not found"#); "non existing document type returns InvalidArgument")]
#[test_case::test_case(|q| q.index_name = "nx index".to_string(), Err(r#"code: InvalidArgument, message: "index with name nx index is not the contested index"#); "non existing index returns InvalidArgument")]
#[test_case::test_case(|q| q.index_name = "dashIdentityId".to_string(), Err(r#"code: InvalidArgument, message: "index with name dashIdentityId is not the contested index"#); "existing non-contested index returns InvalidArgument")]
// Disabled due to bug PLAN-653
// #[test_case::test_case(|q| q.start_at_value = Some((Value::Array(vec![]), true)), Err(r#"code: InvalidArgument"#); "start_at_value wrong index type returns InvalidArgument PLAN-653")]
#[test_case::test_case(|q| q.start_index_values = vec![], Ok(r#"ContestedResources([Value(Text("dash"))])"#.into()); "start_index_values empty vec returns top-level keys")]
#[test_case::test_case(|q| q.start_index_values = vec![], Ok(r#"ContestedResources([ContestedResource(Text("dash"))])"#.into()); "start_index_values empty vec returns top-level keys")]
#[test_case::test_case(|q| q.start_index_values = vec![Value::Text("".to_string())], Ok(r#"ContestedResources([])"#.into()); "start_index_values empty string returns zero results")]
#[test_case::test_case(|q| {
q.start_index_values = vec![
Expand All @@ -249,7 +249,7 @@ async fn contested_resources_limit_PLAN_656() {
#[test_case::test_case(|q| {
q.start_index_values = vec![];
q.end_index_values = vec![Value::Text(TEST_DPNS_NAME.to_string())];
}, Ok(r#"ContestedResources([Value(Text("dash"))])"#.into()); "end_index_values one value with empty start_index_values returns 'dash'")]
}, Ok(r#"ContestedResources([ContestedResource(Text("dash"))])"#.into()); "end_index_values one value with empty start_index_values returns 'dash'")]
#[test_case::test_case(|q| {
q.start_index_values = vec![];
q.end_index_values = vec![Value::Text(TEST_DPNS_NAME.to_string()), Value::Text("non existing".to_string())];
Expand Down Expand Up @@ -278,7 +278,7 @@ async fn contested_resources_limit_PLAN_656() {
}, Ok("ContestedResources([])".into()); "Non-existing end_index_values returns error")]
#[test_case::test_case(|q| q.end_index_values = vec![Value::Array(vec![0.into(), 1.into()])], Err("incorrect index values error: too many end index values were provided"); "wrong type of end_index_values should return InvalidArgument")]
#[test_case::test_case(|q| q.limit = Some(0), Err(r#"code: InvalidArgument"#); "limit 0 returns InvalidArgument")]
#[test_case::test_case(|q| q.limit = Some(std::u16::MAX), Err(r#"code: InvalidArgument"#); "limit std::u16::MAX returns InvalidArgument")]
#[test_case::test_case(|q| q.limit = Some(u16::MAX), Err(r#"code: InvalidArgument"#); "limit u16::MAX returns InvalidArgument")]
// Disabled due to bug PLAN-656
// #[test_case::test_case(|q| {
// q.start_index_values = vec![Value::Text("dash".to_string())];
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9192c1fc0db514525cac97f9afc0bd811b64d9cfbe81c62afdc4568238bb27e72fa397498db6a0869b4ff55be912edc3

This file was deleted.

Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9192c1fc0db514525cac97f9afc0bd811b64d9cfbe81c62afdc4568238bb27e72fa397498db6a0869b4ff55be912edc3

This file was deleted.

Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9192c1fc0db514525cac97f9afc0bd811b64d9cfbe81c62afdc4568238bb27e72fa397498db6a0869b4ff55be912edc3

This file was deleted.

Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9192c1fc0db514525cac97f9afc0bd811b64d9cfbe81c62afdc4568238bb27e72fa397498db6a0869b4ff55be912edc3

This file was deleted.

Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9192c1fc0db514525cac97f9afc0bd811b64d9cfbe81c62afdc4568238bb27e72fa397498db6a0869b4ff55be912edc3

This file was deleted.

Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9192c1fc0db514525cac97f9afc0bd811b64d9cfbe81c62afdc4568238bb27e72fa397498db6a0869b4ff55be912edc3

This file was deleted.

Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9192c1fc0db514525cac97f9afc0bd811b64d9cfbe81c62afdc4568238bb27e72fa397498db6a0869b4ff55be912edc3

This file was deleted.

0 comments on commit a646669

Please sign in to comment.