Skip to content

Commit 60ea01f

Browse files
apollo_gateway: split instantiate_validator
1 parent e1ada60 commit 60ea01f

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

crates/apollo_gateway/src/gateway.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ impl ProcessTxBlockingTask {
256256
}
257257

258258
fn process_tx(self) -> GatewayResult<Nonce> {
259+
// TODO(Itamar): Remove this block_on by extracting the instantiation to the async path.
259260
let mut stateful_transaction_validator = self.runtime.block_on(
260261
self.stateful_tx_validator_factory.instantiate_validator(self.state_reader_factory),
261262
)?;

crates/apollo_gateway/src/stateful_transaction_validator.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ pub trait StatefulTransactionValidatorFactoryTrait: Send + Sync {
5656
&self,
5757
state_reader_factory: Arc<dyn StateReaderFactory>,
5858
) -> StatefulTransactionValidatorResult<Box<dyn StatefulTransactionValidatorTrait>>;
59+
60+
async fn get_state_reader_for_validation(
61+
&self,
62+
state_reader_factory: Arc<dyn StateReaderFactory>,
63+
) -> StatefulTransactionValidatorResult<Box<dyn GatewayStateReaderWithCompiledClasses>>;
64+
65+
async fn create_validator_from_state_reader(
66+
&self,
67+
state_reader: Box<dyn GatewayStateReaderWithCompiledClasses>,
68+
) -> StatefulTransactionValidatorResult<Box<dyn StatefulTransactionValidatorTrait>>;
5969
}
6070
pub struct StatefulTransactionValidatorFactory {
6171
pub config: StatefulTransactionValidatorConfig,
@@ -70,9 +80,16 @@ impl StatefulTransactionValidatorFactoryTrait for StatefulTransactionValidatorFa
7080
&self,
7181
state_reader_factory: Arc<dyn StateReaderFactory>,
7282
) -> StatefulTransactionValidatorResult<Box<dyn StatefulTransactionValidatorTrait>> {
73-
// TODO(yael 6/5/2024): consider storing the block_info as part of the
74-
// StatefulTransactionValidator and update it only once a new block is created.
75-
let state_reader = state_reader_factory
83+
// Compose the split API to preserve original behavior.
84+
let state_reader = self.get_state_reader_for_validation(state_reader_factory).await?;
85+
self.create_validator_from_state_reader(state_reader).await
86+
}
87+
88+
async fn get_state_reader_for_validation(
89+
&self,
90+
state_reader_factory: Arc<dyn StateReaderFactory>,
91+
) -> StatefulTransactionValidatorResult<Box<dyn GatewayStateReaderWithCompiledClasses>> {
92+
state_reader_factory
7693
.get_state_reader_from_latest_block()
7794
.await
7895
.map_err(|err| GatewaySpecError::UnexpectedError {
@@ -83,8 +100,16 @@ impl StatefulTransactionValidatorFactoryTrait for StatefulTransactionValidatorFa
83100
"Failed to get state reader from latest block",
84101
e,
85102
)
86-
})?;
87-
let latest_block_info = get_latest_block_info(&state_reader).await?;
103+
})
104+
}
105+
106+
async fn create_validator_from_state_reader(
107+
&self,
108+
state_reader: Box<dyn GatewayStateReaderWithCompiledClasses>,
109+
) -> StatefulTransactionValidatorResult<Box<dyn StatefulTransactionValidatorTrait>> {
110+
// TODO(yael 6/5/2024): consider storing the block_info as part of the
111+
// StatefulTransactionValidator and update it only once a new block is created.
112+
let latest_block_info = get_latest_block_info(state_reader.as_ref()).await?;
88113

89114
let state_reader_and_contract_manager = StateReaderAndContractManager {
90115
state_reader,
@@ -335,6 +360,8 @@ fn skip_stateful_validations(
335360
// to check if the account exists in the mempool since it means that either it has a
336361
// deploy_account transaction or transactions with future nonces that passed
337362
// validations.
363+
// TODO(Itamar): Avoid blocking here by moving this mempool existence check to the async
364+
// path prior to spawning the blocking task, and pass the result into validation.
338365
return runtime
339366
.block_on(mempool_client.account_tx_in_pool_or_recent_block(tx.sender_address()))
340367
.map_err(|err| mempool_client_err_to_deprecated_gw_err(&tx.signature(), err))

0 commit comments

Comments
 (0)