Skip to content

Commit

Permalink
chore(blockifier): implement staknet_api class_info to match blockifi…
Browse files Browse the repository at this point in the history
…er class_info
  • Loading branch information
avivg-starkware committed Nov 4, 2024
1 parent 8af325a commit b9d906d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/starknet_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ testing = []
bitvec.workspace = true
cairo-lang-starknet-classes.workspace = true
cairo-lang-runner.workspace = true
cairo-vm.workspace = true
derive_more.workspace = true
hex.workspace = true
indexmap = { workspace = true, features = ["serde"] }
Expand Down
57 changes: 57 additions & 0 deletions crates/starknet_api/src/contract_class.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use cairo_vm::serde::deserialize_program::deserialize_array_of_bigint_hex;
use serde::{Deserialize, Serialize};

use crate::core::CompiledClassHash;
use crate::deprecated_contract_class::ContractClass as DeprecatedContractClass;
use crate::StarknetApiError;

// Calldata.
pub const WORD_WIDTH: usize = 32;

#[derive(
Debug, Default, Clone, Copy, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord,
Expand Down Expand Up @@ -47,3 +52,55 @@ pub struct ClassInfo {
pub sierra_program_length: usize,
pub abi_length: usize,
}

impl ClassInfo {
pub fn bytecode_length(&self) -> usize {
match &self.contract_class {
ContractClass::V0(contract_address) => {
let data = deserialize_array_of_bigint_hex(&contract_address.program.data)
.expect("Failed deserializing the program data.");
data.len()
}
ContractClass::V1(contract_address) => contract_address.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,
) -> Result<Self, StarknetApiError> {
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(StarknetApiError::ContractClassVersionSierraProgramLengthMismatch {
contract_class_version,
sierra_program_length,
})
}
}
}
8 changes: 8 additions & 0 deletions crates/starknet_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ pub enum StarknetApiError {
InvalidStarknetVersion(Vec<u8>),
#[error("NonzeroGasPrice cannot be zero.")]
ZeroGasPrice,
#[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,
},
}

pub type StarknetApiResult<T> = Result<T, StarknetApiError>;

0 comments on commit b9d906d

Please sign in to comment.