diff --git a/crates/blockifier/src/execution/contract_class.rs b/crates/blockifier/src/execution/contract_class.rs index 4e182ed837..1e257619b7 100644 --- a/crates/blockifier/src/execution/contract_class.rs +++ b/crates/blockifier/src/execution/contract_class.rs @@ -56,6 +56,17 @@ pub enum ContractClass { V1(ContractClassV1), } +impl TryFrom for ContractClass { + type Error = ProgramError; + + fn try_from( + contract_class: starknet_api::contract_class::ContractClass, + ) -> Result { + let starknet_api::contract_class::ContractClass::V1(contract_class_v1) = contract_class; + Ok(ContractClass::V1(contract_class_v1.try_into()?)) + } +} + impl ContractClass { pub fn constructor_selector(&self) -> Option { match self { @@ -356,6 +367,18 @@ impl EntryPointV1 { } } +impl TryFrom for ContractClassV1 { + type Error = ProgramError; + + fn try_from( + contract_class: starknet_api::contract_class::ContractClassV1, + ) -> Result { + let starknet_api::contract_class::ContractClassV1::Casm(casm_contract_class) = + contract_class; + casm_contract_class.try_into() + } +} + impl TryFrom for ContractClassV1 { type Error = ProgramError; @@ -475,6 +498,21 @@ pub struct ClassInfo { abi_length: usize, } +impl TryFrom for ClassInfo { + type Error = ProgramError; + + fn try_from(class_info: starknet_api::contract_class::ClassInfo) -> Result { + let starknet_api::contract_class::ClassInfo { + contract_class, + sierra_program_length, + abi_length, + } = class_info; + + let contract_class: ContractClass = contract_class.try_into()?; + Ok(Self { contract_class, sierra_program_length, abi_length }) + } +} + impl ClassInfo { pub fn bytecode_length(&self) -> usize { self.contract_class.bytecode_length() diff --git a/crates/blockifier/src/transaction/errors.rs b/crates/blockifier/src/transaction/errors.rs index 70b9fbe6ab..cbd9d38085 100644 --- a/crates/blockifier/src/transaction/errors.rs +++ b/crates/blockifier/src/transaction/errors.rs @@ -1,3 +1,4 @@ +use cairo_vm::types::errors::program_errors::ProgramError; use num_bigint::BigUint; use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector, Nonce}; use starknet_api::transaction::{Fee, TransactionVersion}; @@ -109,6 +110,8 @@ pub enum TransactionExecutionError { not." )] InvalidSegmentStructure(usize, usize), + #[error(transparent)] + ProgramError(#[from] ProgramError), } #[derive(Debug, Error)] diff --git a/crates/blockifier/src/transaction/transactions.rs b/crates/blockifier/src/transaction/transactions.rs index 834660c740..8bdd466b50 100644 --- a/crates/blockifier/src/transaction/transactions.rs +++ b/crates/blockifier/src/transaction/transactions.rs @@ -135,6 +135,16 @@ pub struct DeclareTransaction { pub class_info: ClassInfo, } +impl TryFrom for DeclareTransaction { + type Error = TransactionExecutionError; + + fn try_from( + declare_tx: starknet_api::executable_transaction::DeclareTransaction, + ) -> Result { + Self::new_from_executable_tx(declare_tx, false) + } +} + impl DeclareTransaction { fn create( declare_tx: starknet_api::transaction::DeclareTransaction, @@ -163,6 +173,17 @@ impl DeclareTransaction { Self::create(declare_tx, tx_hash, class_info, true) } + fn new_from_executable_tx( + declare_tx: starknet_api::executable_transaction::DeclareTransaction, + only_query: bool, + ) -> Result { + let starknet_api::executable_transaction::DeclareTransaction { tx, tx_hash, class_info } = + declare_tx; + let class_info: ClassInfo = class_info.try_into()?; + + Self::create(tx, tx_hash, class_info, only_query) + } + implement_inner_tx_getter_calls!((class_hash, ClassHash), (signature, TransactionSignature)); pub fn tx(&self) -> &starknet_api::transaction::DeclareTransaction { diff --git a/crates/starknet_api/src/contract_class.rs b/crates/starknet_api/src/contract_class.rs new file mode 100644 index 0000000000..8d9d1be4ec --- /dev/null +++ b/crates/starknet_api/src/contract_class.rs @@ -0,0 +1,22 @@ +use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; + +/// Compiled contract class. +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum ContractClass { + V1(ContractClassV1), +} + +/// Compiled contract class variant for Cairo 1 contracts. +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum ContractClassV1 { + Casm(CasmContractClass), +} + +/// All relevant information about a declared contract class, including the compiled contract class +/// and other parameters derived from the original declare transaction required for billing. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct ClassInfo { + pub contract_class: ContractClass, + pub sierra_program_length: usize, + pub abi_length: usize, +} diff --git a/crates/starknet_api/src/executable_transaction.rs b/crates/starknet_api/src/executable_transaction.rs index 40ac70a97a..b7ebff021a 100644 --- a/crates/starknet_api/src/executable_transaction.rs +++ b/crates/starknet_api/src/executable_transaction.rs @@ -1,5 +1,5 @@ +use crate::contract_class::ClassInfo; use crate::core::{ContractAddress, Nonce}; -use crate::state::ContractClass; use crate::transaction::{Tip, TransactionHash}; /// Represents a paid Starknet transaction. @@ -73,11 +73,3 @@ pub struct InvokeTransaction { pub tx: crate::transaction::InvokeTransaction, pub tx_hash: TransactionHash, } - -#[derive(Clone, Debug, Eq, PartialEq)] -pub struct ClassInfo { - // TODO: use compiled contract class. - pub contract_class: ContractClass, - pub sierra_program_length: usize, - pub abi_length: usize, -} diff --git a/crates/starknet_api/src/lib.rs b/crates/starknet_api/src/lib.rs index 6e91d89ff0..0b335dcfc2 100644 --- a/crates/starknet_api/src/lib.rs +++ b/crates/starknet_api/src/lib.rs @@ -4,6 +4,7 @@ pub mod block; pub mod block_hash; +pub mod contract_class; pub mod core; pub mod crypto; pub mod data_availability;