From ba0b9810a69ee5532828f38deb4c2f304d1049e8 Mon Sep 17 00:00:00 2001 From: Ivan Cavlek Date: Thu, 24 Oct 2024 10:56:59 +0200 Subject: [PATCH] refactor: additional update based on comments --- tests/account_katana.rs | 32 ++------------ tests/rpc.rs | 62 +++++++++++++------------- tests/starknet/coordinator.rs | 82 ----------------------------------- tests/starknet/mod.rs | 2 +- tests/starknet/scarb.rs | 67 +++++++++------------------- tests/starknet/utils.rs | 28 ++++++++++++ 6 files changed, 84 insertions(+), 189 deletions(-) delete mode 100644 tests/starknet/coordinator.rs create mode 100644 tests/starknet/utils.rs diff --git a/tests/account_katana.rs b/tests/account_katana.rs index 5791ac32..b0388067 100644 --- a/tests/account_katana.rs +++ b/tests/account_katana.rs @@ -11,17 +11,11 @@ use beerus::{ TxnHash, }, }; -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, - }, - coordinator::{Coordinator, TestMode}, +use starknet::constants::{ + CLASS_HASH, COMPILED_ACCOUNT_CONTRACT_V2, COMPILED_ACCOUNT_CONTRACT_V3, + CONTRACT_ADDRESS, DECLARE_ACCOUNT_V2, DECLARE_ACCOUNT_V3, SENDER_ADDRESS, }; +use starknet::katana::Katana; mod common; mod starknet; @@ -48,24 +42,6 @@ async fn declare_deploy_account_v2() { deploy(client).await; } -#[tokio::test] -async fn deploy_new_account_on_katana() -> Result<(), Error> { - let _katana = Katana::init("http://127.0.0.1:0").await?; - 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(()) -} - async fn declare( client: &Client, compiled_contract: &str, diff --git a/tests/rpc.rs b/tests/rpc.rs index f392af2b..ddb7190f 100644 --- a/tests/rpc.rs +++ b/tests/rpc.rs @@ -1,11 +1,14 @@ -use beerus::gen::{ - Address, BlockHash, BlockId, BlockNumber, BlockTag, BroadcastedInvokeTxn, - BroadcastedTxn, Felt, FunctionCall, GetBlockWithTxHashesResult, - GetBlockWithTxsResult, GetClassAtResult, GetClassResult, - GetTransactionByBlockIdAndIndexIndex, InvokeTxn, InvokeTxnV1, - InvokeTxnV1Version, PriceUnit, Rpc, StorageKey, SyncingResult, Txn, - TxnExecutionStatus, TxnHash, TxnReceipt, TxnReceiptWithBlockInfo, - TxnStatus, +use beerus::{ + config::MAINNET_STARKNET_CHAINID, + gen::{ + Address, BlockHash, BlockId, BlockNumber, BlockTag, + BroadcastedInvokeTxn, BroadcastedTxn, Felt, FunctionCall, + GetBlockWithTxHashesResult, GetBlockWithTxsResult, GetClassAtResult, + GetClassResult, GetTransactionByBlockIdAndIndexIndex, InvokeTxn, + InvokeTxnV1, InvokeTxnV1Version, PriceUnit, Rpc, StorageKey, + SyncingResult, Txn, TxnExecutionStatus, TxnHash, TxnReceipt, + TxnReceiptWithBlockInfo, TxnStatus, + }, }; mod common; @@ -13,6 +16,26 @@ mod starknet; use common::err::Error; +#[tokio::test] +#[allow(non_snake_case)] +async fn test_specVersion() -> Result<(), Error> { + let ctx = setup!(); + + let ret = ctx.client.specVersion().await?; + assert_eq!(ret, "0.7.1"); + Ok(()) +} + +#[tokio::test] +#[allow(non_snake_case)] +async fn test_chainId() -> Result<(), Error> { + let ctx = setup!(); + + let ret = ctx.client.chainId().await?; + assert_eq!(ret.as_ref(), MAINNET_STARKNET_CHAINID); + Ok(()) +} + #[tokio::test] #[allow(non_snake_case)] async fn test_blockHashAndNumber() -> Result<(), Error> { @@ -510,26 +533,3 @@ async fn account_call() -> Result<(), Error> { Ok(()) } - -#[tokio::test] -async fn deploy_new_account_on_sepolia() -> Result<(), Error> { - // TODO #807 - // 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 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(()) -} diff --git a/tests/starknet/coordinator.rs b/tests/starknet/coordinator.rs deleted file mode 100644 index 417693fb..00000000 --- a/tests/starknet/coordinator.rs +++ /dev/null @@ -1,82 +0,0 @@ -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("", &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 => {} - } - } -} diff --git a/tests/starknet/mod.rs b/tests/starknet/mod.rs index 280a6a6d..4dae99a6 100644 --- a/tests/starknet/mod.rs +++ b/tests/starknet/mod.rs @@ -1,4 +1,4 @@ pub mod constants; -pub mod coordinator; pub mod katana; pub mod scarb; +pub mod utils; diff --git a/tests/starknet/scarb.rs b/tests/starknet/scarb.rs index 37449ce5..d597fb79 100644 --- a/tests/starknet/scarb.rs +++ b/tests/starknet/scarb.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, Error}; +use anyhow::Error; use scarb::{ core::{Config, PackageId, PackageName, SourceId, TargetKind}, ops::{self, CompileOpts, FeaturesOpts, FeaturesSelector}, @@ -7,51 +7,24 @@ use semver::Version; use std::{fs::canonicalize, path::PathBuf}; #[allow(dead_code)] -pub struct Compiler { - toml: PathBuf, - opts: CompileOpts, - packages: Vec, -} - -#[allow(dead_code)] -impl Compiler { - pub fn new(toml: &str) -> Result { - let toml_absolute = canonicalize(PathBuf::from(toml))?; - let opts = CompileOpts { - include_target_kinds: vec![], - exclude_target_kinds: vec![TargetKind::new("test")], - include_target_names: vec![], - features: FeaturesOpts { - features: FeaturesSelector::Features(vec![]), - no_default_features: false, - }, - }; - let packages = vec![PackageId::new( - PackageName::new("account"), - Version::new(0, 1, 0), - SourceId::for_path(toml_absolute.to_str().unwrap().into())?, - )]; - - Ok(Compiler { toml: toml_absolute, opts, packages }) - } - - 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, - )), - } - } +pub fn compile(toml: String) -> Result<(), Error> { + let toml_absolute = canonicalize(PathBuf::from(toml))?; + let opts = CompileOpts { + include_target_kinds: vec![], + exclude_target_kinds: vec![TargetKind::new("test")], + include_target_names: vec![], + features: FeaturesOpts { + features: FeaturesSelector::Features(vec![]), + no_default_features: false, + }, + }; + let packages = vec![PackageId::new( + PackageName::new("account"), + Version::new(0, 1, 0), + SourceId::for_path(toml_absolute.to_str().unwrap().into())?, + )]; + let config = Config::builder(toml_absolute.to_str().unwrap()).build()?; + let ws = ops::read_workspace(config.manifest_path(), &config)?; - 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) - } + scarb::ops::compile(packages, opts, &ws) } diff --git a/tests/starknet/utils.rs b/tests/starknet/utils.rs new file mode 100644 index 00000000..d3fac708 --- /dev/null +++ b/tests/starknet/utils.rs @@ -0,0 +1,28 @@ +use std::fs; + +use anyhow::Error; +use chrono; + +const SOURCE_LIB: &str = "./tests/starknet/contract/account/src/lib.cairo"; +const SOURCE_SCARB: &str = "./tests/starknet/contract/account/Scarb.toml"; + +#[allow(dead_code)] +pub fn prepare_account() -> Result { + 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; + let target_lib = target.clone() + "/src/lib.cairo"; + let target_scarb = target.clone() + "/Scarb.toml"; + let target_src = target.clone() + "/src"; + + fs::create_dir(target)?; + fs::create_dir(target_src)?; + fs::copy(SOURCE_LIB, target_lib.clone())?; + fs::copy(SOURCE_SCARB, target_scarb.clone())?; + + let account_template = fs::read_to_string(target_lib.clone())?; + let account_new = account_template.replace("", &id); + fs::write(target_lib, account_new)?; + + Ok(target_scarb) +}