Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(transaction): add common getters to transaction #229

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading