Skip to content

Commit

Permalink
feat: adding empty server and refactoring ComponentServer
Browse files Browse the repository at this point in the history
commit-id:689882a6
  • Loading branch information
lev-starkware committed Jul 1, 2024
1 parent eccc3e2 commit 1faa04f
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ workspace = true
testing = []

[dependencies]
async-trait.workspace = true
axum.workspace = true
blockifier.workspace = true
cairo-lang-starknet-classes.workspace = true
Expand All @@ -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"}
Expand Down
18 changes: 18 additions & 0 deletions crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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};
Expand Down Expand Up @@ -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(())
}
}
1 change: 1 addition & 0 deletions crates/gateway/src/gateway_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
47 changes: 46 additions & 1 deletion crates/mempool_infra/src/component_server.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion crates/mempool_infra/tests/component_server_client_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions crates/tests-integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions crates/tests-integration/src/integration_test_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down

0 comments on commit 1faa04f

Please sign in to comment.