diff --git a/Cargo.toml b/Cargo.toml index 7e382bed4..04d0949bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,7 @@ futures-cpupool = "0.1" fdlimit = "0.1" ctrlc = { git = "https://github.com/paritytech/rust-ctrlc.git" } jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.11" } -ethcore = { path = "ethcore" } +ethcore = { path = "ethcore", features = ["work-notify", "price-info", "stratum"] } ethcore-bytes = { path = "util/bytes" } ethcore-io = { path = "util/io" } ethcore-light = { path = "ethcore/light" } diff --git a/ethcore/Cargo.toml b/ethcore/Cargo.toml index 7a6eff7be..756c9a8b1 100644 --- a/ethcore/Cargo.toml +++ b/ethcore/Cargo.toml @@ -16,7 +16,6 @@ crossbeam = "0.3" ethash = { path = "../ethash" } ethcore-bloom-journal = { path = "../util/bloom" } ethcore-bytes = { path = "../util/bytes" } -fetch = { path = "../util/fetch" } hashdb = { path = "../util/hashdb" } memorydb = { path = "../util/memorydb" } patricia-trie = { path = "../util/patricia_trie" } @@ -26,7 +25,7 @@ error-chain = { version = "0.12", default-features = false } ethcore-io = { path = "../util/io" } ethcore-logger = { path = "../logger" } ethcore-miner = { path = "../miner" } -ethcore-stratum = { path = "./stratum" } +ethcore-stratum = { path = "./stratum", optional = true } ethcore-transaction = { path = "./transaction" } ethereum-types = "0.3" memory-cache = { path = "../util/memory_cache" } @@ -99,3 +98,6 @@ test-heavy = [] benches = [] # Compile test helpers test-helpers = ["tempdir"] +work-notify = ["ethcore-miner/work-notify"] +price-info = ["ethcore-miner/price-info"] +stratum = ["ethcore-stratum"] diff --git a/ethcore/src/lib.rs b/ethcore/src/lib.rs index 3663a1cc9..6e3cae4a4 100644 --- a/ethcore/src/lib.rs +++ b/ethcore/src/lib.rs @@ -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; diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index 29b0f8850..81ace93fe 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -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, params: RwLock, + #[cfg(feature = "work-notify")] listeners: RwLock>>, nonce_cache: RwLock>, gas_pricer: Mutex, @@ -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) { 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; diff --git a/ethcore/src/miner/mod.rs b/ethcore/src/miner/mod.rs index 8886fff54..cc56bf01f 100644 --- a/ethcore/src/miner/mod.rs +++ b/ethcore/src/miner/mod.rs @@ -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}; diff --git a/ethcore/src/miner/stratum.rs b/ethcore/src/miner/stratum.rs index 0fd892bf5..26fb3eae6 100644 --- a/ethcore/src/miner/stratum.rs +++ b/ethcore/src/miner/stratum.rs @@ -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 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, client: Weak) -> Result<(), Error> { let stratum = Stratum::start(cfg, Arc::downgrade(&miner.clone()), client)?; miner.add_work_listener(Box::new(stratum) as Box); diff --git a/miner/Cargo.toml b/miner/Cargo.toml index 9614a8f30..e1e7974a6 100644 --- a/miner/Cargo.toml +++ b/miner/Cargo.toml @@ -8,11 +8,11 @@ authors = ["Parity Technologies "] [dependencies] # Only work_notify, consider a separate crate -ethash = { path = "../ethash" } -fetch = { path = "../util/fetch" } -hyper = "0.11" -parity-reactor = { path = "../util/reactor" } -url = "1" +ethash = { path = "../ethash", optional = true } +fetch = { path = "../util/fetch", optional = true } +hyper = { version = "0.11", optional = true } +parity-reactor = { path = "../util/reactor", optional = true } +url = { version = "1", optional = true } # Miner ansi_term = "0.10" @@ -26,7 +26,7 @@ keccak-hash = { path = "../util/hash" } linked-hash-map = "0.5" log = "0.3" parking_lot = "0.6" -price-info = { path = "../price-info" } +price-info = { path = "../price-info", optional = true } rlp = { path = "../util/rlp" } trace-time = { path = "../util/trace-time" } transaction-pool = { path = "../transaction-pool" } @@ -35,3 +35,6 @@ transaction-pool = { path = "../transaction-pool" } env_logger = "0.4" ethkey = { path = "../ethkey" } rustc-hex = "1.0" + +[features] +work-notify = ["ethash", "fetch", "hyper", "parity-reactor", "url"] diff --git a/miner/src/gas_price_calibrator.rs b/miner/src/gas_price_calibrator.rs new file mode 100644 index 000000000..d2978220a --- /dev/null +++ b/miner/src/gas_price_calibrator.rs @@ -0,0 +1,73 @@ +// Copyright 2015-2018 Parity Technologies (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +//! Auto-updates minimal gas price requirement from a price-info source. + +use std::time::{Instant, Duration}; + +use ansi_term::Colour; +use ethereum_types::U256; +use futures_cpupool::CpuPool; +use price_info::{Client as PriceInfoClient, PriceInfo}; +use price_info::fetch::Client as FetchClient; + +/// Options for the dynamic gas price recalibrator. +#[derive(Debug, PartialEq)] +pub struct GasPriceCalibratorOptions { + /// Base transaction price to match against. + pub usd_per_tx: f32, + /// How frequently we should recalibrate. + pub recalibration_period: Duration, +} + +/// The gas price validator variant for a `GasPricer`. +#[derive(Debug, PartialEq)] +pub struct GasPriceCalibrator { + options: GasPriceCalibratorOptions, + next_calibration: Instant, + price_info: PriceInfoClient, +} + +impl GasPriceCalibrator { + /// Create a new gas price calibrator. + pub fn new(options: GasPriceCalibratorOptions, fetch: FetchClient, p: CpuPool) -> GasPriceCalibrator { + GasPriceCalibrator { + options: options, + next_calibration: Instant::now(), + price_info: PriceInfoClient::new(fetch, p), + } + } + + pub(crate) fn recalibrate(&mut self, set_price: F) { + trace!(target: "miner", "Recalibrating {:?} versus {:?}", Instant::now(), self.next_calibration); + if Instant::now() >= self.next_calibration { + let usd_per_tx = self.options.usd_per_tx; + trace!(target: "miner", "Getting price info"); + + self.price_info.get(move |price: PriceInfo| { + trace!(target: "miner", "Price info arrived: {:?}", price); + let usd_per_eth = price.ethusd; + let wei_per_usd: f32 = 1.0e18 / usd_per_eth; + let gas_per_tx: f32 = 21000.0; + let wei_per_gas: f32 = wei_per_usd * usd_per_tx / gas_per_tx; + info!(target: "miner", "Updated conversion rate to Ξ1 = {} ({} wei/gas)", Colour::White.bold().paint(format!("US${:.2}", usd_per_eth)), Colour::Yellow.bold().paint(format!("{}", wei_per_gas))); + set_price(U256::from(wei_per_gas as u64)); + }); + + self.next_calibration = Instant::now() + self.options.recalibration_period; + } + } +} diff --git a/miner/src/gas_pricer.rs b/miner/src/gas_pricer.rs index ecb69ba57..328778365 100644 --- a/miner/src/gas_pricer.rs +++ b/miner/src/gas_pricer.rs @@ -16,52 +16,9 @@ //! Auto-updates minimal gas price requirement. -use std::time::{Instant, Duration}; - -use ansi_term::Colour; use ethereum_types::U256; -use futures_cpupool::CpuPool; -use price_info::{Client as PriceInfoClient, PriceInfo}; -use price_info::fetch::Client as FetchClient; - -/// Options for the dynamic gas price recalibrator. -#[derive(Debug, PartialEq)] -pub struct GasPriceCalibratorOptions { - /// Base transaction price to match against. - pub usd_per_tx: f32, - /// How frequently we should recalibrate. - pub recalibration_period: Duration, -} - -/// The gas price validator variant for a `GasPricer`. -#[derive(Debug, PartialEq)] -pub struct GasPriceCalibrator { - options: GasPriceCalibratorOptions, - next_calibration: Instant, - price_info: PriceInfoClient, -} - -impl GasPriceCalibrator { - fn recalibrate(&mut self, set_price: F) { - trace!(target: "miner", "Recalibrating {:?} versus {:?}", Instant::now(), self.next_calibration); - if Instant::now() >= self.next_calibration { - let usd_per_tx = self.options.usd_per_tx; - trace!(target: "miner", "Getting price info"); - - self.price_info.get(move |price: PriceInfo| { - trace!(target: "miner", "Price info arrived: {:?}", price); - let usd_per_eth = price.ethusd; - let wei_per_usd: f32 = 1.0e18 / usd_per_eth; - let gas_per_tx: f32 = 21000.0; - let wei_per_gas: f32 = wei_per_usd * usd_per_tx / gas_per_tx; - info!(target: "miner", "Updated conversion rate to Ξ1 = {} ({} wei/gas)", Colour::White.bold().paint(format!("US${:.2}", usd_per_eth)), Colour::Yellow.bold().paint(format!("{}", wei_per_gas))); - set_price(U256::from(wei_per_gas as u64)); - }); - - self.next_calibration = Instant::now() + self.options.recalibration_period; - } - } -} +#[cfg(feature = "price-info")] +use gas_price_calibrator::GasPriceCalibrator; /// Struct to look after updating the acceptable gas price of a miner. #[derive(Debug, PartialEq)] @@ -69,17 +26,15 @@ pub enum GasPricer { /// A fixed gas price in terms of Wei - always the argument given. Fixed(U256), /// Gas price is calibrated according to a fixed amount of USD. + #[cfg(feature = "price-info")] Calibrated(GasPriceCalibrator), } impl GasPricer { /// Create a new Calibrated `GasPricer`. - pub fn new_calibrated(options: GasPriceCalibratorOptions, fetch: FetchClient, p: CpuPool) -> GasPricer { - GasPricer::Calibrated(GasPriceCalibrator { - options: options, - next_calibration: Instant::now(), - price_info: PriceInfoClient::new(fetch, p), - }) + #[cfg(feature = "price-info")] + pub fn new_calibrated(calibrator: GasPriceCalibrator) -> GasPricer { + GasPricer::Calibrated(calibrator) } /// Create a new Fixed `GasPricer`. @@ -91,6 +46,7 @@ impl GasPricer { pub fn recalibrate(&mut self, set_price: F) { match *self { GasPricer::Fixed(ref max) => set_price(max.clone()), + #[cfg(feature = "price-info")] GasPricer::Calibrated(ref mut cal) => cal.recalibrate(set_price), } } diff --git a/miner/src/lib.rs b/miner/src/lib.rs index 6b61df4de..e1ae0a9e0 100644 --- a/miner/src/lib.rs +++ b/miner/src/lib.rs @@ -28,6 +28,7 @@ extern crate heapsize; extern crate keccak_hash as hash; extern crate linked_hash_map; extern crate parking_lot; +#[cfg(feature = "price-info")] extern crate price_info; extern crate rlp; extern crate transaction_pool as txpool; @@ -47,6 +48,9 @@ extern crate ethkey; extern crate env_logger; pub mod external; +#[cfg(feature = "price-info")] +pub mod gas_price_calibrator; pub mod gas_pricer; pub mod pool; +#[cfg(feature = "work-notify")] pub mod work_notify; diff --git a/parity/params.rs b/parity/params.rs index 9417c4322..2227af3e9 100644 --- a/parity/params.rs +++ b/parity/params.rs @@ -24,7 +24,8 @@ use ethereum_types::{U256, Address}; use futures_cpupool::CpuPool; use hash_fetch::fetch::Client as FetchClient; use journaldb::Algorithm; -use miner::gas_pricer::{GasPricer, GasPriceCalibratorOptions}; +use miner::gas_pricer::GasPricer; +use miner::gas_price_calibrator::{GasPriceCalibratorOptions, GasPriceCalibrator}; use parity_version::version_data; use user_defaults::UserDefaults; @@ -248,12 +249,14 @@ impl GasPricerConfig { GasPricerConfig::Fixed(u) => GasPricer::Fixed(u), GasPricerConfig::Calibrated { usd_per_tx, recalibration_period, .. } => { GasPricer::new_calibrated( - GasPriceCalibratorOptions { - usd_per_tx: usd_per_tx, - recalibration_period: recalibration_period, - }, - fetch, - p, + GasPriceCalibrator::new( + GasPriceCalibratorOptions { + usd_per_tx: usd_per_tx, + recalibration_period: recalibration_period, + }, + fetch, + p, + ) ) } } diff --git a/parity/run.rs b/parity/run.rs index f4795f982..aa1ba9d71 100644 --- a/parity/run.rs +++ b/parity/run.rs @@ -31,6 +31,7 @@ use ethcore::verification::queue::VerifierSettings; use ethcore_logger::{Config as LogConfig, RotatingLogger}; use ethcore_service::ClientService; use sync::{self, SyncConfig}; +#[cfg(feature = "work-notify")] use miner::work_notify::WorkPoster; use futures_cpupool::CpuPool; use hash_fetch::{self, fetch}; @@ -529,11 +530,16 @@ fn execute_impl(cmd: RunCmd, logger: Arc, on_client_rq: miner.set_author(cmd.miner_extras.author, None).expect("Fails only if password is Some; password is None; qed"); miner.set_gas_range_target(cmd.miner_extras.gas_range_target); miner.set_extra_data(cmd.miner_extras.extra_data); - if !cmd.miner_extras.work_notify.is_empty() { - miner.add_work_listener(Box::new( - WorkPoster::new(&cmd.miner_extras.work_notify, fetch.clone(), event_loop.remote()) - )); + + #[cfg(feature = "work-notify")] + { + if !cmd.miner_extras.work_notify.is_empty() { + miner.add_work_listener(Box::new( + WorkPoster::new(&cmd.miner_extras.work_notify, fetch.clone(), event_loop.remote()) + )); + } } + let engine_signer = cmd.miner_extras.engine_signer; if engine_signer != Default::default() { // Check if engine signer exists