rpc and bin moved to its own crates

This commit is contained in:
debris
2016-01-26 13:14:22 +01:00
parent d27c7f3902
commit 3ac40b68f8
15 changed files with 61 additions and 27 deletions

19
rpc/Cargo.toml Normal file
View File

@@ -0,0 +1,19 @@
[package]
description = "Ethcore jsonrpc"
name = "ethcore-rpc"
version = "0.1.0"
license = "GPL-3.0"
authors = ["Marek Kotewicz <marek.kotewicz@gmail.com"]
[lib]
[dependencies]
rustc-serialize = "0.3"
serde = "0.6.7"
serde_macros = "0.6.10"
serde_json = "0.6.0"
jsonrpc-core = "1.0"
jsonrpc-http-server = "1.0"
ethcore-util = { path = "../util" }
ethcore = { path = ".." }

96
rpc/src/impls/eth.rs Normal file
View File

@@ -0,0 +1,96 @@
use std::sync::Arc;
use rustc_serialize::hex::ToHex;
use util::hash::*;
use ethcore::client::*;
use jsonrpc_core::*;
use traits::{Eth, EthFilter};
pub struct EthClient {
client: Arc<Client>,
}
impl EthClient {
pub fn new(client: Arc<Client>) -> Self {
EthClient {
client: client
}
}
}
impl Eth for EthClient {
fn protocol_version(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::U64(63)),
_ => Err(Error::invalid_params())
}
}
fn author(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::String(Address::new().to_hex())),
_ => Err(Error::invalid_params())
}
}
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.chain_info().best_block_number)),
_ => Err(Error::invalid_params())
}
}
fn is_mining(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::Bool(false)),
_ => Err(Error::invalid_params())
}
}
fn hashrate(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::U64(0)),
_ => Err(Error::invalid_params())
}
}
fn block_transaction_count(&self, _: Params) -> Result<Value, Error> {
Ok(Value::U64(0))
}
fn block(&self, _: Params) -> Result<Value, Error> {
Ok(Value::Null)
}
}
pub struct EthFilterClient {
client: Arc<Client>
}
impl EthFilterClient {
pub fn new(client: Arc<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::Array(vec![Value::String(self.client.chain_info().best_block_hash.to_hex())]))
}
}

8
rpc/src/impls/mod.rs Normal file
View File

@@ -0,0 +1,8 @@
//! Ethereum rpc interface implementation.
mod web3;
mod eth;
mod net;
pub use self::web3::Web3Client;
pub use self::eth::{EthClient, EthFilterClient};
pub use self::net::NetClient;

19
rpc/src/impls/net.rs Normal file
View File

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

17
rpc/src/impls/web3.rs Normal file
View File

@@ -0,0 +1,17 @@
use jsonrpc_core::*;
use traits::Web3;
pub struct Web3Client;
impl Web3Client {
pub fn new() -> Self { Web3Client }
}
impl Web3 for Web3Client {
fn client_version(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::String("parity/0.1.0/-/rust1.7-nightly".to_string())),
_ => Err(Error::invalid_params())
}
}
}

45
rpc/src/lib.rs Normal file
View File

@@ -0,0 +1,45 @@
#![feature(custom_derive, custom_attribute, plugin)]
#![plugin(serde_macros)]
extern crate rustc_serialize;
extern crate serde;
extern crate jsonrpc_core;
extern crate jsonrpc_http_server;
extern crate ethcore_util as util;
extern crate ethcore;
use self::jsonrpc_core::{IoHandler, IoDelegate};
macro_rules! rpcerr {
() => (Err(Error::internal_error()))
}
pub mod traits;
mod impls;
mod types;
pub use self::traits::{Web3, Eth, EthFilter, Net};
pub use self::impls::*;
pub struct HttpServer {
handler: IoHandler,
threads: usize
}
impl HttpServer {
pub fn new(threads: usize) -> HttpServer {
HttpServer {
handler: IoHandler::new(),
threads: threads
}
}
pub fn add_delegate<D>(&mut self, delegate: IoDelegate<D>) where D: Send + Sync + 'static {
self.handler.add_delegate(delegate);
}
pub fn start_async(self, addr: &str) {
let server = jsonrpc_http_server::Server::new(self.handler, self.threads);
server.start_async(addr)
}
}

65
rpc/src/traits/eth.rs Normal file
View File

