Skip to content

Commit

Permalink
refactor(blockifier): add getter for tx_hash to transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
meship-starkware committed Sep 26, 2024
1 parent 8fa1dba commit 836b981
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
25 changes: 24 additions & 1 deletion crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::calldata;
use starknet_api::core::{ContractAddress, EntryPointSelector};
use starknet_api::core::{ContractAddress, EntryPointSelector, Nonce};
use starknet_api::deprecated_contract_class::EntryPointType;
use starknet_api::transaction::Resource::{L1DataGas, L1Gas, L2Gas};
use starknet_api::transaction::{
Expand All @@ -11,6 +11,7 @@ use starknet_api::transaction::{
Fee,
ResourceBounds,
TransactionHash,
TransactionSignature,
TransactionVersion,
ValidResourceBounds,
};
Expand Down Expand Up @@ -80,6 +81,18 @@ pub enum AccountTransaction {
Invoke(InvokeTransaction),
}

macro_rules! implement_account_tx_inner_getters {
($(($field:ident, $field_type:ty)),*) => {
$(pub fn $field(&self) -> $field_type {
match self {
Self::Declare(tx) => tx.tx.$field().clone(),
Self::DeployAccount(tx) => tx.tx.$field().clone(),
Self::Invoke(tx) => tx.tx.$field().clone(),
}
})*
};
}

/// This implementation of try_from clones the base transaction struct. This method's advantage is
/// that it does not clone the Casm contract class code.
impl TryFrom<&starknet_api::executable_transaction::Transaction> for AccountTransaction {
Expand Down Expand Up @@ -143,6 +156,16 @@ impl HasRelatedFeeType for AccountTransaction {
}

impl AccountTransaction {
implement_account_tx_inner_getters!((signature, TransactionSignature), (nonce, Nonce));

pub fn sender_address(&self) -> ContractAddress {
match self {
Self::Declare(tx) => tx.tx.sender_address(),
Self::DeployAccount(tx) => tx.tx.contract_address(),
Self::Invoke(tx) => tx.tx.sender_address(),
}
}

// TODO(nir, 01/11/2023): Consider instantiating CommonAccountFields in AccountTransaction.
pub fn tx_type(&self) -> TransactionType {
match self {
Expand Down
23 changes: 22 additions & 1 deletion crates/blockifier/src/transaction/transaction_execution.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::core::{calculate_contract_address, ContractAddress};
use starknet_api::core::{calculate_contract_address, ContractAddress, Nonce};
use starknet_api::transaction::{Fee, Transaction as StarknetApiTransaction, TransactionHash};

use crate::bouncer::verify_tx_weights_in_bounds;
Expand Down Expand Up @@ -37,6 +37,27 @@ pub enum Transaction {
}

impl Transaction {
pub fn nonce(&self) -> Nonce {
match self {
Self::AccountTransaction(tx) => tx.nonce(),
Self::L1HandlerTransaction(tx) => tx.tx.nonce,
}
}

pub fn sender_address(&self) -> ContractAddress {
match self {
Self::AccountTransaction(tx) => tx.sender_address(),
Self::L1HandlerTransaction(tx) => tx.tx.contract_address,
}
}

pub fn tx_hash(tx: &Transaction) -> TransactionHash {
match tx {
Transaction::AccountTransaction(tx) => tx.tx_hash(),
Transaction::L1HandlerTransaction(tx) => tx.tx_hash,
}
}

pub fn from_api(
tx: StarknetApiTransaction,
tx_hash: TransactionHash,
Expand Down

0 comments on commit 836b981

Please sign in to comment.