Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Allow configuration of when to reseal blocks. #1460

Merged
merged 14 commits into from
Jun 28, 2016
7 changes: 4 additions & 3 deletions ethcore/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -791,8 +792,8 @@ impl<V> BlockChainClient for Client<V> where V: Verifier {
}
}

fn all_transactions(&self) -> Vec<SignedTransaction> {
self.miner.all_transactions()
fn pending_transactions(&self) -> Vec<SignedTransaction> {
self.miner.pending_transactions()
}
}

Expand Down
2 changes: 1 addition & 1 deletion ethcore/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ pub trait BlockChainClient : Sync + Send {
fn queue_transactions(&self, transactions: Vec<Bytes>);

/// list all transactions
fn all_transactions(&self) -> Vec<SignedTransaction>;
fn pending_transactions(&self) -> Vec<SignedTransaction>;

/// Get the gas price distribution.
fn gas_price_statistics(&self, sample_size: usize, distribution_size: usize) -> Result<Vec<U256>, ()> {
Expand Down
4 changes: 2 additions & 2 deletions ethcore/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ impl BlockChainClient for TestBlockChainClient {
self.import_transactions(tx);
}

fn all_transactions(&self) -> Vec<SignedTransaction> {
self.miner.all_transactions()
fn pending_transactions(&self) -> Vec<SignedTransaction> {
self.miner.pending_transactions()
}
}
111 changes: 80 additions & 31 deletions ethcore/src/miner/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,54 @@ use spec::Spec;
use engine::Engine;
use miner::{MinerService, MinerStatus, TransactionQueue, AccountDetails, TransactionImportResult, TransactionOrigin};

/// Different possible definitions for pending transaction set.
#[derive(Debug)]
pub enum PendingSet {
/// Always just the transactions in the queue. These have had only cheap checks.
AlwaysQueue,
/// Always just the transactions in the sealing block. These have had full checks but
/// may be empty if the node is not actively mining or has force_sealing enabled.
AlwaysSealing,
/// Try the sealing block, but if it is not currently sealing, fallback to the queue.
SealingOrElseQueue,
}

/// Configures the behaviour of the miner.
#[derive(Debug)]
pub struct MinerOptions {
/// Force the miner to reseal, even when nobody has asked for work.
pub force_sealing: bool,
/// Reseal on receipt of new external transactions.
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 pending_set: PendingSet,
}

impl Default for MinerOptions {
fn default() -> Self {
MinerOptions {
force_sealing: false,
reseal_on_external_tx: true,
reseal_on_own_tx: true,
max_tx_gas: None,
pending_set: PendingSet::AlwaysQueue,
}
}
}

/// Keeps track of transactions using priority queue and holds currently mined block.
pub struct Miner {
// NOTE [ToDr] When locking always lock in this order!
transaction_queue: Mutex<TransactionQueue>,
sealing_work: Mutex<UsingQueue<ClosedBlock>>,

// for sealing...
force_sealing: bool,
options: MinerOptions,
sealing_enabled: AtomicBool,
sealing_block_last_request: Mutex<u64>,
gas_range_target: RwLock<(U256, U256)>,
Expand All @@ -52,7 +92,7 @@ impl Miner {
pub fn with_spec(spec: Spec) -> Miner {
Miner {
transaction_queue: Mutex::new(TransactionQueue::new()),
force_sealing: false,
options: Default::default(),
sealing_enabled: AtomicBool::new(false),
sealing_block_last_request: Mutex::new(0),
sealing_work: Mutex::new(UsingQueue::new(5)),
Expand All @@ -65,11 +105,11 @@ impl Miner {
}

/// Creates new instance of miner
pub fn new(force_sealing: bool, spec: Spec, accounts: Option<Arc<AccountProvider>>) -> Arc<Miner> {
pub fn new(options: MinerOptions, spec: Spec, accounts: Option<Arc<AccountProvider>>) -> Arc<Miner> {
Arc::new(Miner {
transaction_queue: Mutex::new(TransactionQueue::new()),
force_sealing: force_sealing,
sealing_enabled: AtomicBool::new(force_sealing),
sealing_enabled: AtomicBool::new(options.force_sealing),
options: options,
sealing_block_last_request: Mutex::new(0),
sealing_work: Mutex::new(UsingQueue::new(5)),
gas_range_target: RwLock::new((U256::zero(), U256::zero())),
Expand All @@ -91,7 +131,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();
/*
Expand Down Expand Up @@ -385,7 +425,7 @@ impl MinerService for Miner {
.map(|tx| transaction_queue.add(tx, &fetch_account, TransactionOrigin::External))
.collect()
};
if !results.is_empty() {
if !results.is_empty() && self.options.reseal_on_external_tx {
self.update_sealing(chain);
}
results
Expand Down Expand Up @@ -420,7 +460,7 @@ impl MinerService for Miner {
import
};

if imported.is_ok() {
if imported.is_ok() && self.options.reseal_on_own_tx {
// Make sure to do it after transaction is imported and lock is droped.
// We need to create pending block and enable sealing
let prepared = self.enable_and_prepare_sealing(chain);
Expand All @@ -434,39 +474,48 @@ impl MinerService for Miner {
imported
}

fn pending_transactions_hashes(&self) -> Vec<H256> {
fn all_transactions(&self) -> Vec<SignedTransaction> {
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()
}
}
queue.top_transactions()
}

fn transaction(&self, hash: &H256) -> Option<SignedTransaction> {
fn pending_transactions(&self) -> Vec<SignedTransaction> {
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)
}
let sw = self.sealing_work.lock().unwrap();
// TODO: should only use the sealing_work when it's current (it could be an old block)
let sealing_set = match self.sealing_enabled.load(atomic::Ordering::Relaxed) {
true => sw.peek_last_ref(),
false => None,
};
match (&self.options.pending_set, sealing_set) {
(&PendingSet::AlwaysQueue, _) | (&PendingSet::SealingOrElseQueue, None) => queue.top_transactions(),
(_, sealing) => sealing.map(|s| s.transactions().clone()).unwrap_or(Vec::new()),
}
}

fn all_transactions(&self) -> Vec<SignedTransaction> {
fn pending_transactions_hashes(&self) -> Vec<H256> {
let queue = self.transaction_queue.lock().unwrap();
queue.top_transactions()
let sw = self.sealing_work.lock().unwrap();
let sealing_set = match self.sealing_enabled.load(atomic::Ordering::Relaxed) {
true => sw.peek_last_ref(),
false => None,
};
match (&self.options.pending_set, sealing_set) {
(&PendingSet::AlwaysQueue, _) | (&PendingSet::SealingOrElseQueue, None) => queue.pending_hashes(),
(_, sealing) => sealing.map(|s| s.transactions().iter().map(|t| t.hash()).collect()).unwrap_or(Vec::new()),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sealing.map_or_else(Vec::new, |s| ...)

}
}

fn pending_transactions(&self) -> Vec<SignedTransaction> {
fn transaction(&self, hash: &H256) -> Option<SignedTransaction> {
let queue = self.transaction_queue.lock().unwrap();
// 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()
}
let sw = self.sealing_work.lock().unwrap();
Copy link
Contributor Author

@gavofyork gavofyork Jun 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

anyone know if this is the correct order for the lock? (it's the same as before, so should be good...)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks correct

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

let sealing_set = match self.sealing_enabled.load(atomic::Ordering::Relaxed) {
true => sw.peek_last_ref(),
false => None,
};
match (&self.options.pending_set, sealing_set) {
(&PendingSet::AlwaysQueue, _) | (&PendingSet::SealingOrElseQueue, None) => queue.find(hash),
(_, sealing) => sealing.and_then(|s| s.transactions().iter().find(|t| &t.hash() == hash).cloned()),
}
}

Expand Down Expand Up @@ -494,7 +543,7 @@ impl MinerService for Miner {
let current_no = chain.chain_info().best_block_number;
let has_local_transactions = self.transaction_queue.lock().unwrap().has_local_pending_transactions();
let last_request = *self.sealing_block_last_request.lock().unwrap();
let should_disable_sealing = !self.force_sealing
let should_disable_sealing = !self.options.force_sealing
&& !has_local_transactions
&& current_no > last_request
&& current_no - last_request > SEALING_TIMEOUT_IN_BLOCKS;
Expand Down
2 changes: 1 addition & 1 deletion ethcore/src/miner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod external;
mod transaction_queue;

pub use self::transaction_queue::{TransactionQueue, AccountDetails, TransactionImportResult, TransactionOrigin};
pub use self::miner::{Miner};
pub use self::miner::{Miner, MinerOptions, PendingSet};
pub use self::external::{ExternalMiner, ExternalMinerService};

use std::collections::BTreeMap;
Expand Down
16 changes: 16 additions & 0 deletions ethcore/src/miner/transaction_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Copy link
Collaborator

@tomusdrw tomusdrw Jun 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.map(|t| ...t.transaction).filter(|t| ...).cloned() ?

.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
Expand Down
25 changes: 22 additions & 3 deletions parity/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ Sealing/Mining Options:
NOTE: MINING WILL NOT WORK WITHOUT THIS OPTION.
--force-sealing Force the node to author new blocks as if it were
always sealing/mining.
--reseal-on-txs Specify which transactions should force the node
to reseal a block. One of:
none - never reseal on new transactions;
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-set SET Set of transactions to relay. SET may be:
cheap - Relay any transaction in the queue (this
may include invalid transactions);
strict - Relay only executed transactions (this
guarantees we don't relay invalid transactions, but
means we relay nothing if not mining);
lenient - Same as struct when mining, and cheap
when not [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.
Expand All @@ -146,8 +162,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
Expand Down Expand Up @@ -283,13 +299,16 @@ pub struct Args {
pub flag_signer_path: String,
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_set: 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,
Expand Down
31 changes: 31 additions & 0 deletions parity/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use util::*;
use ethcore::account_provider::AccountProvider;
use util::network_settings::NetworkSettings;
use ethcore::client::{append_path, get_db_path, ClientConfig, Switch, VMType};
use ethcore::miner::{MinerOptions, PendingSet};
use ethcore::ethereum;
use ethcore::spec::Spec;
use ethsync::SyncConfig;
Expand Down Expand Up @@ -67,6 +68,36 @@ 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),
"own" => (true, false),
"ext" => (false, true),
"all" => (true, true),
x => die!("{}: Invalid value for --reseal option. Use --help for more information.", x)
};
MinerOptions {
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")),
pending_set: match self.args.flag_relay_set.as_str() {
"cheap" => PendingSet::AlwaysQueue,
"strict" => PendingSet::AlwaysSealing,
"lenient" => PendingSet::SealingOrElseQueue,
x => die!("{}: Invalid value for --relay-set option. Use --help for more information.", x)
},
}
}

pub fn author(&self) -> Option<Address> {
self.args.flag_etherbase.as_ref()
.or(self.args.flag_author.as_ref())
Expand Down
4 changes: 2 additions & 2 deletions parity/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,13 @@ fn execute_client(conf: Configuration, spec: Spec, client_config: ClientConfig)
let account_service = Arc::new(conf.account_service());

// Miner
let miner = Miner::new(conf.args.flag_force_sealing, conf.spec(), Some(account_service.clone()));
let miner = Miner::new(conf.miner_options(), conf.spec(), Some(account_service.clone()));
miner.set_author(conf.author().unwrap_or_default());
miner.set_gas_floor_target(conf.gas_floor_target());
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(
Expand Down
12 changes: 10 additions & 2 deletions rpc/src/v1/tests/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use ethcore::spec::{Genesis, Spec};
use ethcore::block::Block;
use ethcore::views::BlockView;
use ethcore::ethereum;
use ethcore::miner::{MinerService, ExternalMiner, Miner};
use ethcore::miner::{MinerOptions, MinerService, ExternalMiner, Miner};
use ethcore::account_provider::AccountProvider;
use devtools::RandomTempPath;
use util::Hashable;
Expand All @@ -49,7 +49,15 @@ fn sync_provider() -> Arc<TestSyncProvider> {
}

fn miner_service(spec: Spec, accounts: Arc<AccountProvider>) -> Arc<Miner> {
Miner::new(true, spec, Some(accounts))
Miner::new(
MinerOptions {
force_sealing: true,
reseal_on_external_tx: true,
reseal_on_own_tx: true,
},
spec,
Some(accounts)
)
}

fn make_spec(chain: &BlockChain) -> Spec {
Expand Down
Loading