Skip to content

Commit

Permalink
test(mempool): test update gas threshold api
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadNassar1 committed Sep 21, 2024
1 parent 1bcea4e commit 329c810
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
62 changes: 56 additions & 6 deletions crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use assert_matches::assert_matches;
use mempool_test_utils::starknet_api_test_utils::{
create_executable_tx,
test_resource_bounds_mapping,
VALID_L2_GAS_MAX_PRICE_PER_UNIT,
};
use pretty_assertions::assert_eq;
use rstest::{fixture, rstest};
Expand Down Expand Up @@ -49,8 +50,8 @@ impl MempoolContent {
self.tx_queue.as_ref().unwrap().assert_eq_priority_queue(&mempool.tx_queue);
}

fn _assert_eq_transaction_pending_queue_content(&self, mempool: &Mempool) {
self.tx_queue.as_ref().unwrap()._assert_eq_pending_queue(&mempool.tx_queue);
fn assert_eq_transaction_pending_queue_content(&self, mempool: &Mempool) {
self.tx_queue.as_ref().unwrap().assert_eq_pending_queue(&mempool.tx_queue);
}

fn _assert_eq_transaction_queues_content(&self, mempool: &Mempool) {
Expand Down Expand Up @@ -104,7 +105,7 @@ impl MempoolContentBuilder {
self
}

fn _with_pending_queue<Q>(mut self, queue_txs: Q) -> Self
fn with_pending_queue<Q>(mut self, queue_txs: Q) -> Self
where
Q: IntoIterator<Item = TransactionReference>,
{
Expand All @@ -113,10 +114,10 @@ impl MempoolContentBuilder {
self
}

fn _with_gas_price_threshold(mut self, gas_price_threshold: u128) -> Self {
fn with_gas_price_threshold(mut self, gas_price_threshold: u128) -> Self {
self.tx_queue = Some(
TransactionQueueContentBuilder::new()
._with_gas_price_threshold(gas_price_threshold)
.with_gas_price_threshold(gas_price_threshold)
.build(),
);
self
Expand Down Expand Up @@ -180,7 +181,7 @@ fn commit_block(
/// 5. add_tx_input!(tx_hash: 1, tx_nonce: 1, account_nonce: 0)
/// 6. add_tx_input!(tx_nonce: 1, account_nonce: 0)
/// 7. add_tx_input!(tip: 1, tx_hash: 2)
/// 8. add_tx_input!()
/// 8. add_tx_input!(tx_hash: 1)

macro_rules! add_tx_input {
(tip: $tip:expr, tx_hash: $tx_hash:expr, sender_address: $sender_address:expr,
Expand Down Expand Up @@ -217,6 +218,9 @@ macro_rules! add_tx_input {
(tip: $tip:expr, tx_hash: $tx_hash:expr) => {
add_tx_input!(tip: $tip, tx_hash: $tx_hash, sender_address: "0x0", tx_nonce: 0_u8, account_nonce: 0_u8)
};
(tx_hash: $tx_hash:expr) => {
add_tx_input!(tip: 1, tx_hash: $tx_hash, sender_address: "0x0", tx_nonce: 0_u8, account_nonce: 0_u8)
};
}

// Fixtures.
Expand Down Expand Up @@ -964,6 +968,52 @@ fn test_account_nonces_removal_in_commit_block(mut mempool: Mempool) {
expected_mempool_content.assert_eq_account_nonces(&mempool);
}

// Update gas price threshold tests.

#[rstest]
fn test_increase_gas_price_threshold() {
// Setup.
let txs = [&add_tx_input!(tx_hash: 0_u8).tx, &add_tx_input!(tx_hash: 1_u8).tx]
.map(TransactionReference::new);
let (priority_txs, pending_txs) = ([txs[0]], [txs[1]]);
let mut mempool: Mempool = MempoolContentBuilder::new()
.with_priority_queue(priority_txs)
.with_pending_queue(pending_txs)
.with_gas_price_threshold(VALID_L2_GAS_MAX_PRICE_PER_UNIT)
.build()
.into();

// Test.
// All txs should be in the pending queue.
mempool._update_gas_price_threshold(VALID_L2_GAS_MAX_PRICE_PER_UNIT + 1);

// Assert.
let expected_mempool_content = MempoolContentBuilder::new().with_pending_queue(txs).build();
expected_mempool_content.assert_eq_transaction_pending_queue_content(&mempool);
}

#[rstest]
fn test_decrease_gas_price_threshold() {
// Setup.
let txs = [&add_tx_input!(tx_hash: 0_u8).tx, &add_tx_input!(tx_hash: 1_u8).tx]
.map(TransactionReference::new);
let (priority_txs, pending_txs) = ([txs[0]], [txs[1]]);
let mut mempool: Mempool = MempoolContentBuilder::new()
.with_priority_queue(priority_txs)
.with_pending_queue(pending_txs)
.with_gas_price_threshold(VALID_L2_GAS_MAX_PRICE_PER_UNIT)
.build()
.into();

// Test.
// All txs should be in the pending queue.
mempool._update_gas_price_threshold(VALID_L2_GAS_MAX_PRICE_PER_UNIT - 1);

// Assert.
let expected_mempool_content = MempoolContentBuilder::new().with_priority_queue(txs).build();
expected_mempool_content.assert_eq_transaction_priority_queue_content(&mempool);
}

// Flow tests.

#[rstest]
Expand Down
6 changes: 3 additions & 3 deletions crates/mempool/src/transaction_queue_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ pub struct TransactionQueueContent {
impl TransactionQueueContent {
pub fn _assert_eq_priority_and_pending_queues(&self, tx_queue: &TransactionQueue) {
self.assert_eq_priority_queue(tx_queue);
self._assert_eq_pending_queue(tx_queue);
self.assert_eq_pending_queue(tx_queue);
}

pub fn assert_eq_priority_queue(&self, tx_queue: &TransactionQueue) {
assert_eq!(self.priority_queue.as_ref().unwrap(), &tx_queue.priority_queue);
assert_eq!(self.address_to_tx.as_ref().unwrap(), &tx_queue.address_to_tx);
}

pub fn _assert_eq_pending_queue(&self, tx_queue: &TransactionQueue) {
pub fn assert_eq_pending_queue(&self, tx_queue: &TransactionQueue) {
assert_eq!(self.pending_queue.as_ref().unwrap(), &tx_queue.pending_queue);
assert_eq!(self.address_to_tx.as_ref().unwrap(), &tx_queue.address_to_tx);
}
Expand Down Expand Up @@ -72,7 +72,7 @@ impl TransactionQueueContentBuilder {
self
}

pub fn _with_gas_price_threshold(mut self, gas_price_threshold: u128) -> Self {
pub fn with_gas_price_threshold(mut self, gas_price_threshold: u128) -> Self {
self._gas_price_threshold = Some(gas_price_threshold);
self
}
Expand Down

0 comments on commit 329c810

Please sign in to comment.