Skip to content

Commit 4a01137

Browse files
blockifier: extract get_expected_execution_info to function
1 parent 088c95f commit 4a01137

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+
pub fn new(
489+
only_query: bool,
490+
account_address: ContractAddress,
491+
caller_address: ContractAddress,
492+
contract_address: ContractAddress,
493+
chain_id: ChainId,
494+
entry_point_selector: EntryPointSelector,
495+
block_number: BlockNumber,
496+
block_timestamp: BlockTimestamp,
497+
sequencer_address: ContractAddress,
498+
resource_bounds: ValidResourceBounds,
499+
nonce: Nonce,
500+
) -> Self {
501+
let mut version = Felt::THREE;
502+
if only_query {
503+
version += *QUERY_VERSION_BASE;
504+
}
505+
Self {
506+
version: TransactionVersion(version),
507+
account_address,
508+
caller_address,
509+
contract_address,
510+
chain_id,
511+
entry_point_selector,
512+
block_number,
513+
block_timestamp,
514+
sequencer_address,
515+
resource_bounds,
516+
nonce,
517+
max_fee: Fee::default(),
518+
transaction_hash: TransactionHash::default(),
519+
paymaster_data: PaymasterData::default(),
520+
nonce_data_availability_mode: DataAvailabilityMode::default(),
521+
fee_data_availability_mode: DataAvailabilityMode::default(),
522+
account_deployment_data: AccountDeploymentData::default(),
523+
}
524+
}
525+
526+
pub fn to_syscall_result(self) -> Vec<Felt> {
527+
let expected_tx_info = vec![
528+
self.version.0,
529+
**self.account_address,
530+
self.max_fee.0.into(),
531+
Felt::ZERO,
532+
self.transaction_hash.0,
533+
Felt::from_hex_unchecked(&self.chain_id.as_hex()),
534+
self.nonce.0,
535+
];
536+
537+
let expected_resource_bounds = match self.resource_bounds {
538+
ValidResourceBounds::L1Gas(l1_gas) => vec![
539+
Felt::ONE,
540+
felt!(Resource::L1Gas.to_hex()),
541+
felt!(l1_gas.max_amount.0),
542+
felt!(l1_gas.max_price_per_unit.0),
543+
],
544+
ValidResourceBounds::AllResources(AllResourceBounds {
545+
l1_gas,
546+
l2_gas,
547+
l1_data_gas,
548+
}) => {
549+
vec![
550+
Felt::THREE,
551+
felt!(Resource::L1Gas.to_hex()),
552+
felt!(l1_gas.max_amount.0),
553+
felt!(l1_gas.max_price_per_unit.0),
554+
felt!(Resource::L2Gas.to_hex()),
555+
felt!(l2_gas.max_amount.0),
556+
felt!(l2_gas.max_price_per_unit.0),
557+
felt!(Resource::L1DataGas.to_hex()),
558+
felt!(l1_data_gas.max_amount.0),
559+
felt!(l1_data_gas.max_price_per_unit.0),
560+
]
561+
}
562+
};
563+
564+
// Tip, Paymaster data, Nonce DA, Fee DA, Account data.
565+
let expected_unsupported_fields = vec![Felt::ZERO; 5];
566+
567+
let expected_call_info =
568+
vec![**self.caller_address, **self.contract_address, self.entry_point_selector.0];
569+
let expected_block_info = vec![
570+
felt!(self.block_number.0),
571+
felt!(self.block_timestamp.0),
572+
**self.sequencer_address,
573+
];
574+
575+
[
576+
expected_block_info,
577+
expected_tx_info,
578+
expected_resource_bounds,
579+
expected_unsupported_fields,
580+
expected_call_info,
581+
]
582+
.concat()
583+
.into()
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,
@@ -2584,76 +2581,31 @@ fn test_only_query_flag(
25842581
&block_context.chain_info,
25852582
CairoVersion::Cairo1(RunnableCairo1::Casm),
25862583
);
2587-
let mut version = Felt::from(3_u8);
2588-
if only_query {
2589-
version += *QUERY_VERSION_BASE;
2590-
}
2591-
let expected_tx_info = vec![
2592-
version, // Transaction version.
2593-
*account_address.0.key(), // Account address.
2594-
Felt::ZERO, // Max fee.
2595-
Felt::ZERO, // Signature.
2596-
Felt::ZERO, // Transaction hash.
2597-
felt!(&*CHAIN_ID_FOR_TESTS.as_hex()), // Chain ID.
2598-
Felt::ZERO, // Nonce.
2599-
];
2600-
2601-
let expected_resource_bounds = vec![
2602-
Felt::THREE, // Length of ResourceBounds array.
2603-
felt!(L1Gas.to_hex()), // Resource.
2604-
felt!(DEFAULT_L1_GAS_AMOUNT.0), // Max amount.
2605-
felt!(DEFAULT_STRK_L1_GAS_PRICE.get().0), // Max price per unit.
2606-
felt!(L2Gas.to_hex()), // Resource.
2607-
felt!(DEFAULT_L2_GAS_MAX_AMOUNT.0), // Max amount.
2608-
felt!(DEFAULT_STRK_L2_GAS_PRICE.get().0), // Max price per unit.
2609-
felt!(L1DataGas.to_hex()), // Resource.
2610-
felt!(DEFAULT_L1_DATA_GAS_MAX_AMOUNT.0), // Max amount.
2611-
felt!(DEFAULT_STRK_L1_DATA_GAS_PRICE.get().0), // Max price per unit.
2612-
];
2613-
2614-
let expected_unsupported_fields = vec![
2615-
Felt::ZERO, // Tip.
2616-
Felt::ZERO, // Paymaster data.
2617-
Felt::ZERO, // Nonce DA.
2618-
Felt::ZERO, // Fee DA.
2619-
Felt::ZERO, // Account data.
2620-
];
2621-
26222584
let entry_point_selector = selector_from_name("test_get_execution_info");
2623-
let expected_call_info = vec![
2624-
*account_address.0.key(), // Caller address.
2625-
*contract_address.0.key(), // Storage address.
2626-
entry_point_selector.0, // Entry point selector.
2627-
];
2628-
let expected_block_info = [
2629-
felt!(CURRENT_BLOCK_NUMBER), // Block number.
2630-
felt!(CURRENT_BLOCK_TIMESTAMP), // Block timestamp.
2631-
felt!(TEST_SEQUENCER_ADDRESS), // Sequencer address.
2632-
];
2633-
let calldata_len = expected_block_info.len()
2634-
+ expected_tx_info.len()
2635-
+ expected_resource_bounds.len()
2636-
+ expected_unsupported_fields.len()
2637-
+ expected_call_info.len();
2585+
let expected_execution_info = ExpectedExecutionInfo::new(
2586+
only_query,
2587+
account_address,
2588+
account_address,
2589+
contract_address,
2590+
CHAIN_ID_FOR_TESTS.clone(),
2591+
entry_point_selector,
2592+
BlockNumber(CURRENT_BLOCK_NUMBER),
2593+
BlockTimestamp(CURRENT_BLOCK_TIMESTAMP),
2594+
ContractAddress(felt!(TEST_SEQUENCER_ADDRESS).try_into().unwrap()),
2595+
default_all_resource_bounds,
2596+
Nonce::default(),
2597+
)
2598+
.to_syscall_result();
26382599
let execute_calldata = vec![
26392600
*contract_address.0.key(), // Contract address.
26402601
entry_point_selector.0, // EP selector.
26412602
// TODO(Ori, 1/2/2024): Write an indicative expect message explaining why the conversion
26422603
// works.
2643-
felt!(u64::try_from(calldata_len).expect("Failed to convert usize to u64.")), /* Calldata length. */
2604+
felt!(
2605+
u64::try_from(expected_execution_info.len()).expect("Failed to convert usize to u64.")
2606+
), // Calldata length.
26442607
];
2645-
let execute_calldata = Calldata(
2646-
[
2647-
execute_calldata,
2648-
expected_block_info.clone().to_vec(),
2649-
expected_tx_info,
2650-
expected_resource_bounds,
2651-
expected_unsupported_fields,
2652-
expected_call_info,
2653-
]
2654-
.concat()
2655-
.into(),
2656-
);
2608+
let execute_calldata = Calldata([execute_calldata, expected_execution_info].concat().into());
26572609
let tx = executable_invoke_tx(invoke_tx_args! {
26582610
calldata: execute_calldata,
26592611
resource_bounds: default_all_resource_bounds,

0 commit comments

Comments
 (0)