From 758a7bea9d0eeac7bf9d130270139284aa99d357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Sat, 30 Jun 2018 11:11:31 +0200 Subject: [PATCH] Minimal effective gas price in the queue (#8934) * Minimal effective gas price. * Fix naming, add test * Fix minimal entry score and add test. * Fix worst_transaction. * Remove effective gas price threshold. * Don't leak gas_price decisions out of Scoring. --- src/pool.rs | 13 ++++++++++++- src/tests/mod.rs | 29 ++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/pool.rs b/src/pool.rs index 4bbf00e..67da1b1 100644 --- a/src/pool.rs +++ b/src/pool.rs @@ -390,7 +390,13 @@ impl Pool where /// Returns worst transaction in the queue (if any). pub fn worst_transaction(&self) -> Option> { - self.worst_transactions.iter().next().map(|x| x.transaction.transaction.clone()) + self.worst_transactions.iter().next_back().map(|x| x.transaction.transaction.clone()) + } + + /// Returns true if the pool is at it's capacity. + pub fn is_full(&self) -> bool { + self.by_hash.len() >= self.options.max_count + || self.mem_usage >= self.options.max_mem_usage } /// Returns an iterator of pending (ready) transactions. @@ -486,6 +492,11 @@ impl Pool where &self.listener } + /// Borrows scoring instance. + pub fn scoring(&self) -> &S { + &self.scoring + } + /// Borrows listener mutably. pub fn listener_mut(&mut self) -> &mut L { &mut self.listener diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 77c2528..808f804 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -48,6 +48,15 @@ pub type SharedTransaction = Arc; type TestPool = Pool; +impl TestPool { + pub fn with_limit(max_count: usize) -> Self { + Self::with_options(Options { + max_count, + ..Default::default() + }) + } +} + #[test] fn should_clear_queue() { // given @@ -505,9 +514,27 @@ fn should_return_worst_transaction() { // when txq.import(b.tx().nonce(0).gas_price(5).new()).unwrap(); + txq.import(b.tx().sender(1).nonce(0).gas_price(4).new()).unwrap(); // then - assert!(txq.worst_transaction().is_some()); + assert_eq!(txq.worst_transaction().unwrap().gas_price, 4.into()); +} + +#[test] +fn should_return_is_full() { + // given + let b = TransactionBuilder::default(); + let mut txq = TestPool::with_limit(2); + assert!(!txq.is_full()); + + // when + txq.import(b.tx().nonce(0).gas_price(110).new()).unwrap(); + assert!(!txq.is_full()); + + txq.import(b.tx().sender(1).nonce(0).gas_price(100).new()).unwrap(); + + // then + assert!(txq.is_full()); } mod listener {