Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Refactor error unpacking in test_validate_accounts_tx. #1218

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::execution::contract_class::{ContractClass, ContractClassV0};
use crate::execution::entry_point::{CallEntryPoint, CallType};
use crate::execution::errors::{EntryPointExecutionError, VirtualMachineExecutionError};
use crate::execution::execution_utils::felt_to_stark_felt;
use crate::transaction::errors::TransactionExecutionError;
use crate::utils::const_max;

// Addresses.
Expand Down Expand Up @@ -197,6 +198,19 @@ fn default_testing_resource_bounds() -> ResourceBoundsMapping {

// Transactions.

pub fn check_transaction_execution_error_for_custom_hint(
error: &TransactionExecutionError,
expected_hint: &str,
) {
match error {
TransactionExecutionError::ContractConstructorExecutionFailed(error)
| TransactionExecutionError::ValidateTransactionError(error) => {
check_entry_point_execution_error_for_custom_hint(error, expected_hint)
}
_ => panic!("Unexpected structure for error: {:?}", error),
}
}

/// Checks that the given error is a `HintError::CustomHint` with the given hint.
pub fn check_entry_point_execution_error_for_custom_hint(
error: &EntryPointExecutionError,
Expand All @@ -221,6 +235,23 @@ pub fn check_entry_point_execution_error_for_custom_hint(
}
}

pub fn check_transaction_execution_error_for_diff_assert_values(error: &TransactionExecutionError) {
if let TransactionExecutionError::ValidateTransactionError(
EntryPointExecutionError::VirtualMachineExecutionErrorWithTrace {
source:
VirtualMachineExecutionError::CairoRunError(CairoRunError::VmException(VmException {
inner_exc: VirtualMachineError::DiffAssertValues(_),
..
})),
..
},
) = error
{
} else {
panic!("Unexpected structure for error: {:?}", error);
}
}

pub fn create_calldata(
contract_address: ContractAddress,
entry_point_name: &str,
Expand Down
30 changes: 11 additions & 19 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ use crate::test_utils::declare::declare_tx;
use crate::test_utils::dict_state_reader::DictStateReader;
use crate::test_utils::invoke::{invoke_tx, InvokeTxArgs};
use crate::test_utils::{
check_entry_point_execution_error_for_custom_hint, create_calldata,
check_transaction_execution_error_for_custom_hint,
check_transaction_execution_error_for_diff_assert_values, create_calldata,
test_erc20_account_balance_key, test_erc20_sequencer_balance_key, NonceManager,
ACCOUNT_CONTRACT_CAIRO1_PATH, BALANCE, CHAIN_ID_NAME, CURRENT_BLOCK_NUMBER,
CURRENT_BLOCK_TIMESTAMP, MAX_FEE, MAX_L1_GAS_AMOUNT, MAX_L1_GAS_PRICE,
Expand Down Expand Up @@ -1125,8 +1126,7 @@ fn test_validate_accounts_tx(#[case] tx_type: TransactionType) {
let account_tx =
create_account_tx_for_validate_test(tx_type, INVALID, None, &mut NonceManager::default());
let error = account_tx.execute(state, block_context, true, true).unwrap_err();
// TODO(Noa,01/05/2023): Test the exact failure reason.
assert_matches!(error, TransactionExecutionError::ValidateTransactionError(_));
check_transaction_execution_error_for_diff_assert_values(&error);

// Trying to call another contract (forbidden).
let account_tx = create_account_tx_for_validate_test(
Expand All @@ -1136,14 +1136,10 @@ fn test_validate_accounts_tx(#[case] tx_type: TransactionType) {
&mut NonceManager::default(),
);
let error = account_tx.execute(state, block_context, true, true).unwrap_err();
if let TransactionExecutionError::ValidateTransactionError(error) = error {
check_entry_point_execution_error_for_custom_hint(
&error,
"Unauthorized syscall call_contract in execution mode Validate.",
);
} else {
panic!("Expected ValidateTransactionError.")
}
check_transaction_execution_error_for_custom_hint(
&error,
"Unauthorized syscall call_contract in execution mode Validate.",
);

// Verify that the contract does not call another contract in the constructor of deploy account
// as well.
Expand All @@ -1165,14 +1161,10 @@ fn test_validate_accounts_tx(#[case] tx_type: TransactionType) {
);
let account_tx = AccountTransaction::DeployAccount(deploy_account_tx);
let error = account_tx.execute(state, block_context, true, true).unwrap_err();
if let TransactionExecutionError::ContractConstructorExecutionFailed(error) = error {
check_entry_point_execution_error_for_custom_hint(
&error,
"Unauthorized syscall call_contract in execution mode Validate.",
);
} else {
panic!("Expected ContractConstructorExecutionFailed.")
}
check_transaction_execution_error_for_custom_hint(
&error,
"Unauthorized syscall call_contract in execution mode Validate.",
);
}
}

Expand Down
Loading