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

Sierra emu #3

Merged
merged 3 commits into from
Sep 20, 2024
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
30 changes: 30 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ byteorder = "1.4.3"
bytes = "1"
cached = "0.44.0"
cairo-felt = "0.9.1"
cairo-lang-casm = "2.8.0"
cairo-lang-runner = "2.8.0"
cairo-lang-sierra = "2.8.0"
cairo-lang-sierra-to-casm = "2.7.1"
cairo-lang-starknet-classes = "2.8.0"
cairo-lang-utils = "2.7.1"
cairo-lang-casm = "2.8.2"
cairo-lang-runner = "2.8.2"
cairo-lang-sierra = "2.8.2"
cairo-lang-sierra-to-casm = "2.8.2"
cairo-lang-starknet-classes = "2.8.2"
cairo-lang-utils = "2.8.2"
# This is a temporary dependency, will be removed once the new version of cairo-native is released to main.
cairo-native = { git = "https://github.com/lambdaclass/cairo_native" }
sierra-emu = { git = "https://github.com/lambdaclass/sierra-emu.git" }
cairo-vm = "1.0.1"
camelpaste = "0.1.0"
chrono = "0.4.26"
Expand Down
2 changes: 2 additions & 0 deletions crates/blockifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ workspace = true
concurrency = []
jemalloc = ["dep:tikv-jemallocator"]
testing = ["rand", "rstest"]
use-sierra-emu = []


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -30,6 +31,7 @@ cairo-lang-sierra.workspace = true
cairo-lang-starknet-classes.workspace = true
cairo-lang-utils.workspace = true
cairo-native.workspace = true
sierra-emu.workspace = true
cairo-vm.workspace = true
derive_more.workspace = true
indexmap.workspace = true
Expand Down
4 changes: 3 additions & 1 deletion crates/blockifier/bench/blockifier_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
//! Run the benchmarks using `cargo bench --bench blockifier_bench`.

use blockifier::test_utils::transfers_generator::{
RecipientGeneratorType, TransfersGenerator, TransfersGeneratorConfig,
RecipientGeneratorType,
TransfersGenerator,
TransfersGeneratorConfig,
};
use criterion::{criterion_group, criterion_main, Criterion};

Expand Down
7 changes: 5 additions & 2 deletions crates/blockifier/src/bouncer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ use serde::{Deserialize, Serialize};
use starknet_api::core::ClassHash;

use crate::blockifier::transaction_executor::{
TransactionExecutorError, TransactionExecutorResult,
TransactionExecutorError,
TransactionExecutorResult,
};
use crate::execution::call_info::ExecutionSummary;
use crate::fee::gas_usage::get_onchain_data_segment_length;
use crate::state::cached_state::{StateChangesKeys, StorageEntry};
use crate::state::state_api::StateReader;
use crate::transaction::errors::TransactionExecutionError;
use crate::transaction::objects::{
ExecutionResourcesTraits, TransactionExecutionResult, TransactionResources,
ExecutionResourcesTraits,
TransactionExecutionResult,
TransactionResources,
};

#[cfg(test)]
Expand Down
3 changes: 2 additions & 1 deletion crates/blockifier/src/bouncer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use starknet_api::{class_hash, contract_address, felt, patricia_key};

use super::BouncerConfig;
use crate::blockifier::transaction_executor::{
TransactionExecutorError, TransactionExecutorResult,
TransactionExecutorError,
TransactionExecutorResult,
};
use crate::bouncer::{verify_tx_weights_in_bounds, Bouncer, BouncerWeights, BuiltinCount};
use crate::context::BlockContext;
Expand Down
9 changes: 6 additions & 3 deletions crates/blockifier/src/execution/common_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use std::ops::Shl;
use std::rc::Rc;

use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::{
BuiltinHintProcessor, HintFunc,
BuiltinHintProcessor,
HintFunc,
};
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{
get_integer_from_var_name, insert_value_from_var_name,
get_integer_from_var_name,
insert_value_from_var_name,
};
use cairo_vm::hint_processor::hint_processor_definition::HintReference;
use cairo_vm::serde::deserialize_program::ApTracking;
Expand All @@ -18,7 +20,8 @@ use num_bigint::BigUint;
use starknet_types_core::felt::Felt;

