Skip to content

Commit

Permalink
Remove VM Fallback
Browse files Browse the repository at this point in the history
NativeContractClassV1Inner contained full Sierra contract in order to
run the VM code in the event of a NativeUnexpectedError.
Now, this is no longer needed.

Issue: #10
PR: #13
Testing: crates/blockifier/tests/test_native_contract.rs
  • Loading branch information
PearsonWhite committed Sep 10, 2024
1 parent 7b578f3 commit 736aa01
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 47 deletions.
40 changes: 18 additions & 22 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use cairo_lang_sierra::ids::FunctionId;
use cairo_lang_starknet_classes::casm_contract_class::{
CasmContractClass,
CasmContractEntryPoint,
StarknetSierraCompilationError,
};
use cairo_lang_starknet_classes::contract_class::{
ContractClass as SierraContractClass,
Expand All @@ -32,6 +31,8 @@ use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use itertools::Itertools;
use serde::de::Error as DeserializationError;
use serde::{Deserialize, Deserializer};
use starknet_api::block::BlockHash;
use starknet_api::crypto::utils::HashChain;
use starknet_api::core::EntryPointSelector;
use starknet_api::deprecated_contract_class::{
ContractClass as DeprecatedContractClass,
Expand Down Expand Up @@ -611,21 +612,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 @@ -648,9 +634,8 @@ impl NativeContractClassV1 {
pub struct NativeContractClassV1Inner {
pub executor: 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,
// Used for PartialEq
sierra_program_hash : BlockHash,
}

impl NativeContractClassV1Inner {
Expand Down Expand Up @@ -681,18 +666,29 @@ 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,
// sierra_program_hash : calculate_block_hash(sierra_contract_class.sierra_program)
// todo: add hash of progam here for PoC
sierra_program_hash : calculate_sierra_program_hash(sierra_contract_class.sierra_program),
// sierra_program_raw: sierra_contract_class.sierra_program,
// fallback_entry_points_by_type: sierra_contract_class.entry_points_by_type,
})
}
}

fn calculate_sierra_program_hash(sierra: Vec<BigUintAsHex>) -> BlockHash {
let sierra_felts : Vec<Felt> = sierra.iter().map(|wrapper| &wrapper.value).map_into().collect();
BlockHash(
HashChain::new()
.chain_size_and_elements(&sierra_felts)
.get_poseidon_hash())
}

// 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 @@ -19,9 +18,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 @@ -68,7 +66,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 @@ -83,26 +80,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
2 changes: 1 addition & 1 deletion crates/starknet_api/src/crypto/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn verify_message_hash_signature(
}

// Collect elements for applying hash chain.
pub(crate) struct HashChain {
pub struct HashChain {
elements: Vec<Felt>,
}

Expand Down

0 comments on commit 736aa01

Please sign in to comment.