From ca92528666aafbd08f1e1f0bcf50cf305ef12635 Mon Sep 17 00:00:00 2001 From: Aviv Greenburg Date: Sun, 17 Nov 2024 12:47:50 +0200 Subject: [PATCH] chore(blockifier): remove duplicacy tip() and resource_bounds() in AccountTransaction --- .../src/transaction/account_transaction.rs | 15 +++++- .../src/executable_transaction.rs | 54 ++++++------------- crates/starknet_mempool/src/mempool.rs | 3 +- 3 files changed, 31 insertions(+), 41 deletions(-) diff --git a/crates/blockifier/src/transaction/account_transaction.rs b/crates/blockifier/src/transaction/account_transaction.rs index bc2ab3ececf..39244fe4e03 100644 --- a/crates/blockifier/src/transaction/account_transaction.rs +++ b/crates/blockifier/src/transaction/account_transaction.rs @@ -91,6 +91,14 @@ pub struct AccountTransaction { only_query: bool, } +macro_rules! implement_tx_getter_calls { + ($(($field:ident, $field_type:ty)),*) => { + $(pub fn $field(&self) -> $field_type { + self.tx.$field() + })* +}; +} + macro_rules! implement_account_tx_inner_getters { ($(($field:ident, $field_type:ty)),*) => { $(pub fn $field(&self) -> $field_type { @@ -147,13 +155,16 @@ impl AccountTransaction { implement_account_tx_inner_getters!( (signature, TransactionSignature), (nonce, Nonce), - (resource_bounds, ValidResourceBounds), - (tip, Tip), (nonce_data_availability_mode, DataAvailabilityMode), (fee_data_availability_mode, DataAvailabilityMode), (paymaster_data, PaymasterData) ); + implement_tx_getter_calls!( + (resource_bounds, ValidResourceBounds), + (tip, Tip) + ); + pub fn new(tx: starknet_api::executable_transaction::AccountTransaction) -> Self { AccountTransaction { tx, only_query: false } } diff --git a/crates/starknet_api/src/executable_transaction.rs b/crates/starknet_api/src/executable_transaction.rs index c32621126fd..9dcfe607e24 100644 --- a/crates/starknet_api/src/executable_transaction.rs +++ b/crates/starknet_api/src/executable_transaction.rs @@ -39,6 +39,18 @@ macro_rules! implement_getter_calls { }; } +macro_rules! implement_account_tx_inner_getters { + ($(($field:ident, $field_type:ty)),*) => { + $(pub fn $field(&self) -> $field_type { + match self { + AccountTransaction::Declare(tx) => tx.tx.$field().clone(), + AccountTransaction::DeployAccount(tx) => tx.tx.$field().clone(), + AccountTransaction::Invoke(tx) => tx.tx.$field().clone(), + } + })* + }; +} + /// Represents a paid Starknet transaction. #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] pub enum AccountTransaction { @@ -48,6 +60,11 @@ pub enum AccountTransaction { } impl AccountTransaction { + implement_account_tx_inner_getters!( + (resource_bounds, ValidResourceBounds), + (tip, Tip) + ); + pub fn contract_address(&self) -> ContractAddress { match self { AccountTransaction::Declare(tx_data) => tx_data.tx.sender_address(), @@ -75,43 +92,6 @@ impl AccountTransaction { AccountTransaction::Invoke(tx_data) => tx_data.tx_hash, } } - - // TODO(Mohammad): add a getter macro. - pub fn tip(&self) -> Option { - match self { - AccountTransaction::Declare(declare_tx) => match &declare_tx.tx { - crate::transaction::DeclareTransaction::V3(tx_v3) => Some(tx_v3.tip), - _ => None, - }, - AccountTransaction::DeployAccount(deploy_account_tx) => match &deploy_account_tx.tx { - crate::transaction::DeployAccountTransaction::V3(tx_v3) => Some(tx_v3.tip), - _ => None, - }, - AccountTransaction::Invoke(invoke_tx) => match &invoke_tx.tx { - crate::transaction::InvokeTransaction::V3(tx_v3) => Some(tx_v3.tip), - _ => None, - }, - } - } - - pub fn resource_bounds(&self) -> Option<&ValidResourceBounds> { - match self { - AccountTransaction::Declare(declare_tx) => match &declare_tx.tx { - crate::transaction::DeclareTransaction::V3(tx_v3) => Some(&tx_v3.resource_bounds), - _ => None, - }, - AccountTransaction::DeployAccount(deploy_account_tx) => match &deploy_account_tx.tx { - crate::transaction::DeployAccountTransaction::V3(tx_v3) => { - Some(&tx_v3.resource_bounds) - } - _ => None, - }, - AccountTransaction::Invoke(invoke_tx) => match &invoke_tx.tx { - crate::transaction::InvokeTransaction::V3(tx_v3) => Some(&tx_v3.resource_bounds), - _ => None, - }, - } - } } // TODO: add a converter for Declare transactions as well. diff --git a/crates/starknet_mempool/src/mempool.rs b/crates/starknet_mempool/src/mempool.rs index d178c1398e5..48d0d442443 100644 --- a/crates/starknet_mempool/src/mempool.rs +++ b/crates/starknet_mempool/src/mempool.rs @@ -320,12 +320,11 @@ impl Mempool { // TODO(Elin): move to a shared location with other next-gen node crates. fn tip(tx: &AccountTransaction) -> Tip { - tx.tip().expect("Expected a valid tip value.") + tx.tip() } fn max_l2_gas_price(tx: &AccountTransaction) -> GasPrice { tx.resource_bounds() - .expect("Expected a valid resource bounds value.") .get_l2_bounds() .max_price_per_unit }