use crate::execution::hint_code::{
NORMALIZE_ADDRESS_SET_IS_250_HINT, NORMALIZE_ADDRESS_SET_IS_SMALL_HINT,
NORMALIZE_ADDRESS_SET_IS_250_HINT,
NORMALIZE_ADDRESS_SET_IS_SMALL_HINT,
};

/// Transaction execution mode.
Expand Down
49 changes: 22 additions & 27 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ use std::sync::Arc;
use cairo_lang_casm;
use cairo_lang_casm::hints::Hint;
use cairo_lang_sierra::ids::FunctionId;
use cairo_lang_starknet_classes::casm_contract_class::{
CasmContractClass,
CasmContractEntryPoint,
StarknetSierraCompilationError,
};
use cairo_lang_starknet_classes::casm_contract_class::{CasmContractClass, CasmContractEntryPoint};
use cairo_lang_starknet_classes::contract_class::{
ContractClass as SierraContractClass,
ContractEntryPoint,
Expand Down Expand Up @@ -41,6 +37,7 @@ use starknet_api::deprecated_contract_class::{
Program as DeprecatedProgram,
};
use starknet_types_core::felt::Felt;
use starknet_types_core::hash::{Poseidon, StarkHash};

use super::entry_point::EntryPointExecutionResult;
use super::errors::EntryPointExecutionError;
Expand Down Expand Up @@ -611,21 +608,6 @@ impl NativeContractClassV1 {
Ok(Self(Arc::new(contract)))
}

pub fn to_casm_contract_class(
self,
) -> Result<CasmContractClass, StarknetSierraCompilationError> {
let sierra_contract_class = SierraContractClass {
// Cloning because these are behind an Arc.
sierra_program: self.sierra_program_raw.clone(),
entry_points_by_type: self.fallback_entry_points_by_type.clone(),
abi: None,
sierra_program_debug_info: None,
contract_class_version: String::default(),
};

CasmContractClass::from_contract_class(sierra_contract_class, false, usize::MAX)
}

/// Returns an entry point into the natively compiled contract.
pub fn get_entrypoint(
&self,
Expand All @@ -644,13 +626,18 @@ impl NativeContractClassV1 {
}
}

#[derive(Debug)]
pub struct NativeContractClassV1Inner {
pub executor: Arc<AotNativeExecutor>,
entry_points_by_type: NativeContractEntryPoints,
// Storing the raw sierra program and entry points to be able to fallback to the vm
sierra_program_raw: Vec<BigUintAsHex>,
fallback_entry_points_by_type: SierraContractEntryPoints,
pub program: cairo_lang_sierra::program::Program, // for sierra emu
// Used for PartialEq
sierra_program_hash: starknet_api::hash::StarkHash,
}

impl std::fmt::Debug for NativeContractClassV1Inner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "NativeContractClassV1Inner")
}
}

impl NativeContractClassV1Inner {
Expand Down Expand Up @@ -681,18 +668,26 @@ impl NativeContractClassV1Inner {
&lookup_fid,
&sierra_contract_class.entry_points_by_type,
)?,
sierra_program_raw: sierra_contract_class.sierra_program,
fallback_entry_points_by_type: sierra_contract_class.entry_points_by_type,
program: sierra_program.clone(),
sierra_program_hash: calculate_sierra_program_hash(
sierra_contract_class.sierra_program,
),
})
}
}

fn calculate_sierra_program_hash(sierra: Vec<BigUintAsHex>) -> starknet_api::hash::StarkHash {
let sierra_felts: Vec<Felt> =
sierra.iter().map(|big_uint| &big_uint.value).map_into().collect();
Poseidon::hash_array(&sierra_felts)
}

