From abfec0ea59ca88022f6b50026ddb09ae5b3c4fa9 Mon Sep 17 00:00:00 2001 From: sekiro Date: Fri, 16 Aug 2024 23:49:22 -0700 Subject: [PATCH 01/13] add axum --- Cargo.lock | 1 + Cargo.toml | 5 ++++- bin/opt8n/Cargo.toml | 1 + bin/opt8n/src/cmd/server.rs | 36 ++++++++++++++++++++++++++++++++++++ bin/opt8n/src/opt8n.rs | 2 -- 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6e03f4d..ea5fc1b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4649,6 +4649,7 @@ dependencies = [ "alloy-rpc-types", "anvil", "anvil-core", + "axum", "cast", "clap", "color-eyre", diff --git a/Cargo.toml b/Cargo.toml index 3824fed..be596a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ futures = "0.3" clap = { version = "4", features = ["derive"] } shellwords = "1" reqwest = "0.12" +axum = "0.7.5" tracing-subscriber = "0.3.18" # Alloy Dependencies @@ -45,7 +46,9 @@ revm = { version = "12.1", features = ["alloydb", "optimism"] } # Kona + OP Types superchain-registry = "0.2.2" -kona-derive = { git = "https://github.com/ethereum-optimism/kona", rev = "6f7c119d93c854d31de27feadbe11362bafe9cfc", features = ["online"] } +kona-derive = { git = "https://github.com/ethereum-optimism/kona", rev = "6f7c119d93c854d31de27feadbe11362bafe9cfc", features = [ + "online", +] } # Internal op-test-vectors = { path = "crates/op-test-vectors" } diff --git a/bin/opt8n/Cargo.toml b/bin/opt8n/Cargo.toml index b01cfd5..9a5cd23 100644 --- a/bin/opt8n/Cargo.toml +++ b/bin/opt8n/Cargo.toml @@ -16,6 +16,7 @@ tracing.workspace = true tokio.workspace = true futures.workspace = true color-eyre.workspace = true +axum.workspace = true # CLI clap.workspace = true diff --git a/bin/opt8n/src/cmd/server.rs b/bin/opt8n/src/cmd/server.rs index e3241d9..8b71622 100644 --- a/bin/opt8n/src/cmd/server.rs +++ b/bin/opt8n/src/cmd/server.rs @@ -1,5 +1,13 @@ use anvil::cmd::NodeArgs; +use axum::extract::State; +use axum::Router; use clap::Parser; +use futures::future::Ready; +use hyper::service::{make_servce, service_fn, Service}; +use hyper::{body::Body, client::conn::http1, Method, Request, Response}; +use std::sync::Arc; +use std::{convert::Infallible, net::SocketAddr}; +use tokio::net::TcpListener; // <-- Ensure this line is present use crate::opt8n::{Opt8n, Opt8nArgs}; @@ -13,6 +21,34 @@ pub struct ServerArgs { impl ServerArgs { pub async fn run(&self) -> color_eyre::Result<()> { + let mut opt8n = Arc::new( + Opt8n::new( + Some(self.node_args.clone()), + self.opt8n_args.output.clone(), + self.opt8n_args.genesis.clone(), + ) + .await?, + ); + + let router = axum::Router::new() + .route("/dump_fixture", axum::routing::post(dump_fixture_handler)) + .with_state(opt8n); + //TODO: add fallback route + + let addr: SocketAddr = ([127, 0, 0, 1], 0).into(); + let listener = TcpListener::bind(addr).await?; + + let bound_server = axum::Server::from_tcp(listener)?; + bound_server.serve(router.into_make_service()).await?; + Ok(()) } } + +async fn dump_fixture_handler() -> &'static str { + "Fixture dumped!" +} + +async fn fallback_handler(State(opt8n): State>, req: Request) -> Response { + // TODO: handle the request +} diff --git a/bin/opt8n/src/opt8n.rs b/bin/opt8n/src/opt8n.rs index f031da6..b41d159 100644 --- a/bin/opt8n/src/opt8n.rs +++ b/bin/opt8n/src/opt8n.rs @@ -8,7 +8,6 @@ use anvil_core::eth::block::Block; use anvil_core::eth::transaction::PendingTransaction; use cast::traces::{GethTraceBuilder, TracingInspectorConfig}; use clap::Parser; -use forge_script::ScriptArgs; use std::{ error::Error, fs::{self, File}, @@ -16,7 +15,6 @@ use std::{ }; use color_eyre::eyre::{ensure, eyre, Result}; -use futures::StreamExt; use op_test_vectors::execution::{ExecutionFixture, ExecutionReceipt, ExecutionResult}; use revm::{ db::{AlloyDB, CacheDB}, From 885240b9a07a604d57b1a627e0635b3a697789fe Mon Sep 17 00:00:00 2001 From: sekiro Date: Sat, 17 Aug 2024 07:47:17 -0700 Subject: [PATCH 02/13] update dump executon fixture endpoint --- Cargo.lock | 1 + bin/opt8n/Cargo.toml | 1 + bin/opt8n/src/cmd/server.rs | 85 +++++++++++++++++++++++++++---------- 3 files changed, 65 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ea5fc1b..cb5cd47 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4662,6 +4662,7 @@ dependencies = [ "serde", "serde_json", "shellwords", + "thiserror", "tokio", "tracing", ] diff --git a/bin/opt8n/Cargo.toml b/bin/opt8n/Cargo.toml index 9a5cd23..f20288b 100644 --- a/bin/opt8n/Cargo.toml +++ b/bin/opt8n/Cargo.toml @@ -37,3 +37,4 @@ revm.workspace = true # OP Types op-test-vectors.workspace = true op-alloy-rpc-types.workspace = true +thiserror.workspace = true diff --git a/bin/opt8n/src/cmd/server.rs b/bin/opt8n/src/cmd/server.rs index 8b71622..297f16c 100644 --- a/bin/opt8n/src/cmd/server.rs +++ b/bin/opt8n/src/cmd/server.rs @@ -1,13 +1,16 @@ use anvil::cmd::NodeArgs; +use axum::body::Body; use axum::extract::State; -use axum::Router; +use axum::http::{Request, StatusCode}; +use axum::response::{IntoResponse, Response}; use clap::Parser; -use futures::future::Ready; -use hyper::service::{make_servce, service_fn, Service}; -use hyper::{body::Body, client::conn::http1, Method, Request, Response}; +use color_eyre::eyre::eyre; +use futures::StreamExt; +use std::net::SocketAddr; use std::sync::Arc; -use std::{convert::Infallible, net::SocketAddr}; -use tokio::net::TcpListener; // <-- Ensure this line is present +use thiserror::Error; +use tokio::net::TcpListener; +use tokio::sync::Mutex; // <-- Ensure this line is present use crate::opt8n::{Opt8n, Opt8nArgs}; @@ -21,34 +24,72 @@ pub struct ServerArgs { impl ServerArgs { pub async fn run(&self) -> color_eyre::Result<()> { - let mut opt8n = Arc::new( - Opt8n::new( - Some(self.node_args.clone()), - self.opt8n_args.output.clone(), - self.opt8n_args.genesis.clone(), - ) - .await?, - ); + let mut opt8n = Opt8n::new( + Some(self.node_args.clone()), + self.opt8n_args.output.clone(), + self.opt8n_args.genesis.clone(), + ) + .await?; + + let opt8n = Arc::new(Mutex::new(opt8n)); let router = axum::Router::new() - .route("/dump_fixture", axum::routing::post(dump_fixture_handler)) + .route("/dump_fixture", axum::routing::post(dump_execution_fixture)) + .fallback(fallback_handler) .with_state(opt8n); - //TODO: add fallback route let addr: SocketAddr = ([127, 0, 0, 1], 0).into(); let listener = TcpListener::bind(addr).await?; - let bound_server = axum::Server::from_tcp(listener)?; - bound_server.serve(router.into_make_service()).await?; + axum::serve(listener, router.into_make_service()).await?; Ok(()) } } -async fn dump_fixture_handler() -> &'static str { - "Fixture dumped!" +async fn dump_execution_fixture(State(opt8n): State>>) -> Result<(), ServerError> { + let mut opt8n = opt8n.lock().await; + + let mut new_blocks = opt8n.eth_api.backend.new_block_notifications(); + + opt8n.mine_block().await; + + let block = new_blocks + .next() + .await + .ok_or(eyre!("No new block")) + .map_err(ServerError::Opt8nError)?; + if let Some(block) = opt8n.eth_api.backend.get_block_by_hash(block.hash) { + opt8n + .generate_execution_fixture(block) + .await + .map_err(ServerError::Opt8nError)?; + } + + Ok(()) } -async fn fallback_handler(State(opt8n): State>, req: Request) -> Response { - // TODO: handle the request +async fn fallback_handler( + State(opt8n): State>, + req: Request, +) -> Result<(), ServerError> { + todo!() +} + +#[derive(Error, Debug)] +pub enum ServerError { + #[error("Opt8n error: {0}")] + Opt8nError(color_eyre::Report), +} + +impl IntoResponse for ServerError { + fn into_response(self) -> Response { + let message = match self { + ServerError::Opt8nError(err) => err.to_string(), + }; + + let body = Body::from(message); + + (StatusCode::INTERNAL_SERVER_ERROR, body).into_response() + } } From 19f0bf6ca8e5c4ba4283a519878c2faa16bf553c Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xkitsune@protonmail.com> Date: Sat, 17 Aug 2024 07:49:36 -0700 Subject: [PATCH 03/13] wip --- bin/opt8n/src/cmd/server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/opt8n/src/cmd/server.rs b/bin/opt8n/src/cmd/server.rs index 297f16c..12e71af 100644 --- a/bin/opt8n/src/cmd/server.rs +++ b/bin/opt8n/src/cmd/server.rs @@ -10,7 +10,7 @@ use std::net::SocketAddr; use std::sync::Arc; use thiserror::Error; use tokio::net::TcpListener; -use tokio::sync::Mutex; // <-- Ensure this line is present +use tokio::sync::Mutex; use crate::opt8n::{Opt8n, Opt8nArgs}; From 20f76fe04f8eef73b4047448c0a10d06a041edd0 Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xkitsune@protonmail.com> Date: Sat, 17 Aug 2024 07:55:03 -0700 Subject: [PATCH 04/13] add todo --- bin/opt8n/src/cmd/server.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bin/opt8n/src/cmd/server.rs b/bin/opt8n/src/cmd/server.rs index 12e71af..9459216 100644 --- a/bin/opt8n/src/cmd/server.rs +++ b/bin/opt8n/src/cmd/server.rs @@ -70,10 +70,14 @@ async fn dump_execution_fixture(State(opt8n): State>>) -> Resul } async fn fallback_handler( - State(opt8n): State>, + State(opt8n): State>>, req: Request, ) -> Result<(), ServerError> { - todo!() + let opt8n = opt8n.lock().await; + + // TODO: Forward request to the ETH api + + Ok(()) } #[derive(Error, Debug)] From 8e7d73e804dfb9d855857f2278d5eb7f5a31621c Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xkitsune@protonmail.com> Date: Sat, 17 Aug 2024 15:08:53 -0700 Subject: [PATCH 05/13] update proxy server --- Cargo.lock | 17 ++++++++++++ Cargo.toml | 3 +-- bin/opt8n/Cargo.toml | 4 ++- bin/opt8n/src/cmd/server.rs | 53 +++++++++++++++++++++++++++++++++---- 4 files changed, 69 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cb5cd47..b8f38ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4656,8 +4656,10 @@ dependencies = [ "forge-script", "foundry-common", "futures", + "http-body-util", "op-alloy-rpc-types", "op-test-vectors", + "reqwest", "revm 12.1.0", "serde", "serde_json", @@ -5407,10 +5409,12 @@ dependencies = [ "tokio", "tokio-native-tls", "tokio-rustls 0.26.0", + "tokio-util", "tower-service", "url", "wasm-bindgen", "wasm-bindgen-futures", + "wasm-streams", "web-sys", "webpki-roots", "winreg", @@ -7263,6 +7267,19 @@ version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" +[[package]] +name = "wasm-streams" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b65dc4c90b63b118468cf747d8bf3566c1913ef60be765b5730ead9e0a3ba129" +dependencies = [ + "futures-util", + "js-sys", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "web-sys" version = "0.3.69" diff --git a/Cargo.toml b/Cargo.toml index be596a3..924861a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,8 +23,7 @@ tokio = { version = "1", features = ["full"] } futures = "0.3" clap = { version = "4", features = ["derive"] } shellwords = "1" -reqwest = "0.12" -axum = "0.7.5" +reqwest = { version = "0.12", features = ["stream"] } tracing-subscriber = "0.3.18" # Alloy Dependencies diff --git a/bin/opt8n/Cargo.toml b/bin/opt8n/Cargo.toml index f20288b..d506011 100644 --- a/bin/opt8n/Cargo.toml +++ b/bin/opt8n/Cargo.toml @@ -16,7 +16,8 @@ tracing.workspace = true tokio.workspace = true futures.workspace = true color-eyre.workspace = true -axum.workspace = true +axum = "0.7.5" +http-body-util = "0.1.2" # CLI clap.workspace = true @@ -38,3 +39,4 @@ revm.workspace = true op-test-vectors.workspace = true op-alloy-rpc-types.workspace = true thiserror.workspace = true +reqwest.workspace = true diff --git a/bin/opt8n/src/cmd/server.rs b/bin/opt8n/src/cmd/server.rs index 9459216..bc96d3e 100644 --- a/bin/opt8n/src/cmd/server.rs +++ b/bin/opt8n/src/cmd/server.rs @@ -1,11 +1,13 @@ +use alloy_rpc_types::error; use anvil::cmd::NodeArgs; -use axum::body::Body; +use axum::body::{Body, Bytes}; use axum::extract::State; use axum::http::{Request, StatusCode}; use axum::response::{IntoResponse, Response}; use clap::Parser; use color_eyre::eyre::eyre; use futures::StreamExt; +use http_body_util::BodyExt; use std::net::SocketAddr; use std::sync::Arc; use thiserror::Error; @@ -24,7 +26,7 @@ pub struct ServerArgs { impl ServerArgs { pub async fn run(&self) -> color_eyre::Result<()> { - let mut opt8n = Opt8n::new( + let opt8n = Opt8n::new( Some(self.node_args.clone()), self.opt8n_args.output.clone(), self.opt8n_args.genesis.clone(), @@ -73,23 +75,64 @@ async fn fallback_handler( State(opt8n): State>>, req: Request, ) -> Result<(), ServerError> { - let opt8n = opt8n.lock().await; + let anvil_endpoint = opt8n.lock().await.node_handle.http_endpoint(); + proxy_to_anvil(req, anvil_endpoint).await?; + Ok(()) +} - // TODO: Forward request to the ETH api +pub async fn proxy_to_anvil( + req: Request, + anvil_endpoint: String, +) -> Result, ServerError> { + let http_client = reqwest::Client::new(); - Ok(()) + // add endpoiint to request + + let (headers, body) = req.into_parts(); + let body = body + .collect() + .await + .map_err(ServerError::AxumError)? + .to_bytes(); + + let axum_req: Request = Request::from_parts(headers, body); + let mut req = reqwest::Request::try_from(axum_req).expect("TODO: handle error"); + req.url_mut().set_fragment(Some(&anvil_endpoint)); + + let res: Response = http_client + .execute(req) + .await + .expect("TODO: handle error ") + .into(); + + let (headers, body) = res.into_parts(); + + let body = body + .collect() + .await + .map_err(ServerError::ReqwestError)? + .to_bytes() + .into(); + + Ok(Response::from_parts(headers, body)) } #[derive(Error, Debug)] pub enum ServerError { #[error("Opt8n error: {0}")] Opt8nError(color_eyre::Report), + #[error("Axum error: {0}")] + AxumError(axum::Error), + #[error("Reqwest error: {0}")] + ReqwestError(reqwest::Error), } impl IntoResponse for ServerError { fn into_response(self) -> Response { let message = match self { ServerError::Opt8nError(err) => err.to_string(), + ServerError::ReqwestError(err) => err.to_string(), + ServerError::AxumError(err) => err.to_string(), }; let body = Body::from(message); From 2091ccf8197cc213c5b98a9ded25d838272784d5 Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xkitsune@protonmail.com> Date: Sat, 17 Aug 2024 15:19:02 -0700 Subject: [PATCH 06/13] import --- bin/opt8n/src/cmd/server.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/opt8n/src/cmd/server.rs b/bin/opt8n/src/cmd/server.rs index bc96d3e..bf29177 100644 --- a/bin/opt8n/src/cmd/server.rs +++ b/bin/opt8n/src/cmd/server.rs @@ -1,4 +1,3 @@ -use alloy_rpc_types::error; use anvil::cmd::NodeArgs; use axum::body::{Body, Bytes}; use axum::extract::State; From e17261b97c714b7dcb42cee23710a3416c716cc7 Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xkitsune@protonmail.com> Date: Sat, 17 Aug 2024 15:19:18 -0700 Subject: [PATCH 07/13] clippy --- bin/opt8n/src/cmd/repl.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/opt8n/src/cmd/repl.rs b/bin/opt8n/src/cmd/repl.rs index b271c16..c939382 100644 --- a/bin/opt8n/src/cmd/repl.rs +++ b/bin/opt8n/src/cmd/repl.rs @@ -1,6 +1,5 @@ use anvil::cmd::NodeArgs; use clap::{CommandFactory, FromArgMatches, Parser}; -use color_eyre::eyre::eyre; use futures::StreamExt; use serde::{Deserialize, Serialize}; use tokio::io::{AsyncBufReadExt, BufReader}; From 8ce3c7f4cca4bd1760ed4b17b6d5c837ccd49d69 Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xkitsune@protonmail.com> Date: Sat, 17 Aug 2024 15:30:10 -0700 Subject: [PATCH 08/13] add dump channel --- bin/opt8n/src/cmd/server.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/bin/opt8n/src/cmd/server.rs b/bin/opt8n/src/cmd/server.rs index bf29177..1ab5cac 100644 --- a/bin/opt8n/src/cmd/server.rs +++ b/bin/opt8n/src/cmd/server.rs @@ -5,8 +5,11 @@ use axum::http::{Request, StatusCode}; use axum::response::{IntoResponse, Response}; use clap::Parser; use color_eyre::eyre::eyre; +use color_eyre::owo_colors::OwoColorize; +use foundry_common::shell::println; use futures::StreamExt; use http_body_util::BodyExt; +use std::fmt::format; use std::net::SocketAddr; use std::sync::Arc; use thiserror::Error; @@ -34,6 +37,7 @@ impl ServerArgs { let opt8n = Arc::new(Mutex::new(opt8n)); + let (dump_tx, mut dump_rx) = tokio::sync::mpsc::channel::<()>(1); let router = axum::Router::new() .route("/dump_fixture", axum::routing::post(dump_execution_fixture)) .fallback(fallback_handler) @@ -41,8 +45,20 @@ impl ServerArgs { let addr: SocketAddr = ([127, 0, 0, 1], 0).into(); let listener = TcpListener::bind(addr).await?; + let local_addr = listener.local_addr()?; - axum::serve(listener, router.into_make_service()).await?; + let server = axum::serve(listener, router.into_make_service()); + let _ = println!("Opt8n server listening on: {:#?}", local_addr).green(); + + tokio::select! { + err = server => { + todo!("Handle error") + } + + _ = dump_rx.recv() => { + + } + } Ok(()) } @@ -85,8 +101,6 @@ pub async fn proxy_to_anvil( ) -> Result, ServerError> { let http_client = reqwest::Client::new(); - // add endpoiint to request - let (headers, body) = req.into_parts(); let body = body .collect() From f4b9da5ee27ebe451a1836417a1b752529eb2a08 Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xkitsune@protonmail.com> Date: Sat, 17 Aug 2024 15:38:07 -0700 Subject: [PATCH 09/13] send dump tx notif --- bin/opt8n/src/cmd/server.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/bin/opt8n/src/cmd/server.rs b/bin/opt8n/src/cmd/server.rs index 1ab5cac..c133915 100644 --- a/bin/opt8n/src/cmd/server.rs +++ b/bin/opt8n/src/cmd/server.rs @@ -6,14 +6,13 @@ use axum::response::{IntoResponse, Response}; use clap::Parser; use color_eyre::eyre::eyre; use color_eyre::owo_colors::OwoColorize; -use foundry_common::shell::println; use futures::StreamExt; use http_body_util::BodyExt; -use std::fmt::format; use std::net::SocketAddr; use std::sync::Arc; use thiserror::Error; use tokio::net::TcpListener; +use tokio::sync::mpsc::Sender; use tokio::sync::Mutex; use crate::opt8n::{Opt8n, Opt8nArgs}; @@ -38,11 +37,17 @@ impl ServerArgs { let opt8n = Arc::new(Mutex::new(opt8n)); let (dump_tx, mut dump_rx) = tokio::sync::mpsc::channel::<()>(1); - let router = axum::Router::new() + + let dump_fixture_router = axum::Router::new() .route("/dump_fixture", axum::routing::post(dump_execution_fixture)) + .with_state((opt8n.clone(), dump_tx)); + + let fallback_router = axum::Router::new() .fallback(fallback_handler) .with_state(opt8n); + let router = dump_fixture_router.merge(fallback_router); + let addr: SocketAddr = ([127, 0, 0, 1], 0).into(); let listener = TcpListener::bind(addr).await?; let local_addr = listener.local_addr()?; @@ -56,6 +61,7 @@ impl ServerArgs { } _ = dump_rx.recv() => { + let _ = println!("Exuction fixture dumped to: {:#?}", self.opt8n_args.output).green(); } } @@ -64,7 +70,9 @@ impl ServerArgs { } } -async fn dump_execution_fixture(State(opt8n): State>>) -> Result<(), ServerError> { +async fn dump_execution_fixture( + State((opt8n, dump_tx)): State<(Arc>, Sender<()>)>, +) -> Result<(), ServerError> { let mut opt8n = opt8n.lock().await; let mut new_blocks = opt8n.eth_api.backend.new_block_notifications(); @@ -83,6 +91,8 @@ async fn dump_execution_fixture(State(opt8n): State>>) -> Resul .map_err(ServerError::Opt8nError)?; } + dump_tx.send(()); + Ok(()) } From 3d62f9363672542c53fdea27bdba8d92f0b853f1 Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xkitsune@protonmail.com> Date: Tue, 20 Aug 2024 12:51:04 -0400 Subject: [PATCH 10/13] add endpoint to mine prestate --- bin/opt8n/src/cmd/server.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/bin/opt8n/src/cmd/server.rs b/bin/opt8n/src/cmd/server.rs index c133915..fdafb90 100644 --- a/bin/opt8n/src/cmd/server.rs +++ b/bin/opt8n/src/cmd/server.rs @@ -42,11 +42,12 @@ impl ServerArgs { .route("/dump_fixture", axum::routing::post(dump_execution_fixture)) .with_state((opt8n.clone(), dump_tx)); - let fallback_router = axum::Router::new() + let router = axum::Router::new() + .route("/mine_prestate", axum::routing::post(mine_prestate)) .fallback(fallback_handler) .with_state(opt8n); - let router = dump_fixture_router.merge(fallback_router); + let router = dump_fixture_router.merge(router); let addr: SocketAddr = ([127, 0, 0, 1], 0).into(); let listener = TcpListener::bind(addr).await?; @@ -73,6 +74,18 @@ impl ServerArgs { async fn dump_execution_fixture( State((opt8n, dump_tx)): State<(Arc>, Sender<()>)>, ) -> Result<(), ServerError> { + mine_block(opt8n).await?; + dump_tx.send(()).await.map_err(ServerError::SendError)?; + + Ok(()) +} + +async fn mine_prestate(State(opt8n): State<(Arc>)>) -> Result<(), ServerError> { + mine_block(opt8n.clone()).await?; + Ok(()) +} + +async fn mine_block(opt8n: Arc>) -> Result<(), ServerError> { let mut opt8n = opt8n.lock().await; let mut new_blocks = opt8n.eth_api.backend.new_block_notifications(); @@ -91,8 +104,6 @@ async fn dump_execution_fixture( .map_err(ServerError::Opt8nError)?; } - dump_tx.send(()); - Ok(()) } @@ -148,6 +159,8 @@ pub enum ServerError { AxumError(axum::Error), #[error("Reqwest error: {0}")] ReqwestError(reqwest::Error), + #[error("Senderror: {0}")] + SendError(tokio::sync::mpsc::error::SendError<()>), } impl IntoResponse for ServerError { @@ -156,6 +169,7 @@ impl IntoResponse for ServerError { ServerError::Opt8nError(err) => err.to_string(), ServerError::ReqwestError(err) => err.to_string(), ServerError::AxumError(err) => err.to_string(), + ServerError::SendError(err) => err.to_string(), }; let body = Body::from(message); From 2ae5a01b14ab1af49499c27f1af124916a30ab86 Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xKitsune@protonmail.com> Date: Tue, 20 Aug 2024 16:54:27 -0400 Subject: [PATCH 11/13] remove scriptargs --- bin/opt8n/src/cmd/script.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/bin/opt8n/src/cmd/script.rs b/bin/opt8n/src/cmd/script.rs index e75b094..2e49618 100644 --- a/bin/opt8n/src/cmd/script.rs +++ b/bin/opt8n/src/cmd/script.rs @@ -1,5 +1,5 @@ -use anvil::cmd::NodeArgs; -use clap::Parser; +use anvil::{cmd::NodeArgs, Hardfork}; +use clap::{Parser, ValueHint}; use color_eyre::eyre::eyre; use futures::StreamExt; @@ -9,8 +9,10 @@ use crate::opt8n::{Opt8n, Opt8nArgs}; pub struct ScriptArgs { #[command(flatten)] opt8n_args: Opt8nArgs, - #[command(flatten)] - inner: forge_script::ScriptArgs, + // #[command(flatten)] + // inner: forge_script::ScriptArgs, + #[arg(value_hint = ValueHint::FilePath)] + pub path: String, #[command(flatten)] pub node_args: NodeArgs, } @@ -24,23 +26,26 @@ impl ScriptArgs { ) .await?; + let mut script_args = forge_script::ScriptArgs::default(); + script_args.path = self.path.clone(); + foundry_common::shell::set_shell(foundry_common::shell::Shell::from_args( - self.inner.opts.silent, - self.inner.json, + script_args.opts.silent, + script_args.json, ))?; - self.inner.broadcast = true; - self.inner.evm_opts.sender = Some( + script_args.broadcast = true; + script_args.evm_opts.sender = Some( opt8n .node_handle .genesis_accounts() .last() .expect("Could not get genesis account"), ); - self.inner.unlocked = true; - self.inner.evm_opts.fork_url = Some(opt8n.node_handle.http_endpoint()); + script_args.unlocked = true; + script_args.evm_opts.fork_url = Some(opt8n.node_handle.http_endpoint()); - run_script(opt8n, Box::new(self.inner)).await?; + run_script(opt8n, Box::new(script_args)).await?; Ok(()) } From 6cdc30b221cc1675b276bb2ef68578553677b552 Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xKitsune@protonmail.com> Date: Tue, 20 Aug 2024 16:56:53 -0400 Subject: [PATCH 12/13] clippy --- bin/opt8n/src/cmd/script.rs | 10 ++++++---- bin/opt8n/src/cmd/server.rs | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/bin/opt8n/src/cmd/script.rs b/bin/opt8n/src/cmd/script.rs index 2e49618..f4867d0 100644 --- a/bin/opt8n/src/cmd/script.rs +++ b/bin/opt8n/src/cmd/script.rs @@ -1,4 +1,4 @@ -use anvil::{cmd::NodeArgs, Hardfork}; +use anvil::cmd::NodeArgs; use clap::{Parser, ValueHint}; use color_eyre::eyre::eyre; use futures::StreamExt; @@ -18,7 +18,7 @@ pub struct ScriptArgs { } impl ScriptArgs { - pub async fn run(mut self) -> color_eyre::Result<()> { + pub async fn run(self) -> color_eyre::Result<()> { let opt8n = Opt8n::new( Some(self.node_args.clone()), self.opt8n_args.output.clone(), @@ -26,8 +26,10 @@ impl ScriptArgs { ) .await?; - let mut script_args = forge_script::ScriptArgs::default(); - script_args.path = self.path.clone(); + let mut script_args = forge_script::ScriptArgs { + path: self.path.clone(), + ..Default::default() + }; foundry_common::shell::set_shell(foundry_common::shell::Shell::from_args( script_args.opts.silent, diff --git a/bin/opt8n/src/cmd/server.rs b/bin/opt8n/src/cmd/server.rs index fdafb90..1bc23da 100644 --- a/bin/opt8n/src/cmd/server.rs +++ b/bin/opt8n/src/cmd/server.rs @@ -58,7 +58,7 @@ impl ServerArgs { tokio::select! { err = server => { - todo!("Handle error") + todo!("Handle server error: {:#?}", err); } _ = dump_rx.recv() => { @@ -80,7 +80,7 @@ async fn dump_execution_fixture( Ok(()) } -async fn mine_prestate(State(opt8n): State<(Arc>)>) -> Result<(), ServerError> { +async fn mine_prestate(State(opt8n): State>>) -> Result<(), ServerError> { mine_block(opt8n.clone()).await?; Ok(()) } From 6dbbb7649330e2dc7032878d54c42e804515d753 Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xKitsune@protonmail.com> Date: Tue, 20 Aug 2024 19:47:28 -0400 Subject: [PATCH 13/13] remove unused code --- bin/opt8n/src/cmd/script.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/bin/opt8n/src/cmd/script.rs b/bin/opt8n/src/cmd/script.rs index f4867d0..b412eca 100644 --- a/bin/opt8n/src/cmd/script.rs +++ b/bin/opt8n/src/cmd/script.rs @@ -9,8 +9,6 @@ use crate::opt8n::{Opt8n, Opt8nArgs}; pub struct ScriptArgs { #[command(flatten)] opt8n_args: Opt8nArgs, - // #[command(flatten)] - // inner: forge_script::ScriptArgs, #[arg(value_hint = ValueHint::FilePath)] pub path: String, #[command(flatten)]