Skip to content

Commit

Permalink
Parameterize state and account contract addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielbosio committed Sep 27, 2024
1 parent 21071a0 commit b163510
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 85 deletions.
2 changes: 2 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
ETH_CHAIN=<devnet/holesky>
MINA_RPC_URL=<url>
STATE_SETTLEMENT_ETH_ADDR=<address>
ACCOUNT_VALIDATION_ETH_ADDR=<address>
SAVE_PROOF=true/false # also false if other than "true" or variable were to be not defined

## These can be skipped if using devnet with Anvil.
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
.PHONY: submit-state submit-account gen_contract_abi deploy_contract
.PHONY: submit_mainnet_state submit_devnet_state submit_account gen_contract_abi deploy_contract

submit_state:
submit_mainnet_state:
@cargo run --manifest-path core/Cargo.toml --release -- submit-state

submit_devnet_state:
@cargo run --manifest-path core/Cargo.toml --release -- submit-state --devnet

submit_account:
@cargo run --manifest-path core/Cargo.toml --release -- submit-account ${PUBLIC_KEY} ${STATE_HASH}

Expand Down
14 changes: 11 additions & 3 deletions contract_deployer/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use aligned_sdk::core::types::Chain;
use log::{debug, error, info};
use mina_bridge_core::{
eth::{
Expand All @@ -6,8 +7,9 @@ use mina_bridge_core::{
},
mina::query_root,
utils::{
constants::BRIDGE_TRANSITION_FRONTIER_LEN, contract::get_aligned_sm_contract_addr,
env::EnvironmentVariables, wallet_alloy::get_wallet,
constants::{ALIGNED_SM_DEVNET_ETH_ADDR, BRIDGE_TRANSITION_FRONTIER_LEN},
env::EnvironmentVariables,
wallet_alloy::get_wallet,
},
};
use std::process;
Expand Down Expand Up @@ -43,7 +45,13 @@ async fn main() {
process::exit(1);
});

