diff --git a/src/bin/client/ethrpc.rs b/src/bin/client/ethrpc.rs deleted file mode 100644 index 09c93041f..000000000 --- a/src/bin/client/ethrpc.rs +++ /dev/null @@ -1,163 +0,0 @@ -extern crate jsonrpc_core; -extern crate jsonrpc_http_server; - -use std::sync::{Arc, RwLock}; -use rustc_serialize::hex::ToHex; -use self::jsonrpc_core::{IoHandler, IoDelegate, Params, Value, Error, ErrorCode}; -use ethcore::client::*; -use util::hash::*; - -macro_rules! rpcerr { - () => (Err(Error::internal_error())) -} - -/// Web3 rpc interface. -pub trait Web3: Sized + Send + Sync + 'static { - /// Returns current client version. - fn client_version(&self, _: Params) -> Result { rpcerr!() } - - /// Should be used to convert object to io delegate. - fn to_delegate(self) -> IoDelegate { - let mut delegate = IoDelegate::new(Arc::new(self)); - delegate.add_method("web3_clientVersion", Web3::client_version); - delegate - } -} - - -/// Eth rpc interface. -pub trait Eth: Sized + Send + Sync + 'static { - /// Returns protocol version. - fn protocol_version(&self, _: Params) -> Result { rpcerr!() } - - /// Returns block author. - fn author(&self, _: Params) -> Result { rpcerr!() } - - /// Returns current gas_price. - fn gas_price(&self, _: Params) -> Result { rpcerr!() } - - /// Returns highest block number. - fn block_number(&self, _: Params) -> Result { rpcerr!() } - - /// Returns block with given index / hash. - fn block(&self, _: Params) -> Result { rpcerr!() } - - /// Returns true if client is actively mining new blocks. - fn is_mining(&self, _: Params) -> Result { rpcerr!() } - - /// Returns the number of hashes per second that the node is mining with. - fn hashrate(&self, _: Params) -> Result { rpcerr!() } - - /// Returns the number of transactions in a block. - fn block_transaction_count(&self, _: Params) -> Result { rpcerr!() } - - /// Should be used to convert object to io delegate. - fn to_delegate(self) -> IoDelegate { - 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 - } -} - -/// Net rpc interface. -pub trait Net: Sized + Send + Sync + 'static { - /// Returns protocol version. - fn version(&self, _: Params) -> Result { rpcerr!() } - - /// Returns number of peers connected to node. - fn peer_count(&self, _: Params) -> Result { rpcerr!() } - - fn to_delegate(self) -> IoDelegate { - let mut delegate = IoDelegate::new(Arc::new(self)); - delegate.add_method("peer_count", Net::version); - delegate.add_method("net_version", Net::version); - delegate - } -} - -pub struct Web3Client; - -impl Web3Client { - pub fn new() -> Self { Web3Client } -} - -impl Web3 for Web3Client { - fn client_version(&self, params: Params) -> Result { - match params { - Params::None => Ok(Value::String("parity/0.1.0/-/rust1.7-nightly".to_string())), - _ => Err(Error::invalid_params()) - } - } -} - -pub struct EthClient { - client: Arc>, -} - -impl EthClient { - pub fn new(client: Arc>) -> Self { - EthClient { - client: client - } - } -} - -impl Eth for EthClient { - fn protocol_version(&self, params: Params) -> Result { - match params { - Params::None => Ok(Value::U64(63)), - _ => Err(Error::invalid_params()) - } - } - - fn author(&self, params: Params) -> Result { - match params { - Params::None => Ok(Value::String(Address::new().to_hex())), - _ => Err(Error::invalid_params()) - } - } - - fn block_number(&self, params: Params) -> Result { - match params { - Params::None => Ok(Value::U64(self.client.read().unwrap().chain_info().best_block_number)), - _ => Err(Error::invalid_params()) - } - } - - fn is_mining(&self, params: Params) -> Result { - match params { - Params::None => Ok(Value::Bool(false)), - _ => Err(Error::invalid_params()) - } - } -} - -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(&mut self, delegate: IoDelegate) 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) - } -} diff --git a/src/bin/client/main.rs b/src/bin/client/main.rs index f9f073718..bb19ca700 100644 --- a/src/bin/client/main.rs +++ b/src/bin/client/main.rs @@ -5,7 +5,7 @@ extern crate log; extern crate env_logger; #[cfg(feature = "rpc")] -mod ethrpc; +mod rpc; use std::io::stdin; use std::env; @@ -32,7 +32,7 @@ fn setup_log() { #[cfg(feature = "rpc")] fn setup_rpc_server(client: Arc>) { - use self::ethrpc::*; + use self::rpc::*; let mut server = HttpServer::new(1); server.add_delegate(Web3Client::new().to_delegate()); diff --git a/src/bin/client/rpc/impls/eth.rs b/src/bin/client/rpc/impls/eth.rs new file mode 100644 index 000000000..89c23419e --- /dev/null +++ b/src/bin/client/rpc/impls/eth.rs @@ -0,0 +1,48 @@ +use std::sync::{Arc, RwLock}; +use rustc_serialize::hex::ToHex; +use util::hash::*; +use ethcore::client::*; +use rpc::jsonrpc_core::*; +use rpc::Eth; + +pub struct EthClient { + client: Arc>, +} + +impl EthClient { + pub fn new(client: Arc>) -> Self { + EthClient { + client: client + } + } +} + +impl Eth for EthClient { + fn protocol_version(&self, params: Params) -> Result { + match params { + Params::None => Ok(Value::U64(63)), + _ => Err(Error::invalid_params()) + } + } + + fn author(&self, params: Params) -> Result { + match params { + Params::None => Ok(Value::String(Address::new().to_hex())), + _ => Err(Error::invalid_params()) + } + } + + fn block_number(&self, params: Params) -> Result { + match params { + Params::None => Ok(Value::U64(self.client.read().unwrap().chain_info().best_block_number)), + _ => Err(Error::invalid_params()) + } + } + + fn is_mining(&self, params: Params) -> Result { + match params { + Params::None => Ok(Value::Bool(false)), + _ => Err(Error::invalid_params()) + } + } +} diff --git a/src/bin/client/rpc/impls/mod.rs b/src/bin/client/rpc/impls/mod.rs new file mode 100644 index 000000000..d229c5c13 --- /dev/null +++ b/src/bin/client/rpc/impls/mod.rs @@ -0,0 +1,7 @@ +//! Ethereum rpc interface implementation. +pub mod web3; +pub mod eth; +pub mod net; + +pub use self::web3::Web3Client; +pub use self::eth::EthClient; diff --git a/src/bin/client/rpc/impls/net.rs b/src/bin/client/rpc/impls/net.rs new file mode 100644 index 000000000..e69de29bb diff --git a/src/bin/client/rpc/impls/web3.rs b/src/bin/client/rpc/impls/web3.rs new file mode 100644 index 000000000..b7d8919e2 --- /dev/null +++ b/src/bin/client/rpc/impls/web3.rs @@ -0,0 +1,17 @@ +use rpc::jsonrpc_core::*; +use rpc::Web3; + +pub struct Web3Client; + +impl Web3Client { + pub fn new() -> Self { Web3Client } +} + +impl Web3 for Web3Client { + fn client_version(&self, params: Params) -> Result { + match params { + Params::None => Ok(Value::String("parity/0.1.0/-/rust1.7-nightly".to_string())), + _ => Err(Error::invalid_params()) + } + } +} diff --git a/src/bin/client/rpc/mod.rs b/src/bin/client/rpc/mod.rs new file mode 100644 index 000000000..1053904e9 --- /dev/null +++ b/src/bin/client/rpc/mod.rs @@ -0,0 +1,38 @@ +extern crate jsonrpc_core; +extern crate jsonrpc_http_server; + +use self::jsonrpc_core::{IoHandler, IoDelegate}; + +macro_rules! rpcerr { + () => (Err(Error::internal_error())) +} + +pub mod traits; +pub mod impls; + +pub use self::traits::{Web3, Eth, 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(&mut self, delegate: IoDelegate) 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) + } +} diff --git a/src/bin/client/rpc/traits/eth.rs b/src/bin/client/rpc/traits/eth.rs new file mode 100644 index 000000000..8703d21fc --- /dev/null +++ b/src/bin/client/rpc/traits/eth.rs @@ -0,0 +1,45 @@ +//! Eth rpc interface. +use std::sync::Arc; +use rpc::jsonrpc_core::*; + +/// Eth rpc interface. +pub trait Eth: Sized + Send + Sync + 'static { + /// Returns protocol version. + fn protocol_version(&self, _: Params) -> Result { rpcerr!() } + + /// Returns block author. + fn author(&self, _: Params) -> Result { rpcerr!() } + + /// Returns current gas_price. + fn gas_price(&self, _: Params) -> Result { rpcerr!() } + + /// Returns highest block number. + fn block_number(&self, _: Params) -> Result { rpcerr!() } + + /// Returns block with given index / hash. + fn block(&self, _: Params) -> Result { rpcerr!() } + + /// Returns true if client is actively mining new blocks. + fn is_mining(&self, _: Params) -> Result { rpcerr!() } + + /// Returns the number of hashes per second that the node is mining with. + fn hashrate(&self, _: Params) -> Result { rpcerr!() } + + /// Returns the number of transactions in a block. + fn block_transaction_count(&self, _: Params) -> Result { rpcerr!() } + + /// Should be used to convert object to io delegate. + fn to_delegate(self) -> IoDelegate { + 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 + } +} + diff --git a/src/bin/client/rpc/traits/mod.rs b/src/bin/client/rpc/traits/mod.rs new file mode 100644 index 000000000..83f70f17d --- /dev/null +++ b/src/bin/client/rpc/traits/mod.rs @@ -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; +pub use self::net::Net; diff --git a/src/bin/client/rpc/traits/net.rs b/src/bin/client/rpc/traits/net.rs new file mode 100644 index 000000000..7cb7f6bee --- /dev/null +++ b/src/bin/client/rpc/traits/net.rs @@ -0,0 +1,20 @@ +//! Net rpc interface. +use std::sync::Arc; +use rpc::jsonrpc_core::*; + +/// Net rpc interface. +pub trait Net: Sized + Send + Sync + 'static { + /// Returns protocol version. + fn version(&self, _: Params) -> Result { rpcerr!() } + + /// Returns number of peers connected to node. + fn peer_count(&self, _: Params) -> Result { rpcerr!() } + + /// Should be used to convert object to io delegate. + fn to_delegate(self) -> IoDelegate { + let mut delegate = IoDelegate::new(Arc::new(self)); + delegate.add_method("peer_count", Net::version); + delegate.add_method("net_version", Net::version); + delegate + } +} diff --git a/src/bin/client/rpc/traits/web3.rs b/src/bin/client/rpc/traits/web3.rs new file mode 100644 index 000000000..b71c867aa --- /dev/null +++ b/src/bin/client/rpc/traits/web3.rs @@ -0,0 +1,16 @@ +//! Web3 rpc interface. +use std::sync::Arc; +use rpc::jsonrpc_core::*; + +/// Web3 rpc interface. +pub trait Web3: Sized + Send + Sync + 'static { + /// Returns current client version. + fn client_version(&self, _: Params) -> Result { rpcerr!() } + + /// Should be used to convert object to io delegate. + fn to_delegate(self) -> IoDelegate { + let mut delegate = IoDelegate::new(Arc::new(self)); + delegate.add_method("web3_clientVersion", Web3::client_version); + delegate + } +}