|
| 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 | +} |
0 commit comments