From 1667808ecb67dc0c45f51f83b3a95369b075643a Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 27 Jun 2016 18:27:06 +0200 Subject: [PATCH] More miner options. - Optional limit for the amount of gas transactions may have; - option to restruct transactions returned/queried to only those which have been executed. --- ethcore/src/client/client.rs | 3 ++- ethcore/src/miner/miner.rs | 24 ++++++++++++++---------- ethcore/src/miner/transaction_queue.rs | 16 ++++++++++++++++ parity/cli.rs | 13 ++++++++++--- parity/configuration.rs | 14 ++++++++++++++ parity/main.rs | 2 +- 6 files changed, 57 insertions(+), 15 deletions(-) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index 16422250a..2c4499e57 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -149,7 +149,8 @@ impl Client where V: Verifier { let mut state_db = journaldb::new( &append_path(&path, "state"), config.pruning, - config.db_cache_size); + config.db_cache_size + ); if state_db.is_empty() && spec.ensure_db_good(state_db.as_hashdb_mut()) { state_db.commit(0, &spec.genesis_header().hash(), None).expect("Error commiting genesis state to state DB"); diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index 1e76e6288..e5ecff8c4 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -38,6 +38,11 @@ pub struct MinerOptions { pub reseal_on_external_tx: bool, /// Reseal on receipt of new local transactions. pub reseal_on_own_tx: bool, + /// Specify maximum amount of gas to bother considering for block insertion. + /// If `None`, then no limit. + pub max_tx_gas: Option, + /// Whether we should fallback to providing all the queue's transactions or just pending. + pub strict_valid_pending: bool, } impl Default for MinerOptions { @@ -46,6 +51,8 @@ impl Default for MinerOptions { force_sealing: false, reseal_on_external_tx: true, reseal_on_own_tx: true, + max_tx_gas: None, + strict_valid_pending: false, } } } @@ -112,7 +119,7 @@ impl Miner { trace!(target: "miner", "prepare_sealing: entering"); let (transactions, mut open_block) = { - let transactions = {self.transaction_queue.lock().unwrap().top_transactions()}; + let transactions = {self.transaction_queue.lock().unwrap().top_transactions_maybe_limit(&self.options.max_tx_gas)}; let mut sealing_work = self.sealing_work.lock().unwrap(); let best_hash = chain.best_block_header().sha3(); /* @@ -459,9 +466,8 @@ impl MinerService for Miner { let queue = self.transaction_queue.lock().unwrap(); match (self.sealing_enabled.load(atomic::Ordering::Relaxed), self.sealing_work.lock().unwrap().peek_last_ref()) { (true, Some(pending)) => pending.transactions().iter().map(|t| t.hash()).collect(), - _ => { - queue.pending_hashes() - } + _ if self.options.strict_valid_pending => Vec::new(), + _ => queue.pending_hashes(), } } @@ -469,9 +475,8 @@ impl MinerService for Miner { let queue = self.transaction_queue.lock().unwrap(); match (self.sealing_enabled.load(atomic::Ordering::Relaxed), self.sealing_work.lock().unwrap().peek_last_ref()) { (true, Some(pending)) => pending.transactions().iter().find(|t| &t.hash() == hash).cloned(), - _ => { - queue.find(hash) - } + _ if self.options.strict_valid_pending => None, + _ => queue.find(hash), } } @@ -485,9 +490,8 @@ impl MinerService for Miner { // TODO: should only use the sealing_work when it's current (it could be an old block) match (self.sealing_enabled.load(atomic::Ordering::Relaxed), self.sealing_work.lock().unwrap().peek_last_ref()) { (true, Some(pending)) => pending.transactions().clone(), - _ => { - queue.top_transactions() - } + _ if self.options.strict_valid_pending => Vec::new(), + _ => queue.top_transactions(), } } diff --git a/ethcore/src/miner/transaction_queue.rs b/ethcore/src/miner/transaction_queue.rs index b2ca31e0d..9159cc6b3 100644 --- a/ethcore/src/miner/transaction_queue.rs +++ b/ethcore/src/miner/transaction_queue.rs @@ -583,6 +583,22 @@ impl TransactionQueue { .collect() } + /// Returns top transactions from the queue ordered by priority with a maximum gas limit. + pub fn top_transactions_with_limit(&self, max_tx_gas: &U256) -> Vec { + self.current.by_priority + .iter() + .map(|t| self.by_hash.get(&t.hash).expect("All transactions in `current` and `future` are always included in `by_hash`")) + .filter_map(|t| if &t.transaction.gas <= max_tx_gas { Some(t.transaction.clone()) } else { None }) + .collect() + } + + /// Returns top transactions from the queue ordered by priority with a maximum gas limit. + pub fn top_transactions_maybe_limit(&self, max_tx_gas: &Option) -> Vec { + match *max_tx_gas { + None => self.top_transactions(), + Some(ref m) => self.top_transactions_with_limit(m), + } + } /// Returns hashes of all transactions from current, ordered by priority. pub fn pending_hashes(&self) -> Vec { self.current.by_priority diff --git a/parity/cli.rs b/parity/cli.rs index a3b3bb5df..3090e83c5 100644 --- a/parity/cli.rs +++ b/parity/cli.rs @@ -139,6 +139,11 @@ Sealing/Mining Options: own - reseal only on a new local transaction; ext - reseal only on a new external transaction; all - reseal on all new transactions [default: all]. + --max-tx-gas GAS Apply a limit of GAS as the maximum amount of gas + a single transaction may have for it to be mined. + --relay-validity REQ Requirements for relaying. REQ may be: + cheap - Relay only after cheap checks; + strict - Relay only once executed [default: cheap]. --usd-per-tx USD Amount of USD to be paid for a basic transaction [default: 0.005]. The minimum gas price is set accordingly. @@ -152,8 +157,8 @@ Sealing/Mining Options: block due to transaction volume [default: 3141592]. --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]. + --tx-queue-size LIMIT Maxmimum amount of transactions in the queue (waiting + to be included in next block) [default: 1024]. Footprint Options: --tracing BOOL Indicates if full transaction tracing should be @@ -290,13 +295,15 @@ pub struct Args { pub flag_no_token: bool, pub flag_force_sealing: bool, pub flag_reseal_on_txs: String, + pub flag_max_tx_gas: Option, + pub flag_relay_validity: String, pub flag_author: Option, pub flag_usd_per_tx: String, pub flag_usd_per_eth: String, pub flag_gas_floor_target: String, pub flag_gas_cap: String, pub flag_extra_data: Option, - pub flag_tx_limit: usize, + pub flag_tx_queue_size: usize, pub flag_logging: Option, pub flag_version: bool, pub flag_from: String, diff --git a/parity/configuration.rs b/parity/configuration.rs index 39893622a..57c62f839 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -68,6 +68,14 @@ impl Configuration { self.args.flag_maxpeers.unwrap_or(self.args.flag_peers) as u32 } + fn decode_u256(d: &str, argument: &str) -> U256 { + U256::from_dec_str(d).unwrap_or_else(|_| + U256::from_str(clean_0x(d)).unwrap_or_else(|_| + die!("{}: Invalid numeric value for {}. Must be either a decimal or a hex number.", d, argument) + ) + ) + } + pub fn miner_options(&self) -> MinerOptions { let (own, ext) = match self.args.flag_reseal_on_txs.as_str() { "none" => (false, false), @@ -80,6 +88,12 @@ impl Configuration { force_sealing: self.args.flag_force_sealing, reseal_on_external_tx: ext, reseal_on_own_tx: own, + max_tx_gas: self.args.flag_max_tx_gas.as_ref().map(|d| Self::decode_u256(d, "--max-tx-gas")), + strict_valid_pending: match self.args.flag_relay_validity.as_str() { + "cheap" => false, + "strict" => true, + x => die!("{}: Invalid value for --relay-validity option. Use --help for more information.", x) + }, } } diff --git a/parity/main.rs b/parity/main.rs index e3c20caf4..047338bc8 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -214,7 +214,7 @@ fn execute_client(conf: Configuration, spec: Spec, client_config: ClientConfig) miner.set_gas_ceil_target(conf.gas_ceil_target()); miner.set_extra_data(conf.extra_data()); miner.set_minimal_gas_price(conf.gas_price()); - miner.set_transactions_limit(conf.args.flag_tx_limit); + miner.set_transactions_limit(conf.args.flag_tx_queue_size); // Build client let mut service = ClientService::start(