From 5df817c8e0ad8231d93e7f1224f0bcf510515441 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Mon, 18 Apr 2016 23:03:41 +0200 Subject: [PATCH] Setting limit from CLI --- miner/src/lib.rs | 6 ++++++ miner/src/miner.rs | 8 ++++++++ miner/src/transaction_queue.rs | 34 ++++++++++++++++++++++++++++------ parity/main.rs | 4 ++++ 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/miner/src/lib.rs b/miner/src/lib.rs index 1b080f30c..712ceb33b 100644 --- a/miner/src/lib.rs +++ b/miner/src/lib.rs @@ -100,6 +100,12 @@ pub trait MinerService : Send + Sync { /// Set the gas limit we wish to target when sealing a new block. fn set_gas_floor_target(&self, target: U256); + /// Get current transactions limit in queue. + fn transactions_limit(&self) -> usize; + + /// Set maximal number of transactions kept in the queue (both current and future). + fn set_transactions_limit(&self, limit: usize); + /// Imports transactions to transaction queue. fn import_transactions(&self, transactions: Vec, fetch_account: T) -> Vec> diff --git a/miner/src/miner.rs b/miner/src/miner.rs index 287250f04..a45b34f85 100644 --- a/miner/src/miner.rs +++ b/miner/src/miner.rs @@ -205,6 +205,14 @@ impl MinerService for Miner { *self.gas_floor_target.read().unwrap() / x!(5) } + fn transactions_limit(&self) -> usize { + self.transaction_queue.lock().unwrap().limit() + } + + fn set_transactions_limit(&self, limit: usize) { + self.transaction_queue.lock().unwrap().set_limit(limit) + } + /// Get the author that we will seal blocks as. fn author(&self) -> Address { *self.author.read().unwrap() diff --git a/miner/src/transaction_queue.rs b/miner/src/transaction_queue.rs index bd017fd69..4c1a0f457 100644 --- a/miner/src/transaction_queue.rs +++ b/miner/src/transaction_queue.rs @@ -242,6 +242,12 @@ impl TransactionSet { self.by_priority.clear(); self.by_address.clear(); } + + /// Sets new limit for number of transactions in this `TransactionSet`. + /// Note the limit is not applied (no transactions are removed) by calling this method. + fn set_limit(&mut self, limit: usize) { + self.limit = limit; + } } #[derive(Debug)] @@ -290,20 +296,21 @@ impl Default for TransactionQueue { impl TransactionQueue { /// Creates new instance of this Queue pub fn new() -> Self { - Self::with_limits(1024, 1024) + Self::with_limit(1024) } /// Create new instance of this Queue with specified limits - pub fn with_limits(current_limit: usize, future_limit: usize) -> Self { + pub fn with_limit(limit: usize) -> Self { let current = TransactionSet { by_priority: BTreeSet::new(), by_address: Table::new(), - limit: current_limit, + limit: limit, }; + let future = TransactionSet { by_priority: BTreeSet::new(), by_address: Table::new(), - limit: future_limit, + limit: limit, }; TransactionQueue { @@ -316,6 +323,20 @@ impl TransactionQueue { } } + /// Set the new limit for `current` and `future` queue. + pub fn set_limit(&mut self, limit: usize) { + self.current.set_limit(limit); + self.future.set_limit(limit); + // And ensure the limits + self.current.enforce_limit(&mut self.by_hash); + self.future.enforce_limit(&mut self.by_hash); + } + + /// Returns current limit of transactions in the queue. + pub fn limit(&self) -> usize { + self.current.limit + } + /// Get the minimal gas price. pub fn minimal_gas_price(&self) -> &U256 { &self.minimal_gas_price @@ -1085,7 +1106,7 @@ mod test { #[test] fn should_drop_old_transactions_when_hitting_the_limit() { // given - let mut txq = TransactionQueue::with_limits(1, 1); + let mut txq = TransactionQueue::with_limit(1); let (tx, tx2) = new_txs(U256::one()); txq.add(tx.clone(), &default_nonce).unwrap(); assert_eq!(txq.status().pending, 1); @@ -1102,7 +1123,8 @@ mod test { #[test] fn should_limit_future_transactions() { - let mut txq = TransactionQueue::with_limits(10, 1); + let mut txq = TransactionQueue::with_limit(1); + txq.current.set_limit(10); let (tx1, tx2) = new_txs(U256::from(4)); let (tx3, tx4) = new_txs(U256::from(4)); txq.add(tx1.clone(), &default_nonce).unwrap(); diff --git a/parity/main.rs b/parity/main.rs index 8e9c79b27..8eb67a46f 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -172,6 +172,8 @@ Sealing/Mining Options: [default: 0037a6b811ffeb6e072da21179d11b1406371c63]. --extra-data STRING Specify a custom extra-data for authored blocks, no more than 32 characters. + --tx-limit LIMIT Limit of transactions kept in the queue (waiting to + be included in next block) [default: 1024]. Footprint Options: --pruning METHOD Configure pruning of the state/storage trie. METHOD @@ -259,6 +261,7 @@ struct Args { flag_usd_per_eth: String, flag_gas_floor_target: String, flag_extra_data: Option, + flag_tx_limit: usize, flag_logging: Option, flag_version: bool, // geth-compatibility... @@ -713,6 +716,7 @@ impl Configuration { miner.set_gas_floor_target(self.gas_floor_target()); miner.set_extra_data(self.extra_data()); miner.set_minimal_gas_price(self.gas_price()); + miner.set_transactions_limit(self.args.flag_tx_limit); // Sync let sync = EthSync::register(service.network(), sync_config, client.clone(), miner.clone());