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.
This commit is contained in:
Gav Wood 2016-06-27 18:27:06 +02:00
parent 6c1802e412
commit 1667808ecb
6 changed files with 57 additions and 15 deletions

View File

@ -149,7 +149,8 @@ impl<V> Client<V> 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");

View File

@ -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<U256>,
/// 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(),
}
}

View File

@ -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<SignedTransaction> {
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<U256>) -> Vec<SignedTransaction> {
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<H256> {
self.current.by_priority

View File

@ -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<String>,
pub flag_relay_validity: String,
pub flag_author: Option<String>,
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<String>,
pub flag_tx_limit: usize,
pub flag_tx_queue_size: usize,
pub flag_logging: Option<String>,
pub flag_version: bool,
pub flag_from: String,

View File

@ -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)
},
}
}

View File

@ -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(