Skip to content

Commit

Permalink
Add CLI commands
Browse files Browse the repository at this point in the history
  • Loading branch information
xqft committed Aug 20, 2024
1 parent cd28a42 commit 605439c
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 78 deletions.
55 changes: 43 additions & 12 deletions contract_deployer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contract_deployer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ethers = { tag = "v2.0.15-fix-reconnections", features = [
mina-curves = { git = "https://github.com/openmina/proof-systems", branch = "ledger-newtypes-rampup4-vrf" }
o1-utils = { git = "https://github.com/lambdaclass/proof-systems", branch = "add-verifier-serializations" }
kimchi = { git = "https://github.com/openmina/proof-systems", branch = "ledger-newtypes-rampup4-vrf" }
aligned-sdk = { git = "https://github.com/lambdaclass/aligned_layer.git", branch = "mina" }
aligned-sdk = { git = "https://github.com/lambdaclass/aligned_layer.git", branch = "account_inclusion_verifier" }

[patch.crates-io]
ark-ff = { git = "https://github.com/openmina/algebra", branch = "openmina" }
Expand Down
41 changes: 41 additions & 0 deletions core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ graphql_client = { version = "0.14.0", features = [
"reqwest-blocking",
] }
alloy = { version = "0.2.1", features = ["full", "signer-keystore"] }
clap = { version = "4.5.16", features = ["derive"] }

[patch.crates-io]
ark-ff = { git = "https://github.com/openmina/algebra", branch = "openmina" }
Expand Down
146 changes: 86 additions & 60 deletions core/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
use clap::{Parser, Subcommand};
use core::{
aligned_polling_service, mina_polling_service, smart_contract_utility,
utils::{env::EnvironmentVariables, wallet::get_wallet},
};
use kimchi::turshi::helper::CairoFieldHelpers;
use log::{debug, error, info};
use log::{error, info};
use mina_p2p_messages::v2::StateHash;
use std::{process, time::SystemTime};

#[derive(Parser)]
#[command(version, about)]
struct Cli {
#[command(subcommand)]
command: Command,
#[arg(short, long)]
save_proof: bool,
}

#[derive(Subcommand)]
enum Command {
SubmitState,
SubmitAccount {
/// Write the proof into .proof and .pub files.
public_key: String,
},
}

#[tokio::main]
async fn main() {
let cli = Cli::parse();
let now = SystemTime::now();
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();

debug!("Reading env. variables");
let EnvironmentVariables {
rpc_url,
chain,
Expand All @@ -21,85 +40,92 @@ async fn main() {
proof_generator_addr,
keystore_path,
private_key,
save_proof,
} = EnvironmentVariables::new().unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
});

debug!("Getting user wallet");
let wallet = get_wallet(&chain, keystore_path.as_deref(), private_key.as_deref())
.unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
});

debug!("Executing Mina polling service");
let mina_proof = mina_polling_service::get_mina_proof_of_state(
&rpc_url,
&proof_generator_addr,
&chain,
&eth_rpc_url,
)
.await
.unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
});

if save_proof {
std::fs::write(
"./protocol_state.pub",
mina_proof.pub_input.as_ref().unwrap_or_else(|| {
error!("Tried to save public inputs to file but they're missing");
match cli.command {
Command::SubmitState => {
let mina_proof = mina_polling_service::get_mina_proof_of_state(
&rpc_url,
&proof_generator_addr,
&chain,
&eth_rpc_url,
)
.await
.unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
}),
)
.unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
});
std::fs::write("./protocol_state.proof", &mina_proof.proof).unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
});
}
});

debug!("Executing Aligned polling service");
let verification_data = aligned_polling_service::submit(
&mina_proof,
&chain,
&batcher_addr,
&batcher_eth_addr,
&eth_rpc_url,
wallet.clone(),
)
.await
.unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
});
if cli.save_proof {
std::fs::write(
"./protocol_state.pub",
mina_proof.pub_input.as_ref().unwrap_or_else(|| {
error!("Tried to save public inputs to file but they're missing");
process::exit(1);
}),
)
.unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
});
std::fs::write("./protocol_state.proof", &mina_proof.proof).unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
});
}

debug!("Updating the bridge's smart contract");
let pub_input = mina_proof.pub_input.unwrap_or_else(|| {
error!("Missing public inputs from Mina proof");
process::exit(1);
});
let verification_data = aligned_polling_service::submit(
&mina_proof,
&chain,
&batcher_addr,
&batcher_eth_addr,
&eth_rpc_url,
wallet.clone(),
)
.await
.unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
});

let verified_state_hash =
smart_contract_utility::update(verification_data, pub_input, &chain, &eth_rpc_url, wallet)
let pub_input = mina_proof.pub_input.unwrap_or_else(|| {
error!("Missing public inputs from Mina proof");
process::exit(1);
});

let verified_state_hash = smart_contract_utility::update(
verification_data,
pub_input,
&chain,
&eth_rpc_url,
wallet,
)
.await
.unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
});

info!(
"Success! verified Mina state hash 0x{} was stored in the bridge's smart contract",
verified_state_hash.to_hex_be()
);
info!(
"Success! verified Mina state hash {} was stored in the bridge's smart contract",
StateHash::from_fp(verified_state_hash)
);
}
Command::SubmitAccount { public_key } => {
todo!()
}
}

if let Ok(elapsed) = now.elapsed() {
info!("Time spent: {} ms", elapsed.as_millis());
info!("Time spent: {} s", elapsed.as_secs());
}
}
5 changes: 0 additions & 5 deletions core/src/utils/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub struct EnvironmentVariables {
pub proof_generator_addr: String,
pub keystore_path: Option<String>,
pub private_key: Option<String>,
pub save_proof: bool,
}

fn load_var_or(key: &str, default: &str, chain: &Chain) -> Result<String, String> {
Expand Down Expand Up @@ -74,9 +73,6 @@ impl EnvironmentVariables {
);
}

let save_proof =
matches!(std::env::var("SAVE_PROOF"), Ok(value) if value.to_lowercase() == "true");

Ok(EnvironmentVariables {
rpc_url,
chain,
Expand All @@ -86,7 +82,6 @@ impl EnvironmentVariables {
proof_generator_addr,
keystore_path,
private_key,
save_proof,
})
}
}

0 comments on commit 605439c

Please sign in to comment.