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

Commit

Permalink
test: use FeatureContract in create_test_state and tests which uses it (
Browse files Browse the repository at this point in the history
  • Loading branch information
TzahiTaub authored Feb 21, 2024
1 parent 6a7742c commit 5fe26bd
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 89 deletions.
8 changes: 6 additions & 2 deletions crates/blockifier/src/blockifier/block_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ use crate::abi::constants;
use crate::blockifier::block::{pre_process_block, BlockInfo, BlockNumberHashPair};
use crate::context::ChainInfo;
use crate::state::state_api::StateReader;
use crate::test_utils::cached_state::create_test_state;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{CairoVersion, BALANCE};
use crate::versioned_constants::VersionedConstants;

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

// Test the positive flow of pre_process_block inside the allowed block number interval
let block_number = constants::STORED_BLOCK_HASH_BUFFER;
Expand Down
12 changes: 7 additions & 5 deletions crates/blockifier/src/execution/entry_point_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ use crate::execution::entry_point::CallEntryPoint;
use crate::execution::errors::EntryPointExecutionError;
use crate::retdata;
use crate::state::cached_state::CachedState;
use crate::test_utils::cached_state::{create_test_state, deprecated_create_test_state};
use crate::test_utils::cached_state::deprecated_create_test_state;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::dict_state_reader::DictStateReader;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{
create_calldata, trivial_external_entry_point, trivial_external_entry_point_with_address,
CairoVersion, BALANCE,
create_calldata, trivial_external_entry_point, trivial_external_entry_point_new,
trivial_external_entry_point_with_address, CairoVersion, BALANCE,
};
use crate::versioned_constants::VersionedConstants;

Expand Down Expand Up @@ -529,12 +529,14 @@ fn test_storage_related_members() {

#[test]
fn test_cairo1_entry_point_segment_arena() {
let mut state = create_test_state();
let test_contract = FeatureContract::TestContract(CairoVersion::Cairo1);
let chain_info = &ChainInfo::create_for_testing();
let mut state = test_state(chain_info, BALANCE, &[(test_contract, 1)]);
let calldata = calldata![];
let entry_point_call = CallEntryPoint {
calldata,
entry_point_selector: selector_from_name("segment_arena_builtin"),
..trivial_external_entry_point()
..trivial_external_entry_point_new(test_contract)
};

assert!(
Expand Down
127 changes: 77 additions & 50 deletions crates/blockifier/src/execution/syscalls/syscalls_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ use crate::execution::syscalls::hint_processor::{
EmitEventError, BLOCK_NUMBER_OUT_OF_RANGE_ERROR, L1_GAS, L2_GAS, OUT_OF_GAS_ERROR,
};
use crate::state::state_api::{State, StateReader};
use crate::test_utils::cached_state::{create_deploy_test_state, create_test_state};
use crate::test_utils::cached_state::create_deploy_test_state;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{
create_calldata, trivial_external_entry_point, CairoVersion, BALANCE, CHAIN_ID_NAME,
CURRENT_BLOCK_NUMBER, CURRENT_BLOCK_NUMBER_FOR_VALIDATE, CURRENT_BLOCK_TIMESTAMP,
CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE, TEST_CLASS_HASH, TEST_CONTRACT_ADDRESS,
TEST_EMPTY_CONTRACT_CAIRO0_PATH, TEST_EMPTY_CONTRACT_CLASS_HASH, TEST_SEQUENCER_ADDRESS,
create_calldata, trivial_external_entry_point, trivial_external_entry_point_new, CairoVersion,
BALANCE, CHAIN_ID_NAME, CURRENT_BLOCK_NUMBER, CURRENT_BLOCK_NUMBER_FOR_VALIDATE,
CURRENT_BLOCK_TIMESTAMP, CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE, TEST_CLASS_HASH,
TEST_CONTRACT_ADDRESS, TEST_EMPTY_CONTRACT_CAIRO0_PATH, TEST_EMPTY_CONTRACT_CLASS_HASH,
TEST_SEQUENCER_ADDRESS,
};
use crate::transaction::constants::QUERY_VERSION_BASE_BIT;
use crate::transaction::objects::{
Expand All @@ -59,15 +60,17 @@ pub const REQUIRED_GAS_LIBRARY_CALL_TEST: u64 = REQUIRED_GAS_CALL_CONTRACT_TEST;

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

let key = stark_felt!(1234_u16);
let value = stark_felt!(18_u8);
let calldata = calldata![key, value];
let entry_point_call = CallEntryPoint {
calldata,
entry_point_selector: selector_from_name("test_storage_read_write"),
..trivial_external_entry_point()
..trivial_external_entry_point_new(test_contract)
};
let storage_address = entry_point_call.storage_address;
assert_eq!(
Expand All @@ -86,11 +89,13 @@ fn test_storage_read_write() {

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

let outer_entry_point_selector = selector_from_name("test_call_contract");
let calldata = create_calldata(
contract_address!(TEST_CONTRACT_ADDRESS),
FeatureContract::TestContract(CairoVersion::Cairo1).get_instance_address(0),
"test_storage_read_write",
&[
stark_felt!(405_u16), // Calldata: address.
Expand All @@ -100,7 +105,7 @@ fn test_call_contract() {
let entry_point_call = CallEntryPoint {
entry_point_selector: outer_entry_point_selector,
calldata,
..trivial_external_entry_point()
..trivial_external_entry_point_new(test_contract)
};
assert_eq!(
entry_point_call.execute_directly(&mut state).unwrap().execution,
Expand Down Expand Up @@ -172,7 +177,9 @@ fn emit_events(
keys: &[StarkFelt],
data: &[StarkFelt],
) -> Result<CallInfo, EntryPointExecutionError> {
let mut state = create_test_state();
let test_contract = FeatureContract::TestContract(CairoVersion::Cairo1);
let chain_info = &ChainInfo::create_for_testing();
let mut state = test_state(chain_info, BALANCE, &[(test_contract, 1)]);
let calldata = Calldata(
concat(vec![
n_emitted_events.to_owned(),
Expand All @@ -187,15 +194,17 @@ fn emit_events(
let entry_point_call = CallEntryPoint {
entry_point_selector: selector_from_name("test_emit_events"),
calldata,
..trivial_external_entry_point()
..trivial_external_entry_point_new(test_contract)
};

entry_point_call.execute_directly(&mut state)
}

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

// Initialize block number -> block hash entry.
let upper_bound_block_number = CURRENT_BLOCK_NUMBER - constants::STORED_BLOCK_HASH_BUFFER;
Expand All @@ -211,7 +220,7 @@ fn test_get_block_hash() {
let entry_point_call = CallEntryPoint {
entry_point_selector: selector_from_name("test_get_block_hash"),
calldata,
..trivial_external_entry_point()
..trivial_external_entry_point_new(test_contract)
};

assert_eq!(
Expand All @@ -233,7 +242,7 @@ fn test_get_block_hash() {
let entry_point_call = CallEntryPoint {
entry_point_selector: selector_from_name("test_get_block_hash"),
calldata,
..trivial_external_entry_point()
..trivial_external_entry_point_new(test_contract)
};
let error = entry_point_call.execute_directly(&mut state).unwrap_err();
assert_matches!(error, EntryPointExecutionError::ExecutionFailed{ error_data }
Expand All @@ -242,13 +251,15 @@ fn test_get_block_hash() {

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

let calldata = Calldata(vec![].into());
let entry_point_call = CallEntryPoint {
entry_point_selector: selector_from_name("test_keccak"),
calldata,
..trivial_external_entry_point()
..trivial_external_entry_point_new(test_contract)
};

assert_eq!(
Expand Down Expand Up @@ -488,21 +499,24 @@ fn test_get_execution_info(

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

let inner_entry_point_selector = selector_from_name("test_storage_read_write");
let test_contract = FeatureContract::TestContract(CairoVersion::Cairo1);
let calldata = calldata![
stark_felt!(TEST_CLASS_HASH), // Class hash.
inner_entry_point_selector.0, // Function selector.
stark_felt!(2_u8), // Calldata length.
stark_felt!(1234_u16), // Calldata: address.
stark_felt!(91_u8) // Calldata: value.
test_contract.get_class_hash().0, // Class hash.
inner_entry_point_selector.0, // Function selector.
stark_felt!(2_u8), // Calldata length.
stark_felt!(1234_u16), // Calldata: address.
stark_felt!(91_u8) // Calldata: value.
];
let entry_point_call = CallEntryPoint {
entry_point_selector: selector_from_name("test_library_call"),
calldata,
class_hash: Some(class_hash!(TEST_CLASS_HASH)),
..trivial_external_entry_point()
class_hash: Some(test_contract.get_class_hash()),
..trivial_external_entry_point_new(test_contract)
};

assert_eq!(
Expand All @@ -517,20 +531,22 @@ fn test_library_call() {

#[test]
fn test_library_call_assert_fails() {
let mut state = create_test_state();
let test_contract = FeatureContract::TestContract(CairoVersion::Cairo1);
let chain_info = &ChainInfo::create_for_testing();
let mut state = test_state(chain_info, BALANCE, &[(test_contract, 1)]);
let inner_entry_point_selector = selector_from_name("assert_eq");
let calldata = calldata![
stark_felt!(TEST_CLASS_HASH), // Class hash.
inner_entry_point_selector.0, // Function selector.
stark_felt!(2_u8), // Calldata length.
stark_felt!(0_u8), // Calldata: first assert value.
stark_felt!(1_u8) // Calldata: second assert value.
test_contract.get_class_hash().0, // Class hash.
inner_entry_point_selector.0, // Function selector.
stark_felt!(2_u8), // Calldata length.
stark_felt!(0_u8), // Calldata: first assert value.
stark_felt!(1_u8) // Calldata: second assert value.
];
let entry_point_call = CallEntryPoint {
entry_point_selector: selector_from_name("test_library_call"),
calldata,
class_hash: Some(class_hash!(TEST_CLASS_HASH)),
..trivial_external_entry_point()
class_hash: Some(test_contract.get_class_hash()),
..trivial_external_entry_point_new(test_contract)
};

assert_matches!(
Expand All @@ -541,13 +557,16 @@ fn test_library_call_assert_fails() {

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

let (key, value) = (255_u64, 44_u64);
let outer_entry_point_selector = selector_from_name("test_library_call");
let inner_entry_point_selector = selector_from_name("test_storage_read_write");
let test_class_hash = test_contract.get_class_hash();
let main_entry_point_calldata = calldata![
stark_felt!(TEST_CLASS_HASH), // Class hash.
test_class_hash.0, // Class hash.
outer_entry_point_selector.0, // Library call function selector.
inner_entry_point_selector.0, // Storage function selector.
stark_felt!(key), // Calldata: address.
Expand All @@ -558,33 +577,33 @@ fn test_nested_library_call() {
let main_entry_point = CallEntryPoint {
entry_point_selector: selector_from_name("test_nested_library_call"),
calldata: main_entry_point_calldata,
class_hash: Some(class_hash!(TEST_CLASS_HASH)),
class_hash: Some(test_class_hash),
initial_gas: 9999906600,
..trivial_external_entry_point()
..trivial_external_entry_point_new(test_contract)
};
let nested_storage_entry_point = CallEntryPoint {
entry_point_selector: inner_entry_point_selector,
calldata: calldata![stark_felt!(key + 1), stark_felt!(value + 1)],
class_hash: Some(class_hash!(TEST_CLASS_HASH)),
class_hash: Some(test_class_hash),
code_address: None,
call_type: CallType::Delegate,
initial_gas: 9999720720,
..trivial_external_entry_point()
..trivial_external_entry_point_new(test_contract)
};
let library_entry_point = CallEntryPoint {
entry_point_selector: outer_entry_point_selector,
calldata: calldata![
stark_felt!(TEST_CLASS_HASH), // Class hash.
test_class_hash.0, // Class hash.
inner_entry_point_selector.0, // Storage function selector.
stark_felt!(2_u8), // Calldata: address.
stark_felt!(key + 1), // Calldata: address.
stark_felt!(value + 1) // Calldata: value.
],
class_hash: Some(class_hash!(TEST_CLASS_HASH)),
class_hash: Some(test_class_hash),
code_address: None,
call_type: CallType::Delegate,
initial_gas: 9999814150,
..trivial_external_entry_point()
..trivial_external_entry_point_new(test_contract)
};
let storage_entry_point = CallEntryPoint {
calldata: calldata![stark_felt!(key), stark_felt!(value)],
Expand Down Expand Up @@ -704,13 +723,15 @@ fn test_replace_class() {

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

let calldata = Calldata(vec![].into());
let entry_point_call = CallEntryPoint {
entry_point_selector: selector_from_name("test_secp256k1"),
calldata,
..trivial_external_entry_point()
..trivial_external_entry_point_new(test_contract)
};

assert_eq!(
Expand All @@ -721,13 +742,15 @@ fn test_secp256k1() {

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

let calldata = Calldata(vec![].into());
let entry_point_call = CallEntryPoint {
entry_point_selector: selector_from_name("test_secp256r1"),
calldata,
..trivial_external_entry_point()
..trivial_external_entry_point_new(test_contract)
};

assert_eq!(
Expand All @@ -738,7 +761,9 @@ fn test_secp256r1() {

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

let to_address = stark_felt!(1234_u16);
let payload = vec![stark_felt!(2019_u16), stark_felt!(2020_u16), stark_felt!(2021_u16)];
Expand All @@ -757,7 +782,7 @@ fn test_send_message_to_l1() {
let entry_point_call = CallEntryPoint {
entry_point_selector: selector_from_name("test_send_message_to_l1"),
calldata,
..trivial_external_entry_point()
..trivial_external_entry_point_new(test_contract)
};

let to_address = EthAddress::try_from(to_address).unwrap();
Expand Down Expand Up @@ -878,7 +903,9 @@ fn test_deploy(

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

let key = stark_felt!(1234_u16);
let value = stark_felt!(18_u8);
Expand All @@ -887,7 +914,7 @@ fn test_out_of_gas() {
calldata,
entry_point_selector: selector_from_name("test_storage_read_write"),
initial_gas: REQUIRED_GAS_STORAGE_READ_WRITE_TEST - 1,
..trivial_external_entry_point()
..trivial_external_entry_point_new(test_contract)
};
let error = entry_point_call.execute_directly(&mut state).unwrap_err();
assert_matches!(error, EntryPointExecutionError::ExecutionFailed{ error_data }
Expand Down
Loading

0 comments on commit 5fe26bd

Please sign in to comment.