Skip to content

Commit

Permalink
Merge pull request #145 from cspr-rad/contract-hash-registry
Browse files Browse the repository at this point in the history
Implement a basic contract hash registry by serving the currently configured contract hash
  • Loading branch information
marijanp authored Jul 16, 2024
2 parents b7b8b33 + 41ded51 commit 374ca97
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 8 deletions.
18 changes: 18 additions & 0 deletions kairos-cli/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use axum_extra::routing::TypedPath;
use casper_client::types::{DeployBuilder, DeployHash, ExecutableDeployItem, TimeDiff, Timestamp};
use casper_client_types::{crypto::SecretKey, runtime_args, ContractHash, RuntimeArgs, U512};
use kairos_server::routes::contract_hash::ContractHashPath;
use kairos_server::routes::deposit::DepositPath;
use reqwest::Url;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -94,3 +95,20 @@ pub fn deposit(
.map_err(KairosClientError::from)
}
}

pub fn contract_hash(base_url: &Url) -> Result<ContractHash, KairosClientError> {
let response = reqwest::blocking::Client::new()
.get(base_url.join(ContractHashPath::PATH).unwrap())
.header("Content-Type", "application/json")
.send()
.map_err(KairosClientError::from)?;

let status = response.status();
if !status.is_success() {
Err(KairosClientError::KairosServerError(status.to_string()))
} else {
response
.json::<ContractHash>()
.map_err(KairosClientError::from)
}
}
9 changes: 7 additions & 2 deletions kairos-cli/src/commands/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ pub fn run(args: Args, kairos_server_address: Url) -> Result<String, CliError> {
error: err.to_string(),
})?;

let contract_hash_bytes = <[u8; 32]>::from_hex(contract_hash)?;
let contract_hash = ContractHash::new(contract_hash_bytes);
let contract_hash = match contract_hash {
Some(contract_hash_string) => {
let contract_hash_bytes = <[u8; 32]>::from_hex(contract_hash_string)?;
ContractHash::new(contract_hash_bytes)
}
None => client::contract_hash(&kairos_server_address)?,
};

client::deposit(
&kairos_server_address,
Expand Down
2 changes: 1 addition & 1 deletion kairos-cli/src/common/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct NonceArg {
#[derive(Args, Debug)]
pub struct ContractHashArg {
#[arg(id = "contract-hash", long, short = 'c', value_name = "CONTRACT_HASH")]
pub field: String,
pub field: Option<String>,
}

#[derive(Args, Debug)]
Expand Down
2 changes: 0 additions & 2 deletions kairos-cli/tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ async fn deposit_successful_with_ed25519() {
cmd.arg("--kairos-server-address")
.arg(kairos.url.as_str())
.arg("deposit")
.arg("--contract-hash")
.arg(contract_hash.to_string())
.arg("--amount")
.arg("123")
.arg("--private-key")
Expand Down
1 change: 1 addition & 0 deletions kairos-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub fn app_router(state: ServerState) -> Router {
.typed_post(routes::withdraw_handler)
.typed_post(routes::transfer_handler)
.typed_post(routes::deposit_mock_handler)
.typed_get(routes::contract_hash_handler)
.with_state(state)
}

Expand Down
18 changes: 18 additions & 0 deletions kairos-server/src/routes/contract_hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use axum::{extract::State, Json};
use axum_extra::routing::TypedPath;
use tracing::*;

use crate::{state::ServerState, AppErr};
use casper_client_types::ContractHash;

#[derive(TypedPath, Debug, Clone, Copy)]
#[typed_path("/api/v1/contract-hash")]
pub struct ContractHashPath;

#[instrument(level = "trace", skip(state), ret)]
pub async fn contract_hash_handler(
_: ContractHashPath,
state: State<ServerState>,
) -> Result<Json<ContractHash>, AppErr> {
Ok(Json(state.server_config.kairos_demo_contract_hash))
}
2 changes: 2 additions & 0 deletions kairos-server/src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
pub mod contract_hash;
pub mod deposit;
#[cfg(feature = "deposit-mock")]
pub mod deposit_mock;
pub mod transfer;
pub mod withdraw;

pub use contract_hash::contract_hash_handler;
pub use deposit::deposit_handler;
#[cfg(feature = "deposit-mock")]
pub use deposit_mock::deposit_mock_handler;
Expand Down
4 changes: 1 addition & 3 deletions nixos/tests/end-to-end.nix
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,13 @@ nixosTest {
# For more details, see cctl module implementation
client.succeed("wget --no-parent -r http://kairos/cctl/users/")
contract_hash = kairos.succeed("cat ${serverContractHashPath}")
kairos.succeed("casper-client get-node-status --node-address ${casperNodeAddress}")
# CLI with ed25519
# deposit
depositor = client.succeed("cat ${clientUsersDirectory}/user-2/public_key_hex")
depositor_private_key = "${clientUsersDirectory}/user-2/secret_key.pem"
deposit_deploy_hash = client.succeed("kairos-cli --kairos-server-address http://kairos deposit --amount 3000000000 --recipient {} --private-key {} --contract-hash {}".format(depositor, depositor_private_key, contract_hash))
deposit_deploy_hash = client.succeed("kairos-cli --kairos-server-address http://kairos deposit --amount 3000000000 --recipient {} --private-key {}".format(depositor, depositor_private_key))
assert int(deposit_deploy_hash, 16), "The deposit command did not output a hex encoded deploy hash. The output was {}".format(deposit_deploy_hash)
wait_for_successful_deploy(deposit_deploy_hash)
Expand Down

0 comments on commit 374ca97

Please sign in to comment.