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 29, 2024
1 parent 6d3f0e5 commit 6815d18
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 61 deletions.
24 changes: 18 additions & 6 deletions crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ use starknet_api::core::{ClassHash, ContractAddress, PatriciaKey};
use starknet_api::execution_resources::{GasAmount, GasVector};
use starknet_api::state::StorageKey;
use starknet_api::transaction::{
Calldata,
ContractAddressSalt,
Fee,
GasVectorComputationMode,
TransactionVersion,
Calldata, ContractAddressSalt, Fee, GasVectorComputationMode, TransactionVersion,
};
use starknet_api::{contract_address, felt, patricia_key};
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -62,6 +58,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 @@ -89,6 +86,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 @@ -116,6 +114,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 @@ -323,7 +323,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::Cairo1 => {
if let $crate::transaction::errors::TransactionExecutionError::ValidateTransactionError {
error, ..
} = $error {
Expand Down
40 changes: 22 additions & 18 deletions crates/blockifier/src/test_utils/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use starknet_api::contract_class::EntryPointType;
use starknet_api::core::{
ClassHash,
CompiledClassHash,
ContractAddress,
EntryPointSelector,
PatriciaKey,
ClassHash, CompiledClassHash, ContractAddress, EntryPointSelector, PatriciaKey,
};
use starknet_api::deprecated_contract_class::{
ContractClass as DeprecatedContractClass,
EntryPointOffset,
ContractClass as DeprecatedContractClass, EntryPointOffset,
};
use starknet_api::{class_hash, contract_address, felt, patricia_key};
use starknet_types_core::felt::Felt;
Expand All @@ -17,14 +12,14 @@ 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 +144,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 +160,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 +189,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 +248,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 +258,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 +272,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 +283,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,6 +323,7 @@ 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)
}
#[cfg(feature = "cairo_native")]
CairoVersion::Native => starknet_compile(self.get_source_path(), None, None),
}
}
Expand Down
28 changes: 11 additions & 17 deletions crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::sync::Arc;

#[cfg(feature = "cairo_native")]
use crate::execution::native::contract_class::NativeContractClassV1;
#[cfg(feature = "cairo_native")]
use cairo_native::executor::AotNativeExecutor;

use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use serde_json::Value;
use starknet_api::block::{BlockHash, BlockNumber, BlockTimestamp, NonzeroGasPrice};
Expand All @@ -18,32 +22,21 @@ 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,
CallEntryPoint, EntryPointExecutionContext, EntryPointExecutionResult,
};
use crate::state::state_api::State;
use crate::test_utils::{
get_raw_contract_class,
CURRENT_BLOCK_NUMBER,
CURRENT_BLOCK_TIMESTAMP,
DEFAULT_ETH_L1_DATA_GAS_PRICE,
DEFAULT_ETH_L1_GAS_PRICE,
DEFAULT_STRK_L1_DATA_GAS_PRICE,
DEFAULT_STRK_L1_GAS_PRICE,
TEST_ERC20_CONTRACT_ADDRESS,
TEST_ERC20_CONTRACT_ADDRESS2,
get_raw_contract_class, CURRENT_BLOCK_NUMBER, CURRENT_BLOCK_TIMESTAMP,
DEFAULT_ETH_L1_DATA_GAS_PRICE, DEFAULT_ETH_L1_GAS_PRICE, DEFAULT_STRK_L1_DATA_GAS_PRICE,
DEFAULT_STRK_L1_GAS_PRICE, TEST_ERC20_CONTRACT_ADDRESS, TEST_ERC20_CONTRACT_ADDRESS2,
TEST_SEQUENCER_ADDRESS,
};
use crate::transaction::objects::{DeprecatedTransactionInfo, TransactionInfo};
use crate::transaction::transactions::L1HandlerTransaction;
use crate::versioned_constants::{
GasCosts,
OsConstants,
VersionedConstants,
VERSIONED_CONSTANTS_LATEST_JSON,
GasCosts, OsConstants, VersionedConstants, VERSIONED_CONSTANTS_LATEST_JSON,
};

impl CallEntryPoint {
Expand Down Expand Up @@ -264,6 +257,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 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
30 changes: 10 additions & 20 deletions crates/tests-integration/src/state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ use blockifier::abi::abi_utils::get_fee_token_var_address;
use blockifier::context::{BlockContext, ChainInfo};
use blockifier::test_utils::contracts::FeatureContract;
use blockifier::test_utils::{
CairoVersion,
BALANCE,
CURRENT_BLOCK_TIMESTAMP,
DEFAULT_ETH_L1_GAS_PRICE,
DEFAULT_STRK_L1_GAS_PRICE,
TEST_SEQUENCER_ADDRESS,
CairoVersion, BALANCE, CURRENT_BLOCK_TIMESTAMP, DEFAULT_ETH_L1_GAS_PRICE,
DEFAULT_STRK_L1_GAS_PRICE, TEST_SEQUENCER_ADDRESS,
};
use blockifier::transaction::objects::FeeType;
use blockifier::versioned_constants::VersionedConstants;
Expand All @@ -28,20 +24,10 @@ use papyrus_storage::state::StateStorageWriter;
use papyrus_storage::test_utils::{get_test_storage, get_test_storage_with_config_by_scope};
use papyrus_storage::{StorageConfig, StorageReader, StorageWriter};
use starknet_api::block::{
BlockBody,
BlockHeader,
BlockHeaderWithoutHash,
BlockNumber,
BlockTimestamp,
GasPricePerToken,
BlockBody, BlockHeader, BlockHeaderWithoutHash, BlockNumber, BlockTimestamp, GasPricePerToken,
};
use starknet_api::core::{
ChainId,
ClassHash,
ContractAddress,
Nonce,
PatriciaKey,
SequencerContractAddress,
ChainId, ClassHash, ContractAddress, Nonce, PatriciaKey, SequencerContractAddress,
};
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
use starknet_api::state::{StorageKey, ThinStateDiff};
Expand Down Expand Up @@ -183,7 +169,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 +308,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 6815d18

Please sign in to comment.