@@ -0,0 +1,65 @@
//! Eth rpc interface.
use std::sync::Arc;
use jsonrpc_core::*;
/// Eth rpc interface.
pub trait Eth: Sized + Send + Sync + 'static {
/// Returns protocol version.
fn protocol_version(&self, _: Params) -> Result<Value, Error> { rpcerr!() }
/// Returns block author.
fn author(&self, _: Params) -> Result<Value, Error> { rpcerr!() }
/// Returns current gas_price.
fn gas_price(&self, _: Params) -> Result<Value, Error> { rpcerr!() }
/// Returns highest block number.
fn block_number(&self, _: Params) -> Result<Value, Error> { rpcerr!() }
/// Returns block with given index / hash.
fn block(&self, _: Params) -> Result<Value, Error> { rpcerr!() }
/// Returns true if client is actively mining new blocks.
fn is_mining(&self, _: Params) -> Result<Value, Error> { rpcerr!() }
/// Returns the number of hashes per second that the node is mining with.
fn hashrate(&self, _: Params) -> Result<Value, Error> { rpcerr!() }
/// Returns the number of transactions in a block.
fn block_transaction_count(&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_protocolVersion", Eth::protocol_version);
delegate.add_method("eth_coinbase", Eth::author);
delegate.add_method("eth_gasPrice", Eth::gas_price);
delegate.add_method("eth_blockNumber", Eth::block_number);
delegate.add_method("eth_getBlockByNumber", Eth::block);
delegate.add_method("eth_mining", Eth::is_mining);
delegate.add_method("eth_hashrate", Eth::hashrate);
delegate.add_method("eth_getBlockTransactionCountByNumber", Eth::block_transaction_count);
delegate
}
}
// 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::filter_changes);
delegate
}
}

8
rpc/src/traits/mod.rs Normal file
View File

@@ -0,0 +1,8 @@
//! Ethereum rpc interfaces.
pub mod web3;
pub mod eth;
pub mod net;
pub use self::web3::Web3;
pub use self::eth::{Eth, EthFilter};
pub use self::net::Net;

20
rpc/src/traits/net.rs Normal file
View File

@@ -0,0 +1,20 @@
//! Net rpc interface.
use std::sync::Arc;
use jsonrpc_core::*;
/// Net rpc interface.
pub trait Net: Sized + Send + Sync + 'static {
/// Returns protocol version.
fn version(&self, _: Params) -> Result<Value, Error> { rpcerr!() }
/// Returns number of peers connected to node.
fn peer_count(&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("net_version", Net::version);
delegate.add_method("net_peerCount", Net::peer_count);
delegate
}
}

16
rpc/src/traits/web3.rs Normal file
View File

@@ -0,0 +1,16 @@
//! Web3 rpc interface.
use std::sync::Arc;
use jsonrpc_core::*;
/// Web3 rpc interface.
pub trait Web3: Sized + Send + Sync + 'static {
/// Returns current client version.
fn client_version(&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("web3_clientVersion", Web3::client_version);
delegate
}
}

41
rpc/src/types/block.rs Normal file
View File

@@ -0,0 +1,41 @@
use util::hash::*;
use util::uint::*;
#[derive(Default)]
pub struct Block {
hash: H256,
#[serde(rename="parentHash")]
parent_hash: H256,
#[serde(rename="sha3Uncles")]
uncles_hash: H256,
author: Address,
// TODO: get rid of this one
miner: Address,
#[serde(rename="stateRoot")]
state_root: H256,
#[serde(rename="transactionsRoot")]
transactions_root: H256,
#[serde(rename="receiptsRoot")]
receipts_root: H256,
number: u64,
#[serde(rename="gasUsed")]
gas_used: U256,
#[serde(rename="gasLimit")]
gas_limit: U256,
// TODO: figure out how to properly serialize bytes
//#[serde(rename="extraData")]
//extra_data: Vec<u8>,
#[serde(rename="logsBloom")]
logs_bloom: H2048,
timestamp: u64
}
#[test]
fn test_block_serialize() {
use serde_json;
let block = Block::default();
//let serialized = serde_json::to_string(&block).unwrap();
//println!("s: {:?}", serialized);
//assert!(false);
}

1
rpc/src/types/mod.rs Normal file
View File

@@ -0,0 +1 @@
mod block;