rpc api in progress

This commit is contained in:
debris 2016-01-21 11:25:39 +01:00
parent 201c4726a2
commit 85de41642e
7 changed files with 83 additions and 5 deletions

View File

@ -36,7 +36,9 @@ fn setup_rpc_server(client: Arc<RwLock<Client>>) {
let mut server = HttpServer::new(1);
server.add_delegate(Web3Client::new().to_delegate());
server.add_delegate(EthClient::new(client).to_delegate());
server.add_delegate(EthClient::new(client.clone()).to_delegate());
server.add_delegate(EthFilterClient::new(client).to_delegate());
server.add_delegate(NetClient::new().to_delegate());
server.start_async("127.0.0.1:3030");
}

View File

@ -3,7 +3,7 @@ use rustc_serialize::hex::ToHex;
use util::hash::*;
use ethcore::client::*;
use rpc::jsonrpc_core::*;
use rpc::Eth;
use rpc::{Eth, EthFilter};
pub struct EthClient {
client: Arc<RwLock<Client>>,
@ -32,6 +32,13 @@ impl Eth for EthClient {
}
}
fn gas_price(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::U64(0)),
_ => Err(Error::invalid_params())
}
}
fn block_number(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::U64(self.client.read().unwrap().chain_info().best_block_number)),
@ -45,4 +52,37 @@ impl Eth for EthClient {
_ => Err(Error::invalid_params())
}
}
fn hashrate(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::U64(0)),
_ => Err(Error::invalid_params())
}
}
}
pub struct EthFilterClient {
client: Arc<RwLock<Client>>
}
impl EthFilterClient {
pub fn new(client: Arc<RwLock<Client>>) -> Self {
EthFilterClient {
client: client
}
}
}
impl EthFilter for EthFilterClient {
fn new_block_filter(&self, _params: Params) -> Result<Value, Error> {
Ok(Value::U64(0))
}
fn new_pending_transaction_filter(&self, _params: Params) -> Result<Value, Error> {
Ok(Value::U64(1))
}
fn filter_changes(&self, _: Params) -> Result<Value, Error> {
Ok(Value::String(self.client.read().unwrap().chain_info().best_block_hash.to_hex()))
}
}

View File

@ -4,4 +4,5 @@ pub mod eth;
pub mod net;
pub use self::web3::Web3Client;
pub use self::eth::EthClient;
pub use self::eth::{EthClient, EthFilterClient};
pub use self::net::NetClient;

View File

@ -0,0 +1,15 @@
//! Net rpc implementation.
use rpc::jsonrpc_core::*;
use rpc::Net;
pub struct NetClient;
impl NetClient {
pub fn new() -> Self { NetClient }
}
impl Net for NetClient {
fn peer_count(&self, _params: Params) -> Result<Value, Error> {
Ok(Value::U64(0))
}
}

View File

@ -10,7 +10,7 @@ macro_rules! rpcerr {
pub mod traits;
pub mod impls;
pub use self::traits::{Web3, Eth, Net};
pub use self::traits::{Web3, Eth, EthFilter, Net};
pub use self::impls::*;

View File

@ -43,3 +43,23 @@ pub trait Eth: Sized + Send + Sync + 'static {
}
}
// TODO: do filters api properly if we commit outselves to polling again...
pub trait EthFilter: Sized + Send + Sync + 'static {
/// Returns id of new block filter
fn new_block_filter(&self, _: Params) -> Result<Value, Error> { rpcerr!() }
/// Returns id of new block filter
fn new_pending_transaction_filter(&self, _: Params) -> Result<Value, Error> { rpcerr!() }
/// Returns filter changes since last poll
fn filter_changes(&self, _: Params) -> Result<Value, Error> { rpcerr!() }
/// Should be used to convert object to io delegate.
fn to_delegate(self) -> IoDelegate<Self> {
let mut delegate = IoDelegate::new(Arc::new(self));
delegate.add_method("eth_newBlockFilter", EthFilter::new_block_filter);
delegate.add_method("eth_newPendingTransactionFilter", EthFilter::new_pending_transaction_filter);
delegate.add_method("eth_getFilterChanges", EthFilter::new_block_filter);
delegate
}
}

View File

@ -4,5 +4,5 @@ pub mod eth;
pub mod net;
pub use self::web3::Web3;
pub use self::eth::Eth;
pub use self::eth::{Eth, EthFilter};
pub use self::net::Net;