Skip to content

Commit

Permalink
add upstream changes
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Sep 19, 2024
1 parent e7fb9f4 commit a7a9fa5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 54 deletions.
40 changes: 14 additions & 26 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 @@ -647,10 +629,9 @@ impl NativeContractClassV1 {
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>,
pub program: cairo_lang_sierra::program::Program, // for sierra emu
fallback_entry_points_by_type: SierraContractEntryPoints,
// Used for PartialEq
sierra_program_hash: starknet_api::hash::StarkHash,
}

impl std::fmt::Debug for NativeContractClassV1Inner {
Expand Down Expand Up @@ -687,19 +668,26 @@ impl NativeContractClassV1Inner {
&lookup_fid,
&sierra_contract_class.entry_points_by_type,
)?,
sierra_program_raw: sierra_contract_class.sierra_program,
program: sierra_program.clone(),
fallback_entry_points_by_type: sierra_contract_class.entry_points_by_type,
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
25 changes: 1 addition & 24 deletions crates/blockifier/src/execution/execution_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::env;

use cairo_lang_runner::casm_run::format_next_item;
use cairo_vm::serde::deserialize_program::{
Expand All @@ -23,9 +22,8 @@ use starknet_api::deprecated_contract_class::Program as DeprecatedProgram;
use starknet_api::transaction::Calldata;
use starknet_types_core::felt::Felt;

use super::contract_class::ContractClassV1;
use super::entry_point::ConstructorEntryPointExecutionResult;
use super::errors::{ConstructorEntryPointExecutionError, EntryPointExecutionError};
use super::errors::ConstructorEntryPointExecutionError;
use crate::execution::call_info::{CallInfo, Retdata};
use crate::execution::contract_class::ContractClass;
use crate::execution::entry_point::{
Expand Down Expand Up @@ -75,7 +73,6 @@ pub fn execute_entry_point_call(
ContractClass::V1Native(contract_class) => {
// Wrap the state into a DynStateWrapper to be transactional
let mut state_wrapped = DynStateWrapper::new(state);
let fallback = env::var("FALLBACK_ENABLED").unwrap_or(String::from("0")) == "1";

match native_entry_point_execution::execute_entry_point_call(
call.clone(),
Expand All @@ -90,26 +87,6 @@ pub fn execute_entry_point_call(

Ok(res)
}
Err(EntryPointExecutionError::NativeUnexpectedError { .. }) if fallback => {
// Fallback to VM execution in case of an Error
let casm_contract_class =
contract_class.to_casm_contract_class().map_err(|e| {
EntryPointExecutionError::FailedToConvertSierraToCasm(e.to_string())
})?;
let contract_class_v1: ContractClassV1 =
casm_contract_class.try_into().unwrap();
// Use old state if native execution failed
entry_point_execution::execute_entry_point_call(
call,
contract_class_v1,
state,
resources,
context,
)
.map_err(|e| {
EntryPointExecutionError::NativeFallbackError { info: Box::new(e) }
})
}
Err(e) => Err(e),
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/blockifier/src/execution/native/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ fn create_callinfo(
syscall_handler: NativeSyscallHandler<'_>,
) -> Result<CallInfo, EntryPointExecutionError> {
let gas_consumed = {
let low = run_result.remaining_gas as u64;
let high = (run_result.remaining_gas >> 64) as u64;
let low: u64 = run_result.remaining_gas.try_into().unwrap();
let high: u64 = (run_result.remaining_gas >> 64).try_into().unwrap();
if high != 0 {
return Err(EntryPointExecutionError::NativeExecutionError {
info: "Overflow: gas consumed bigger than 64 bit".into(),
Expand Down Expand Up @@ -169,8 +169,8 @@ pub fn create_callinfo_emu(
accessed_storage_keys: HashSet<StorageKey, RandomState>,
) -> Result<CallInfo, EntryPointExecutionError> {
let gas_consumed = {
let low = run_result.remaining_gas as u64;
let high = (run_result.remaining_gas >> 64) as u64;
let low: u64 = run_result.remaining_gas.try_into().unwrap();
let high: u64 = (run_result.remaining_gas >> 64).try_into().unwrap();
if high != 0 {
return Err(EntryPointExecutionError::NativeExecutionError {
info: "Overflow: gas consumed bigger than 64 bit".into(),
Expand Down

0 comments on commit a7a9fa5

Please sign in to comment.