diff --git a/Cargo.lock b/Cargo.lock index 0491c040d..ebd50b556 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5489,6 +5489,7 @@ name = "starknet_gateway" version = "0.0.0" dependencies = [ "assert_matches", + "async-trait", "axum", "blockifier 0.7.0-dev.1 (git+https://github.com/starkware-libs/blockifier.git?branch=main-mempool)", "cairo-lang-starknet-classes", @@ -5502,6 +5503,7 @@ dependencies = [ "serde_json", "starknet_api", "starknet_mempool", + "starknet_mempool_infra", "starknet_mempool_types", "starknet_sierra_compile", "test_utils", diff --git a/crates/gateway/Cargo.toml b/crates/gateway/Cargo.toml index c95142737..0e12f9369 100644 --- a/crates/gateway/Cargo.toml +++ b/crates/gateway/Cargo.toml @@ -12,6 +12,7 @@ workspace = true testing = [] [dependencies] +async-trait.workspace = true axum.workspace = true assert_matches.workspace = true blockifier.workspace = true @@ -23,6 +24,7 @@ reqwest.workspace = true serde.workspace = true serde_json.workspace = true starknet_api.workspace = true +starknet_mempool_infra = { path = "../mempool_infra", version = "0.0" } starknet_mempool_types = { path = "../mempool_types", version = "0.0" } starknet_sierra_compile = { path = "../starknet_sierra_compile", version = "0.0" } test_utils = { path = "../test_utils", version = "0.0"} diff --git a/crates/gateway/src/communication.rs b/crates/gateway/src/communication.rs new file mode 100644 index 000000000..0c4a02ce5 --- /dev/null +++ b/crates/gateway/src/communication.rs @@ -0,0 +1,7 @@ +use starknet_mempool_infra::component_server::{create_empty_server, EmptyServer}; + +use crate::gateway::Gateway; + +pub fn create_gateway_server(gateway: Gateway) -> EmptyServer { + create_empty_server(gateway) +} diff --git a/crates/gateway/src/gateway.rs b/crates/gateway/src/gateway.rs index 8c570e297..e517ffd0e 100644 --- a/crates/gateway/src/gateway.rs +++ b/crates/gateway/src/gateway.rs @@ -3,6 +3,7 @@ use std::net::SocketAddr; use std::panic; use std::sync::Arc; +use async_trait::async_trait; use axum::extract::State; use axum::routing::{get, post}; use axum::{Json, Router}; @@ -11,6 +12,7 @@ use blockifier::execution::execution_utils::felt_to_stark_felt; use starknet_api::core::CompiledClassHash; use starknet_api::rpc_transaction::{RPCDeclareTransaction, RPCTransaction}; use starknet_api::transaction::TransactionHash; +use starknet_mempool_infra::component_runner::{ComponentRunner, ComponentStartError}; use starknet_mempool_types::communication::SharedMempoolClient; use starknet_mempool_types::mempool_types::{Account, MempoolInput}; use starknet_sierra_compile::compile::compile_sierra_to_casm; @@ -189,3 +191,12 @@ pub fn create_gateway( let state_reader_factory = Arc::new(RpcStateReaderFactory { config: rpc_state_reader_config }); Gateway::new(config, state_reader_factory, client) } + +#[async_trait] +impl ComponentRunner for Gateway { + async fn start(&mut self) -> Result<(), ComponentStartError> { + // TODO(Lev, 23/07/2024): Implement the real logic. + println!("Gateway::start()"); + Ok(()) + } +} diff --git a/crates/gateway/src/gateway_test.rs b/crates/gateway/src/gateway_test.rs index 85d7c3eb9..ad113140b 100644 --- a/crates/gateway/src/gateway_test.rs +++ b/crates/gateway/src/gateway_test.rs @@ -15,6 +15,7 @@ use starknet_api::rpc_transaction::{RPCDeclareTransaction, RPCTransaction}; use starknet_api::transaction::TransactionHash; use starknet_mempool::communication::create_mempool_server; use starknet_mempool::mempool::Mempool; +use starknet_mempool_infra::component_server::ComponentServerStarter; use starknet_mempool_types::communication::{MempoolClientImpl, MempoolRequestAndResponseSender}; use starknet_sierra_compile::errors::CompilationUtilError; use tokio::sync::mpsc::channel; diff --git a/crates/gateway/src/lib.rs b/crates/gateway/src/lib.rs index 131c834c6..aa6396c88 100644 --- a/crates/gateway/src/lib.rs +++ b/crates/gateway/src/lib.rs @@ -1,3 +1,4 @@ +pub mod communication; pub mod compiler_version; pub mod config; pub mod errors; diff --git a/crates/mempool_infra/src/component_server.rs b/crates/mempool_infra/src/component_server.rs index ca740ba09..7ec15c756 100644 --- a/crates/mempool_infra/src/component_server.rs +++ b/crates/mempool_infra/src/component_server.rs @@ -2,6 +2,7 @@ use std::marker::PhantomData; use std::net::{IpAddr, SocketAddr}; use std::sync::Arc; +use async_trait::async_trait; use bincode::{deserialize, serialize}; use hyper::body::to_bytes; use hyper::header::CONTENT_TYPE; @@ -14,6 +15,7 @@ use tokio::sync::Mutex; use crate::component_definitions::{ ComponentRequestAndResponseSender, ComponentRequestHandler, APPLICATION_OCTET_STREAM, }; +use crate::component_runner::ComponentRunner; pub struct ComponentServer where @@ -37,8 +39,22 @@ where ) -> Self { Self { component, rx } } +} - pub async fn start(&mut self) { +#[async_trait] +pub trait ComponentServerStarter: Send + Sync { + async fn start(&mut self); +} + +#[async_trait] +impl ComponentServerStarter + for ComponentServer +where + Component: ComponentRequestHandler + Send + Sync, + Request: Send + Sync, + Response: Send + Sync, +{ + async fn start(&mut self) { while let Some(request_and_res_tx) = self.rx.recv().await { let request = request_and_res_tx.request; let tx = request_and_res_tx.tx; @@ -114,3 +130,27 @@ where Ok(http_response) } } + +pub struct EmptyServer { + component: T, +} + +impl EmptyServer { + pub fn new(component: T) -> Self { + Self { component } + } +} + +#[async_trait] +impl ComponentServerStarter for EmptyServer { + async fn start(&mut self) { + match self.component.start().await { + Ok(_) => println!("ComponentServer::start() completed."), + Err(err) => println!("ComponentServer::start() failed: {:?}", err), + } + } +} + +pub fn create_empty_server(component: T) -> EmptyServer { + EmptyServer::new(component) +} diff --git a/crates/mempool_infra/tests/component_server_client_test.rs b/crates/mempool_infra/tests/component_server_client_test.rs index fefb97b0e..8dcdf51f8 100644 --- a/crates/mempool_infra/tests/component_server_client_test.rs +++ b/crates/mempool_infra/tests/component_server_client_test.rs @@ -6,7 +6,7 @@ use starknet_mempool_infra::component_client::ComponentClient; use starknet_mempool_infra::component_definitions::{ ComponentRequestAndResponseSender, ComponentRequestHandler, }; -use starknet_mempool_infra::component_server::ComponentServer; +use starknet_mempool_infra::component_server::{ComponentServer, ComponentServerStarter}; use tokio::sync::mpsc::{channel, Sender}; use tokio::task; diff --git a/crates/tests-integration/Cargo.toml b/crates/tests-integration/Cargo.toml index b6e36b7d1..66fd3e195 100644 --- a/crates/tests-integration/Cargo.toml +++ b/crates/tests-integration/Cargo.toml @@ -23,6 +23,7 @@ starknet_api.workspace = true starknet_client.workspace = true starknet_gateway = { path = "../gateway", version = "0.0", features = ["testing"] } starknet_mempool = { path = "../mempool", version = "0.0" } +starknet_mempool_infra = { path = "../mempool_infra", version = "0.0" } starknet_mempool_types = { path = "../mempool_types", version = "0.0" } starknet_task_executor = { path = "../task_executor", version = "0.0" } strum.workspace = true diff --git a/crates/tests-integration/src/integration_test_setup.rs b/crates/tests-integration/src/integration_test_setup.rs index b51db5831..4a5891752 100644 --- a/crates/tests-integration/src/integration_test_setup.rs +++ b/crates/tests-integration/src/integration_test_setup.rs @@ -7,6 +7,7 @@ use starknet_gateway::config::GatewayNetworkConfig; use starknet_gateway::errors::GatewayError; use starknet_mempool::communication::create_mempool_server; use starknet_mempool::mempool::Mempool; +use starknet_mempool_infra::component_server::ComponentServerStarter; use starknet_mempool_types::communication::{ MempoolClient, MempoolClientImpl, MempoolRequestAndResponseSender, };