Skip to content

Commit

Permalink
Block re-sealing restored
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewAR2 committed Aug 28, 2024
1 parent 94e2ca5 commit 8725d66
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
10 changes: 5 additions & 5 deletions crates/ethcore/src/engines/authority_round/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2538,19 +2538,19 @@ impl Engine<EthereumMachine> for AuthorityRound {
}

fn get_gas_price(&self, header: &Header) -> Option<U256> {
let cl = self.upgrade_client_or("Failed to get the client").expect("Failed to get the client");
let client = cl.as_full_client().unwrap();
if let Some(address) = self.current_fees_address(header) {
let cl = self.upgrade_client_or("Failed to get the client").expect("Failed to get the client");
let client = cl.as_full_client().unwrap();
block_gas_price::block_gas_price(client, header, address)
} else {
None
}
}

fn get_fee_params(&self, header: &Header) -> Option<FeesParams> {
let cl = self.upgrade_client_or("Failed to get the client").expect("Failed to get the client");
let client = cl.as_full_client().unwrap();
if let Some(address) = self.current_fees_address(header) {
let cl = self.upgrade_client_or("Failed to get the client").expect("Failed to get the client");
let client = cl.as_full_client().unwrap();
block_tx_fee::block_tx_fee(client, header, address)
} else {
None
Expand All @@ -2566,7 +2566,7 @@ impl Engine<EthereumMachine> for AuthorityRound {
// trace!(target: "engine", "Got fees contract transition on block number {}", header.number());
return Some(*address);
} else {
trace!(target: "engine", "No fees contract transition on blcok number {}", header.number());
// trace!(target: "engine", "No fees contract transition on blcok number {}", header.number());
return None;
}
}
Expand Down
66 changes: 66 additions & 0 deletions crates/ethcore/src/miner/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,19 @@ impl Author {
struct SealingWork {
queue: UsingQueue<ClosedBlock>,
enabled: bool,
next_allowed_reseal: Instant,
next_mandatory_reseal: Instant,
// block number when sealing work was last requested
last_request: Option<u64>,
}

impl SealingWork {
/// Are we allowed to do a non-mandatory reseal?
fn reseal_allowed(&self) -> bool {
Instant::now() >= self.next_allowed_reseal
}
}

/// Keeps track of transactions using priority queue and holds currently mined block.
/// Handles preparing work for "work sealing" or seals "internally" if Engine does not require work.
pub struct Miner {
Expand Down Expand Up @@ -283,6 +292,8 @@ impl Miner {
queue: UsingQueue::new(options.work_queue_size),
enabled: options.force_sealing
|| spec.engine.sealing_state() != SealingState::External,
next_allowed_reseal: Instant::now(),
next_mandatory_reseal: Instant::now() + options.reseal_max_period,
last_request: None,
}),
params: RwLock::new(AuthoringParams::default()),
Expand Down Expand Up @@ -713,6 +724,11 @@ impl Miner {
return false;
}

if !sealing.reseal_allowed() {
trace!(target: "miner", "requires_reseal: reseal too early, next allowed reseal {:?} < {:?}", Instant::now(), sealing.next_allowed_reseal);
return false;
}

trace!(target: "miner", "requires_reseal: sealing enabled");

// Disable sealing if there were no requests for SEALING_TIMEOUT_IN_BLOCKS
Expand Down Expand Up @@ -746,6 +762,7 @@ impl Miner {
false
} else {
// sealing enabled and we don't want to sleep.
sealing.next_allowed_reseal = Instant::now(); // + self.options.reseal_min_period;
true
}
}
Expand All @@ -759,8 +776,10 @@ impl Miner {
C: BlockChain + SealedBlockImporter,
{
{
let sealing = self.sealing.lock();
if block.transactions.is_empty()
&& !self.forced_sealing()
&& Instant::now() <= sealing.next_mandatory_reseal
{
return false;
}
Expand All @@ -782,6 +801,7 @@ impl Miner {
trace!(target: "miner", "Received a Proposal seal.");
{
let mut sealing = self.sealing.lock();
sealing.next_mandatory_reseal = Instant::now() + self.options.reseal_max_period;
sealing.queue.set_pending(block.clone());
sealing.queue.use_last_ref();
}
Expand All @@ -804,6 +824,11 @@ impl Miner {
// Directly import a regular sealed block.
Seal::Regular(seal) => {
trace!(target: "miner", "Received a Regular seal.");
{
let mut sealing = self.sealing.lock();
sealing.next_mandatory_reseal = Instant::now() + self.options.reseal_max_period;
}

block
.lock()
.seal(&*self.engine, seal)
Expand Down Expand Up @@ -943,6 +968,22 @@ impl Miner {

preparation_status
}

/// Prepare pending block, check whether sealing is needed, and then update sealing.
fn prepare_and_update_sealing<C: miner::BlockChainClient>(&self, chain: &C) {
// Make sure to do it after transaction is imported and lock is dropped.
// We need to create pending block and enable sealing.
let sealing_state = self.engine.sealing_state();

if sealing_state == SealingState::Ready
|| self.prepare_pending_block(chain) == BlockPreparationStatus::NotPrepared
{
// If new block has not been prepared (means we already had one)
// or Engine might be able to seal internally,
// we need to update sealing.
self.update_sealing(chain, ForceUpdateSealing::No);
}
}
}

const SEALING_TIMEOUT_IN_BLOCKS: u64 = 5;
Expand Down Expand Up @@ -1061,6 +1102,17 @@ impl miner::MinerService for Miner {
.collect(),
);

// --------------------------------------------------------------------------
// | NOTE Code below requires sealing locks. |
// | Make sure to release the locks before calling that method. |
// --------------------------------------------------------------------------
if !results.is_empty()
&& self.options.reseal_on_external_tx
&& self.sealing.lock().reseal_allowed()
{
self.prepare_and_update_sealing(chain);
}

results
}

Expand All @@ -1080,6 +1132,15 @@ impl miner::MinerService for Miner {
.pop()
.expect("one result returned per added transaction; one added => one result; qed");

// --------------------------------------------------------------------------
// | NOTE Code below requires sealing locks. |
// | Make sure to release the locks before calling that method. |
// --------------------------------------------------------------------------
if imported.is_ok() && self.options.reseal_on_own_tx && self.sealing.lock().reseal_allowed()
{
self.prepare_and_update_sealing(chain);
}

imported
}

Expand Down Expand Up @@ -1474,6 +1535,11 @@ impl miner::MinerService for Miner {
}

if has_new_best_block || (imported.len() > 0 && self.options.reseal_on_uncle) {
// t_nb 10.3 Reset `next_allowed_reseal` in case a block is imported.
// Even if min_period is high, we will always attempt to create
// new pending block.
self.sealing.lock().next_allowed_reseal = Instant::now();

if !is_internal_import {
// t_nb 10.4 if it is internal import update sealing
// --------------------------------------------------------------------------
Expand Down

0 comments on commit 8725d66

Please sign in to comment.