Introduce more optional features in ethcore (#9020)
* Make work-notify an optional feature * More optional ethcore features: price-info, stratum * Make ethcore features part of dependency instead of local features * Put cfg gate in right location * Feature gate register function on work-notify
This commit is contained in:
committed by
Afri Schoedon
parent
3db353f356
commit
71bbcd54ff
@@ -70,6 +70,7 @@ extern crate ethcore_io as io;
|
||||
extern crate ethcore_bytes as bytes;
|
||||
extern crate ethcore_logger;
|
||||
extern crate ethcore_miner;
|
||||
#[cfg(feature = "stratum")]
|
||||
extern crate ethcore_stratum;
|
||||
extern crate ethcore_transaction as transaction;
|
||||
extern crate ethereum_types;
|
||||
|
||||
@@ -24,6 +24,7 @@ use engines::{EthEngine, Seal};
|
||||
use error::{Error, ErrorKind, ExecutionError};
|
||||
use ethcore_miner::gas_pricer::GasPricer;
|
||||
use ethcore_miner::pool::{self, TransactionQueue, VerifiedTransaction, QueueStatus, PrioritizationStrategy};
|
||||
#[cfg(feature = "work-notify")]
|
||||
use ethcore_miner::work_notify::NotifyWork;
|
||||
use ethereum_types::{H256, U256, Address};
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
@@ -200,6 +201,7 @@ pub struct Miner {
|
||||
// NOTE [ToDr] When locking always lock in this order!
|
||||
sealing: Mutex<SealingWork>,
|
||||
params: RwLock<AuthoringParams>,
|
||||
#[cfg(feature = "work-notify")]
|
||||
listeners: RwLock<Vec<Box<NotifyWork>>>,
|
||||
nonce_cache: RwLock<HashMap<Address, U256>>,
|
||||
gas_pricer: Mutex<GasPricer>,
|
||||
@@ -212,6 +214,7 @@ pub struct Miner {
|
||||
|
||||
impl Miner {
|
||||
/// Push listener that will handle new jobs
|
||||
#[cfg(feature = "work-notify")]
|
||||
pub fn add_work_listener(&self, notifier: Box<NotifyWork>) {
|
||||
self.listeners.write().push(notifier);
|
||||
self.sealing.lock().enabled = true;
|
||||
@@ -238,6 +241,7 @@ impl Miner {
|
||||
last_request: None,
|
||||
}),
|
||||
params: RwLock::new(AuthoringParams::default()),
|
||||
#[cfg(feature = "work-notify")]
|
||||
listeners: RwLock::new(vec![]),
|
||||
gas_pricer: Mutex::new(gas_pricer),
|
||||
nonce_cache: RwLock::new(HashMap::with_capacity(1024)),
|
||||
@@ -491,7 +495,14 @@ impl Miner {
|
||||
/// 1. --force-sealing CLI parameter is provided
|
||||
/// 2. There are listeners awaiting new work packages (e.g. remote work notifications or stratum).
|
||||
fn forced_sealing(&self) -> bool {
|
||||
self.options.force_sealing || !self.listeners.read().is_empty()
|
||||
let listeners_empty = {
|
||||
#[cfg(feature = "work-notify")]
|
||||
{ self.listeners.read().is_empty() }
|
||||
#[cfg(not(feature = "work-notify"))]
|
||||
{ true }
|
||||
};
|
||||
|
||||
self.options.force_sealing || !listeners_empty
|
||||
}
|
||||
|
||||
/// Check is reseal is allowed and necessary.
|
||||
@@ -639,9 +650,13 @@ impl Miner {
|
||||
let is_new = original_work_hash.map_or(true, |h| h != block_hash);
|
||||
|
||||
sealing.queue.push(block);
|
||||
// If push notifications are enabled we assume all work items are used.
|
||||
if is_new && !self.listeners.read().is_empty() {
|
||||
sealing.queue.use_last_ref();
|
||||
|
||||
#[cfg(feature = "work-notify")]
|
||||
{
|
||||
// If push notifications are enabled we assume all work items are used.
|
||||
if is_new && !self.listeners.read().is_empty() {
|
||||
sealing.queue.use_last_ref();
|
||||
}
|
||||
}
|
||||
|
||||
(Some((block_hash, *block_header.difficulty(), block_header.number())), is_new)
|
||||
@@ -655,12 +670,23 @@ impl Miner {
|
||||
);
|
||||
(work, is_new)
|
||||
};
|
||||
if is_new {
|
||||
work.map(|(pow_hash, difficulty, number)| {
|
||||
for notifier in self.listeners.read().iter() {
|
||||
notifier.notify(pow_hash, difficulty, number)
|
||||
}
|
||||
});
|
||||
|
||||
#[cfg(feature = "work-notify")]
|
||||
{
|
||||
if is_new {
|
||||
work.map(|(pow_hash, difficulty, number)| {
|
||||
for notifier in self.listeners.read().iter() {
|
||||
notifier.notify(pow_hash, difficulty, number)
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// NB: hack to use variables to avoid warning.
|
||||
#[cfg(not(feature = "work-notify"))]
|
||||
{
|
||||
let _work = work;
|
||||
let _is_new = is_new;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1442,6 +1468,7 @@ mod tests {
|
||||
assert!(!miner.is_currently_sealing());
|
||||
}
|
||||
|
||||
#[cfg(feature = "work-notify")]
|
||||
#[test]
|
||||
fn should_mine_if_fetch_work_request() {
|
||||
struct DummyNotifyWork;
|
||||
|
||||
@@ -23,6 +23,7 @@ mod miner;
|
||||
mod service_transaction_checker;
|
||||
|
||||
pub mod pool_client;
|
||||
#[cfg(feature = "stratum")]
|
||||
pub mod stratum;
|
||||
|
||||
pub use self::miner::{Miner, MinerOptions, Penalization, PendingSet, AuthoringParams};
|
||||
|
||||
@@ -24,10 +24,12 @@ use client::{Client, ImportSealedBlock};
|
||||
use ethereum_types::{H64, H256, clean_0x, U256};
|
||||
use ethereum::ethash::Ethash;
|
||||
use ethash::SeedHashCompute;
|
||||
#[cfg(feature = "work-notify")]
|
||||
use ethcore_miner::work_notify::NotifyWork;
|
||||
#[cfg(feature = "work-notify")]
|
||||
use ethcore_stratum::PushWorkHandler;
|
||||
use ethcore_stratum::{
|
||||
JobDispatcher, PushWorkHandler,
|
||||
Stratum as StratumService, Error as StratumServiceError,
|
||||
JobDispatcher, Stratum as StratumService, Error as StratumServiceError,
|
||||
};
|
||||
use miner::{Miner, MinerService};
|
||||
use parking_lot::Mutex;
|
||||
@@ -209,6 +211,7 @@ impl From<AddrParseError> for Error {
|
||||
fn from(err: AddrParseError) -> Error { Error::Address(err) }
|
||||
}
|
||||
|
||||
#[cfg(feature = "work-notify")]
|
||||
impl NotifyWork for Stratum {
|
||||
fn notify(&self, pow_hash: H256, difficulty: U256, number: u64) {
|
||||
trace!(target: "stratum", "Notify work");
|
||||
@@ -242,6 +245,7 @@ impl Stratum {
|
||||
}
|
||||
|
||||
/// Start STRATUM job dispatcher and register it in the miner
|
||||
#[cfg(feature = "work-notify")]
|
||||
pub fn register(cfg: &Options, miner: Arc<Miner>, client: Weak<Client>) -> Result<(), Error> {
|
||||
let stratum = Stratum::start(cfg, Arc::downgrade(&miner.clone()), client)?;
|
||||
miner.add_work_listener(Box::new(stratum) as Box<NotifyWork>);
|
||||
|
||||
Reference in New Issue
Block a user