From 3cdd457afe37bfe829856058a450f556e07ca138 Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Mon, 12 Aug 2024 09:54:51 +0300 Subject: [PATCH] chore: streamline tx_hash getter method --- .../src/transaction/account_transaction.rs | 16 +++++++++++++++- .../blockifier/src/transaction/transactions.rs | 12 ++++++++++-- crates/gateway/src/gateway_test.rs | 4 ++-- .../src/stateful_transaction_validator.rs | 4 ++-- crates/gateway/src/utils.rs | 9 --------- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/crates/blockifier/src/transaction/account_transaction.rs b/crates/blockifier/src/transaction/account_transaction.rs index 1cc97e9ed1..2395274b70 100644 --- a/crates/blockifier/src/transaction/account_transaction.rs +++ b/crates/blockifier/src/transaction/account_transaction.rs @@ -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; @@ -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 ExecutableTransaction for AccountTransaction { diff --git a/crates/blockifier/src/transaction/transactions.rs b/crates/blockifier/src/transaction/transactions.rs index efbc867802..262acbb657 100644 --- a/crates/blockifier/src/transaction/transactions.rs +++ b/crates/blockifier/src/transaction/transactions.rs @@ -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 Executable for DeployAccountTransaction { @@ -379,7 +383,7 @@ impl Executable 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(), @@ -439,6 +443,10 @@ impl InvokeTransaction { (sender_address, ContractAddress), (version, TransactionVersion) ); + + pub fn tx_hash(&self) -> TransactionHash { + self.tx_hash + } } impl Executable for InvokeTransaction { @@ -487,7 +495,7 @@ impl Executable 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(), diff --git a/crates/gateway/src/gateway_test.rs b/crates/gateway/src/gateway_test.rs index d73c76ad46..3e9c993dfa 100644 --- a/crates/gateway/src/gateway_test.rs +++ b/crates/gateway/src/gateway_test.rs @@ -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, @@ -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() } diff --git a/crates/gateway/src/stateful_transaction_validator.rs b/crates/gateway/src/stateful_transaction_validator.rs index 6e140f1cf5..1e2a17cf68 100644 --- a/crates/gateway/src/stateful_transaction_validator.rs +++ b/crates/gateway/src/stateful_transaction_validator.rs @@ -19,7 +19,7 @@ use starknet_types_core::felt::Felt; use crate::config::StatefulTransactionValidatorConfig; use crate::errors::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"] @@ -75,7 +75,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)?; let skip_validate = skip_stateful_validations(external_tx, account_nonce); diff --git a/crates/gateway/src/utils.rs b/crates/gateway/src/utils.rs index 2823f00098..ee13827268 100644 --- a/crates/gateway/src/utils.rs +++ b/crates/gateway/src/utils.rs @@ -115,15 +115,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 {