diff --git a/Cargo.lock b/Cargo.lock index d5e9fcfde0..ed9222bbfa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -116,6 +116,7 @@ dependencies = [ "alloy-eips 0.1.0 (git+https://github.com/alloy-rs/alloy?rev=7128c53)", "alloy-genesis 0.1.0 (git+https://github.com/alloy-rs/alloy?rev=7128c53)", "alloy-network 0.1.0 (git+https://github.com/alloy-rs/alloy?rev=7128c53)", + "alloy-node-bindings", "alloy-provider 0.1.0 (git+https://github.com/alloy-rs/alloy?rev=7128c53)", "alloy-rpc-client 0.1.0 (git+https://github.com/alloy-rs/alloy?rev=7128c53)", "alloy-serde 0.1.0 (git+https://github.com/alloy-rs/alloy?rev=7128c53)", @@ -337,6 +338,21 @@ dependencies = [ "thiserror", ] +[[package]] +name = "alloy-node-bindings" +version = "0.1.0" +source = "git+https://github.com/alloy-rs/alloy?rev=7128c53#7128c53169736a0f4f93362a73bb7109ce93d59d" +dependencies = [ + "alloy-genesis 0.1.0 (git+https://github.com/alloy-rs/alloy?rev=7128c53)", + "alloy-primitives", + "k256", + "serde_json", + "tempfile", + "thiserror", + "tracing", + "url", +] + [[package]] name = "alloy-primitives" version = "0.7.4" @@ -396,10 +412,12 @@ dependencies = [ "alloy-eips 0.1.0 (git+https://github.com/alloy-rs/alloy?rev=7128c53)", "alloy-json-rpc 0.1.0 (git+https://github.com/alloy-rs/alloy?rev=7128c53)", "alloy-network 0.1.0 (git+https://github.com/alloy-rs/alloy?rev=7128c53)", + "alloy-node-bindings", "alloy-primitives", "alloy-rpc-client 0.1.0 (git+https://github.com/alloy-rs/alloy?rev=7128c53)", "alloy-rpc-types 0.1.0 (git+https://github.com/alloy-rs/alloy?rev=7128c53)", "alloy-rpc-types-trace 0.1.0 (git+https://github.com/alloy-rs/alloy?rev=7128c53)", + "alloy-signer-wallet 0.1.0 (git+https://github.com/alloy-rs/alloy?rev=7128c53)", "alloy-transport 0.1.0 (git+https://github.com/alloy-rs/alloy?rev=7128c53)", "alloy-transport-http 0.1.0 (git+https://github.com/alloy-rs/alloy?rev=7128c53)", "async-stream", @@ -6719,25 +6737,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "h2" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa82e28a107a8cc405f0839610bdc9b15f1e25ec7d696aa5cf173edbcb1486ab" -dependencies = [ - "atomic-waker", - "bytes", - "fnv", - "futures-core", - "futures-sink", - "http 1.1.0", - "indexmap 2.2.6", - "slab", - "tokio", - "tokio-util", - "tracing", -] - [[package]] name = "half" version = "2.4.1" @@ -7062,7 +7061,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "h2 0.3.26", + "h2", "http 0.2.12", "http-body 0.4.6", "httparse", @@ -7085,7 +7084,6 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "h2 0.4.5", "http 1.1.0", "http-body 1.0.0", "httparse", @@ -8367,11 +8365,9 @@ dependencies = [ "assert_fs", "chrono", "home", - "hyper 0.14.28", "katana-core", "katana-primitives", "lazy_static", - "reqwest 0.12.4", "runner-macro", "serde", "serde_json", @@ -11297,7 +11293,7 @@ dependencies = [ "encoding_rs", "futures-core", "futures-util", - "h2 0.3.26", + "h2", "http 0.2.12", "http-body 0.4.6", "hyper 0.14.28", @@ -11341,11 +11337,9 @@ checksum = "566cafdd92868e0939d3fb961bd0dc25fcfaaed179291093b3d43e6b3150ea10" dependencies = [ "base64 0.22.1", "bytes", - "encoding_rs", "futures-channel", "futures-core", "futures-util", - "h2 0.4.5", "http 1.1.0", "http-body 1.0.0", "http-body-util", @@ -11368,7 +11362,6 @@ dependencies = [ "serde_json", "serde_urlencoded", "sync_wrapper", - "system-configuration", "tokio", "tokio-native-tls", "tokio-rustls 0.25.0", @@ -14082,7 +14075,7 @@ dependencies = [ "axum", "base64 0.21.7", "bytes", - "h2 0.3.26", + "h2", "http 0.2.12", "http-body 0.4.6", "hyper 0.14.28", diff --git a/crates/katana/rpc/rpc/tests/messaging.rs b/crates/katana/rpc/rpc/tests/messaging.rs index ba18908cf9..dd1936e3e5 100644 --- a/crates/katana/rpc/rpc/tests/messaging.rs +++ b/crates/katana/rpc/rpc/tests/messaging.rs @@ -2,7 +2,11 @@ use std::path::PathBuf; use std::str::FromStr; use std::time::Duration; +use alloy::network::EthereumSigner; +use alloy::node_bindings::Anvil; use alloy::primitives::{Uint, U256}; +use alloy::providers::{ProviderBuilder, WalletProvider}; +use alloy::signers::wallet::LocalWallet; use alloy::sol; use cainome::cairo_serde::EthAddress; use cainome::rs::abigen; @@ -10,7 +14,7 @@ use dojo_world::utils::TransactionWaiter; use katana_primitives::utils::transaction::{ compute_l1_handler_tx_hash, compute_l1_to_l2_message_hash, compute_l2_to_l1_message_hash, }; -use katana_runner::{AnvilRunner, KatanaRunner, KatanaRunnerConfig}; +use katana_runner::{KatanaRunner, KatanaRunnerConfig}; use serde_json::json; use starknet::accounts::{Account, ConnectedAccount}; use starknet::contract::ContractFactory; @@ -22,6 +26,7 @@ use starknet::core::utils::get_contract_address; use starknet::macros::selector; use starknet::providers::Provider; use tempfile::tempdir; +use url::Url; mod common; @@ -44,22 +49,28 @@ abigen!(CairoMessagingContract, "crates/katana/rpc/rpc/tests/test_data/cairo_l1_ #[tokio::test(flavor = "multi_thread")] async fn test_messaging() { // Prepare Anvil + Messaging Contracts - let anvil_runner = AnvilRunner::new().await.unwrap(); - let l1_provider = anvil_runner.provider(); + let anvil_runner = Anvil::default().spawn(); + + let l1_provider = { + let wallet: LocalWallet = anvil_runner.keys()[0].clone().into(); + ProviderBuilder::new() + .with_recommended_fillers() + .signer(EthereumSigner::from(wallet)) + .on_http(Url::from_str(&anvil_runner.endpoint()).unwrap()) + }; // Deploy the core messaging contract on L1 - let core_contract = StarknetContract::deploy(l1_provider).await.unwrap(); + let core_contract = StarknetContract::deploy(&l1_provider).await.unwrap(); // Deploy test contract on L1 used to send/receive messages to/from L2 - let l1_test_contract = - Contract1::deploy(l1_provider, dbg!(*core_contract.address())).await.unwrap(); + let l1_test_contract = Contract1::deploy(&l1_provider, *core_contract.address()).await.unwrap(); // Prepare Katana + Messaging Contract let messaging_config = json!({ "chain": "ethereum", - "rpc_url": anvil_runner.endpoint, + "rpc_url": anvil_runner.endpoint(), "contract_address": core_contract.address().to_string(), - "sender_address": anvil_runner.address(), - "private_key": anvil_runner.secret_key(), + "sender_address": l1_provider.default_signer_address(), + "private_key": "", "interval": 2, "from_block": 0 }) @@ -71,14 +82,8 @@ async fn test_messaging() { let katana_runner = KatanaRunner::new_with_config(KatanaRunnerConfig { n_accounts: 2, - disable_fee: false, - block_time: None, - port: None, - // program_name: Some("/Users/kariy/.dojo/bin/katana".to_string()), - program_name: None, - run_name: None, messaging: Some(path.to_str().unwrap().to_string()), - log_path: None, + ..Default::default() }) .unwrap(); diff --git a/crates/katana/runner/Cargo.toml b/crates/katana/runner/Cargo.toml index 43721f40cc..47db09460f 100644 --- a/crates/katana/runner/Cargo.toml +++ b/crates/katana/runner/Cargo.toml @@ -24,7 +24,5 @@ alloy = { git = "https://github.com/alloy-rs/alloy", rev = "7128c53", features = "providers", "provider-http", "signer-wallet", -] } - -reqwest = "0.12.4" -hyper = "0.14.20" \ No newline at end of file + "node-bindings" +] } \ No newline at end of file diff --git a/crates/katana/runner/src/lib.rs b/crates/katana/runner/src/lib.rs index 370d791d76..342379f87a 100644 --- a/crates/katana/runner/src/lib.rs +++ b/crates/katana/runner/src/lib.rs @@ -4,32 +4,19 @@ mod utils; use std::path::PathBuf; use std::process::{Child, Command, Stdio}; -use std::str::FromStr; use std::sync::mpsc::{self}; use std::thread; use std::time::Duration; -use alloy::network::{Ethereum, EthereumSigner}; -use alloy::primitives::Address; -use alloy::providers::fillers::{ - ChainIdFiller, FillProvider, GasFiller, JoinFill, NonceFiller, SignerFiller, -}; -use alloy::providers::{Identity, ProviderBuilder, RootProvider}; -use alloy::signers::wallet::LocalWallet; -use alloy::transports::http::Http; use anyhow::{Context, Result}; use assert_fs::TempDir; -use hyper::http::request; -use hyper::{Client as HyperClient, Response, StatusCode}; use katana_primitives::contract::ContractAddress; use katana_primitives::genesis::allocation::{DevAllocationsGenerator, DevGenesisAccount}; use katana_primitives::FieldElement; -use reqwest::Client; pub use runner_macro::{katana_test, runner}; use starknet::providers::jsonrpc::HttpTransport; use starknet::providers::JsonRpcClient; use tokio::sync::Mutex; -use tokio::time; use url::Url; use utils::find_free_port; @@ -215,109 +202,6 @@ fn determine_default_program_path() -> String { if let Ok(bin) = std::env::var("KATANA_RUNNER_BIN") { bin } else { "katana".to_string() } } -type Provider = FillProvider< - JoinFill< - JoinFill, NonceFiller>, ChainIdFiller>, - SignerFiller, - >, - RootProvider>, - Http, - Ethereum, ->; - -#[derive(Debug)] -pub struct AnvilRunner { - process: Child, - provider: Provider, - pub endpoint: String, - address: Address, - secret_key: String, -} - -impl AnvilRunner { - pub async fn new() -> Result { - let port = find_free_port(); - - let process = Command::new("anvil") - .arg("--port") - .arg(&port.to_string()) - .arg("--silent") - .arg("--disable-block-gas-limit") - .spawn() - .expect("Could not start background Anvil"); - - let endpoint = format!("http://127.0.0.1:{port}"); - - if !is_anvil_up(&endpoint).await { - panic!("Error bringing up Anvil") - } - let secret_key = - "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80".to_string(); - let wallet: LocalWallet = secret_key.parse().unwrap(); - - let address = wallet.address(); - - let provider = ProviderBuilder::new() - .with_recommended_fillers() - .signer(EthereumSigner::from(wallet)) - .on_http(Url::from_str(&endpoint).unwrap()); - Ok(AnvilRunner { process, endpoint, provider, address, secret_key }) - } - - pub fn provider(&self) -> &Provider { - &self.provider - } - - pub fn endpoint(&self) -> String { - self.endpoint.clone() - } - - pub fn address(&self) -> Address { - self.address - } - - pub fn secret_key(&self) -> String { - self.secret_key.clone() - } -} - -impl Drop for AnvilRunner { - fn drop(&mut self) { - self.process.kill().expect("Cannot kill process"); - } -} - -async fn post_dummy_request(url: &String) -> Result, hyper::Error> { - let req = request::Request::post(url) - .header("content-type", "application/json") - .body(hyper::Body::from( - serde_json::json!({ - "jsonrpc": "2.0", - "method": "eth_blockNumberfuiorhgorueh", - "params": [], - "id": "1" - }) - .to_string(), - )) - .unwrap(); - - HyperClient::new().request(req).await -} - -async fn is_anvil_up(endpoint: &String) -> bool { - let mut retries = 0; - let max_retries = 10; - while retries < max_retries { - if let Ok(anvil_block_rsp) = post_dummy_request(endpoint).await { - assert_eq!(anvil_block_rsp.status(), StatusCode::OK); - return true; - } - retries += 1; - tokio::time::sleep(time::Duration::from_millis(500)).await; - } - false -} - #[cfg(test)] mod tests { use super::*;