Skip to content

Commit

Permalink
chore: streamline tx_hash getter method (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware authored Aug 14, 2024
1 parent 2c4de5f commit 7082e88
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
16 changes: 15 additions & 1 deletion crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::calldata;
use starknet_api::core::{ContractAddress, EntryPointSelector};
use starknet_api::deprecated_contract_class::EntryPointType;
use starknet_api::transaction::{Calldata, Fee, ResourceBounds, TransactionVersion};
use starknet_api::transaction::{
Calldata,
Fee,
ResourceBounds,
TransactionHash,
TransactionVersion,
};
use starknet_types_core::felt::Felt;

use crate::abi::abi_utils::selector_from_name;
Expand Down Expand Up @@ -638,6 +644,14 @@ impl AccountTransaction {

self.run_revertible(state, tx_context, remaining_gas, validate, charge_fee)
}

pub fn tx_hash(&self) -> TransactionHash {
match self {
AccountTransaction::Declare(tx) => tx.tx_hash(),
AccountTransaction::DeployAccount(tx) => tx.tx_hash(),
AccountTransaction::Invoke(tx) => tx.tx_hash(),
}
}
}

impl<U: UpdatableState> ExecutableTransaction<U> for AccountTransaction {
Expand Down
12 changes: 10 additions & 2 deletions crates/blockifier/src/transaction/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ impl DeployAccountTransaction {
pub fn tx(&self) -> &starknet_api::transaction::DeployAccountTransaction {
&self.tx
}

pub fn tx_hash(&self) -> TransactionHash {
self.tx_hash
}
}

impl<S: State> Executable<S> for DeployAccountTransaction {
Expand Down Expand Up @@ -379,7 +383,7 @@ impl<S: State> Executable<S> for DeployAccountTransaction {
impl TransactionInfoCreator for DeployAccountTransaction {
fn create_tx_info(&self) -> TransactionInfo {
let common_fields = CommonAccountFields {
transaction_hash: self.tx_hash,
transaction_hash: self.tx_hash(),
version: self.version(),
signature: self.signature(),
nonce: self.nonce(),
Expand Down Expand Up @@ -439,6 +443,10 @@ impl InvokeTransaction {
(sender_address, ContractAddress),
(version, TransactionVersion)
);

pub fn tx_hash(&self) -> TransactionHash {
self.tx_hash
}
}

impl<S: State> Executable<S> for InvokeTransaction {
Expand Down Expand Up @@ -487,7 +495,7 @@ impl<S: State> Executable<S> for InvokeTransaction {
impl TransactionInfoCreator for InvokeTransaction {
fn create_tx_info(&self) -> TransactionInfo {
let common_fields = CommonAccountFields {
transaction_hash: self.tx_hash,
transaction_hash: self.tx_hash(),
version: self.version(),
signature: self.signature(),
nonce: self.nonce(),
Expand Down
4 changes: 2 additions & 2 deletions crates/gateway/src/gateway_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::gateway::{add_tx, AppState, SharedMempoolClient};
use crate::state_reader_test_utils::{local_test_state_reader_factory, TestStateReaderFactory};
use crate::stateful_transaction_validator::StatefulTransactionValidator;
use crate::stateless_transaction_validator::StatelessTransactionValidator;
use crate::utils::{external_tx_to_account_tx, get_tx_hash};
use crate::utils::external_tx_to_account_tx;

pub fn app_state(
mempool_client: SharedMempoolClient,
Expand Down Expand Up @@ -99,5 +99,5 @@ fn calculate_hash(external_tx: &RpcTransaction) -> TransactionHash {
&ChainInfo::create_for_testing().chain_id,
)
.unwrap();
get_tx_hash(&account_tx)
account_tx.tx_hash()
}
4 changes: 2 additions & 2 deletions crates/gateway/src/stateful_transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use tracing::error;
use crate::config::StatefulTransactionValidatorConfig;
use crate::errors::{GatewaySpecError, StatefulTransactionValidatorResult};
use crate::state_reader::{MempoolStateReader, StateReaderFactory};
use crate::utils::{external_tx_to_account_tx, get_sender_address, get_tx_hash};
use crate::utils::{external_tx_to_account_tx, get_sender_address};

#[cfg(test)]
#[path = "stateful_transaction_validator_test.rs"]
Expand Down Expand Up @@ -76,7 +76,7 @@ impl StatefulTransactionValidator {
optional_class_info,
&self.config.chain_info.chain_id,
)?;
let tx_hash = get_tx_hash(&account_tx);
let tx_hash = account_tx.tx_hash();
let sender_address = get_sender_address(&account_tx);
let account_nonce = validator.get_nonce(sender_address).map_err(|e| {
error!("Failed to get nonce for sender address {}: {}", sender_address, e);
Expand Down
9 changes: 0 additions & 9 deletions crates/gateway/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,6 @@ pub fn external_tx_to_account_tx(
}
}

// TODO(yael 9/5/54): Should be implemented as part of InternalTransaction in starknet-api
pub fn get_tx_hash(tx: &AccountTransaction) -> TransactionHash {
match tx {
AccountTransaction::Declare(tx) => tx.tx_hash,
AccountTransaction::DeployAccount(tx) => tx.tx_hash,
AccountTransaction::Invoke(tx) => tx.tx_hash,
}
}

// TODO(yael 9/5/54): Should be implemented as part of InternalTransaction in starknet-api
pub fn get_sender_address(tx: &AccountTransaction) -> ContractAddress {
match tx {
Expand Down

0 comments on commit 7082e88

Please sign in to comment.