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 3, 2024
1 parent 5c782b6 commit bb6dd7f
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 4 deletions.
56 changes: 54 additions & 2 deletions crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ 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::{ClassHash, ContractAddress, EntryPointSelector, Nonce};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::deprecated_contract_class::EntryPointType;
use starknet_api::transaction::{
AccountDeploymentData,
Calldata,
DeprecatedResourceBoundsMapping,
Fee,
PaymasterData,
ResourceBounds,
Tip,
TransactionHash,
TransactionSignature,
TransactionVersion,
};
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -77,9 +83,20 @@ 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(),
}
})*
};
}

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

fn try_from(
executable_transaction: starknet_api::executable_transaction::Transaction,
) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -115,6 +132,41 @@ impl HasRelatedFeeType for AccountTransaction {
}

impl AccountTransaction {
implement_account_tx_inner_getters!(
(signature, TransactionSignature),
(nonce, Nonce),
(resource_bounds, DeprecatedResourceBoundsMapping),
(tip, Tip),
(nonce_data_availability_mode, DataAvailabilityMode),
(fee_data_availability_mode, DataAvailabilityMode),
(paymaster_data, PaymasterData),
(max_fee, Option<Fee>)
);

pub fn class_hash(&self) -> Option<ClassHash> {
match self {
Self::Declare(tx) => Some(tx.tx.class_hash()),
Self::DeployAccount(tx) => Some(tx.tx.class_hash()),
Self::Invoke(_) => None,
}
}

pub fn sender_address(&self) -> Option<ContractAddress> {
match self {
Self::Declare(tx) => Some(tx.tx.sender_address()),
Self::DeployAccount(_) => None,
Self::Invoke(tx) => Some(tx.tx.sender_address()),
}
}

pub fn account_deployment_data(&self) -> Option<AccountDeploymentData> {
match self {
Self::Declare(tx) => Some(tx.tx.account_deployment_data().clone()),
Self::DeployAccount(_) => None,
Self::Invoke(tx) => Some(tx.tx.account_deployment_data().clone()),
}
}

// TODO(nir, 01/11/2023): Consider instantiating CommonAccountFields in AccountTransaction.
pub fn tx_type(&self) -> TransactionType {
match self {
Expand Down
7 changes: 7 additions & 0 deletions crates/blockifier/src/transaction/transaction_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ impl Transaction {
_ => unimplemented!(),
}
}

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

impl TransactionInfoCreator for Transaction {
Expand Down
18 changes: 16 additions & 2 deletions crates/starknet_api/src/executable_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::transaction::{
Calldata,
ContractAddressSalt,
DeprecatedResourceBoundsMapping,
Fee,
PaymasterData,
Tip,
TransactionHash,
Expand Down Expand Up @@ -150,7 +151,13 @@ impl DeployAccountTransaction {
(contract_address_salt, ContractAddressSalt),
(nonce, Nonce),
(signature, TransactionSignature),
(version, TransactionVersion)
(version, TransactionVersion),
(resource_bounds, DeprecatedResourceBoundsMapping),
(tip, Tip),
(nonce_data_availability_mode, DataAvailabilityMode),
(fee_data_availability_mode, DataAvailabilityMode),
(paymaster_data, PaymasterData),
(max_fee, Option<Fee>)
);
implement_getter_calls!((tx_hash, TransactionHash), (contract_address, ContractAddress));

Expand All @@ -171,7 +178,14 @@ impl InvokeTransaction {
(nonce, Nonce),
(signature, TransactionSignature),
(sender_address, ContractAddress),
(version, TransactionVersion)
(version, TransactionVersion),
(resource_bounds, DeprecatedResourceBoundsMapping),
(tip, Tip),
(nonce_data_availability_mode, DataAvailabilityMode),
(fee_data_availability_mode, DataAvailabilityMode),
(paymaster_data, PaymasterData),
(account_deployment_data, AccountDeploymentData),
(max_fee, Option<Fee>)
);
implement_getter_calls!((tx_hash, TransactionHash));

Expand Down
92 changes: 92 additions & 0 deletions crates/starknet_api/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,17 @@ macro_rules! implement_declare_tx_getters {
};
}

macro_rules! implement_declare_tx_v3_getters {
($(($field:ident, $field_type:ty)),*) => {
$(pub fn $field(&self) -> $field_type {
match self {
Self::V3(tx) => tx.$field.clone(),
_ => panic!("Field {} is only available for DeclareTransactionV3.", stringify!($field)),
}
})*
};
}

impl DeclareTransaction {
implement_declare_tx_getters!(
(class_hash, ClassHash),
Expand All @@ -268,6 +279,15 @@ impl DeclareTransaction {
(signature, TransactionSignature)
);

implement_declare_tx_v3_getters!(
(resource_bounds, DeprecatedResourceBoundsMapping),
(tip, Tip),
(nonce_data_availability_mode, DataAvailabilityMode),
(fee_data_availability_mode, DataAvailabilityMode),
(paymaster_data, PaymasterData),
(account_deployment_data, AccountDeploymentData)
);

pub fn version(&self) -> TransactionVersion {
match self {
DeclareTransaction::V0(_) => TransactionVersion::ZERO,
Expand All @@ -276,6 +296,24 @@ impl DeclareTransaction {
DeclareTransaction::V3(_) => TransactionVersion::THREE,
}
}

pub fn compiled_class_hash(&self) -> Option<CompiledClassHash> {
match self {
DeclareTransaction::V0(_) => None,
DeclareTransaction::V1(_) => None,
DeclareTransaction::V2(tx) => Some(tx.compiled_class_hash),
DeclareTransaction::V3(tx) => Some(tx.compiled_class_hash),
}
}

pub fn max_fee(&self) -> Option<Fee> {
match self {
DeclareTransaction::V0(tx) => Some(tx.max_fee),
DeclareTransaction::V1(tx) => Some(tx.max_fee),
DeclareTransaction::V2(tx) => Some(tx.max_fee),
DeclareTransaction::V3(_) => None,
}
}
}

impl TransactionHasher for DeclareTransaction {
Expand Down Expand Up @@ -366,6 +404,17 @@ macro_rules! implement_deploy_account_tx_getters {
};
}

macro_rules! implement_deploy_account_tx_v3_getters {
($(($field:ident, $field_type:ty)),*) => {
$(pub fn $field(&self) -> $field_type {
match self {
Self::V3(tx) => tx.$field.clone(),
_ => panic!("Field {} is only available for DeclareTransactionV3.", stringify!($field)),
}
})*
};
}

impl DeployAccountTransaction {
implement_deploy_account_tx_getters!(
(class_hash, ClassHash),
Expand All @@ -375,12 +424,27 @@ impl DeployAccountTransaction {
(signature, TransactionSignature)
);

implement_deploy_account_tx_v3_getters!(
(resource_bounds, DeprecatedResourceBoundsMapping),
(tip, Tip),
(nonce_data_availability_mode, DataAvailabilityMode),
(fee_data_availability_mode, DataAvailabilityMode),
(paymaster_data, PaymasterData)
);

pub fn version(&self) -> TransactionVersion {
match self {
DeployAccountTransaction::V1(_) => TransactionVersion::ONE,
DeployAccountTransaction::V3(_) => TransactionVersion::THREE,
}
}

pub fn max_fee(&self) -> Option<Fee> {
match self {
DeployAccountTransaction::V1(tx) => Some(tx.max_fee),
DeployAccountTransaction::V3(_) => None,
}
}
}

impl TransactionHasher for DeployAccountTransaction {
Expand Down Expand Up @@ -503,9 +567,29 @@ macro_rules! implement_invoke_tx_getters {
};
}

macro_rules! implement_invoke_tx_v3_getters {
($(($field:ident, $field_type:ty)),*) => {
$(pub fn $field(&self) -> $field_type {
match self {
Self::V3(tx) => tx.$field.clone(),
_ => panic!("Field {} is only available for DeclareTransactionV3.", stringify!($field)),
}
})*
};
}

impl InvokeTransaction {
implement_invoke_tx_getters!((calldata, Calldata), (signature, TransactionSignature));

implement_invoke_tx_v3_getters!(
(resource_bounds, DeprecatedResourceBoundsMapping),
(tip, Tip),
(nonce_data_availability_mode, DataAvailabilityMode),
(fee_data_availability_mode, DataAvailabilityMode),
(paymaster_data, PaymasterData),
(account_deployment_data, AccountDeploymentData)
);

pub fn nonce(&self) -> Nonce {
match self {
Self::V0(_) => Nonce::default(),
Expand All @@ -529,6 +613,14 @@ impl InvokeTransaction {
InvokeTransaction::V3(_) => TransactionVersion::THREE,
}
}

pub fn max_fee(&self) -> Option<Fee> {
match self {
InvokeTransaction::V0(tx) => Some(tx.max_fee),
InvokeTransaction::V1(tx) => Some(tx.max_fee),
InvokeTransaction::V3(_) => None,
}
}
}

impl TransactionHasher for InvokeTransaction {
Expand Down

0 comments on commit bb6dd7f

Please sign in to comment.