Skip to content

Commit

Permalink
chore: removed executor, created utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ICavlek committed Oct 22, 2024
1 parent f2e0e10 commit 4095926
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 123 deletions.
38 changes: 32 additions & 6 deletions tests/account_katana.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{thread, time};

use anyhow::anyhow;
use beerus::{
client::Http,
gen::{
Expand All @@ -12,15 +13,16 @@ use beerus::{
},
};
use common::err::Error;
use starknet::katana::Katana;
use starknet::scarb::Compiler;
use starknet::{
constants::{
CLASS_HASH, COMPILED_ACCOUNT_CONTRACT_V2, COMPILED_ACCOUNT_CONTRACT_V3,
CONTRACT_ADDRESS, DECLARE_ACCOUNT_V2, DECLARE_ACCOUNT_V3,
SENDER_ADDRESS,
},
executor::Executor,
utils::update_account,
};
use starknet::{katana::Katana, utils::copy_to_target};

mod common;
mod starknet;
Expand Down Expand Up @@ -50,10 +52,34 @@ async fn declare_deploy_account_v2() {
#[tokio::test]
async fn deploy_multiple_accounts_on_katana() -> Result<(), Error> {
let _katana = Katana::init("http://127.0.0.1:0").await?;
let num_of_new_accounts = 3;
let mut executor = Executor::new(num_of_new_accounts)?;
let update_template = false;
executor.deploy_accounts(update_template).await?;
let path = "./target/account".to_string();
if false {
update_account(&path)?;
}
copy_to_target()?;
let compilation =
tokio::task::spawn_blocking(move || -> Result<(), anyhow::Error> {
let toml = path + "/Scarb.toml";
let compiler = Compiler::new(&toml)?;
compiler.compile()
})
.await;
match compilation {
Ok(val) => val?,
Err(e) => {
return Err(Error::Anyhow(anyhow!(
"Error during thread execution. Original error message: {:#?}",
e,
)));
}
}
// TODO
// #804 starkli signer keystore new key.json - Storing somewhere or deleting?
// #804 starkli account oz init account.json - Storing somewhere or deleting?
// #804 declare accounts
// #804 #805 fund accounts from pre-funded account
// #804 deploy accounts
// #806 iterate through class hashes and call getClass to see if they are verified
Ok(())
}

Expand Down
5 changes: 0 additions & 5 deletions tests/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ mod common;
mod starknet;

use common::err::Error;
use starknet::executor::Executor;

#[tokio::test]
#[allow(non_snake_case)]
Expand Down Expand Up @@ -519,9 +518,5 @@ async fn deploy_new_account_on_sepolia() -> Result<(), Error> {
// with each test, template account id is incremented by 1
// commit from workflows to update latest state of id
let _ctx = setup!("sepolia");
let num_of_new_accounts = 1;
let _executor = Executor::new(num_of_new_accounts)?;
let _update_template = true;
// executor.deploy_accounts(update_template)?;
Ok(())
}
112 changes: 1 addition & 111 deletions tests/starknet/executor.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::{fs, thread};
use std::fs;

use anyhow::{anyhow, Error};
use regex::Regex;

use super::scarb::Compiler;

#[allow(dead_code)]
pub struct Executor {
accounts: Vec<String>,
Expand All @@ -18,112 +16,4 @@ impl Executor {
accounts.push(template);
Ok(Self { accounts })
}

pub async fn deploy_accounts(
&mut self,
update_template: bool,
) -> Result<(), Error> {
if update_template {
self.update_account(&self.accounts[0])?;
}
self.prepare_contracts_environment()?;
self.compile().await?;
// TODO
// #804 starkli signer keystore new key.json - Storing somewhere or deleting?
// #804 starkli account oz init account.json - Storing somewhere or deleting?
// #804 declare accounts
// #804 #805 fund accounts from pre-funded account
// #804 deploy accounts
// #806 iterate through class hashes and call getClass to see if they are verified
Ok(())
}

fn update_account(&self, path: &str) -> Result<(), Error> {
let lib_path = path.to_owned() + "/src/lib.cairo";
let account_old = fs::read_to_string(lib_path.clone())?;
let re = Regex::new(r"self.id.write\((?<number>\d+)\);")?;

let Some(val) = re.captures(&account_old) else {
return Err(anyhow!("Could not find pattern in lib.cairo."));
};
let num_old =
&val["number"].parse::<u64>().expect("Failed to read number");
let num_new = num_old + 1;
let account_new = account_old.replace(
&format!("self.id.write({num_old})"),
&format!("self.id.write({num_new})"),
);
fs::write(lib_path, account_new)?;

Ok(())
}

fn prepare_contracts_environment(&mut self) -> Result<(), Error> {
let capacity = self.accounts.capacity();
let template = self.accounts[0].clone();
let lib_path = "/src/lib.cairo";
let toml_path = "/Scarb.toml";

for i in 1..capacity {
let account = template.clone() + &i.to_string();
fs::create_dir(account.clone())?;
fs::create_dir(account.clone() + "/src")?;
fs::copy(
self.accounts[i - 1].clone() + lib_path,
account.clone() + lib_path,
)?;
fs::copy(
template.clone() + toml_path,
account.clone() + toml_path,
)?;
self.update_account(&account)?;
self.accounts.push(account);
}

Ok(())
}

async fn compile(&self) -> Result<(), Error> {
let mut vec_of_threads = Vec::with_capacity(self.accounts.len());

for account in self.accounts.iter() {
let path = account.clone() + "/Scarb.toml";
let compilation =
tokio::task::spawn_blocking(move || -> Result<(), Error> {
let compiler = Compiler::new(&path)?;
compiler.compile()
});
vec_of_threads.push(compilation);
}
for (i, thread) in vec_of_threads.into_iter().enumerate() {
let compilation = thread.await;
match compilation {
Ok(val) => val?,
Err(e) => {
return Err(anyhow!("Error during thread {i} execution. Original error message: {:#?}", e));
}
}
}

Ok(())
}
}

impl Drop for Executor {
fn drop(&mut self) {
let dir = self.accounts[0].clone() + "/target";
let scarb = self.accounts[0].clone() + "/Scarb.lock";
if fs::exists(dir.clone()).expect("Failed to check template target") {
fs::remove_dir_all(dir).expect("Failed to remove template target");
};
if fs::exists(scarb.clone()).expect("Failed to check template Scarb") {
fs::remove_file(scarb).expect("Failed to remove template Scarb");
}
for i in 1..self.accounts.len() {
let dir = self.accounts[i].clone();
if fs::exists(dir.clone()).expect("Failed to check account dir") {
fs::remove_dir_all(dir).expect("Failed to remove account dir");
}
}
}
}
2 changes: 1 addition & 1 deletion tests/starknet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod constants;
pub mod executor;
pub mod katana;
pub mod scarb;
pub mod utils;
41 changes: 41 additions & 0 deletions tests/starknet/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::fs;

use anyhow::{anyhow, Error};
use regex::Regex;

#[allow(dead_code)]
pub fn update_account(path: &str) -> Result<(), Error> {
let lib_path = path.to_owned() + "/src/lib.cairo";
let account_old = fs::read_to_string(lib_path.clone())?;
let re = Regex::new(r"self.id.write\((?<number>\d+)\);")?;

let Some(val) = re.captures(&account_old) else {
return Err(anyhow!("Could not find pattern in lib.cairo."));
};
let num_old = &val["number"].parse::<u64>().expect("Failed to read number");
let num_new = num_old + 1;
let account_new = account_old.replace(
&format!("self.id.write({num_old})"),
&format!("self.id.write({num_new})"),
);
fs::write(lib_path, account_new)?;

Ok(())
}

#[allow(dead_code)]
pub fn copy_to_target() -> Result<(), Error> {
if !fs::metadata("./target/account").is_ok() {
fs::create_dir("./target/account/")?;
fs::create_dir("./target/account/src/")?;
fs::copy(
"./tests/starknet/contract/account/src/lib.cairo",
"./target/account/src/lib.cairo",
)?;
fs::copy(
"./tests/starknet/contract/account/Scarb.toml",
"./target/account/Scarb.toml",
)?;
}
Ok(())
}

0 comments on commit 4095926

Please sign in to comment.