Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Dojo Katana Runner #882

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/account_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ sha2 = { workspace = true, optional = true }
dirs = { version = "5", optional = true }

[dev-dependencies]
katana-runner = { git = "https://github.com/dojoengine/dojo", version = "1.0.9" }
hyper = { version = "0.14.27", features = ["server"] }
rand_core = { version = "0.6", features = ["getrandom"] }
tempfile = "3.8"
Expand Down
44 changes: 16 additions & 28 deletions packages/account_sdk/src/tests/runners/katana.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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};
use starknet::core::utils::cairo_short_string_to_felt;
use starknet::macros::short_string;
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 +25,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,52 +39,37 @@ lazy_static! {
"0xb3ff441a68610b30fd5e2abbf3a1548eb6ba6f3559f2862bf2dc757e5828ca"
)
);

pub static ref CONFIG: TestnetConfig = TestnetConfig{
chain_id: "SN_SEPOLIA".to_string(),
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(["--chain-id", &config.chain_id])
.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 chain_id = cairo_short_string_to_felt(&config.chain_id).expect("Should convert");
let rpc_client = Arc::new(JsonRpcClient::new(HttpTransport::new(rpc_url.clone())));
let proxy = CartridgeProxy::new(rpc_url, proxy_url.clone(), chain_id);
let proxy = CartridgeProxy::new(rpc_url.clone(), proxy_url.clone(), chain_id);
let proxy_handle = tokio::spawn(async move {
proxy.run().await;
});

println!("RPC URL: {}", rpc_url.clone());
println!("Proxy URL: {}", proxy_url);

KatanaRunner {
chain_id,
testnet,
client,
rpc_url: proxy_url,
rpc_client,
Expand All @@ -92,7 +78,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 @@ -195,7 +184,6 @@ impl KatanaRunner {

impl Drop for KatanaRunner {
fn drop(&mut self) {
self.testnet.kill();
self.proxy_handle.abort();
}
}
Expand Down
39 changes: 1 addition & 38 deletions packages/account_sdk/src/tests/runners/mod.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,7 @@
use std::{net::TcpListener, process::Child};

use serde::Deserialize;

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 @@ -39,11 +10,3 @@ pub fn find_free_port() -> u16 {
.unwrap()
.port()
}

#[derive(Debug, Clone, Deserialize)]
pub struct TestnetConfig {
pub chain_id: String,
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.

Loading