Skip to content

Commit

Permalink
chore: clean http server module
Browse files Browse the repository at this point in the history
commit-id:5267cf65
  • Loading branch information
Itay-Tsabary-Starkware committed Oct 15, 2024
1 parent fecee29 commit f376d08
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion crates/gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ async-trait.workspace = true
axum.workspace = true
blockifier = { workspace = true, features = ["testing"] }
cairo-lang-starknet-classes.workspace = true
hyper.workspace = true
mempool_test_utils.workspace = true
papyrus_config.workspace = true
papyrus_network_types.workspace = true
Expand Down
7 changes: 0 additions & 7 deletions crates/gateway/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ pub type StatelessTransactionValidatorResult<T> = Result<T, StatelessTransaction

pub type StatefulTransactionValidatorResult<T> = Result<T, GatewaySpecError>;

/// Errors originating from `[`Gateway::run`]` command, to be handled by infrastructure code.
#[derive(Debug, Error)]
pub enum GatewayRunError {
#[error(transparent)]
ServerStartupError(#[from] hyper::Error),
}

#[derive(Debug, Error)]
pub enum RPCStateReaderError {
#[error("Block not found for request {0}")]
Expand Down
3 changes: 2 additions & 1 deletion crates/http_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ workspace = true

[dependencies]
axum.workspace = true
hyper.workspace = true
papyrus_config.workspace = true
serde.workspace = true
starknet_api.workspace = true
starknet_gateway.workspace = true
starknet_gateway_types.workspace = true
starknet_mempool_infra.workspace = true
thiserror.workspace = true
tracing.workspace = true
validator.workspace = true

Expand Down
6 changes: 6 additions & 0 deletions crates/http_server/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
use thiserror::Error;

#[derive(Debug, Error)]
pub enum HttpServerRunError {
#[error(transparent)]
ServerStartupError(#[from] hyper::Error),
}
20 changes: 5 additions & 15 deletions crates/http_server/src/http_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use axum::routing::{get, post};
use axum::{async_trait, Json, Router};
use starknet_api::rpc_transaction::RpcTransaction;
use starknet_api::transaction::TransactionHash;
use starknet_gateway::errors::GatewayRunError;
use starknet_gateway_types::communication::SharedGatewayClient;
use starknet_gateway_types::errors::GatewaySpecError;
use starknet_gateway_types::gateway_types::GatewayInput;
Expand All @@ -16,6 +15,7 @@ use starknet_mempool_infra::errors::ComponentError;
use tracing::{error, info, instrument};

use crate::config::HttpServerConfig;
use crate::errors::HttpServerRunError;

#[cfg(test)]
#[path = "http_server_test.rs"]
Expand All @@ -39,8 +39,7 @@ impl HttpServer {
HttpServer { config, app_state }
}

// TODO(Tsabary/Lev): Rename "GatewayRunError" to "HttpServerRunError".
pub async fn run(&mut self) -> Result<(), GatewayRunError> {
pub async fn run(&mut self) -> Result<(), HttpServerRunError> {
// Parses the bind address from HttpServerConfig, returning an error for invalid addresses.
let HttpServerConfig { ip, port } = self.config;
let addr = SocketAddr::new(ip, port);
Expand All @@ -52,32 +51,23 @@ impl HttpServer {
}

pub fn app(&self) -> Router {
Router::new()
.route("/is_alive", get(is_alive))
.route("/add_tx", post(add_tx))
.with_state(self.app_state.clone())
Router::new().route("/add_tx", post(add_tx)).with_state(self.app_state.clone())
}
}

// HttpServer handlers.

#[instrument]
async fn is_alive() -> HttpServerResult<String> {
unimplemented!("Future handling should be implemented here.");
}

#[instrument(skip(app_state))]
async fn add_tx(
State(app_state): State<AppState>,
Json(tx): Json<RpcTransaction>,
) -> HttpServerResult<Json<TransactionHash>> {
let gateway_input: GatewayInput = GatewayInput { rpc_tx: tx.clone(), message_metadata: None };

info!("Received tx: {:?}", tx);
let gateway_input = GatewayInput { rpc_tx: tx, message_metadata: None };
let add_tx_result = app_state.gateway_client.add_tx(gateway_input).await.map_err(|join_err| {
error!("Failed to process tx: {}", join_err);
GatewaySpecError::UnexpectedError { data: "Internal server error".to_owned() }
});

add_tx_result_as_json(add_tx_result)
}

Expand Down

0 comments on commit f376d08

Please sign in to comment.