Skip to content

Commit 0b1815b

Browse files
blockifier: box TransactionFeeError in TransactionPreValidationError
1 parent 04e09a6 commit 0b1815b

File tree

5 files changed

+138
-109
lines changed

5 files changed

+138
-109
lines changed

crates/apollo_gateway/src/stateful_transaction_validator_test.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use apollo_gateway_types::deprecated_gateway_error::{
66
StarknetErrorCode,
77
};
88
use apollo_mempool_types::communication::MockMempoolClient;
9-
use blockifier::blockifier::stateful_validator::{
10-
StatefulValidatorError as BlockifierStatefulValidatorError,
11-
StatefulValidatorResult as BlockifierStatefulValidatorResult,
12-
};
9+
use blockifier::blockifier::stateful_validator::StatefulValidatorError as BlockifierStatefulValidatorError;
1310
use blockifier::context::ChainInfo;
1411
use blockifier::test_utils::contracts::FeatureContractTrait;
1512
use blockifier::transaction::errors::{TransactionFeeError, TransactionPreValidationError};
@@ -43,39 +40,35 @@ use crate::stateful_transaction_validator::{
4340
StatefulTransactionValidator,
4441
};
4542

46-
pub const STATEFUL_VALIDATOR_FEE_ERROR: BlockifierStatefulValidatorError =
47-
BlockifierStatefulValidatorError::TransactionPreValidationError(
48-
TransactionPreValidationError::TransactionFeeError(
49-
TransactionFeeError::GasBoundsExceedBalance {
50-
resource: Resource::L1DataGas,
51-
max_amount: GasAmount(VALID_L1_GAS_MAX_AMOUNT),
52-
max_price: GasPrice(VALID_L1_GAS_MAX_PRICE_PER_UNIT),
53-
balance: BigUint::ZERO,
54-
},
55-
),
56-
);
57-
5843
#[fixture]
5944
fn stateful_validator() -> StatefulTransactionValidator {
6045
StatefulTransactionValidator { config: StatefulTransactionValidatorConfig::default() }
6146
}
6247

6348
// TODO(Arni): consider testing declare and deploy account.
6449
#[rstest]
65-
#[case::valid_tx(
66-
create_executable_invoke_tx(CairoVersion::Cairo1(RunnableCairo1::Casm)),
67-
Ok(())
68-
)]
69-
#[case::invalid_tx(
70-
create_executable_invoke_tx(CairoVersion::Cairo1(RunnableCairo1::Casm)),
71-
Err(STATEFUL_VALIDATOR_FEE_ERROR)
72-
)]
50+
#[case::valid_tx(create_executable_invoke_tx(CairoVersion::Cairo1(RunnableCairo1::Casm)), true)]
51+
#[case::invalid_tx(create_executable_invoke_tx(CairoVersion::Cairo1(RunnableCairo1::Casm)), false)]
7352
#[tokio::test]
7453
async fn test_stateful_tx_validator(
7554
#[case] executable_tx: AccountTransaction,
76-
#[case] expected_result: BlockifierStatefulValidatorResult<()>,
55+
#[case] expect_ok: bool,
7756
stateful_validator: StatefulTransactionValidator,
7857
) {
58+
let expected_result = if expect_ok {
59+
Ok(())
60+
} else {
61+
Err(BlockifierStatefulValidatorError::TransactionPreValidationError(
62+
TransactionPreValidationError::TransactionFeeError(Box::new(
63+
TransactionFeeError::GasBoundsExceedBalance {
64+
resource: Resource::L1DataGas,
65+
max_amount: GasAmount(VALID_L1_GAS_MAX_AMOUNT),
66+
max_price: GasPrice(VALID_L1_GAS_MAX_PRICE_PER_UNIT),
67+
balance: BigUint::ZERO,
68+
},
69+
)),
70+
))
71+
};
7972
let expected_result_as_stateful_transaction_result = expected_result
8073
.as_ref()
8174
.map(|validate_result| *validate_result)

crates/blockifier/src/transaction/account_transaction.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl AccountTransaction {
258258
if self.execution_flags.charge_fee {
259259
self.check_fee_bounds(tx_context)?;
260260

261-
verify_can_pay_committed_bounds(state, tx_context)?;
261+
verify_can_pay_committed_bounds(state, tx_context).map_err(Box::new)?;
262262
}
263263

264264
Ok(())
@@ -345,9 +345,9 @@ impl AccountTransaction {
345345
)
346346
.collect::<Vec<_>>();
347347
if !insufficiencies.is_empty() {
348-
return Err(TransactionFeeError::InsufficientResourceBounds {
348+
return Err(Box::new(TransactionFeeError::InsufficientResourceBounds {
349349
errors: insufficiencies,
350-
})?;
350+
}))?;
351351
}
352352
}
353353
TransactionInfo::Deprecated(context) => {
@@ -359,9 +359,9 @@ impl AccountTransaction {
359359
tx_context.effective_tip(),
360360
);
361361
if max_fee < min_fee {
362-
return Err(TransactionPreValidationError::TransactionFeeError(
362+
return Err(TransactionPreValidationError::TransactionFeeError(Box::new(
363363
TransactionFeeError::MaxFeeTooLow { min_fee, max_fee },
364-
));
364+
)));
365365
}
366366
}
367367
};

