Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 18 additions & 25 deletions crates/apollo_gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use apollo_gateway_types::deprecated_gateway_error::{
StarknetErrorCode,
};
use apollo_mempool_types::communication::MockMempoolClient;
use blockifier::blockifier::stateful_validator::{
StatefulValidatorError as BlockifierStatefulValidatorError,
StatefulValidatorResult as BlockifierStatefulValidatorResult,
};
use blockifier::blockifier::stateful_validator::StatefulValidatorError as BlockifierStatefulValidatorError;
use blockifier::context::ChainInfo;
use blockifier::test_utils::contracts::FeatureContractTrait;
use blockifier::transaction::errors::{TransactionFeeError, TransactionPreValidationError};
Expand Down Expand Up @@ -43,39 +40,35 @@ use crate::stateful_transaction_validator::{
StatefulTransactionValidator,
};

pub const STATEFUL_VALIDATOR_FEE_ERROR: BlockifierStatefulValidatorError =
BlockifierStatefulValidatorError::TransactionPreValidationError(
TransactionPreValidationError::TransactionFeeError(
TransactionFeeError::GasBoundsExceedBalance {
resource: Resource::L1DataGas,
max_amount: GasAmount(VALID_L1_GAS_MAX_AMOUNT),
max_price: GasPrice(VALID_L1_GAS_MAX_PRICE_PER_UNIT),
balance: BigUint::ZERO,
},
),
);

#[fixture]
fn stateful_validator() -> StatefulTransactionValidator {
StatefulTransactionValidator { config: StatefulTransactionValidatorConfig::default() }
}