let aligned_sm_addr = get_aligned_sm_contract_addr(&chain).unwrap_or_else(|err| {
let aligned_sm_addr = match chain {
Chain::Devnet => Ok(ALIGNED_SM_DEVNET_ETH_ADDR.to_owned()),
Chain::Holesky => std::env::var("ALIGNED_SM_HOLESKY_ETH_ADDR")
.map_err(|err| format!("Error getting Aligned SM contract address: {err}")),
_ => Err("Unimplemented Ethereum contract on selected chain".to_owned()),
}
.unwrap_or_else(|err| {
error!("{err}");
process::exit(1);
});
Expand Down
47 changes: 25 additions & 22 deletions core/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ use serde_with::serde_as;
use crate::{
proof::{account_proof::MinaAccountPubInputs, state_proof::MinaStatePubInputs},
sol::serialization::SolSerialize,
utils::{
constants::{ANVIL_CHAIN_ID, BRIDGE_TRANSITION_FRONTIER_LEN, HOLESKY_CHAIN_ID},
contract::{get_account_validation_contract_addr, get_bridge_contract_addr},
},
utils::constants::{ANVIL_CHAIN_ID, BRIDGE_TRANSITION_FRONTIER_LEN, HOLESKY_CHAIN_ID},
};

abigen!(
Expand Down Expand Up @@ -97,10 +94,10 @@ pub async fn update_chain(
chain: &Chain,
eth_rpc_url: &str,
wallet: Wallet<SigningKey>,
contract_addr: &str,
batcher_payment_service: &str,
) -> Result<(), String> {
let bridge_eth_addr =
Address::from_str(&get_bridge_contract_addr(chain)?).map_err(|err| err.to_string())?;
let bridge_eth_addr = Address::from_str(contract_addr).map_err(|err| err.to_string())?;

let serialized_pub_input = bincode::serialize(pub_input)
.map_err(|err| format!("Failed to serialize public inputs: {err}"))?;
Expand Down Expand Up @@ -173,7 +170,7 @@ pub async fn update_chain(

// TODO(xqft): do the same for ledger hashes
debug!("Getting chain state hashes");
let new_chain_state_hashes = get_bridge_chain_state_hashes(chain, eth_rpc_url)
let new_chain_state_hashes = get_bridge_chain_state_hashes(contract_addr, eth_rpc_url)
.await
.map_err(|err| err.to_string())?;

Expand All @@ -190,9 +187,11 @@ pub async fn update_chain(
Ok(())
}

pub async fn get_bridge_tip_hash(chain: &Chain, eth_rpc_url: &str) -> Result<SolStateHash, String> {
let bridge_eth_addr =
Address::from_str(&get_bridge_contract_addr(chain)?).map_err(|err| err.to_string())?;
pub async fn get_bridge_tip_hash(
contract_addr: &str,
eth_rpc_url: &str,
) -> Result<SolStateHash, String> {
let bridge_eth_addr = Address::from_str(contract_addr).map_err(|err| err.to_string())?;

debug!("Creating contract instance");
let mina_bridge_contract = mina_bridge_contract_call_only(eth_rpc_url, bridge_eth_addr)?;
Expand All @@ -210,11 +209,10 @@ pub async fn get_bridge_tip_hash(chain: &Chain, eth_rpc_url: &str) -> Result<Sol
}

pub async fn get_bridge_chain_state_hashes(
chain: &Chain,
contract_addr: &str,
eth_rpc_url: &str,
) -> Result<[StateHash; BRIDGE_TRANSITION_FRONTIER_LEN], String> {
let bridge_eth_addr =
Address::from_str(&get_bridge_contract_addr(chain)?).map_err(|err| err.to_string())?;
let bridge_eth_addr = Address::from_str(contract_addr).map_err(|err| err.to_string())?;

debug!("Creating contract instance");
let mina_bridge_contract = mina_bridge_contract_call_only(eth_rpc_url, bridge_eth_addr)?;
Expand Down Expand Up @@ -243,12 +241,11 @@ pub async fn get_bridge_chain_state_hashes(
pub async fn validate_account(
verification_data: AlignedVerificationData,
pub_input: &MinaAccountPubInputs,
chain: &Chain,
eth_rpc_url: &str,
contract_addr: &str,
batcher_payment_service: &str,
) -> Result<(), String> {
let bridge_eth_addr = Address::from_str(&get_account_validation_contract_addr(chain)?)
.map_err(|err| err.to_string())?;
let bridge_eth_addr = Address::from_str(contract_addr).map_err(|err| err.to_string())?;

debug!("Creating contract instance");

Expand Down Expand Up @@ -331,14 +328,19 @@ pub async fn deploy_mina_bridge_contract(
.map_err(|err| err.to_string())?;
let address = contract.address();

let network = if is_state_proof_from_devnet {
"Devnet"
} else {
"Mainnet"
};

info!(
"Mina {} Bridge contract successfuly deployed with address {}",
if is_state_proof_from_devnet {
"Devnet"
} else {
"Mainnet"
},
address
network, address
);
info!(
"Set STATE_SETTLEMENT_ETH_ADDR={} if using Mina {}",
address, network
);

Ok(*address)
Expand Down Expand Up @@ -366,6 +368,7 @@ pub async fn deploy_mina_account_validation_contract(
"Mina Account Validation contract successfuly deployed with address {}",
address
);
info!("Set ACCOUNT_VALIDATION_ETH_ADDR={}", address);

Ok(*address)
}
Expand Down
32 changes: 24 additions & 8 deletions core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ async fn main() {
let EnvironmentVariables {
rpc_url,
chain,
state_settlement_addr,
account_validation_addr,
batcher_addr,
batcher_eth_addr,
eth_rpc_url,
Expand All @@ -54,6 +56,15 @@ async fn main() {
process::exit(1);
});

let state_settlement_addr = state_settlement_addr.unwrap_or_else(|| {
error!("Error getting State settlement contract address");
process::exit(1);
});
let account_validation_addr = account_validation_addr.unwrap_or_else(|| {
error!("Error getting Account validation contract address");
process::exit(1);
});

let wallet = get_wallet(&chain, keystore_path.as_deref(), private_key.as_deref())
.unwrap_or_else(|err| {
error!("{}", err);
Expand All @@ -62,13 +73,17 @@ async fn main() {

match cli.command {
Command::SubmitState { devnet, save_proof } => {
let (proof, pub_input) =
mina::get_mina_proof_of_state(&rpc_url, &chain, &eth_rpc_url, devnet)
.await
.unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
});
let (proof, pub_input) = mina::get_mina_proof_of_state(
&rpc_url,
&eth_rpc_url,
&state_settlement_addr,
devnet,
)
.await
.unwrap_or_else(|err| {
error!("{}", err);
process::exit(1);
});

let verification_data = aligned::submit(
MinaProof::State((proof, pub_input.clone())),
Expand All @@ -92,6 +107,7 @@ async fn main() {
&chain,
&eth_rpc_url,
wallet,
&state_settlement_addr,
&batcher_eth_addr,
)
.await
Expand Down Expand Up @@ -132,8 +148,8 @@ async fn main() {
if let Err(err) = eth::validate_account(
verification_data,
&pub_input,
&chain,
&eth_rpc_url,
&account_validation_addr,
&batcher_eth_addr,
)
.await
Expand Down
5 changes: 2 additions & 3 deletions core/src/mina.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::str::FromStr;

use aligned_sdk::core::types::Chain;
use alloy_sol_types::SolValue;
use base64::prelude::*;
use futures::future::join_all;
Expand Down Expand Up @@ -59,11 +58,11 @@ struct AccountQuery;

pub async fn get_mina_proof_of_state(
rpc_url: &str,
chain: &Chain,
eth_rpc_url: &str,
contract_addr: &str,
is_state_proof_from_devnet: bool,
) -> Result<(MinaStateProof, MinaStatePubInputs), String> {
let bridge_tip_state_hash = get_bridge_tip_hash(chain, eth_rpc_url).await?.0;
let bridge_tip_state_hash = get_bridge_tip_hash(contract_addr, eth_rpc_url).await?.0;
let (
candidate_chain_states,
candidate_chain_state_hashes,
Expand Down
31 changes: 23 additions & 8 deletions core/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,21 @@ pub struct AccountVerificationData {

pub async fn is_state_verified(
hash: &str,
chain: &Chain,
state_settlement_addr: &str,
eth_rpc_url: &str,
) -> Result<bool, String> {
let chain_state_hashes = get_bridge_chain_state_hashes(chain, eth_rpc_url).await?;
let chain_state_hashes =
get_bridge_chain_state_hashes(state_settlement_addr, eth_rpc_url).await?;
let hash = StateHash::from_str(hash)
.map_err(|err| format!("Failed to convert hash string to state hash: {err}"))?;
Ok(chain_state_hashes.contains(&hash))
}

pub async fn get_bridged_chain_tip_state_hash(
chain: &Chain,
state_settlement_addr: &str,
eth_rpc_url: &str,
) -> Result<String, String> {
get_bridge_chain_state_hashes(chain, eth_rpc_url)
get_bridge_chain_state_hashes(state_settlement_addr, eth_rpc_url)
.await
.map(|hashes| hashes.last().unwrap().to_string())
}
Expand All @@ -47,6 +48,7 @@ pub async fn get_bridged_chain_tip_state_hash(
pub async fn update_bridge_chain(
rpc_url: &str,
chain: &Chain,
state_settlement_addr: &str,
batcher_addr: &str,
batcher_eth_addr: &str,
eth_rpc_url: &str,
Expand All @@ -56,11 +58,22 @@ pub async fn update_bridge_chain(
is_state_proof_from_devnet: bool,
save_proof: bool,
) -> Result<(), String> {
let (proof, pub_input) =
get_mina_proof_of_state(rpc_url, chain, eth_rpc_url, is_state_proof_from_devnet).await?;
let (proof, pub_input) = get_mina_proof_of_state(
rpc_url,
eth_rpc_url,
state_settlement_addr,
is_state_proof_from_devnet,
)
.await?;

let candidate_root_state_hash = pub_input.candidate_chain_state_hashes.first().unwrap();
if is_state_verified(&candidate_root_state_hash.to_string(), chain, eth_rpc_url).await? {
if is_state_verified(
&candidate_root_state_hash.to_string(),
state_settlement_addr,
eth_rpc_url,
)
.await?
{
debug!("Candidate root {candidate_root_state_hash} is verified, so the bridge chain is updated");
return Err("Latest chain is already verified".to_string());
}
Expand All @@ -83,6 +96,7 @@ pub async fn update_bridge_chain(
chain,
eth_rpc_url,
wallet,
state_settlement_addr,
batcher_payment_service,
)
.await?;
Expand All @@ -96,6 +110,7 @@ pub async fn validate_account(
state_hash: &str,
rpc_url: &str,
chain: &Chain,
account_validation_addr: &str,
batcher_addr: &str,
batcher_eth_addr: &str,
eth_rpc_url: &str,
Expand All @@ -121,8 +136,8 @@ pub async fn validate_account(
eth::validate_account(
verification_data.clone(),
&pub_input,
chain,
eth_rpc_url,
account_validation_addr,
batcher_payment_service,
)
.await?;
Expand Down
32 changes: 0 additions & 32 deletions core/src/utils/contract.rs

This file was deleted.

Loading

0 comments on commit b163510

Please sign in to comment.