Setting limit from CLI

This commit is contained in:
Tomasz Drwięga 2016-04-18 23:03:41 +02:00
parent 55051951f8
commit 5df817c8e0
4 changed files with 46 additions and 6 deletions

View File

@ -100,6 +100,12 @@ pub trait MinerService : Send + Sync {
/// Set the gas limit we wish to target when sealing a new block. /// Set the gas limit we wish to target when sealing a new block.
fn set_gas_floor_target(&self, target: U256); 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. /// Imports transactions to transaction queue.
fn import_transactions<T>(&self, transactions: Vec<SignedTransaction>, fetch_account: T) -> fn import_transactions<T>(&self, transactions: Vec<SignedTransaction>, fetch_account: T) ->
Vec<Result<TransactionImportResult, Error>> Vec<Result<TransactionImportResult, Error>>

View File

@ -205,6 +205,14 @@ impl MinerService for Miner {
*self.gas_floor_target.read().unwrap() / x!(5) *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. /// Get the author that we will seal blocks as.
fn author(&self) -> Address { fn author(&self) -> Address {
*self.author.read().unwrap() *self.author.read().unwrap()

View File

@ -242,6 +242,12 @@ impl TransactionSet {
self.by_priority.clear(); self.by_priority.clear();
self.by_address.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)] #[derive(Debug)]
@ -290,20 +296,21 @@ impl Default for TransactionQueue {
impl TransactionQueue { impl TransactionQueue {
/// Creates new instance of this Queue /// Creates new instance of this Queue
pub fn new() -> Self { pub fn new() -> Self {
Self::with_limits(1024, 1024) Self::with_limit(1024)
} }
/// Create new instance of this Queue with specified limits /// 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 { let current = TransactionSet {
by_priority: BTreeSet::new(), by_priority: BTreeSet::new(),
by_address: Table::new(), by_address: Table::new(),
limit: current_limit, limit: limit,
}; };
let future = TransactionSet { let future = TransactionSet {
by_priority: BTreeSet::new(), by_priority: BTreeSet::new(),
by_address: Table::new(), by_address: Table::new(),
limit: future_limit, limit: limit,
}; };
TransactionQueue { 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. /// Get the minimal gas price.
pub fn minimal_gas_price(&self) -> &U256 { pub fn minimal_gas_price(&self) -> &U256 {
&self.minimal_gas_price &self.minimal_gas_price
@ -1085,7 +1106,7 @@ mod test {
#[test] #[test]
fn should_drop_old_transactions_when_hitting_the_limit() { fn should_drop_old_transactions_when_hitting_the_limit() {
// given // given
let mut txq = TransactionQueue::with_limits(1, 1); let mut txq = TransactionQueue::with_limit(1);
let (tx, tx2) = new_txs(U256::one()); let (tx, tx2) = new_txs(U256::one());
txq.add(tx.clone(), &default_nonce).unwrap(); txq.add(tx.clone(), &default_nonce).unwrap();
assert_eq!(txq.status().pending, 1); assert_eq!(txq.status().pending, 1);
@ -1102,7 +1123,8 @@ mod test {
#[test] #[test]
fn should_limit_future_transactions() { 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 (tx1, tx2) = new_txs(U256::from(4));
let (tx3, tx4) = new_txs(U256::from(4)); let (tx3, tx4) = new_txs(U256::from(4));
txq.add(tx1.clone(), &default_nonce).unwrap(); txq.add(tx1.clone(), &default_nonce).unwrap();

View File

@ -172,6 +172,8 @@ Sealing/Mining Options:
[default: 0037a6b811ffeb6e072da21179d11b1406371c63]. [default: 0037a6b811ffeb6e072da21179d11b1406371c63].
--extra-data STRING Specify a custom extra-data for authored blocks, no --extra-data STRING Specify a custom extra-data for authored blocks, no
more than 32 characters. 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: Footprint Options:
--pruning METHOD Configure pruning of the state/storage trie. METHOD --pruning METHOD Configure pruning of the state/storage trie. METHOD
@ -259,6 +261,7 @@ struct Args {
flag_usd_per_eth: String, flag_usd_per_eth: String,
flag_gas_floor_target: String, flag_gas_floor_target: String,
flag_extra_data: Option<String>, flag_extra_data: Option<String>,
flag_tx_limit: usize,
flag_logging: Option<String>, flag_logging: Option<String>,
flag_version: bool, flag_version: bool,
// geth-compatibility... // geth-compatibility...
@ -713,6 +716,7 @@ impl Configuration {
miner.set_gas_floor_target(self.gas_floor_target()); miner.set_gas_floor_target(self.gas_floor_target());
miner.set_extra_data(self.extra_data()); miner.set_extra_data(self.extra_data());
miner.set_minimal_gas_price(self.gas_price()); miner.set_minimal_gas_price(self.gas_price());
miner.set_transactions_limit(self.args.flag_tx_limit);
// Sync // Sync
let sync = EthSync::register(service.network(), sync_config, client.clone(), miner.clone()); let sync = EthSync::register(service.network(), sync_config, client.clone(), miner.clone());