This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Allow configuration of when to reseal blocks. #1460
Merged
Merged
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6c1802e
Allow configuration of when to reseal blocks.
gavofyork 1667808
More miner options.
gavofyork 2a51a30
Fix up the pending set stuff.
gavofyork a102015
Fix doc test.
gavofyork 10aa32b
Include RPC configurability for max tx gas limit.
gavofyork 5d67a32
Merge remote-tracking branch 'origin/master' into miner-improvements
arkpar 495e560
Merge branch 'master' into miner-improvements
gavofyork 5919c66
Fix typo
gavofyork ff788e4
Fix another typo
gavofyork c221f69
Clean up some of the FP stuff.
gavofyork d0cec24
Merge branch 'miner-improvements' of github.com:ethcore/parity into m…
gavofyork af935df
Merge branch 'master' into miner-improvements
gavofyork 31de739
U256 instead of Option<U256>. Fix up tests.
gavofyork 599a610
Minor renaming.
gavofyork File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)>, | ||
|
@@ -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)), | ||
|
@@ -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())), | ||
|
@@ -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(); | ||
/* | ||
|
@@ -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 | ||
|
@@ -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); | ||
|
@@ -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()), | ||
} | ||
} | ||
|
||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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...) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks correct There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()), | ||
} | ||
} | ||
|
||
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
.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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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| ...)