diff --git a/packages/account_sdk/Cargo.toml b/packages/account_sdk/Cargo.toml index 9da44e457..10d3d84a8 100644 --- a/packages/account_sdk/Cargo.toml +++ b/packages/account_sdk/Cargo.toml @@ -49,6 +49,7 @@ sha2 = { workspace = true, optional = true } dirs = { version = "5", optional = true } [dev-dependencies] +katana-runner = { git = "https://github.com/dojoengine/dojo", rev = "4e52a45" } hyper = { version = "0.14.27", features = ["server"] } rand_core = { version = "0.6", features = ["getrandom"] } tempfile = "3.8" diff --git a/packages/account_sdk/src/tests/runners/katana.rs b/packages/account_sdk/src/tests/runners/katana.rs index b48ac1896..a88527112 100644 --- a/packages/account_sdk/src/tests/runners/katana.rs +++ b/packages/account_sdk/src/tests/runners/katana.rs @@ -1,4 +1,5 @@ use cainome::cairo_serde::{ContractAddress, U256}; +use katana_runner::{KatanaRunner as DojoKatanaRunner, KatanaRunnerConfig}; use starknet::accounts::{AccountFactory, ExecutionEncoding, SingleOwnerAccount}; use starknet::contract::ContractFactory; use starknet::core::types::{BlockId, BlockTag, DeclareTransactionResult}; @@ -7,7 +8,6 @@ use starknet::providers::jsonrpc::HttpTransport; use starknet::providers::{JsonRpcClient, Provider}; use starknet::signers::LocalWallet; use starknet::{core::types::Felt, macros::felt, signers::SigningKey}; -use std::process::{Command, Stdio}; use std::sync::Arc; use tokio::task::JoinHandle; use url::Url; @@ -24,7 +24,7 @@ use crate::tests::account::{AccountDeclaration, FEE_TOKEN_ADDRESS, UDC_ADDRESS}; use crate::tests::transaction_waiter::TransactionWaiter; use super::cartridge::CartridgeProxy; -use super::{find_free_port, SubprocessRunner, TestnetConfig}; +use super::find_free_port; lazy_static! { // Signing key and address of the katana prefunded account. @@ -38,18 +38,11 @@ lazy_static! { "0xb3ff441a68610b30fd5e2abbf3a1548eb6ba6f3559f2862bf2dc757e5828ca" ) ); - - pub static ref CONFIG: TestnetConfig = TestnetConfig{ - chain_id: short_string!("KATANA"), - exec: "katana".to_string(), - log_file_path: "log/katana.log".to_string(), - }; } #[derive(Debug)] pub struct KatanaRunner { chain_id: Felt, - testnet: SubprocessRunner, client: CartridgeJsonRpcProvider, pub rpc_url: Url, rpc_client: Arc>, @@ -57,31 +50,22 @@ pub struct KatanaRunner { } impl KatanaRunner { - pub fn new(config: TestnetConfig) -> Self { - let katana_port = find_free_port(); - let child = Command::new(config.exec) - .args(["-p", &katana_port.to_string()]) - .args(["--json-log"]) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn() - .expect("failed to start subprocess"); - - let testnet = SubprocessRunner::new(child, |l| l.contains(r#""target":"katana::cli""#)); - - let rpc_url = Url::parse(&format!("http://0.0.0.0:{}/", katana_port)).unwrap(); + pub fn new(config: KatanaRunnerConfig) -> Self { + let chain_id = config.chain_id.unwrap_or(short_string!("KATANA")); + let katana = DojoKatanaRunner::new_with_config(config).expect("Katana started"); + + let rpc_url = katana.url(); let proxy_url = Url::parse(&format!("http://0.0.0.0:{}/", find_free_port())).unwrap(); let client = CartridgeJsonRpcProvider::new(proxy_url.clone()); let rpc_client = Arc::new(JsonRpcClient::new(HttpTransport::new(rpc_url.clone()))); - let proxy = CartridgeProxy::new(rpc_url, proxy_url.clone(), config.chain_id); + let proxy = CartridgeProxy::new(rpc_url, proxy_url.clone(), chain_id); let proxy_handle = tokio::spawn(async move { proxy.run().await; }); KatanaRunner { - chain_id: config.chain_id, - testnet, + chain_id, client, rpc_url: proxy_url, rpc_client, @@ -90,7 +74,10 @@ impl KatanaRunner { } pub fn load() -> Self { - KatanaRunner::new(CONFIG.clone()) + KatanaRunner::new(KatanaRunnerConfig { + chain_id: Some(short_string!("SN_SEPOLIA")), + ..KatanaRunnerConfig::default() + }) } pub fn client(&self) -> &CartridgeJsonRpcProvider { @@ -190,7 +177,6 @@ impl KatanaRunner { impl Drop for KatanaRunner { fn drop(&mut self) { - self.testnet.kill(); self.proxy_handle.abort(); } } diff --git a/packages/account_sdk/src/tests/runners/mod.rs b/packages/account_sdk/src/tests/runners/mod.rs index 680302850..e0381ea0f 100644 --- a/packages/account_sdk/src/tests/runners/mod.rs +++ b/packages/account_sdk/src/tests/runners/mod.rs @@ -1,37 +1,7 @@ -use std::{net::TcpListener, process::Child}; - -use serde::Deserialize; -use starknet_crypto::Felt; - -use self::waiter::OutputWaiter; +use std::net::TcpListener; pub mod cartridge; pub mod katana; -pub mod waiter; - -#[derive(Debug)] -struct SubprocessRunner { - child: Child, -} - -impl SubprocessRunner { - pub fn new(mut child: Child, line_predicate: impl (Fn(&str) -> bool) + Send + 'static) -> Self { - let stdout = child - .stdout - .take() - .expect("failed to take subprocess stdout"); - OutputWaiter::new(stdout).wait(line_predicate); - Self { child } - } - pub fn kill(&mut self) { - if let Err(e) = self.child.kill() { - eprintln!("Failed to kill katana subprocess: {}", e); - } - if let Err(e) = self.child.wait() { - eprintln!("Failed to wait for katana subprocess: {}", e); - } - } -} pub fn find_free_port() -> u16 { TcpListener::bind("127.0.0.1:0") @@ -40,11 +10,3 @@ pub fn find_free_port() -> u16 { .unwrap() .port() } - -#[derive(Debug, Clone, Deserialize)] -pub struct TestnetConfig { - pub chain_id: Felt, - pub exec: String, - #[allow(dead_code)] - pub log_file_path: String, -} diff --git a/packages/account_sdk/src/tests/runners/waiter.rs b/packages/account_sdk/src/tests/runners/waiter.rs deleted file mode 100644 index 332a394ba..000000000 --- a/packages/account_sdk/src/tests/runners/waiter.rs +++ /dev/null @@ -1,55 +0,0 @@ -use std::{ - io::{BufRead, BufReader}, - process::ChildStdout, - sync::mpsc::{self, Sender}, - thread, - time::Duration, -}; - -pub struct OutputWaiter { - child_stdout: ChildStdout, -} - -impl OutputWaiter { - pub fn new(child_stdout: ChildStdout) -> Self { - Self { child_stdout } - } - - pub fn wait(self, line_predicate: F) - where - F: Fn(&str) -> bool, - F: Send + 'static, - { - let (sender, receiver) = mpsc::channel(); - let child_stdout = self.child_stdout; - - thread::spawn(move || { - OutputWaiter::wait_for_server_started_and_signal(child_stdout, sender, |l| { - line_predicate(l) - }); - }); - - receiver - .recv_timeout(Duration::from_secs(5)) - .expect("timeout waiting for subprocess"); - } - - fn wait_for_server_started_and_signal( - stdout: ChildStdout, - sender: Sender<()>, - line_predicate: impl Fn(&str) -> bool, - ) { - let reader = BufReader::new(stdout); - - let mut is_send = false; - for line in reader.lines() { - let line = line.expect("failed to read line from subprocess stdout"); - println!("{}", line); // Log to parent stdout - - if !is_send && line_predicate(&line) { - sender.send(()).expect("failed to send start signal"); - is_send = true; - } - } - } -}