Skip to content

Commit 2460176

Browse files
apollo_gateway: move instantiate_validator to be async
1 parent 98fcb6b commit 2460176

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

crates/apollo_gateway/src/gateway.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,10 @@ impl ProcessTxBlockingTask {
252252
}
253253

254254
fn process_tx(self) -> GatewayResult<Nonce> {
255-
let mut stateful_transaction_validator = self
256-
.stateful_tx_validator_factory
257-
.instantiate_validator(self.state_reader_factory.as_ref())?;
255+
let mut stateful_transaction_validator = self.runtime.block_on(
256+
self.stateful_tx_validator_factory
257+
.instantiate_validator(self.state_reader_factory.clone()),
258+
)?;
258259

259260
let nonce = stateful_transaction_validator.extract_state_nonce_and_run_validations(
260261
&self.executable_tx,

crates/apollo_gateway/src/gateway_test.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ use crate::state_reader_test_utils::{local_test_state_reader_factory, TestStateR
9797
use crate::stateful_transaction_validator::{
9898
MockStatefulTransactionValidatorFactoryTrait,
9999
MockStatefulTransactionValidatorTrait,
100+
StatefulTransactionValidatorTrait,
100101
};
101102
use crate::stateless_transaction_validator::MockStatelessTransactionValidatorTrait;
102103

@@ -561,9 +562,13 @@ async fn process_tx_returns_error_when_extract_state_nonce_and_run_validations_f
561562
.expect_extract_state_nonce_and_run_validations()
562563
.return_once(|_, _, _| Err(expected_error));
563564

564-
mock_stateful_transaction_validator_factory
565-
.expect_instantiate_validator()
566-
.return_once(|_| Ok(Box::new(mock_stateful_transaction_validator)));
565+
mock_stateful_transaction_validator_factory.expect_instantiate_validator().return_once(|_| {
566+
Box::pin(async {
567+
Ok::<Box<dyn StatefulTransactionValidatorTrait>, _>(Box::new(
568+
mock_stateful_transaction_validator,
569+
))
570+
})
571+
});
567572

568573
let process_tx_task = process_tx_task(mock_stateful_transaction_validator_factory);
569574

@@ -606,7 +611,7 @@ async fn process_tx_returns_error_when_instantiating_validator_fails(
606611
};
607612
mock_stateful_transaction_validator_factory
608613
.expect_instantiate_validator()
609-
.return_once(|_| Err(expected_error));
614+
.return_once(|_| Box::pin(async { Err::<_, _>(expected_error) }));
610615

611616
let process_tx_task = process_tx_task(mock_stateful_transaction_validator_factory);
612617

crates/apollo_gateway/src/stateful_transaction_validator.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::Arc;
2+
13
use apollo_gateway_config::config::StatefulTransactionValidatorConfig;
24
use apollo_gateway_types::deprecated_gateway_error::{
35
KnownStarknetErrorCode,
@@ -7,6 +9,7 @@ use apollo_gateway_types::deprecated_gateway_error::{
79
use apollo_gateway_types::errors::GatewaySpecError;
810
use apollo_mempool_types::communication::SharedMempoolClient;
911
use apollo_proc_macros::sequencer_latency_histogram;
12+
use async_trait::async_trait;
1013
use blockifier::blockifier::stateful_validator::{
1114
StatefulValidator,
1215
StatefulValidatorTrait as BlockifierStatefulValidatorTrait,
@@ -38,29 +41,31 @@ mod stateful_transaction_validator_test;
3841

3942
type BlockifierStatefulValidator = StatefulValidator<Box<dyn MempoolStateReader>>;
4043

44+
#[async_trait]
4145
#[cfg_attr(test, mockall::automock)]
4246
pub trait StatefulTransactionValidatorFactoryTrait: Send + Sync {
43-
fn instantiate_validator(
47+
async fn instantiate_validator(
4448
&self,
45-
state_reader_factory: &dyn StateReaderFactory,
49+
state_reader_factory: Arc<dyn StateReaderFactory>,
4650
) -> StatefulTransactionValidatorResult<Box<dyn StatefulTransactionValidatorTrait>>;
4751
}
4852
pub struct StatefulTransactionValidatorFactory {
4953
pub config: StatefulTransactionValidatorConfig,
5054
pub chain_info: ChainInfo,
5155
}
5256

57+
#[async_trait]
5358
impl StatefulTransactionValidatorFactoryTrait for StatefulTransactionValidatorFactory {
5459
// TODO(Ayelet): Move state_reader_factory and chain_info to the struct.
55-
fn instantiate_validator(
60+
async fn instantiate_validator(
5661
&self,
57-
state_reader_factory: &dyn StateReaderFactory,
62+
state_reader_factory: Arc<dyn StateReaderFactory>,
5863
) -> StatefulTransactionValidatorResult<Box<dyn StatefulTransactionValidatorTrait>> {
5964
// TODO(yael 6/5/2024): consider storing the block_info as part of the
6065
// StatefulTransactionValidator and update it only once a new block is created.
61-
let current_runtime = tokio::runtime::Handle::current();
62-
let state_reader = current_runtime
63-
.block_on(state_reader_factory.get_state_reader_from_latest_block())
66+
let state_reader = state_reader_factory
67+
.get_state_reader_from_latest_block()
68+
.await
6469
.map_err(|err| GatewaySpecError::UnexpectedError {
6570
data: format!("Internal server error: {err}"),
6671
})
@@ -70,7 +75,7 @@ impl StatefulTransactionValidatorFactoryTrait for StatefulTransactionValidatorFa
7075
e,
7176
)
7277
})?;
73-
let latest_block_info = current_runtime.block_on(get_latest_block_info(&state_reader))?;
78+
let latest_block_info = get_latest_block_info(&state_reader).await?;
7479

7580
let state = CachedState::new(state_reader);
7681
let mut versioned_constants = VersionedConstants::get_versioned_constants(

crates/apollo_gateway/src/stateful_transaction_validator_test.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,8 @@ async fn test_instantiate_validator() {
174174
.expect_get_state_reader_from_latest_block()
175175
.return_once(move || latest_state_reader);
176176

177-
let validator = tokio::task::block_in_place(|| {
178-
stateful_validator_factory.instantiate_validator(&mock_state_reader_factory)
179-
});
177+
let validator =
178+
stateful_validator_factory.instantiate_validator(Arc::new(mock_state_reader_factory)).await;
180179
assert!(validator.is_ok());
181180
}
182181

0 commit comments

Comments
 (0)