From 20845272bcb2c7c7bf9caa0347859febbecb2da1 Mon Sep 17 00:00:00 2001 From: Yair <92672946+yair-starkware@users.noreply.github.com> Date: Thu, 7 Nov 2024 13:14:04 +0200 Subject: [PATCH] refactor: wrap executable tx in account/l1handler enum (#1788) --- crates/batcher/src/block_builder.rs | 5 +-- crates/batcher/src/test_utils.rs | 6 +-- crates/batcher/src/transaction_provider.rs | 9 ++++- .../src/transaction/account_transaction.rs | 40 +++++++++---------- .../src/transaction/transaction_execution.rs | 16 ++++++++ crates/gateway/src/gateway.rs | 4 +- crates/gateway/src/gateway_test.rs | 4 +- .../src/stateful_transaction_validator.rs | 2 +- .../stateful_transaction_validator_test.rs | 14 +++---- crates/gateway/src/utils.rs | 2 +- crates/mempool/src/communication.rs | 14 +++---- crates/mempool/src/mempool.rs | 12 +++--- crates/mempool/src/mempool_test.rs | 2 +- crates/mempool/src/test_utils.rs | 8 ++-- crates/mempool/src/transaction_pool.rs | 10 ++--- .../src/starknet_api_test_utils.rs | 10 +++-- crates/mempool_types/src/communication.rs | 10 ++--- crates/mempool_types/src/mempool_types.rs | 4 +- .../src/sequencer_consensus_context_test.rs | 6 +-- .../src/executable_transaction.rs | 20 ++++++++-- 20 files changed, 117 insertions(+), 81 deletions(-) diff --git a/crates/batcher/src/block_builder.rs b/crates/batcher/src/block_builder.rs index a18a3f64dd..fe827b6e2f 100644 --- a/crates/batcher/src/block_builder.rs +++ b/crates/batcher/src/block_builder.rs @@ -14,7 +14,6 @@ use blockifier::context::{BlockContext, ChainInfo}; use blockifier::state::cached_state::CommitmentStateDiff; use blockifier::state::errors::StateError; use blockifier::state::global_cache::GlobalContractCache; -use blockifier::transaction::account_transaction::AccountTransaction; use blockifier::transaction::errors::TransactionExecutionError as BlockifierTransactionExecutionError; use blockifier::transaction::objects::TransactionExecutionInfo; use blockifier::transaction::transaction_execution::Transaction as BlockifierTransaction; @@ -163,8 +162,8 @@ impl BlockBuilderTrait for BlockBuilder { let mut executor_input_chunk = vec![]; for tx in &next_tx_chunk { - executor_input_chunk - .push(BlockifierTransaction::Account(AccountTransaction::try_from(tx)?)); + // TODO(yair): Avoid this clone. + executor_input_chunk.push(BlockifierTransaction::try_from(tx.clone())?); } let results = self.executor.lock().await.add_txs_to_block(&executor_input_chunk); trace!("Transaction execution results: {:?}", results); diff --git a/crates/batcher/src/test_utils.rs b/crates/batcher/src/test_utils.rs index 3f4dab3f32..e6b8183638 100644 --- a/crates/batcher/src/test_utils.rs +++ b/crates/batcher/src/test_utils.rs @@ -4,7 +4,7 @@ use blockifier::blockifier::transaction_executor::VisitedSegmentsMapping; use blockifier::bouncer::BouncerWeights; use blockifier::state::cached_state::CommitmentStateDiff; use indexmap::IndexMap; -use starknet_api::executable_transaction::Transaction; +use starknet_api::executable_transaction::{AccountTransaction, Transaction}; use starknet_api::felt; use starknet_api::test_utils::invoke::{executable_invoke_tx, InvokeTxArgs}; use starknet_api::transaction::TransactionHash; @@ -14,10 +14,10 @@ use crate::block_builder::BlockExecutionArtifacts; pub fn test_txs(tx_hash_range: Range) -> Vec { tx_hash_range .map(|i| { - Transaction::Invoke(executable_invoke_tx(InvokeTxArgs { + Transaction::Account(AccountTransaction::Invoke(executable_invoke_tx(InvokeTxArgs { tx_hash: TransactionHash(felt!(u128::try_from(i).unwrap())), ..Default::default() - })) + }))) }) .collect() } diff --git a/crates/batcher/src/transaction_provider.rs b/crates/batcher/src/transaction_provider.rs index e2732d35ff..a0d1751e7e 100644 --- a/crates/batcher/src/transaction_provider.rs +++ b/crates/batcher/src/transaction_provider.rs @@ -31,7 +31,14 @@ pub struct ProposeTransactionProvider { impl TransactionProvider for ProposeTransactionProvider { async fn get_txs(&mut self, n_txs: usize) -> Result { // TODO: Get also L1 transactions. - Ok(NextTxs::Txs(self.mempool_client.get_txs(n_txs).await?)) + Ok(NextTxs::Txs( + self.mempool_client + .get_txs(n_txs) + .await? + .into_iter() + .map(Transaction::Account) + .collect(), + )) } } diff --git a/crates/blockifier/src/transaction/account_transaction.rs b/crates/blockifier/src/transaction/account_transaction.rs index 76bad0b168..09335d4fa2 100644 --- a/crates/blockifier/src/transaction/account_transaction.rs +++ b/crates/blockifier/src/transaction/account_transaction.rs @@ -96,46 +96,46 @@ macro_rules! implement_account_tx_inner_getters { }; } -impl TryFrom<&starknet_api::executable_transaction::Transaction> for AccountTransaction { +impl TryFrom<&starknet_api::executable_transaction::AccountTransaction> for AccountTransaction { type Error = TransactionExecutionError; fn try_from( - value: &starknet_api::executable_transaction::Transaction, + value: &starknet_api::executable_transaction::AccountTransaction, ) -> Result { match value { - starknet_api::executable_transaction::Transaction::Declare(declare_tx) => { + starknet_api::executable_transaction::AccountTransaction::Declare(declare_tx) => { Ok(Self::Declare(declare_tx.clone().try_into()?)) } - starknet_api::executable_transaction::Transaction::DeployAccount(deploy_account_tx) => { - Ok(Self::DeployAccount(DeployAccountTransaction { - tx: deploy_account_tx.clone(), - only_query: false, - })) - } - starknet_api::executable_transaction::Transaction::Invoke(invoke_tx) => { + starknet_api::executable_transaction::AccountTransaction::DeployAccount( + deploy_account_tx, + ) => Ok(Self::DeployAccount(DeployAccountTransaction { + tx: deploy_account_tx.clone(), + only_query: false, + })), + starknet_api::executable_transaction::AccountTransaction::Invoke(invoke_tx) => { Ok(Self::Invoke(InvokeTransaction { tx: invoke_tx.clone(), only_query: false })) } } } } -impl TryFrom for AccountTransaction { +impl TryFrom for AccountTransaction { type Error = TransactionExecutionError; fn try_from( - executable_transaction: starknet_api::executable_transaction::Transaction, + executable_transaction: starknet_api::executable_transaction::AccountTransaction, ) -> Result { match executable_transaction { - starknet_api::executable_transaction::Transaction::Declare(declare_tx) => { + starknet_api::executable_transaction::AccountTransaction::Declare(declare_tx) => { Ok(Self::Declare(declare_tx.try_into()?)) } - starknet_api::executable_transaction::Transaction::DeployAccount(deploy_account_tx) => { - Ok(Self::DeployAccount(DeployAccountTransaction { - tx: deploy_account_tx, - only_query: false, - })) - } - starknet_api::executable_transaction::Transaction::Invoke(invoke_tx) => { + starknet_api::executable_transaction::AccountTransaction::DeployAccount( + deploy_account_tx, + ) => Ok(Self::DeployAccount(DeployAccountTransaction { + tx: deploy_account_tx, + only_query: false, + })), + starknet_api::executable_transaction::AccountTransaction::Invoke(invoke_tx) => { Ok(Self::Invoke(InvokeTransaction { tx: invoke_tx, only_query: false })) } } diff --git a/crates/blockifier/src/transaction/transaction_execution.rs b/crates/blockifier/src/transaction/transaction_execution.rs index 64ca5b5901..2b148de9a2 100644 --- a/crates/blockifier/src/transaction/transaction_execution.rs +++ b/crates/blockifier/src/transaction/transaction_execution.rs @@ -37,6 +37,22 @@ pub enum Transaction { L1Handler(L1HandlerTransaction), } +impl TryFrom for Transaction { + type Error = super::errors::TransactionExecutionError; + fn try_from( + value: starknet_api::executable_transaction::Transaction, + ) -> Result { + match value { + starknet_api::executable_transaction::Transaction::Account(tx) => { + Ok(Transaction::Account(tx.try_into()?)) + } + starknet_api::executable_transaction::Transaction::L1Handler(tx) => { + Ok(Transaction::L1Handler(tx)) + } + } + } +} + impl Transaction { pub fn nonce(&self) -> Nonce { match self { diff --git a/crates/gateway/src/gateway.rs b/crates/gateway/src/gateway.rs index 29b1601215..b5bedb2589 100644 --- a/crates/gateway/src/gateway.rs +++ b/crates/gateway/src/gateway.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use blockifier::context::ChainInfo; use papyrus_network_types::network_types::BroadcastedMessageMetadata; -use starknet_api::executable_transaction::Transaction; +use starknet_api::executable_transaction::AccountTransaction; use starknet_api::rpc_transaction::RpcTransaction; use starknet_api::transaction::TransactionHash; use starknet_gateway_types::errors::GatewaySpecError; @@ -127,7 +127,7 @@ fn process_tx( compile_contract_and_build_executable_tx(tx, &gateway_compiler, &chain_info.chain_id)?; // Perfom post compilation validations. - if let Transaction::Declare(executable_declare_tx) = &executable_tx { + if let AccountTransaction::Declare(executable_declare_tx) = &executable_tx { if !executable_declare_tx.validate_compiled_class_hash() { return Err(GatewaySpecError::CompiledClassHashMismatch); } diff --git a/crates/gateway/src/gateway_test.rs b/crates/gateway/src/gateway_test.rs index 3a587ad8b1..ae7675f4ce 100644 --- a/crates/gateway/src/gateway_test.rs +++ b/crates/gateway/src/gateway_test.rs @@ -8,7 +8,7 @@ use mockall::predicate::eq; use papyrus_network_types::network_types::BroadcastedMessageMetadata; use papyrus_test_utils::{get_rng, GetTestInstance}; use starknet_api::core::{ChainId, CompiledClassHash, ContractAddress}; -use starknet_api::executable_transaction::{InvokeTransaction, Transaction}; +use starknet_api::executable_transaction::{AccountTransaction, InvokeTransaction}; use starknet_api::rpc_transaction::{RpcDeclareTransaction, RpcTransaction}; use starknet_gateway_types::errors::GatewaySpecError; use starknet_mempool_types::communication::{AddTransactionArgsWrapper, MockMempoolClient}; @@ -61,7 +61,7 @@ async fn test_add_tx() { let (rpc_tx, address) = create_tx(); let rpc_invoke_tx = assert_matches!(rpc_tx.clone(), RpcTransaction::Invoke(rpc_invoke_tx) => rpc_invoke_tx); - let executable_tx = Transaction::Invoke( + let executable_tx = AccountTransaction::Invoke( InvokeTransaction::from_rpc_tx(rpc_invoke_tx, &ChainId::create_for_testing()).unwrap(), ); diff --git a/crates/gateway/src/stateful_transaction_validator.rs b/crates/gateway/src/stateful_transaction_validator.rs index 51694eb868..6384ab039d 100644 --- a/crates/gateway/src/stateful_transaction_validator.rs +++ b/crates/gateway/src/stateful_transaction_validator.rs @@ -12,8 +12,8 @@ use blockifier::versioned_constants::VersionedConstants; use mockall::automock; use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::executable_transaction::{ + AccountTransaction as ExecutableTransaction, InvokeTransaction as ExecutableInvokeTransaction, - Transaction as ExecutableTransaction, }; use starknet_gateway_types::errors::GatewaySpecError; use starknet_types_core::felt::Felt; diff --git a/crates/gateway/src/stateful_transaction_validator_test.rs b/crates/gateway/src/stateful_transaction_validator_test.rs index 4a6b09cf02..dbdade5a4f 100644 --- a/crates/gateway/src/stateful_transaction_validator_test.rs +++ b/crates/gateway/src/stateful_transaction_validator_test.rs @@ -17,7 +17,7 @@ use pretty_assertions::assert_eq; use rstest::{fixture, rstest}; use starknet_api::block::GasPrice; use starknet_api::core::Nonce; -use starknet_api::executable_transaction::Transaction; +use starknet_api::executable_transaction::AccountTransaction; use starknet_api::execution_resources::GasAmount; use starknet_api::test_utils::deploy_account::executable_deploy_account_tx; use starknet_api::test_utils::invoke::executable_invoke_tx; @@ -61,7 +61,7 @@ fn stateful_validator() -> StatefulTransactionValidator { Err(STATEFUL_VALIDATOR_FEE_ERROR) )] fn test_stateful_tx_validator( - #[case] executable_tx: Transaction, + #[case] executable_tx: AccountTransaction, #[case] expected_result: BlockifierStatefulValidatorResult<()>, stateful_validator: StatefulTransactionValidator, ) { @@ -108,23 +108,23 @@ fn test_instantiate_validator(stateful_validator: StatefulTransactionValidator) #[rstest] #[case::should_skip_validation( - Transaction::Invoke(executable_invoke_tx(invoke_tx_args!(nonce: nonce!(1)))), + AccountTransaction::Invoke(executable_invoke_tx(invoke_tx_args!(nonce: nonce!(1)))), nonce!(0), true )] #[case::should_not_skip_validation_nonce_over_max_nonce_for_skip( - Transaction::Invoke(executable_invoke_tx(invoke_tx_args!(nonce: nonce!(0)))), + AccountTransaction::Invoke(executable_invoke_tx(invoke_tx_args!(nonce: nonce!(0)))), nonce!(0), false )] #[case::should_not_skip_validation_non_invoke( - Transaction::DeployAccount( + AccountTransaction::DeployAccount( executable_deploy_account_tx(deploy_account_tx_args!(), nonce!(0)) ), nonce!(0), false)] #[case::should_not_skip_validation_account_nonce_1( - Transaction::Invoke(executable_invoke_tx( + AccountTransaction::Invoke(executable_invoke_tx( invoke_tx_args!( nonce: nonce!(1), sender_address: TEST_SENDER_ADDRESS.into() @@ -134,7 +134,7 @@ fn test_instantiate_validator(stateful_validator: StatefulTransactionValidator) false )] fn test_skip_stateful_validation( - #[case] executable_tx: Transaction, + #[case] executable_tx: AccountTransaction, #[case] sender_nonce: Nonce, #[case] should_skip_validate: bool, stateful_validator: StatefulTransactionValidator, diff --git a/crates/gateway/src/utils.rs b/crates/gateway/src/utils.rs index 5c0cfe0a95..bd6f81405f 100644 --- a/crates/gateway/src/utils.rs +++ b/crates/gateway/src/utils.rs @@ -1,9 +1,9 @@ use starknet_api::core::ChainId; use starknet_api::executable_transaction::{ + AccountTransaction as ExecutableTransaction, DeclareTransaction as ExecutableDeclareTransaction, DeployAccountTransaction as ExecutableDeployAccountTransaction, InvokeTransaction as ExecutableInvokeTransaction, - Transaction as ExecutableTransaction, }; use starknet_api::rpc_transaction::{RpcDeclareTransaction, RpcTransaction}; use starknet_gateway_types::errors::GatewaySpecError; diff --git a/crates/mempool/src/communication.rs b/crates/mempool/src/communication.rs index abe8e7ad14..b80bb8aab8 100644 --- a/crates/mempool/src/communication.rs +++ b/crates/mempool/src/communication.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; use papyrus_network_types::network_types::BroadcastedMessageMetadata; -use starknet_api::executable_transaction::Transaction; +use starknet_api::executable_transaction::AccountTransaction; use starknet_api::rpc_transaction::{ RpcDeployAccountTransaction, RpcInvokeTransaction, @@ -45,7 +45,7 @@ impl MempoolCommunicationWrapper { async fn send_tx_to_p2p( &self, message_metadata: Option, - tx: Transaction, + tx: AccountTransaction, ) -> MempoolResult<()> { match message_metadata { Some(message_metadata) => self @@ -56,21 +56,21 @@ impl MempoolCommunicationWrapper { None => { let tx_hash = tx.tx_hash(); match tx { - Transaction::Invoke(invoke_tx) => self + AccountTransaction::Invoke(invoke_tx) => self .mempool_p2p_propagator_client .add_transaction(RpcTransaction::Invoke(RpcInvokeTransaction::V3( invoke_tx.into(), ))) .await .map_err(|_| MempoolError::P2pPropagatorClientError { tx_hash })?, - Transaction::DeployAccount(deploy_account_tx) => self + AccountTransaction::DeployAccount(deploy_account_tx) => self .mempool_p2p_propagator_client .add_transaction(RpcTransaction::DeployAccount( RpcDeployAccountTransaction::V3(deploy_account_tx.into()), )) .await .map_err(|_| MempoolError::P2pPropagatorClientError { tx_hash })?, - Transaction::Declare(_) => {} + AccountTransaction::Declare(_) => {} } Ok(()) } @@ -82,7 +82,7 @@ impl MempoolCommunicationWrapper { // TODO: Verify that only transactions that were added to the mempool are sent. // TODO: handle declare correctly and remove this match. match args_wrapper.args.tx { - Transaction::Declare(_) => Ok(()), + AccountTransaction::Declare(_) => Ok(()), _ => self.send_tx_to_p2p(args_wrapper.p2p_message_metadata, args_wrapper.args.tx).await, } } @@ -91,7 +91,7 @@ impl MempoolCommunicationWrapper { self.mempool.commit_block(args) } - fn get_txs(&mut self, n_txs: usize) -> MempoolResult> { + fn get_txs(&mut self, n_txs: usize) -> MempoolResult> { self.mempool.get_txs(n_txs) } } diff --git a/crates/mempool/src/mempool.rs b/crates/mempool/src/mempool.rs index 6b691dc326..695d3f73f9 100644 --- a/crates/mempool/src/mempool.rs +++ b/crates/mempool/src/mempool.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use starknet_api::block::GasPrice; use starknet_api::core::{ContractAddress, Nonce}; -use starknet_api::executable_transaction::Transaction; +use starknet_api::executable_transaction::AccountTransaction; use starknet_api::transaction::{Tip, TransactionHash}; use starknet_mempool_types::errors::MempoolError; use starknet_mempool_types::mempool_types::{ @@ -62,7 +62,7 @@ impl Mempool { /// created. // TODO: Consider renaming to `pop_txs` to be more consistent with the standard library. #[tracing::instrument(skip(self), err)] - pub fn get_txs(&mut self, n_txs: usize) -> MempoolResult> { + pub fn get_txs(&mut self, n_txs: usize) -> MempoolResult> { let mut eligible_tx_references: Vec = Vec::with_capacity(n_txs); let mut n_remaining_txs = n_txs; @@ -252,7 +252,7 @@ impl Mempool { } #[tracing::instrument(level = "debug", skip(self, incoming_tx), err)] - fn handle_fee_escalation(&mut self, incoming_tx: &Transaction) -> MempoolResult<()> { + fn handle_fee_escalation(&mut self, incoming_tx: &AccountTransaction) -> MempoolResult<()> { if !self.config.enable_fee_escalation { return Ok(()); } @@ -314,11 +314,11 @@ impl Mempool { } // TODO(Elin): move to a shared location with other next-gen node crates. -fn tip(tx: &Transaction) -> Tip { +fn tip(tx: &AccountTransaction) -> Tip { tx.tip().expect("Expected a valid tip value.") } -fn max_l2_gas_price(tx: &Transaction) -> GasPrice { +fn max_l2_gas_price(tx: &AccountTransaction) -> GasPrice { tx.resource_bounds() .expect("Expected a valid resource bounds value.") .get_l2_bounds() @@ -339,7 +339,7 @@ pub struct TransactionReference { } impl TransactionReference { - pub fn new(tx: &Transaction) -> Self { + pub fn new(tx: &AccountTransaction) -> Self { TransactionReference { address: tx.contract_address(), nonce: tx.nonce(), diff --git a/crates/mempool/src/mempool_test.rs b/crates/mempool/src/mempool_test.rs index 73a41269f8..cec948c3e8 100644 --- a/crates/mempool/src/mempool_test.rs +++ b/crates/mempool/src/mempool_test.rs @@ -1,6 +1,6 @@ use pretty_assertions::assert_eq; use rstest::{fixture, rstest}; -use starknet_api::executable_transaction::Transaction; +use starknet_api::executable_transaction::AccountTransaction as Transaction; use starknet_api::{contract_address, nonce}; use starknet_mempool_types::errors::MempoolError; use starknet_mempool_types::mempool_types::AddTransactionArgs; diff --git a/crates/mempool/src/test_utils.rs b/crates/mempool/src/test_utils.rs index eef998e126..00f72a866d 100644 --- a/crates/mempool/src/test_utils.rs +++ b/crates/mempool/src/test_utils.rs @@ -1,7 +1,7 @@ use std::collections::{HashMap, HashSet}; use pretty_assertions::assert_eq; -use starknet_api::executable_transaction::Transaction; +use starknet_api::executable_transaction::AccountTransaction; use starknet_api::transaction::TransactionHash; use starknet_api::{contract_address, felt, nonce}; use starknet_mempool_types::errors::MempoolError; @@ -21,7 +21,7 @@ macro_rules! tx { max_l2_gas_price: $max_l2_gas_price:expr ) => {{ use starknet_api::block::GasPrice; - use starknet_api::executable_transaction::Transaction; + use starknet_api::executable_transaction::AccountTransaction; use starknet_api::hash::StarkHash; use starknet_api::invoke_tx_args; use starknet_api::test_utils::invoke::executable_invoke_tx; @@ -41,7 +41,7 @@ macro_rules! tx { ..Default::default() }); - Transaction::Invoke(executable_invoke_tx(invoke_tx_args!{ + AccountTransaction::Invoke(executable_invoke_tx(invoke_tx_args!{ tx_hash: TransactionHash(StarkHash::from($tx_hash)), sender_address: contract_address!($address), nonce: nonce!($tx_nonce), @@ -231,7 +231,7 @@ pub fn commit_block( pub fn get_txs_and_assert_expected( mempool: &mut Mempool, n_txs: usize, - expected_txs: &[Transaction], + expected_txs: &[AccountTransaction], ) { let txs = mempool.get_txs(n_txs).unwrap(); assert_eq!(txs, expected_txs); diff --git a/crates/mempool/src/transaction_pool.rs b/crates/mempool/src/transaction_pool.rs index da5b47ea24..26978a6114 100644 --- a/crates/mempool/src/transaction_pool.rs +++ b/crates/mempool/src/transaction_pool.rs @@ -1,7 +1,7 @@ use std::collections::{hash_map, BTreeMap, HashMap}; use starknet_api::core::{ContractAddress, Nonce}; -use starknet_api::executable_transaction::Transaction; +use starknet_api::executable_transaction::AccountTransaction; use starknet_api::transaction::TransactionHash; use starknet_mempool_types::errors::MempoolError; use starknet_mempool_types::mempool_types::{AccountState, MempoolResult}; @@ -9,7 +9,7 @@ use starknet_mempool_types::mempool_types::{AccountState, MempoolResult}; use crate::mempool::TransactionReference; use crate::utils::try_increment_nonce; -type HashToTransaction = HashMap; +type HashToTransaction = HashMap; /// Contains all transactions currently held in the mempool. /// Invariant: both data structures are consistent regarding the existence of transactions: @@ -26,7 +26,7 @@ pub struct TransactionPool { } impl TransactionPool { - pub fn insert(&mut self, tx: Transaction) -> MempoolResult<()> { + pub fn insert(&mut self, tx: AccountTransaction) -> MempoolResult<()> { let tx_reference = TransactionReference::new(&tx); let tx_hash = tx_reference.tx_hash; @@ -51,7 +51,7 @@ impl TransactionPool { Ok(()) } - pub fn remove(&mut self, tx_hash: TransactionHash) -> MempoolResult { + pub fn remove(&mut self, tx_hash: TransactionHash) -> MempoolResult { // Remove from pool. let tx = self.tx_pool.remove(&tx_hash).ok_or(MempoolError::TransactionNotFound { tx_hash })?; @@ -91,7 +91,7 @@ impl TransactionPool { self.txs_by_account.account_txs_sorted_by_nonce(address) } - pub fn get_by_tx_hash(&self, tx_hash: TransactionHash) -> MempoolResult<&Transaction> { + pub fn get_by_tx_hash(&self, tx_hash: TransactionHash) -> MempoolResult<&AccountTransaction> { self.tx_pool.get(&tx_hash).ok_or(MempoolError::TransactionNotFound { tx_hash }) } diff --git a/crates/mempool_test_utils/src/starknet_api_test_utils.rs b/crates/mempool_test_utils/src/starknet_api_test_utils.rs index c085f2247b..815912ac22 100644 --- a/crates/mempool_test_utils/src/starknet_api_test_utils.rs +++ b/crates/mempool_test_utils/src/starknet_api_test_utils.rs @@ -13,7 +13,7 @@ use serde_json::to_string_pretty; use starknet_api::block::GasPrice; use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; use starknet_api::data_availability::DataAvailabilityMode; -use starknet_api::executable_transaction::Transaction; +use starknet_api::executable_transaction::AccountTransaction; use starknet_api::execution_resources::GasAmount; use starknet_api::rpc_transaction::{ ContractClass, @@ -212,7 +212,7 @@ pub fn invoke_tx(cairo_version: CairoVersion) -> RpcTransaction { )) } -pub fn executable_invoke_tx(cairo_version: CairoVersion) -> Transaction { +pub fn executable_invoke_tx(cairo_version: CairoVersion) -> AccountTransaction { let default_account = FeatureContract::AccountWithoutValidations(cairo_version); let mut tx_generator = MultiAccountTransactionGenerator::new(); @@ -345,7 +345,7 @@ impl AccountTransactionGenerator { rpc_invoke_tx(invoke_args) } - pub fn generate_executable_invoke(&mut self) -> Transaction { + pub fn generate_executable_invoke(&mut self) -> AccountTransaction { let nonce = self.next_nonce(); assert_ne!( nonce, @@ -361,7 +361,9 @@ impl AccountTransactionGenerator { calldata: create_trivial_calldata(self.sender_address()), ); - Transaction::Invoke(starknet_api::test_utils::invoke::executable_invoke_tx(invoke_args)) + AccountTransaction::Invoke(starknet_api::test_utils::invoke::executable_invoke_tx( + invoke_args, + )) } /// Generates an `RpcTransaction` with fully custom parameters. diff --git a/crates/mempool_types/src/communication.rs b/crates/mempool_types/src/communication.rs index 8d48097e6c..0c1a27111d 100644 --- a/crates/mempool_types/src/communication.rs +++ b/crates/mempool_types/src/communication.rs @@ -6,7 +6,7 @@ use mockall::*; use papyrus_network_types::network_types::BroadcastedMessageMetadata; use papyrus_proc_macros::handle_response_variants; use serde::{Deserialize, Serialize}; -use starknet_api::executable_transaction::Transaction; +use starknet_api::executable_transaction::AccountTransaction; use starknet_sequencer_infra::component_client::{ ClientError, LocalComponentClient, @@ -41,7 +41,7 @@ pub trait MempoolClient: Send + Sync { // TODO: Rename tx to transaction async fn add_tx(&self, args: AddTransactionArgsWrapper) -> MempoolClientResult<()>; async fn commit_block(&self, args: CommitBlockArgs) -> MempoolClientResult<()>; - async fn get_txs(&self, n_txs: usize) -> MempoolClientResult>; + async fn get_txs(&self, n_txs: usize) -> MempoolClientResult>; } #[derive(Clone, Debug, Serialize, Deserialize)] @@ -55,7 +55,7 @@ pub enum MempoolRequest { pub enum MempoolResponse { AddTransaction(MempoolResult<()>), CommitBlock(MempoolResult<()>), - GetTransactions(MempoolResult>), + GetTransactions(MempoolResult>), } #[derive(Clone, Debug, Error)] @@ -80,7 +80,7 @@ impl MempoolClient for LocalMempoolClient { handle_response_variants!(MempoolResponse, CommitBlock, MempoolClientError, MempoolError) } - async fn get_txs(&self, n_txs: usize) -> MempoolClientResult> { + async fn get_txs(&self, n_txs: usize) -> MempoolClientResult> { let request = MempoolRequest::GetTransactions(n_txs); let response = self.send(request).await; handle_response_variants!( @@ -106,7 +106,7 @@ impl MempoolClient for RemoteMempoolClient { handle_response_variants!(MempoolResponse, CommitBlock, MempoolClientError, MempoolError) } - async fn get_txs(&self, n_txs: usize) -> MempoolClientResult> { + async fn get_txs(&self, n_txs: usize) -> MempoolClientResult> { let request = MempoolRequest::GetTransactions(n_txs); let response = self.send(request).await?; handle_response_variants!( diff --git a/crates/mempool_types/src/mempool_types.rs b/crates/mempool_types/src/mempool_types.rs index e9068a93f5..65199db6d6 100644 --- a/crates/mempool_types/src/mempool_types.rs +++ b/crates/mempool_types/src/mempool_types.rs @@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet}; use serde::{Deserialize, Serialize}; use starknet_api::core::{ContractAddress, Nonce}; -use starknet_api::executable_transaction::Transaction; +use starknet_api::executable_transaction::AccountTransaction; use starknet_api::transaction::TransactionHash; use crate::errors::MempoolError; @@ -23,7 +23,7 @@ impl std::fmt::Display for AccountState { #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct AddTransactionArgs { - pub tx: Transaction, + pub tx: AccountTransaction, pub account_state: AccountState, } diff --git a/crates/sequencing/papyrus_consensus_orchestrator/src/sequencer_consensus_context_test.rs b/crates/sequencing/papyrus_consensus_orchestrator/src/sequencer_consensus_context_test.rs index dea03b4000..3806295482 100644 --- a/crates/sequencing/papyrus_consensus_orchestrator/src/sequencer_consensus_context_test.rs +++ b/crates/sequencing/papyrus_consensus_orchestrator/src/sequencer_consensus_context_test.rs @@ -8,7 +8,7 @@ use lazy_static::lazy_static; use papyrus_consensus::types::{ConsensusContext, ProposalInit}; use starknet_api::block::{BlockHash, BlockNumber}; use starknet_api::core::{ContractAddress, StateDiffCommitment}; -use starknet_api::executable_transaction::Transaction; +use starknet_api::executable_transaction::{AccountTransaction, Transaction}; use starknet_api::hash::PoseidonHash; use starknet_api::test_utils::invoke::{executable_invoke_tx, InvokeTxArgs}; use starknet_api::transaction::TransactionHash; @@ -40,10 +40,10 @@ lazy_static! { } fn generate_invoke_tx(tx_hash: Felt) -> Transaction { - Transaction::Invoke(executable_invoke_tx(InvokeTxArgs { + Transaction::Account(AccountTransaction::Invoke(executable_invoke_tx(InvokeTxArgs { tx_hash: TransactionHash(tx_hash), ..Default::default() - })) + }))) } #[tokio::test] diff --git a/crates/starknet_api/src/executable_transaction.rs b/crates/starknet_api/src/executable_transaction.rs index da3c2530f1..45fdab0c24 100644 --- a/crates/starknet_api/src/executable_transaction.rs +++ b/crates/starknet_api/src/executable_transaction.rs @@ -41,9 +41,6 @@ macro_rules! implement_getter_calls { }; } -// TODO: Remove after introducing new transaction type. -pub type Transaction = AccountTransaction; - /// Represents a paid Starknet transaction. #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] pub enum AccountTransaction { @@ -295,7 +292,7 @@ impl InvokeTransaction { } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] pub struct L1HandlerTransaction { pub tx: crate::transaction::L1HandlerTransaction, pub tx_hash: TransactionHash, @@ -308,3 +305,18 @@ impl L1HandlerTransaction { self.tx.calldata.0.len() - 1 } } + +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] +pub enum Transaction { + Account(AccountTransaction), + L1Handler(L1HandlerTransaction), +} + +impl Transaction { + pub fn tx_hash(&self) -> TransactionHash { + match self { + Transaction::Account(tx) => tx.tx_hash(), + Transaction::L1Handler(tx) => tx.tx_hash, + } + } +}