Skip to content

Commit 3813fb6

Browse files
apollo_gateway: move get_state_reader_from_latest_block to be async
1 parent ec83746 commit 3813fb6

File tree

6 files changed

+24
-13
lines changed

6 files changed

+24
-13
lines changed

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,
@@ -185,8 +186,9 @@ pub struct RpcStateReaderFactory {
185186
pub config: RpcStateReaderConfig,
186187
}
187188

189+
#[async_trait]
188190
impl StateReaderFactory for RpcStateReaderFactory {
189-
fn get_state_reader_from_latest_block(
191+
async fn get_state_reader_from_latest_block(
190192
&self,
191193
) -> StateSyncClientResult<Box<dyn MempoolStateReader>> {
192194
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::state_api::{StateReader as BlockifierStateReader, StateResult};
@@ -14,8 +15,9 @@ pub trait MempoolStateReader: BlockifierStateReader + Send + Sync {
1415
}
1516

1617
#[cfg_attr(test, automock)]
18+
#[async_trait]
1719
pub trait StateReaderFactory: Send + Sync {
18-
fn get_state_reader_from_latest_block(
20+
async fn get_state_reader_from_latest_block(
1921
&self,
2022
) -> StateSyncClientResult<Box<dyn MempoolStateReader>>;
2123
}

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;
@@ -58,8 +59,9 @@ pub struct TestStateReaderFactory {
5859
pub state_reader: TestStateReader,
5960
}
6061

62+
#[async_trait]
6163
impl StateReaderFactory for TestStateReaderFactory {
62-
fn get_state_reader_from_latest_block(
64+
async fn get_state_reader_from_latest_block(
6365
&self,
6466
) -> StateSyncClientResult<Box<dyn MempoolStateReader>> {
6567
Ok(Box::new(self.state_reader.clone()))

crates/apollo_gateway/src/stateful_transaction_validator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ impl StatefulTransactionValidatorFactoryTrait for StatefulTransactionValidatorFa
5858
) -> StatefulTransactionValidatorResult<Box<dyn StatefulTransactionValidatorTrait>> {
5959
// TODO(yael 6/5/2024): consider storing the block_info as part of the
6060
// StatefulTransactionValidator and update it only once a new block is created.
61-
let state_reader = state_reader_factory
62-
.get_state_reader_from_latest_block()
61+
let state_reader = tokio::runtime::Handle::current()
62+
.block_on(state_reader_factory.get_state_reader_from_latest_block())
6363
.map_err(|err| GatewaySpecError::UnexpectedError {
6464
data: format!("Internal server error: {err}"),
6565
})

crates/apollo_gateway/src/stateful_transaction_validator_test.rs

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

159159
#[rstest]
160-
fn test_instantiate_validator() {
160+
#[tokio::test(flavor = "multi_thread")]
161+
async fn test_instantiate_validator() {
161162
let stateful_validator_factory = StatefulTransactionValidatorFactory {
162163
config: StatefulTransactionValidatorConfig::default(),
163164
chain_info: ChainInfo::create_for_testing(),
@@ -168,12 +169,14 @@ 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+
let validator = tokio::task::block_in_place(|| {
178+
stateful_validator_factory.instantiate_validator(&mock_state_reader_factory)
179+
});
177180
assert!(validator.is_ok());
178181
}
179182

crates/apollo_gateway/src/sync_state_reader.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,16 @@ pub(crate) struct SyncStateReaderFactory {
281281
}
282282

283283
/// Use any of these factory methods only once per transaction to make sure metrics are accurate.
284+
#[async_trait]
284285
impl StateReaderFactory for SyncStateReaderFactory {
285-
fn get_state_reader_from_latest_block(
286+
async fn get_state_reader_from_latest_block(
286287
&self,
287288
) -> StateSyncClientResult<Box<dyn MempoolStateReader>> {
289+
// TODO(guy.f): Do we want to count this as well?
288290
let latest_block_number = self
289-
.runtime
290-
// TODO(guy.f): Do we want to count this as well?
291-
.block_on(self.shared_state_sync_client.get_latest_block_number())?
291+
.shared_state_sync_client
292+
.get_latest_block_number()
293+
.await?
292294
.ok_or(StateSyncClientError::StateSyncError(StateSyncError::EmptyState))?;
293295

294296
Ok(Box::new(SyncStateReader::from_number(

0 commit comments

Comments
 (0)