Skip to content

Commit 05a86d7

Browse files
apollo_gateway: use get_nonce outside extract_state_nonce_and_run_validations
1 parent de399b6 commit 05a86d7

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
lines changed

crates/apollo_gateway/src/gateway.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl Gateway {
151151
transaction_converter_err_to_deprecated_gw_err(&tx_signature, e)
152152
})?;
153153

154-
let blocking_task =
154+
let mut blocking_task =
155155
ProcessTxBlockingTask::new(self, executable_tx, tokio::runtime::Handle::current())
156156
.await
157157
.map_err(|e| {
@@ -162,10 +162,23 @@ impl Gateway {
162162
metric_counters.record_add_tx_failure(&e);
163163
e
164164
})?;
165+
166+
// Obtain account nonce before spawning the blocking computation.
167+
let account_nonce = blocking_task.get_nonce().map_err(|e| {
168+
info!(
169+
"Gateway validation failed for tx with signature: {:?} with error: {}",
170+
tx.signature(),
171+
e
172+
);
173+
metric_counters.record_add_tx_failure(&e);
174+
e
175+
})?;
176+
165177
// Run the blocking task in the current span.
166178
let curr_span = Span::current();
167-
let handle =
168-
tokio::task::spawn_blocking(move || curr_span.in_scope(|| blocking_task.process_tx()));
179+
let handle = tokio::task::spawn_blocking(move || {
180+
curr_span.in_scope(|| blocking_task.process_tx(account_nonce))
181+
});
169182
let handle_result = handle.await;
170183
let nonce = match handle_result {
171184
Ok(Ok(nonce)) => nonce,
@@ -264,9 +277,15 @@ impl ProcessTxBlockingTask {
264277
})
265278
}
266279

267-
fn process_tx(mut self) -> GatewayResult<Nonce> {
280+
fn get_nonce(&mut self) -> GatewayResult<Nonce> {
281+
let address = self.executable_tx.contract_address();
282+
self.stateful_tx_validator.get_nonce(address)
283+
}
284+
285+
fn process_tx(mut self, account_nonce: Nonce) -> GatewayResult<Nonce> {
268286
let nonce = self.stateful_tx_validator.extract_state_nonce_and_run_validations(
269287
&self.executable_tx,
288+
account_nonce,
270289
self.mempool_client,
271290
self.runtime,
272291
)?;

crates/apollo_gateway/src/gateway_test.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ async fn process_tx_returns_error_when_extract_state_nonce_and_run_validations_f
567567

568568
mock_stateful_transaction_validator
569569
.expect_extract_state_nonce_and_run_validations()
570-
.return_once(|_, _, _| Err(expected_error));
570+
.return_once(|_, _, _, _| Err(expected_error));
571571

572572
mock_stateful_transaction_validator_factory.expect_instantiate_validator().return_once(|_| {
573573
Box::pin(async {
@@ -578,8 +578,10 @@ async fn process_tx_returns_error_when_extract_state_nonce_and_run_validations_f
578578
});
579579

580580
let process_tx_task = process_tx_task(mock_stateful_transaction_validator_factory).await;
581-
582-
let result = tokio::task::spawn_blocking(move || process_tx_task.process_tx()).await.unwrap();
581+
let account_nonce = nonce!(0);
582+
let result = tokio::task::spawn_blocking(move || process_tx_task.process_tx(account_nonce))
583+
.await
584+
.unwrap();
583585

584586
assert!(result.is_err());
585587
assert_eq!(result.unwrap_err().code, error_code);

crates/apollo_gateway/src/stateful_transaction_validator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ pub trait StatefulTransactionValidatorTrait: Send {
107107
fn extract_state_nonce_and_run_validations(
108108
&mut self,
109109
executable_tx: &ExecutableTransaction,
110+
account_nonce: Nonce,
110111
mempool_client: SharedMempoolClient,
111112
runtime: tokio::runtime::Handle,
112113
) -> StatefulTransactionValidatorResult<Nonce>;
@@ -128,11 +129,10 @@ impl<B: BlockifierStatefulValidatorTrait + Send> StatefulTransactionValidatorTra
128129
fn extract_state_nonce_and_run_validations(
129130
&mut self,
130131
executable_tx: &ExecutableTransaction,
132+
account_nonce: Nonce,
131133
mempool_client: SharedMempoolClient,
132134
runtime: tokio::runtime::Handle,
133135
) -> StatefulTransactionValidatorResult<Nonce> {
134-
let address = executable_tx.contract_address();
135-
let account_nonce = self.get_nonce(address)?;
136136
self.run_transaction_validations(executable_tx, account_nonce, mempool_client, runtime)?;
137137
Ok(account_nonce)
138138
}

crates/apollo_gateway/src/stateful_transaction_validator_test.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,32 +64,24 @@ async fn test_get_nonce_fail_on_extract_state_nonce_and_run_validations() {
6464
// The mempool does not have any transactions from the sender.
6565
Ok(false)
6666
});
67-
let mempool_client = Arc::new(mock_mempool_client);
68-
let runtime = tokio::runtime::Handle::current();
67+
// Removed unused mempool client and runtime in this test since nonce is fetched outside now.
6968

7069
let mut stateful_validator = StatefulTransactionValidator {
7170
config: StatefulTransactionValidatorConfig::default(),
7271
blockifier_stateful_tx_validator: mock_blockifier_validator,
7372
};
7473

7574
let executable_tx = create_executable_invoke_tx(CairoVersion::Cairo1(RunnableCairo1::Casm));
76-
let result = tokio::task::spawn_blocking(move || {
77-
stateful_validator.extract_state_nonce_and_run_validations(
78-
&executable_tx,
79-
mempool_client,
80-
runtime,
81-
)
75+
// get_nonce should fail and return an InternalError.
76+
let err = tokio::task::spawn_blocking(move || {
77+
stateful_validator.get_nonce(executable_tx.contract_address())
8278
})
8379
.await
84-
.unwrap();
80+
.unwrap()
81+
.unwrap_err();
8582
assert_eq!(
86-
result,
87-
Err(StarknetError {
88-
code: StarknetErrorCode::UnknownErrorCode(
89-
"StarknetErrorCode.InternalError".to_string()
90-
),
91-
message: "Internal error".to_string(),
92-
})
83+
err.code,
84+
StarknetErrorCode::UnknownErrorCode("StarknetErrorCode.InternalError".to_string())
9385
);
9486
}
9587

@@ -147,6 +139,7 @@ async fn test_extract_state_nonce_and_run_validations(
147139
let result = tokio::task::spawn_blocking(move || {
148140
stateful_validator.extract_state_nonce_and_run_validations(
149141
&executable_tx,
142+
account_nonce,
150143
mempool_client,
151144
runtime,
152145
)

0 commit comments

Comments
 (0)