Skip to content

Commit

Permalink
Work In Pieces
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaplas committed Jun 26, 2024
1 parent 8074b92 commit 007b8a5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 27 deletions.
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ test-livenet:
mkdir -p examples/.node-keys
cp modules/wasm/Erc20.wasm examples/wasm/
# Extract the secret keys from the local Casper node
docker exec mynctl /bin/bash -c "cat /home/casper/casper-node/utils/nctl/assets/net-1/users/user-1/secret_key.pem" > examples/.node-keys/secret_key.pem
docker exec mynctl /bin/bash -c "cat /home/casper/casper-node/utils/nctl/assets/net-1/users/user-2/secret_key.pem" > examples/.node-keys/secret_key_1.pem
docker exec mynctl /bin/bash -c "cat /home/casper/casper-nctl/assets/net-1/users/user-1/secret_key.pem" > examples/.node-keys/secret_key.pem
docker exec mynctl /bin/bash -c "cat /home/casper/casper-nctl/assets/net-1/users/user-2/secret_key.pem" > examples/.node-keys/secret_key_1.pem
# Run the tests
cd examples && ODRA_CASPER_LIVENET_SECRET_KEY_PATH=.node-keys/secret_key.pem ODRA_CASPER_LIVENET_NODE_ADDRESS=http://localhost:11101 ODRA_CASPER_LIVENET_CHAIN_NAME=casper-net-1 ODRA_CASPER_LIVENET_KEY_1=.node-keys/secret_key_1.pem cargo run --bin livenet_tests --features=livenet
rm -rf examples/.node-keys
Expand Down
56 changes: 37 additions & 19 deletions odra-casper/rpc-client/src/casper_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
pub mod configuration;

use std::{fs, path::PathBuf, str::from_utf8_unchecked, time::Duration};
use casper_client::cli::{get_state_root_hash, query_balance, query_global_state};
use casper_client::cli::{DeployStrParams, get_state_root_hash, make_deploy, make_transaction, query_balance, query_global_state, TransactionBuilderParams, TransactionStrParams};
use casper_client::{put_transaction, Verbosity};
use casper_client::rpcs::results::{PutDeployResult, QueryGlobalStateResult};

use itertools::Itertools;
Expand All @@ -11,7 +12,7 @@ use serde::de::DeserializeOwned;
use serde_json::{json, Value};
use tokio::runtime::Runtime;

use odra_core::casper_types::{DeployHash, Digest, ExecutableDeployItem, sign, URef};
use odra_core::casper_types::{DeployHash, Digest, ExecutableDeployItem, sign, Transaction, TransactionCategory, URef};
use odra_core::{
casper_types::{
bytesrepr::{Bytes, FromBytes, ToBytes},
Expand Down Expand Up @@ -407,30 +408,47 @@ impl CasperClient {
// ContractHash::from_formatted_str(contract_hash).unwrap()
}


/// Deploy the contract.
pub fn deploy_wasm(&self, contract_name: &str, args: RuntimeArgs) -> Address {
log::info(format!("Deploying \"{}\".", contract_name));
let wasm_path = find_wasm_file_path(contract_name);
let wasm_bytes = fs::read(wasm_path).unwrap();
let session = ExecutableDeployItem::ModuleBytes {
module_bytes: Bytes::from(wasm_bytes),
args
let transaction_builder_params = TransactionBuilderParams::Session {
transaction_bytes: wasm_bytes.into(),
transaction_category: TransactionCategory::InstallUpgrade,
};
let deploy = self.new_deploy(session, self.gas);
let request = json!(
{
"jsonrpc": "2.0",
"method": "account_put_deploy",
"params": {
"deploy": deploy
},
"id": 1,
}
);

let response: PutDeployResult = self.post_request(request);
let deploy_hash = response.deploy_hash;
self.wait_for_deploy_hash(deploy_hash);
let secret_key = self.configuration.secret_key_paths.first();
let secret_key = secret_key.unwrap().to_string();

let gas = self.gas.to_string();

let transaction_str_params = TransactionStrParams {
secret_key: secret_key.as_str(),
timestamp: "",
ttl: "30min",
chain_name: self.configuration.chain_name.as_str(),
initiator_addr: "".to_string(),
session_args_simple: vec![],
session_args_json: "",
pricing_mode: "classic",
output_path: "",
payment_amount: gas.as_str(),
gas_price_tolerance: "1.0",
receipt: "",
standard_payment: "",
};

let transaction = make_transaction(transaction_builder_params, transaction_str_params, true).unwrap();
let transaction = Transaction::V1(transaction);
let response =
put_transaction(self.configuration.rpc_id.clone(), self.configuration.node_address.as_str(), Verbosity::High, transaction);

let rt = Runtime::new().unwrap();
let result = rt.block_on(response).unwrap();
dbg!(result);
// self.wait_for_deploy_hash(deploy_hash);

let address = self.get_contract_address(contract_name);
log::info(format!("Contract {:?} deployed.", &address.to_string()));
Expand Down
21 changes: 15 additions & 6 deletions odra-casper/rpc-client/src/casper_client/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ use crate::casper_client::{
};
use odra_core::casper_types::SecretKey;
use std::path::PathBuf;
use casper_client::JsonRpcId;
use crate::utils::get_env_variable;

#[derive(Debug)]
pub struct CasperClientConfiguration {
pub node_address: String,
pub rpc_id: JsonRpcId,
pub chain_name: String,
pub secret_keys: Vec<SecretKey>
pub secret_keys: Vec<SecretKey>,
pub secret_key_paths: Vec<String>,
}

impl CasperClientConfiguration {
Expand All @@ -27,35 +30,41 @@ impl CasperClientConfiguration {

let node_address = get_env_variable(ENV_NODE_ADDRESS);
let chain_name = get_env_variable(ENV_CHAIN_NAME);
let secret_keys = Self::secret_keys_from_env();
let (secret_keys, secret_key_paths) = Self::secret_keys_from_env();
CasperClientConfiguration {
node_address,
rpc_id: JsonRpcId::String("1".to_string()),
chain_name,
secret_keys
secret_keys,
secret_key_paths
}
}

/// Loads secret keys from ENV_SECRET_KEY file and ENV_ACCOUNT_PREFIX files.
/// e.g. ENV_SECRET_KEY=secret_key.pem, ENV_ACCOUNT_PREFIX=account_1_key.pem
/// This will load secret_key.pem as account 0 and account_1_key.pem as account 1.
fn secret_keys_from_env() -> Vec<SecretKey> {
fn secret_keys_from_env() -> (Vec<SecretKey>, Vec<String>) {
let mut secret_keys = vec![];
let mut secret_key_paths = vec![];
let file_name = get_env_variable(ENV_SECRET_KEY);
secret_keys.push(
SecretKey::from_file(get_env_variable(ENV_SECRET_KEY)).unwrap_or_else(|_| {
SecretKey::from_file(file_name.clone()).unwrap_or_else(|_| {
panic!(
"Couldn't load secret key from file {:?}",
get_env_variable(ENV_SECRET_KEY)
)
})
);
secret_key_paths.push(file_name);

let mut i = 1;
while let Ok(key_filename) = std::env::var(format!("{}{}", ENV_ACCOUNT_PREFIX, i)) {
secret_keys.push(SecretKey::from_file(&key_filename).unwrap_or_else(|_| {
panic!("Couldn't load secret key from file {:?}", key_filename)
}));
secret_key_paths.push(key_filename);
i += 1;
}
secret_keys
(secret_keys, secret_key_paths)
}
}

0 comments on commit 007b8a5

Please sign in to comment.