Skip to content

Commit ad481c7

Browse files
apollo_gateway: move get_block_info to be async
1 parent 3813fb6 commit ad481c7

File tree

7 files changed

+28
-18
lines changed

7 files changed

+28
-18
lines changed

crates/apollo_gateway/src/rpc_state_reader.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,20 @@ impl RpcStateReader {
9494
}
9595
}
9696

97+
#[async_trait]
9798
impl MempoolStateReader for RpcStateReader {
98-
fn get_block_info(&self) -> StateResult<BlockInfo> {
99+
async fn get_block_info(&self) -> StateResult<BlockInfo> {
99100
let get_block_params = GetBlockWithTxHashesParams { block_id: self.block_id };
101+
let reader = self.clone();
102+
103+
let rpc_request_value = tokio::task::spawn_blocking(move || {
104+
reader.send_rpc_request("starknet_getBlockWithTxHashes", get_block_params)
105+
})
106+
.await
107+
.map_err(|e| StateError::StateReadError(format!("JoinError: {e}")))??;
100108

101-
// The response from the rpc is a full block but we only deserialize the header.
102-
let block_header: BlockHeader = serde_json::from_value(
103-
self.send_rpc_request("starknet_getBlockWithTxHashes", get_block_params)?,
104-
)
105-
.map_err(serde_err_to_state_err)?;
109+
let block_header: BlockHeader =
110+
serde_json::from_value(rpc_request_value).map_err(serde_err_to_state_err)?;
106111
let block_info = block_header.try_into()?;
107112
Ok(block_info)
108113
}

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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
1010
use starknet_api::state::StorageKey;
1111
use starknet_types_core::felt::Felt;
1212

13+
#[async_trait]
1314
pub trait MempoolStateReader: BlockifierStateReader + Send + Sync {
14-
fn get_block_info(&self) -> Result<BlockInfo, StateError>;
15+
async fn get_block_info(&self) -> Result<BlockInfo, StateError>;
1516
}
1617

1718
#[cfg_attr(test, automock)]
@@ -25,9 +26,10 @@ pub trait StateReaderFactory: Send + Sync {
2526
// By default, a Box<dyn Trait> does not implement the trait of the object it contains.
2627
// Therefore, for using the Box<dyn MempoolStateReader>, that the StateReaderFactory creates,
2728
// we need to implement the MempoolStateReader trait for Box<dyn MempoolStateReader>.
29+
#[async_trait]
2830
impl MempoolStateReader for Box<dyn MempoolStateReader> {
29-
fn get_block_info(&self) -> Result<BlockInfo, StateError> {
30-
self.as_ref().get_block_info()
31+
async fn get_block_info(&self) -> Result<BlockInfo, StateError> {
32+
self.as_ref().get_block_info().await
3133
}
3234
}
3335

crates/apollo_gateway/src/state_reader_test_utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ pub struct TestStateReader {
2323
pub blockifier_state_reader: DictStateReader,
2424
}
2525

26+
#[async_trait]
2627
impl MempoolStateReader for TestStateReader {
27-
fn get_block_info(&self) -> Result<BlockInfo, StateError> {
28+
async fn get_block_info(&self) -> Result<BlockInfo, StateError> {
2829
Ok(self.block_info.clone())
2930
}
3031
}

crates/apollo_gateway/src/stateful_transaction_validator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ fn skip_stateful_validations(
333333
pub fn get_latest_block_info(
334334
state_reader: &dyn MempoolStateReader,
335335
) -> StatefulTransactionValidatorResult<BlockInfo> {
336-
state_reader
337-
.get_block_info()
336+
tokio::runtime::Handle::current()
337+
.block_on(state_reader.get_block_info())
338338
.map_err(|e| StarknetError::internal_with_logging("Failed to get latest block info", e))
339339
}

crates/apollo_gateway/src/sync_state_reader.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use async_trait::async_trait;
1616
use blockifier::execution::contract_class::RunnableCompiledClass;
1717
use blockifier::state::errors::StateError;
1818
use blockifier::state::state_api::{StateReader as BlockifierStateReader, StateResult};
19-
use futures::executor::block_on;
2019
use starknet_api::block::{BlockHash, BlockInfo, BlockNumber, GasPriceVector, GasPrices};
2120
use starknet_api::contract_class::ContractClass;
2221
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
@@ -55,9 +54,13 @@ impl SyncStateReader {
5554
}
5655
}
5756

57+
#[async_trait]
5858
impl MempoolStateReader for SyncStateReader {
59-
fn get_block_info(&self) -> StateResult<BlockInfo> {
60-
let block = block_on(self.state_sync_client.get_block(self.block_number))
59+
async fn get_block_info(&self) -> StateResult<BlockInfo> {
60+
let block = self
61+
.state_sync_client
62+
.get_block(self.block_number)
63+
.await
6164
.map_err(|e| StateError::StateReadError(e.to_string()))?;
6265

6366
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
@@ -71,7 +71,7 @@ async fn test_get_block_info() {
7171
block_number,
7272
tokio::runtime::Handle::current(),
7373
);
74-
let result = state_sync_reader.get_block_info().unwrap();
74+
let result = state_sync_reader.get_block_info().await.unwrap();
7575

7676
assert_eq!(
7777
result,

0 commit comments

Comments
 (0)