Skip to content

Commit bce99b6

Browse files
blockifier: extract get_expected_execution_info to function
1 parent 39a4c59 commit bce99b6

File tree

2 files changed

+154
-71
lines changed

2 files changed

+154
-71
lines changed

crates/blockifier/src/transaction/test_utils.rs

Lines changed: 134 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ use blockifier_test_utils::calldata::create_calldata;
33
use blockifier_test_utils::contracts::FeatureContract;
44
use rstest::fixture;
55
use starknet_api::abi::abi_utils::get_fee_token_var_address;
6-
use starknet_api::block::{FeeType, GasPrice};
6+
use starknet_api::block::{BlockNumber, BlockTimestamp, FeeType, GasPrice};
77
use starknet_api::contract_class::compiled_class_hash::HashVersion;
88
use starknet_api::contract_class::{ClassInfo, ContractClass, SierraVersion};
9-
use starknet_api::core::{ClassHash, ContractAddress, Nonce};
9+
use starknet_api::core::{ChainId, ClassHash, ContractAddress, EntryPointSelector, Nonce};
10+
use starknet_api::data_availability::DataAvailabilityMode;
1011
use starknet_api::executable_transaction::TransactionType;
1112
use starknet_api::execution_resources::{GasAmount, GasVector};
1213
use starknet_api::test_utils::declare::executable_declare_tx;
@@ -26,15 +27,23 @@ use starknet_api::test_utils::{
2627
MAX_FEE,
2728
};
2829
use starknet_api::transaction::fields::{
30+
AccountDeploymentData,
2931
AllResourceBounds,
3032
ContractAddressSalt,
3133
Fee,
3234
GasVectorComputationMode,
35+
PaymasterData,
36+
Resource,
3337
ResourceBounds,
3438
TransactionSignature,
3539
ValidResourceBounds,
3640
};
37-
use starknet_api::transaction::{constants, TransactionVersion};
41+
use starknet_api::transaction::{
42+
constants,
43+
TransactionHash,
44+
TransactionVersion,
45+
QUERY_VERSION_BASE,
46+
};
3847
use starknet_api::{calldata, declare_tx_args, deploy_account_tx_args, felt, invoke_tx_args};
3948
use starknet_types_core::felt::Felt;
4049
use strum::IntoEnumIterator;
@@ -452,3 +461,125 @@ pub fn emit_n_events_tx(
452461

453462
AccountTransaction::new_for_sequencing(tx)
454463
}
464+
465+
/// Utility struct to test the execution info syscall.
466+
/// For simplicity, some fields are not included in the struct, and assumed empty.
467+
pub struct ExpectedExecutionInfo {
468+
pub version: TransactionVersion,
469+
pub account_address: ContractAddress,
470+
pub max_fee: Fee,
471+
pub transaction_hash: TransactionHash,
472+
pub chain_id: ChainId,
473+
pub nonce: Nonce,
474+
pub resource_bounds: ValidResourceBounds,
475+
pub paymaster_data: PaymasterData,
476+
pub nonce_data_availability_mode: DataAvailabilityMode,
477+
pub fee_data_availability_mode: DataAvailabilityMode,
478+
pub account_deployment_data: AccountDeploymentData,
479+
pub caller_address: ContractAddress,
480+
pub contract_address: ContractAddress,
481+
pub entry_point_selector: EntryPointSelector,
482+
pub block_number: BlockNumber,
483+
pub block_timestamp: BlockTimestamp,
484+
pub sequencer_address: ContractAddress,
485+
}
486+
487+
impl ExpectedExecutionInfo {
488+
#[allow(clippy::too_many_arguments)]
489+
pub fn new(
490+
only_query: bool,
491+
account_address: ContractAddress,
492+
caller_address: ContractAddress,
493+
contract_address: ContractAddress,
494+
chain_id: ChainId,
495+
entry_point_selector: EntryPointSelector,
496+
block_number: BlockNumber,
497+
block_timestamp: BlockTimestamp,
498+
sequencer_address: ContractAddress,
499+
resource_bounds: ValidResourceBounds,
500+
nonce: Nonce,
501+
) -> Self {
502+
let mut version = Felt::THREE;
503+
if only_query {
504+
version += *QUERY_VERSION_BASE;
505+
}
506+
Self {
507+
version: TransactionVersion(version),
508+
account_address,
509+
caller_address,
510+
contract_address,
511+
chain_id,
512+
entry_point_selector,
513+
block_number,
514+
block_timestamp,
515+
sequencer_address,
516+
resource_bounds,
517+
nonce,
518+
max_fee: Fee::default(),
519+
transaction_hash: TransactionHash::default(),
520+
paymaster_data: PaymasterData::default(),
521+
nonce_data_availability_mode: DataAvailabilityMode::default(),
522+
fee_data_availability_mode: DataAvailabilityMode::default(),
523+
account_deployment_data: AccountDeploymentData::default(),
524+
}
525+
}
526+
527+
pub fn to_syscall_result(self) -> Vec<Felt> {
528+
let expected_tx_info = vec![
529+
self.version.0,
530+
**self.account_address,
531+
self.max_fee.0.into(),
532+
Felt::ZERO,
533+
self.transaction_hash.0,
534+
Felt::from_hex_unchecked(&self.chain_id.as_hex()),
535+
self.nonce.0,
536+
];
537+
538+
let expected_resource_bounds = match self.resource_bounds {
539+
ValidResourceBounds::L1Gas(l1_gas) => vec![
540+
Felt::ONE,
541+
felt!(Resource::L1Gas.to_hex()),
542+
felt!(l1_gas.max_amount.0),
543+
felt!(l1_gas.max_price_per_unit.0),
544+
],
545+
ValidResourceBounds::AllResources(AllResourceBounds {
546+
l1_gas,
547+
l2_gas,
548+
l1_data_gas,
549+
}) => {
550+
vec![
551+
Felt::THREE,
552+
felt!(Resource::L1Gas.to_hex()),
553+
felt!(l1_gas.max_amount.0),
554+
felt!(l1_gas.max_price_per_unit.0),
555+
felt!(Resource::L2Gas.to_hex()),
556+
felt!(l2_gas.max_amount.0),
557+
felt!(l2_gas.max_price_per_unit.0),
558+
felt!(Resource::L1DataGas.to_hex()),
559+
felt!(l1_data_gas.max_amount.0),
560+
felt!(l1_data_gas.max_price_per_unit.0),
561+
]
562+
}
563+
};
564+
565+
// Tip, Paymaster data, Nonce DA, Fee DA, Account data.
566+
let expected_unsupported_fields = vec![Felt::ZERO; 5];
567+
568+
let expected_call_info =
569+
vec![**self.caller_address, **self.contract_address, self.entry_point_selector.0];
570+
let expected_block_info = vec![
571+
felt!(self.block_number.0),
572+
felt!(self.block_timestamp.0),
573+
**self.sequencer_address,
574+
];
575+
576+
[
577+
expected_block_info,
578+
expected_tx_info,
579+
expected_resource_bounds,
580+
expected_unsupported_fields,
581+
expected_call_info,
582+
]
583+
.concat()
584+
}
585+
}

