From 9d664336b5e2bdd99855ca9961cc2da0ab4b9b31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Wed, 9 Mar 2016 13:28:37 +0100 Subject: [PATCH] Tratifying Miner --- miner/src/lib.rs | 4 +- miner/src/miner.rs | 90 ++++++++++++++++++++++++--------------- parity/main.rs | 2 +- rpc/src/v1/impls/eth.rs | 2 +- sync/src/chain.rs | 4 +- sync/src/tests/helpers.rs | 4 +- 6 files changed, 65 insertions(+), 41 deletions(-) diff --git a/miner/src/lib.rs b/miner/src/lib.rs index ae6235393..36b040b78 100644 --- a/miner/src/lib.rs +++ b/miner/src/lib.rs @@ -31,7 +31,8 @@ mod transaction_queue; use std::ops::*; use std::sync::*; -pub use miner::Miner; +pub use miner::{Miner, MinerService}; + pub struct EthMiner { miner: Miner, @@ -45,6 +46,7 @@ impl EthMiner { }) } } + impl Deref for EthMiner { type Target = Miner; diff --git a/miner/src/miner.rs b/miner/src/miner.rs index 501f8c35c..64d3c9083 100644 --- a/miner/src/miner.rs +++ b/miner/src/miner.rs @@ -17,16 +17,45 @@ use util::*; use std::sync::atomic::AtomicBool; use rayon::prelude::*; -use ethcore::views::{HeaderView, BlockView}; -use ethcore::header::{BlockNumber, Header as BlockHeader}; -use ethcore::client::{BlockChainClient, BlockStatus, BlockId, BlockChainInfo}; +use ethcore::views::{BlockView}; +use ethcore::client::{BlockChainClient, BlockId}; use ethcore::block::*; use ethcore::error::*; use ethcore::transaction::SignedTransaction; -use transaction_queue::{TransactionQueue, TransactionQueueStatus}; +use transaction_queue::{TransactionQueue}; + +pub trait MinerService { + fn status(&self) -> MinerStatus; + + fn import_transactions(&self, transactions: Vec, fetch_nonce: T) + where T: Fn(&Address) -> U256; + + /// called when blocks are imported to chain, updates transactions queue + fn chain_new_blocks(&self, chain: &BlockChainClient, good: &[H256], bad: &[H256], _retracted: &[H256]); + + /// Set the author that we will seal blocks as. + fn set_author(&self, author: Address); + + /// Set the extra_data that we will seal blocks with. + fn set_extra_data(&self, extra_data: Bytes); + + /// New chain head event. Restart mining operation. + fn prepare_sealing(&self, chain: &BlockChainClient); + + /// Grab the `ClosedBlock` that we want to be sealed. Comes as a mutex that you have to lock. + fn sealing_block(&self, chain: &BlockChainClient) -> &Mutex>; + + /// Submit `seal` as a valid solution for the header of `pow_hash`. + /// Will check the seal, but not actually insert the block into the chain. + fn submit_seal(&self, chain: &BlockChainClient, pow_hash: H256, seal: Vec) -> Result<(), Error>; +} + +pub struct MinerStatus { + pub transaction_queue_pending: usize, + pub transaction_queue_future: usize, +} pub struct Miner { - /// Transactions Queue transaction_queue: Mutex, // for sealing... @@ -36,12 +65,8 @@ pub struct Miner { extra_data: RwLock, } -pub struct MinerStatus { - pub transaction_queue_pending: usize, - pub transaction_queue_future: usize, -} - impl Miner { + /// Creates new instance of miner pub fn new() -> Miner { Miner { transaction_queue: Mutex::new(TransactionQueue::new()), @@ -52,7 +77,20 @@ impl Miner { } } - pub fn status(&self) -> MinerStatus { + /// Get the author that we will seal blocks as. + fn author(&self) -> Address { + *self.author.read().unwrap() + } + + /// Get the extra_data that we will seal blocks wuth. + fn extra_data(&self) -> Bytes { + self.extra_data.read().unwrap().clone() + } +} + +impl MinerService for Miner { + + fn status(&self) -> MinerStatus { let status = self.transaction_queue.lock().unwrap().status(); MinerStatus { transaction_queue_pending: status.pending, @@ -60,34 +98,22 @@ impl Miner { } } - pub fn import_transactions(&self, transactions: Vec, fetch_nonce: T) + fn import_transactions(&self, transactions: Vec, fetch_nonce: T) where T: Fn(&Address) -> U256 { let mut transaction_queue = self.transaction_queue.lock().unwrap(); transaction_queue.add_all(transactions, fetch_nonce); } - /// Get the author that we will seal blocks as. - pub fn author(&self) -> Address { - *self.author.read().unwrap() - } - - /// Set the author that we will seal blocks as. - pub fn set_author(&self, author: Address) { + fn set_author(&self, author: Address) { *self.author.write().unwrap() = author; } - /// Get the extra_data that we will seal blocks wuth. - pub fn extra_data(&self) -> Bytes { - self.extra_data.read().unwrap().clone() - } - /// Set the extra_data that we will seal blocks with. - pub fn set_extra_data(&self, extra_data: Bytes) { + fn set_extra_data(&self, extra_data: Bytes) { *self.extra_data.write().unwrap() = extra_data; } - /// New chain head event. Restart mining operation. - pub fn prepare_sealing(&self, chain: &BlockChainClient) { + fn prepare_sealing(&self, chain: &BlockChainClient) { let no_of_transactions = 128; let transactions = self.transaction_queue.lock().unwrap().top_transactions(no_of_transactions); @@ -99,8 +125,7 @@ impl Miner { *self.sealing_block.lock().unwrap() = b; } - /// Grab the `ClosedBlock` that we want to be sealed. Comes as a mutex that you have to lock. - pub fn sealing_block(&self, chain: &BlockChainClient) -> &Mutex> { + fn sealing_block(&self, chain: &BlockChainClient) -> &Mutex> { if self.sealing_block.lock().unwrap().is_none() { self.sealing_enabled.store(true, atomic::Ordering::Relaxed); // TODO: Above should be on a timer that resets after two blocks have arrived without being asked for. @@ -109,9 +134,7 @@ impl Miner { &self.sealing_block } - /// Submit `seal` as a valid solution for the header of `pow_hash`. - /// Will check the seal, but not actually insert the block into the chain. - pub fn submit_seal(&self, chain: &BlockChainClient, pow_hash: H256, seal: Vec) -> Result<(), Error> { + fn submit_seal(&self, chain: &BlockChainClient, pow_hash: H256, seal: Vec) -> Result<(), Error> { let mut maybe_b = self.sealing_block.lock().unwrap(); match *maybe_b { Some(ref b) if b.hash() == pow_hash => {} @@ -132,8 +155,7 @@ impl Miner { } } - /// called when block is imported to chain, updates transactions queue and propagates the blocks - pub fn chain_new_blocks(&self, chain: &BlockChainClient, good: &[H256], bad: &[H256], _retracted: &[H256]) { + fn chain_new_blocks(&self, chain: &BlockChainClient, good: &[H256], bad: &[H256], _retracted: &[H256]) { fn fetch_transactions(chain: &BlockChainClient, hash: &H256) -> Vec { let block = chain .block(BlockId::Hash(hash.clone())) diff --git a/parity/main.rs b/parity/main.rs index a0bc87a03..89668a456 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -50,7 +50,7 @@ use ethcore::client::*; use ethcore::service::{ClientService, NetSyncMessage}; use ethcore::ethereum; use ethsync::{EthSync, SyncConfig}; -use ethminer::{EthMiner}; +use ethminer::{EthMiner, MinerService}; use docopt::Docopt; use daemonize::Daemonize; use number_prefix::{binary_prefix, Standalone, Prefixed}; diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 46d875c99..d40761b09 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -19,7 +19,7 @@ use std::collections::HashMap; use std::sync::{Arc, Weak, Mutex, RwLock}; use std::ops::Deref; use ethsync::{EthSync, SyncState}; -use ethminer::{EthMiner}; +use ethminer::{EthMiner, MinerService}; use jsonrpc_core::*; use util::numbers::*; use util::sha3::*; diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 2669b71e2..cb584f51d 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -38,7 +38,7 @@ use range_collection::{RangeCollection, ToUsize, FromUsize}; use ethcore::error::*; use ethcore::transaction::SignedTransaction; use ethcore::block::Block; -use ethminer::EthMiner; +use ethminer::{EthMiner, MinerService}; use io::SyncIo; use time; use super::SyncConfig; @@ -1285,7 +1285,7 @@ mod tests { use super::{PeerInfo, PeerAsking}; use ethcore::header::*; use ethcore::client::*; - use ethminer::EthMiner; + use ethminer::{EthMiner, MinerService}; fn get_dummy_block(order: u32, parent_hash: H256) -> Bytes { let mut header = Header::new(); diff --git a/sync/src/tests/helpers.rs b/sync/src/tests/helpers.rs index 8c8f669a2..9a4dd2814 100644 --- a/sync/src/tests/helpers.rs +++ b/sync/src/tests/helpers.rs @@ -311,11 +311,11 @@ impl BlockChainClient for TestBlockChainClient { } } - fn prepare_sealing(&self, author: Address, extra_data: Bytes, transactions: Vec) -> Option { + fn prepare_sealing(&self, _author: Address, _extra_data: Bytes, _transactions: Vec) -> Option { unimplemented!() } - fn try_seal(&self, block: ClosedBlock, seal: Vec) -> Result { + fn try_seal(&self, _block: ClosedBlock, _seal: Vec) -> Result { unimplemented!() } }