Skip to content

Commit

Permalink
refactor: update based on review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ICavlek committed Oct 24, 2024
1 parent 6ad8983 commit 00af73c
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 143 deletions.
1 change: 1 addition & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ gloo-timers = { version = "0.3.0", features = ["futures"] }
[dev-dependencies]
anyhow = "1.0.89"
alloy-primitives = { version = "0.8.5", default-features = false }
chrono = "0.4.38"
katana-core = { git = "https://github.com/dojoengine/dojo", tag = "v1.0.0-alpha.9" }
katana-executor = { git = "https://github.com/dojoengine/dojo", tag = "v1.0.0-alpha.9" }
katana-node = { git = "https://github.com/dojoengine/dojo", tag = "v1.0.0-alpha.9" }
Expand Down
21 changes: 15 additions & 6 deletions tests/account_katana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ 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,
coordinator::{Coordinator, TestMode},
};

mod common;
Expand Down Expand Up @@ -48,12 +49,20 @@ async fn declare_deploy_account_v2() {
}

#[tokio::test]
async fn deploy_multiple_accounts_on_katana() -> Result<(), Error> {
async fn deploy_new_account_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)?;
let coordinator = Coordinator::new(TestMode::Katana);
coordinator.copy_template_to_target()?;
coordinator.update_account()?;
let compiler = Compiler::new(&coordinator.target_scarb())?;
compiler.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(())
}

Expand Down
18 changes: 13 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 @@ -518,10 +517,19 @@ async fn deploy_new_account_on_sepolia() -> Result<(), Error> {
// schedule test once each month in separate workflow
// 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)?;
// let coordinator = Coordinator::new(TestMode::Sepolia);
// coordinator.copy_template_to_target()?;
// coordinator.update_account()?;
// let compiler = Compiler::new(&coordinator.target_scarb())?;
// compiler.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 account
// #804 #805 fund account from pre-funded account
// #804 deploy account
Ok(())
}
2 changes: 1 addition & 1 deletion tests/starknet/contract/account/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod Account {
#[constructor]
fn constructor(ref self: ContractState, public_key: felt252) {
self.public_key.write(public_key);
self.id.write(0);
self.id.write(<ID>);
}

#[abi(embed_v0)]
Expand Down
82 changes: 82 additions & 0 deletions tests/starknet/coordinator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use std::fs;

use anyhow::Error;
use chrono;

#[allow(dead_code)]
pub enum TestMode {
Katana,
Sepolia,
}

#[allow(dead_code)]
pub struct Coordinator {
id: String,
mode: TestMode,
source: String,
target: String,
}

#[allow(dead_code)]
impl Coordinator {
pub fn new(mode: TestMode) -> Self {
let now = chrono::offset::Local::now();
let id = now.format("%Y%m%y%H%M%S").to_string();
let target = "./target/account-".to_string() + &id;
Self {
id,
mode,
source: "./tests/starknet/contract/account".to_string(),
target,
}
}

pub fn copy_template_to_target(&self) -> Result<(), Error> {
fs::create_dir(&self.target)?;
fs::create_dir(self.target_src())?;
fs::copy(self.source_lib(), self.target_lib())?;
fs::copy(self.source_scarb(), self.target_scarb())?;
Ok(())
}

pub fn update_account(&self) -> Result<(), Error> {
let account_template = fs::read_to_string(self.target_lib())?;
let account_new = account_template.replace("<ID>", &self.id);
fs::write(self.target_lib(), account_new)?;
Ok(())
}

pub fn source_lib(&self) -> String {
self.source.clone() + "/src/lib.cairo"
}

pub fn source_scarb(&self) -> String {
self.source.clone() + "/Scarb.toml"
}

pub fn target_lib(&self) -> String {
self.target.clone() + "/src/lib.cairo"
}

pub fn target_scarb(&self) -> String {
self.target.clone() + "/Scarb.toml"
}

pub fn target_src(&self) -> String {
self.target.clone() + "/src"
}
}

impl Drop for Coordinator {
fn drop(&mut self) {
match self.mode {
TestMode::Katana => {
let target = self.target.clone();
if fs::exists(&target).unwrap() {
fs::remove_dir_all(target).unwrap()
}
}
TestMode::Sepolia => {}
}
}
}
128 changes: 0 additions & 128 deletions tests/starknet/executor.rs

This file was deleted.

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 coordinator;
pub mod katana;
pub mod scarb;
18 changes: 16 additions & 2 deletions tests/starknet/scarb.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Error;
use anyhow::{anyhow, Error};
use scarb::{
core::{Config, PackageId, PackageName, SourceId, TargetKind},
ops::{self, CompileOpts, FeaturesOpts, FeaturesSelector},
Expand Down Expand Up @@ -35,7 +35,21 @@ impl Compiler {
Ok(Compiler { toml: toml_absolute, opts, packages })
}

pub fn compile(self) -> Result<(), Error> {
pub async fn compile(self) -> Result<(), Error> {
let compilation =
tokio::task::spawn_blocking(move || -> Result<(), Error> {
self.run_compilation()
});
match compilation.await {
Ok(val) => Ok(val?),
Err(e) => Err(anyhow!(
"Error during thread execution. Original error message: {:#?}",
e,
)),
}
}

fn run_compilation(self) -> Result<(), Error> {
let config = Config::builder(self.toml.to_str().unwrap()).build()?;
let ws = ops::read_workspace(config.manifest_path(), &config)?;
scarb::ops::compile(self.packages, self.opts, &ws)
Expand Down

0 comments on commit 00af73c

Please sign in to comment.