Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Commit

Permalink
fix: deploy_verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
brech1 committed Jul 13, 2023
1 parent 93296b4 commit 0f0209e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 57 deletions.
63 changes: 24 additions & 39 deletions client/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use crate::{
error::EigenError,
fs::{get_assets_path, get_file_path, read_yul, write_binary, FileType},
fs::{get_assets_path, get_file_path, read_yul, FileType},
ClientSigner,
};
use eigen_trust_circuit::{
Expand All @@ -15,7 +15,7 @@ use eigen_trust_circuit::{
};
use ethers::{
abi::Address,
prelude::{k256::ecdsa::SigningKey, Abigen, ContractError, ContractFactory},
prelude::{k256::ecdsa::SigningKey, Abigen, ContractFactory},
providers::Middleware,
signers::coins_bip39::{English, Mnemonic},
solc::{artifacts::ContractBytecode, Artifact, Solc},
Expand All @@ -24,10 +24,7 @@ use ethers::{
};
use log::info;
use secp256k1::SecretKey;
use std::{
fs::{read_dir, write},
sync::Arc,
};
use std::{fs::write, sync::Arc};

/// Deploys the AttestationStation contract.
pub async fn deploy_as(signer: Arc<ClientSigner>) -> Result<Address, EigenError> {
Expand Down Expand Up @@ -59,18 +56,6 @@ pub async fn deploy_as(signer: Arc<ClientSigner>) -> Result<Address, EigenError>
address.ok_or(EigenError::ParseError)
}

/// Deploys the EtVerifier contract.
pub async fn deploy_verifier(
signer: Arc<ClientSigner>, contract_bytes: Vec<u8>,
) -> Result<Address, ContractError<ClientSigner>> {
let tx = TransactionRequest::default().data(contract_bytes);
let pen_tx = signer.send_transaction(tx, None).await.unwrap();
let tx = pen_tx.await;

let rec = tx.unwrap().unwrap();
Ok(rec.contract_address.unwrap())
}

/// Calls the EtVerifier contract.
pub async fn call_verifier(
signer: Arc<ClientSigner>, verifier_address: Address, proof: NativeProof,
Expand Down Expand Up @@ -117,21 +102,23 @@ pub fn compile_att_station() -> Result<(), EigenError> {
Ok(())
}

/// Compiles the Yul contracts in the `data` directory.
pub fn compile_yul_contracts() {
let data_dir = get_assets_path().unwrap();
let paths = read_dir(data_dir).unwrap();

for path in paths {
if let Some(name_with_suffix) = path.unwrap().path().file_name().and_then(|n| n.to_str()) {
if name_with_suffix.ends_with(".yul") {
let name = name_with_suffix.strip_suffix(".yul").unwrap();
let compiled_contract = compile_yul(&read_yul(name).unwrap());

write_binary(compiled_contract, name).unwrap();
}
}
}
/// Deploys the EtVerifier contract.
pub async fn deploy_verifier(signer: Arc<ClientSigner>) -> Result<Address, EigenError> {
// Compile the et_verifier.yul contract
let path = get_assets_path().map_err(|_| EigenError::ParseError)?.join("et_verifier.yul");
let path_str = path.to_str().ok_or(EigenError::ParseError)?;
let compiled_contract =
compile_yul(&read_yul(path_str).map_err(|_| EigenError::ContractCompilationError)?);

// Deploy the contract
let tx = TransactionRequest::default().data(compiled_contract);
let pen_tx =
signer.send_transaction(tx, None).await.map_err(|_| EigenError::TransactionError)?;
let tx = pen_tx.await.map_err(|_| EigenError::TransactionError)?;

let rec = tx.ok_or(EigenError::TransactionError)?;

rec.contract_address.ok_or(EigenError::TransactionError)
}

/// Returns a vector of ECDSA private keys derived from the given mnemonic phrase.
Expand Down Expand Up @@ -196,7 +183,7 @@ pub fn scalar_from_address(address: &Address) -> Result<Scalar, &'static str> {
mod tests {
use crate::{
eth::{address_from_public_key, call_verifier, deploy_as, deploy_verifier},
fs::{read_binary, read_json},
fs::read_json,
Client, ClientConfig,
};
use eigen_trust_circuit::{Proof, ProofRaw};
Expand Down Expand Up @@ -231,7 +218,6 @@ mod tests {
#[tokio::test]
async fn test_deploy_verifier() {
let anvil = Anvil::new().spawn();
let et_contract = read_binary("et_verifier").unwrap();
let config = ClientConfig {
as_address: "0x5fbdb2315678afecb367f032d93f642f64180aa3".to_string(),
band_id: "38922764296632428858395574229367".to_string(),
Expand All @@ -244,7 +230,7 @@ mod tests {
let client = Client::new(config);

// Deploy
let res = deploy_verifier(client.get_signer(), et_contract).await;
let res = deploy_verifier(client.get_signer()).await;
assert!(res.is_ok());

drop(anvil);
Expand All @@ -264,9 +250,8 @@ mod tests {
};
let client = Client::new(config);

// Read contract data and deploy verifier
let bytecode = read_binary("et_verifier").unwrap();
let addr = deploy_verifier(client.get_signer(), bytecode).await.unwrap();
// Deploy the verifier contract
let addr = deploy_verifier(client.get_signer()).await.unwrap();

// Read proof data and call verifier
let proof_raw: ProofRaw = read_json("et_proof").unwrap();
Expand Down
7 changes: 2 additions & 5 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,6 @@ mod lib_tests {
eth::{deploy_as, deploy_verifier},
Client, ClientConfig,
};
use eigen_trust_circuit::utils::read_bytes_data;
use ethers::{abi::Address, types::H256, utils::Anvil};

#[tokio::test]
Expand All @@ -369,8 +368,7 @@ mod lib_tests {

// Deploy attestation station and verifier
let as_address = deploy_as(client.get_signer()).await.unwrap();
let verifier_address =
deploy_verifier(client.get_signer(), read_bytes_data("et_verifier")).await.unwrap();
let verifier_address = deploy_verifier(client.get_signer()).await.unwrap();

// Update config with new addresses
let config = ClientConfig {
Expand Down Expand Up @@ -406,8 +404,7 @@ mod lib_tests {

// Deploy attestation station and verifier
let as_address = deploy_as(client.get_signer()).await.unwrap();
let verifier_address =
deploy_verifier(client.get_signer(), read_bytes_data("et_verifier")).await.unwrap();
let verifier_address = deploy_verifier(client.get_signer()).await.unwrap();

// Update config with new addresses and instantiate client
let config = ClientConfig {
Expand Down
22 changes: 9 additions & 13 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use clap::Parser;
use cli::*;
use dotenv::dotenv;
use eigen_trust_client::{
eth::{compile_att_station, compile_yul_contracts, deploy_as, deploy_verifier},
fs::{read_binary, read_json},
eth::{compile_att_station, deploy_as, deploy_verifier},
fs::read_json,
Client, ClientConfig,
};
use env_logger::{init_from_env, Env};
Expand Down Expand Up @@ -58,7 +58,6 @@ async fn main() {
Ok(_) => info!("AttestationStation Compilation successful"),
Err(e) => error!("Error during AttestationStation compilation: {}", e),
}
compile_yul_contracts();
info!("Done!");
},
Mode::Deploy => {
Expand All @@ -74,16 +73,13 @@ async fn main() {
};
info!("AttestationStation deployed at {:?}", as_address);

let verifier_contract = read_binary("et_verifier").unwrap();

let verifier_address =
match deploy_verifier(client.get_signer(), verifier_contract).await {
Ok(a) => a,
Err(e) => {
error!("Failed to deploy EigenTrustVerifier: {:?}", e);
return;
},
};
let verifier_address = match deploy_verifier(client.get_signer()).await {
Ok(a) => a,
Err(e) => {
error!("Failed to deploy EigenTrustVerifier: {:?}", e);
return;
},
};

info!("EigenTrustVerifier deployed at {:?}", verifier_address);
},
Expand Down

0 comments on commit 0f0209e

Please sign in to comment.