-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into revert-202-EPROD-653-define-how-to-handle-pa…
…nics-in-the-task-scheduler
- Loading branch information
Showing
10 changed files
with
294 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
use std::sync::Arc; | ||
|
||
use candid::utils::ArgumentEncoder; | ||
use candid::{CandidType, Decode, Principal}; | ||
use ic_exports::ic_kit::RejectionCode; | ||
use ic_exports::ic_test_state_machine::{StateMachine, WasmResult}; | ||
use serde::Deserialize; | ||
use tokio::sync::Mutex; | ||
|
||
use crate::{CanisterClient, CanisterClientError, CanisterClientResult}; | ||
|
||
/// A client for interacting with a canister inside dfinity's | ||
/// state machine tests framework. | ||
#[derive(Clone)] | ||
pub struct StateMachineCanisterClient { | ||
state_machine: Arc<Mutex<StateMachine>>, | ||
canister: Principal, | ||
caller: Principal, | ||
} | ||
|
||
impl StateMachineCanisterClient { | ||
/// Creates a new instance of a StateMachineCanisterClient. | ||
pub fn new( | ||
state_machine: Arc<Mutex<StateMachine>>, | ||
canister: Principal, | ||
caller: Principal, | ||
) -> Self { | ||
Self { | ||
state_machine, | ||
canister, | ||
caller, | ||
} | ||
} | ||
|
||
/// Returns the caller of the canister. | ||
pub fn caller(&self) -> Principal { | ||
self.caller | ||
} | ||
|
||
/// Replace the caller. | ||
pub fn set_caller(&mut self, caller: Principal) { | ||
self.caller = caller; | ||
} | ||
|
||
/// Returns the canister of the canister. | ||
pub fn canister(&self) -> Principal { | ||
self.canister | ||
} | ||
|
||
/// Replace the canister to call. | ||
pub fn set_canister(&mut self, canister: Principal) { | ||
self.canister = canister; | ||
} | ||
|
||
/// Returns the state machine of the canister. | ||
pub fn state_machine(&self) -> &Mutex<StateMachine> { | ||
self.state_machine.as_ref() | ||
} | ||
|
||
/// Performs a blocking action with state machine and awaits the result. | ||
/// | ||
/// Arguments of the closure `f`: | ||
/// 1) `env` - The state machine environment. | ||
/// 2) `canister` - The canister principal. | ||
/// 3) `caller` - The caller principal. | ||
pub async fn with_state_machine<F, R>(&self, f: F) -> R | ||
where | ||
F: Send + FnOnce(&StateMachine, Principal, Principal) -> R + 'static, | ||
R: Send + 'static, | ||
{ | ||
let client = self.state_machine.clone(); | ||
let cansiter = self.canister; | ||
let caller = self.caller; | ||
|
||
tokio::task::spawn_blocking(move || { | ||
let locked_client = client.blocking_lock(); | ||
f(&locked_client, cansiter, caller) | ||
}) | ||
.await | ||
.unwrap() | ||
} | ||
|
||
pub async fn update<T, R>(&self, method: &str, args: T) -> CanisterClientResult<R> | ||
where | ||
T: ArgumentEncoder + Send + Sync, | ||
R: for<'de> Deserialize<'de> + CandidType, | ||
{ | ||
let args = candid::encode_args(args)?; | ||
let method = String::from(method); | ||
|
||
let call_result = self | ||
.with_state_machine(move |env, canister, caller| { | ||
env.update_call(canister, caller, &method, args) | ||
}) | ||
.await?; | ||
|
||
let reply = match call_result { | ||
WasmResult::Reply(reply) => reply, | ||
WasmResult::Reject(e) => { | ||
return Err(CanisterClientError::CanisterError(( | ||
RejectionCode::CanisterError, | ||
e, | ||
))); | ||
} | ||
}; | ||
|
||
let decoded = Decode!(&reply, R)?; | ||
Ok(decoded) | ||
} | ||
|
||
pub async fn query<T, R>(&self, method: &str, args: T) -> CanisterClientResult<R> | ||
where | ||
T: ArgumentEncoder + Send + Sync, | ||
R: for<'de> Deserialize<'de> + CandidType, | ||
{ | ||
let args = candid::encode_args(args)?; | ||
let method = String::from(method); | ||
|
||
let call_result = self | ||
.with_state_machine(move |env, canister, caller| { | ||
env.query_call(canister, caller, &method, args) | ||
}) | ||
.await?; | ||
|
||
let reply = match call_result { | ||
WasmResult::Reply(reply) => reply, | ||
WasmResult::Reject(e) => { | ||
return Err(CanisterClientError::CanisterError(( | ||
RejectionCode::CanisterError, | ||
e, | ||
))); | ||
} | ||
}; | ||
|
||
let decoded = Decode!(&reply, R)?; | ||
Ok(decoded) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl CanisterClient for StateMachineCanisterClient { | ||
async fn update<T, R>(&self, method: &str, args: T) -> CanisterClientResult<R> | ||
where | ||
T: ArgumentEncoder + Send + Sync, | ||
R: for<'de> Deserialize<'de> + CandidType, | ||
{ | ||
StateMachineCanisterClient::update(self, method, args).await | ||
} | ||
|
||
async fn query<T, R>(&self, method: &str, args: T) -> CanisterClientResult<R> | ||
where | ||
T: ArgumentEncoder + Send + Sync, | ||
R: for<'de> Deserialize<'de> + CandidType, | ||
{ | ||
StateMachineCanisterClient::query(self, method, args).await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use std::fs::{create_dir_all, File}; | ||
use std::io::*; | ||
use std::path::Path; | ||
use std::time::Duration; | ||
|
||
use flate2::read::GzDecoder; | ||
pub use ic_test_state_machine_client::*; | ||
use log::*; | ||
use once_cell::sync::OnceCell; | ||
|
||
pub const IC_STATE_MACHINE_BINARY_HASH: &str = "48da85ee6c03e8c15f3e90b21bf9ccae7b753ee6"; | ||
|
||
/// Returns the path to the ic-test-state-machine binary. | ||
/// If the binary is not present, it downloads it. | ||
/// See: https://github.com/dfinity/test-state-machine-client | ||
/// | ||
/// It supports only linux and macos | ||
/// | ||
/// The search_path variable is the folder where to search for the binary | ||
/// or to download it if not present | ||
pub fn get_ic_test_state_machine_client_path(search_path: &str) -> String { | ||
static FILES: OnceCell<String> = OnceCell::new(); | ||
FILES.get_or_init(|| download_binary(search_path)).clone() | ||
} | ||
|
||
fn download_binary(base_path: &str) -> String { | ||
let platform = match std::env::consts::OS { | ||
"linux" => "linux", | ||
"macos" => "darwin", | ||
_ => panic!("ic_test_state_machine_client requires linux or macos"), | ||
}; | ||
|
||
let output_file_name = "ic-test-state-machine"; | ||
let gz_file_name = format!("{output_file_name}.gz"); | ||
let download_url = format!("https://download.dfinity.systems/ic/{IC_STATE_MACHINE_BINARY_HASH}/binaries/x86_64-{platform}/{gz_file_name}"); | ||
|
||
let dest_path_name = format!("{}/{}", base_path, "ic_test_state_machine"); | ||
let dest_dir_path = Path::new(&dest_path_name); | ||
let gz_dest_file_path = format!("{}/{}", dest_path_name, gz_file_name); | ||
let output_dest_file_path = format!("{}/{}", dest_path_name, output_file_name); | ||
|
||
if !Path::new(&output_dest_file_path).exists() { | ||
// Download file | ||
{ | ||
info!( | ||
"ic-test-state-machine binarey not found, downloading binary from: {download_url}" | ||
); | ||
|
||
let response = reqwest::blocking::Client::builder() | ||
.timeout(Duration::from_secs(120)) | ||
.build() | ||
.unwrap() | ||
.get(download_url) | ||
.send() | ||
.unwrap(); | ||
|
||
create_dir_all(dest_dir_path).unwrap(); | ||
|
||
let mut file = match File::create(&gz_dest_file_path) { | ||
Err(why) => panic!("couldn't create {}", why), | ||
Ok(file) => file, | ||
}; | ||
let content = response.bytes().unwrap(); | ||
info!("ic-test-state-machine.gz file length: {}", content.len()); | ||
file.write_all(&content).unwrap(); | ||
file.flush().unwrap(); | ||
} | ||
|
||
// unzip file | ||
{ | ||
info!( | ||
"unzip ic-test-state-machine to [{}]", | ||
dest_dir_path.to_str().unwrap() | ||
); | ||
let tar_gz = File::open(gz_dest_file_path).unwrap(); | ||
let mut tar = GzDecoder::new(tar_gz); | ||
let mut temp = vec![]; | ||
tar.read_to_end(&mut temp).unwrap(); | ||
|
||
let mut output = File::create(&output_dest_file_path).unwrap(); | ||
output.write_all(&temp).unwrap(); | ||
output.flush().unwrap(); | ||
|
||
#[cfg(target_family = "unix")] | ||
{ | ||
use std::os::unix::prelude::PermissionsExt; | ||
let mut perms = std::fs::metadata(&output_dest_file_path) | ||
.unwrap() | ||
.permissions(); | ||
perms.set_mode(0o770); | ||
std::fs::set_permissions(&output_dest_file_path, perms).unwrap(); | ||
} | ||
} | ||
} | ||
output_dest_file_path | ||
} | ||
|
||
#[test] | ||
fn should_get_ic_test_state_machine_client_path() { | ||
let path = get_ic_test_state_machine_client_path("../target"); | ||
assert!(Path::new(&path).exists()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters