Skip to content

Commit c6eff2a

Browse files
apollo_http_server: reload dynamic config upon tx processing (#11452)
1 parent 8140a4a commit c6eff2a

File tree

5 files changed

+49
-26
lines changed

5 files changed

+49
-26
lines changed

crates/apollo_http_server/src/errors.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use apollo_config_manager_types::communication::ConfigManagerClientError;
12
use apollo_gateway_types::communication::GatewayClientError;
23
use apollo_gateway_types::deprecated_gateway_error::{
34
KnownStarknetErrorCode,
@@ -23,19 +24,24 @@ pub enum HttpServerRunError {
2324
#[derive(Error, Debug)]
2425
pub enum HttpServerError {
2526
#[error(transparent)]
26-
GatewayClientError(#[from] Box<GatewayClientError>),
27+
ConfigManagerClientError(#[from] ConfigManagerClientError),
28+
#[error(transparent)]
29+
DecompressionError(#[from] CompressionError),
2730
#[error(transparent)]
2831
DeserializationError(#[from] serde_json::Error),
2932
#[error(transparent)]
30-
DecompressionError(#[from] CompressionError),
33+
GatewayClientError(#[from] Box<GatewayClientError>),
3134
}
3235

3336
impl IntoResponse for HttpServerError {
3437
fn into_response(self) -> Response {
3538
match self {
36-
HttpServerError::GatewayClientError(e) => gw_client_err_into_response(*e),
37-
HttpServerError::DeserializationError(e) => serde_error_into_response(e),
39+
HttpServerError::ConfigManagerClientError(e) => {
40+
config_manager_client_err_into_response(e)
41+
}
3842
HttpServerError::DecompressionError(e) => compression_error_into_response(e),
43+
HttpServerError::DeserializationError(e) => serde_error_into_response(e),
44+
HttpServerError::GatewayClientError(e) => gw_client_err_into_response(*e),
3945
}
4046
}
4147
}
@@ -89,6 +95,15 @@ fn gw_client_err_into_response(err: GatewayClientError) -> Response {
8995
(response_code, response_body).into_response()
9096
}
9197

98+
fn config_manager_client_err_into_response(err: ConfigManagerClientError) -> Response {
99+
let (response_code, config_manager_error) = (
100+
StatusCode::INTERNAL_SERVER_ERROR,
101+
StarknetError::internal_with_logging("Failed to process client request", err),
102+
);
103+
let response_body = serialize_error(&config_manager_error);
104+
(response_code, response_body).into_response()
105+
}
106+
92107
/// Serializes a `StarknetError` into an HTTP response, encode the error message
93108
/// to defend potential Cross-Site risks.
94109
fn serialize_error(error: &StarknetError) -> Response {

crates/apollo_http_server/src/http_server.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ pub struct HttpServer {
5757

5858
#[derive(Clone)]
5959
pub struct AppState {
60-
// TODO(Tsabary): use the config manager in the tx processing.
61-
_config_manager_client: SharedConfigManagerClient,
60+
config_manager_client: SharedConfigManagerClient,
6261
pub gateway_client: SharedGatewayClient,
6362
}
6463

@@ -68,7 +67,7 @@ impl HttpServer {
6867
config_manager_client: SharedConfigManagerClient,
6968
gateway_client: SharedGatewayClient,
7069
) -> Self {
71-
let app_state = AppState { _config_manager_client: config_manager_client, gateway_client };
70+
let app_state = AppState { config_manager_client, gateway_client };
7271
HttpServer { config, app_state }
7372
}
7473

@@ -93,8 +92,7 @@ impl HttpServer {
9392
.with_state(self.app_state.clone())
9493
// Rest api endpoint
9594
.route("/gateway/add_transaction", post({
96-
let max_sierra_program_size = self.config.dynamic_config.max_sierra_program_size;
97-
move |app_state: State<AppState>, headers: HeaderMap, tx: String| add_tx(app_state, headers, tx, max_sierra_program_size)
95+
move |app_state: State<AppState>, headers: HeaderMap, tx: String| add_tx(app_state, headers, tx)
9896
}))
9997
.with_state(self.app_state.clone())
10098
// TODO(shahak): Remove this once we fix the centralized simulator to not use is_alive
@@ -129,11 +127,12 @@ async fn add_tx(
129127
State(app_state): State<AppState>,
130128
headers: HeaderMap,
131129
tx: String,
132-
max_sierra_program_size: usize,
133130
) -> HttpServerResult<Json<GatewayOutput>> {
134131
ADDED_TRANSACTIONS_TOTAL.increment(1);
135132
debug!("ADD_TX_START: Http server received a new transaction.");
136133

134+
let dynamic_config = app_state.config_manager_client.get_http_server_dynamic_config().await?;
135+
137136
let tx: DeprecatedGatewayTransactionV3 = match serde_json::from_str(&tx) {
138137
Ok(value) => value,
139138
Err(e) => {
@@ -148,7 +147,7 @@ async fn add_tx(
148147
}
149148
};
150149

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

crates/apollo_http_server/src/http_server_test.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use apollo_config_manager_types::communication::MockConfigManagerClient;
21
use apollo_gateway_types::communication::{GatewayClientError, MockGatewayClient};
32
use apollo_gateway_types::deprecated_gateway_error::{
43
KnownStarknetErrorCode,
@@ -31,6 +30,7 @@ use crate::test_utils::{
3130
deprecated_gateway_declare_tx,
3231
deprecated_gateway_deploy_account_tx,
3332
deprecated_gateway_invoke_tx,
33+
get_mock_config_manager_client,
3434
rpc_invoke_tx,
3535
GatewayTransaction,
3636
TransactionSerialization,
@@ -127,8 +127,7 @@ async fn record_region_test(#[case] index: u16, #[case] tx: impl GatewayTransact
127127
.times(1)
128128
.return_const(Ok(GatewayOutput::Invoke(InvokeGatewayOutput::new(tx_hash_2))));
129129

130-
let mock_config_manager_client = MockConfigManagerClient::new();
131-
130+
let mock_config_manager_client = get_mock_config_manager_client();
132131
// TODO(Yael): avoid the hardcoded node offset index, consider dynamic allocation.
133132
let http_client =
134133
add_tx_http_client(mock_config_manager_client, mock_gateway_client, 1 + index).await;
@@ -163,8 +162,7 @@ async fn record_region_gateway_failing_tx(#[case] index: u16, #[case] tx: impl G
163162
)),
164163
));
165164

166-
let mock_config_manager_client = MockConfigManagerClient::new();
167-
165+
let mock_config_manager_client = get_mock_config_manager_client();
168166
let http_client =
169167
add_tx_http_client(mock_config_manager_client, mock_gateway_client, 3 + index).await;
170168

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

217-
let mock_config_manager_client = MockConfigManagerClient::new();
218-
215+
let mock_config_manager_client = get_mock_config_manager_client();
219216
let http_client =
220217
add_tx_http_client(mock_config_manager_client, mock_gateway_client, 5 + index).await;
221218

@@ -284,8 +281,7 @@ async fn test_unsupported_tx_version(
284281
}
285282

286283
let mock_gateway_client = MockGatewayClient::new();
287-
let mock_config_manager_client = MockConfigManagerClient::new();
288-
284+
let mock_config_manager_client = get_mock_config_manager_client();
289285
let http_client =
290286
add_tx_http_client(mock_config_manager_client, mock_gateway_client, 9 + index).await;
291287

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

308304
let mock_gateway_client = MockGatewayClient::new();
309-
let mock_config_manager_client = MockConfigManagerClient::new();
310-
305+
let mock_config_manager_client = get_mock_config_manager_client();
311306
let http_client = add_tx_http_client(mock_config_manager_client, mock_gateway_client, 13).await;
312307

313308
let serialized_err =

crates/apollo_http_server/src/metrics_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use apollo_config_manager_types::communication::MockConfigManagerClient;
21
use apollo_gateway_types::communication::{GatewayClientError, MockGatewayClient};
32
use apollo_gateway_types::gateway_types::{GatewayOutput, InvokeGatewayOutput};
43
use apollo_infra::component_client::ClientError;
@@ -14,6 +13,7 @@ use crate::metrics::{
1413
use crate::test_utils::{
1514
add_tx_http_client,
1615
deprecated_gateway_invoke_tx,
16+
get_mock_config_manager_client,
1717
rpc_invoke_tx,
1818
GatewayTransaction,
1919
};
@@ -55,7 +55,7 @@ async fn add_tx_metrics_test(#[case] index: u16, #[case] tx: impl GatewayTransac
5555
)))
5656
});
5757

58-
let mock_config_manager_client = MockConfigManagerClient::new();
58+
let mock_config_manager_client = get_mock_config_manager_client();
5959

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

92-
let mock_config_manager_client = MockConfigManagerClient::new();
92+
let mock_config_manager_client = get_mock_config_manager_client();
9393

9494
// Initialize the metrics directly instead of spawning a monitoring endpoint task.
9595
let recorder = PrometheusBuilder::new().build_recorder();

crates/apollo_http_server/src/test_utils.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ use std::sync::Arc;
44
use apollo_config_manager_types::communication::MockConfigManagerClient;
55
use apollo_gateway_types::communication::MockGatewayClient;
66
use apollo_gateway_types::gateway_types::GatewayOutput;
7-
use apollo_http_server_config::config::{HttpServerConfig, DEFAULT_MAX_SIERRA_PROGRAM_SIZE};
7+
use apollo_http_server_config::config::{
8+
HttpServerConfig,
9+
HttpServerDynamicConfig,
10+
DEFAULT_MAX_SIERRA_PROGRAM_SIZE,
11+
};
812
use apollo_infra_utils::test_utils::{AvailablePorts, TestIdentifier};
913
use blockifier_test_utils::cairo_versions::CairoVersion;
1014
use mempool_test_utils::starknet_api_test_utils::{declare_tx, deploy_account_tx, invoke_tx};
@@ -178,3 +182,13 @@ pub fn deprecated_gateway_deploy_account_tx() -> DeprecatedGatewayTransactionV3
178182
pub fn deprecated_gateway_declare_tx() -> DeprecatedGatewayTransactionV3 {
179183
DeprecatedGatewayTransactionV3::from(declare_tx())
180184
}
185+
186+
// A mock config manager client returning the default http server dynamic config for an unlimited
187+
// number of requests.
188+
pub fn get_mock_config_manager_client() -> MockConfigManagerClient {
189+
let mut mock_config_manager_client = MockConfigManagerClient::new();
190+
mock_config_manager_client
191+
.expect_get_http_server_dynamic_config()
192+
.returning(move || Ok(HttpServerDynamicConfig::default()));
193+
mock_config_manager_client
194+
}

0 commit comments

Comments
 (0)