diff --git a/demo-contract-tests/tests/test_fixture/mod.rs b/demo-contract-tests/tests/test_fixture/mod.rs index 4f326c25..f5c85e6c 100644 --- a/demo-contract-tests/tests/test_fixture/mod.rs +++ b/demo-contract-tests/tests/test_fixture/mod.rs @@ -100,7 +100,7 @@ impl TestContext { let session_args = runtime_args! {}; let install_contract_request = ExecuteRequestBuilder::standard( admin, - &*PATH_TO_WASM_BINARIES + PATH_TO_WASM_BINARIES .join("demo-contract-optimized.wasm") .to_str() .expect("Failed to parse path as str"), @@ -142,7 +142,7 @@ impl TestContext { }; let session_request = ExecuteRequestBuilder::standard( user, - &*PATH_TO_WASM_BINARIES + PATH_TO_WASM_BINARIES .join("deposit-session-optimized.wasm") .to_str() .expect("Failed to parse path as str"), @@ -165,7 +165,7 @@ impl TestContext { }; let session_request = ExecuteRequestBuilder::standard( msg_sender, - &*PATH_TO_WASM_BINARIES + PATH_TO_WASM_BINARIES .join("malicious-session-optimized.wasm") .to_str() .expect("Failed to parse path as str"), @@ -190,7 +190,7 @@ impl TestContext { }; let session_request = ExecuteRequestBuilder::standard( msg_sender, - &*PATH_TO_WASM_BINARIES + PATH_TO_WASM_BINARIES .join("malicious-reader-optimized.wasm") .to_str() .expect("Failed to parse path as str"), diff --git a/kairos-l1-utils/src/lib.rs b/kairos-l1-utils/src/lib.rs index a33ec29c..5ef9a752 100644 --- a/kairos-l1-utils/src/lib.rs +++ b/kairos-l1-utils/src/lib.rs @@ -2,7 +2,7 @@ use casper_client::{ get_state_root_hash, query_global_state, types::StoredValue, JsonRpcId, Verbosity, }; use casper_hashing::Digest; -use casper_types::{account::AccountHash, PublicKey, URef}; +use casper_types::URef; use casper_client::{ rpcs::results::PutDeployResult, @@ -10,7 +10,7 @@ use casper_client::{ SuccessResponse, }; use casper_types::{crypto::SecretKey, Key, RuntimeArgs}; -use std::{fs, thread, time::Duration}; +use std::fs; pub const DEFAULT_PAYMENT_AMOUNT: u64 = 1_000_000_000_000; @@ -104,7 +104,7 @@ pub async fn query_contract_counter( node_address, Verbosity::Low, casper_client::rpcs::GlobalStateIdentifier::StateRootHash(srh), - contract_hash.into(), + contract_hash, path, ) .await @@ -119,184 +119,193 @@ pub async fn query_contract_counter( value } -/* -#[cfg_attr(not(feature = "cctl-tests"), ignore)] -#[tokio::test] -async fn install_wasm() { - use anyhow::anyhow; - use backoff::{backoff::Constant, future::retry}; - use casper_client::{get_deploy, Error, JsonRpcId, Verbosity}; - use casper_types::{runtime_args, RuntimeArgs}; - use kairos_test_utils::cctl::CCTLNetwork; - use std::fs::File; - use std::io::Read; - use std::path::Path; - let network = CCTLNetwork::run().await.unwrap(); - let node = network - .nodes - .first() - .expect("Expected at least one node after successful network run"); - let node_address: &str = &format!("http://localhost:{}", node.port.rpc_port); +#[cfg(test)] +mod tests { + use super::*; + use std::thread; + /* + #[cfg_attr(not(feature = "cctl-tests"), ignore)] + #[tokio::test] + async fn install_wasm() { + use anyhow::anyhow; + use backoff::{backoff::Constant, future::retry}; + use casper_client::{get_deploy, Error, JsonRpcId, Verbosity}; + use casper_types::{runtime_args, RuntimeArgs}; + use kairos_test_utils::cctl::CCTLNetwork; + use std::fs::File; + use std::io::Read; + use std::path::Path; - let wasm_path = Path::new(env!("PATH_TO_WASM_BINARIES")).join("demo-contract-optimized.wasm"); - let mut wasm_file: File = File::open(wasm_path).unwrap(); - let mut wasm_bytes: Vec = Vec::new(); - wasm_file.read_to_end(&mut wasm_bytes).unwrap(); + let network = CCTLNetwork::run().await.unwrap(); + let node = network + .nodes + .first() + .expect("Expected at least one node after successful network run"); + let node_address: &str = &format!("http://localhost:{}", node.port.rpc_port); - let runtime_args: RuntimeArgs = runtime_args! {}; - let result: SuccessResponse = install_wasm_bytecode( - node_address, - "cspr-dev-cctl", - runtime_args, - &wasm_bytes, - network - .assets_dir - .join("users/user-1/secret_key.pem") - .to_str() - .unwrap(), - ) - .await; + let wasm_path = Path::new(env!("PATH_TO_WASM_BINARIES")).join("demo-contract-optimized.wasm"); + let mut wasm_file: File = File::open(wasm_path).unwrap(); + let mut wasm_bytes: Vec = Vec::new(); + wasm_file.read_to_end(&mut wasm_bytes).unwrap(); - // wait for successful processing of deploy - retry( - Constant::new(std::time::Duration::from_millis(100)), - || async { - get_deploy( - JsonRpcId::Number(1), - node_address, - Verbosity::High, - result.result.deploy_hash, - true, - ) - .await - .map_err(|err| match &err { - Error::ResponseIsHttpError { .. } | Error::FailedToGetResponse { .. } => { - backoff::Error::transient(anyhow!(err)) - } - _ => backoff::Error::permanent(anyhow!(err)), - }) - .map(|success| { - if success - .result - .execution_results - .iter() - .all(|execution_result| match execution_result.result { - casper_types::ExecutionResult::Success { .. } => true, - casper_types::ExecutionResult::Failure { .. } => false, - }) - { - Ok(()) - } else { - Err(backoff::Error::transient(anyhow!( - "Deploy was not processed yet" - ))) - } - })? - }, - ) - .await - .unwrap() -} -*/ + let runtime_args: RuntimeArgs = runtime_args! {}; + let result: SuccessResponse = install_wasm_bytecode( + node_address, + "cspr-dev-cctl", + runtime_args, + &wasm_bytes, + network + .assets_dir + .join("users/user-1/secret_key.pem") + .to_str() + .unwrap(), + ) + .await; -#[cfg_attr(not(feature = "cctl-tests"), ignore)] -#[tokio::test] -async fn counter_query_test() { - use anyhow::anyhow; - use backoff::{backoff::Constant, future::retry}; - use casper_client::{get_deploy, Error, JsonRpcId, Verbosity}; - use casper_types::{runtime_args, RuntimeArgs}; - use kairos_test_utils::cctl::CCTLNetwork; - use std::fs::File; - use std::io::Read; - use std::path::Path; + // wait for successful processing of deploy + retry( + Constant::new(std::time::Duration::from_millis(100)), + || async { + get_deploy( + JsonRpcId::Number(1), + node_address, + Verbosity::High, + result.result.deploy_hash, + true, + ) + .await + .map_err(|err| match &err { + Error::ResponseIsHttpError { .. } | Error::FailedToGetResponse { .. } => { + backoff::Error::transient(anyhow!(err)) + } + _ => backoff::Error::permanent(anyhow!(err)), + }) + .map(|success| { + if success + .result + .execution_results + .iter() + .all(|execution_result| match execution_result.result { + casper_types::ExecutionResult::Success { .. } => true, + casper_types::ExecutionResult::Failure { .. } => false, + }) + { + Ok(()) + } else { + Err(backoff::Error::transient(anyhow!( + "Deploy was not processed yet" + ))) + } + })? + }, + ) + .await + .unwrap() + } + */ - let network = CCTLNetwork::run().await.unwrap(); - let node = network - .nodes - .first() - .expect("Expected at least one node after successful network run"); - let node_address: &str = &format!("http://localhost:{}", node.port.rpc_port); + #[cfg_attr(not(feature = "cctl-tests"), ignore)] + #[tokio::test] + async fn counter_query_test() { + use anyhow::anyhow; + use backoff::{backoff::Constant, future::retry}; + use casper_client::{get_deploy, Error, JsonRpcId, Verbosity}; + use casper_types::account::AccountHash; + use casper_types::{crypto::PublicKey, runtime_args, RuntimeArgs}; + use kairos_test_utils::cctl::CCTLNetwork; + use std::fs::File; + use std::io::Read; + use std::path::Path; - let wasm_path = Path::new(env!("PATH_TO_WASM_BINARIES")).join("demo-contract-optimized.wasm"); - let mut wasm_file: File = File::open(wasm_path).unwrap(); - let mut wasm_bytes: Vec = Vec::new(); - wasm_file.read_to_end(&mut wasm_bytes).unwrap(); + let network = CCTLNetwork::run().await.unwrap(); + let node = network + .nodes + .first() + .expect("Expected at least one node after successful network run"); + let node_address: &str = &format!("http://localhost:{}", node.port.rpc_port); - let runtime_args: RuntimeArgs = runtime_args! {}; - let result: SuccessResponse = install_wasm_bytecode( - node_address, - "cspr-dev-cctl", - runtime_args, - &wasm_bytes, - network - .assets_dir - .join("users/user-1/secret_key.pem") - .to_str() - .unwrap(), - ) - .await; + let wasm_path = + Path::new(env!("PATH_TO_WASM_BINARIES")).join("demo-contract-optimized.wasm"); + let mut wasm_file: File = File::open(wasm_path).unwrap(); + let mut wasm_bytes: Vec = Vec::new(); + wasm_file.read_to_end(&mut wasm_bytes).unwrap(); - // wait for successful processing of deploy - retry( - Constant::new(std::time::Duration::from_millis(100)), - || async { - get_deploy( - JsonRpcId::Number(1), - node_address, - Verbosity::High, - result.result.deploy_hash, - true, - ) - .await - .map_err(|err| match &err { - Error::ResponseIsHttpError { .. } | Error::FailedToGetResponse { .. } => { - backoff::Error::transient(anyhow!(err)) - } - _ => backoff::Error::permanent(anyhow!(err)), - }) - .map(|success| { - if success - .result - .execution_results - .iter() - .all(|execution_result| match execution_result.result { - casper_types::ExecutionResult::Success { .. } => true, - casper_types::ExecutionResult::Failure { .. } => false, - }) - { - Ok(()) - } else { - Err(backoff::Error::transient(anyhow!( - "Deploy was not processed yet" - ))) - } - })? - }, - ) - .await - .unwrap(); + let runtime_args: RuntimeArgs = runtime_args! {}; + let result: SuccessResponse = install_wasm_bytecode( + node_address, + "cspr-dev-cctl", + runtime_args, + &wasm_bytes, + network + .assets_dir + .join("users/user-1/secret_key.pem") + .to_str() + .unwrap(), + ) + .await; - // replace this with a better solution - thread::sleep(Duration::from_secs(10)); + // wait for successful processing of deploy + retry( + Constant::new(std::time::Duration::from_millis(100)), + || async { + get_deploy( + JsonRpcId::Number(1), + node_address, + Verbosity::High, + result.result.deploy_hash, + true, + ) + .await + .map_err(|err| match &err { + Error::ResponseIsHttpError { .. } | Error::FailedToGetResponse { .. } => { + backoff::Error::transient(anyhow!(err)) + } + _ => backoff::Error::permanent(anyhow!(err)), + }) + .map(|success| { + if success + .result + .execution_results + .iter() + .all(|execution_result| match execution_result.result { + casper_types::ExecutionResult::Success { .. } => true, + casper_types::ExecutionResult::Failure { .. } => false, + }) + { + Ok(()) + } else { + Err(backoff::Error::transient(anyhow!( + "Deploy was not processed yet" + ))) + } + })? + }, + ) + .await + .unwrap(); - let public_key_path = network.assets_dir.join("users/user-1/public_key.pem"); + // replace this with a better solution + thread::sleep(std::time::Duration::from_secs(10)); - let public_key: PublicKey = PublicKey::from_file(public_key_path.to_str().unwrap()).unwrap(); - let account_hash: AccountHash = public_key.to_account_hash(); - // this is the default cctl account for user-1 - let account: Key = Key::from(account_hash); - let srh: Digest = query_state_root_hash(node_address).await; - let counter_value: u64 = query_contract_counter( - node_address, - srh, - account, - vec![ - "kairos_demo_contract".to_string(), - "last_processed_deposit_counter".to_string(), - ], - ) - .await; - assert_eq!(counter_value, 0_u64); + let public_key_path = network.assets_dir.join("users/user-1/public_key.pem"); + + let public_key: PublicKey = + PublicKey::from_file(public_key_path.to_str().unwrap()).unwrap(); + let account_hash: AccountHash = public_key.to_account_hash(); + // this is the default cctl account for user-1 + let account: Key = Key::from(account_hash); + let srh: Digest = query_state_root_hash(node_address).await; + let counter_value: u64 = query_contract_counter( + node_address, + srh, + account, + vec![ + "kairos_demo_contract".to_string(), + "last_processed_deposit_counter".to_string(), + ], + ) + .await; + assert_eq!(counter_value, 0_u64); + } }