Skip to content

Commit

Permalink
Migrate to Dojo Katana Runner
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Oct 18, 2024
1 parent ece1239 commit 6149753
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 121 deletions.
1 change: 1 addition & 0 deletions packages/account_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 13 additions & 27 deletions packages/account_sdk/src/tests/runners/katana.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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;
Expand All @@ -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.
Expand All @@ -38,50 +38,34 @@ 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<JsonRpcClient<HttpTransport>>,
proxy_handle: JoinHandle<()>,
}

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,
Expand All @@ -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 {
Expand Down Expand Up @@ -190,7 +177,6 @@ impl KatanaRunner {

impl Drop for KatanaRunner {
fn drop(&mut self) {
self.testnet.kill();
self.proxy_handle.abort();
}
}
Expand Down
40 changes: 1 addition & 39 deletions packages/account_sdk/src/tests/runners/mod.rs
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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,
}
55 changes: 0 additions & 55 deletions packages/account_sdk/src/tests/runners/waiter.rs

This file was deleted.

0 comments on commit 6149753

Please sign in to comment.