Skip to content

Commit

Permalink
test(blockifier): parametrize feature contracts and tests by runnable…
Browse files Browse the repository at this point in the history
… type
  • Loading branch information
dorimedini-starkware committed Dec 2, 2024
1 parent d1ad687 commit 29115d1
Show file tree
Hide file tree
Showing 45 changed files with 1,452 additions and 653 deletions.
6 changes: 3 additions & 3 deletions crates/blockifier/src/blockifier/block_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use crate::abi::constants;
use crate::blockifier::block::pre_process_block;
use crate::context::ChainInfo;
use crate::state::state_api::StateReader;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::contracts::{FeatureContract, RunnableContractVersion};
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{CairoVersion, BALANCE};
use crate::test_utils::BALANCE;

#[test]
fn test_pre_process_block() {
let test_contract = FeatureContract::TestContract(CairoVersion::Cairo1);
let test_contract = FeatureContract::TestContract(RunnableContractVersion::Cairo1Casm);
let mut state = test_state(&ChainInfo::create_for_testing(), BALANCE, &[(test_contract, 1)]);

// Test the positive flow of pre_process_block inside the allowed block number interval
Expand Down
12 changes: 8 additions & 4 deletions crates/blockifier/src/blockifier/stateful_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use starknet_api::transaction::TransactionVersion;

use crate::blockifier::stateful_validator::StatefulValidator;
use crate::context::BlockContext;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::contracts::{FeatureContract, RunnableContractVersion};
use crate::test_utils::initial_test_state::{fund_account, test_state};
use crate::test_utils::{CairoVersion, BALANCE};
use crate::test_utils::BALANCE;
use crate::transaction::test_utils::{
block_context,
create_account_tx_for_validate_test_nonce_0,
Expand Down Expand Up @@ -37,7 +37,10 @@ fn test_tx_validator(
block_context: BlockContext,
#[values(default_l1_resource_bounds(), default_all_resource_bounds())]
resource_bounds: ValidResourceBounds,
#[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] cairo_version: CairoVersion,
// TODO: Add Native case if the feature is active. Currently it seems that this would require
// duplicating the test...
#[values(RunnableContractVersion::Cairo0, RunnableContractVersion::Cairo1Casm)]
cairo_version: RunnableContractVersion,
) {
let chain_info = &block_context.chain_info;

Expand Down Expand Up @@ -83,7 +86,8 @@ fn test_tx_validator_skip_validate(
resource_bounds: ValidResourceBounds,
) {
let block_context = BlockContext::create_for_testing();
let faulty_account = FeatureContract::FaultyAccount(CairoVersion::Cairo1);
// TODO: If Native is available, parametrize to also use native contract.
let faulty_account = FeatureContract::FaultyAccount(RunnableContractVersion::Cairo1Casm);
let state = test_state(&block_context.chain_info, BALANCE, &[(faulty_account, 1)]);

// Create a transaction that does not pass validations.
Expand Down
155 changes: 136 additions & 19 deletions crates/blockifier/src/blockifier/transaction_executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ use crate::bouncer::{Bouncer, BouncerWeights};
use crate::context::BlockContext;
use crate::state::cached_state::CachedState;
use crate::state::state_api::StateReader;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::contracts::{FeatureContract, RunnableContractVersion};
use crate::test_utils::declare::declare_tx;
use crate::test_utils::deploy_account::deploy_account_tx;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::l1_handler::l1handler_tx;
use crate::test_utils::{
create_calldata,
maybe_dummy_block_hash_and_number,
CairoVersion,
BALANCE,
DEFAULT_STRK_L1_GAS_PRICE,
};
Expand Down Expand Up @@ -71,7 +70,7 @@ fn tx_executor_test_body<S: StateReader>(
#[rstest]
#[case::tx_version_0(
TransactionVersion::ZERO,
CairoVersion::Cairo0,
RunnableContractVersion::Cairo0,
BouncerWeights {
state_diff_size: 0,
message_segment_length: 0,
Expand All @@ -81,39 +80,63 @@ fn tx_executor_test_body<S: StateReader>(
)]
#[case::tx_version_1(
TransactionVersion::ONE,
CairoVersion::Cairo0,
RunnableContractVersion::Cairo0,
BouncerWeights {
state_diff_size: 2,
message_segment_length: 0,
n_events: 0,
..BouncerWeights::empty()
}
)]
#[case::tx_version_2(
#[case::tx_version_2_casm(
TransactionVersion::TWO,
CairoVersion::Cairo1,
RunnableContractVersion::Cairo1Casm,
BouncerWeights {
state_diff_size: 4,
message_segment_length: 0,
n_events: 0,
..BouncerWeights::empty()
}
)]
#[case::tx_version_3(
#[cfg_attr(feature = "cairo_native", case::tx_version_2_native(
TransactionVersion::TWO,
RunnableContractVersion::Cairo1Native,
BouncerWeights {
state_diff_size: 4,
message_segment_length: 0,
n_events: 0,
..BouncerWeights::empty()
}
))]
#[case::tx_version_3_casm(
TransactionVersion::THREE,
CairoVersion::Cairo1,
RunnableContractVersion::Cairo1Casm,
BouncerWeights {
state_diff_size: 4,
message_segment_length: 0,
n_events: 0,
..BouncerWeights::empty()
}
)]
#[cfg_attr(feature = "cairo_native", case::tx_version_3_native(
TransactionVersion::THREE,
RunnableContractVersion::Cairo1Native,
BouncerWeights {
state_diff_size: 4,
message_segment_length: 0,
n_events: 0,
..BouncerWeights::empty()
}
))]
fn test_declare(
block_context: BlockContext,
#[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] account_cairo_version: CairoVersion,
// TODO: Add Native case if the feature is active. Currently it seems that this would require
// duplicating the test... or move from using #[values] to more #[case]es as done for the
// cairo_version parameter.
#[values(RunnableContractVersion::Cairo0, RunnableContractVersion::Cairo1Casm)]
account_cairo_version: RunnableContractVersion,
#[case] tx_version: TransactionVersion,
#[case] cairo_version: CairoVersion,
#[case] cairo_version: RunnableContractVersion,
#[case] expected_bouncer_weights: BouncerWeights,
) {
let account_contract = FeatureContract::AccountWithoutValidations(account_cairo_version);
Expand All @@ -135,10 +158,16 @@ fn test_declare(
}

#[rstest]
#[case::cairo0(RunnableContractVersion::Cairo0)]
#[case::cairo1_casm(RunnableContractVersion::Cairo1Casm)]
#[cfg_attr(
feature = "cairo_native",
case::cairo1_native(RunnableContractVersion::Cairo1Native)
)]
fn test_deploy_account(
block_context: BlockContext,
#[values(TransactionVersion::ONE, TransactionVersion::THREE)] version: TransactionVersion,
#[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] cairo_version: CairoVersion,
#[case] cairo_version: RunnableContractVersion,
) {
let account_contract = FeatureContract::AccountWithoutValidations(cairo_version);
let state = test_state(&block_context.chain_info, BALANCE, &[(account_contract, 0)]);
Expand All @@ -162,7 +191,48 @@ fn test_deploy_account(
}

#[rstest]
#[case::invoke_function_base_case(
#[case::invoke_function_base_case_cairo0(
RunnableContractVersion::Cairo0,
"assert_eq",
vec![
felt!(3_u32), // x.
felt!(3_u32) // y.
],
BouncerWeights {
state_diff_size: 2,
message_segment_length: 0,
n_events: 0,
..BouncerWeights::empty()
}
)]
#[case::emit_event_syscall_cairo0(
RunnableContractVersion::Cairo0,
"test_emit_events",
vec![
felt!(1_u32), // events_number.
felt!(0_u32), // keys length.
felt!(0_u32) // data length.
],
BouncerWeights {
state_diff_size: 2,
message_segment_length: 0,
n_events: 1,
..BouncerWeights::empty()
}
)]
#[case::storage_write_syscall_cairo0(
RunnableContractVersion::Cairo0,
"test_count_actual_storage_changes",
vec![],
BouncerWeights {
state_diff_size: 6,
message_segment_length: 0,
n_events: 0,
..BouncerWeights::empty()
}
)]
#[case::invoke_function_base_case_cairo1_casm(
RunnableContractVersion::Cairo1Casm,
"assert_eq",
vec![
felt!(3_u32), // x.
Expand All @@ -175,7 +245,8 @@ fn test_deploy_account(
..BouncerWeights::empty()
}
)]
#[case::emit_event_syscall(
#[case::emit_event_syscall_cairo1_casm(
RunnableContractVersion::Cairo1Casm,
"test_emit_events",
vec![
felt!(1_u32), // events_number.
Expand All @@ -189,7 +260,8 @@ fn test_deploy_account(
..BouncerWeights::empty()
}
)]
#[case::storage_write_syscall(
#[case::storage_write_syscall_cairo1_casm(
RunnableContractVersion::Cairo1Casm,
"test_count_actual_storage_changes",
vec![],
BouncerWeights {
Expand All @@ -199,10 +271,50 @@ fn test_deploy_account(
..BouncerWeights::empty()
}
)]
#[cfg_attr(feature = "cairo_native", case::invoke_function_base_case_cairo1_native(
RunnableContractVersion::Cairo1Native,
"assert_eq",
vec![
felt!(3_u32), // x.
felt!(3_u32) // y.
],
BouncerWeights {
state_diff_size: 2,
message_segment_length: 0,
n_events: 0,
..BouncerWeights::empty()
}
))]
#[cfg_attr(feature = "cairo_native", case::emit_event_syscall_cairo1_native(
RunnableContractVersion::Cairo1Native,
"test_emit_events",
vec![
felt!(1_u32), // events_number.
felt!(0_u32), // keys length.
felt!(0_u32) // data length.
],
BouncerWeights {
state_diff_size: 2,
message_segment_length: 0,
n_events: 1,
..BouncerWeights::empty()
}
))]
#[cfg_attr(feature = "cairo_native", case::storage_write_syscall_cairo1_native(
RunnableContractVersion::Cairo1Native,
"test_count_actual_storage_changes",
vec![],
BouncerWeights {
state_diff_size: 6,
message_segment_length: 0,
n_events: 0,
..BouncerWeights::empty()
}
))]
fn test_invoke(
block_context: BlockContext,
#[values(TransactionVersion::ONE, TransactionVersion::THREE)] version: TransactionVersion,
#[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] cairo_version: CairoVersion,
#[case] cairo_version: RunnableContractVersion,
#[case] entry_point_name: &str,
#[case] entry_point_args: Vec<Felt>,
#[case] expected_bouncer_weights: BouncerWeights,
Expand All @@ -227,8 +339,13 @@ fn test_invoke(
}

#[rstest]
fn test_l1_handler(block_context: BlockContext) {
let test_contract = FeatureContract::TestContract(CairoVersion::Cairo1);
#[case::cairo1_casm(RunnableContractVersion::Cairo1Casm)]
#[cfg_attr(
feature = "cairo_native",
case::cairo1_native(RunnableContractVersion::Cairo1Native)
)]
fn test_l1_handler(#[case] cairo_version: RunnableContractVersion, block_context: BlockContext) {
let test_contract = FeatureContract::TestContract(cairo_version);
let state = test_state(&block_context.chain_info, BALANCE, &[(test_contract, 1)]);

let tx = Transaction::L1Handler(l1handler_tx(
Expand Down Expand Up @@ -263,7 +380,7 @@ fn test_bouncing(#[case] initial_bouncer_weights: BouncerWeights, #[case] n_even
let block_context = BlockContext::create_for_bouncer_testing(max_n_events_in_block);

let TestInitData { state, account_address, contract_address, mut nonce_manager } =
create_test_init_data(&block_context.chain_info, CairoVersion::Cairo1);
create_test_init_data(&block_context.chain_info, RunnableContractVersion::Cairo1Casm);

// TODO(Yoni, 15/6/2024): turn on concurrency mode.
let mut tx_executor =
Expand Down Expand Up @@ -292,7 +409,7 @@ fn test_execute_txs_bouncing(#[values(true, false)] concurrency_enabled: bool) {
let block_context = BlockContext::create_for_bouncer_testing(max_n_events_in_block);

let TestInitData { state, account_address, contract_address, .. } =
create_test_init_data(&block_context.chain_info, CairoVersion::Cairo1);
create_test_init_data(&block_context.chain_info, RunnableContractVersion::Cairo1Casm);

let mut tx_executor = TransactionExecutor::new(state, block_context, config);

Expand Down
18 changes: 12 additions & 6 deletions crates/blockifier/src/concurrency/fee_utils_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,35 @@ use crate::concurrency::test_utils::create_fee_transfer_call_info;
use crate::context::BlockContext;
use crate::fee::fee_utils::get_sequencer_balance_keys;
use crate::state::state_api::StateReader;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::contracts::{FeatureContract, RunnableContractVersion};
use crate::test_utils::initial_test_state::{fund_account, test_state, test_state_inner};
use crate::test_utils::{create_trivial_calldata, CairoVersion, BALANCE};
use crate::test_utils::{create_trivial_calldata, BALANCE};
use crate::transaction::test_utils::{
account_invoke_tx,
block_context,
default_all_resource_bounds,
};

#[rstest]
#[case::cairo0(RunnableContractVersion::Cairo0)]
#[case::cairo1_casm(RunnableContractVersion::Cairo1Casm)]
#[cfg_attr(
feature = "cairo_native",
case::cairo1_native(RunnableContractVersion::Cairo1Native)
)]
pub fn test_fill_sequencer_balance_reads(
block_context: BlockContext,
default_all_resource_bounds: ValidResourceBounds,
#[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] erc20_version: CairoVersion,
#[case] cairo_version: RunnableContractVersion,
) {
let account = FeatureContract::AccountWithoutValidations(CairoVersion::Cairo1);
let account = FeatureContract::AccountWithoutValidations(cairo_version);
let account_tx = account_invoke_tx(invoke_tx_args! {
sender_address: account.get_instance_address(0),
calldata: create_trivial_calldata(account.get_instance_address(0)),
resource_bounds: default_all_resource_bounds,
});
let chain_info = &block_context.chain_info;
let state = &mut test_state_inner(chain_info, BALANCE, &[(account, 1)], erc20_version);
let state = &mut test_state_inner(chain_info, BALANCE, &[(account, 1)], cairo_version);

let sequencer_balance = Fee(100);
let sequencer_address = block_context.block_info.sequencer_address;
Expand Down Expand Up @@ -61,7 +67,7 @@ pub fn test_add_fee_to_sequencer_balance(
#[case] sequencer_balance_high: Felt,
) {
let block_context = BlockContext::create_for_account_testing();
let account = FeatureContract::Empty(CairoVersion::Cairo1);
let account = FeatureContract::Empty(RunnableContractVersion::Cairo1Casm);
let mut state = test_state(&block_context.chain_info, Fee(0), &[(account, 1)]);
let (sequencer_balance_key_low, sequencer_balance_key_high) =
get_sequencer_balance_keys(&block_context);
Expand Down
Loading

0 comments on commit 29115d1

Please sign in to comment.