File tree Expand file tree Collapse file tree 8 files changed +29
-16
lines changed
crates/apollo_gateway/src Expand file tree Collapse file tree 8 files changed +29
-16
lines changed Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11use apollo_gateway_config:: config:: RpcStateReaderConfig ;
22use apollo_rpc:: CompiledContractClass ;
33use apollo_state_sync_types:: communication:: StateSyncClientResult ;
4+ use async_trait:: async_trait;
45use blockifier:: execution:: contract_class:: {
56 CompiledClassV0 ,
67 CompiledClassV1 ,
@@ -203,8 +204,9 @@ impl FetchCompiledClasses for RpcStateReader {
203204
204205impl GatewayStateReaderWithCompiledClasses for RpcStateReader { }
205206
207+ #[ async_trait]
206208impl 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 ) ) )
Original file line number Diff line number Diff line change 11use apollo_state_sync_types:: communication:: StateSyncClientResult ;
2+ use async_trait:: async_trait;
23use blockifier:: execution:: contract_class:: RunnableCompiledClass ;
34use blockifier:: state:: errors:: StateError ;
45use 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]
2224pub 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}
Original file line number Diff line number Diff line change 11use apollo_state_sync_types:: communication:: StateSyncClientResult ;
2+ use axum:: async_trait;
23use blockifier:: context:: BlockContext ;
34use blockifier:: execution:: contract_class:: RunnableCompiledClass ;
45use blockifier:: state:: errors:: StateError ;
@@ -76,8 +77,9 @@ pub struct TestStateReaderFactory {
7677 pub state_reader : TestStateReader ,
7778}
7879
80+ #[ async_trait]
7981impl 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 ( ) ) )
Original file line number Diff line number Diff 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}
5657pub 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 } )
Original file line number Diff line number Diff 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,15 @@ 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
179+ . instantiate_validator ( & mock_state_reader_factory, tokio:: runtime:: Handle :: current ( ) )
180+ } ) ;
177181 assert ! ( validator. is_ok( ) ) ;
178182}
179183
Original file line number Diff line number Diff line change @@ -353,14 +353,15 @@ 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]
356357impl StateReaderFactory for SyncStateReaderFactory {
357- fn get_state_reader_from_latest_block (
358+ async fn get_state_reader_from_latest_block (
358359 & self ,
359360 ) -> StateSyncClientResult < Box < dyn GatewayStateReaderWithCompiledClasses > > {
360361 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 ( ) ) ?
362+ . shared_state_sync_client
363+ . get_latest_block_number ( )
364+ . await ?
364365 . ok_or ( StateSyncClientError :: StateSyncError ( StateSyncError :: EmptyState ) ) ?;
365366
366367 Ok ( Box :: new ( SyncStateReader :: from_number (
You can’t perform that action at this time.
0 commit comments