Skip to content

Commit

Permalink
update contract-deploy logic and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marijanp committed Sep 6, 2024
1 parent 8fc056e commit b43b3bc
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 30 deletions.
41 changes: 19 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ use std::{
use tempfile::tempdir;

use casper_client::{
get_account, get_deploy, get_state_root_hash, put_deploy, query_global_state,
rpcs::AccountIdentifier, Error, JsonRpcId, Verbosity,
get_deploy, get_state_root_hash, put_deploy, query_global_state, Error, JsonRpcId, Verbosity,
};
use casper_types::{
account::AccountHash,
contracts::ContractHash,
execution::{execution_result_v1::ExecutionResultV1, ExecutionResult},
Key, PublicKey, RuntimeArgs, SecretKey,
DeployBuilder, ExecutableDeployItem, Key, PublicKey, RuntimeArgs, SecretKey, StoredValue,
TimeDiff, Timestamp,
};
use casper_types::{DeployBuilder, ExecutableDeployItem, StoredValue, TimeDiff, Timestamp};

use parsers::RawNodeType;

Expand Down Expand Up @@ -143,8 +143,6 @@ impl CCTLNetwork {
tracing::info!("{}", output);
let (_, nodes) = parsers::parse_cctl_infra_net_start_lines(output).unwrap();

// Match the started nodes and sidecars with their respective ports

tracing::info!("Fetching the networks node ports");
let output = Command::new("cctl-infra-node-view-ports")
.env("CCTL_ASSETS", &assets_dir)
Expand All @@ -163,6 +161,8 @@ impl CCTLNetwork {
tracing::info!("{}", output);
let (_, sidecar_ports) = parsers::parse_cctl_infra_sidecar_view_port_lines(output).unwrap();

// Match the started nodes and sidecars with their respective ports

let (casper_nodes, casper_sidecars): (Vec<CasperNode>, Vec<CasperSidecar>) =
nodes.iter().partition_map(|node_type| match node_type {
RawNodeType::CasperNode(validator_group_id, node_id, state) => {
Expand Down Expand Up @@ -221,7 +221,7 @@ impl CCTLNetwork {
let (hash_name, contract_hash) = deploy_contract(
&casper_sidecar_rpc_url,
&deployer_skey,
&deployer_pkey,
&deployer_pkey.to_account_hash(),
&contract_to_deploy,
)
.await?;
Expand Down Expand Up @@ -268,15 +268,15 @@ impl Drop for CCTLNetwork {
async fn deploy_contract(
casper_node_rpc_url: &str,
contract_deployer_skey: &SecretKey,
contract_deployer_pkey: &PublicKey,
contract_deployer_addr: &AccountHash,
DeployableContract {
hash_name,
runtime_args,
path,
}: &DeployableContract,
) -> anyhow::Result<(String, ContractHash)> {
tracing::info!(
"Deploying contract {}: {}",
"Deploying contract '{}': {}",
&hash_name,
path.to_str().unwrap()
);
Expand Down Expand Up @@ -386,29 +386,26 @@ async fn deploy_contract(
.ok_or(anyhow!("No state root hash present in response"))
})?;

let account = get_account(
JsonRpcId::Number(1),
casper_node_rpc_url,
casper_client_verbosity,
Option::None,
AccountIdentifier::PublicKey(contract_deployer_pkey.clone()),
)
.await
.map_err(Into::<anyhow::Error>::into)
.map(|response| response.result.account)?;

let account_key = Key::Account(account.account_hash());
tracing::info!("Querying global state");
let contract_hash: ContractHash = query_global_state(
JsonRpcId::Number(1),
casper_node_rpc_url,
casper_client_verbosity,
casper_client::rpcs::GlobalStateIdentifier::StateRootHash(state_root_hash), // fetches recent blocks state root hash
account_key,
Key::AddressableEntity(casper_types::EntityAddr::Account(contract_deployer_addr.0)),
vec![hash_name.clone()],
)
.await
.map_err(Into::<anyhow::Error>::into)
.and_then(|response| match response.result.stored_value {
StoredValue::Package(package) => Ok(ContractHash::from(
package
.versions()
.contract_hashes()
.next()
.expect("")
.value(),
)),
StoredValue::ContractPackage(contract_package) => Ok(*contract_package
.versions()
.values()
Expand Down
Binary file removed test-resources/demo-contract-optimized.wasm
Binary file not shown.
Binary file added test-resources/dummy-contract.wasm
Binary file not shown.
8 changes: 4 additions & 4 deletions tests/test_cctl_deploys_a_contract_successfully.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ fn tracing_init() {
async fn test_cctl_deploys_a_contract_successfully() {
tracing_init();

let contract_wasm_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("test-resources/demo-contract-optimized.wasm");
let hash_name = "kairos_contract_package_hash";
let contract_wasm_path =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-resources/dummy-contract.wasm");
let hash_name = "contract-hash";
let contract_to_deploy = DeployableContract {
hash_name: hash_name.to_string(),
runtime_args: runtime_args! { "initial_trie_root" => Option::<[u8; 32]>::None },
runtime_args: runtime_args! {},
path: contract_wasm_path,
};

Expand Down
8 changes: 4 additions & 4 deletions tests/test_cctl_network_starts_and_terminates.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

use casper_client::{get_node_status, JsonRpcId, Verbosity};
use casper_client::{get_peers, JsonRpcId, Verbosity};
use cctl::{CCTLNetwork, NodeState};

fn tracing_init() {
Expand All @@ -18,14 +18,14 @@ async fn test_cctl_network_starts_and_terminates() {

for node in &network.casper_sidecars {
if node.state == NodeState::Running {
let node_status = get_node_status(
let node_status = get_peers(
JsonRpcId::Number(1),
&format!("http://0.0.0.0:{}", node.port.rpc_port),
&format!("http://0.0.0.0:{}/rpc", node.port.rpc_port),
Verbosity::High,
)
.await
.unwrap();
assert_eq!(node_status.result.reactor_state, "Validate");
assert!(!node_status.result.peers.is_empty());
}
}
}

0 comments on commit b43b3bc

Please sign in to comment.