Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Built-in customizable deposit WASM #160

Merged
merged 8 commits into from
Jul 26, 2024
20 changes: 20 additions & 0 deletions kairos-cli/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::env;
use std::fs;
use std::path::Path;

fn main() {
// Path
let session_binaries_dir = env::var("PATH_TO_SESSION_BINARIES")
.expect("PATH_TO_SESSION_BINARIES environment variable is not set");

// Get the output directory set by Cargo.
let out_dir = env::var("OUT_DIR").expect("OUT_DIR not set");
let source_path = Path::new(&session_binaries_dir).join("deposit-session-optimized.wasm");
let dest_path = Path::new(&out_dir).join("deposit-session-optimized.wasm");

// Copy the file from the source to the destination
fs::copy(&source_path, dest_path).expect("Failed to copy WASM file");

// Print out a message to re-run this script if the source file changes.
println!("cargo:rerun-if-changed={}", source_path.display());
Comment on lines +7 to +19
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: This works, but I am not clear why we need to copy the wasm into OUT_DIR. Why not just include_bytes from PATH_TO_SESSION_BINARIES directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include_bytes!(env!("PATH_TO_SESSION_BINARIES")) would work too. However, I would like to extend build script to automatically discover and optimize WASMs (similar to this) so PATH_TO_SESSION_BINARIES can be avoided.

The whole project works great in Nix, but being able to just cargo build without bunch of extra env setup would be better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good

}
11 changes: 1 addition & 10 deletions kairos-cli/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use kairos_server::PublicKey;
use reqwest::Url;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::fs;
use std::path::Path;

// max amount allowed to be used on gas fees
pub const MAX_GAS_FEE_PAYMENT_AMOUNT: u64 = 1_000_000_000_000;
Expand Down Expand Up @@ -49,19 +47,12 @@ impl From<reqwest::Error> for KairosClientError {

pub fn deposit(
base_url: &Url,
deposit_session_wasm_bytes: &[u8],
depositor_secret_key: &SecretKey,
contract_hash: &ContractHash,
amount: impl Into<U512>,
recipient: casper_client_types::PublicKey,
) -> Result<DeployHash, KairosClientError> {
let deposit_session_wasm_path =
Path::new(env!("PATH_TO_SESSION_BINARIES")).join("deposit-session-optimized.wasm");
let deposit_session_wasm_bytes = fs::read(&deposit_session_wasm_path).unwrap_or_else(|err| {
panic!(
"Failed to read the deposit session wasm as bytes from file: {:?}.\n{}",
deposit_session_wasm_path, err
)
});
let deposit_session = ExecutableDeployItem::new_module_bytes(
deposit_session_wasm_bytes.into(),
runtime_args! {
Expand Down
35 changes: 34 additions & 1 deletion kairos-cli/src/commands/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
use std::fs;
use std::path::PathBuf;

use crate::client;
use crate::common::args::{AmountArg, ContractHashArg, PrivateKeyPathArg, RecipientArg};
use crate::error::CliError;

use casper_client_types::{crypto::SecretKey, ContractHash};
use clap::Parser;
use clap::{Args as ClapArgs, Parser};
use hex::FromHex;
use reqwest::Url;

use kairos_crypto::error::CryptoError;

const DEFAULT_DEPOSIT_SESSION_WASM: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/deposit-session-optimized.wasm"));

#[derive(Parser, Debug)]
pub struct Args {
#[clap(flatten)]
Expand All @@ -19,6 +25,8 @@ pub struct Args {
contract_hash: ContractHashArg,
#[clap(flatten)]
recipient: RecipientArg,
#[clap(flatten)]
session_path: SessionPathArg,
}

pub fn run(args: Args, kairos_server_address: Url) -> Result<String, CliError> {
Expand All @@ -38,8 +46,21 @@ pub fn run(args: Args, kairos_server_address: Url) -> Result<String, CliError> {
None => client::contract_hash(&kairos_server_address)?,
};

let deposit_session_wasm: Vec<u8> = match args.session_path.field {
Some(deposit_session_wasm_path) => {
fs::read(&deposit_session_wasm_path).unwrap_or_else(|err| {
panic!(
"Failed to read the deposit session wasm as bytes from file: {:?}.\n{}",
deposit_session_wasm_path, err
)
})
}
None => DEFAULT_DEPOSIT_SESSION_WASM.to_vec(),
};

client::deposit(
&kairos_server_address,
&deposit_session_wasm,
&depositor_secret_key,
&contract_hash,
amount,
Expand All @@ -59,3 +80,15 @@ pub fn run(args: Args, kairos_server_address: Url) -> Result<String, CliError> {
output
})
}

#[derive(ClapArgs, Debug)]
pub struct SessionPathArg {
#[arg(
id = "session-path",
long,
short = 's',
value_name = "PATH",
help = "Path to the custom WASM session code for deposit"
)]
pub field: Option<PathBuf>,
}
Loading