Skip to content

Commit

Permalink
refactor(blockifier): remove WORD_WIDTH from blockifier, use starkn e…
Browse files Browse the repository at this point in the history
…t_api instead
  • Loading branch information
avivg-starkware committed Nov 12, 2024
1 parent d75daef commit 595fcf0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 5 deletions.
72 changes: 71 additions & 1 deletion crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use semver::Version;
use serde::de::Error as DeserializationError;
use serde::{Deserialize, Deserializer, Serialize};
use starknet_api::contract_class::{ContractClass, EntryPointType};
use starknet_api::core::EntryPointSelector;
use starknet_api::core::{EntryPointSelector, WORD_WIDTH};
use starknet_api::deprecated_contract_class::{
ContractClass as DeprecatedContractClass,
EntryPointOffset,
Expand Down Expand Up @@ -514,6 +514,76 @@ 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.
* 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
4 changes: 2 additions & 2 deletions crates/blockifier/src/fee/eth_gas_constants.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use starknet_api::core::WORD_WIDTH;

// Calldata.
pub const GAS_PER_MEMORY_ZERO_BYTE: usize = 4;
pub const GAS_PER_MEMORY_BYTE: usize = 16;
// TODO(AvivG): use starknet_api::core::WORD_WIDTH instead.
pub const WORD_WIDTH: usize = 32;
pub const GAS_PER_MEMORY_WORD: usize = GAS_PER_MEMORY_BYTE * WORD_WIDTH;

// Blob Data.
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/fee/receipt_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rstest::{fixture, rstest};
use starknet_api::core::WORD_WIDTH;
use starknet_api::execution_resources::GasVector;
use starknet_api::transaction::fields::GasVectorComputationMode;
use starknet_api::transaction::L2ToL1Payload;
Expand Down Expand Up @@ -85,8 +86,7 @@ fn test_calculate_tx_gas_usage_basic<'a>(
.gas_per_code_byte;
let code_gas_cost = (gas_per_code_byte
* u64_from_usize(
(class_info.bytecode_length() + class_info.sierra_program_length())
* eth_gas_constants::WORD_WIDTH
(class_info.bytecode_length() + class_info.sierra_program_length()) * WORD_WIDTH
+ class_info.abi_length(),
))
.to_integer()
Expand Down

0 comments on commit 595fcf0

Please sign in to comment.