Skip to content

Commit

Permalink
refactor(blockifier): switch use of blockifier classinfo by starknet_…
Browse files Browse the repository at this point in the history
…api classinfo
  • Loading branch information
avivg-starkware committed Nov 7, 2024
1 parent 7f03129 commit 343b3bc
Show file tree
Hide file tree
Showing 11 changed files with 9 additions and 104 deletions.
75 changes: 1 addition & 74 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,17 @@ use starknet_types_core::felt::Felt;

use crate::abi::constants::{self};
use crate::execution::entry_point::CallEntryPoint;
use crate::execution::errors::{ContractClassError, PreExecutionError};
use crate::execution::errors::PreExecutionError;
use crate::execution::execution_utils::{poseidon_hash_many_cost, sn_api_to_cairo_vm_program};
#[cfg(feature = "cairo_native")]
use crate::execution::native::contract_class::NativeContractClassV1;
use crate::fee::eth_gas_constants;
use crate::transaction::errors::TransactionExecutionError;
use crate::versioned_constants::CompilerVersion;

#[cfg(test)]
#[path = "contract_class_test.rs"]
pub mod test;

pub type ContractClassResult<T> = Result<T, ContractClassError>;

pub trait HasSelector {
fn selector(&self) -> &EntryPointSelector;
}
Expand Down Expand Up @@ -521,76 +518,6 @@ fn convert_entry_points_v1(external: &[CasmContractEntryPoint]) -> Vec<EntryPoin
.collect()
}

#[derive(Clone, Debug)]
// TODO(Ayelet,10/02/2024): Change to bytes.
pub struct ClassInfo {
contract_class: ContractClass,
sierra_program_length: usize,
abi_length: usize,
}

impl TryFrom<starknet_api::contract_class::ClassInfo> for ClassInfo {
type Error = ProgramError;

fn try_from(class_info: starknet_api::contract_class::ClassInfo) -> Result<Self, Self::Error> {
let starknet_api::contract_class::ClassInfo {
contract_class,
sierra_program_length,
abi_length,
} = class_info;

Ok(Self { contract_class: contract_class.clone(), sierra_program_length, abi_length })
}
}

impl ClassInfo {
pub fn bytecode_length(&self) -> usize {
match &self.contract_class {
ContractClass::V0(contract_class) => contract_class.bytecode_length(),
ContractClass::V1(contract_class) => contract_class.bytecode.len(),
}
}

pub fn contract_class(&self) -> ContractClass {
self.contract_class.clone()
}

pub fn sierra_program_length(&self) -> usize {
self.sierra_program_length
}

pub fn abi_length(&self) -> usize {
self.abi_length
}

pub fn code_size(&self) -> usize {
(self.bytecode_length() + self.sierra_program_length())
// We assume each felt is a word.
* eth_gas_constants::WORD_WIDTH
+ self.abi_length()
}

pub fn new(
contract_class: &ContractClass,
sierra_program_length: usize,
abi_length: usize,
) -> ContractClassResult<Self> {
let (contract_class_version, condition) = match contract_class {
ContractClass::V0(_) => (0, sierra_program_length == 0),
ContractClass::V1(_) => (1, sierra_program_length > 0),
};

if condition {
Ok(Self { contract_class: contract_class.clone(), sierra_program_length, abi_length })
} else {
Err(ContractClassError::ContractClassVersionSierraProgramLengthMismatch {
contract_class_version,
sierra_program_length,
})
}
}
}

// TODO(Yoni): organize this file.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
/// Modelled after [cairo_lang_starknet_classes::contract_class::ContractEntryPoints].
Expand Down
12 changes: 0 additions & 12 deletions crates/blockifier/src/execution/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,3 @@ impl ConstructorEntryPointExecutionError {
}
}
}

#[derive(Debug, Error)]
pub enum ContractClassError {
#[error(
"Sierra program length must be > 0 for Cairo1, and == 0 for Cairo0. Got: \
{sierra_program_length:?} for contract class version {contract_class_version:?}"
)]
ContractClassVersionSierraProgramLengthMismatch {
contract_class_version: u8,
sierra_program_length: usize,
},
}
2 changes: 1 addition & 1 deletion crates/blockifier/src/test_utils/declare.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use starknet_api::contract_class::ClassInfo;
use starknet_api::test_utils::declare::DeclareTxArgs;

use crate::execution::contract_class::ClassInfo;
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::transactions::DeclareTransaction;

Expand Down
3 changes: 1 addition & 2 deletions crates/blockifier/src/transaction/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rstest::fixture;
use starknet_api::block::GasPrice;
use starknet_api::contract_class::ContractClass;
use starknet_api::contract_class::{ClassInfo, ContractClass};
use starknet_api::core::{ClassHash, ContractAddress, Nonce};
use starknet_api::execution_resources::GasAmount;
use starknet_api::test_utils::deploy_account::DeployAccountTxArgs;
Expand All @@ -26,7 +26,6 @@ use strum::IntoEnumIterator;

use crate::abi::abi_utils::get_fee_token_var_address;
use crate::context::{BlockContext, ChainInfo};
use crate::execution::contract_class::ClassInfo;
use crate::state::cached_state::CachedState;
use crate::state::state_api::State;
use crate::test_utils::contracts::FeatureContract;
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/transaction/transaction_execution.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::sync::Arc;

use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::contract_class::ClassInfo;
use starknet_api::core::{calculate_contract_address, ContractAddress, Nonce};
use starknet_api::executable_transaction::L1HandlerTransaction;
use starknet_api::transaction::{Fee, Transaction as StarknetApiTransaction, TransactionHash};

