From 1faa04fd820196d8441e4bbe78a4931cf18f0e47 Mon Sep 17 00:00:00 2001
From: Lev Roitman <lev@starkware.co>
Date: Wed, 26 Jun 2024 18:09:23 +0300
Subject: [PATCH] feat: adding empty server and refactoring ComponentServer

commit-id:689882a6
---
 Cargo.lock                                    |  2 +
 crates/gateway/Cargo.toml                     |  2 +
 crates/gateway/src/gateway.rs                 | 18 +++++++
 crates/gateway/src/gateway_test.rs            |  1 +
 crates/mempool_infra/src/component_server.rs  | 47 ++++++++++++++++++-
 .../tests/component_server_client_test.rs     |  2 +-
 crates/tests-integration/Cargo.toml           |  1 +
 .../src/integration_test_setup.rs             |  1 +
 8 files changed, 72 insertions(+), 2 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index c48648ae3..61c0f2b15 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 b66a6ec84..4311e9a77 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
 blockifier.workspace = true
 cairo-lang-starknet-classes.workspace = true
@@ -22,6 +23,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/gateway.rs b/crates/gateway/src/gateway.rs
index e0646ef3f..60473110e 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,8 @@ 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_infra::component_server::{create_empty_server, EmptyServer};
 use starknet_mempool_types::communication::SharedMempoolClient;
 use starknet_mempool_types::mempool_types::{Account, MempoolInput};
 use starknet_sierra_compile::compile::{compile_sierra_to_casm, CompilationUtilError};
@@ -188,3 +191,18 @@ pub fn create_gateway(
     let state_reader_factory = Arc::new(RpcStateReaderFactory { config: rpc_state_reader_config });
     Gateway::new(config, state_reader_factory, client)
 }
+
+pub type GatewayCommunicationServer = EmptyServer<Gateway>;
+
+pub fn create_gateway_server(gateway: Gateway) -> GatewayCommunicationServer {
+    create_empty_server(gateway)
+}
+
+#[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 7c1fb8578..bd7b34b51 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::compile::CompilationUtilError;
 use tokio::sync::mpsc::channel;
diff --git a/crates/mempool_infra/src/component_server.rs b/crates/mempool_infra/src/component_server.rs
index 0a726100e..39f7682e5 100644
--- a/crates/mempool_infra/src/component_server.rs
+++ b/crates/mempool_infra/src/component_server.rs
@@ -1,6 +1,8 @@
+use async_trait::async_trait;
 use tokio::sync::mpsc::Receiver;
 
 use crate::component_definitions::{ComponentRequestAndResponseSender, ComponentRequestHandler};
+use crate::component_runner::ComponentRunner;
 
 pub struct ComponentServer<Component, Request, Response>
 where
@@ -25,7 +27,7 @@ where
         Self { component, rx }
     }
 
-    pub async fn start(&mut self) {
+    pub async fn start_main_loop(&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;
@@ -36,3 +38,46 @@ where
         }
     }
 }
+
+#[async_trait]
+pub trait ComponentServerStarter: Send + Sync {
+    async fn start(&mut self);
+}
+
+#[async_trait]
+impl<Component, Request, Response> ComponentServerStarter
+    for ComponentServer<Component, Request, Response>
+where
+    Component: ComponentRequestHandler<Request, Response> + Send + Sync,
+    Request: Send + Sync,
+    Response: Send + Sync,
+{
+    async fn start(&mut self) {
+        self.start_main_loop().await;
+        println!("ComponentServer completed unexpectedly.");
+    }
+}
+
+pub struct EmptyServer<T: ComponentRunner + Send + Sync> {
+    component: T,
+}
+
+impl<T: ComponentRunner + Send + Sync> EmptyServer<T> {
+    pub fn new(component: T) -> Self {
+        Self { component }
+    }
+}
+
+#[async_trait]
+impl<T: ComponentRunner + Send + Sync> ComponentServerStarter for EmptyServer<T> {
+    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<T: ComponentRunner + Send + Sync>(component: T) -> EmptyServer<T> {
+    EmptyServer::new(component)
+}
\ No newline at end of file
diff --git a/crates/mempool_infra/tests/component_server_client_test.rs b/crates/mempool_infra/tests/component_server_client_test.rs
index fefb97b0e..0caf693bb 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::{ComponentServerStarter, ComponentServer};
 use tokio::sync::mpsc::{channel, Sender};
 use tokio::task;
 
diff --git a/crates/tests-integration/Cargo.toml b/crates/tests-integration/Cargo.toml
index bfd56dbfb..2fb3eb1a4 100644
--- a/crates/tests-integration/Cargo.toml
+++ b/crates/tests-integration/Cargo.toml
@@ -22,6 +22,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,
 };