|
|
|
|
@@ -60,6 +60,8 @@ pub struct MinerOptions {
|
|
|
|
|
pub reseal_on_own_tx: bool,
|
|
|
|
|
/// Minimum period between transaction-inspired reseals.
|
|
|
|
|
pub reseal_min_period: Duration,
|
|
|
|
|
/// Seal blocks internally.
|
|
|
|
|
pub internal_sealing: bool,
|
|
|
|
|
/// Maximum amount of gas to bother considering for block insertion.
|
|
|
|
|
pub tx_gas_limit: U256,
|
|
|
|
|
/// Maximum size of the transaction queue.
|
|
|
|
|
@@ -79,6 +81,7 @@ impl Default for MinerOptions {
|
|
|
|
|
force_sealing: false,
|
|
|
|
|
reseal_on_external_tx: false,
|
|
|
|
|
reseal_on_own_tx: true,
|
|
|
|
|
internal_sealing: false,
|
|
|
|
|
tx_gas_limit: !U256::zero(),
|
|
|
|
|
tx_queue_size: 1024,
|
|
|
|
|
pending_set: PendingSet::AlwaysQueue,
|
|
|
|
|
@@ -244,13 +247,13 @@ impl Miner {
|
|
|
|
|
|
|
|
|
|
fn prepare_block(&self, chain: &MiningBlockChainClient) -> (ClosedBlock, Option<H256>) {
|
|
|
|
|
{
|
|
|
|
|
trace!(target: "miner", "recalibrating...");
|
|
|
|
|
trace!(target: "miner", "prepare_block: recalibrating...");
|
|
|
|
|
let txq = self.transaction_queue.clone();
|
|
|
|
|
self.gas_pricer.lock().recalibrate(move |price| {
|
|
|
|
|
trace!(target: "miner", "Got gas price! {}", price);
|
|
|
|
|
trace!(target: "miner", "prepare_block: Got gas price! {}", price);
|
|
|
|
|
txq.lock().set_minimal_gas_price(price);
|
|
|
|
|
});
|
|
|
|
|
trace!(target: "miner", "done recalibration.");
|
|
|
|
|
trace!(target: "miner", "prepare_block: done recalibration.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let (transactions, mut open_block, original_work_hash) = {
|
|
|
|
|
@@ -268,13 +271,13 @@ impl Miner {
|
|
|
|
|
*/
|
|
|
|
|
let open_block = match sealing_work.queue.pop_if(|b| b.block().fields().header.parent_hash() == &best_hash) {
|
|
|
|
|
Some(old_block) => {
|
|
|
|
|
trace!(target: "miner", "Already have previous work; updating and returning");
|
|
|
|
|
trace!(target: "miner", "prepare_block: Already have previous work; updating and returning");
|
|
|
|
|
// add transactions to old_block
|
|
|
|
|
old_block.reopen(&*self.engine)
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
// block not found - create it.
|
|
|
|
|
trace!(target: "miner", "No existing work - making new block");
|
|
|
|
|
trace!(target: "miner", "prepare_block: No existing work - making new block");
|
|
|
|
|
chain.prepare_open_block(
|
|
|
|
|
self.author(),
|
|
|
|
|
(self.gas_floor_target(), self.gas_ceil_target()),
|
|
|
|
|
@@ -335,20 +338,19 @@ impl Miner {
|
|
|
|
|
/// Attempts to perform internal sealing to return Ok(sealed),
|
|
|
|
|
/// Err(Some(block)) returns for unsuccesful sealing while Err(None) indicates misspecified engine.
|
|
|
|
|
fn seal_block_internally(&self, block: ClosedBlock) -> Result<SealedBlock, Option<ClosedBlock>> {
|
|
|
|
|
trace!(target: "miner", "prepare_sealing: block has transaction - attempting internal seal.");
|
|
|
|
|
// block with transactions - see if we can seal immediately.
|
|
|
|
|
trace!(target: "miner", "seal_block_internally: block has transaction - attempting internal seal.");
|
|
|
|
|
let s = self.engine.generate_seal(block.block(), match self.accounts {
|
|
|
|
|
Some(ref x) => Some(&**x),
|
|
|
|
|
None => None,
|
|
|
|
|
});
|
|
|
|
|
if let Some(seal) = s {
|
|
|
|
|
trace!(target: "miner", "prepare_sealing: managed internal seal. importing...");
|
|
|
|
|
trace!(target: "miner", "seal_block_internally: managed internal seal. importing...");
|
|
|
|
|
block.lock().try_seal(&*self.engine, seal).or_else(|_| {
|
|
|
|
|
warn!("prepare_sealing: ERROR: try_seal failed when given internally generated seal. WTF?");
|
|
|
|
|
Err(None)
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
trace!(target: "miner", "prepare_sealing: unable to generate seal internally");
|
|
|
|
|
trace!(target: "miner", "seal_block_internally: unable to generate seal internally");
|
|
|
|
|
Err(Some(block))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -370,9 +372,9 @@ impl Miner {
|
|
|
|
|
let (work, is_new) = {
|
|
|
|
|
let mut sealing_work = self.sealing_work.lock();
|
|
|
|
|
let last_work_hash = sealing_work.queue.peek_last_ref().map(|pb| pb.block().fields().header.hash());
|
|
|
|
|
trace!(target: "miner", "Checking whether we need to reseal: orig={:?} last={:?}, this={:?}", original_work_hash, last_work_hash, block.block().fields().header.hash());
|
|
|
|
|
trace!(target: "miner", "prepare_work: Checking whether we need to reseal: orig={:?} last={:?}, this={:?}", original_work_hash, last_work_hash, block.block().fields().header.hash());
|
|
|
|
|
let (work, is_new) = if last_work_hash.map_or(true, |h| h != block.block().fields().header.hash()) {
|
|
|
|
|
trace!(target: "miner", "Pushing a new, refreshed or borrowed pending {}...", block.block().fields().header.hash());
|
|
|
|
|
trace!(target: "miner", "prepare_work: Pushing a new, refreshed or borrowed pending {}...", block.block().fields().header.hash());
|
|
|
|
|
let pow_hash = block.block().fields().header.hash();
|
|
|
|
|
let number = block.block().fields().header.number();
|
|
|
|
|
let difficulty = *block.block().fields().header.difficulty();
|
|
|
|
|
@@ -386,7 +388,7 @@ impl Miner {
|
|
|
|
|
} else {
|
|
|
|
|
(None, false)
|
|
|
|
|
};
|
|
|
|
|
trace!(target: "miner", "prepare_sealing: leaving (last={:?})", sealing_work.queue.peek_last_ref().map(|b| b.block().fields().header.hash()));
|
|
|
|
|
trace!(target: "miner", "prepare_work: leaving (last={:?})", sealing_work.queue.peek_last_ref().map(|b| b.block().fields().header.hash()));
|
|
|
|
|
(work, is_new)
|
|
|
|
|
};
|
|
|
|
|
if is_new {
|
|
|
|
|
@@ -395,26 +397,11 @@ impl Miner {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Prepares new block for sealing including top transactions from queue.
|
|
|
|
|
#[cfg_attr(feature="dev", allow(match_same_arms))]
|
|
|
|
|
#[cfg_attr(feature="dev", allow(cyclomatic_complexity))]
|
|
|
|
|
fn prepare_block_and_work(&self, chain: &MiningBlockChainClient) {
|
|
|
|
|
trace!(target: "miner", "prepare_sealing: entering");
|
|
|
|
|
trace!(target: "miner", "prepare_block_and_work: entering");
|
|
|
|
|
|
|
|
|
|
let (block, original_work_hash) = self.prepare_block(chain);
|
|
|
|
|
|
|
|
|
|
let block = if !block.transactions().is_empty() {
|
|
|
|
|
let block_opt = self.seal_block_internally(block).map(|sealed|
|
|
|
|
|
match chain.import_block(sealed.rlp_bytes()) {
|
|
|
|
|
Ok(_) => trace!(target: "miner", "prepare_sealing: sealed internally and imported. leaving."),
|
|
|
|
|
_ => warn!("prepare_sealing: ERROR: could not import internally sealed block. WTF?"),
|
|
|
|
|
}
|
|
|
|
|
).err().unwrap_or(None);
|
|
|
|
|
if block_opt.is_none() { return; }
|
|
|
|
|
block_opt.unwrap()
|
|
|
|
|
} else {
|
|
|
|
|
block
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.prepare_work(block, original_work_hash);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -425,12 +412,12 @@ impl Miner {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns true if we had to prepare new pending block
|
|
|
|
|
fn enable_and_prepare_sealing(&self, chain: &MiningBlockChainClient) -> bool {
|
|
|
|
|
trace!(target: "miner", "enable_and_prepare_sealing: entering");
|
|
|
|
|
fn prepare_work_sealing(&self, chain: &MiningBlockChainClient) -> bool {
|
|
|
|
|
trace!(target: "miner", "prepare_work_sealing: entering");
|
|
|
|
|
let prepare_new = {
|
|
|
|
|
let mut sealing_work = self.sealing_work.lock();
|
|
|
|
|
let have_work = sealing_work.queue.peek_last_ref().is_some();
|
|
|
|
|
trace!(target: "miner", "enable_and_prepare_sealing: have_work={}", have_work);
|
|
|
|
|
trace!(target: "miner", "prepare_work_sealing: have_work={}", have_work);
|
|
|
|
|
if !have_work {
|
|
|
|
|
sealing_work.enabled = true;
|
|
|
|
|
true
|
|
|
|
|
@@ -448,7 +435,7 @@ impl Miner {
|
|
|
|
|
let mut sealing_block_last_request = self.sealing_block_last_request.lock();
|
|
|
|
|
let best_number = chain.chain_info().best_block_number;
|
|
|
|
|
if *sealing_block_last_request != best_number {
|
|
|
|
|
trace!(target: "miner", "enable_and_prepare_sealing: Miner received request (was {}, now {}) - waking up.", *sealing_block_last_request, best_number);
|
|
|
|
|
trace!(target: "miner", "prepare_work_sealing: Miner received request (was {}, now {}) - waking up.", *sealing_block_last_request, best_number);
|
|
|
|
|
*sealing_block_last_request = best_number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -667,7 +654,7 @@ impl MinerService for Miner {
|
|
|
|
|
trace!(target: "own_tx", "Importing transaction: {:?}", transaction);
|
|
|
|
|
|
|
|
|
|
let imported = {
|
|
|
|
|
// Be sure to release the lock before we call enable_and_prepare_sealing
|
|
|
|
|
// Be sure to release the lock before we call prepare_work_sealing
|
|
|
|
|
let mut transaction_queue = self.transaction_queue.lock();
|
|
|
|
|
let import = self.add_transactions_to_queue(
|
|
|
|
|
chain, vec![transaction], TransactionOrigin::Local, &mut transaction_queue
|
|
|
|
|
@@ -694,7 +681,7 @@ impl MinerService for Miner {
|
|
|
|
|
if imported.is_ok() && self.options.reseal_on_own_tx && self.tx_reseal_allowed() {
|
|
|
|
|
// 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);
|
|
|
|
|
let prepared = self.prepare_work_sealing(chain);
|
|
|
|
|
// If new block has not been prepared (means we already had one)
|
|
|
|
|
// we need to update sealing
|
|
|
|
|
if !prepared {
|
|
|
|
|
@@ -836,7 +823,11 @@ impl MinerService for Miner {
|
|
|
|
|
// | NOTE Code below requires transaction_queue and sealing_work locks. |
|
|
|
|
|
// | Make sure to release the locks before calling that method. |
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
self.prepare_block_and_work(chain);
|
|
|
|
|
if self.options.internal_sealing {
|
|
|
|
|
self.seal_and_import_block_internally(chain);
|
|
|
|
|
} else {
|
|
|
|
|
self.prepare_block_and_work(chain);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -846,7 +837,7 @@ impl MinerService for Miner {
|
|
|
|
|
|
|
|
|
|
fn map_sealing_work<F, T>(&self, chain: &MiningBlockChainClient, f: F) -> Option<T> where F: FnOnce(&ClosedBlock) -> T {
|
|
|
|
|
trace!(target: "miner", "map_sealing_work: entering");
|
|
|
|
|
self.enable_and_prepare_sealing(chain);
|
|
|
|
|
self.prepare_work_sealing(chain);
|
|
|
|
|
trace!(target: "miner", "map_sealing_work: sealing prepared");
|
|
|
|
|
let mut sealing_work = self.sealing_work.lock();
|
|
|
|
|
let ret = sealing_work.queue.use_last_ref();
|
|
|
|
|
@@ -994,6 +985,7 @@ mod tests {
|
|
|
|
|
force_sealing: false,
|
|
|
|
|
reseal_on_external_tx: false,
|
|
|
|
|
reseal_on_own_tx: true,
|
|
|
|
|
internal_sealing: false,
|
|
|
|
|
reseal_min_period: Duration::from_secs(5),
|
|
|
|
|
tx_gas_limit: !U256::zero(),
|
|
|
|
|
tx_queue_size: 1024,
|
|
|
|
|
@@ -1034,7 +1026,7 @@ mod tests {
|
|
|
|
|
assert_eq!(miner.pending_transactions_hashes().len(), 1);
|
|
|
|
|
assert_eq!(miner.pending_receipts().len(), 1);
|
|
|
|
|
// This method will let us know if pending block was created (before calling that method)
|
|
|
|
|
assert_eq!(miner.enable_and_prepare_sealing(&client), false);
|
|
|
|
|
assert_eq!(miner.prepare_work_sealing(&client), false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
@@ -1064,6 +1056,6 @@ mod tests {
|
|
|
|
|
assert_eq!(miner.pending_transactions().len(), 0);
|
|
|
|
|
assert_eq!(miner.pending_receipts().len(), 0);
|
|
|
|
|
// This method will let us know if pending block was created (before calling that method)
|
|
|
|
|
assert_eq!(miner.enable_and_prepare_sealing(&client), true);
|
|
|
|
|
assert_eq!(miner.prepare_work_sealing(&client), true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|