Merge branch 'master' into ethminer_crate

Conflicts:
	ethcore/src/client.rs
	parity/main.rs
	rpc/src/v1/impls/eth.rs
	sync/src/chain.rs
This commit is contained in:
Tomasz Drwięga 2016-03-10 16:18:33 +01:00
commit 92022ac14d
9 changed files with 59 additions and 44 deletions

View File

@ -190,6 +190,7 @@ pub trait BlockChainClient : Sync + Send {
/// Attempts to seal given block. Returns `SealedBlock` on success and the same block in case of error.
fn try_seal(&self, block: ClosedBlock, seal: Vec<Bytes>) -> Result<SealedBlock, ClosedBlock>;
}
#[derive(Default, Clone, Debug, Eq, PartialEq)]

View File

@ -25,7 +25,7 @@ use ethcore::transaction::SignedTransaction;
use transaction_queue::{TransactionQueue};
/// Miner client API
pub trait MinerService {
pub trait MinerService : Send + Sync {
/// Returns miner's status.
fn status(&self) -> MinerStatus;

View File

@ -50,7 +50,7 @@ use ethcore::spec::*;
use ethcore::client::*;
use ethcore::service::{ClientService, NetSyncMessage};
use ethcore::ethereum;
use ethsync::{EthSync, SyncConfig};
use ethsync::{EthSync, SyncConfig, SyncStatusProvider};
use ethminer::{Miner, MinerService};
use docopt::Docopt;
use daemonize::Daemonize;

View File

@ -84,7 +84,7 @@ impl<F, T> PollManager<F, T> where T: Timer {
}
/// Returns number of block when last poll happend.
pub fn get_poll_info(&mut self, id: &PollId) -> Option<&PollInfo<F>> {
pub fn poll_info(&mut self, id: &PollId) -> Option<&PollInfo<F>> {
self.polls.prune();
self.polls.get(id)
}
@ -124,21 +124,21 @@ mod tests {
*time.borrow_mut() = 10;
indexer.update_poll(&0, 21);
assert_eq!(indexer.get_poll_info(&0).unwrap().filter, false);
assert_eq!(indexer.get_poll_info(&0).unwrap().block_number, 21);
assert_eq!(indexer.poll_info(&0).unwrap().filter, false);
assert_eq!(indexer.poll_info(&0).unwrap().block_number, 21);
*time.borrow_mut() = 30;
indexer.update_poll(&1, 23);
assert_eq!(indexer.get_poll_info(&1).unwrap().filter, true);
assert_eq!(indexer.get_poll_info(&1).unwrap().block_number, 23);
assert_eq!(indexer.poll_info(&1).unwrap().filter, true);
assert_eq!(indexer.poll_info(&1).unwrap().block_number, 23);
*time.borrow_mut() = 75;
indexer.update_poll(&0, 30);
assert!(indexer.get_poll_info(&0).is_none());
assert_eq!(indexer.get_poll_info(&1).unwrap().filter, true);
assert_eq!(indexer.get_poll_info(&1).unwrap().block_number, 23);
assert!(indexer.poll_info(&0).is_none());
assert_eq!(indexer.poll_info(&1).unwrap().filter, true);
assert_eq!(indexer.poll_info(&1).unwrap().block_number, 23);
indexer.remove_poll(&1);
assert!(indexer.get_poll_info(&1).is_none());
assert!(indexer.poll_info(&1).is_none());
}
}

View File

@ -18,8 +18,8 @@
use std::collections::HashMap;
use std::sync::{Arc, Weak, Mutex, RwLock};
use std::ops::Deref;
use ethsync::{EthSync, SyncState};
use ethminer::{Miner, MinerService};
use ethsync::{SyncStatusProvider, SyncState};
use ethminer::{MinerService};
use jsonrpc_core::*;
use util::numbers::*;
use util::sha3::*;
@ -27,7 +27,6 @@ use util::rlp::encode;
use ethcore::client::*;
use ethcore::block::{IsBlock};
use ethcore::views::*;
//#[macro_use] extern crate log;
use ethcore::ethereum::Ethash;
use ethcore::ethereum::denominations::shannon;
use v1::traits::{Eth, EthFilter};
@ -35,16 +34,20 @@ use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncIn
use v1::helpers::{PollFilter, PollManager};
/// Eth rpc implementation.
pub struct EthClient {
client: Weak<Client>,
sync: Weak<EthSync>,
miner: Weak<Miner>,
pub struct EthClient<C, S, M>
where C: BlockChainClient,
S: SyncStatusProvider,
M: MinerService {
client: Weak<C>,
sync: Weak<S>,
miner: Weak<M>,
hashrates: RwLock<HashMap<H256, u64>>,
}
impl EthClient {
impl<C, S, M> EthClient<C, S, M> where C: BlockChainClient, S: SyncStatusProvider, M: MinerService {
/// Creates new EthClient.
pub fn new(client: &Arc<Client>, sync: &Arc<EthSync>, miner: &Arc<Miner>) -> Self {
pub fn new(client: &Arc<C>, sync: &Arc<S>, miner: &Arc<M>) -> Self {
EthClient {
client: Arc::downgrade(client),
sync: Arc::downgrade(sync),
@ -99,7 +102,10 @@ impl EthClient {
}
}
impl Eth for EthClient {
impl<C, S, M> Eth for EthClient<C, S, M>
where C: BlockChainClient + 'static,
S: SyncStatusProvider + 'static,
M: MinerService + 'static {
fn protocol_version(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => to_value(&U256::from(take_weak!(self.sync).status().protocol_version)),
@ -262,14 +268,14 @@ impl Eth for EthClient {
}
/// Eth filter rpc implementation.
pub struct EthFilterClient {
client: Weak<Client>,
pub struct EthFilterClient<C> where C: BlockChainClient {
client: Weak<C>,
polls: Mutex<PollManager<PollFilter>>,
}
impl EthFilterClient {
impl<C> EthFilterClient<C> where C: BlockChainClient {
/// Creates new Eth filter client.
pub fn new(client: &Arc<Client>) -> Self {
pub fn new(client: &Arc<C>) -> Self {
EthFilterClient {
client: Arc::downgrade(client),
polls: Mutex::new(PollManager::new())
@ -277,7 +283,7 @@ impl EthFilterClient {
}
}
impl EthFilter for EthFilterClient {
impl<C> EthFilter for EthFilterClient<C> where C: BlockChainClient + 'static {
fn new_filter(&self, params: Params) -> Result<Value, Error> {
from_params::<(Filter,)>(params)
.and_then(|(filter,)| {
@ -313,7 +319,7 @@ impl EthFilter for EthFilterClient {
let client = take_weak!(self.client);
from_params::<(Index,)>(params)
.and_then(|(index,)| {
let info = self.polls.lock().unwrap().get_poll_info(&index.value()).cloned();
let info = self.polls.lock().unwrap().poll_info(&index.value()).cloned();
match info {
None => Ok(Value::Array(vec![] as Vec<Value>)),
Some(info) => match info.filter {

View File

@ -17,24 +17,24 @@
//! Net rpc implementation.
use std::sync::{Arc, Weak};
use jsonrpc_core::*;
use ethsync::EthSync;
use ethsync::SyncStatusProvider;
use v1::traits::Net;
/// Net rpc implementation.
pub struct NetClient {
sync: Weak<EthSync>
pub struct NetClient<S> where S: SyncStatusProvider {
sync: Weak<S>
}
impl NetClient {
impl<S> NetClient<S> where S: SyncStatusProvider {
/// Creates new NetClient.
pub fn new(sync: &Arc<EthSync>) -> Self {
pub fn new(sync: &Arc<S>) -> Self {
NetClient {
sync: Arc::downgrade(sync)
}
}
}
impl Net for NetClient {
impl<S> Net for NetClient<S> where S: SyncStatusProvider + 'static {
fn version(&self, _: Params) -> Result<Value, Error> {
Ok(Value::U64(take_weak!(self.sync).status().protocol_version as u64))
}

View File

@ -1275,7 +1275,6 @@ impl ChainSync {
pub fn chain_new_head(&mut self, io: &mut SyncIo) {
self.miner.prepare_sealing(io.chain());
}
}
#[cfg(test)]

View File

@ -93,6 +93,12 @@ impl Default for SyncConfig {
}
}
/// Current sync status
pub trait SyncStatusProvider: Send + Sync {
/// Get sync status
fn status(&self) -> SyncStatus;
}
/// Ethereum network protocol handler
pub struct EthSync {
/// Shared blockchain client. TODO: this should evetually become an IPC endpoint
@ -114,11 +120,6 @@ impl EthSync {
sync
}
/// Get sync status
pub fn status(&self) -> SyncStatus {
self.sync.read().unwrap().status()
}
/// Stop sync
pub fn stop(&mut self, io: &mut NetworkContext<SyncMessage>) {
self.sync.write().unwrap().abort(&mut NetSyncIo::new(io, self.chain.deref()));
@ -128,6 +129,14 @@ impl EthSync {
pub fn restart(&mut self, io: &mut NetworkContext<SyncMessage>) {
self.sync.write().unwrap().restart(&mut NetSyncIo::new(io, self.chain.deref()));
}
}
impl SyncStatusProvider for EthSync {
/// Get sync status
fn status(&self) -> SyncStatus {
self.sync.read().unwrap().status()
}
}
impl NetworkProtocolHandler<SyncMessage> for EthSync {

View File

@ -15,18 +15,18 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
use util::*;
use ::SyncConfig;
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::Miner;
use io::SyncIo;
use chain::ChainSync;
use ::SyncConfig;
use ethcore::receipt::Receipt;
use ethcore::transaction::{LocalizedTransaction, SignedTransaction, Transaction, Action};
use ethcore::filter::Filter;
use ethcore::log_entry::LocalizedLogEntry;
use ethcore::block::{ClosedBlock, SealedBlock};
use ethminer::Miner;
use io::SyncIo;
use chain::ChainSync;
pub struct TestBlockChainClient {
pub blocks: RwLock<HashMap<H256, Bytes>>,