Skip to content

Commit 0b4a733

Browse files
feat(sdk): add query for data contract history (#1787)
Co-authored-by: Quantum Explorer <quantum@dash.org>
1 parent 5404679 commit 0b4a733

7 files changed

+61
-1
lines changed

packages/rs-sdk/src/mock/sdk.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ impl MockDashPlatformSdk {
120120
self.load_expectation::<proto::GetDataContractsRequest>(filename)
121121
.await?
122122
}
123+
"GetDataContractHistoryRequest" => {
124+
self.load_expectation::<proto::GetDataContractHistoryRequest>(filename)
125+
.await?
126+
}
123127
"IdentityRequest" => self.load_expectation::<IdentityRequest>(filename).await?,
124128
"GetIdentityRequest" => {
125129
self.load_expectation::<proto::GetIdentityRequest>(filename)

packages/rs-sdk/src/platform/fetch.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ impl Fetch for drive_proof_verifier::types::IdentityBalanceAndRevision {
219219
type Request = platform_proto::GetIdentityBalanceAndRevisionRequest;
220220
}
221221

222+
impl Fetch for drive_proof_verifier::types::DataContractHistory {
223+
type Request = platform_proto::GetDataContractHistoryRequest;
224+
}
225+
222226
impl Fetch for ExtendedEpochInfo {
223227
type Request = platform_proto::GetEpochsInfoRequest;
224228
}

packages/rs-sdk/src/platform/query.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,27 @@ impl Query<proto::GetDataContractsRequest> for Vec<Identifier> {
114114
}
115115
}
116116

117+
impl Query<proto::GetDataContractHistoryRequest> for LimitQuery<(Identifier, u64)> {
118+
fn query(self, prove: bool) -> Result<proto::GetDataContractHistoryRequest, Error> {
119+
if !prove {
120+
unimplemented!("queries without proofs are not supported yet");
121+
}
122+
let (id, start_at_ms) = self.query;
123+
124+
Ok(proto::GetDataContractHistoryRequest {
125+
version: Some(proto::get_data_contract_history_request::Version::V0(
126+
proto::get_data_contract_history_request::GetDataContractHistoryRequestV0 {
127+
id: id.to_vec(),
128+
limit: self.limit,
129+
offset: None,
130+
start_at_ms,
131+
prove,
132+
},
133+
)),
134+
})
135+
}
136+
}
137+
117138
impl Query<proto::GetIdentityKeysRequest> for Identifier {
118139
/// Get all keys for an identity with provided identifier.
119140
fn query(self, prove: bool) -> Result<proto::GetIdentityKeysRequest, Error> {

packages/rs-sdk/tests/fetch/data_contract.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use super::config::Config;
2-
use dash_sdk::platform::{Fetch, FetchMany};
2+
use dash_sdk::platform::{Fetch, FetchMany, LimitQuery};
3+
use dpp::data_contract::accessors::v0::DataContractV0Getters;
4+
use dpp::platform_value::string_encoding::Encoding;
35
use dpp::prelude::{DataContract, Identifier};
6+
use drive_proof_verifier::types::DataContractHistory;
47

58
/// Given some dummy data contract ID, when I fetch data contract, I get None because it doesn't exist.
69
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
@@ -88,3 +91,30 @@ async fn test_data_contracts_2_nx() {
8891
"proof of non-existence 2 failed"
8992
);
9093
}
94+
95+
// This test currently supports offline mode only.
96+
// It needs Data Contract with `keep_history` set to true, which is not available in the network
97+
// by default and has to be created manually.
98+
// At the moment tests in rs-sdk do not provide Core Wallet signer, and unable to create
99+
// identities and data contracts.
100+
// The contract for this test was pre-created with
101+
// `packages/platform-test-suite/test/functional/platform/DataContract.spec.js`
102+
// and stored as a test vector for offline testing only.
103+
#[cfg(not(feature = "network-testing"))]
104+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
105+
async fn test_data_contract_history_read() {
106+
let cfg = Config::new();
107+
let id = Identifier::from_string(
108+
"3c71ba2d0b655ac3d231c4411d3c3fad43451d3f48213537ff9da331b24e5dea",
109+
Encoding::Hex,
110+
)
111+
.unwrap();
112+
113+
let sdk = cfg.setup_api("test_data_contract_history_read").await;
114+
115+
let result = DataContractHistory::fetch(&sdk, LimitQuery::from((id, 10))).await;
116+
117+
assert!(matches!(result, Ok(Some(_))), "result: {:?}", result);
118+
let (_, contract) = result.unwrap().unwrap().pop_first().unwrap();
119+
assert_eq!(contract.id(), id);
120+
}

packages/rs-sdk/tests/vectors/test_data_contract_history_read/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[134,90,211,2,159,204,117,201,202,79,41,171,146,86,115,171,247,117,185,147,133,32,111,139,93,126,226,86,104,239,140,58,90,36,89,121,41,96,244,146,48,229,249,80,185,69,142,129]

0 commit comments

Comments
 (0)