Fixing tests compilation. Removing ethminer dependency on client
This commit is contained in:
parent
84444c697c
commit
9acb36af87
@ -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) => {
|
||||
|
||||
let b = client.prepare_sealing(Address::default(), vec![]).unwrap();
|
||||
|
||||
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());
|
||||
assert!(client.try_seal(b, vec![]).is_ok());
|
||||
}
|
||||
|
@ -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<Client>,
|
||||
}
|
||||
|
||||
impl EthMiner {
|
||||
/// Creates and register protocol with the network service
|
||||
pub fn new(chain: Arc<Client>) -> Arc<EthMiner> {
|
||||
pub fn new() -> Arc<EthMiner> {
|
||||
Arc::new(EthMiner {
|
||||
miner: Miner::new(),
|
||||
chain: chain,
|
||||
})
|
||||
}
|
||||
}
|
||||
impl Deref for EthMiner {
|
||||
type Target = Miner;
|
||||
|
||||
pub fn sealing_block(&self) -> &Mutex<Option<ClosedBlock>> {
|
||||
self.miner.sealing_block(self.chain.deref())
|
||||
}
|
||||
|
||||
pub fn submit_seal(&self, pow_hash: H256, seal: Vec<Bytes>) -> 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<SignedTransaction>) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -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<Bytes>,
|
||||
}
|
||||
|
||||
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<T>(&self, transactions: Vec<SignedTransaction>, 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;
|
||||
}
|
||||
|
||||
|
@ -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());
|
||||
|
||||
|
@ -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())
|
||||
})
|
||||
}
|
||||
|
@ -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]
|
||||
|
@ -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<ClosedBlock> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn try_seal(&self, block: ClosedBlock, seal: Vec<Bytes>) -> Result<SealedBlock, ClosedBlock> {
|
||||
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(),
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user