use crate::bouncer::verify_tx_weights_within_max_capacity;
use crate::context::BlockContext;
use crate::execution::call_info::CallInfo;
use crate::execution::contract_class::ClassInfo;
use crate::execution::entry_point::EntryPointExecutionContext;
use crate::fee::receipt::TransactionReceipt;
use crate::state::cached_state::TransactionalState;
Expand Down
4 changes: 1 addition & 3 deletions crates/blockifier/src/transaction/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::contract_class::{ContractClass, EntryPointType};
use starknet_api::contract_class::{ClassInfo, ContractClass, EntryPointType};
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::executable_transaction::{
DeclareTransaction as ExecutableDeclareTx,
Expand All @@ -24,7 +24,6 @@ use starknet_api::transaction::{
use crate::abi::abi_utils::selector_from_name;
use crate::context::{BlockContext, TransactionContext};
use crate::execution::call_info::CallInfo;
use crate::execution::contract_class::ClassInfo;
use crate::execution::entry_point::{
CallEntryPoint,
CallType,
Expand Down Expand Up @@ -199,7 +198,6 @@ impl DeclareTransaction {
) -> Result<Self, TransactionExecutionError> {
let starknet_api::executable_transaction::DeclareTransaction { tx, tx_hash, class_info } =
declare_tx;
let class_info = class_info.try_into()?;

Self::create(tx, tx_hash, class_info, only_query)
}
Expand Down
3 changes: 0 additions & 3 deletions crates/blockifier_reexecution/src/state_reader/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use blockifier::execution::errors::ContractClassError;
use blockifier::state::errors::StateError;
use blockifier::transaction::errors::TransactionExecutionError;
use blockifier::versioned_constants::VersionedConstantsError;
Expand All @@ -22,6 +21,4 @@ pub enum ReexecutionError {
TransactionExecutionError(#[from] TransactionExecutionError),
#[error(transparent)]
VersionedConstants(#[from] VersionedConstantsError),
#[error(transparent)]
ContractClassError(#[from] ContractClassError),
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use blockifier::execution::contract_class::ClassInfo;
use blockifier::state::state_api::StateResult;
use blockifier::transaction::transaction_execution::Transaction as BlockifierTransaction;
use papyrus_execution::DEPRECATED_CONTRACT_SIERRA_SIZE;
use starknet_api::contract_class::ClassInfo;
use starknet_api::core::ClassHash;
use starknet_api::transaction::{Transaction, TransactionHash};
use starknet_core::types::ContractClass as StarknetContractClass;
Expand Down
2 changes: 0 additions & 2 deletions crates/native_blockifier/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use blockifier::blockifier::stateful_validator::StatefulValidatorError;
use blockifier::blockifier::transaction_executor::TransactionExecutorError;
use blockifier::bouncer::BuiltinCount;
use blockifier::execution::errors::ContractClassError;
use blockifier::state::errors::StateError;
use blockifier::transaction::errors::{
ParseError,
Expand Down Expand Up @@ -67,7 +66,6 @@ macro_rules! native_blockifier_errors {
}

native_blockifier_errors!(
(ContractClassError, ContractClassError, PyContractClassError),
(NativeBlockifierInputError, NativeBlockifierInputError, PyNativeBlockifierInputError),
(ProgramError, ProgramError, PyProgramError),
(Pyo3Error, PyErr, PyPyo3Error),
Expand Down
3 changes: 1 addition & 2 deletions crates/native_blockifier/src/py_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::collections::BTreeMap;

use blockifier::execution::contract_class::ClassInfo;
use blockifier::transaction::account_transaction::AccountTransaction;
use blockifier::transaction::transaction_execution::Transaction;
use blockifier::transaction::transaction_types::TransactionType;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use starknet_api::block::GasPrice;
use starknet_api::contract_class::ContractClass;
use starknet_api::contract_class::{ClassInfo, ContractClass};
use starknet_api::execution_resources::GasAmount;
use starknet_api::transaction::{
DeprecatedResourceBoundsMapping,
Expand Down
5 changes: 2 additions & 3 deletions crates/papyrus_execution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use blockifier::blockifier::block::{pre_process_block, BlockInfo, GasPrices};
use blockifier::bouncer::BouncerConfig;
use blockifier::context::{BlockContext, ChainInfo, FeeTokenAddresses, TransactionContext};
use blockifier::execution::call_info::CallExecution;
use blockifier::execution::contract_class::ClassInfo;
use blockifier::execution::entry_point::{
CallEntryPoint,
CallType as BlockifierCallType,
Expand All @@ -54,7 +53,7 @@ use papyrus_storage::header::HeaderStorageReader;
use papyrus_storage::{StorageError, StorageReader};
use serde::{Deserialize, Serialize};
use starknet_api::block::{BlockHashAndNumber, BlockNumber, NonzeroGasPrice, StarknetVersion};
use starknet_api::contract_class::EntryPointType;
use starknet_api::contract_class::{ClassInfo, EntryPointType};
use starknet_api::core::{ChainId, ClassHash, ContractAddress, EntryPointSelector};
use starknet_api::data_availability::L1DataAvailabilityMode;
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
Expand Down Expand Up @@ -165,7 +164,7 @@ pub enum ExecutionError {
BadDeclareTransaction {
tx: DeclareTransaction,
#[source]
err: blockifier::execution::errors::ContractClassError,
err: StarknetApiError,
},
#[error("Execution config file does not contain a configuration for all blocks")]
ConfigContentError,
Expand Down

0 comments on commit 343b3bc

Please sign in to comment.