Skip to content

Commit

Permalink
refactor: add casm contract class struct compatible for executable tr…
Browse files Browse the repository at this point in the history
…ansaction (#357)

* refactor: add casm contract class struct compatible for executable transaction

* refactor: use casm contract class for executable transaction

* refactor: add try from executable declare tx to account tx
  • Loading branch information
ArniStarkware authored Aug 12, 2024
1 parent 6996784 commit a4ced95
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 9 deletions.
38 changes: 38 additions & 0 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ pub enum ContractClass {
V1(ContractClassV1),
}

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

fn try_from(
contract_class: starknet_api::contract_class::ContractClass,
) -> Result<Self, Self::Error> {
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<EntryPointSelector> {
match self {
Expand Down Expand Up @@ -356,6 +367,18 @@ impl EntryPointV1 {
}
}

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

fn try_from(
contract_class: starknet_api::contract_class::ContractClassV1,
) -> Result<Self, Self::Error> {
let starknet_api::contract_class::ContractClassV1::Casm(casm_contract_class) =
contract_class;
casm_contract_class.try_into()
}
}

impl TryFrom<CasmContractClass> for ContractClassV1 {
type Error = ProgramError;

Expand Down Expand Up @@ -475,6 +498,21 @@ pub struct ClassInfo {
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;

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()
Expand Down
3 changes: 3 additions & 0 deletions crates/blockifier/src/transaction/errors.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -109,6 +110,8 @@ pub enum TransactionExecutionError {
not."
)]
InvalidSegmentStructure(usize, usize),
#[error(transparent)]
ProgramError(#[from] ProgramError),
}

#[derive(Debug, Error)]
Expand Down
21 changes: 21 additions & 0 deletions crates/blockifier/src/transaction/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ pub struct DeclareTransaction {
pub class_info: ClassInfo,
}

impl TryFrom<starknet_api::executable_transaction::DeclareTransaction> for DeclareTransaction {
type Error = TransactionExecutionError;

fn try_from(
declare_tx: starknet_api::executable_transaction::DeclareTransaction,
) -> Result<Self, Self::Error> {
Self::new_from_executable_tx(declare_tx, false)
}
}

impl DeclareTransaction {
fn create(
declare_tx: starknet_api::transaction::DeclareTransaction,
Expand Down Expand Up @@ -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<Self, TransactionExecutionError> {
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 {
Expand Down
22 changes: 22 additions & 0 deletions crates/starknet_api/src/contract_class.rs
Original file line number Diff line number Diff line change
@@ -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,
}
10 changes: 1 addition & 9 deletions crates/starknet_api/src/executable_transaction.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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,
}
1 change: 1 addition & 0 deletions crates/starknet_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a4ced95

Please sign in to comment.