Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions crates/apollo_http_server/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use apollo_config_manager_types::communication::ConfigManagerClientError;
use apollo_gateway_types::communication::GatewayClientError;
use apollo_gateway_types::deprecated_gateway_error::{
KnownStarknetErrorCode,
Expand All @@ -23,19 +24,24 @@ pub enum HttpServerRunError {
#[derive(Error, Debug)]
pub enum HttpServerError {
#[error(transparent)]
GatewayClientError(#[from] Box<GatewayClientError>),
ConfigManagerClientError(#[from] ConfigManagerClientError),
#[error(transparent)]
DecompressionError(#[from] CompressionError),
#[error(transparent)]
DeserializationError(#[from] serde_json::Error),
#[error(transparent)]
DecompressionError(#[from] CompressionError),
GatewayClientError(#[from] Box<GatewayClientError>),
}

impl IntoResponse for HttpServerError {
fn into_response(self) -> Response {
match self {
HttpServerError::GatewayClientError(e) => gw_client_err_into_response(*e),
HttpServerError::DeserializationError(e) => serde_error_into_response(e),
HttpServerError::ConfigManagerClientError(e) => {
config_manager_client_err_into_response(e)
}
HttpServerError::DecompressionError(e) => compression_error_into_response(e),
HttpServerError::DeserializationError(e) => serde_error_into_response(e),
HttpServerError::GatewayClientError(e) => gw_client_err_into_response(*e),
}
}
}
Expand Down Expand Up @@ -89,6 +95,15 @@ fn gw_client_err_into_response(err: GatewayClientError) -> Response {
(response_code, response_body).into_response()
}

fn config_manager_client_err_into_response(err: ConfigManagerClientError) -> Response {
let (response_code, config_manager_error) = (
StatusCode::INTERNAL_SERVER_ERROR,
StarknetError::internal_with_logging("Failed to process client request", err),
);
let response_body = serialize_error(&config_manager_error);
(response_code, response_body).into_response()
}

/// Serializes a `StarknetError` into an HTTP response, encode the error message
/// to defend potential Cross-Site risks.
fn serialize_error(error: &StarknetError) -> Response {
Expand Down
13 changes: 6 additions & 7 deletions crates/apollo_http_server/src/http_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ pub struct HttpServer {

#[derive(Clone)]
pub struct AppState {
// TODO(Tsabary): use the config manager in the tx processing.
_config_manager_client: SharedConfigManagerClient,
config_manager_client: SharedConfigManagerClient,
pub gateway_client: SharedGatewayClient,
}

Expand All @@ -68,7 +67,7 @@ impl HttpServer {
config_manager_client: SharedConfigManagerClient,
gateway_client: SharedGatewayClient,
) -> Self {
let app_state = AppState { _config_manager_client: config_manager_client, gateway_client };
let app_state = AppState { config_manager_client, gateway_client };
HttpServer { config, app_state }
}

Expand All @@ -93,8 +92,7 @@ impl HttpServer {
.with_state(self.app_state.clone())
// Rest api endpoint
.route("/gateway/add_transaction", post({
let max_sierra_program_size = self.config.dynamic_config.max_sierra_program_size;
move |app_state: State<AppState>, headers: HeaderMap, tx: String| add_tx(app_state, headers, tx, max_sierra_program_size)
move |app_state: State<AppState>, headers: HeaderMap, tx: String| add_tx(app_state, headers, tx)
}))
.with_state(self.app_state.clone())
// TODO(shahak): Remove this once we fix the centralized simulator to not use is_alive
Expand Down Expand Up @@ -129,11 +127,12 @@ async fn add_tx(
State(app_state): State<AppState>,
headers: HeaderMap,
tx: String,
max_sierra_program_size: usize,
) -> HttpServerResult<Json<GatewayOutput>> {
ADDED_TRANSACTIONS_TOTAL.increment(1);
debug!("ADD_TX_START: Http server received a new transaction.");

let dynamic_config = app_state.config_manager_client.get_http_server_dynamic_config().await?;

let tx: DeprecatedGatewayTransactionV3 = match serde_json::from_str(&tx) {
Ok(value) => value,
Err(e) => {
Expand All @@ -148,7 +147,7 @@ async fn add_tx(
}
};

let rpc_tx = tx.convert_to_rpc_tx(max_sierra_program_size).inspect_err(|e| {
let rpc_tx = tx.convert_to_rpc_tx(dynamic_config.max_sierra_program_size).inspect_err(|e| {
debug!("Error while converting deprecated gateway transaction into RPC transaction: {}", e);
})?;

Expand Down
17 changes: 6 additions & 11 deletions crates/apollo_http_server/src/http_server_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use apollo_config_manager_types::communication::MockConfigManagerClient;
use apollo_gateway_types::communication::{GatewayClientError, MockGatewayClient};
use apollo_gateway_types::deprecated_gateway_error::{
KnownStarknetErrorCode,
Expand Down Expand Up @@ -31,6 +30,7 @@ use crate::test_utils::{
deprecated_gateway_declare_tx,
deprecated_gateway_deploy_account_tx,
deprecated_gateway_invoke_tx,
get_mock_config_manager_client,
rpc_invoke_tx,
GatewayTransaction,
TransactionSerialization,
Expand Down Expand Up @@ -127,8 +127,7 @@ async fn record_region_test(#[case] index: u16, #[case] tx: impl GatewayTransact
.times(1)
.return_const(Ok(GatewayOutput::Invoke(InvokeGatewayOutput::new(tx_hash_2))));

let mock_config_manager_client = MockConfigManagerClient::new();

let mock_config_manager_client = get_mock_config_manager_client();
// TODO(Yael): avoid the hardcoded node offset index, consider dynamic allocation.
let http_client =
add_tx_http_client(mock_config_manager_client, mock_gateway_client, 1 + index).await;
Expand Down Expand Up @@ -163,8 +162,7 @@ async fn record_region_gateway_failing_tx(#[case] index: u16, #[case] tx: impl G
)),
));

let mock_config_manager_client = MockConfigManagerClient::new();

let mock_config_manager_client = get_mock_config_manager_client();
let http_client =
add_tx_http_client(mock_config_manager_client, mock_gateway_client, 3 + index).await;

Expand Down Expand Up @@ -214,8 +212,7 @@ async fn test_response(#[case] index: u16, #[case] tx: impl GatewayTransaction)
expected_internal_err,
));

let mock_config_manager_client = MockConfigManagerClient::new();

let mock_config_manager_client = get_mock_config_manager_client();
let http_client =
add_tx_http_client(mock_config_manager_client, mock_gateway_client, 5 + index).await;

Expand Down Expand Up @@ -284,8 +281,7 @@ async fn test_unsupported_tx_version(
}

let mock_gateway_client = MockGatewayClient::new();
let mock_config_manager_client = MockConfigManagerClient::new();

let mock_config_manager_client = get_mock_config_manager_client();
let http_client =
add_tx_http_client(mock_config_manager_client, mock_gateway_client, 9 + index).await;

Expand All @@ -306,8 +302,7 @@ async fn sanitizing_error_message() {
tx_object.insert("version".to_string(), Value::String(malicious_version.to_string())).unwrap();

let mock_gateway_client = MockGatewayClient::new();
let mock_config_manager_client = MockConfigManagerClient::new();

let mock_config_manager_client = get_mock_config_manager_client();
let http_client = add_tx_http_client(mock_config_manager_client, mock_gateway_client, 13).await;

let serialized_err =
Expand Down
6 changes: 3 additions & 3 deletions crates/apollo_http_server/src/metrics_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use apollo_config_manager_types::communication::MockConfigManagerClient;
use apollo_gateway_types::communication::{GatewayClientError, MockGatewayClient};
use apollo_gateway_types::gateway_types::{GatewayOutput, InvokeGatewayOutput};
use apollo_infra::component_client::ClientError;
Expand All @@ -14,6 +13,7 @@ use crate::metrics::{
use crate::test_utils::{
add_tx_http_client,
deprecated_gateway_invoke_tx,
get_mock_config_manager_client,
rpc_invoke_tx,
GatewayTransaction,
};
Expand Down Expand Up @@ -55,7 +55,7 @@ async fn add_tx_metrics_test(#[case] index: u16, #[case] tx: impl GatewayTransac
)))
});

let mock_config_manager_client = MockConfigManagerClient::new();
let mock_config_manager_client = get_mock_config_manager_client();

// Initialize the metrics directly instead of spawning a monitoring endpoint task.
let recorder = PrometheusBuilder::new().build_recorder();
Expand Down Expand Up @@ -89,7 +89,7 @@ async fn add_tx_serde_failure_metrics_test() {
.times(1)
.return_once(move |_| Ok(success_gateway_client_output()));

let mock_config_manager_client = MockConfigManagerClient::new();
let mock_config_manager_client = get_mock_config_manager_client();

// Initialize the metrics directly instead of spawning a monitoring endpoint task.
let recorder = PrometheusBuilder::new().build_recorder();
Expand Down
16 changes: 15 additions & 1 deletion crates/apollo_http_server/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use std::sync::Arc;
use apollo_config_manager_types::communication::MockConfigManagerClient;
use apollo_gateway_types::communication::MockGatewayClient;
use apollo_gateway_types::gateway_types::GatewayOutput;
use apollo_http_server_config::config::{HttpServerConfig, DEFAULT_MAX_SIERRA_PROGRAM_SIZE};
use apollo_http_server_config::config::{
HttpServerConfig,
HttpServerDynamicConfig,
DEFAULT_MAX_SIERRA_PROGRAM_SIZE,
};
use apollo_infra_utils::test_utils::{AvailablePorts, TestIdentifier};
use blockifier_test_utils::cairo_versions::CairoVersion;
use mempool_test_utils::starknet_api_test_utils::{declare_tx, deploy_account_tx, invoke_tx};
Expand Down Expand Up @@ -178,3 +182,13 @@ pub fn deprecated_gateway_deploy_account_tx() -> DeprecatedGatewayTransactionV3
pub fn deprecated_gateway_declare_tx() -> DeprecatedGatewayTransactionV3 {
DeprecatedGatewayTransactionV3::from(declare_tx())
}

// A mock config manager client returning the default http server dynamic config for an unlimited
// number of requests.
pub fn get_mock_config_manager_client() -> MockConfigManagerClient {
let mut mock_config_manager_client = MockConfigManagerClient::new();
mock_config_manager_client
.expect_get_http_server_dynamic_config()
.returning(move || Ok(HttpServerDynamicConfig::default()));
mock_config_manager_client
}