From 45c6e6febba5e3e41bcd2eb4c789e04d5dbccd91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Mon, 15 Jul 2024 13:53:40 +0200 Subject: [PATCH 1/3] cctl: configure verbosity depending on tracing log level --- kairos-test-utils/src/cctl.rs | 76 +++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/kairos-test-utils/src/cctl.rs b/kairos-test-utils/src/cctl.rs index 00c1dbc3..d05bc209 100644 --- a/kairos-test-utils/src/cctl.rs +++ b/kairos-test-utils/src/cctl.rs @@ -51,6 +51,16 @@ pub struct DeployableContract { pub path: PathBuf, } +pub fn casper_client_verbosity() -> Verbosity { + if tracing::enabled!(tracing::Level::TRACE) { + Verbosity::High + } else if tracing::enabled!(tracing::Level::DEBUG) { + Verbosity::Medium + } else { + Verbosity::Low + } +} + // max amount allowed to be used on gas fees pub const MAX_GAS_FEE_PAYMENT_AMOUNT: u64 = 10_000_000_000_000; @@ -139,32 +149,34 @@ impl CCTLNetwork { // This prevents retrying forever even after ctrl-c let timed_out = start_time.elapsed().as_secs() > 60; - get_node_status(JsonRpcId::Number(1), &casper_node_rpc_url, Verbosity::Low) - .await - .map_err(|err| { - let elapsed = start_time.elapsed().as_secs(); - tracing::info!("Running for {elapsed}s, Error: {err:?}"); - err - }) - .map_err(|err| match &err { - err if timed_out => { - backoff::Error::permanent(anyhow!("Timeout on error: {err:?}")) - } - Error::ResponseIsHttpError { .. } | Error::FailedToGetResponse { .. } => { - backoff::Error::transient(anyhow!(err)) - } - _ => backoff::Error::permanent(anyhow!(err)), - }) - .map(|success| match success.result.reactor_state { - ReactorState::Validate => Ok(()), - // _ if timed_out => Ok(()), - rs if timed_out => Err(backoff::Error::permanent(anyhow!( - "Node didn't reach the VALIDATE state before timeout: {rs:?}" - ))), - _ => Err(backoff::Error::transient(anyhow!( - "Node didn't reach the VALIDATE state yet" - ))), - })? + get_node_status( + JsonRpcId::Number(1), + &casper_node_rpc_url, + casper_client_verbosity(), + ) + .await + .map_err(|err| { + let elapsed = start_time.elapsed().as_secs(); + tracing::info!("Running for {elapsed}s, Error: {err:?}"); + err + }) + .map_err(|err| match &err { + err if timed_out => backoff::Error::permanent(anyhow!("Timeout on error: {err:?}")), + Error::ResponseIsHttpError { .. } | Error::FailedToGetResponse { .. } => { + backoff::Error::transient(anyhow!(err)) + } + _ => backoff::Error::permanent(anyhow!(err)), + }) + .map(|success| match success.result.reactor_state { + ReactorState::Validate => Ok(()), + // _ if timed_out => Ok(()), + rs if timed_out => Err(backoff::Error::permanent(anyhow!( + "Node didn't reach the VALIDATE state before timeout: {rs:?}" + ))), + _ => Err(backoff::Error::transient(anyhow!( + "Node didn't reach the VALIDATE state yet" + ))), + })? }) .await .expect("Waiting for network to pass genesis failed"); @@ -243,6 +255,8 @@ async fn deploy_contract( path.to_str().unwrap() ); + let casper_client_verbosity = casper_client_verbosity(); + let contract_bytes = fs::read(path)?; let contract = ExecutableDeployItem::new_module_bytes(contract_bytes.into(), runtime_args.clone()); @@ -262,7 +276,7 @@ async fn deploy_contract( let deploy_hash = put_deploy( expected_rpc_id.clone(), casper_node_rpc_url, - Verbosity::High, + casper_client_verbosity, deploy, ) .await @@ -284,7 +298,7 @@ async fn deploy_contract( let response = get_deploy( expected_rpc_id.clone(), casper_node_rpc_url, - Verbosity::High, + casper_client_verbosity, deploy_hash, false, ) @@ -330,7 +344,7 @@ async fn deploy_contract( let state_root_hash = get_state_root_hash( expected_rpc_id.clone(), casper_node_rpc_url, - Verbosity::High, + casper_client_verbosity, Option::None, ) .await @@ -350,7 +364,7 @@ async fn deploy_contract( let account = get_account( expected_rpc_id.clone(), casper_node_rpc_url, - Verbosity::High, + casper_client_verbosity, Option::None, contract_deployer_pkey.clone(), ) @@ -369,7 +383,7 @@ async fn deploy_contract( let contract_hash: casper_client_types::ContractHash = query_global_state( expected_rpc_id.clone(), casper_node_rpc_url, - Verbosity::High, + casper_client_verbosity, casper_client::rpcs::GlobalStateIdentifier::StateRootHash(state_root_hash), // fetches recent blocks state root hash account_key, vec![hash_name.clone()], From 567b7204224d6693a3e872b58bfd7a9e34e48dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Mon, 15 Jul 2024 13:54:01 +0200 Subject: [PATCH 2/3] cctl: increase timeout value --- kairos-test-utils/src/cctl.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kairos-test-utils/src/cctl.rs b/kairos-test-utils/src/cctl.rs index d05bc209..a8514591 100644 --- a/kairos-test-utils/src/cctl.rs +++ b/kairos-test-utils/src/cctl.rs @@ -147,7 +147,7 @@ impl CCTLNetwork { tracing::info!("Waiting for network to pass genesis"); retry(ExponentialBackoff::default(), || async { // This prevents retrying forever even after ctrl-c - let timed_out = start_time.elapsed().as_secs() > 60; + let timed_out = start_time.elapsed().as_secs() > 90; get_node_status( JsonRpcId::Number(1), From 640b8077999d280c5a41914e8bcce64b55303523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Mon, 15 Jul 2024 13:54:29 +0200 Subject: [PATCH 3/3] cctl: don't use rng since we are testing --- Cargo.lock | 1 - kairos-test-utils/Cargo.toml | 1 - kairos-test-utils/src/cctl.rs | 100 ++++++++++++---------------------- 3 files changed, 34 insertions(+), 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 90acdd7b..830cc01f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2564,7 +2564,6 @@ dependencies = [ "hex", "kairos-server", "nom", - "rand 0.8.5", "reqwest 0.12.4", "sd-notify", "tempfile", diff --git a/kairos-test-utils/Cargo.toml b/kairos-test-utils/Cargo.toml index 664d72bd..4800300f 100644 --- a/kairos-test-utils/Cargo.toml +++ b/kairos-test-utils/Cargo.toml @@ -29,7 +29,6 @@ casper-types.workspace = true casper-client-types.workspace = true nom = "7" hex = "0.4" -rand = "0.8" sd-notify = "0.4" tokio = { version = "1", features = [ "full", "tracing", "macros" ] } tempfile = "3" diff --git a/kairos-test-utils/src/cctl.rs b/kairos-test-utils/src/cctl.rs index a8514591..86d08fb2 100644 --- a/kairos-test-utils/src/cctl.rs +++ b/kairos-test-utils/src/cctl.rs @@ -9,7 +9,6 @@ use casper_client::{ }; use casper_client_types::{ContractHash, ExecutionResult, Key, PublicKey, RuntimeArgs, SecretKey}; use hex::FromHex; -use rand::Rng; use std::io::{self, Write}; use std::path::Path; use std::path::PathBuf; @@ -272,31 +271,23 @@ async fn deploy_contract( .build()?; tracing::info!("Submitting contract deploy"); - let expected_rpc_id = JsonRpcId::Number(rand::thread_rng().gen::()); let deploy_hash = put_deploy( - expected_rpc_id.clone(), + JsonRpcId::Number(1), casper_node_rpc_url, casper_client_verbosity, deploy, ) .await .map_err(Into::::into) - .and_then(|response| { - if response.id == expected_rpc_id { - Ok(response.result.deploy_hash) - } else { - Err(anyhow!("JSON RPC Id missmatch")) - } - })?; + .map(|response| response.result.deploy_hash)?; tracing::info!("Waiting for successful contract initialization"); let start = Instant::now(); retry(ExponentialBackoff::default(), || async { let timed_out = start.elapsed().as_secs() > 60; - let expected_rpc_id = JsonRpcId::Number(rand::thread_rng().gen::()); let response = get_deploy( - expected_rpc_id.clone(), + JsonRpcId::Number(1), casper_node_rpc_url, casper_client_verbosity, deploy_hash, @@ -316,23 +307,19 @@ async fn deploy_contract( _ => backoff::Error::permanent(anyhow!(err)), })?; - if response.id == expected_rpc_id { - match response.result.execution_results.first() { - Some(result) => match &result.result { - ExecutionResult::Failure { error_message, .. } => { - Err(backoff::Error::permanent(anyhow!(error_message.clone()))) - } - ExecutionResult::Success { .. } => Ok(()), - }, - None if timed_out => Err(backoff::Error::permanent(anyhow!( - "Timeout on error: No execution results" - ))), - None => Err(backoff::Error::transient(anyhow!( - "No execution results there yet" - ))), - } - } else { - Err(backoff::Error::permanent(anyhow!("JSON RPC Id missmatch"))) + match response.result.execution_results.first() { + Some(result) => match &result.result { + ExecutionResult::Failure { error_message, .. } => { + Err(backoff::Error::permanent(anyhow!(error_message.clone()))) + } + ExecutionResult::Success { .. } => Ok(()), + }, + None if timed_out => Err(backoff::Error::permanent(anyhow!( + "Timeout on error: No execution results" + ))), + None => Err(backoff::Error::transient(anyhow!( + "No execution results there yet" + ))), } }) .await?; @@ -340,9 +327,8 @@ async fn deploy_contract( tracing::info!("Fetching deployed contract hash"); // Query global state - let expected_rpc_id = JsonRpcId::Number(rand::thread_rng().gen::()); let state_root_hash = get_state_root_hash( - expected_rpc_id.clone(), + JsonRpcId::Number(1), casper_node_rpc_url, casper_client_verbosity, Option::None, @@ -350,19 +336,14 @@ async fn deploy_contract( .await .map_err(Into::::into) .and_then(|response| { - if response.id == expected_rpc_id { - response - .result - .state_root_hash - .ok_or(anyhow!("No state root hash present in response")) - } else { - Err(anyhow!("JSON RPC Id missmatch")) - } + response + .result + .state_root_hash + .ok_or(anyhow!("No state root hash present in response")) })?; - let expected_rpc_id = JsonRpcId::Number(rand::thread_rng().gen::()); let account = get_account( - expected_rpc_id.clone(), + JsonRpcId::Number(1), casper_node_rpc_url, casper_client_verbosity, Option::None, @@ -370,18 +351,11 @@ async fn deploy_contract( ) .await .map_err(Into::::into) - .and_then(|response| { - if response.id == expected_rpc_id { - Ok(response.result.account) - } else { - Err(anyhow!("JSON RPC Id missmatch")) - } - })?; + .map(|response| response.result.account)?; - let expected_rpc_id = JsonRpcId::Number(rand::thread_rng().gen::()); let account_key = Key::Account(*account.account_hash()); let contract_hash: casper_client_types::ContractHash = query_global_state( - expected_rpc_id.clone(), + JsonRpcId::Number(1), casper_node_rpc_url, casper_client_verbosity, casper_client::rpcs::GlobalStateIdentifier::StateRootHash(state_root_hash), // fetches recent blocks state root hash @@ -390,22 +364,16 @@ async fn deploy_contract( ) .await .map_err(Into::::into) - .and_then(|response| { - if response.id == expected_rpc_id { - match response.result.stored_value { - StoredValue::ContractPackage(contract_package) => Ok(*contract_package - .versions() - .next() - .expect("Expected at least one contract version") - .contract_hash()), - other => Err(anyhow!( - "Unexpected result type, type is not a CLValue: {:?}", - other - )), - } - } else { - Err(anyhow!("JSON RPC Id missmatch")) - } + .and_then(|response| match response.result.stored_value { + StoredValue::ContractPackage(contract_package) => Ok(*contract_package + .versions() + .next() + .expect("Expected at least one contract version") + .contract_hash()), + other => Err(anyhow!( + "Unexpected result type, type is not a CLValue: {:?}", + other + )), })?; tracing::info!( "Successfully fetched the contract hash for {}: {}",