Skip to content

Commit

Permalink
chore: add Native as a compilation feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-pino committed Oct 31, 2024
1 parent 0c92a0e commit 586415b
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 19 deletions.
6 changes: 6 additions & 0 deletions crates/blockifier/src/execution/stack_trace_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ Error in contract (contract address: {test_contract_address_2_felt:#064x}, class
let expected_trace = match cairo_version {
CairoVersion::Cairo0 => expected_trace_cairo0,
CairoVersion::Cairo1 => expected_trace_cairo1,
#[cfg(feature = "cairo_native")]
CairoVersion::Native => panic!("Cairo Native contracts are not supported"),
};

Expand Down Expand Up @@ -366,6 +367,7 @@ Error in contract (contract address: {contract_address_felt:#064x}, class hash:
"
)
}
#[cfg(feature = "cairo_native")]
CairoVersion::Native => {
todo!("Cairo Native is not yet supported here")
}
Expand Down Expand Up @@ -524,6 +526,7 @@ Error in contract (contract address: {address_felt:#064x}, class hash: {test_con
"
)
}
#[cfg(feature = "cairo_native")]
CairoVersion::Native => {
todo!("Cairo Native not yet supported here.")
}
Expand Down Expand Up @@ -627,6 +630,7 @@ Error in contract (contract address: {contract_address:#064x}, class hash: {:#06
0x496e76616c6964207363656e6172696f ('Invalid scenario').",
class_hash.0
),
#[cfg(feature = "cairo_native")]
CairoVersion::Native => todo!("Cairo Native is not yet supported here."),
};

Expand Down Expand Up @@ -700,6 +704,7 @@ Error in contract (contract address: {expected_address:#064x}, class hash: {:#06
class_hash.0
)
.to_string(),
#[cfg(feature = "cairo_native")]
CairoVersion::Native => {
todo!("Cairo Native not yet supported here.")
}
Expand Down Expand Up @@ -840,6 +845,7 @@ Error in contract (contract address: {expected_address:#064x}, class hash: {:#06
ctor_selector.0
)
}
#[cfg(feature = "cairo_native")]
CairoVersion::Native => {
todo!("Cairo Native not yet supported here.")
}
Expand Down
18 changes: 17 additions & 1 deletion crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub const ERC20_CONTRACT_PATH: &str = "./ERC20/ERC20_Cairo0/ERC20_without_some_s
pub enum CairoVersion {
Cairo0,
Cairo1,
#[cfg(feature = "cairo_native")]
Native,
}

Expand Down Expand Up @@ -91,6 +92,7 @@ impl CairoVersion {
match self {
Self::Cairo0 => Self::Cairo1,
Self::Cairo1 => Self::Cairo0,
#[cfg(feature = "cairo_native")]
Self::Native => todo!("who should be your other?"),
}
}
Expand Down Expand Up @@ -118,6 +120,8 @@ impl CompilerBasedVersion {
TrackedResource::CairoSteps
}
Self::CairoVersion(CairoVersion::Cairo1) => TrackedResource::SierraGas,
#[cfg(feature = "cairo_native")]
Self::CairoVersion(CairoVersion::Native) => TrackedResource::SierraGas,
}
}
}
Expand Down Expand Up @@ -328,7 +332,19 @@ macro_rules! check_tx_execution_error_for_invalid_scenario {
$validate_constructor,
);
}
CairoVersion::Cairo1 | CairoVersion::Native => {
CairoVersion::Cairo1 => {
if let $crate::transaction::errors::TransactionExecutionError::ValidateTransactionError {
error, ..
} = $error {
assert_eq!(
error.to_string(),
"Execution failed. Failure reason: 0x496e76616c6964207363656e6172696f \
('Invalid scenario')."
)
}
}
#[cfg(feature = "cairo_native")]
CairoVersion::Native => {
if let $crate::transaction::errors::TransactionExecutionError::ValidateTransactionError {
error, ..
} = $error {
Expand Down
35 changes: 23 additions & 12 deletions crates/blockifier/src/test_utils/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ use strum_macros::EnumIter;

use crate::abi::abi_utils::selector_from_name;
use crate::abi::constants::CONSTRUCTOR_ENTRY_POINT_NAME;
use crate::execution::contract_class::{
ContractClass,
ContractClassV0,
ContractClassV1,
NativeContractClassV1,
};
use crate::execution::contract_class::{ContractClass, ContractClassV0, ContractClassV1};
use crate::execution::entry_point::CallEntryPoint;
use crate::test_utils::cairo_compile::{cairo0_compile, cairo1_compile, starknet_compile};
#[cfg(feature = "cairo_native")]
use crate::execution::native::contract_class::NativeContractClassV1;
#[cfg(feature = "cairo_native")]
use crate::test_utils::cairo_compile::starknet_compile;
use crate::test_utils::cairo_compile::{cairo0_compile, cairo1_compile};
use crate::test_utils::{get_raw_contract_class, CairoVersion};

// This file contains featured contracts, used for tests. Use the function 'test_state' in
Expand Down Expand Up @@ -149,9 +148,9 @@ impl FeatureContract {
pub fn get_compiled_class_hash(&self) -> CompiledClassHash {
match self.cairo_version() {
CairoVersion::Cairo0 => CompiledClassHash(Felt::ZERO),
CairoVersion::Cairo1 | CairoVersion::Native => {
CompiledClassHash(felt!(self.get_integer_base()))
}
CairoVersion::Cairo1 => CompiledClassHash(felt!(self.get_integer_base())),
#[cfg(feature = "cairo_native")]
CairoVersion::Native => CompiledClassHash(felt!(self.get_integer_base())),
}
}

Expand All @@ -165,6 +164,7 @@ impl FeatureContract {
match self.cairo_version() {
CairoVersion::Cairo0 => ContractClassV0::from_file(&self.get_compiled_path()).into(),
CairoVersion::Cairo1 => ContractClassV1::from_file(&self.get_compiled_path()).into(),
#[cfg(feature = "cairo_native")]
CairoVersion::Native => {
NativeContractClassV1::from_file(&self.get_compiled_path()).into()
}
Expand Down Expand Up @@ -193,7 +193,9 @@ impl FeatureContract {
fn get_cairo_version_bit(&self) -> u32 {
match self.cairo_version() {
CairoVersion::Cairo0 => 0,
CairoVersion::Cairo1 | CairoVersion::Native => CAIRO1_BIT,
CairoVersion::Cairo1 => CAIRO1_BIT,
#[cfg(feature = "cairo_native")]
CairoVersion::Native => CAIRO1_BIT,
}
}

Expand Down Expand Up @@ -250,6 +252,7 @@ impl FeatureContract {
match cairo_version {
CairoVersion::Cairo0 => ERC20_CAIRO0_CONTRACT_SOURCE_PATH,
CairoVersion::Cairo1 => ERC20_CAIRO1_CONTRACT_SOURCE_PATH,
#[cfg(feature = "cairo_native")]
CairoVersion::Native => todo!("ERC20 cannot be tested with Native"),
}
.into()
Expand All @@ -259,6 +262,7 @@ impl FeatureContract {
match self.cairo_version() {
CairoVersion::Cairo0 => "0",
CairoVersion::Cairo1 => "1",
#[cfg(feature = "cairo_native")]
CairoVersion::Native => "_native",
},
self.get_non_erc20_base_name()
Expand All @@ -272,6 +276,7 @@ impl FeatureContract {
match cairo_version {
CairoVersion::Cairo0 => ERC20_CAIRO0_CONTRACT_PATH,
CairoVersion::Cairo1 => ERC20_CAIRO1_CONTRACT_PATH,
#[cfg(feature = "cairo_native")]
CairoVersion::Native => todo!("ERC20 cannot be tested with Native"),
}
.into()
Expand All @@ -282,12 +287,14 @@ impl FeatureContract {
match cairo_version {
CairoVersion::Cairo0 => "0",
CairoVersion::Cairo1 => "1",
#[cfg(feature = "cairo_native")]
CairoVersion::Native => "_native",
},
self.get_non_erc20_base_name(),
match cairo_version {
CairoVersion::Cairo0 => "_compiled",
CairoVersion::Cairo1 => ".casm",
#[cfg(feature = "cairo_native")]
CairoVersion::Native => ".sierra",
}
)
Expand Down Expand Up @@ -320,7 +327,11 @@ impl FeatureContract {
let (tag_override, cargo_nightly_arg) = self.fixed_tag_and_rust_toolchain();
cairo1_compile(self.get_source_path(), tag_override, cargo_nightly_arg)
}
CairoVersion::Native => starknet_compile(self.get_source_path(), None, None),
#[cfg(feature = "cairo_native")]
CairoVersion::Native => {
let (tag_override, cargo_nightly_arg) = self.fixed_tag_and_rust_toolchain();
starknet_compile(self.get_source_path(), tag_override, cargo_nightly_arg)
}
}
}

Expand Down
11 changes: 9 additions & 2 deletions crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

#[cfg(feature = "cairo_native")]
use cairo_native::executor::AotNativeExecutor;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use serde_json::Value;
Expand All @@ -16,12 +17,14 @@ use crate::bouncer::{BouncerConfig, BouncerWeights, BuiltinCount};
use crate::context::{BlockContext, ChainInfo, FeeTokenAddresses, TransactionContext};
use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
use crate::execution::common_hints::ExecutionMode;
use crate::execution::contract_class::{ContractClassV0, ContractClassV1, NativeContractClassV1};
use crate::execution::contract_class::{ContractClassV0, ContractClassV1};
use crate::execution::entry_point::{
CallEntryPoint,
EntryPointExecutionContext,
EntryPointExecutionResult,
};
#[cfg(feature = "cairo_native")]
use crate::execution::native::contract_class::NativeContractClassV1;
use crate::state::state_api::State;
use crate::test_utils::{
get_raw_contract_class,
Expand Down Expand Up @@ -250,6 +253,7 @@ impl BouncerWeights {
}
}

#[cfg(feature = "cairo_native")]
impl NativeContractClassV1 {
/// Convenience function to construct a NativeContractClassV1 from a raw contract class.
/// If control over the compilation is desired use [Self::new] instead.
Expand All @@ -275,7 +279,10 @@ impl NativeContractClassV1 {
let sierra_program = sierra_contract_class.extract_sierra_program()?;
let executor = compile_and_load(&sierra_program)?;

Ok(Self::new(executor, sierra_contract_class))
let casm_contract_class = cairo_lang_starknet_classes::casm_contract_class::CasmContractClass::from_contract_class(sierra_contract_class.clone(), false, usize::MAX)?;
let casm = ContractClassV1::try_from(casm_contract_class)?;

Ok(Self::new(executor, sierra_contract_class, casm))
}

pub fn from_file(contract_path: &str) -> Self {
Expand Down
10 changes: 8 additions & 2 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,20 @@ fn expected_validate_call_info(
) -> Option<CallInfo> {
let retdata = match cairo_version {
CairoVersion::Cairo0 => Retdata::default(),
CairoVersion::Cairo1 | CairoVersion::Native => retdata!(felt!(constants::VALIDATE_RETDATA)),
CairoVersion::Cairo1 => retdata!(felt!(constants::VALIDATE_RETDATA)),
#[cfg(feature = "cairo_native")]
CairoVersion::Native => retdata!(felt!(constants::VALIDATE_RETDATA)),
};
// Extra range check in regular (invoke) validate call, due to passing the calldata as an array.
let n_range_checks = match cairo_version {
CairoVersion::Cairo0 => {
usize::from(entry_point_selector_name == constants::VALIDATE_ENTRY_POINT_NAME)
}
CairoVersion::Cairo1 | CairoVersion::Native => {
CairoVersion::Cairo1 => {
if entry_point_selector_name == constants::VALIDATE_ENTRY_POINT_NAME { 7 } else { 2 }
}
#[cfg(feature = "cairo_native")]
CairoVersion::Native => {
if entry_point_selector_name == constants::VALIDATE_ENTRY_POINT_NAME { 7 } else { 2 }
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rstest::rstest;

const CAIRO0_FEATURE_CONTRACTS_DIR: &str = "feature_contracts/cairo0";
const CAIRO1_FEATURE_CONTRACTS_DIR: &str = "feature_contracts/cairo1";
#[cfg(feature = "cairo_native")]
const NATIVE_FEATURE_CONTRACTS_DIR: &str = "feature_contracts/cairo_native";
const COMPILED_CONTRACTS_SUBDIR: &str = "compiled";
const FIX_COMMAND: &str = "FIX_FEATURE_TEST=1 cargo test -p blockifier --test \
Expand Down Expand Up @@ -65,11 +66,13 @@ fn verify_and_get_files(cairo_version: CairoVersion) -> Vec<(String, String, Str
let directory = match cairo_version {
CairoVersion::Cairo0 => CAIRO0_FEATURE_CONTRACTS_DIR,
CairoVersion::Cairo1 => CAIRO1_FEATURE_CONTRACTS_DIR,
#[cfg(feature = "cairo_native")]
CairoVersion::Native => NATIVE_FEATURE_CONTRACTS_DIR,
};
let compiled_extension = match cairo_version {
CairoVersion::Cairo0 => "_compiled.json",
CairoVersion::Cairo1 => ".casm.json",
#[cfg(feature = "cairo_native")]
CairoVersion::Native => ".sierra.json",
};
for file in fs::read_dir(directory).unwrap() {
Expand Down Expand Up @@ -120,6 +123,7 @@ fn verify_feature_contracts_match_enum() {
assert_eq!(compiled_paths_from_enum, compiled_paths_on_filesystem);
}

// todo(rdr): find the right way to feature verify native contracts as well
#[rstest]
#[ignore]
fn verify_feature_contracts(
Expand Down
3 changes: 3 additions & 0 deletions crates/tests-integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ license.workspace = true
[lints]
workspace = true

[features]
cairo_native = ["blockifier/cairo_native"]

[dependencies]
anyhow.workspace = true
assert_matches.workspace = true
Expand Down
8 changes: 6 additions & 2 deletions crates/tests-integration/src/state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ fn prepare_compiled_contract_classes(
serde_json::from_str(&contract.raw_class()).unwrap(),
));
}
CairoVersion::Native => todo!("look up what we need here"),
#[cfg(feature = "cairo_native")]
CairoVersion::Native => {
todo!("native integration doesn't support this yet")
}
}
}

Expand Down Expand Up @@ -319,8 +322,9 @@ impl<'a> ThinStateDiffBuilder<'a> {
CairoVersion::Cairo1 => {
self.declared_classes.insert(contract.class_hash(), Default::default());
}
#[cfg(feature = "cairo_native")]
CairoVersion::Native => {
todo!("look up what we need to do here")
todo!("native integration doesn't support this yet")
}
}
}
Expand Down

0 comments on commit 586415b

Please sign in to comment.