Skip to content

Commit

Permalink
refactor: add try from executable declare tx to account tx
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Aug 8, 2024
1 parent a43bbe8 commit c132b7f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
15 changes: 15 additions & 0 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,21 @@ pub struct ClassInfo {
abi_length: usize,
}

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

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

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
23 changes: 23 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(
value: starknet_api::executable_transaction::DeclareTransaction,
) -> Result<Self, Self::Error> {
Self::new_from_executable_tx(value, false)
}
}

impl DeclareTransaction {
fn create(
declare_tx: starknet_api::transaction::DeclareTransaction,
Expand Down Expand Up @@ -163,6 +173,19 @@ impl DeclareTransaction {
Self::create(declare_tx, tx_hash, class_info, true)
}

pub 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()?;

// TODO(Arni): We don't need to verify the contract class version here, any executable
// declare transaction will always have matching versions.
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
6 changes: 3 additions & 3 deletions crates/starknet_api/src/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum ContractClassV1 {

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ClassInfo {
contract_class: ContractClass,
sierra_program_length: usize,
abi_length: usize,
pub contract_class: ContractClass,
pub sierra_program_length: usize,
pub abi_length: usize,
}

0 comments on commit c132b7f

Please sign in to comment.