Merge pull request #641 from ethcore/jsonrpc_interfaces

jsonrpc uses client and sync interfaces
This commit is contained in:
Gav Wood
2016-03-10 15:44:05 +01:00
6 changed files with 81 additions and 58 deletions

View File

@@ -17,7 +17,7 @@
//! Eth rpc implementation.
use std::collections::HashMap;
use std::sync::{Arc, Weak, Mutex, RwLock};
use ethsync::{EthSync, SyncState};
use ethsync::{SyncStatusProvider, SyncState};
use jsonrpc_core::*;
use util::numbers::*;
use util::sha3::*;
@@ -25,7 +25,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};
@@ -33,15 +32,15 @@ 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>,
pub struct EthClient<C, S> where C: BlockChainClient, S: SyncStatusProvider {
client: Weak<C>,
sync: Weak<S>,
hashrates: RwLock<HashMap<H256, u64>>,
}
impl EthClient {
impl<C, S> EthClient<C, S> where C: BlockChainClient, S: SyncStatusProvider {
/// Creates new EthClient.
pub fn new(client: &Arc<Client>, sync: &Arc<EthSync>) -> Self {
pub fn new(client: &Arc<C>, sync: &Arc<S>) -> Self {
EthClient {
client: Arc::downgrade(client),
sync: Arc::downgrade(sync),
@@ -95,7 +94,7 @@ impl EthClient {
}
}
impl Eth for EthClient {
impl<C, S> Eth for EthClient<C, S> where C: BlockChainClient + 'static, S: SyncStatusProvider + '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)),
@@ -256,14 +255,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())
@@ -271,7 +270,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,)| {

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))
}