Skip to content

Commit

Permalink
refactor(mempool): change thin tx to full tx
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadNassar1 committed Aug 19, 2024
1 parent b6955bb commit fdd402c
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 104 deletions.
5 changes: 3 additions & 2 deletions crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async fn add_tx(
GatewaySpecError::UnexpectedError { data: "Internal server error".to_owned() }
})??;

let tx_hash = mempool_input.tx.tx_hash;
let tx_hash = mempool_input.tx.tx_hash();

app_state.mempool_client.add_tx(mempool_input).await.map_err(|e| {
error!("Failed to send tx to mempool: {}", e);
Expand Down Expand Up @@ -143,7 +143,8 @@ fn process_tx(

// TODO(Arni): Add the Sierra and the Casm to the mempool input.
Ok(MempoolInput {
tx: external_tx_to_thin_tx(&tx, validate_info.tx_hash, validate_info.sender_address),
tx: (&external_tx_to_thin_tx(&tx, validate_info.tx_hash, validate_info.sender_address))
.into(),
account: Account {
sender_address: validate_info.sender_address,
state: AccountState { nonce: validate_info.account_nonce },
Expand Down
3 changes: 2 additions & 1 deletion crates/gateway/src/gateway_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ async fn test_add_tx() {
.expect_add_tx()
.once()
.with(eq(MempoolInput {
tx: ThinTransaction { sender_address, tx_hash, tip: *tx.tip(), nonce: *tx.nonce() },
tx: (&ThinTransaction { sender_address, tx_hash, tip: *tx.tip(), nonce: *tx.nonce() })
.into(),
account: Account { sender_address, state: AccountState { nonce: *tx.nonce() } },
}))
.return_once(|_| Ok(()));
Expand Down
5 changes: 3 additions & 2 deletions crates/mempool/src/communication.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::net::IpAddr;

use async_trait::async_trait;
use starknet_api::executable_transaction::Transaction;
use starknet_mempool_infra::component_definitions::ComponentRequestHandler;
use starknet_mempool_infra::component_runner::ComponentStarter;
use starknet_mempool_infra::component_server::{LocalComponentServer, RemoteComponentServer};
Expand All @@ -9,7 +10,7 @@ use starknet_mempool_types::communication::{
MempoolRequestAndResponseSender,
MempoolResponse,
};
use starknet_mempool_types::mempool_types::{MempoolInput, MempoolResult, ThinTransaction};
use starknet_mempool_types::mempool_types::{MempoolInput, MempoolResult};
use tokio::sync::mpsc::Receiver;

use crate::mempool::Mempool;
Expand Down Expand Up @@ -51,7 +52,7 @@ impl MempoolCommunicationWrapper {
self.mempool.add_tx(mempool_input)
}

fn get_txs(&mut self, n_txs: usize) -> MempoolResult<Vec<ThinTransaction>> {
fn get_txs(&mut self, n_txs: usize) -> MempoolResult<Vec<Transaction>> {
self.mempool.get_txs(n_txs)
}
}
Expand Down
45 changes: 15 additions & 30 deletions crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ 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::{
Account,
AccountState,
MempoolInput,
MempoolResult,
ThinTransaction,
};
use starknet_mempool_types::mempool_types::{Account, AccountState, MempoolInput, MempoolResult};

use crate::transaction_pool::TransactionPool;
use crate::transaction_queue::TransactionQueue;
Expand Down Expand Up @@ -46,7 +40,7 @@ impl Mempool {
// TODO: the last part about commit_block is incorrect if we delete txs in get_txs and then push
// back. TODO: Consider renaming to `pop_txs` to be more consistent with the standard
// library.
pub fn get_txs(&mut self, n_txs: usize) -> MempoolResult<Vec<ThinTransaction>> {
pub fn get_txs(&mut self, n_txs: usize) -> MempoolResult<Vec<Transaction>> {
let mut eligible_tx_references: Vec<TransactionReference> = Vec::with_capacity(n_txs);
let mut n_remaining_txs = n_txs;

Expand All @@ -57,15 +51,15 @@ impl Mempool {
eligible_tx_references.extend(chunk);
}

let mut eligible_txs: Vec<ThinTransaction> = Vec::with_capacity(n_txs);
let mut eligible_txs: Vec<Transaction> = 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).into());
eligible_txs.push(tx);
}

// Update the mempool state with the given transactions' nonces.
for tx in &eligible_txs {
self.mempool_state.entry(tx.sender_address).or_default().nonce = tx.nonce;
self.mempool_state.entry(tx.contract_address()).or_default().nonce = tx.nonce();
}

Ok(eligible_txs)
Expand Down Expand Up @@ -129,7 +123,7 @@ impl Mempool {
// Remove transactions with lower nonce than the account nonce.
self.tx_pool.remove_up_to_nonce(sender_address, nonce);

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

// Maybe close nonce gap.
if self.tx_queue.get_nonce(sender_address).is_none() {
Expand All @@ -143,16 +137,16 @@ impl Mempool {
}

fn validate_input(&self, input: &MempoolInput) -> MempoolResult<()> {
let MempoolInput {
tx: ThinTransaction { sender_address, nonce: tx_nonce, .. },
account: Account { state: AccountState { nonce: account_nonce }, .. },
} = input;
let sender_address = input.tx.contract_address();
let tx_nonce = input.tx.nonce();
let duplicate_nonce_error =
MempoolError::DuplicateNonce { address: *sender_address, nonce: *tx_nonce };
MempoolError::DuplicateNonce { address: sender_address, nonce: tx_nonce };

// Stateless checks.

// Check the input: transaction nonce against given account state.
let account_nonce = input.account.state.nonce;

if account_nonce > tx_nonce {
return Err(duplicate_nonce_error);
}
Expand All @@ -161,18 +155,18 @@ impl Mempool {

// Check nonce against mempool state.
if let Some(AccountState { nonce: mempool_state_nonce }) =
self.mempool_state.get(sender_address)
self.mempool_state.get(&sender_address)
{
if mempool_state_nonce >= tx_nonce {
if mempool_state_nonce >= &tx_nonce {
return Err(duplicate_nonce_error);
}
}

// Check nonce against the queue.
if self
.tx_queue
.get_nonce(*sender_address)
.is_some_and(|queued_nonce| queued_nonce > *tx_nonce)
.get_nonce(sender_address)
.is_some_and(|queued_nonce| queued_nonce > tx_nonce)
{
return Err(duplicate_nonce_error);
}
Expand Down Expand Up @@ -216,15 +210,6 @@ pub struct TransactionReference {
}

impl TransactionReference {
pub fn new_from_thin_tx(tx: &ThinTransaction) -> Self {
TransactionReference {
sender_address: tx.sender_address,
nonce: tx.nonce,
tx_hash: tx.tx_hash,
tip: tx.tip,
}
}

pub fn new(tx: &Transaction) -> Self {
TransactionReference {
sender_address: tx.contract_address(),
Expand Down
Loading

0 comments on commit fdd402c

Please sign in to comment.