From 81ec384dc04e99469080521fd02107349d37cafe Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Wed, 31 Jul 2024 16:20:03 +0300 Subject: [PATCH] refactor: use deref in account tx for removal of builerplate --- .../src/transaction/account_transaction.rs | 6 +- .../src/transaction/transactions.rs | 92 ++++++++++--------- .../src/transaction/transactions_test.rs | 2 +- 3 files changed, 54 insertions(+), 46 deletions(-) diff --git a/crates/blockifier/src/transaction/account_transaction.rs b/crates/blockifier/src/transaction/account_transaction.rs index 40be142f57..635759e572 100644 --- a/crates/blockifier/src/transaction/account_transaction.rs +++ b/crates/blockifier/src/transaction/account_transaction.rs @@ -73,9 +73,9 @@ pub enum AccountTransaction { impl HasRelatedFeeType for AccountTransaction { fn version(&self) -> TransactionVersion { match self { - Self::Declare(tx) => tx.tx.version(), - Self::DeployAccount(tx) => tx.tx.version(), - Self::Invoke(tx) => tx.tx.version(), + Self::Declare(tx) => tx.version(), + Self::DeployAccount(tx) => tx.version(), + Self::Invoke(tx) => tx.version(), } } diff --git a/crates/blockifier/src/transaction/transactions.rs b/crates/blockifier/src/transaction/transactions.rs index 834660c740..1f38cfc018 100644 --- a/crates/blockifier/src/transaction/transactions.rs +++ b/crates/blockifier/src/transaction/transactions.rs @@ -1,12 +1,12 @@ +use std::ops::Deref; use std::sync::Arc; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; -use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; +use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress}; use starknet_api::deprecated_contract_class::EntryPointType; use starknet_api::transaction::{ AccountDeploymentData, Calldata, - ContractAddressSalt, DeclareTransactionV2, DeclareTransactionV3, Fee, @@ -47,14 +47,6 @@ use crate::transaction::transaction_utils::{update_remaining_gas, verify_contrac #[path = "transactions_test.rs"] mod test; -macro_rules! implement_inner_tx_getter_calls { - ($(($field:ident, $field_type:ty)),*) => { - $(pub fn $field(&self) -> $field_type { - self.tx.$field().clone() - })* - }; -} - #[derive(Clone, Copy, Debug)] pub struct ExecutionFlags { pub charge_fee: bool, @@ -135,6 +127,14 @@ pub struct DeclareTransaction { pub class_info: ClassInfo, } +impl Deref for DeclareTransaction { + type Target = starknet_api::transaction::DeclareTransaction; + + fn deref(&self) -> &Self::Target { + &self.tx + } +} + impl DeclareTransaction { fn create( declare_tx: starknet_api::transaction::DeclareTransaction, @@ -163,8 +163,6 @@ impl DeclareTransaction { Self::create(declare_tx, tx_hash, class_info, true) } - implement_inner_tx_getter_calls!((class_hash, ClassHash), (signature, TransactionSignature)); - pub fn tx(&self) -> &starknet_api::transaction::DeclareTransaction { &self.tx } @@ -245,10 +243,10 @@ impl TransactionInfoCreator for DeclareTransaction { // TODO(Nir, 01/11/2023): Consider to move this (from all get_tx_info methods). let common_fields = CommonAccountFields { transaction_hash: self.tx_hash(), - version: self.tx.version(), - signature: self.tx.signature(), - nonce: self.tx.nonce(), - sender_address: self.tx.sender_address(), + version: self.version(), + signature: self.signature(), + nonce: self.nonce(), + sender_address: self.sender_address(), only_query: self.only_query, }; @@ -289,6 +287,14 @@ pub struct DeployAccountTransaction { pub only_query: bool, } +impl Deref for DeployAccountTransaction { + type Target = starknet_api::transaction::DeployAccountTransaction; + + fn deref(&self) -> &Self::Target { + &self.tx + } +} + impl DeployAccountTransaction { pub fn new( deploy_account_tx: starknet_api::transaction::DeployAccountTransaction, @@ -306,14 +312,6 @@ impl DeployAccountTransaction { Self { tx: deploy_account_tx, tx_hash, contract_address, only_query: true } } - implement_inner_tx_getter_calls!( - (class_hash, ClassHash), - (constructor_calldata, Calldata), - (contract_address_salt, ContractAddressSalt), - (nonce, Nonce), - (signature, TransactionSignature) - ); - pub fn tx(&self) -> &starknet_api::transaction::DeployAccountTransaction { &self.tx } @@ -352,9 +350,9 @@ impl TransactionInfoCreator for DeployAccountTransaction { fn create_tx_info(&self) -> TransactionInfo { let common_fields = CommonAccountFields { transaction_hash: self.tx_hash, - version: self.tx.version(), - signature: self.tx.signature(), - nonce: self.tx.nonce(), + version: self.version(), + signature: self.signature(), + nonce: self.nonce(), sender_address: self.contract_address, only_query: self.only_query, }; @@ -389,6 +387,14 @@ pub struct InvokeTransaction { pub only_query: bool, } +impl Deref for InvokeTransaction { + type Target = starknet_api::transaction::InvokeTransaction; + + fn deref(&self) -> &Self::Target { + &self.tx + } +} + impl InvokeTransaction { pub fn new( invoke_tx: starknet_api::transaction::InvokeTransaction, @@ -403,12 +409,6 @@ impl InvokeTransaction { ) -> Self { Self { tx: invoke_tx, tx_hash, only_query: true } } - - implement_inner_tx_getter_calls!( - (calldata, Calldata), - (signature, TransactionSignature), - (sender_address, ContractAddress) - ); } impl Executable for InvokeTransaction { @@ -458,10 +458,10 @@ impl TransactionInfoCreator for InvokeTransaction { fn create_tx_info(&self) -> TransactionInfo { let common_fields = CommonAccountFields { transaction_hash: self.tx_hash, - version: self.tx.version(), - signature: self.tx.signature(), - nonce: self.tx.nonce(), - sender_address: self.tx.sender_address(), + version: self.version(), + signature: self.signature(), + nonce: self.nonce(), + sender_address: self.sender_address(), only_query: self.only_query, }; @@ -500,16 +500,24 @@ pub struct L1HandlerTransaction { pub paid_fee_on_l1: Fee, } +impl Deref for L1HandlerTransaction { + type Target = starknet_api::transaction::L1HandlerTransaction; + + fn deref(&self) -> &Self::Target { + &self.tx + } +} + impl L1HandlerTransaction { pub fn payload_size(&self) -> usize { // The calldata includes the "from" field, which is not a part of the payload. - self.tx.calldata.0.len() - 1 + self.calldata.0.len() - 1 } } impl HasRelatedFeeType for L1HandlerTransaction { fn version(&self) -> TransactionVersion { - self.tx.version + self.version } fn is_l1_handler(&self) -> bool { @@ -557,10 +565,10 @@ impl TransactionInfoCreator for L1HandlerTransaction { TransactionInfo::Deprecated(DeprecatedTransactionInfo { common_fields: CommonAccountFields { transaction_hash: self.tx_hash, - version: self.tx.version, + version: self.version, signature: TransactionSignature::default(), - nonce: self.tx.nonce, - sender_address: self.tx.contract_address, + nonce: self.nonce, + sender_address: self.contract_address, only_query: false, }, max_fee: Fee::default(), diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index 6e7ef6058c..50108104fb 100644 --- a/crates/blockifier/src/transaction/transactions_test.rs +++ b/crates/blockifier/src/transaction/transactions_test.rs @@ -1827,7 +1827,7 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) { let contract_address = test_contract.get_instance_address(0); let versioned_constants = &block_context.versioned_constants; let tx = L1HandlerTransaction::create_for_testing(Fee(1), contract_address); - let calldata = tx.tx.calldata.clone(); + let calldata = tx.calldata.clone(); let key = calldata.0[1]; let value = calldata.0[2]; let payload_size = tx.payload_size();