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

Fix compiler warning (that will become an error) #10683

Merged
merged 6 commits into from
May 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions ethcore/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2545,16 +2545,6 @@ impl ProvingBlockChainClient for Client {

impl SnapshotClient for Client {}

impl Drop for Client {
fn drop(&mut self) {
if let Some(c) = Arc::get_mut(&mut self.engine) {
c.stop()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT Clique is the only engine that actually impls stop(). And the Arc::get_mut here fails every time.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change https://github.com/paritytech/parity-ethereum/blob/master/ethcore/src/engines/mod.rs#L429 to fn stop(&self) it is broken, because Engine is stored as Arc<Engine> and doesn't support mutation.

I'm sorry that I didn't catch when reviewing it :(

} else {
warn!(target: "shutdown", "unable to get mut ref for engine for shutdown.");
}
}
}

/// Returns `LocalizedReceipt` given `LocalizedTransaction`
/// and a vector of receipts from given block up to transaction index.
fn transaction_receipt(
Expand Down
46 changes: 25 additions & 21 deletions ethcore/src/engines/clique/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub struct Clique {
block_state_by_hash: RwLock<LruCache<H256, CliqueBlockState>>,
proposals: RwLock<HashMap<Address, VoteType>>,
signer: RwLock<Option<Box<EngineSigner>>>,
step_service: Option<Arc<StepService>>,
step_service: Option<StepService>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we disallow period=0 we could get rid of the Option here as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's planned for the follow up PR. :)

}

#[cfg(test)]
Expand All @@ -181,30 +181,33 @@ 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<Arc<StepService>>,
pub step_service: Option<StepService>,
}

impl Clique {
/// Initialize Clique engine from empty state.
pub fn new(our_params: CliqueParams, machine: EthereumMachine) -> Result<Arc<Self>, Error> {
pub fn new(params: CliqueParams, machine: EthereumMachine) -> Result<Arc<Self>, Error> {
let mut engine = Clique {
epoch_length: our_params.epoch,
period: our_params.period,
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,
};

let res = Arc::new(engine);

if our_params.period > 0 {
engine.step_service = Some(StepService::start(Arc::downgrade(&res) as Weak<Engine<_>>));
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))
}

Ok(res)
}

#[cfg(test)]
Expand Down Expand Up @@ -345,6 +348,15 @@ 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 Expand Up @@ -695,7 +707,7 @@ impl Engine<EthereumMachine> for Clique {
trace!(target: "engine", "populate_from_parent in sealing");

// It's unclear how to prevent creating new blocks unless we are authorized, the best way (and geth does this too)
// it's just to ignore setting an correct difficulty here, we will check authorization in next step in generate_seal anyway.
// it's just to ignore setting a correct difficulty here, we will check authorization in next step in generate_seal anyway.
if let Some(signer) = self.signer.read().as_ref() {
let state = match self.state(&parent) {
Err(e) => {
Expand Down Expand Up @@ -744,14 +756,6 @@ impl Engine<EthereumMachine> for Clique {
}
}

fn stop(&mut self) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually need stop on the Engine trait then? Is it used anywhere?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change https://github.com/paritytech/parity-ethereum/blob/master/ethcore/src/engines/mod.rs#L429 to fn stop(&self) it is broken, because Engine is stored as Arc and doesn't support mutation.

It is not used and it is broken. Either remove it or change it to fn stop(&self)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's planned for the follow up PR. :)

(What, you a mind reader @tomusdrw ???)

if let Some(mut s) = self.step_service.as_mut() {
Arc::get_mut(&mut s).map(|x| x.stop());
} else {
warn!(target: "engine", "Stopping `CliqueStepService` failed requires mutable access");
}
}

/// Clique timestamp is set to parent + period , or current time which ever is higher.
fn open_block_header_timestamp(&self, parent_timestamp: u64) -> u64 {
let now = time::SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap_or_default();
Expand Down
33 changes: 18 additions & 15 deletions ethcore/src/engines/clique/step_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,29 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use std::thread;
use std::sync::Arc;
use parking_lot::RwLock;

use engines::Engine;
use machine::Machine;

/// Service that is managing the engine
pub struct StepService {
shutdown: Arc<AtomicBool>,
thread: Option<thread::JoinHandle<()>>,
thread: RwLock<Option<thread::JoinHandle<()>>>,
}

impl StepService {
/// Start the `StepService`
pub fn start<M: Machine + 'static>(engine: Weak<Engine<M>>) -> Arc<Self> {
/// Create a new StepService without spawning a sealing thread.
pub fn new() -> Self {
let shutdown = Arc::new(AtomicBool::new(false));
let s = shutdown.clone();
StepService { shutdown, thread: RwLock::new(None) }
}

/// Start the StepService: spawns a thread that loops and triggers a sealing operation every 2sec.
pub fn start<M: Machine + 'static>(&self, engine: Weak<Engine<M>>) {
let shutdown = self.shutdown.clone();

let thread = thread::Builder::new()
let thr = thread::Builder::new()
.name("CliqueStepService".into())
.spawn(move || {
// startup delay.
Expand All @@ -45,8 +51,8 @@ impl StepService {
loop {
// see if we are in shutdown.
if shutdown.load(Ordering::Acquire) {
trace!(target: "miner", "CliqueStepService: received shutdown signal!");
break;
trace!(target: "shutdown", "CliqueStepService: received shutdown signal!");
break;
}

trace!(target: "miner", "CliqueStepService: triggering sealing");
Expand All @@ -57,20 +63,17 @@ impl StepService {
// Yield
thread::sleep(Duration::from_millis(2000));
}
trace!(target: "miner", "CliqueStepService: shutdown.");
trace!(target: "shutdown", "CliqueStepService: exited loop, shutdown.");
}).expect("CliqueStepService thread failed");

Arc::new(StepService {
shutdown: s,
thread: Some(thread),
})
*self.thread.write() = Some(thr);
}

/// Stop the `StepService`
pub fn stop(&mut self) {
trace!(target: "miner", "CliqueStepService: shutting down.");
pub fn stop(&self) {
trace!(target: "shutdown", "CliqueStepService: signalling shutting to stepping thread.");
self.shutdown.store(true, Ordering::Release);
if let Some(t) = self.thread.take() {
if let Some(t) = self.thread.write().take() {
t.join().expect("CliqueStepService thread panicked!");
}
}
Expand Down