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

Commit 4f00ac7

Browse files
committed
Merge branch 'master' into dp/chore/aura-log-validator-set-in-epoch-manager
* master: Stop breaking out of loop if a non-canonical hash is found (#10729) Refactor Clique stepping (#10691)
2 parents 8ed48b4 + 5da8da6 commit 4f00ac7

File tree

3 files changed

+27
-111
lines changed

3 files changed

+27
-111
lines changed

ethcore/src/engines/clique/mod.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use std::collections::VecDeque;
6464
use std::sync::{Arc, Weak};
6565
use std::thread;
6666
use std::time;
67-
use std::time::{Duration, SystemTime, UNIX_EPOCH};
67+
use std::time::{Instant, Duration, SystemTime, UNIX_EPOCH};
6868

6969
use block::ExecutedBlock;
7070
use client::{BlockId, EngineClient};
@@ -89,11 +89,9 @@ use time_utils::CheckedSystemTime;
8989

9090
use self::block_state::CliqueBlockState;
9191
use self::params::CliqueParams;
92-
use self::step_service::StepService;
9392

9493
mod params;
9594
mod block_state;
96-
mod step_service;
9795
mod util;
9896

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

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

187183
impl Clique {
188184
/// Initialize Clique engine from empty state.
189185
pub fn new(params: CliqueParams, machine: EthereumMachine) -> Result<Arc<Self>, Error> {
190-
let mut engine = Clique {
186+
/// Step Clique at most every 2 seconds
187+
const SEALING_FREQ: Duration = Duration::from_secs(2);
188+
189+
let engine = Clique {
191190
epoch_length: params.epoch,
192191
period: params.period,
193192
client: Default::default(),
194193
block_state_by_hash: RwLock::new(LruCache::new(STATE_CACHE_NUM)),
195194
proposals: Default::default(),
196195
signer: Default::default(),
197196
machine,
198-
step_service: None,
199197
};
200-
if params.period > 0 {
201-
engine.step_service = Some(StepService::new());
202-
let engine = Arc::new(engine);
203-
let weak_eng = Arc::downgrade(&engine);
204-
if let Some(step_service) = &engine.step_service {
205-
step_service.start(weak_eng);
206-
}
207-
Ok(engine)
208-
} else {
209-
Ok(Arc::new(engine))
210-
}
198+
let engine = Arc::new(engine);
199+
let weak_eng = Arc::downgrade(&engine);
200+
201+
thread::Builder::new().name("StepService".into())
202+
.spawn(move || {
203+
loop {
204+
let next_step_at = Instant::now() + SEALING_FREQ;
205+
trace!(target: "miner", "StepService: triggering sealing");
206+
if let Some(eng) = weak_eng.upgrade() {
207+
eng.step()
208+
} else {
209+
warn!(target: "shutdown", "StepService: engine is dropped; exiting.");
210+
break;
211+
}
212+
213+
let now = Instant::now();
214+
if now < next_step_at {
215+
thread::sleep(next_step_at - now);
216+
}
217+
}
218+
})?;
219+
Ok(engine)
211220
}
212221

213222
#[cfg(test)]
@@ -225,7 +234,6 @@ impl Clique {
225234
proposals: Default::default(),
226235
signer: Default::default(),
227236
machine: Spec::new_test_machine(),
228-
step_service: None,
229237
}
230238
}
231239

@@ -348,15 +356,6 @@ impl Clique {
348356
}
349357
}
350358

351-
impl Drop for Clique {
352-
fn drop(&mut self) {
353-
if let Some(step_service) = &self.step_service {
354-
trace!(target: "shutdown", "Clique; stopping step service");
355-
step_service.stop();
356-
}
357-
}
358-
}
359-
360359
impl Engine<EthereumMachine> for Clique {
361360
fn name(&self) -> &str { "Clique" }
362361

ethcore/src/engines/clique/step_service.rs

Lines changed: 0 additions & 80 deletions
This file was deleted.

ethcore/src/engines/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,6 @@ pub trait Engine<M: Machine>: Sync + Send {
437437
/// Trigger next step of the consensus engine.
438438
fn step(&self) {}
439439

440-
/// Stops any services that the may hold the Engine and makes it safe to drop.
441-
fn stop(&mut self) {}
442-
443440
/// Create a factory for building snapshot chunks and restoring from them.
444441
/// Returning `None` indicates that this engine doesn't support snapshot creation.
445442
fn snapshot_components(&self) -> Option<Box<SnapshotComponents>> {

0 commit comments

Comments
 (0)