// TODO(Arni): consider testing declare and deploy account.
#[rstest]
#[case::valid_tx(
create_executable_invoke_tx(CairoVersion::Cairo1(RunnableCairo1::Casm)),
Ok(())
)]
#[case::invalid_tx(
create_executable_invoke_tx(CairoVersion::Cairo1(RunnableCairo1::Casm)),
Err(STATEFUL_VALIDATOR_FEE_ERROR)
)]
#[case::valid_tx(create_executable_invoke_tx(CairoVersion::Cairo1(RunnableCairo1::Casm)), true)]
#[case::invalid_tx(create_executable_invoke_tx(CairoVersion::Cairo1(RunnableCairo1::Casm)), false)]
#[tokio::test]
async fn test_stateful_tx_validator(
#[case] executable_tx: AccountTransaction,
#[case] expected_result: BlockifierStatefulValidatorResult<()>,
#[case] expect_ok: bool,
stateful_validator: StatefulTransactionValidator,
) {
let expected_result = if expect_ok {
Ok(())
} else {
Err(BlockifierStatefulValidatorError::TransactionPreValidationError(
TransactionPreValidationError::TransactionFeeError(Box::new(
TransactionFeeError::GasBoundsExceedBalance {
resource: Resource::L1DataGas,
max_amount: GasAmount(VALID_L1_GAS_MAX_AMOUNT),
max_price: GasPrice(VALID_L1_GAS_MAX_PRICE_PER_UNIT),
balance: BigUint::ZERO,
},
)),
))
};
let expected_result_as_stateful_transaction_result = expected_result
.as_ref()
.map(|validate_result| *validate_result)
Expand Down
10 changes: 5 additions & 5 deletions crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ impl AccountTransaction {
if self.execution_flags.charge_fee {
self.check_fee_bounds(tx_context)?;

verify_can_pay_committed_bounds(state, tx_context)?;
verify_can_pay_committed_bounds(state, tx_context).map_err(Box::new)?;
}

Ok(())
Expand Down Expand Up @@ -345,9 +345,9 @@ impl AccountTransaction {
)
.collect::<Vec<_>>();
if !insufficiencies.is_empty() {
return Err(TransactionFeeError::InsufficientResourceBounds {
return Err(Box::new(TransactionFeeError::InsufficientResourceBounds {
errors: insufficiencies,
})?;
}))?;
}
}
TransactionInfo::Deprecated(context) => {
Expand All @@ -359,9 +359,9 @@ impl AccountTransaction {
tx_context.effective_tip(),
);
if max_fee < min_fee {
return Err(TransactionPreValidationError::TransactionFeeError(
return Err(TransactionPreValidationError::TransactionFeeError(Box::new(
TransactionFeeError::MaxFeeTooLow { min_fee, max_fee },
));
)));
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/transaction/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub enum TransactionPreValidationError {
#[error(transparent)]
StateError(#[from] StateError),
#[error(transparent)]
TransactionFeeError(#[from] TransactionFeeError),
TransactionFeeError(#[from] Box<TransactionFeeError>),
}

#[derive(Debug, Error)]
Expand Down
49 changes: 28 additions & 21 deletions crates/blockifier/src/transaction/execution_flavors_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,10 @@ fn test_simulate_validate_pre_validate_with_charge_fee(
assert_matches!(
err,
TransactionExecutionError::TransactionPreValidationError(boxed_error)
if matches!(
=> assert_matches!(
*boxed_error,
TransactionPreValidationError::TransactionFeeError(
TransactionFeeError::MaxFeeTooLow { .. }
)
TransactionPreValidationError::TransactionFeeError(boxed_fee_error)
if matches!(*boxed_fee_error, TransactionFeeError::MaxFeeTooLow { .. })
)
);
} else {
Expand All @@ -293,13 +292,15 @@ fn test_simulate_validate_pre_validate_with_charge_fee(
TransactionExecutionError::TransactionPreValidationError(boxed_error)
=> assert_matches!(
*boxed_error,
TransactionPreValidationError::TransactionFeeError(
TransactionFeeError::InsufficientResourceBounds { errors }
)
TransactionPreValidationError::TransactionFeeError(boxed_fee_error)
=> assert_matches!(
errors[0],
ResourceBoundsError::MaxGasAmountTooLow { resource , .. }
if resource == Resource::L1Gas
*boxed_fee_error,
TransactionFeeError::InsufficientResourceBounds { errors }
=> assert_matches!(
errors[0],
ResourceBoundsError::MaxGasAmountTooLow { resource , .. }
if resource == Resource::L1Gas
)
)
)
);
Expand Down Expand Up @@ -334,9 +335,11 @@ fn test_simulate_validate_pre_validate_with_charge_fee(
assert_matches!(
result.unwrap_err(),
TransactionExecutionError::TransactionPreValidationError(boxed_error)
if matches!(
=> assert_matches!(
*boxed_error,
TransactionPreValidationError::TransactionFeeError(
TransactionPreValidationError::TransactionFeeError(boxed_fee_error)
=> assert_matches!(
*boxed_fee_error,
TransactionFeeError::MaxFeeExceedsBalance { .. }
)
)
Expand All @@ -345,12 +348,14 @@ fn test_simulate_validate_pre_validate_with_charge_fee(
assert_matches!(
result.unwrap_err(),
TransactionExecutionError::TransactionPreValidationError(boxed_error)
if matches!(
=> assert_matches!(
*boxed_error,
TransactionPreValidationError::TransactionFeeError(
TransactionPreValidationError::TransactionFeeError(boxed_fee_error)
=> assert_matches!(
*boxed_fee_error,
TransactionFeeError::GasBoundsExceedBalance {resource, .. }
if resource == Resource::L1Gas
)
if resource == Resource::L1Gas
)
);
}
Expand Down Expand Up @@ -380,13 +385,15 @@ fn test_simulate_validate_pre_validate_with_charge_fee(
TransactionExecutionError::TransactionPreValidationError(boxed_error)
=> assert_matches!(
*boxed_error,
TransactionPreValidationError::TransactionFeeError(
TransactionFeeError::InsufficientResourceBounds{ errors }
)
TransactionPreValidationError::TransactionFeeError(boxed_fee_error)
=> assert_matches!(
errors[0],
ResourceBoundsError::MaxGasPriceTooLow { resource, .. }
if resource == Resource::L1Gas
*boxed_fee_error,
TransactionFeeError::InsufficientResourceBounds{ errors }
=> assert_matches!(
errors[0],
ResourceBoundsError::MaxGasPriceTooLow { resource, .. }
if resource == Resource::L1Gas
)
)
)
);
Expand Down
Loading
Loading