Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(blockifier): add builtins_test #3381

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
2,957 changes: 1,977 additions & 980 deletions crates/blockifier/feature_contracts/cairo1/compiled/test_contract.casm.json

Large diffs are not rendered by default.

16,349 changes: 8,293 additions & 8,056 deletions crates/blockifier/feature_contracts/cairo1/sierra/test_contract.sierra.json

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions crates/blockifier/feature_contracts/cairo1/test_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ mod TestContract {
EvalCircuitResult, EvalCircuitTrait, u384, CircuitOutputsTrait, CircuitModulus,
CircuitInputs, AddInputResultTrait
};
use core::hash::HashStateTrait;
use core::pedersen::PedersenTrait;
use core::poseidon::PoseidonTrait;

#[storage]
struct Storage {
Expand Down Expand Up @@ -735,4 +738,37 @@ mod TestContract {
#[external(v0)]
fn empty_function(ref self: ContractState) {
}

#[external(v0)]
fn test_bitwise(ref self: ContractState) {
let x: u32 = 0x1;
let y: u32 = 0x2;
let _z = x & y;
}

#[external(v0)]
fn test_pedersen (ref self: ContractState) {
let mut state = PedersenTrait::new(0);
state = state.update(1);
let _hash = state.finalize();
}

#[external(v0)]
fn test_poseidon (ref self: ContractState) {
let mut state = PoseidonTrait::new();
state = state.update(1);
let _hash = state.finalize();
}

#[external(v0)]
fn test_ecop (ref self: ContractState) {
let m: felt252 = 2;
let a: felt252 = 336742005567258698661916498343089167447076063081786685068305785816009957563;
let b: felt252 = 1706004133033694959518200210163451614294041810778629639790706933324248611779;
let p : ec::NonZeroEcPoint = (ec::ec_point_try_new_nz(a, b)).unwrap();
let mut s: ec::EcState = ec::ec_state_init();
ec::ec_state_add_mul(ref s, m, p);
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::sync::Arc;

use rstest::rstest;
use rstest_reuse::apply;
use starknet_api::abi::abi_utils::selector_from_name;
use starknet_api::calldata;

use crate::context::{BlockContext, ChainInfo};
use crate::execution::entry_point::CallEntryPoint;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::test_templates::runnable_version;
use crate::test_utils::{trivial_external_entry_point_new, CairoVersion, RunnableCairo1, BALANCE};
use crate::versioned_constants::BuiltinGasCosts;

const TESTED_BUILTIN_GAS_COST: u64 = u64::pow(10, 7);

#[apply(runnable_version)]
#[case::pedersen("test_pedersen")]
#[case::bitwise("test_bitwise")]
#[case::ecop("test_ecop")]
#[case::poseidon("test_poseidon")]
// This test case tests the add_mod and mul_mod builtins.
#[case::add_and_mul_mod("test_circuit")]
fn builtins_test(runnable_version: RunnableCairo1, #[case] selector_name: &str) {
let test_contract = FeatureContract::TestContract(CairoVersion::Cairo1(runnable_version));
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 {
entry_point_selector: selector_from_name(selector_name),
calldata,
..trivial_external_entry_point_new(test_contract)
};

let mut block_context = BlockContext::create_for_account_testing();
assert!(
block_context.versioned_constants.os_constants.execute_max_sierra_gas.0
> TESTED_BUILTIN_GAS_COST
);
change_builtins_gas_cost(&mut block_context, selector_name);
let mut minimal_gas = TESTED_BUILTIN_GAS_COST;
if selector_name == "test_circuit" {
minimal_gas *= 2;
}

let call_info =
entry_point_call.execute_directly_given_block_context(&mut state, block_context).unwrap();

assert!(!call_info.execution.failed, "Execution failed");
assert!(call_info.execution.gas_consumed >= minimal_gas);
}

fn change_builtins_gas_cost(block_context: &mut BlockContext, selector_name: &str) {
let os_constants = Arc::make_mut(&mut block_context.versioned_constants.os_constants);
os_constants.gas_costs.builtins = BuiltinGasCosts::default();
match selector_name {
"test_pedersen" => {
os_constants.gas_costs.builtins.pedersen = TESTED_BUILTIN_GAS_COST;
}
"test_bitwise" => {
os_constants.gas_costs.builtins.bitwise = TESTED_BUILTIN_GAS_COST;
}
"test_ecop" => {
os_constants.gas_costs.builtins.ecop = TESTED_BUILTIN_GAS_COST;
}
"test_poseidon" => {
os_constants.gas_costs.builtins.poseidon = TESTED_BUILTIN_GAS_COST;
}
"test_circuit" => {
os_constants.gas_costs.builtins.add_mod = TESTED_BUILTIN_GAS_COST;
os_constants.gas_costs.builtins.mul_mod = TESTED_BUILTIN_GAS_COST;
}
_ => panic!("Unknown selector name: {}", selector_name),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ use crate::transaction::objects::{
ExecutionMode::Execute,
TransactionVersion::ONE,
false;
"Native: Legacy contract. Execute execution mode: block info should be as usual. Transaction
V1."
"Native: Legacy contract. Execute execution mode: block info should be as usual. Transaction V1."
)
)]
#[cfg_attr(
Expand All @@ -101,8 +100,7 @@ use crate::transaction::objects::{
ExecutionMode::Execute,
TransactionVersion::THREE,
false;
"Native: Legacy contract. Execute execution mode: block info should be as usual. Transaction
V3."
"Native: Legacy contract. Execute execution mode: block info should be as usual. Transaction V3."
)
)]
#[cfg_attr(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod builtins_test;
mod call_contract;
mod constants;
mod deploy;
Expand Down
22 changes: 22 additions & 0 deletions crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ impl CallEntryPoint {
)
}

pub fn execute_directly_given_block_context(
self,
state: &mut dyn State,
block_context: BlockContext,
) -> EntryPointExecutionResult<CallInfo> {
// Do not limit steps by resources as we use default resources.
let limit_steps_by_resources = false;
let tx_context = TransactionContext {
block_context,
tx_info: TransactionInfo::Current(CurrentTransactionInfo::create_for_testing()),
};

let mut context = EntryPointExecutionContext::new(
Arc::new(tx_context),
ExecutionMode::Execute,
limit_steps_by_resources,
SierraGasRevertTracker::new(GasAmount(self.initial_gas)),
);
let mut remaining_gas = self.initial_gas;
self.execute(state, &mut context, &mut remaining_gas)
}

pub fn execute_directly_given_tx_info(
self,
state: &mut dyn State,
Expand Down
14 changes: 14 additions & 0 deletions crates/blockifier/src/test_utils/test_templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ fn cairo_version(
) {
}

#[cfg(test)]
#[cfg(not(feature = "cairo_native"))]
#[template]
#[rstest]
fn runnable_version(#[values(RunnableCairo1::Casm)] runnable_version: RunnableCairo1) {}

#[cfg(feature = "cairo_native")]
#[template]
#[rstest]
fn runnable_version(
#[values(RunnableCairo1::Casm, RunnableCairo1::Native)] runnable_version: RunnableCairo1,
) {
}

#[cfg(not(feature = "cairo_native"))]
#[template]
#[rstest]
Expand Down
Loading