From 17fc2becb2c11eb7fa90ff67d435290012460f01 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 | 12 ++++- .../src/executable_transaction.rs | 51 +++++-------------- crates/starknet_mempool/src/mempool.rs | 7 +-- 3 files changed, 26 insertions(+), 44 deletions(-) diff --git a/crates/blockifier/src/transaction/account_transaction.rs b/crates/blockifier/src/transaction/account_transaction.rs index 7305411a83..85c5916e0e 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 { @@ -144,11 +152,11 @@ impl HasRelatedFeeType for AccountTransaction { } impl AccountTransaction { + implement_tx_getter_calls!((resource_bounds, ValidResourceBounds), (tip, Tip)); + 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) diff --git a/crates/starknet_api/src/executable_transaction.rs b/crates/starknet_api/src/executable_transaction.rs index c32621126f..ec0a3557fa 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,8 @@ 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 +89,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 d24d0f5d9c..401e56b282 100644 --- a/crates/starknet_mempool/src/mempool.rs +++ b/crates/starknet_mempool/src/mempool.rs @@ -326,14 +326,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 + tx.resource_bounds().get_l2_bounds().max_price_per_unit } /// Provides a lightweight representation of a transaction for mempool usage (e.g., excluding