Skip to content

Commit

Permalink
test(mempool): impl update gas price threshold tests (#1356)
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadNassar1 authored Nov 14, 2024
1 parent 5056866 commit af9e306
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
59 changes: 57 additions & 2 deletions crates/starknet_mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use pretty_assertions::assert_eq;
use rstest::{fixture, rstest};
use starknet_api::block::GasPrice;
use starknet_api::executable_transaction::AccountTransaction;
use starknet_api::{contract_address, nonce};
use starknet_mempool_types::errors::MempoolError;
Expand Down Expand Up @@ -94,9 +95,9 @@ 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_content_builder =
self.tx_queue_content_builder._with_gas_price_threshold(gas_price_threshold);
self.tx_queue_content_builder.with_gas_price_threshold(gas_price_threshold);
self
}

Expand Down Expand Up @@ -613,3 +614,57 @@ fn test_fee_escalation_invalid_replacement_overflow_gracefully_handled() {
let invalid_replacement_input = add_tx_input!(tip: u64::MAX, max_l2_gas_price: u128::MAX);
add_txs_and_verify_no_replacement(mempool, existing_tx, [invalid_replacement_input]);
}

// `update_gas_price_threshold` tests.

#[rstest]
fn test_update_gas_price_threshold_increases_threshold() {
// Setup.
let [tx_low_gas, tx_high_gas] = [
&tx!(tx_hash: 0, address: "0x0", max_l2_gas_price: 100),
&tx!(tx_hash: 1, address: "0x1", max_l2_gas_price: 101),
]
.map(TransactionReference::new);

let mut mempool: Mempool = MempoolContentBuilder::new()
.with_priority_queue([tx_low_gas, tx_high_gas])
.with_gas_price_threshold(100)
.build()
.into();

// Test.
mempool._update_gas_price_threshold(GasPrice(101));

// Assert.
let expected_mempool_content = MempoolContentBuilder::new()
.with_pending_queue([tx_low_gas])
.with_priority_queue([tx_high_gas])
.build();
expected_mempool_content.assert_eq(&mempool);
}

#[rstest]
fn test_update_gas_price_threshold_decreases_threshold() {
// Setup.
let [tx_low_gas, tx_high_gas] = [
&tx!(tx_hash: 0, address: "0x0", max_l2_gas_price: 89),
&tx!(tx_hash: 1, address: "0x1", max_l2_gas_price: 90),
]
.map(TransactionReference::new);

let mut mempool: Mempool = MempoolContentBuilder::new()
.with_pending_queue([tx_low_gas, tx_high_gas])
.with_gas_price_threshold(100)
.build()
.into();

// Test.
mempool._update_gas_price_threshold(GasPrice(90));

// Assert.
let expected_mempool_content = MempoolContentBuilder::new()
.with_pending_queue([tx_low_gas])
.with_priority_queue([tx_high_gas])
.build();
expected_mempool_content.assert_eq(&mempool);
}
9 changes: 9 additions & 0 deletions crates/starknet_mempool/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ macro_rules! tx {
(tx_hash: $tx_hash:expr, address: $address:expr, tip: $tip:expr) => {
tx!(tx_hash: $tx_hash, address: $address, tx_nonce: 0, tip: $tip)
};
(tx_hash: $tx_hash:expr, address: $address:expr, max_l2_gas_price: $max_l2_gas_price:expr) => {
tx!(
tx_hash: $tx_hash,
address: $address,
tx_nonce: 0,
tip: 0,
max_l2_gas_price: $max_l2_gas_price
)
};
(tx_hash: $tx_hash:expr, tip: $tip:expr, max_l2_gas_price: $max_l2_gas_price:expr) => {
tx!(
tx_hash: $tx_hash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,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.into());
self
}
Expand Down

0 comments on commit af9e306

Please sign in to comment.