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 Jun 26, 2024
1 parent bb0e379 commit cd36166
Show file tree
Hide file tree
Showing 10 changed files with 82 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" }
thiserror.workspace = true
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,12 +3,15 @@ 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};
use blockifier::execution::contract_class::{ClassInfo, ContractClass, ContractClassV1};
use starknet_api::rpc_transaction::{RPCDeclareTransaction, RPCTransaction};
use starknet_api::transaction::TransactionHash;
use starknet_mempool_infra::component_runner::{ComponentRunner, ComponentStartError};
use starknet_mempool_infra::empty_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 @@ -177,3 +180,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(())
}
}
2 changes: 2 additions & 0 deletions crates/gateway/src/gateway_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use starknet_api::rpc_transaction::RPCTransaction;
use starknet_api::transaction::TransactionHash;
use starknet_mempool::communication::create_mempool_server;
use starknet_mempool::mempool::Mempool;
use starknet_mempool_infra::component_server::CommunicationServer;
use starknet_mempool_types::communication::{MempoolClientImpl, MempoolRequestAndResponseSender};
use tokio::sync::mpsc::channel;
use tokio::task;
Expand Down Expand Up @@ -76,6 +77,7 @@ async fn test_add_tx(
) {
// TODO(Tsabary): wrap creation of channels in dedicated functions, take channel capacity from
// config.

let (tx_mempool, rx_mempool) =
channel::<MempoolRequestAndResponseSender>(MEMPOOL_INVOCATIONS_QUEUE_SIZE);
let mut mempool_server = create_mempool_server(mempool, rx_mempool);
Expand Down
22 changes: 21 additions & 1 deletion crates/mempool_infra/src/component_server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use async_trait::async_trait;
use tokio::sync::mpsc::Receiver;

use crate::component_definitions::{ComponentRequestAndResponseSender, ComponentRequestHandler};
Expand Down Expand Up @@ -25,7 +26,7 @@ where
Self { component, rx }
}

pub async fn start(&mut self) {
pub async fn request_response_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 +37,22 @@ where
}
}
}

#[async_trait]
pub trait CommunicationServer: Send + Sync {
async fn start(&mut self);
}

#[async_trait]
impl<Component, Request, Response> CommunicationServer
for ComponentServer<Component, Request, Response>
where
Component: ComponentRequestHandler<Request, Response> + Send + Sync,
Request: Send + Sync,
Response: Send + Sync,
{
async fn start(&mut self) {
self.request_response_loop().await;
println!("ComponentServer completed unexpectedly.");
}
}
33 changes: 33 additions & 0 deletions crates/mempool_infra/src/empty_server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use async_trait::async_trait;

// use tokio::sync::mpsc::channel;

// use crate::component_definitions::{ComponentRequestAndResponseSender,
// ComponentRequestHandler}; use crate::component_runner::{ComponentRunner,
// ComponentStartError};
use crate::component_runner::ComponentRunner;
use crate::component_server::CommunicationServer;

pub struct EmptyServer<T: ComponentRunner + Send> {
component: T,
}

impl<T: ComponentRunner + Send + Sync> EmptyServer<T> {
pub fn new(component: T) -> Self {
Self { component }
}
}

#[async_trait]
impl<T: ComponentRunner + Send + Sync> CommunicationServer 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)
}
1 change: 1 addition & 0 deletions crates/mempool_infra/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod component_client;
pub mod component_definitions;
pub mod component_runner;
pub mod component_server;
pub mod empty_server;
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::{CommunicationServer, 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::CommunicationServer;
use starknet_mempool_types::communication::{
MempoolClient, MempoolClientImpl, MempoolRequestAndResponseSender,
};
Expand Down

0 comments on commit cd36166

Please sign in to comment.