crates/blockifier/src/transaction/transactions_test.rs

Lines changed: 20 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use starknet_api::abi::abi_utils::{
1818
selector_from_name,
1919
};
2020
use starknet_api::abi::constants::CONSTRUCTOR_ENTRY_POINT_NAME;
21-
use starknet_api::block::{FeeType, GasPriceVector};
21+
use starknet_api::block::{BlockNumber, BlockTimestamp, FeeType, GasPriceVector};
2222
use starknet_api::contract_class::compiled_class_hash::HashVersion;
2323
use starknet_api::contract_class::EntryPointType;
2424
use starknet_api::core::{ascii_as_felt, ClassHash, ContractAddress, Nonce};
@@ -43,9 +43,6 @@ use starknet_api::test_utils::{
4343
CURRENT_BLOCK_NUMBER_FOR_VALIDATE,
4444
CURRENT_BLOCK_TIMESTAMP,
4545
CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE,
46-
DEFAULT_L1_DATA_GAS_MAX_AMOUNT,
47-
DEFAULT_L1_GAS_AMOUNT,
48-
DEFAULT_L2_GAS_MAX_AMOUNT,
4946
DEFAULT_STRK_L1_DATA_GAS_PRICE,
5047
DEFAULT_STRK_L1_GAS_PRICE,
5148
DEFAULT_STRK_L2_GAS_PRICE,
@@ -71,7 +68,6 @@ use starknet_api::transaction::{
7168
EventKey,
7269
L2ToL1Payload,
7370
TransactionVersion,
74-
QUERY_VERSION_BASE,
7571
};
7672
use starknet_api::{
7773
calldata,
@@ -168,6 +164,7 @@ use crate::transaction::test_utils::{
168164
invoke_tx_with_default_flags,
169165
l1_resource_bounds,
170166
versioned_constants,
167+
ExpectedExecutionInfo,
171168
FaultyAccountTxCreatorArgs,
172169
TestInitData,
173170
CALL_CONTRACT,
@@ -2601,76 +2598,31 @@ fn test_only_query_flag(
26012598
&block_context.chain_info,
26022599
CairoVersion::Cairo1(RunnableCairo1::Casm),
26032600
);
2604-
let mut version = Felt::from(3_u8);
2605-
if only_query {
2606-
version += *QUERY_VERSION_BASE;
2607-
}
2608-
let expected_tx_info = vec![
2609-
version, // Transaction version.
2610-
*account_address.0.key(), // Account address.
2611-
Felt::ZERO, // Max fee.
2612-
Felt::ZERO, // Signature.
2613-
Felt::ZERO, // Transaction hash.
2614-
felt!(&*CHAIN_ID_FOR_TESTS.as_hex()), // Chain ID.
2615-
Felt::ZERO, // Nonce.
2616-
];
2617-
2618-
let expected_resource_bounds = vec![
2619-
Felt::THREE, // Length of ResourceBounds array.
2620-
felt!(L1Gas.to_hex()), // Resource.
2621-
felt!(DEFAULT_L1_GAS_AMOUNT.0), // Max amount.
2622-
felt!(DEFAULT_STRK_L1_GAS_PRICE.get().0), // Max price per unit.
2623-
felt!(L2Gas.to_hex()), // Resource.
2624-
felt!(DEFAULT_L2_GAS_MAX_AMOUNT.0), // Max amount.
2625-
felt!(DEFAULT_STRK_L2_GAS_PRICE.get().0), // Max price per unit.
2626-
felt!(L1DataGas.to_hex()), // Resource.
2627-
felt!(DEFAULT_L1_DATA_GAS_MAX_AMOUNT.0), // Max amount.
2628-
felt!(DEFAULT_STRK_L1_DATA_GAS_PRICE.get().0), // Max price per unit.
2629-
];
2630-
2631-
let expected_unsupported_fields = vec![
2632-
Felt::ZERO, // Tip.
2633-
Felt::ZERO, // Paymaster data.
2634-
Felt::ZERO, // Nonce DA.
2635-
Felt::ZERO, // Fee DA.
2636-
Felt::ZERO, // Account data.
2637-
];
2638-
26392601
let entry_point_selector = selector_from_name("test_get_execution_info");
2640-
let expected_call_info = vec![
2641-
*account_address.0.key(), // Caller address.
2642-
*contract_address.0.key(), // Storage address.
2643-
entry_point_selector.0, // Entry point selector.
2644-
];
2645-
let expected_block_info = [
2646-
felt!(CURRENT_BLOCK_NUMBER), // Block number.
2647-
felt!(CURRENT_BLOCK_TIMESTAMP), // Block timestamp.
2648-
felt!(TEST_SEQUENCER_ADDRESS), // Sequencer address.
2649-
];
2650-
let calldata_len = expected_block_info.len()
2651-
+ expected_tx_info.len()
2652-
+ expected_resource_bounds.len()
2653-
+ expected_unsupported_fields.len()
2654-
+ expected_call_info.len();
2602+
let expected_execution_info = ExpectedExecutionInfo::new(
2603+
only_query,
2604+
account_address,
2605+
account_address,
2606+
contract_address,
2607+
CHAIN_ID_FOR_TESTS.clone(),
2608+
entry_point_selector,
2609+
BlockNumber(CURRENT_BLOCK_NUMBER),
2610+
BlockTimestamp(CURRENT_BLOCK_TIMESTAMP),
2611+
ContractAddress(felt!(TEST_SEQUENCER_ADDRESS).try_into().unwrap()),
2612+
default_all_resource_bounds,
2613+
Nonce::default(),
2614+
)
2615+
.to_syscall_result();
26552616
let execute_calldata = vec![
26562617
*contract_address.0.key(), // Contract address.
26572618
entry_point_selector.0, // EP selector.
26582619
// TODO(Ori, 1/2/2024): Write an indicative expect message explaining why the conversion
26592620
// works.
2660-
felt!(u64::try_from(calldata_len).expect("Failed to convert usize to u64.")), /* Calldata length. */
2621+
felt!(
2622+
u64::try_from(expected_execution_info.len()).expect("Failed to convert usize to u64.")
2623+
), // Calldata length.
26612624
];
2662-
let execute_calldata = Calldata(
2663-
[
2664-
execute_calldata,
2665-
expected_block_info.clone().to_vec(),
2666-
expected_tx_info,
2667-
expected_resource_bounds,
2668-
expected_unsupported_fields,
2669-
expected_call_info,
2670-
]
2671-
.concat()
2672-
.into(),
2673-
);
2625+
let execute_calldata = Calldata([execute_calldata, expected_execution_info].concat().into());
26742626
let tx = executable_invoke_tx(invoke_tx_args! {
26752627
calldata: execute_calldata,
26762628
resource_bounds: default_all_resource_bounds,

0 commit comments

Comments
 (0)