// The location where the compiled contract is loaded into memory will not
// be the same therefore we exclude it from the comparison.
impl PartialEq for NativeContractClassV1Inner {
fn eq(&self, other: &Self) -> bool {
self.entry_points_by_type == other.entry_points_by_type
&& self.sierra_program_raw == other.sierra_program_raw
&& self.sierra_program_hash == other.sierra_program_hash
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use crate::execution::call_info::{CallExecution, CallInfo};
use crate::execution::contract_class::ContractClassV0;
use crate::execution::deprecated_syscalls::hint_processor::DeprecatedSyscallHintProcessor;
use crate::execution::entry_point::{
CallEntryPoint, EntryPointExecutionContext, EntryPointExecutionResult,
CallEntryPoint,
EntryPointExecutionContext,
EntryPointExecutionResult,
};
use crate::execution::errors::{PostExecutionError, PreExecutionError};
use crate::execution::execution_utils::{read_execution_retdata, Args, ReadOnlySegments};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ use rstest::rstest;
use starknet_api::core::{calculate_contract_address, ChainId, PatriciaKey};
use starknet_api::state::StorageKey;
use starknet_api::transaction::{
Calldata, ContractAddressSalt, EventContent, EventData, EventKey, Fee, TransactionHash,
Calldata,
ContractAddressSalt,
EventContent,
EventData,
EventKey,
Fee,
TransactionHash,
TransactionVersion,
};
use starknet_api::{calldata, felt};
Expand All @@ -27,13 +33,22 @@ use crate::state::state_api::StateReader;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{
calldata_for_deploy_test, get_syscall_resources, trivial_external_entry_point_new,
CairoVersion, CHAIN_ID_NAME, CURRENT_BLOCK_NUMBER, CURRENT_BLOCK_NUMBER_FOR_VALIDATE,
CURRENT_BLOCK_TIMESTAMP, CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE, TEST_SEQUENCER_ADDRESS,
calldata_for_deploy_test,
get_syscall_resources,
trivial_external_entry_point_new,
CairoVersion,
CHAIN_ID_NAME,
CURRENT_BLOCK_NUMBER,
CURRENT_BLOCK_NUMBER_FOR_VALIDATE,
CURRENT_BLOCK_TIMESTAMP,
CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE,
TEST_SEQUENCER_ADDRESS,
};
use crate::transaction::constants::QUERY_VERSION_BASE_BIT;
use crate::transaction::objects::{
CommonAccountFields, DeprecatedTransactionInfo, TransactionInfo,
CommonAccountFields,
DeprecatedTransactionInfo,
TransactionInfo,
};
use crate::versioned_constants::VersionedConstants;
use crate::{check_entry_point_execution_error_for_custom_hint, nonce, retdata, storage_key};
Expand Down
29 changes: 24 additions & 5 deletions crates/blockifier/src/execution/deprecated_syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,45 @@ use cairo_vm::vm::vm_core::VirtualMachine;
use serde::Deserialize;
use starknet_api::block::{BlockNumber, BlockTimestamp};
use starknet_api::core::{
calculate_contract_address, ClassHash, ContractAddress, EntryPointSelector, EthAddress,
calculate_contract_address,
ClassHash,
ContractAddress,
EntryPointSelector,
EthAddress,
};
use starknet_api::deprecated_contract_class::EntryPointType;
use starknet_api::state::StorageKey;
use starknet_api::transaction::{
Calldata, ContractAddressSalt, EventContent, EventData, EventKey, L2ToL1Payload,
Calldata,
ContractAddressSalt,
EventContent,
EventData,
EventKey,
L2ToL1Payload,
};
use starknet_types_core::felt::Felt;
use strum_macros::EnumIter;

use self::hint_processor::{
execute_inner_call, execute_library_call, felt_to_bool, read_call_params, read_calldata,
read_felt_array, DeprecatedSyscallExecutionError, DeprecatedSyscallHintProcessor,
execute_inner_call,
execute_library_call,
felt_to_bool,
read_call_params,
read_calldata,
read_felt_array,
DeprecatedSyscallExecutionError,
DeprecatedSyscallHintProcessor,
};
use super::syscalls::exceeds_event_size_limit;
use crate::execution::call_info::{MessageToL1, OrderedEvent, OrderedL2ToL1Message};
use crate::execution::common_hints::ExecutionMode;
use crate::execution::entry_point::{CallEntryPoint, CallType, ConstructorContext};
use crate::execution::execution_utils::{
execute_deployment, felt_from_ptr, write_felt, write_maybe_relocatable, ReadOnlySegment,
execute_deployment,
felt_from_ptr,
write_felt,
write_maybe_relocatable,
ReadOnlySegment,
};

#[cfg(test)]
Expand Down
Loading
Loading