Skip to content

Commit f41f2bc

Browse files
committed
test(sdk): asset locks verify tests, WIP
1 parent 4df0c40 commit f41f2bc

File tree

5 files changed

+126
-7
lines changed

5 files changed

+126
-7
lines changed

packages/rs-sdk/src/platform/transition/asset_lock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rs_dapi_client::{DapiRequestExecutor, RequestSettings};
1313
pub trait AssetLockProofVerifier {
1414
/// Verifies the asset lock proof against the platform.
1515
///
16-
/// This function will return an error if the proof is not yet available on the platform.
16+
/// This function will return an error if Dash Platform cannot use the provided asset lock proof.
1717
///
1818
/// # Errors
1919
///

packages/rs-sdk/src/platform/types/epoch.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use crate::{
88
Error, Sdk,
99
};
1010

11+
/// Epoch information
12+
pub type Epoch = ExtendedEpochInfo;
13+
1114
#[async_trait]
1215

1316
/// Helper trait for managing Epoch information
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
use dapi_grpc::platform::v0::get_epochs_info_request::GetEpochsInfoRequestV0;
2+
use dapi_grpc::platform::v0::{GetEpochsInfoRequest, GetEpochsInfoResponse};
3+
use dapi_grpc::platform::VersionedGrpcResponse;
4+
use dash_sdk::platform::transition::asset_lock::AssetLockProofVerifier;
5+
use dash_sdk::platform::types::epoch::{Epoch, ExtendedEpochInfoEx};
6+
use dpp::dashcore::consensus::deserialize;
7+
use dpp::dashcore::hash_types::CycleHash;
8+
use dpp::dashcore::hashes::hex::FromHex;
9+
use dpp::dashcore::hashes::Hash;
10+
use dpp::dashcore::{InstantLock, Transaction};
11+
use dpp::identity::state_transition::asset_lock_proof::chain::ChainAssetLockProof;
12+
use dpp::identity::state_transition::asset_lock_proof::InstantAssetLockProof;
13+
use dpp::prelude::AssetLockProof;
14+
use rs_dapi_client::DapiRequest;
15+
16+
use super::{common::setup_logs, config::Config};
17+
18+
async fn current_platform_state(sdk: &dash_sdk::Sdk) -> (u32, Vec<u8>) {
19+
let req: GetEpochsInfoRequest = GetEpochsInfoRequestV0 {
20+
ascending: false,
21+
count: 1,
22+
prove: true,
23+
start_epoch: None,
24+
}
25+
.into();
26+
27+
let resp: GetEpochsInfoResponse = req
28+
.execute(sdk, Default::default())
29+
.await
30+
.expect("get epoch info");
31+
let core_height = resp.metadata().expect("metadata").core_chain_locked_height;
32+
let quorum_hash = resp.proof().expect("proof").quorum_hash.clone();
33+
(core_height, quorum_hash)
34+
}
35+
36+
/// Given some existing identity ID, when I fetch the identity, and I get it.
37+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
38+
async fn test_asset_lock_proof() {
39+
setup_logs();
40+
41+
let cfg = Config::new();
42+
43+
let sdk = cfg.setup_api("test_asset_lock_proof").await;
44+
let (core_chain_locked_height, quorum_hash) = current_platform_state(&sdk).await;
45+
46+
// some semi-correct instant lock
47+
let cyclehash = CycleHash::from_slice(&quorum_hash).expect("cycle hash");
48+
let instant_lock = InstantLock {
49+
cyclehash,
50+
..Default::default()
51+
};
52+
53+
let out_point = [0u8; 36];
54+
55+
// some hardcoded tx, just for tests
56+
let tx_bytes = Vec::from_hex(
57+
"010000000001000100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000"
58+
).unwrap();
59+
let tx: Transaction = deserialize(&tx_bytes).expect("deserialize tx");
60+
61+
struct TestCase {
62+
asset_lock_proof: AssetLockProof,
63+
// expect err that can be retried
64+
expect_err: bool,
65+
}
66+
// instant_lock: InstantLock, transaction: Transaction, output_index: u32
67+
let test_cases = vec![
68+
TestCase {
69+
asset_lock_proof: AssetLockProof::Chain(ChainAssetLockProof::new(
70+
core_chain_locked_height,
71+
out_point,
72+
)),
73+
expect_err: false,
74+
},
75+
TestCase {
76+
asset_lock_proof: AssetLockProof::Instant(InstantAssetLockProof::new(
77+
instant_lock,
78+
tx.clone(),
79+
0,
80+
)),
81+
expect_err: false,
82+
},
83+
TestCase {
84+
asset_lock_proof: AssetLockProof::Instant(InstantAssetLockProof::new(
85+
InstantLock::default(),
86+
tx,
87+
0,
88+
)),
89+
expect_err: true,
90+
},
91+
TestCase {
92+
asset_lock_proof: AssetLockProof::Chain(ChainAssetLockProof::new(
93+
core_chain_locked_height + 100,
94+
out_point,
95+
)),
96+
expect_err: true,
97+
},
98+
];
99+
100+
for (i, tc) in test_cases.into_iter().enumerate() {
101+
let result = tc.asset_lock_proof.verify(&sdk).await;
102+
assert_eq!(
103+
result.is_err(),
104+
tc.expect_err,
105+
"tc {} expeced err = {}, got err = {}: {:?}",
106+
i,
107+
tc.expect_err,
108+
result.is_err(),
109+
result
110+
);
111+
}
112+
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! This module contains [Config] struct that can be used to configure dash-platform-sdk.
44
//! It's mainly used for testing.
55
6+
use dash_sdk::networks::NetworkType;
67
use dpp::platform_value::string_encoding::Encoding;
78
use dpp::{
89
dashcore::{hashes::Hash, ProTxHash},
@@ -176,12 +177,14 @@ impl Config {
176177
#[cfg(all(feature = "network-testing", not(feature = "offline-testing")))]
177178
let sdk = {
178179
// Dump all traffic to disk
179-
let builder = dash_sdk::SdkBuilder::new(self.address_list()).with_core(
180-
&self.platform_host,
181-
self.core_port,
182-
&self.core_user,
183-
&self.core_password,
184-
);
180+
let builder = dash_sdk::SdkBuilder::new(self.address_list())
181+
.with_core(
182+
&self.platform_host,
183+
self.core_port,
184+
&self.core_user,
185+
&self.core_password,
186+
)
187+
.with_network_type(NetworkType::Devnet);
185188

186189
#[cfg(feature = "generate-test-vectors")]
187190
let builder = {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ compile_error!("tests require `mocks` feature to be enabled");
55
#[cfg(not(any(feature = "network-testing", feature = "offline-testing")))]
66
compile_error!("network-testing or offline-testing must be enabled for tests");
77

8+
mod asset_lock;
89
#[cfg(feature = "mocks")]
910
mod broadcast;
1011
mod common;

0 commit comments

Comments
 (0)