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

Commit

Permalink
Merge branch 'master' into dp/chore/aura-warn-when-validators-is-1-or…
Browse files Browse the repository at this point in the history
…-even

* master:
  Stop breaking out of loop if a non-canonical hash is found (#10729)
  Refactor Clique stepping (#10691)
  • Loading branch information
dvdplm committed Jun 10, 2019
2 parents b5743b8 + 5da8da6 commit 070e585
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 111 deletions.
55 changes: 27 additions & 28 deletions ethcore/src/engines/clique/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ use std::collections::VecDeque;
use std::sync::{Arc, Weak};
use std::thread;
use std::time;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::time::{Instant, Duration, SystemTime, UNIX_EPOCH};

use block::ExecutedBlock;
use client::{BlockId, EngineClient};
Expand All @@ -89,11 +89,9 @@ use time_utils::CheckedSystemTime;

use self::block_state::CliqueBlockState;
use self::params::CliqueParams;
use self::step_service::StepService;

mod params;
mod block_state;
mod step_service;
mod util;

// TODO(niklasad1): extract tester types into a separate mod to be shared in the code base
Expand Down Expand Up @@ -168,7 +166,6 @@ pub struct Clique {
block_state_by_hash: RwLock<LruCache<H256, CliqueBlockState>>,
proposals: RwLock<HashMap<Address, VoteType>>,
signer: RwLock<Option<Box<EngineSigner>>>,
step_service: Option<StepService>,
}

#[cfg(test)]
Expand All @@ -181,33 +178,45 @@ pub struct Clique {
pub block_state_by_hash: RwLock<LruCache<H256, CliqueBlockState>>,
pub proposals: RwLock<HashMap<Address, VoteType>>,
pub signer: RwLock<Option<Box<EngineSigner>>>,
pub step_service: Option<StepService>,
}

impl Clique {
/// Initialize Clique engine from empty state.
pub fn new(params: CliqueParams, machine: EthereumMachine) -> Result<Arc<Self>, Error> {
let mut engine = Clique {
/// Step Clique at most every 2 seconds
const SEALING_FREQ: Duration = Duration::from_secs(2);

let engine = Clique {
epoch_length: params.epoch,
period: params.period,
client: Default::default(),
block_state_by_hash: RwLock::new(LruCache::new(STATE_CACHE_NUM)),
proposals: Default::default(),
signer: Default::default(),
machine,
step_service: None,
};
if params.period > 0 {
engine.step_service = Some(StepService::new());
let engine = Arc::new(engine);
let weak_eng = Arc::downgrade(&engine);
if let Some(step_service) = &engine.step_service {
step_service.start(weak_eng);
}
Ok(engine)
} else {
Ok(Arc::new(engine))
}
let engine = Arc::new(engine);
let weak_eng = Arc::downgrade(&engine);

thread::Builder::new().name("StepService".into())
.spawn(move || {
loop {
let next_step_at = Instant::now() + SEALING_FREQ;
trace!(target: "miner", "StepService: triggering sealing");
if let Some(eng) = weak_eng.upgrade() {
eng.step()
} else {
warn!(target: "shutdown", "StepService: engine is dropped; exiting.");
break;
}

let now = Instant::now();
if now < next_step_at {
thread::sleep(next_step_at - now);
}
}
})?;
Ok(engine)
}

#[cfg(test)]
Expand All @@ -225,7 +234,6 @@ impl Clique {
proposals: Default::default(),
signer: Default::default(),
machine: Spec::new_test_machine(),
step_service: None,
}
}

Expand Down Expand Up @@ -348,15 +356,6 @@ impl Clique {
}
}

impl Drop for Clique {
fn drop(&mut self) {
if let Some(step_service) = &self.step_service {
trace!(target: "shutdown", "Clique; stopping step service");
step_service.stop();
}
}
}

impl Engine<EthereumMachine> for Clique {
fn name(&self) -> &str { "Clique" }

Expand Down
80 changes: 0 additions & 80 deletions ethcore/src/engines/clique/step_service.rs

This file was deleted.

3 changes: 0 additions & 3 deletions ethcore/src/engines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,6 @@ pub trait Engine<M: Machine>: Sync + Send {
/// Trigger next step of the consensus engine.
fn step(&self) {}

/// Stops any services that the may hold the Engine and makes it safe to drop.
fn stop(&mut self) {}

/// Create a factory for building snapshot chunks and restoring from them.
/// Returning `None` indicates that this engine doesn't support snapshot creation.
fn snapshot_components(&self) -> Option<Box<SnapshotComponents>> {
Expand Down

0 comments on commit 070e585

Please sign in to comment.