Skip to content

Commit cc3937b

Browse files
apollo_gateway: move get_block_info to be async
1 parent 2aecae9 commit cc3937b

File tree

10 files changed

+35
-25
lines changed

10 files changed

+35
-25
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/apollo_gateway/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ blockifier.workspace = true
3535
blockifier_test_utils = { workspace = true, optional = true }
3636
cairo-lang-starknet-classes.workspace = true
3737
clap.workspace = true
38-
futures.workspace = true
3938
lazy_static.workspace = true
4039
mempool_test_utils.workspace = true
4140
num-rational.workspace = true

crates/apollo_gateway/src/rpc_state_reader.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,20 @@ impl RpcStateReader {
100100
}
101101
}
102102

103+
#[async_trait]
103104
impl MempoolStateReader for RpcStateReader {
104-
fn get_block_info(&self) -> StateResult<BlockInfo> {
105+
async fn get_block_info(&self) -> StateResult<BlockInfo> {
105106
let get_block_params = GetBlockWithTxHashesParams { block_id: self.block_id };
107+
let reader = self.clone();
108+
109+
let get_block_with_tx_hashes_result = tokio::task::spawn_blocking(move || {
110+
reader.send_rpc_request("starknet_getBlockWithTxHashes", get_block_params)
111+
})
112+
.await
113+
.map_err(|e| StateError::StateReadError(format!("JoinError: {e}")))??;
106114

107-
// The response from the rpc is a full block but we only deserialize the header.
108-
let block_header: BlockHeader = serde_json::from_value(
109-
self.send_rpc_request("starknet_getBlockWithTxHashes", get_block_params)?,
110-
)
111-
.map_err(serde_err_to_state_err)?;
115+
let block_header: BlockHeader =
116+
serde_json::from_value(get_block_with_tx_hashes_result).map_err(serde_err_to_state_err)?;
112117
let block_info = block_header.try_into()?;
113118
Ok(block_info)
114119
}

crates/apollo_gateway/src/rpc_state_reader_test.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ async fn test_get_block_info() {
9292
);
9393

9494
let client = RpcStateReader::from_latest(&config);
95-
let result =
96-
tokio::task::spawn_blocking(move || client.get_block_info()).await.unwrap().unwrap();
95+
let result = client.get_block_info().await.unwrap();
9796
assert_eq!(result, expected_result);
9897
mock.assert_async().await;
9998
}

crates/apollo_gateway/src/state_reader.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use apollo_state_sync_types::communication::StateSyncClientResult;
22
use async_trait::async_trait;
33
use blockifier::execution::contract_class::RunnableCompiledClass;
4-
use blockifier::state::errors::StateError;
54
use blockifier::state::global_cache::CompiledClasses;
65
use blockifier::state::state_api::{StateReader as BlockifierStateReader, StateResult};
76
use blockifier::state::state_reader_and_contract_manager::{
@@ -15,8 +14,9 @@ use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
1514
use starknet_api::state::StorageKey;
1615
use starknet_types_core::felt::Felt;
1716

17+
#[async_trait]
1818
pub trait MempoolStateReader: BlockifierStateReader + Send + Sync {
19-
fn get_block_info(&self) -> Result<BlockInfo, StateError>;
19+
async fn get_block_info(&self) -> StateResult<BlockInfo>;
2020
}
2121

2222
#[cfg_attr(test, automock)]
@@ -69,18 +69,20 @@ impl FetchCompiledClasses for Box<dyn GatewayStateReaderWithCompiledClasses> {
6969
// Therefore, for using the Box<dyn GatewayStateReaderWithCompiledClasses>, that the
7070
// StateReaderFactory creates, we need to implement the MempoolStateReader trait for
7171
// Box<dyn GatewayStateReaderWithCompiledClasses>.
72+
#[async_trait]
7273
impl MempoolStateReader for Box<dyn GatewayStateReaderWithCompiledClasses> {
73-
fn get_block_info(&self) -> StateResult<BlockInfo> {
74-
self.as_ref().get_block_info()
74+
async fn get_block_info(&self) -> StateResult<BlockInfo> {
75+
self.as_ref().get_block_info().await
7576
}
7677
}
7778

7879
impl GatewayStateReaderWithCompiledClasses for Box<dyn GatewayStateReaderWithCompiledClasses> {}
7980

81+
#[async_trait]
8082
impl MempoolStateReader
8183
for StateReaderAndContractManager<Box<dyn GatewayStateReaderWithCompiledClasses>>
8284
{
83-
fn get_block_info(&self) -> StateResult<BlockInfo> {
84-
self.state_reader.get_block_info()
85+
async fn get_block_info(&self) -> StateResult<BlockInfo> {
86+
self.state_reader.get_block_info().await
8587
}
8688
}

crates/apollo_gateway/src/state_reader_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ async fn test_get_block_info() {
103103
block_number,
104104
tokio::runtime::Handle::current(),
105105
);
106-
let result = state_reader_and_contract_manager.get_block_info().unwrap();
106+
let result = state_reader_and_contract_manager.get_block_info().await.unwrap();
107107

108108
assert_eq!(
109109
result,

crates/apollo_gateway/src/state_reader_test_utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ pub struct TestStateReader {
2929
pub blockifier_state_reader: DictStateReader,
3030
}
3131

32+
#[async_trait]
3233
impl MempoolStateReader for TestStateReader {
33-
fn get_block_info(&self) -> Result<BlockInfo, StateError> {
34+
async fn get_block_info(&self) -> Result<BlockInfo, StateError> {
3435
Ok(self.block_info.clone())
3536
}
3637
}

crates/apollo_gateway/src/stateful_transaction_validator.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl StatefulTransactionValidatorFactoryTrait for StatefulTransactionValidatorFa
8080
e,
8181
)
8282
})?;
83-
let latest_block_info = get_latest_block_info(&state_reader)?;
83+
let latest_block_info = get_latest_block_info(&state_reader, runtime)?;
8484

8585
let state_reader_and_contract_manager = StateReaderAndContractManager {
8686
state_reader,
@@ -349,10 +349,12 @@ fn skip_stateful_validations(
349349
Ok(false)
350350
}
351351

352+
// TODO(Itamar): Remove using runtime here and make it async.
352353
pub fn get_latest_block_info(
353354
state_reader: &dyn MempoolStateReader,
355+
runtime: tokio::runtime::Handle,
354356
) -> StatefulTransactionValidatorResult<BlockInfo> {
355-
state_reader
356-
.get_block_info()
357+
runtime
358+
.block_on(state_reader.get_block_info())
357359
.map_err(|e| StarknetError::internal_with_logging("Failed to get latest block info", e))
358360
}

crates/apollo_gateway/src/sync_state_reader.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use blockifier::state::errors::StateError;
2323
use blockifier::state::global_cache::CompiledClasses;
2424
use blockifier::state::state_api::{StateReader as BlockifierStateReader, StateResult};
2525
use blockifier::state::state_reader_and_contract_manager::FetchCompiledClasses;
26-
use futures::executor::block_on;
2726
use starknet_api::block::{BlockHash, BlockInfo, BlockNumber, GasPriceVector, GasPrices};
2827
use starknet_api::contract_class::ContractClass;
2928
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
@@ -98,9 +97,13 @@ impl SyncStateReader {
9897
}
9998
}
10099

100+
#[async_trait]
101101
impl MempoolStateReader for SyncStateReader {
102-
fn get_block_info(&self) -> StateResult<BlockInfo> {
103-
let block = block_on(self.state_sync_client.get_block(self.block_number))
102+
async fn get_block_info(&self) -> StateResult<BlockInfo> {
103+
let block = self
104+
.state_sync_client
105+
.get_block(self.block_number)
106+
.await
104107
.map_err(|e| StateError::StateReadError(e.to_string()))?;
105108

106109
let block_header = block.block_header_without_hash;

crates/apollo_gateway/src/sync_state_reader_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async fn test_get_block_info() {
7272
block_number,
7373
tokio::runtime::Handle::current(),
7474
);
75-
let result = state_sync_reader.get_block_info().unwrap();
75+
let result = state_sync_reader.get_block_info().await.unwrap();
7676

7777
assert_eq!(
7878
result,

0 commit comments

Comments
 (0)