Skip to content

Commit 635306c

Browse files
apollo_gateway: move get_state_reader_from_latest_block to be async (#10119)
1 parent 38c8b41 commit 635306c

File tree

8 files changed

+33
-16
lines changed

8 files changed

+33
-16
lines changed

crates/apollo_gateway/src/gateway.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl ProcessTxBlockingTask {
258258
fn process_tx(self) -> GatewayResult<Nonce> {
259259
let mut stateful_transaction_validator = self
260260
.stateful_tx_validator_factory
261-
.instantiate_validator(self.state_reader_factory.as_ref())?;
261+
.instantiate_validator(self.state_reader_factory.as_ref(), self.runtime.clone())?;
262262

263263
let nonce = stateful_transaction_validator.extract_state_nonce_and_run_validations(
264264
&self.executable_tx,

crates/apollo_gateway/src/gateway_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ async fn process_tx_returns_error_when_extract_state_nonce_and_run_validations_f
565565

566566
mock_stateful_transaction_validator_factory
567567
.expect_instantiate_validator()
568-
.return_once(|_| Ok(Box::new(mock_stateful_transaction_validator)));
568+
.return_once(|_, _| Ok(Box::new(mock_stateful_transaction_validator)));
569569

570570
let process_tx_task = process_tx_task(mock_stateful_transaction_validator_factory);
571571

@@ -608,7 +608,7 @@ async fn process_tx_returns_error_when_instantiating_validator_fails(
608608
};
609609
mock_stateful_transaction_validator_factory
610610
.expect_instantiate_validator()
611-
.return_once(|_| Err(expected_error));
611+
.return_once(|_, _| Err(expected_error));
612612

613613
let process_tx_task = process_tx_task(mock_stateful_transaction_validator_factory);
614614

crates/apollo_gateway/src/rpc_state_reader.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use apollo_gateway_config::config::RpcStateReaderConfig;
22
use apollo_rpc::CompiledContractClass;
33
use apollo_state_sync_types::communication::StateSyncClientResult;
4+
use async_trait::async_trait;
45
use blockifier::execution::contract_class::{
56
CompiledClassV0,
67
CompiledClassV1,
@@ -203,8 +204,9 @@ impl FetchCompiledClasses for RpcStateReader {
203204

204205
impl GatewayStateReaderWithCompiledClasses for RpcStateReader {}
205206

207+
#[async_trait]
206208
impl StateReaderFactory for RpcStateReaderFactory {
207-
fn get_state_reader_from_latest_block(
209+
async fn get_state_reader_from_latest_block(
208210
&self,
209211
) -> StateSyncClientResult<Box<dyn GatewayStateReaderWithCompiledClasses>> {
210212
Ok(Box::new(RpcStateReader::from_latest(&self.config)))

crates/apollo_gateway/src/state_reader.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use apollo_state_sync_types::communication::StateSyncClientResult;
2+
use async_trait::async_trait;
23
use blockifier::execution::contract_class::RunnableCompiledClass;
34
use blockifier::state::errors::StateError;
45
use blockifier::state::global_cache::CompiledClasses;
@@ -19,8 +20,9 @@ pub trait MempoolStateReader: BlockifierStateReader + Send + Sync {
1920
}
2021

2122
#[cfg_attr(test, automock)]
23+
#[async_trait]
2224
pub trait StateReaderFactory: Send + Sync {
23-
fn get_state_reader_from_latest_block(
25+
async fn get_state_reader_from_latest_block(
2426
&self,
2527
) -> StateSyncClientResult<Box<dyn GatewayStateReaderWithCompiledClasses>>;
2628
}

crates/apollo_gateway/src/state_reader_test_utils.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use apollo_state_sync_types::communication::StateSyncClientResult;
2+
use axum::async_trait;
23
use blockifier::context::BlockContext;
34
use blockifier::execution::contract_class::RunnableCompiledClass;
45
use blockifier::state::errors::StateError;
@@ -76,8 +77,9 @@ pub struct TestStateReaderFactory {
7677
pub state_reader: TestStateReader,
7778
}
7879

80+
#[async_trait]
7981
impl StateReaderFactory for TestStateReaderFactory {
80-
fn get_state_reader_from_latest_block(
82+
async fn get_state_reader_from_latest_block(
8183
&self,
8284
) -> StateSyncClientResult<Box<dyn GatewayStateReaderWithCompiledClasses>> {
8385
Ok(Box::new(self.state_reader.clone()))

crates/apollo_gateway/src/stateful_transaction_validator.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub trait StatefulTransactionValidatorFactoryTrait: Send + Sync {
5151
fn instantiate_validator(
5252
&self,
5353
state_reader_factory: &dyn StateReaderFactory,
54+
runtime: tokio::runtime::Handle,
5455
) -> StatefulTransactionValidatorResult<Box<dyn StatefulTransactionValidatorTrait>>;
5556
}
5657
pub struct StatefulTransactionValidatorFactory {
@@ -64,11 +65,12 @@ impl StatefulTransactionValidatorFactoryTrait for StatefulTransactionValidatorFa
6465
fn instantiate_validator(
6566
&self,
6667
state_reader_factory: &dyn StateReaderFactory,
68+
runtime: tokio::runtime::Handle,
6769
) -> StatefulTransactionValidatorResult<Box<dyn StatefulTransactionValidatorTrait>> {
6870
// TODO(yael 6/5/2024): consider storing the block_info as part of the
6971
// StatefulTransactionValidator and update it only once a new block is created.
70-
let state_reader = state_reader_factory
71-
.get_state_reader_from_latest_block()
72+
let state_reader = runtime
73+
.block_on(state_reader_factory.get_state_reader_from_latest_block())
7274
.map_err(|err| GatewaySpecError::UnexpectedError {
7375
data: format!("Internal server error: {err}"),
7476
})

crates/apollo_gateway/src/stateful_transaction_validator_test.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ async fn test_extract_state_nonce_and_run_validations(
156156
}
157157

158158
#[rstest]
159-
fn test_instantiate_validator() {
159+
#[tokio::test(flavor = "multi_thread")]
160+
async fn test_instantiate_validator() {
160161
let stateful_validator_factory = StatefulTransactionValidatorFactory {
161162
config: StatefulTransactionValidatorConfig::default(),
162163
chain_info: ChainInfo::create_for_testing(),
@@ -168,12 +169,16 @@ fn test_instantiate_validator() {
168169
let mut mock_state_reader_factory = MockStateReaderFactory::new();
169170

170171
// Make sure stateful_validator uses the latest block in the initial call.
171-
let latest_state_reader = state_reader_factory.get_state_reader_from_latest_block();
172+
let latest_state_reader = state_reader_factory.get_state_reader_from_latest_block().await;
172173
mock_state_reader_factory
173174
.expect_get_state_reader_from_latest_block()
174-
.return_once(|| latest_state_reader);
175+
.return_once(move || latest_state_reader);
175176

176-
let validator = stateful_validator_factory.instantiate_validator(&mock_state_reader_factory);
177+
// TODO(Itamar): Remove using runtime when instantiate_validator is async.
178+
let validator = tokio::task::block_in_place(|| {
179+
stateful_validator_factory
180+
.instantiate_validator(&mock_state_reader_factory, tokio::runtime::Handle::current())
181+
});
177182
assert!(validator.is_ok());
178183
}
179184

crates/apollo_gateway/src/sync_state_reader.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,18 @@ pub(crate) struct SyncStateReaderFactory {
353353
}
354354

355355
/// Use any of these factory methods only once per transaction to make sure metrics are accurate.
356+
#[async_trait]
356357
impl StateReaderFactory for SyncStateReaderFactory {
357-
fn get_state_reader_from_latest_block(
358+
// TODO(guy.f): The call to `get_latest_block_number()` is not counted in the storage metrics as
359+
// it is done prior to the creation of SharedStateSyncClientMetricWrapper, directly via the
360+
// SharedStateSyncClient.
361+
async fn get_state_reader_from_latest_block(
358362
&self,
359363
) -> StateSyncClientResult<Box<dyn GatewayStateReaderWithCompiledClasses>> {
360364
let latest_block_number = self
361-
.runtime
362-
// TODO(guy.f): Do we want to count this as well?
363-
.block_on(self.shared_state_sync_client.get_latest_block_number())?
365+
.shared_state_sync_client
366+
.get_latest_block_number()
367+
.await?
364368
.ok_or(StateSyncClientError::StateSyncError(StateSyncError::EmptyState))?;
365369

366370
Ok(Box::new(SyncStateReader::from_number(

0 commit comments

Comments
 (0)