From 9f6467fbfc2c4207e594102a9ec65794cb99db2e Mon Sep 17 00:00:00 2001 From: Gilad Chase Date: Thu, 13 Jun 2024 12:08:24 +0300 Subject: [PATCH] fix: make address prio queue inner private To protect invariant. --- crates/mempool/src/priority_queue.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/mempool/src/priority_queue.rs b/crates/mempool/src/priority_queue.rs index cec62d0c8..cf66c0a95 100644 --- a/crates/mempool/src/priority_queue.rs +++ b/crates/mempool/src/priority_queue.rs @@ -61,13 +61,23 @@ impl PartialOrd for PrioritizedTransaction { // TODO: remove when is used. #[allow(dead_code)] -// Assumption: there are no gaps, and the transactions are received in order. -pub struct AddressPriorityQueue(pub Vec); +// Invariant: Transactions have strictly increasing nonces, without gaps. +// Assumption: Transactions are provided in the correct order. +#[derive(Default)] +pub struct AddressPriorityQueue(Vec); // TODO: remove when is used. #[allow(dead_code)] impl AddressPriorityQueue { pub fn push(&mut self, tx: ThinTransaction) { + if let Some(last_tx) = self.0.last() { + assert_eq!( + tx.nonce, + last_tx.nonce.try_increment().expect("Nonce overflow."), + "Nonces must be strictly increasing without gaps." + ); + } + self.0.push(tx); }