crates/blockifier/src/transaction/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub enum TransactionPreValidationError {
141141
#[error(transparent)]
142142
StateError(#[from] StateError),
143143
#[error(transparent)]
144-
TransactionFeeError(#[from] TransactionFeeError),
144+
TransactionFeeError(#[from] Box<TransactionFeeError>),
145145
}
146146

147147
#[derive(Debug, Error)]

crates/blockifier/src/transaction/execution_flavors_test.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,10 @@ fn test_simulate_validate_pre_validate_with_charge_fee(
280280
assert_matches!(
281281
err,
282282
TransactionExecutionError::TransactionPreValidationError(boxed_error)
283-
if matches!(
283+
=> assert_matches!(
284284
*boxed_error,
285-
TransactionPreValidationError::TransactionFeeError(
286-
TransactionFeeError::MaxFeeTooLow { .. }
287-
)
285+
TransactionPreValidationError::TransactionFeeError(boxed_fee_error)
286+
if matches!(*boxed_fee_error, TransactionFeeError::MaxFeeTooLow { .. })
288287
)
289288
);
290289
} else {
@@ -293,13 +292,15 @@ fn test_simulate_validate_pre_validate_with_charge_fee(
293292
TransactionExecutionError::TransactionPreValidationError(boxed_error)
294293
=> assert_matches!(
295294
*boxed_error,
296-
TransactionPreValidationError::TransactionFeeError(
297-
TransactionFeeError::InsufficientResourceBounds { errors }
298-
)
295+
TransactionPreValidationError::TransactionFeeError(boxed_fee_error)
299296
=> assert_matches!(
300-
errors[0],
301-
ResourceBoundsError::MaxGasAmountTooLow { resource , .. }
302-
if resource == Resource::L1Gas
297+
*boxed_fee_error,
298+
TransactionFeeError::InsufficientResourceBounds { errors }
299+
=> assert_matches!(
300+
errors[0],
301+
ResourceBoundsError::MaxGasAmountTooLow { resource , .. }
302+
if resource == Resource::L1Gas
303+
)
303304
)
304305
)
305306
);
@@ -334,9 +335,11 @@ fn test_simulate_validate_pre_validate_with_charge_fee(
334335
assert_matches!(
335336
result.unwrap_err(),
336337
TransactionExecutionError::TransactionPreValidationError(boxed_error)
337-
if matches!(
338+
=> assert_matches!(
338339
*boxed_error,
339-
TransactionPreValidationError::TransactionFeeError(
340+
TransactionPreValidationError::TransactionFeeError(boxed_fee_error)
341+
=> assert_matches!(
342+
*boxed_fee_error,
340343
TransactionFeeError::MaxFeeExceedsBalance { .. }
341344
)
342345
)
@@ -345,12 +348,14 @@ fn test_simulate_validate_pre_validate_with_charge_fee(
345348
assert_matches!(
346349
result.unwrap_err(),
347350
TransactionExecutionError::TransactionPreValidationError(boxed_error)
348-
if matches!(
351+
=> assert_matches!(
349352
*boxed_error,
350-
TransactionPreValidationError::TransactionFeeError(
353+
TransactionPreValidationError::TransactionFeeError(boxed_fee_error)
354+
=> assert_matches!(
355+
*boxed_fee_error,
351356
TransactionFeeError::GasBoundsExceedBalance {resource, .. }
357+
if resource == Resource::L1Gas
352358
)
353-
if resource == Resource::L1Gas
354359
)
355360
);
356361
}
@@ -380,13 +385,15 @@ fn test_simulate_validate_pre_validate_with_charge_fee(
380385
TransactionExecutionError::TransactionPreValidationError(boxed_error)
381386
=> assert_matches!(
382387
*boxed_error,
383-
TransactionPreValidationError::TransactionFeeError(
384-
TransactionFeeError::InsufficientResourceBounds{ errors }
385-
)
388+
TransactionPreValidationError::TransactionFeeError(boxed_fee_error)
386389
=> assert_matches!(
387-
errors[0],
388-
ResourceBoundsError::MaxGasPriceTooLow { resource, .. }
389-
if resource == Resource::L1Gas
390+
*boxed_fee_error,
391+
TransactionFeeError::InsufficientResourceBounds{ errors }
392+
=> assert_matches!(
393+
errors[0],
394+
ResourceBoundsError::MaxGasPriceTooLow { resource, .. }
395+
if resource == Resource::L1Gas
396+
)
390397
)
391398
)
392399
);

0 commit comments

Comments
 (0)