Skip to content

Commit

Permalink
feat(mempool): add resource bounds to transaction reference type
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadNassar1 committed Aug 20, 2024
1 parent 9343539 commit f1559fc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
18 changes: 12 additions & 6 deletions crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,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_api::transaction::{ResourceBoundsMapping, Tip, TransactionHash};
use starknet_mempool_types::errors::MempoolError;
use starknet_mempool_types::mempool_types::{Account, AccountState, MempoolInput, MempoolResult};

Expand Down Expand Up @@ -96,8 +96,10 @@ impl Mempool {
}

if self.tx_queue.get_nonce(address).is_none() {
if let Some(tx) = self.tx_pool.get_by_address_and_nonce(address, next_nonce) {
self.tx_queue.insert(*tx);
if let Some(tx_reference) =
self.tx_pool.get_by_address_and_nonce(address, next_nonce)
{
self.tx_queue.insert(tx_reference.clone());
}
}

Expand Down Expand Up @@ -133,7 +135,7 @@ impl Mempool {
if self.tx_queue.get_nonce(sender_address).is_none() {
if let Some(tx_reference) = self.tx_pool.get_by_address_and_nonce(sender_address, nonce)
{
self.tx_queue.insert(*tx_reference);
self.tx_queue.insert(tx_reference.clone());
}
}

Expand Down Expand Up @@ -187,7 +189,7 @@ impl Mempool {
if let Some(next_tx_reference) =
self.tx_pool.get_next_eligible_tx(current_account_state)?
{
self.tx_queue.insert(*next_tx_reference);
self.tx_queue.insert(next_tx_reference.clone());
}
}

Expand All @@ -204,12 +206,14 @@ impl Mempool {
/// execution fields).
/// TODO(Mohammad): rename this struct to `ThinTransaction` once that name
/// becomes available, to better reflect its purpose and usage.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
/// TODO(Mohammad): restore the Copy once ResourceBoundsMapping implements it.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct TransactionReference {
pub sender_address: ContractAddress,
pub nonce: Nonce,
pub tx_hash: TransactionHash,
pub tip: Tip,
pub resource_bounds: ResourceBoundsMapping,
}

impl TransactionReference {
Expand All @@ -219,6 +223,8 @@ impl TransactionReference {
nonce: tx.nonce(),
tx_hash: tx.tx_hash(),
tip: tx.tip().expect("Expected a valid tip value, but received None."),
// TODO(Mohammad): add resource bounds to the transaction.
resource_bounds: ResourceBoundsMapping::default(),
}
}
}
14 changes: 6 additions & 8 deletions crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ fn test_get_txs_with_holes_single_account() {

let pool_txs = [input_nonce_1.tx];
let queue_txs = [];
let mut mempool: Mempool = MempoolContent::new(pool_txs.clone(), queue_txs).into();
let mut mempool: Mempool = MempoolContent::new(pool_txs.clone(), queue_txs.clone()).into();

// Test.
let txs = mempool.get_txs(1).unwrap();
Expand Down Expand Up @@ -429,7 +429,7 @@ fn test_add_tx_lower_than_queued_nonce() {
add_tx_input!(tx_hash: 2, sender_address: "0x0", tx_nonce: 0_u8, account_nonce: 0_u8);

let queue_txs = [TransactionReference::new(&valid_input.tx)];
let expected_mempool_content = MempoolContent::with_queue(queue_txs);
let expected_mempool_content = MempoolContent::with_queue(queue_txs.clone());
let pool_txs = [valid_input.tx];
let mut mempool: Mempool = MempoolContent::new(pool_txs, queue_txs).into();

Expand All @@ -446,15 +446,14 @@ fn test_add_tx_updates_queue_with_higher_account_nonce() {
let higher_account_nonce_input =
add_tx_input!(tx_hash: 2, sender_address: "0x0", tx_nonce: 1_u8, account_nonce: 1_u8);

let queue_txs = [TransactionReference::new_from_thin_tx(&input.tx)];
let queue_txs = [TransactionReference::new(&input.tx)];
let mut mempool: Mempool = MempoolContent::with_queue(queue_txs).into();

// Test.
add_tx(&mut mempool, &higher_account_nonce_input);

// Assert: the higher account nonce transaction is in the queue.
let expected_queue_txs =
[TransactionReference::new_from_thin_tx(&higher_account_nonce_input.tx)];
let expected_queue_txs = [TransactionReference::new(&higher_account_nonce_input.tx)];
let expected_mempool_content = MempoolContent::with_queue(expected_queue_txs);
expected_mempool_content.assert_eq_queue_content(&mempool);
}
Expand Down Expand Up @@ -499,8 +498,7 @@ fn test_add_tx_delete_tx_with_lower_nonce_than_account_nonce() {
add_tx(&mut mempool, &tx_nonce_1_account_nonce_1);

// Assert the transaction with the lower nonce is removed.
let expected_queue_txs =
[TransactionReference::new_from_thin_tx(&tx_nonce_1_account_nonce_1.tx)];
let expected_queue_txs = [TransactionReference::new(&tx_nonce_1_account_nonce_1.tx)];
let expected_pool_txs = [tx_nonce_1_account_nonce_1.tx];
let expected_mempool_content = MempoolContent::new(expected_pool_txs, expected_queue_txs);
expected_mempool_content.assert_eq_mempool_content(&mempool);
Expand Down Expand Up @@ -624,7 +622,7 @@ fn test_commit_block_includes_all_txs() {
let queue_txs = [&tx_address0_nonce4, &tx_address1_nonce3, &tx_address2_nonce1]
.map(TransactionReference::new);
let pool_txs = [tx_address0_nonce4, tx_address0_nonce5, tx_address1_nonce3, tx_address2_nonce1];
let mut mempool: Mempool = MempoolContent::new(pool_txs.clone(), queue_txs).into();
let mut mempool: Mempool = MempoolContent::new(pool_txs.clone(), queue_txs.clone()).into();

// Test.
let state_changes = HashMap::from([
Expand Down
6 changes: 3 additions & 3 deletions crates/mempool/src/transaction_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ impl TransactionQueue {
/// Panics: if given a duplicate tx.
// TODO(Mohammad): Add test for two transactions from the same address, expecting specific
// assert.
pub fn insert(&mut self, tx: TransactionReference) {
pub fn insert(&mut self, tx_reference: TransactionReference) {
assert_eq!(
self.address_to_tx.insert(tx.sender_address, tx),
self.address_to_tx.insert(tx_reference.sender_address, tx_reference.clone()),
None,
"Only a single transaction from the same contract class can be in the mempool at a \
time."
);
assert!(
self.priority_queue.insert(tx.into()),
self.priority_queue.insert(tx_reference.into()),
"Keys should be unique; duplicates are checked prior."
);
}
Expand Down

0 comments on commit f1559fc

Please sign in to comment.