From 8e252d5f1ba81585292477be869f3e1843c16fa1 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Tue, 31 May 2016 19:52:53 +0200 Subject: [PATCH] refactored to merge client & client --- ethcore/Cargo.toml | 2 +- ethcore/src/client/client.rs | 41 +++++++++---------- ethcore/src/client/mod.rs | 17 ++++---- ethcore/src/client/test_client.rs | 11 +++++ ethcore/src/lib.rs | 2 + {miner/src => ethcore/src/miner}/external.rs | 0 {miner/src => ethcore/src/miner}/miner.rs | 20 ++++----- miner/src/lib.rs => ethcore/src/miner/mod.rs | 32 +++++---------- .../src/miner}/transaction_queue.rs | 4 +- ethcore/src/service.rs | 5 ++- 10 files changed, 68 insertions(+), 66 deletions(-) rename {miner/src => ethcore/src/miner}/external.rs (100%) rename {miner/src => ethcore/src/miner}/miner.rs (98%) rename miner/src/lib.rs => ethcore/src/miner/mod.rs (89%) rename {miner/src => ethcore/src/miner}/transaction_queue.rs (99%) diff --git a/ethcore/Cargo.toml b/ethcore/Cargo.toml index f63eb957c..1de24ee32 100644 --- a/ethcore/Cargo.toml +++ b/ethcore/Cargo.toml @@ -29,7 +29,7 @@ ethcore-devtools = { path = "../devtools" } ethjson = { path = "../json" } bloomchain = "0.1" "ethcore-ipc" = { path = "../ipc/rpc" } -ethminer = { path = "../miner" } +rayon = "0.3.1" [features] jit = ["evmjit"] diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index 7a616fdbc..d23bffa31 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -48,6 +48,7 @@ use trace; pub use types::blockchain_info::BlockChainInfo; pub use types::block_status::BlockStatus; use evm::Factory as EvmFactory; +use miner::{Miner, MinerService, TransactionImportResult, AccountDetails}; impl fmt::Display for BlockChainInfo { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -90,7 +91,7 @@ pub struct Client where V: Verifier { panic_handler: Arc, verifier: PhantomData, vm_factory: Arc, - miner: Arc, + miner: Arc, } const HISTORY: u64 = 1200; @@ -103,8 +104,8 @@ const CLIENT_DB_VER_STR: &'static str = "5.3"; impl Client { /// Create a new client with given spec and DB path. - pub fn new(config: ClientConfig, spec: Spec, path: &Path, message_channel: IoChannel ) -> Result, ClientError> { - Client::::new_with_verifier(config, spec, path, message_channel) + pub fn new(config: ClientConfig, spec: Spec, path: &Path, miner: Arc, message_channel: IoChannel ) -> Result, ClientError> { + Client::::new_with_verifier(config, spec, path, miner, message_channel) } } @@ -131,6 +132,7 @@ impl Client where V: Verifier { config: ClientConfig, spec: Spec, path: &Path, + miner: Arc, message_channel: IoChannel) -> Result>, ClientError> { @@ -162,6 +164,7 @@ impl Client where V: Verifier { panic_handler: panic_handler, verifier: PhantomData, vm_factory: Arc::new(EvmFactory::new(config.vm_type)), + miner: miner, }; Ok(Arc::new(client)) @@ -335,13 +338,13 @@ impl Client where V: Verifier { { if !imported_blocks.is_empty() && self.block_queue.queue_info().is_empty() { let (enacted, retracted) = self.calculate_enacted_retracted(import_results); - self.miner.chain_new_blocks(imported_blocks, invalid_blocks, enacted, retracted); + self.miner.chain_new_blocks(self, &imported_blocks, &invalid_blocks, &enacted, &retracted); } } { if self.chain_info().best_block_hash != original_best { - self.miner.update_sealing(&self); + self.miner.update_sealing(self); } } @@ -705,10 +708,21 @@ impl BlockChainClient for Client where V: Verifier { fn last_hashes(&self) -> LastHashes { self.build_last_hashes(self.chain.best_block_hash()) } + + fn import_transactions(&self, transactions: Vec) -> Vec> { + let fetch_account = |a: &Address| AccountDetails { + nonce: self.latest_nonce(a), + balance: self.latest_balance(a), + }; + self.miner.import_transactions(transactions, fetch_account) + } + + fn all_transactions(&self) -> Vec { + self.miner.all_transactions() + } } impl ExtendedBlockChainClient for Client where V: Verifier { - // TODO [todr] Should be moved to miner crate eventually. fn prepare_sealing(&self, author: Address, gas_floor_target: U256, extra_data: Bytes, transactions: Vec) -> (Option, HashSet) { let engine = self.engine.deref().deref(); @@ -774,26 +788,11 @@ impl ExtendedBlockChainClient for Client where V: Verifier { (Some(b), invalid_transactions) } - // TODO [todr] Should be moved to miner crate eventually. fn try_seal(&self, block: LockedBlock, seal: Vec) -> Result { block.try_seal(self.engine.deref().deref(), seal) } } -impl MiningClient for Client { - fn import_transactions(&self, transactions: Vec) -> Vec> { - let fetch_account = |a: &Address| AccountDetails { - nonce: self.latest_nonce(a), - balance: self.latest_balance(a), - }; - self.miner.import_transactions(transactions, fetch_account) - } - - fn all_transactions(&self) -> Vec { - self.miner.all_transactions() - } -} - impl MayPanic for Client { fn on_panic(&self, closure: F) where F: OnPanicListener { self.panic_handler.on_panic(closure); diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index dfef54a40..67c753d46 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -46,6 +46,8 @@ use error::{ImportResult, ExecutionError}; use receipt::LocalizedReceipt; use trace::LocalizedTrace; use evm::Factory as EvmFactory; +use miner::{TransactionImportResult, AccountDetails}; +use error::Error as EthError; /// Blockchain database client. Owns and manages a blockchain and a block queue. pub trait BlockChainClient : Sync + Send { @@ -174,6 +176,12 @@ pub trait BlockChainClient : Sync + Send { /// Get last hashes starting from best block. fn last_hashes(&self) -> LastHashes; + + /// import transactions from network/other 3rd party + fn import_transactions(&self, transactions: Vec) -> Vec>; + + /// list all transactions + fn all_transactions(&self) -> Vec; } /// Extended client interface used for mining @@ -185,12 +193,3 @@ pub trait ExtendedBlockChainClient : BlockChainClient { fn prepare_sealing(&self, author: Address, gas_floor_target: U256, extra_data: Bytes, transactions: Vec) -> (Option, HashSet); } - -/// Extended client interface that supports mining -pub trait MiningClient : BlockChainClient { - /// import transactions from network/other 3rd party - fn import_transactions(&self, transactions: Vec) -> Vec>; - - /// list all transactions - fn all_transactions(&self) -> Vec; -} diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index 5c093b720..ad398bbcb 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -35,6 +35,9 @@ use executive::Executed; use error::{ExecutionError}; use trace::LocalizedTrace; +use miner::{TransactionImportResult, AccountDetails}; +use error::Error as EthError; + /// Test client. pub struct TestBlockChainClient { /// Blocks. @@ -479,4 +482,12 @@ impl BlockChainClient for TestBlockChainClient { fn block_traces(&self, _trace: BlockID) -> Option> { unimplemented!(); } + + fn import_transactions(&self, transactions: Vec) -> Vec> { + unimplemented!(); + } + + fn all_transactions(&self) -> Vec { + unimplemented!(); + } } diff --git a/ethcore/src/lib.rs b/ethcore/src/lib.rs index 1d4ddadbc..5cf9d50a1 100644 --- a/ethcore/src/lib.rs +++ b/ethcore/src/lib.rs @@ -90,6 +90,7 @@ extern crate crossbeam; extern crate ethjson; extern crate bloomchain; #[macro_use] extern crate ethcore_ipc as ipc; +extern crate rayon; #[cfg(test)] extern crate ethcore_devtools as devtools; #[cfg(feature = "jit" )] extern crate evmjit; @@ -109,6 +110,7 @@ pub mod views; pub mod pod_state; pub mod engine; pub mod migrations; +pub mod miner; mod blooms; mod db; diff --git a/miner/src/external.rs b/ethcore/src/miner/external.rs similarity index 100% rename from miner/src/external.rs rename to ethcore/src/miner/external.rs diff --git a/miner/src/miner.rs b/ethcore/src/miner/miner.rs similarity index 98% rename from miner/src/miner.rs rename to ethcore/src/miner/miner.rs index 7438e7e9b..c4bec7659 100644 --- a/miner/src/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -19,16 +19,16 @@ use std::sync::atomic::AtomicBool; use util::*; use util::keys::store::{AccountService, AccountProvider}; -use ethcore::views::{BlockView, HeaderView}; -use ethcore::client::{ExtendedBlockChainClient, BlockID}; -use ethcore::block::{ClosedBlock, IsBlock}; -use ethcore::error::*; -use ethcore::client::{Executive, Executed, EnvInfo, TransactOptions}; -use ethcore::transaction::SignedTransaction; -use ethcore::receipt::{Receipt}; -use ethcore::spec::Spec; -use ethcore::engine::Engine; -use super::{MinerService, MinerStatus, TransactionQueue, AccountDetails, TransactionImportResult, TransactionOrigin}; +use views::{BlockView, HeaderView}; +use client::{ExtendedBlockChainClient, BlockID}; +use block::{ClosedBlock, IsBlock}; +use error::*; +use client::{Executive, Executed, EnvInfo, TransactOptions}; +use transaction::SignedTransaction; +use receipt::{Receipt}; +use spec::Spec; +use engine::Engine; +use miner::{MinerService, MinerStatus, TransactionQueue, AccountDetails, TransactionImportResult, TransactionOrigin}; /// Keeps track of transactions using priority queue and holds currently mined block. pub struct Miner { diff --git a/miner/src/lib.rs b/ethcore/src/miner/mod.rs similarity index 89% rename from miner/src/lib.rs rename to ethcore/src/miner/mod.rs index db3315376..95453027c 100644 --- a/miner/src/lib.rs +++ b/ethcore/src/miner/mod.rs @@ -26,12 +26,11 @@ //! ```rust //! extern crate ethcore_util as util; //! extern crate ethcore; -//! extern crate ethminer; //! use std::env; //! use util::network::{NetworkService, NetworkConfiguration}; -//! use ethcore::client::{Client, ClientConfig}; -//! use ethcore::ethereum; -//! use ethminer::{Miner, MinerService}; +//! use client::{Client, ClientConfig}; +//! use ethereum; +//! use ethcore::miner::{Miner, MinerService}; //! //! fn main() { //! let miner: Miner = Miner::default(); @@ -43,30 +42,21 @@ //! } //! ``` - -#[macro_use] -extern crate log; -#[macro_use] -extern crate ethcore_util as util; -extern crate ethcore; -extern crate env_logger; -extern crate rayon; - mod miner; mod external; mod transaction_queue; -pub use transaction_queue::{TransactionQueue, AccountDetails, TransactionImportResult, TransactionOrigin}; -pub use miner::{Miner}; -pub use external::{ExternalMiner, ExternalMinerService}; +pub use self::transaction_queue::{TransactionQueue, AccountDetails, TransactionImportResult, TransactionOrigin}; +pub use self::miner::{Miner}; +pub use self::external::{ExternalMiner, ExternalMinerService}; use std::collections::BTreeMap; use util::{H256, U256, Address, Bytes}; -use ethcore::client::{ExtendedBlockChainClient, Executed}; -use ethcore::block::ClosedBlock; -use ethcore::receipt::Receipt; -use ethcore::error::{Error, ExecutionError}; -use ethcore::transaction::SignedTransaction; +use client::{ExtendedBlockChainClient, Executed}; +use block::ClosedBlock; +use receipt::Receipt; +use error::{Error, ExecutionError}; +use transaction::SignedTransaction; /// Miner client API pub trait MinerService : Send + Sync { diff --git a/miner/src/transaction_queue.rs b/ethcore/src/miner/transaction_queue.rs similarity index 99% rename from miner/src/transaction_queue.rs rename to ethcore/src/miner/transaction_queue.rs index fc62c411e..1b88a7a11 100644 --- a/miner/src/transaction_queue.rs +++ b/ethcore/src/miner/transaction_queue.rs @@ -89,8 +89,8 @@ use std::collections::{HashMap, BTreeSet}; use util::numbers::{Uint, U256}; use util::hash::{Address, H256}; use util::table::*; -use ethcore::transaction::*; -use ethcore::error::{Error, TransactionError}; +use transaction::*; +use error::{Error, TransactionError}; /// Transaction origin #[derive(Clone, Copy, Debug, PartialEq, Eq)] diff --git a/ethcore/src/service.rs b/ethcore/src/service.rs index 38bd873b8..e82f6725b 100644 --- a/ethcore/src/service.rs +++ b/ethcore/src/service.rs @@ -21,6 +21,7 @@ use util::panics::*; use spec::Spec; use error::*; use client::{Client, ClientConfig}; +use miner::Miner; /// Message type for external and internal events #[derive(Clone)] @@ -54,14 +55,14 @@ pub struct ClientService { impl ClientService { /// Start the service in a separate thread. - pub fn start(config: ClientConfig, spec: Spec, net_config: NetworkConfiguration, db_path: &Path) -> Result { + pub fn start(config: ClientConfig, spec: Spec, net_config: NetworkConfiguration, db_path: &Path, miner: Arc) -> Result { let panic_handler = PanicHandler::new_in_arc(); let mut net_service = try!(NetworkService::start(net_config)); panic_handler.forward_from(&net_service); info!("Starting {}", net_service.host_info()); info!("Configured for {} using {:?} engine", spec.name, spec.engine.name()); - let client = try!(Client::new(config, spec, db_path, net_service.io().channel())); + let client = try!(Client::new(config, spec, db_path, miner, net_service.io().channel())); panic_handler.forward_from(client.deref()); let client_io = Arc::new(ClientIoHandler { client: client.clone()