From 9acb36af871d8b0c84f3a48a4587e9d64ae68456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Tue, 8 Mar 2016 16:23:32 +0100 Subject: [PATCH] Fixing tests compilation. Removing ethminer dependency on client --- ethcore/src/log_entry.rs | 2 +- ethcore/src/tests/client.rs | 17 ++++---------- miner/src/lib.rs | 46 ++++++------------------------------- miner/src/miner.rs | 19 ++++++++++++--- parity/main.rs | 2 +- rpc/src/v1/impls/eth.rs | 7 ++++-- sync/src/chain.rs | 21 +++++++++-------- sync/src/tests/helpers.rs | 12 +++++++++- 8 files changed, 58 insertions(+), 68 deletions(-) diff --git a/ethcore/src/log_entry.rs b/ethcore/src/log_entry.rs index a75e6fcc1..63d09b4f0 100644 --- a/ethcore/src/log_entry.rs +++ b/ethcore/src/log_entry.rs @@ -111,7 +111,7 @@ mod tests { let bloom = H2048::from_str("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap(); let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap(); let log = LogEntry { - address: address, + address: address, topics: vec![], data: vec![] }; diff --git a/ethcore/src/tests/client.rs b/ethcore/src/tests/client.rs index 001d1729b..d31a780e6 100644 --- a/ethcore/src/tests/client.rs +++ b/ethcore/src/tests/client.rs @@ -132,16 +132,9 @@ fn can_mine() { let dummy_blocks = get_good_dummy_block_seq(2); let client_result = get_test_client_with_blocks(vec![dummy_blocks[0].clone()]); let client = client_result.reference(); - let b = client.sealing_block(); - let pow_hash = { - let u = b.lock().unwrap(); - match *u { - Some(ref b) => { - assert_eq!(*b.block().header().parent_hash(), BlockView::new(&dummy_blocks[0]).header_view().sha3()); - b.hash() - } - None => { panic!(); } - } - }; - assert!(client.submit_seal(pow_hash, vec![]).is_ok()); + + let b = client.prepare_sealing(Address::default(), vec![]).unwrap(); + + assert_eq!(*b.block().header().parent_hash(), BlockView::new(&dummy_blocks[0]).header_view().sha3()); + assert!(client.try_seal(b, vec![]).is_ok()); } diff --git a/miner/src/lib.rs b/miner/src/lib.rs index e8a50e9b5..ae6235393 100644 --- a/miner/src/lib.rs +++ b/miner/src/lib.rs @@ -29,58 +29,26 @@ extern crate rayon; mod miner; mod transaction_queue; -use util::{Bytes, H256, Address}; use std::ops::*; use std::sync::*; -use util::TimerToken; -use ethcore::block::*; -use ethcore::error::*; -use ethcore::client::{Client, BlockChainClient}; -use ethcore::transaction::*; -use miner::Miner; +pub use miner::Miner; pub struct EthMiner { miner: Miner, - /// Shared blockchain client. TODO: this should evetually become an IPC endpoint - chain: Arc, } impl EthMiner { /// Creates and register protocol with the network service - pub fn new(chain: Arc) -> Arc { + pub fn new() -> Arc { Arc::new(EthMiner { miner: Miner::new(), - chain: chain, }) } +} +impl Deref for EthMiner { + type Target = Miner; - pub fn sealing_block(&self) -> &Mutex> { - self.miner.sealing_block(self.chain.deref()) - } - - pub fn submit_seal(&self, pow_hash: H256, seal: Vec) -> Result<(), Error> { - self.miner.submit_seal(self.chain.deref(), pow_hash, seal) - } - - /// Set the author that we will seal blocks as. - pub fn set_author(&self, author: Address) { - self.miner.set_author(author); - } - - /// Set the extra_data that we will seal blocks with. - pub fn set_extra_data(&self, extra_data: Bytes) { - self.miner.set_extra_data(extra_data); - } - - pub fn import_transactions(&self, transactions: Vec) { - let chain = self.chain.deref(); - let fetch_latest_nonce = |a : &Address| chain.nonce(a); - - self.miner.import_transactions(transactions, fetch_latest_nonce); - } - - pub fn chain_new_blocks(&self, good: &[H256], bad: &[H256], retracted: &[H256]) { - let mut chain = self.chain.deref(); - self.miner.chain_new_blocks(chain, good, bad, retracted); + fn deref(&self) -> &Self::Target { + &self.miner } } diff --git a/miner/src/miner.rs b/miner/src/miner.rs index 1a48d5288..76130b261 100644 --- a/miner/src/miner.rs +++ b/miner/src/miner.rs @@ -23,7 +23,7 @@ use ethcore::client::{BlockChainClient, BlockStatus, BlockId, BlockChainInfo}; use ethcore::block::*; use ethcore::error::*; use ethcore::transaction::SignedTransaction; -use transaction_queue::TransactionQueue; +use transaction_queue::{TransactionQueue, TransactionQueueStatus}; pub struct Miner { /// Transactions Queue @@ -36,6 +36,11 @@ pub struct Miner { extra_data: RwLock, } +pub struct MinerStatus { + pub transaction_queue_pending: usize, + pub transaction_queue_future: usize, +} + impl Miner { pub fn new() -> Miner { Miner { @@ -47,6 +52,14 @@ impl Miner { } } + pub fn status(&self) -> MinerStatus { + let status = self.transaction_queue.lock().unwrap().status(); + MinerStatus { + transaction_queue_pending: status.pending, + transaction_queue_future: status.future, + } + } + pub fn import_transactions(&self, transactions: Vec, fetch_nonce: T) where T: Fn(&Address) -> U256 { let mut transaction_queue = self.transaction_queue.lock().unwrap(); @@ -55,7 +68,7 @@ impl Miner { /// Get the author that we will seal blocks as. pub fn author(&self) -> Address { - self.author.read().unwrap().clone() + *self.author.read().unwrap() } /// Set the author that we will seal blocks as. @@ -75,7 +88,7 @@ impl Miner { /// New chain head event. Restart mining operation. fn prepare_sealing(&self, chain: &BlockChainClient) { - let b = chain.prepare_sealing(self.author.read().unwrap().clone(), self.extra_data.read().unwrap().clone()); + let b = chain.prepare_sealing(*self.author.read().unwrap(), self.extra_data.read().unwrap().clone()); *self.sealing_block.lock().unwrap() = b; } diff --git a/parity/main.rs b/parity/main.rs index ef088ab5b..a0bc87a03 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -382,7 +382,7 @@ impl Configuration { let client = service.client(); // Miner - let miner = EthMiner::new(client.clone()); + let miner = EthMiner::new(); miner.set_author(self.author()); miner.set_extra_data(self.extra_data()); diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 11c6fe8d0..46d875c99 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -17,6 +17,7 @@ //! Eth rpc implementation. use std::collections::HashMap; use std::sync::{Arc, Weak, Mutex, RwLock}; +use std::ops::Deref; use ethsync::{EthSync, SyncState}; use ethminer::{EthMiner}; use jsonrpc_core::*; @@ -224,7 +225,8 @@ impl Eth for EthClient { match params { Params::None => { let miner = take_weak!(self.miner); - let u = miner.sealing_block().lock().unwrap(); + let client = take_weak!(self.client); + let u = miner.sealing_block(client.deref()).lock().unwrap(); match *u { Some(ref b) => { let pow_hash = b.hash(); @@ -243,8 +245,9 @@ impl Eth for EthClient { from_params::<(H64, H256, H256)>(params).and_then(|(nonce, pow_hash, mix_hash)| { // trace!("Decoded: nonce={}, pow_hash={}, mix_hash={}", nonce, pow_hash, mix_hash); let miner = take_weak!(self.miner); + let client = take_weak!(self.client); let seal = vec![encode(&mix_hash).to_vec(), encode(&nonce).to_vec()]; - let r = miner.submit_seal(pow_hash, seal); + let r = miner.submit_seal(client.deref(), pow_hash, seal); to_value(&r.is_ok()) }) } diff --git a/sync/src/chain.rs b/sync/src/chain.rs index d3277eccc..c607f53b1 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -31,7 +31,7 @@ use util::*; use std::mem::{replace}; -use ethcore::views::{HeaderView, BlockView}; +use ethcore::views::{HeaderView}; use ethcore::header::{BlockNumber, Header as BlockHeader}; use ethcore::client::{BlockChainClient, BlockStatus, BlockId, BlockChainInfo}; use range_collection::{RangeCollection, ToUsize, FromUsize}; @@ -933,7 +933,9 @@ impl ChainSync { let tx: SignedTransaction = try!(r.val_at(i)); transactions.push(tx); } - self.miner.import_transactions(transactions); + let chain = io.chain(); + let fetch_nonce = |a: &Address| chain.nonce(a); + self.miner.import_transactions(transactions, fetch_nonce); Ok(()) } @@ -1262,7 +1264,7 @@ impl ChainSync { pub fn chain_new_blocks(&mut self, io: &mut SyncIo, good: &[H256], bad: &[H256], retracted: &[H256]) { // notify miner - self.miner.chain_new_blocks(good, bad, retracted); + self.miner.chain_new_blocks(io.chain(), good, bad, retracted); // Propagate latests blocks self.propagate_latest_blocks(io); // TODO [todr] propagate transactions? @@ -1279,6 +1281,7 @@ mod tests { use super::{PeerInfo, PeerAsking}; use ethcore::header::*; use ethcore::client::*; + use ethminer::EthMiner; fn get_dummy_block(order: u32, parent_hash: H256) -> Bytes { let mut header = Header::new(); @@ -1388,7 +1391,7 @@ mod tests { } fn dummy_sync_with_peer(peer_latest_hash: H256) -> ChainSync { - let mut sync = ChainSync::new(SyncConfig::default()); + let mut sync = ChainSync::new(SyncConfig::default(), EthMiner::new()); sync.peers.insert(0, PeerInfo { protocol_version: 0, @@ -1610,14 +1613,14 @@ mod tests { // when sync.chain_new_blocks(&mut io, &[], &good_blocks, &[]); - assert_eq!(sync.transaction_queue.lock().unwrap().status().future, 0); - assert_eq!(sync.transaction_queue.lock().unwrap().status().pending, 1); + assert_eq!(sync.miner.status().transaction_queue_future, 0); + assert_eq!(sync.miner.status().transaction_queue_pending, 1); sync.chain_new_blocks(&mut io, &good_blocks, &retracted_blocks, &[]); // then - let status = sync.transaction_queue.lock().unwrap().status(); - assert_eq!(status.pending, 1); - assert_eq!(status.future, 0); + let status = sync.miner.status(); + assert_eq!(status.transaction_queue_pending, 1); + assert_eq!(status.transaction_queue_future, 0); } #[test] diff --git a/sync/src/tests/helpers.rs b/sync/src/tests/helpers.rs index d01dba0b2..37ee862b5 100644 --- a/sync/src/tests/helpers.rs +++ b/sync/src/tests/helpers.rs @@ -17,7 +17,9 @@ use util::*; use ethcore::client::{BlockChainClient, BlockStatus, TreeRoute, BlockChainInfo, TransactionId, BlockId, BlockQueueInfo}; use ethcore::header::{Header as BlockHeader, BlockNumber}; +use ethcore::block::*; use ethcore::error::*; +use ethminer::EthMiner; use io::SyncIo; use chain::ChainSync; use ::SyncConfig; @@ -308,6 +310,14 @@ impl BlockChainClient for TestBlockChainClient { best_block_number: self.blocks.read().unwrap().len() as BlockNumber - 1, } } + + fn prepare_sealing(&self, author: Address, extra_data: Bytes) -> Option { + unimplemented!() + } + + fn try_seal(&self, block: ClosedBlock, seal: Vec) -> Result { + unimplemented!() + } } pub struct TestIo<'p> { @@ -382,7 +392,7 @@ impl TestNet { for _ in 0..n { net.peers.push(TestPeer { chain: TestBlockChainClient::new(), - sync: ChainSync::new(SyncConfig::default()), + sync: ChainSync::new(SyncConfig::default(), EthMiner::new()), queue: VecDeque::new(), }); }