Skip to content

Commit

Permalink
refactor(mempool): transaction pool stores internal transaction, API …
Browse files Browse the repository at this point in the history
…did not change
  • Loading branch information
MohammadNassar1 committed Aug 12, 2024
1 parent 6af5e47 commit c69f4be
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
13 changes: 11 additions & 2 deletions crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::executable_transaction::Transaction;
use starknet_api::transaction::{Tip, TransactionHash};
use starknet_mempool_types::errors::MempoolError;
use starknet_mempool_types::mempool_types::{
Expand Down Expand Up @@ -68,7 +69,7 @@ impl Mempool {
let mut eligible_txs: Vec<ThinTransaction> = Vec::with_capacity(n_txs);
for tx_ref in &eligible_tx_references {
let tx = self.tx_pool.remove(tx_ref.tx_hash)?;
eligible_txs.push(tx);
eligible_txs.push((&tx).into());
}

// Update the mempool state with the given transactions' nonces.
Expand Down Expand Up @@ -134,7 +135,7 @@ impl Mempool {
let MempoolInput { tx, account: Account { sender_address, state: AccountState { nonce } } } =
input;

self.tx_pool.insert(tx)?;
self.tx_pool.insert((&tx).into())?;

// Maybe close nonce gap.
if self.tx_queue.get_nonce(sender_address).is_none() {
Expand Down Expand Up @@ -229,4 +230,12 @@ impl TransactionReference {
tip: tx.tip,
}
}
pub fn new_from_transaction(tx: &Transaction) -> Self {
TransactionReference {
sender_address: tx.contract_address(),
nonce: tx.nonce(),
tx_hash: tx.tx_hash(),
tip: tx.tip().expect("Expected a valid tip value, but received None."),
}
}
}
2 changes: 1 addition & 1 deletion crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl FromIterator<ThinTransaction> for TransactionPool {
fn from_iter<T: IntoIterator<Item = ThinTransaction>>(txs: T) -> Self {
let mut pool = Self::default();
for tx in txs {
pool.insert(tx).unwrap();
pool.insert((&tx).into()).unwrap();
}
pool
}
Expand Down
20 changes: 8 additions & 12 deletions crates/mempool/src/transaction_pool.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use std::collections::{hash_map, BTreeMap, HashMap};

use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::executable_transaction::Transaction;
use starknet_api::transaction::TransactionHash;
use starknet_mempool_types::errors::MempoolError;
use starknet_mempool_types::mempool_types::{
Account,
AccountState,
MempoolResult,
ThinTransaction,
};
use starknet_mempool_types::mempool_types::{Account, AccountState, MempoolResult};

use crate::mempool::TransactionReference;

type HashToTransaction = HashMap<TransactionHash, ThinTransaction>;
type HashToTransaction = HashMap<TransactionHash, Transaction>;

/// Contains all transactions currently held in the mempool.
/// Invariant: both data structures are consistent regarding the existence of transactions:
Expand All @@ -27,8 +23,8 @@ pub struct TransactionPool {
}

impl TransactionPool {
pub fn insert(&mut self, tx: ThinTransaction) -> MempoolResult<()> {
let tx_reference = TransactionReference::new(&tx);
pub fn insert(&mut self, tx: Transaction) -> MempoolResult<()> {
let tx_reference = TransactionReference::new_from_transaction(&tx);
let tx_hash = tx_reference.tx_hash;

// Insert to pool.
Expand All @@ -50,13 +46,13 @@ impl TransactionPool {
Ok(())
}

pub fn remove(&mut self, tx_hash: TransactionHash) -> MempoolResult<ThinTransaction> {
pub fn remove(&mut self, tx_hash: TransactionHash) -> MempoolResult<Transaction> {
// Remove from pool.
let tx =
self.tx_pool.remove(&tx_hash).ok_or(MempoolError::TransactionNotFound { tx_hash })?;

// Remove from account mapping.
self.txs_by_account.remove(TransactionReference::new(&tx)).unwrap_or_else(|| {
self.txs_by_account.remove(TransactionReference::new(&(&tx).into())).unwrap_or_else(|| {
panic!(
"Transaction pool consistency error: transaction with hash {tx_hash} appears in \
main mapping, but does not appear in the account mapping"
Expand All @@ -79,7 +75,7 @@ impl TransactionPool {
}
}

pub fn _get_by_tx_hash(&self, tx_hash: TransactionHash) -> MempoolResult<&ThinTransaction> {
pub fn _get_by_tx_hash(&self, tx_hash: TransactionHash) -> MempoolResult<&Transaction> {
self.tx_pool.get(&tx_hash).ok_or(MempoolError::TransactionNotFound { tx_hash })
}

Expand Down
45 changes: 44 additions & 1 deletion crates/mempool_types/src/mempool_types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
use serde::{Deserialize, Serialize};
use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::transaction::{Tip, TransactionHash};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::executable_transaction::{InvokeTransaction, Transaction};
use starknet_api::transaction::{
AccountDeploymentData,
Calldata,
PaymasterData,
ResourceBoundsMapping,
Tip,
TransactionHash,
TransactionSignature,
};

use crate::errors::MempoolError;

Expand Down Expand Up @@ -32,3 +42,36 @@ pub struct MempoolInput {
}

pub type MempoolResult<T> = Result<T, MempoolError>;

impl From<&Transaction> for ThinTransaction {
fn from(tx: &Transaction) -> Self {
ThinTransaction {
sender_address: tx.contract_address(),
tx_hash: tx.tx_hash(),
tip: tx.tip().unwrap_or_default(),
nonce: tx.nonce(),
}
}
}

impl From<&ThinTransaction> for Transaction {
fn from(tx: &ThinTransaction) -> Self {
Transaction::Invoke(InvokeTransaction {
tx: starknet_api::transaction::InvokeTransaction::V3(
starknet_api::transaction::InvokeTransactionV3 {
sender_address: tx.sender_address,
tip: tx.tip,
nonce: tx.nonce,
resource_bounds: ResourceBoundsMapping::default(),
signature: TransactionSignature::default(),
calldata: Calldata::default(),
nonce_data_availability_mode: DataAvailabilityMode::L1,
fee_data_availability_mode: DataAvailabilityMode::L2,
paymaster_data: PaymasterData::default(),
account_deployment_data: AccountDeploymentData::default(),
},
),
tx_hash: tx.tx_hash,
})
}
}

0 comments on commit c69f4be

Please sign in to comment.