From 82373ab7a412e45eea373f52d3035e4ffcdbe860 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 20 Jan 2016 04:19:38 +0100 Subject: [PATCH 01/19] inital commit with eth_blockNumber working --- Cargo.toml | 3 +++ src/bin/client/ethrpc.rs | 53 ++++++++++++++++++++++++++++++++++++++++ src/bin/client/main.rs | 15 ++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/bin/client/ethrpc.rs diff --git a/Cargo.toml b/Cargo.toml index 04c4bf956..32be356c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,10 +20,13 @@ time = "0.1" evmjit = { path = "rust-evmjit", optional = true } ethash = { path = "ethash" } num_cpus = "0.2" +jsonrpc-core = { version = "1.0", optional = true } +jsonrpc-http-server = { version = "1.0", optional = true } [features] jit = ["evmjit"] evm_debug = [] +rpc = ["jsonrpc-core", "jsonrpc-http-server"] [[bin]] name = "client" diff --git a/src/bin/client/ethrpc.rs b/src/bin/client/ethrpc.rs new file mode 100644 index 000000000..d46a91559 --- /dev/null +++ b/src/bin/client/ethrpc.rs @@ -0,0 +1,53 @@ +extern crate jsonrpc_core; +extern crate jsonrpc_http_server; + +use std::sync::{Arc, RwLock}; +use self::jsonrpc_core::{IoHandler, IoDelegate, Params, Value, Error, ErrorCode}; +use ethcore::client::*; + +struct Eth { + client: Arc> +} + +impl Eth { + fn new(client: Arc>) -> Self { + Eth { + client: client + } + } + + 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::new(ErrorCode::InvalidParams)), + } + } +} + +struct EthRpc; + +impl EthRpc { + fn build_handler(client: Arc>) -> IoHandler { + let mut handler = IoHandler::new(); + let mut eth = IoDelegate::new(Arc::new(Eth::new(client))); + eth.add_method("eth_blockNumber", Eth::block_number); + handler.add_delegate(eth); + handler + } +} + +pub struct HttpServer { + server: jsonrpc_http_server::Server +} + +impl HttpServer { + pub fn new(client: Arc>, threads: usize) -> HttpServer { + HttpServer { + server: jsonrpc_http_server::Server::new(EthRpc::build_handler(client), threads) + } + } + + pub fn start_async(self, addr: &str) { + self.server.start_async(addr) + } +} diff --git a/src/bin/client/main.rs b/src/bin/client/main.rs index 3335d8a72..bf1b82909 100644 --- a/src/bin/client/main.rs +++ b/src/bin/client/main.rs @@ -4,6 +4,9 @@ extern crate rustc_serialize; extern crate log; extern crate env_logger; +#[cfg(feature = "rpc")] +mod ethrpc; + use std::io::stdin; use std::env; use log::{LogLevelFilter}; @@ -26,10 +29,22 @@ fn setup_log() { builder.init().unwrap(); } + +#[cfg(feature = "rpc")] +fn setup_rpc_server(client: Arc>) { + let server = ethrpc::HttpServer::new(client, 1); + server.start_async("127.0.0.1:3030"); +} + +#[cfg(not(feature = "rpc"))] +fn setup_rpc_server(_client: Arc>) { +} + fn main() { setup_log(); let spec = ethereum::new_frontier(); let mut service = ClientService::start(spec).unwrap(); + setup_rpc_server(service.client()); let io_handler = Box::new(ClientIoHandler { client: service.client(), timer: 0, info: Default::default() }); service.io().register_handler(io_handler).expect("Error registering IO handler"); loop { From a8e4912551f1df3877189800d91d8445d17526ef Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 20 Jan 2016 15:49:49 +0100 Subject: [PATCH 02/19] cleanup ethrpc --- src/bin/client/ethrpc.rs | 75 +++++++++++++++++++++++++++++----------- src/bin/client/main.rs | 3 +- 2 files changed, 57 insertions(+), 21 deletions(-) diff --git a/src/bin/client/ethrpc.rs b/src/bin/client/ethrpc.rs index d46a91559..735250d87 100644 --- a/src/bin/client/ethrpc.rs +++ b/src/bin/client/ethrpc.rs @@ -5,17 +5,56 @@ use std::sync::{Arc, RwLock}; use self::jsonrpc_core::{IoHandler, IoDelegate, Params, Value, Error, ErrorCode}; use ethcore::client::*; -struct Eth { - client: Arc> +macro_rules! rpcerr { + () => (Err(Error::new(ErrorCode::InternalError))) } -impl Eth { - fn new(client: Arc>) -> Self { - Eth { +/// This could be a part of `jsonrpc_core`. Unfortunately, +/// "only traits defined in the current crate can be implemented for a type parameter". +pub trait IntoDelegate where T: Send + Sync + 'static { + /// This function should be called to translate custom type into IoDelegate + fn into_delegate(self) -> IoDelegate; +} + +/// eth rpc interface +pub trait Eth { + /// 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!() } +} + +impl IntoDelegate for D where D: Eth + Send + Sync + 'static { + fn into_delegate(self) -> IoDelegate { + let mut delegate = IoDelegate::new(Arc::new(self)); + delegate.add_method("eth_protocolVersion", D::protocol_version); + delegate.add_method("eth_coinbase", D::author); + delegate.add_method("eth_gasPrice", D::gas_price); + delegate.add_method("eth_blockNumber", D::block_number); + delegate + } +} + +pub struct EthClient { + client: Arc>, +} + +impl EthClient { + pub fn new(client: Arc>) -> Self { + EthClient { client: client } } +} +impl Eth for EthClient { fn block_number(&self, params: Params) -> Result { match params { Params::None => Ok(Value::U64(self.client.read().unwrap().chain_info().best_block_number)), @@ -24,30 +63,26 @@ impl Eth { } } -struct EthRpc; - -impl EthRpc { - fn build_handler(client: Arc>) -> IoHandler { - let mut handler = IoHandler::new(); - let mut eth = IoDelegate::new(Arc::new(Eth::new(client))); - eth.add_method("eth_blockNumber", Eth::block_number); - handler.add_delegate(eth); - handler - } -} pub struct HttpServer { - server: jsonrpc_http_server::Server + handler: IoHandler, + threads: usize } impl HttpServer { - pub fn new(client: Arc>, threads: usize) -> HttpServer { + pub fn new(threads: usize) -> HttpServer { HttpServer { - server: jsonrpc_http_server::Server::new(EthRpc::build_handler(client), threads) + handler: IoHandler::new(), + threads: threads } } + pub fn add_delegate(&mut self, delegate: I) where D: Send + Sync + 'static, I: IntoDelegate { + self.handler.add_delegate(delegate.into_delegate()); + } + pub fn start_async(self, addr: &str) { - self.server.start_async(addr) + 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 bf1b82909..3dd9b9c5c 100644 --- a/src/bin/client/main.rs +++ b/src/bin/client/main.rs @@ -32,7 +32,8 @@ fn setup_log() { #[cfg(feature = "rpc")] fn setup_rpc_server(client: Arc>) { - let server = ethrpc::HttpServer::new(client, 1); + let mut server = ethrpc::HttpServer::new(1); + server.add_delegate(ethrpc::EthClient::new(client)); server.start_async("127.0.0.1:3030"); } From 013ac2cf9a4b1ccb0c49610590520f6871e3963a Mon Sep 17 00:00:00 2001 From: debris Date: Thu, 21 Jan 2016 00:54:19 +0100 Subject: [PATCH 03/19] rpc api in progress --- src/bin/client/ethrpc.rs | 119 +++++++++++++++++++++++++++++++-------- src/bin/client/main.rs | 7 ++- 2 files changed, 102 insertions(+), 24 deletions(-) diff --git a/src/bin/client/ethrpc.rs b/src/bin/client/ethrpc.rs index 735250d87..09c93041f 100644 --- a/src/bin/client/ethrpc.rs +++ b/src/bin/client/ethrpc.rs @@ -2,46 +2,101 @@ 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::new(ErrorCode::InternalError))) + () => (Err(Error::internal_error())) } -/// This could be a part of `jsonrpc_core`. Unfortunately, -/// "only traits defined in the current crate can be implemented for a type parameter". -pub trait IntoDelegate where T: Send + Sync + 'static { - /// This function should be called to translate custom type into IoDelegate - fn into_delegate(self) -> IoDelegate; +/// 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 { - /// returns protocol version + +/// Eth rpc interface. +pub trait Eth: Sized + Send + Sync + 'static { + /// Returns protocol version. fn protocol_version(&self, _: Params) -> Result { rpcerr!() } - /// returns block author + /// Returns block author. fn author(&self, _: Params) -> Result { rpcerr!() } - /// returns current gas_price + /// Returns current gas_price. fn gas_price(&self, _: Params) -> Result { rpcerr!() } - /// returns highest block number + /// 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 + } } -impl IntoDelegate for D where D: Eth + Send + Sync + 'static { - fn into_delegate(self) -> IoDelegate { +/// 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("eth_protocolVersion", D::protocol_version); - delegate.add_method("eth_coinbase", D::author); - delegate.add_method("eth_gasPrice", D::gas_price); - delegate.add_method("eth_blockNumber", D::block_number); + 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>, } @@ -55,15 +110,35 @@ impl EthClient { } 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::new(ErrorCode::InvalidParams)), + _ => 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 @@ -77,8 +152,8 @@ impl HttpServer { } } - pub fn add_delegate(&mut self, delegate: I) where D: Send + Sync + 'static, I: IntoDelegate { - self.handler.add_delegate(delegate.into_delegate()); + 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) { diff --git a/src/bin/client/main.rs b/src/bin/client/main.rs index 3dd9b9c5c..f9f073718 100644 --- a/src/bin/client/main.rs +++ b/src/bin/client/main.rs @@ -32,8 +32,11 @@ fn setup_log() { #[cfg(feature = "rpc")] fn setup_rpc_server(client: Arc>) { - let mut server = ethrpc::HttpServer::new(1); - server.add_delegate(ethrpc::EthClient::new(client)); + use self::ethrpc::*; + + let mut server = HttpServer::new(1); + server.add_delegate(Web3Client::new().to_delegate()); + server.add_delegate(EthClient::new(client).to_delegate()); server.start_async("127.0.0.1:3030"); } From 201c4726a22bc0d9e5f32d519f119596233a8be1 Mon Sep 17 00:00:00 2001 From: debris Date: Thu, 21 Jan 2016 01:19:29 +0100 Subject: [PATCH 04/19] split rpc into multiple files --- src/bin/client/ethrpc.rs | 163 ------------------------------ src/bin/client/main.rs | 4 +- src/bin/client/rpc/impls/eth.rs | 48 +++++++++ src/bin/client/rpc/impls/mod.rs | 7 ++ src/bin/client/rpc/impls/net.rs | 0 src/bin/client/rpc/impls/web3.rs | 17 ++++ src/bin/client/rpc/mod.rs | 38 +++++++ src/bin/client/rpc/traits/eth.rs | 45 +++++++++ src/bin/client/rpc/traits/mod.rs | 8 ++ src/bin/client/rpc/traits/net.rs | 20 ++++ src/bin/client/rpc/traits/web3.rs | 16 +++ 11 files changed, 201 insertions(+), 165 deletions(-) delete mode 100644 src/bin/client/ethrpc.rs create mode 100644 src/bin/client/rpc/impls/eth.rs create mode 100644 src/bin/client/rpc/impls/mod.rs create mode 100644 src/bin/client/rpc/impls/net.rs create mode 100644 src/bin/client/rpc/impls/web3.rs create mode 100644 src/bin/client/rpc/mod.rs create mode 100644 src/bin/client/rpc/traits/eth.rs create mode 100644 src/bin/client/rpc/traits/mod.rs create mode 100644 src/bin/client/rpc/traits/net.rs create mode 100644 src/bin/client/rpc/traits/web3.rs 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 + } +} From 85de41642e20e4c7c391af3962e475cb86931d5f Mon Sep 17 00:00:00 2001 From: debris Date: Thu, 21 Jan 2016 11:25:39 +0100 Subject: [PATCH 05/19] rpc api in progress --- src/bin/client/main.rs | 4 ++- src/bin/client/rpc/impls/eth.rs | 42 +++++++++++++++++++++++++++++++- src/bin/client/rpc/impls/mod.rs | 3 ++- src/bin/client/rpc/impls/net.rs | 15 ++++++++++++ src/bin/client/rpc/mod.rs | 2 +- src/bin/client/rpc/traits/eth.rs | 20 +++++++++++++++ src/bin/client/rpc/traits/mod.rs | 2 +- 7 files changed, 83 insertions(+), 5 deletions(-) diff --git a/src/bin/client/main.rs b/src/bin/client/main.rs index bb19ca700..fc1b10b8a 100644 --- a/src/bin/client/main.rs +++ b/src/bin/client/main.rs @@ -36,7 +36,9 @@ fn setup_rpc_server(client: Arc>) { 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"); } diff --git a/src/bin/client/rpc/impls/eth.rs b/src/bin/client/rpc/impls/eth.rs index 89c23419e..1151909ec 100644 --- a/src/bin/client/rpc/impls/eth.rs +++ b/src/bin/client/rpc/impls/eth.rs @@ -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>, @@ -32,6 +32,13 @@ impl Eth for EthClient { } } + fn gas_price(&self, params: Params) -> Result { + match params { + Params::None => Ok(Value::U64(0)), + _ => 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)), @@ -45,4 +52,37 @@ impl Eth for EthClient { _ => Err(Error::invalid_params()) } } + + fn hashrate(&self, params: Params) -> Result { + match params { + Params::None => Ok(Value::U64(0)), + _ => Err(Error::invalid_params()) + } + } +} + +pub struct EthFilterClient { + client: Arc> +} + +impl EthFilterClient { + pub fn new(client: Arc>) -> Self { + EthFilterClient { + client: client + } + } +} + +impl EthFilter for EthFilterClient { + fn new_block_filter(&self, _params: Params) -> Result { + Ok(Value::U64(0)) + } + + fn new_pending_transaction_filter(&self, _params: Params) -> Result { + Ok(Value::U64(1)) + } + + fn filter_changes(&self, _: Params) -> Result { + Ok(Value::String(self.client.read().unwrap().chain_info().best_block_hash.to_hex())) + } } diff --git a/src/bin/client/rpc/impls/mod.rs b/src/bin/client/rpc/impls/mod.rs index d229c5c13..813a168fd 100644 --- a/src/bin/client/rpc/impls/mod.rs +++ b/src/bin/client/rpc/impls/mod.rs @@ -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; diff --git a/src/bin/client/rpc/impls/net.rs b/src/bin/client/rpc/impls/net.rs index e69de29bb..6e528d156 100644 --- a/src/bin/client/rpc/impls/net.rs +++ b/src/bin/client/rpc/impls/net.rs @@ -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 { + Ok(Value::U64(0)) + } +} diff --git a/src/bin/client/rpc/mod.rs b/src/bin/client/rpc/mod.rs index 1053904e9..bf18e4b5f 100644 --- a/src/bin/client/rpc/mod.rs +++ b/src/bin/client/rpc/mod.rs @@ -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::*; diff --git a/src/bin/client/rpc/traits/eth.rs b/src/bin/client/rpc/traits/eth.rs index 8703d21fc..dfc72e89a 100644 --- a/src/bin/client/rpc/traits/eth.rs +++ b/src/bin/client/rpc/traits/eth.rs @@ -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 { rpcerr!() } + + /// Returns id of new block filter + fn new_pending_transaction_filter(&self, _: Params) -> Result { rpcerr!() } + + /// Returns filter changes since last poll + fn filter_changes(&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_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 + } +} diff --git a/src/bin/client/rpc/traits/mod.rs b/src/bin/client/rpc/traits/mod.rs index 83f70f17d..2fa52d538 100644 --- a/src/bin/client/rpc/traits/mod.rs +++ b/src/bin/client/rpc/traits/mod.rs @@ -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; From f38b736c91499da04fbe6b44345181bf89ed63d8 Mon Sep 17 00:00:00 2001 From: debris Date: Mon, 25 Jan 2016 17:45:26 +0100 Subject: [PATCH 06/19] debugging rpc... --- src/bin/client/rpc/impls/eth.rs | 3 ++- src/bin/client/rpc/impls/net.rs | 4 ++++ src/bin/client/rpc/traits/eth.rs | 2 +- src/bin/client/rpc/traits/net.rs | 2 +- src/client.rs | 2 +- 5 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/bin/client/rpc/impls/eth.rs b/src/bin/client/rpc/impls/eth.rs index d960114ed..821eacd07 100644 --- a/src/bin/client/rpc/impls/eth.rs +++ b/src/bin/client/rpc/impls/eth.rs @@ -83,6 +83,7 @@ impl EthFilter for EthFilterClient { } fn filter_changes(&self, _: Params) -> Result { - Ok(Value::String(self.client.chain_info().best_block_hash.to_hex())) + println!("filter changes: {:?}", self.client.chain_info().best_block_hash.to_hex()); + Ok(Value::Array(vec![Value::String(self.client.chain_info().best_block_hash.to_hex())])) } } diff --git a/src/bin/client/rpc/impls/net.rs b/src/bin/client/rpc/impls/net.rs index 6e528d156..f0109429c 100644 --- a/src/bin/client/rpc/impls/net.rs +++ b/src/bin/client/rpc/impls/net.rs @@ -9,6 +9,10 @@ impl NetClient { } impl Net for NetClient { + fn version(&self, _: Params) -> Result { + Ok(Value::U64(63)) + } + fn peer_count(&self, _params: Params) -> Result { Ok(Value::U64(0)) } diff --git a/src/bin/client/rpc/traits/eth.rs b/src/bin/client/rpc/traits/eth.rs index dfc72e89a..856111444 100644 --- a/src/bin/client/rpc/traits/eth.rs +++ b/src/bin/client/rpc/traits/eth.rs @@ -59,7 +59,7 @@ pub trait EthFilter: Sized + Send + Sync + 'static { 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.add_method("eth_getFilterChanges", EthFilter::filter_changes); delegate } } diff --git a/src/bin/client/rpc/traits/net.rs b/src/bin/client/rpc/traits/net.rs index 7cb7f6bee..63c64edb3 100644 --- a/src/bin/client/rpc/traits/net.rs +++ b/src/bin/client/rpc/traits/net.rs @@ -13,8 +13,8 @@ pub trait Net: Sized + Send + Sync + 'static { /// 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.add_method("net_peerCount", Net::peer_count); delegate } } diff --git a/src/client.rs b/src/client.rs index 4461f3d7b..01da143e5 100644 --- a/src/client.rs +++ b/src/client.rs @@ -269,7 +269,7 @@ impl Client { /// Tick the client. pub fn tick(&self) { - self.chain.read().unwrap().collect_garbage(false); + //self.chain.read().unwrap().collect_garbage(false); } } From 5237c575660a1d9dd0566dbfe6a4d0b588a20fdd Mon Sep 17 00:00:00 2001 From: debris Date: Tue, 26 Jan 2016 00:42:07 +0100 Subject: [PATCH 07/19] block transaction count --- src/bin/client/rpc/impls/eth.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/bin/client/rpc/impls/eth.rs b/src/bin/client/rpc/impls/eth.rs index 821eacd07..f4d102131 100644 --- a/src/bin/client/rpc/impls/eth.rs +++ b/src/bin/client/rpc/impls/eth.rs @@ -59,6 +59,10 @@ impl Eth for EthClient { _ => Err(Error::invalid_params()) } } + + fn block_transaction_count(&self, _: Params) -> Result { + Ok(Value::U64(0)) + } } pub struct EthFilterClient { @@ -83,7 +87,6 @@ impl EthFilter for EthFilterClient { } fn filter_changes(&self, _: Params) -> Result { - println!("filter changes: {:?}", self.client.chain_info().best_block_hash.to_hex()); Ok(Value::Array(vec![Value::String(self.client.chain_info().best_block_hash.to_hex())])) } } From b1282fe1f45ffca0dae5848ea684ef53b28f3dc8 Mon Sep 17 00:00:00 2001 From: debris Date: Tue, 26 Jan 2016 11:37:24 +0100 Subject: [PATCH 08/19] api block struct --- src/bin/client/main.rs | 2 ++ src/bin/client/rpc/impls/eth.rs | 6 +++++- src/bin/client/rpc/impls/mod.rs | 6 +++--- src/bin/client/rpc/mod.rs | 3 ++- src/bin/client/rpc/types/block.rs | 32 +++++++++++++++++++++++++++++++ src/bin/client/rpc/types/mod.rs | 1 + 6 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 src/bin/client/rpc/types/block.rs create mode 100644 src/bin/client/rpc/types/mod.rs diff --git a/src/bin/client/main.rs b/src/bin/client/main.rs index b88a28d72..53d5b2cd6 100644 --- a/src/bin/client/main.rs +++ b/src/bin/client/main.rs @@ -1,5 +1,7 @@ #![feature(plugin)] #![plugin(docopt_macros)] +// required for serde, move it to a separate library +#![feature(custom_derive, custom_attribute)] extern crate docopt; extern crate rustc_serialize; extern crate ethcore_util as util; diff --git a/src/bin/client/rpc/impls/eth.rs b/src/bin/client/rpc/impls/eth.rs index f4d102131..8fed91a14 100644 --- a/src/bin/client/rpc/impls/eth.rs +++ b/src/bin/client/rpc/impls/eth.rs @@ -1,4 +1,4 @@ -use std::sync::{Arc, RwLock}; +use std::sync::Arc; use rustc_serialize::hex::ToHex; use util::hash::*; use ethcore::client::*; @@ -63,6 +63,10 @@ impl Eth for EthClient { fn block_transaction_count(&self, _: Params) -> Result { Ok(Value::U64(0)) } + + fn block(&self, _: Params) -> Result { + Ok(Value::Null) + } } pub struct EthFilterClient { diff --git a/src/bin/client/rpc/impls/mod.rs b/src/bin/client/rpc/impls/mod.rs index 813a168fd..f10d613d0 100644 --- a/src/bin/client/rpc/impls/mod.rs +++ b/src/bin/client/rpc/impls/mod.rs @@ -1,7 +1,7 @@ //! Ethereum rpc interface implementation. -pub mod web3; -pub mod eth; -pub mod net; +mod web3; +mod eth; +mod net; pub use self::web3::Web3Client; pub use self::eth::{EthClient, EthFilterClient}; diff --git a/src/bin/client/rpc/mod.rs b/src/bin/client/rpc/mod.rs index bf18e4b5f..64f9137f0 100644 --- a/src/bin/client/rpc/mod.rs +++ b/src/bin/client/rpc/mod.rs @@ -8,7 +8,8 @@ macro_rules! rpcerr { } pub mod traits; -pub mod impls; +mod impls; +mod types; pub use self::traits::{Web3, Eth, EthFilter, Net}; pub use self::impls::*; diff --git a/src/bin/client/rpc/types/block.rs b/src/bin/client/rpc/types/block.rs new file mode 100644 index 000000000..ffb0f8042 --- /dev/null +++ b/src/bin/client/rpc/types/block.rs @@ -0,0 +1,32 @@ +use util::hash::*; +use util::uint::*; + +#[derive(Serialize)] +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, + #[serde(rename="logsBloom")] + logs_bloom: H2048, + timestamp: u64 +} + diff --git a/src/bin/client/rpc/types/mod.rs b/src/bin/client/rpc/types/mod.rs new file mode 100644 index 000000000..fc9210db1 --- /dev/null +++ b/src/bin/client/rpc/types/mod.rs @@ -0,0 +1 @@ +mod block; From d27c7f3902efb5e48635e2b307457c0e6fa20a63 Mon Sep 17 00:00:00 2001 From: debris Date: Tue, 26 Jan 2016 11:51:35 +0100 Subject: [PATCH 09/19] rpc block serialize test --- src/bin/client/rpc/types/block.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/bin/client/rpc/types/block.rs b/src/bin/client/rpc/types/block.rs index ffb0f8042..138140f60 100644 --- a/src/bin/client/rpc/types/block.rs +++ b/src/bin/client/rpc/types/block.rs @@ -1,7 +1,7 @@ use util::hash::*; use util::uint::*; -#[derive(Serialize)] +#[derive(Default, Serialize)] pub struct Block { hash: H256, #[serde(rename="parentHash")] @@ -30,3 +30,12 @@ pub struct Block { 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); +} From 3ac40b68f862f734871bbc7ca19f9dc840689ea0 Mon Sep 17 00:00:00 2001 From: debris Date: Tue, 26 Jan 2016 13:14:22 +0100 Subject: [PATCH 10/19] rpc and bin moved to its own crates --- Cargo.toml | 10 ---------- bin/Cargo.toml | 20 +++++++++++++++++++ {src/bin/client => bin/src}/main.rs | 5 ++--- rpc/Cargo.toml | 19 ++++++++++++++++++ {src/bin/client/rpc => rpc/src}/impls/eth.rs | 4 ++-- {src/bin/client/rpc => rpc/src}/impls/mod.rs | 0 {src/bin/client/rpc => rpc/src}/impls/net.rs | 4 ++-- {src/bin/client/rpc => rpc/src}/impls/web3.rs | 4 ++-- src/bin/client/rpc/mod.rs => rpc/src/lib.rs | 8 +++++++- {src/bin/client/rpc => rpc/src}/traits/eth.rs | 2 +- {src/bin/client/rpc => rpc/src}/traits/mod.rs | 0 {src/bin/client/rpc => rpc/src}/traits/net.rs | 2 +- .../bin/client/rpc => rpc/src}/traits/web3.rs | 2 +- .../bin/client/rpc => rpc/src}/types/block.rs | 8 ++++---- {src/bin/client/rpc => rpc/src}/types/mod.rs | 0 15 files changed, 61 insertions(+), 27 deletions(-) create mode 100644 bin/Cargo.toml rename {src/bin/client => bin/src}/main.rs (98%) create mode 100644 rpc/Cargo.toml rename {src/bin/client/rpc => rpc/src}/impls/eth.rs (97%) rename {src/bin/client/rpc => rpc/src}/impls/mod.rs (100%) rename {src/bin/client/rpc => rpc/src}/impls/net.rs (88%) rename {src/bin/client/rpc => rpc/src}/impls/web3.rs (88%) rename src/bin/client/rpc/mod.rs => rpc/src/lib.rs (80%) rename {src/bin/client/rpc => rpc/src}/traits/eth.rs (99%) rename {src/bin/client/rpc => rpc/src}/traits/mod.rs (100%) rename {src/bin/client/rpc => rpc/src}/traits/net.rs (95%) rename {src/bin/client/rpc => rpc/src}/traits/web3.rs (94%) rename {src/bin/client/rpc => rpc/src}/types/block.rs (84%) rename {src/bin/client/rpc => rpc/src}/types/mod.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 14f12e646..a0ea692b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,18 +21,8 @@ evmjit = { path = "rust-evmjit", optional = true } ethash = { path = "ethash" } num_cpus = "0.2" clippy = "0.0.37" -docopt = "0.6" -docopt_macros = "0.6" -ctrlc = "1.0" -jsonrpc-core = { version = "1.0", optional = true } -jsonrpc-http-server = { version = "1.0", optional = true } [features] jit = ["evmjit"] evm_debug = [] test-heavy = [] -rpc = ["jsonrpc-core", "jsonrpc-http-server"] - -[[bin]] -name = "client" -path = "src/bin/client/main.rs" diff --git a/bin/Cargo.toml b/bin/Cargo.toml new file mode 100644 index 000000000..ba258b586 --- /dev/null +++ b/bin/Cargo.toml @@ -0,0 +1,20 @@ +[package] +description = "Ethcore client." +name = "ethcore-client" +version = "0.1.0" +license = "GPL-3.0" +authors = ["Ethcore "] + +[dependencies] +log = "0.3" +env_logger = "0.3" +rustc-serialize = "0.3" +docopt = "0.6" +docopt_macros = "0.6" +ctrlc = "1.0" +ethcore-util = { path = "../util" } +ethcore-rpc = { path = "../rpc", optional = true } +ethcore = { path = ".." } + +[features] +rpc = ["ethcore-rpc"] diff --git a/src/bin/client/main.rs b/bin/src/main.rs similarity index 98% rename from src/bin/client/main.rs rename to bin/src/main.rs index 53d5b2cd6..942a5cf24 100644 --- a/src/bin/client/main.rs +++ b/bin/src/main.rs @@ -1,7 +1,6 @@ #![feature(plugin)] #![plugin(docopt_macros)] // required for serde, move it to a separate library -#![feature(custom_derive, custom_attribute)] extern crate docopt; extern crate rustc_serialize; extern crate ethcore_util as util; @@ -11,7 +10,7 @@ extern crate env_logger; extern crate ctrlc; #[cfg(feature = "rpc")] -mod rpc; +extern crate ethcore_rpc as rpc; use std::env; use log::{LogLevelFilter}; @@ -52,7 +51,7 @@ fn setup_log(init: &String) { #[cfg(feature = "rpc")] fn setup_rpc_server(client: Arc) { - use self::rpc::*; + use rpc::*; let mut server = HttpServer::new(1); server.add_delegate(Web3Client::new().to_delegate()); diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml new file mode 100644 index 000000000..d0f9f50e8 --- /dev/null +++ b/rpc/Cargo.toml @@ -0,0 +1,19 @@ +[package] +description = "Ethcore jsonrpc" +name = "ethcore-rpc" +version = "0.1.0" +license = "GPL-3.0" +authors = ["Marek Kotewicz , diff --git a/src/bin/client/rpc/impls/mod.rs b/rpc/src/impls/mod.rs similarity index 100% rename from src/bin/client/rpc/impls/mod.rs rename to rpc/src/impls/mod.rs diff --git a/src/bin/client/rpc/impls/net.rs b/rpc/src/impls/net.rs similarity index 88% rename from src/bin/client/rpc/impls/net.rs rename to rpc/src/impls/net.rs index f0109429c..a1d36de54 100644 --- a/src/bin/client/rpc/impls/net.rs +++ b/rpc/src/impls/net.rs @@ -1,6 +1,6 @@ //! Net rpc implementation. -use rpc::jsonrpc_core::*; -use rpc::Net; +use jsonrpc_core::*; +use traits::Net; pub struct NetClient; diff --git a/src/bin/client/rpc/impls/web3.rs b/rpc/src/impls/web3.rs similarity index 88% rename from src/bin/client/rpc/impls/web3.rs rename to rpc/src/impls/web3.rs index b7d8919e2..58e7858eb 100644 --- a/src/bin/client/rpc/impls/web3.rs +++ b/rpc/src/impls/web3.rs @@ -1,5 +1,5 @@ -use rpc::jsonrpc_core::*; -use rpc::Web3; +use jsonrpc_core::*; +use traits::Web3; pub struct Web3Client; diff --git a/src/bin/client/rpc/mod.rs b/rpc/src/lib.rs similarity index 80% rename from src/bin/client/rpc/mod.rs rename to rpc/src/lib.rs index 64f9137f0..148e9f134 100644 --- a/src/bin/client/rpc/mod.rs +++ b/rpc/src/lib.rs @@ -1,5 +1,12 @@ +#![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}; @@ -14,7 +21,6 @@ mod types; pub use self::traits::{Web3, Eth, EthFilter, Net}; pub use self::impls::*; - pub struct HttpServer { handler: IoHandler, threads: usize diff --git a/src/bin/client/rpc/traits/eth.rs b/rpc/src/traits/eth.rs similarity index 99% rename from src/bin/client/rpc/traits/eth.rs rename to rpc/src/traits/eth.rs index 856111444..31e9df164 100644 --- a/src/bin/client/rpc/traits/eth.rs +++ b/rpc/src/traits/eth.rs @@ -1,6 +1,6 @@ //! Eth rpc interface. use std::sync::Arc; -use rpc::jsonrpc_core::*; +use jsonrpc_core::*; /// Eth rpc interface. pub trait Eth: Sized + Send + Sync + 'static { diff --git a/src/bin/client/rpc/traits/mod.rs b/rpc/src/traits/mod.rs similarity index 100% rename from src/bin/client/rpc/traits/mod.rs rename to rpc/src/traits/mod.rs diff --git a/src/bin/client/rpc/traits/net.rs b/rpc/src/traits/net.rs similarity index 95% rename from src/bin/client/rpc/traits/net.rs rename to rpc/src/traits/net.rs index 63c64edb3..4df8d7114 100644 --- a/src/bin/client/rpc/traits/net.rs +++ b/rpc/src/traits/net.rs @@ -1,6 +1,6 @@ //! Net rpc interface. use std::sync::Arc; -use rpc::jsonrpc_core::*; +use jsonrpc_core::*; /// Net rpc interface. pub trait Net: Sized + Send + Sync + 'static { diff --git a/src/bin/client/rpc/traits/web3.rs b/rpc/src/traits/web3.rs similarity index 94% rename from src/bin/client/rpc/traits/web3.rs rename to rpc/src/traits/web3.rs index b71c867aa..8e73d4304 100644 --- a/src/bin/client/rpc/traits/web3.rs +++ b/rpc/src/traits/web3.rs @@ -1,6 +1,6 @@ //! Web3 rpc interface. use std::sync::Arc; -use rpc::jsonrpc_core::*; +use jsonrpc_core::*; /// Web3 rpc interface. pub trait Web3: Sized + Send + Sync + 'static { diff --git a/src/bin/client/rpc/types/block.rs b/rpc/src/types/block.rs similarity index 84% rename from src/bin/client/rpc/types/block.rs rename to rpc/src/types/block.rs index 138140f60..c15c8186d 100644 --- a/src/bin/client/rpc/types/block.rs +++ b/rpc/src/types/block.rs @@ -1,7 +1,7 @@ use util::hash::*; use util::uint::*; -#[derive(Default, Serialize)] +#[derive(Default)] pub struct Block { hash: H256, #[serde(rename="parentHash")] @@ -35,7 +35,7 @@ fn test_block_serialize() { use serde_json; let block = Block::default(); - let serialized = serde_json::to_string(&block).unwrap(); - println!("s: {:?}", serialized); - assert!(false); + //let serialized = serde_json::to_string(&block).unwrap(); + //println!("s: {:?}", serialized); + //assert!(false); } diff --git a/src/bin/client/rpc/types/mod.rs b/rpc/src/types/mod.rs similarity index 100% rename from src/bin/client/rpc/types/mod.rs rename to rpc/src/types/mod.rs From 2f42e0eda0af6207d3d7d59b007af02256d88806 Mon Sep 17 00:00:00 2001 From: debris Date: Tue, 26 Jan 2016 19:24:33 +0100 Subject: [PATCH 11/19] parity on netstats --- rpc/Cargo.toml | 1 - rpc/src/impls/eth.rs | 20 ++++++++++++++------ rpc/src/lib.rs | 3 ++- rpc/src/traits/eth.rs | 1 + rpc/src/types/block.rs | 6 +++--- rpc/src/types/mod.rs | 17 +++++++++++++++++ util/Cargo.toml | 1 + util/src/hash.rs | 37 +++++++++++++++++++++++++++++++++++++ util/src/lib.rs | 1 + util/src/uint.rs | 13 +++++++++++++ 10 files changed, 89 insertions(+), 11 deletions(-) diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index d0f9f50e8..ccdd46679 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -8,7 +8,6 @@ authors = ["Marek Kotewicz , @@ -27,7 +28,7 @@ impl Eth for EthClient { fn author(&self, params: Params) -> Result { match params { - Params::None => Ok(Value::String(Address::new().to_hex())), + Params::None => Ok(as_value(&Address::new())), _ => Err(Error::invalid_params()) } } @@ -64,8 +65,15 @@ impl Eth for EthClient { Ok(Value::U64(0)) } - fn block(&self, _: Params) -> Result { - Ok(Value::Null) + fn block(&self, params: Params) -> Result { + if let Params::Array(ref arr) = params { + if let [ref h, Value::Bool(ref include_transactions)] = arr as &[Value] { + if let Ok(hash) = from_value::(h.clone()) { + return Ok(as_value(&Block::default())) + } + } + } + Err(Error::invalid_params()) } } @@ -91,6 +99,6 @@ impl EthFilter for EthFilterClient { } fn filter_changes(&self, _: Params) -> Result { - Ok(Value::Array(vec![Value::String(self.client.chain_info().best_block_hash.to_hex())])) + Ok(Value::Array(vec![as_value(&self.client.chain_info().best_block_hash)])) } } diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 148e9f134..43a24a1fb 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -1,8 +1,9 @@ #![feature(custom_derive, custom_attribute, plugin)] +#![feature(slice_patterns)] #![plugin(serde_macros)] -extern crate rustc_serialize; extern crate serde; +extern crate serde_json; extern crate jsonrpc_core; extern crate jsonrpc_http_server; extern crate ethcore_util as util; diff --git a/rpc/src/traits/eth.rs b/rpc/src/traits/eth.rs index 31e9df164..35b59a91c 100644 --- a/rpc/src/traits/eth.rs +++ b/rpc/src/traits/eth.rs @@ -35,6 +35,7 @@ pub trait Eth: Sized + Send + Sync + 'static { 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_getBlockByHash", Eth::block); delegate.add_method("eth_getBlockByNumber", Eth::block); delegate.add_method("eth_mining", Eth::is_mining); delegate.add_method("eth_hashrate", Eth::hashrate); diff --git a/rpc/src/types/block.rs b/rpc/src/types/block.rs index c15c8186d..7b23e2e0c 100644 --- a/rpc/src/types/block.rs +++ b/rpc/src/types/block.rs @@ -1,7 +1,7 @@ use util::hash::*; use util::uint::*; -#[derive(Default)] +#[derive(Default, Serialize)] pub struct Block { hash: H256, #[serde(rename="parentHash")] @@ -35,7 +35,7 @@ fn test_block_serialize() { use serde_json; let block = Block::default(); - //let serialized = serde_json::to_string(&block).unwrap(); - //println!("s: {:?}", serialized); + let serialized = serde_json::to_string(&block).unwrap(); + println!("s: {:?}", serialized); //assert!(false); } diff --git a/rpc/src/types/mod.rs b/rpc/src/types/mod.rs index fc9210db1..a2f1da63e 100644 --- a/rpc/src/types/mod.rs +++ b/rpc/src/types/mod.rs @@ -1 +1,18 @@ +use serde::{Serialize, Deserialize, de}; +use serde_json::value::{Value, Serializer, Deserializer}; + mod block; + +pub fn as_value(s: &S) -> Value where S: Serialize { + let mut serializer = Serializer::new(); + // should never panic! + s.serialize(&mut serializer).unwrap(); + serializer.unwrap() +} + +pub fn from_value(value: Value) -> Result::Error> where D: Deserialize { + let mut deserialier = Deserializer::new(value); + Deserialize::deserialize(&mut deserialier) +} + +pub use self::block::Block; diff --git a/util/Cargo.toml b/util/Cargo.toml index a91bff962..362db33b2 100644 --- a/util/Cargo.toml +++ b/util/Cargo.toml @@ -25,6 +25,7 @@ itertools = "0.4" crossbeam = "0.2" slab = { git = "https://github.com/arkpar/slab.git" } sha3 = { path = "sha3" } +serde = "0.6.7" clippy = "*" # Always newest, since we use nightly [dev-dependencies] diff --git a/util/src/hash.rs b/util/src/hash.rs index 0e4139f3c..011746028 100644 --- a/util/src/hash.rs +++ b/util/src/hash.rs @@ -8,6 +8,8 @@ use rand::os::OsRng; use bytes::{BytesConvertable,Populatable}; use from_json::*; use uint::{Uint, U256}; +use rustc_serialize::hex::ToHex; +use serde; /// Trait for a fixed-size byte array to be used as the output of hash functions. /// @@ -205,6 +207,41 @@ macro_rules! impl_hash { } } + impl serde::Serialize for $from { + fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + where S: serde::Serializer { + let mut hex = "0x".to_owned(); + hex.push_str(self.to_hex().as_ref()); + serializer.visit_str(hex.as_ref()) + } + } + + impl serde::Deserialize for $from { + fn deserialize(deserializer: &mut D) -> Result<$from, D::Error> + where D: serde::Deserializer { + struct HashVisitor; + + impl serde::de::Visitor for HashVisitor { + type Value = $from; + + fn visit_str(&mut self, value: &str) -> Result where E: serde::Error { + // 0x + len + if value.len() != 2 + $size * 2 { + return Err(serde::Error::syntax("Invalid length.")); + } + + value[2..].from_hex().map(|ref v| $from::from_slice(v)).map_err(|_| serde::Error::syntax("Invalid valid hex.")) + } + + fn visit_string(&mut self, value: String) -> Result where E: serde::Error { + self.visit_str(value.as_ref()) + } + } + + deserializer.visit(HashVisitor) + } + } + impl FromJson for $from { fn from_json(json: &Json) -> Self { match *json { diff --git a/util/src/lib.rs b/util/src/lib.rs index 970c0713c..b1b93968c 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -55,6 +55,7 @@ extern crate secp256k1; extern crate arrayvec; extern crate elastic_array; extern crate crossbeam; +extern crate serde; /// TODO [Gav Wood] Please document me pub mod standard; diff --git a/util/src/uint.rs b/util/src/uint.rs index ab136d7c6..de05ca4a8 100644 --- a/util/src/uint.rs +++ b/util/src/uint.rs @@ -23,6 +23,8 @@ use standard::*; use from_json::*; +use rustc_serialize::hex::ToHex; +use serde; macro_rules! impl_map_from { ($thing:ident, $from:ty, $to:ty) => { @@ -436,6 +438,17 @@ macro_rules! construct_uint { } } + impl serde::Serialize for $name { + fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + where S: serde::Serializer { + let mut hex = "0x".to_owned(); + let mut bytes = [0u8; 8 * $n_words]; + self.to_bytes(&mut bytes); + hex.push_str(bytes.to_hex().as_ref()); + serializer.visit_str(hex.as_ref()) + } + } + impl From for $name { fn from(value: u64) -> $name { let mut ret = [0; $n_words]; From 58651392065f67071991cbb587cf71aa48ca8516 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 27 Jan 2016 12:31:54 +0100 Subject: [PATCH 12/19] block visible on netstats --- rpc/src/impls/eth.rs | 39 +++++++++++++++++++++++++++++++++------ rpc/src/impls/web3.rs | 3 ++- rpc/src/types/block.rs | 33 +++++++++++++++++++-------------- rpc/src/types/mod.rs | 2 +- src/client.rs | 8 ++++++++ src/externalities.rs | 31 ++++++++++--------------------- 6 files changed, 73 insertions(+), 43 deletions(-) diff --git a/rpc/src/impls/eth.rs b/rpc/src/impls/eth.rs index 54372ab3a..f0ad73c57 100644 --- a/rpc/src/impls/eth.rs +++ b/rpc/src/impls/eth.rs @@ -1,10 +1,12 @@ use std::sync::Arc; -use serde_json; use jsonrpc_core::*; use util::hash::*; +use util::uint::*; +use util::sha3::*; use ethcore::client::*; +use ethcore::views::*; use traits::{Eth, EthFilter}; -use types::{Block, as_value, from_value}; +use types::{Block, to_value, from_value}; pub struct EthClient { client: Arc, @@ -28,7 +30,7 @@ impl Eth for EthClient { fn author(&self, params: Params) -> Result { match params { - Params::None => Ok(as_value(&Address::new())), + Params::None => Ok(to_value(&Address::new())), _ => Err(Error::invalid_params()) } } @@ -67,9 +69,34 @@ impl Eth for EthClient { fn block(&self, params: Params) -> Result { if let Params::Array(ref arr) = params { - if let [ref h, Value::Bool(ref include_transactions)] = arr as &[Value] { + if let [ref h, Value::Bool(ref _include_txs)] = arr as &[Value] { if let Ok(hash) = from_value::(h.clone()) { - return Ok(as_value(&Block::default())) + return match (self.client.block_header(&hash), self.client.block_details(&hash)) { + (Some(bytes), Some(details)) => { + let view = HeaderView::new(&bytes); + let block = Block { + hash: view.sha3(), + parent_hash: view.parent_hash(), + uncles_hash: view.uncles_hash(), + author: view.author(), + miner: view.author(), + state_root: view.state_root(), + transactions_root: view.transactions_root(), + receipts_root: view.receipts_root(), + number: U256::from(view.number()), + gas_used: view.gas_used(), + gas_limit: view.gas_limit(), + logs_bloom: view.log_bloom(), + timestamp: U256::from(view.timestamp()), + difficulty: view.difficulty(), + total_difficulty: details.total_difficulty, + uncles: vec![], + transactions: vec![] + }; + Ok(to_value(&block)) + }, + _ => Ok(Value::Null), + } } } } @@ -99,6 +126,6 @@ impl EthFilter for EthFilterClient { } fn filter_changes(&self, _: Params) -> Result { - Ok(Value::Array(vec![as_value(&self.client.chain_info().best_block_hash)])) + Ok(Value::Array(vec![to_value(&self.client.chain_info().best_block_hash)])) } } diff --git a/rpc/src/impls/web3.rs b/rpc/src/impls/web3.rs index 58e7858eb..50eb9c6f5 100644 --- a/rpc/src/impls/web3.rs +++ b/rpc/src/impls/web3.rs @@ -10,7 +10,8 @@ impl 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())), + //Params::None => Ok(Value::String("parity/0.1.0/-/rust1.7-nightly".to_owned())), + Params::None => Ok(Value::String("surprise/0.1.0/surprise/surprise".to_owned())), _ => Err(Error::invalid_params()) } } diff --git a/rpc/src/types/block.rs b/rpc/src/types/block.rs index 7b23e2e0c..ac1e673a5 100644 --- a/rpc/src/types/block.rs +++ b/rpc/src/types/block.rs @@ -1,33 +1,38 @@ use util::hash::*; use util::uint::*; -#[derive(Default, Serialize)] +#[derive(Default, Debug, Serialize)] pub struct Block { - hash: H256, + pub hash: H256, #[serde(rename="parentHash")] - parent_hash: H256, + pub parent_hash: H256, #[serde(rename="sha3Uncles")] - uncles_hash: H256, - author: Address, + pub uncles_hash: H256, + pub author: Address, // TODO: get rid of this one - miner: Address, + pub miner: Address, #[serde(rename="stateRoot")] - state_root: H256, + pub state_root: H256, #[serde(rename="transactionsRoot")] - transactions_root: H256, + pub transactions_root: H256, #[serde(rename="receiptsRoot")] - receipts_root: H256, - number: u64, + pub receipts_root: H256, + pub number: U256, #[serde(rename="gasUsed")] - gas_used: U256, + pub gas_used: U256, #[serde(rename="gasLimit")] - gas_limit: U256, + pub gas_limit: U256, // TODO: figure out how to properly serialize bytes //#[serde(rename="extraData")] //extra_data: Vec, #[serde(rename="logsBloom")] - logs_bloom: H2048, - timestamp: u64 + pub logs_bloom: H2048, + pub timestamp: U256, + pub difficulty: U256, + #[serde(rename="totalDifficulty")] + pub total_difficulty: U256, + pub uncles: Vec, + pub transactions: Vec } #[test] diff --git a/rpc/src/types/mod.rs b/rpc/src/types/mod.rs index a2f1da63e..0b7d97916 100644 --- a/rpc/src/types/mod.rs +++ b/rpc/src/types/mod.rs @@ -3,7 +3,7 @@ use serde_json::value::{Value, Serializer, Deserializer}; mod block; -pub fn as_value(s: &S) -> Value where S: Serialize { +pub fn to_value(s: &S) -> Value where S: Serialize { let mut serializer = Serializer::new(); // should never panic! s.serialize(&mut serializer).unwrap(); diff --git a/src/client.rs b/src/client.rs index 2e9029709..e8d8fc8f2 100644 --- a/src/client.rs +++ b/src/client.rs @@ -11,6 +11,7 @@ use service::NetSyncMessage; use env_info::LastHashes; use verification::*; use block::*; +use extras::BlockDetails; /// General block status #[derive(Debug)] @@ -64,6 +65,9 @@ pub trait BlockChainClient : Sync + Send { /// Get block status by block header hash. fn block_status(&self, hash: &H256) -> BlockStatus; + /// Get familial details concerning a block. + fn block_details(&self, hash: &H256) -> Option; + /// Get raw block header data by block number. fn block_header_at(&self, n: BlockNumber) -> Option; @@ -299,6 +303,10 @@ impl BlockChainClient for Client { fn block_status(&self, hash: &H256) -> BlockStatus { if self.chain.read().unwrap().is_known(&hash) { BlockStatus::InChain } else { BlockStatus::Unknown } } + + fn block_details(&self, hash: &H256) -> Option { + self.chain.read().unwrap().block_details(hash) + } fn block_header_at(&self, n: BlockNumber) -> Option { self.chain.read().unwrap().block_hash(n).and_then(|h| self.block_header(&h)) diff --git a/src/externalities.rs b/src/externalities.rs index f9a79c3c0..8b16cc72b 100644 --- a/src/externalities.rs +++ b/src/externalities.rs @@ -19,8 +19,7 @@ pub enum OutputPolicy<'a> { pub struct OriginInfo { address: Address, origin: Address, - gas_price: U256, - value: U256 + gas_price: U256 } impl OriginInfo { @@ -29,11 +28,7 @@ impl OriginInfo { OriginInfo { address: params.address.clone(), origin: params.origin.clone(), - gas_price: params.gas_price.clone(), - value: match params.value { - ActionValue::Transfer(val) => val, - ActionValue::Apparent(val) => val, - } + gas_price: params.gas_price.clone() } } } @@ -116,7 +111,7 @@ impl<'a> Ext for Externalities<'a> { origin: self.origin_info.origin.clone(), gas: *gas, gas_price: self.origin_info.gas_price.clone(), - value: ActionValue::Transfer(value.clone()), + value: value.clone(), code: Some(code.to_vec()), data: None, }; @@ -136,29 +131,24 @@ impl<'a> Ext for Externalities<'a> { fn call(&mut self, gas: &U256, - sender_address: &Address, - receive_address: &Address, - value: Option, + address: &Address, + value: &U256, data: &[u8], code_address: &Address, output: &mut [u8]) -> MessageCallResult { - let mut params = ActionParams { - sender: sender_address.clone(), - address: receive_address.clone(), - value: ActionValue::Apparent(self.origin_info.value.clone()), + let params = ActionParams { code_address: code_address.clone(), + address: address.clone(), + sender: self.origin_info.address.clone(), origin: self.origin_info.origin.clone(), gas: *gas, gas_price: self.origin_info.gas_price.clone(), + value: value.clone(), code: self.state.code(code_address), data: Some(data.to_vec()), }; - if let Some(value) = value { - params.value = ActionValue::Transfer(value); - } - let mut ex = Executive::from_parent(self.state, self.env_info, self.engine, self.depth); match ex.call(params, self.substate, BytesRef::Fixed(output)) { @@ -168,10 +158,9 @@ impl<'a> Ext for Externalities<'a> { } fn extcode(&self, address: &Address) -> Bytes { - self.state.code(address).unwrap_or_else(|| vec![]) + self.state.code(address).unwrap_or(vec![]) } - #[allow(match_ref_pats)] fn ret(&mut self, gas: &U256, data: &[u8]) -> Result { match &mut self.output { &mut OutputPolicy::Return(BytesRef::Fixed(ref mut slice)) => unsafe { From 5998c16b17ecc454d4871e8ef0ec9e7010315c90 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 27 Jan 2016 13:08:56 +0100 Subject: [PATCH 13/19] reverted incorrect changes to externalities --- src/externalities.rs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/externalities.rs b/src/externalities.rs index e3a363fbc..f9a79c3c0 100644 --- a/src/externalities.rs +++ b/src/externalities.rs @@ -19,7 +19,8 @@ pub enum OutputPolicy<'a> { pub struct OriginInfo { address: Address, origin: Address, - gas_price: U256 + gas_price: U256, + value: U256 } impl OriginInfo { @@ -28,7 +29,11 @@ impl OriginInfo { OriginInfo { address: params.address.clone(), origin: params.origin.clone(), - gas_price: params.gas_price.clone() + gas_price: params.gas_price.clone(), + value: match params.value { + ActionValue::Transfer(val) => val, + ActionValue::Apparent(val) => val, + } } } } @@ -111,7 +116,7 @@ impl<'a> Ext for Externalities<'a> { origin: self.origin_info.origin.clone(), gas: *gas, gas_price: self.origin_info.gas_price.clone(), - value: value.clone(), + value: ActionValue::Transfer(value.clone()), code: Some(code.to_vec()), data: None, }; @@ -131,24 +136,29 @@ impl<'a> Ext for Externalities<'a> { fn call(&mut self, gas: &U256, - address: &Address, - value: &U256, + sender_address: &Address, + receive_address: &Address, + value: Option, data: &[u8], code_address: &Address, output: &mut [u8]) -> MessageCallResult { - let params = ActionParams { + let mut params = ActionParams { + sender: sender_address.clone(), + address: receive_address.clone(), + value: ActionValue::Apparent(self.origin_info.value.clone()), code_address: code_address.clone(), - address: address.clone(), - sender: self.origin_info.address.clone(), origin: self.origin_info.origin.clone(), gas: *gas, gas_price: self.origin_info.gas_price.clone(), - value: value.clone(), code: self.state.code(code_address), data: Some(data.to_vec()), }; + if let Some(value) = value { + params.value = ActionValue::Transfer(value); + } + let mut ex = Executive::from_parent(self.state, self.env_info, self.engine, self.depth); match ex.call(params, self.substate, BytesRef::Fixed(output)) { @@ -158,9 +168,10 @@ impl<'a> Ext for Externalities<'a> { } fn extcode(&self, address: &Address) -> Bytes { - self.state.code(address).unwrap_or(vec![]) + self.state.code(address).unwrap_or_else(|| vec![]) } + #[allow(match_ref_pats)] fn ret(&mut self, gas: &U256, data: &[u8]) -> Result { match &mut self.output { &mut OutputPolicy::Return(BytesRef::Fixed(ref mut slice)) => unsafe { @@ -204,7 +215,6 @@ impl<'a> Ext for Externalities<'a> { fn suicide(&mut self, refund_address: &Address) { let address = self.origin_info.address.clone(); let balance = self.balance(&address); - trace!("Suiciding {} -> {} (xfer: {})", address, refund_address, balance); self.state.transfer_balance(&address, refund_address, &balance); self.substate.suicides.insert(address); } From 996db7cd29db5d641faaf979cd57ce846c0e3273 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 27 Jan 2016 13:11:09 +0100 Subject: [PATCH 14/19] uncommented client tick --- src/client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index 904a4e02d..50c4e3f81 100644 --- a/src/client.rs +++ b/src/client.rs @@ -294,7 +294,7 @@ impl Client { /// Tick the client. pub fn tick(&self) { - //self.chain.read().unwrap().collect_garbage(false); + self.chain.read().unwrap().collect_garbage(false); } } From 500dd1480dbcd38abac150f0d03e116064c8cca2 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 27 Jan 2016 13:59:14 +0100 Subject: [PATCH 15/19] temporarily comment out checking zero prefixed int --- util/src/bytes.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/util/src/bytes.rs b/util/src/bytes.rs index 3144dd482..b8d440345 100644 --- a/util/src/bytes.rs +++ b/util/src/bytes.rs @@ -274,8 +274,8 @@ pub enum FromBytesError { DataIsTooShort, /// TODO [debris] Please document me DataIsTooLong, - /// Integer-representation is non-canonically prefixed with zero byte(s). - ZeroPrefixedInt, + // Integer-representation is non-canonically prefixed with zero byte(s). + //ZeroPrefixedInt, } impl StdError for FromBytesError { @@ -312,9 +312,9 @@ macro_rules! impl_uint_from_bytes { match bytes.len() { 0 => Ok(0), l if l <= mem::size_of::<$to>() => { - if bytes[0] == 0 { - return Err(FromBytesError::ZeroPrefixedInt) - } + //if bytes[0] == 0 { + //return Err(FromBytesError::ZeroPrefixedInt) + //} let mut res = 0 as $to; for i in 0..l { let shift = (l - 1 - i) * 8; @@ -349,9 +349,10 @@ macro_rules! impl_uint_from_bytes { ($name: ident) => { impl FromBytes for $name { fn from_bytes(bytes: &[u8]) -> FromBytesResult<$name> { - if !bytes.is_empty() && bytes[0] == 0 { - Err(FromBytesError::ZeroPrefixedInt) - } else if bytes.len() <= $name::SIZE { + //if !bytes.is_empty() && bytes[0] == 0 { + //Err(FromBytesError::ZeroPrefixedInt) + //} else + if bytes.len() <= $name::SIZE { Ok($name::from(bytes)) } else { Err(FromBytesError::DataIsTooLong) From 322c1a6cb27ab9a9c2a691e84b1f96a8123b831a Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 27 Jan 2016 14:25:12 +0100 Subject: [PATCH 16/19] use jsonrpc 1.1, moved params deserialization to jsonrpc-core --- rpc/Cargo.toml | 4 +-- rpc/src/impls/eth.rs | 66 ++++++++++++++++++++---------------------- rpc/src/types/block.rs | 10 ------- rpc/src/types/mod.rs | 15 ---------- 4 files changed, 33 insertions(+), 62 deletions(-) diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index ccdd46679..1f10180d6 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -11,8 +11,8 @@ authors = ["Marek Kotewicz , @@ -30,7 +30,7 @@ impl Eth for EthClient { fn author(&self, params: Params) -> Result { match params { - Params::None => Ok(to_value(&Address::new())), + Params::None => to_value(&Address::new()), _ => Err(Error::invalid_params()) } } @@ -68,39 +68,35 @@ impl Eth for EthClient { } fn block(&self, params: Params) -> Result { - if let Params::Array(ref arr) = params { - if let [ref h, Value::Bool(ref _include_txs)] = arr as &[Value] { - if let Ok(hash) = from_value::(h.clone()) { - return match (self.client.block_header(&hash), self.client.block_details(&hash)) { - (Some(bytes), Some(details)) => { - let view = HeaderView::new(&bytes); - let block = Block { - hash: view.sha3(), - parent_hash: view.parent_hash(), - uncles_hash: view.uncles_hash(), - author: view.author(), - miner: view.author(), - state_root: view.state_root(), - transactions_root: view.transactions_root(), - receipts_root: view.receipts_root(), - number: U256::from(view.number()), - gas_used: view.gas_used(), - gas_limit: view.gas_limit(), - logs_bloom: view.log_bloom(), - timestamp: U256::from(view.timestamp()), - difficulty: view.difficulty(), - total_difficulty: details.total_difficulty, - uncles: vec![], - transactions: vec![] - }; - Ok(to_value(&block)) - }, - _ => Ok(Value::Null), - } - } - } + match from_params::<(H256, bool)>(params) { + Ok((hash, _include_txs)) => match (self.client.block_header(&hash), self.client.block_details(&hash)) { + (Some(bytes), Some(details)) => { + let view = HeaderView::new(&bytes); + let block = Block { + hash: view.sha3(), + parent_hash: view.parent_hash(), + uncles_hash: view.uncles_hash(), + author: view.author(), + miner: view.author(), + state_root: view.state_root(), + transactions_root: view.transactions_root(), + receipts_root: view.receipts_root(), + number: U256::from(view.number()), + gas_used: view.gas_used(), + gas_limit: view.gas_limit(), + logs_bloom: view.log_bloom(), + timestamp: U256::from(view.timestamp()), + difficulty: view.difficulty(), + total_difficulty: details.total_difficulty, + uncles: vec![], + transactions: vec![] + }; + to_value(&block) + }, + _ => Ok(Value::Null) + }, + Err(err) => Err(err) } - Err(Error::invalid_params()) } } @@ -126,6 +122,6 @@ impl EthFilter for EthFilterClient { } fn filter_changes(&self, _: Params) -> Result { - Ok(Value::Array(vec![to_value(&self.client.chain_info().best_block_hash)])) + to_value(&self.client.chain_info().best_block_hash).map(|v| Value::Array(vec![v])) } } diff --git a/rpc/src/types/block.rs b/rpc/src/types/block.rs index ac1e673a5..740cf3e09 100644 --- a/rpc/src/types/block.rs +++ b/rpc/src/types/block.rs @@ -34,13 +34,3 @@ pub struct Block { pub uncles: Vec, pub transactions: Vec } - -#[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); -} diff --git a/rpc/src/types/mod.rs b/rpc/src/types/mod.rs index 0b7d97916..7be32e84d 100644 --- a/rpc/src/types/mod.rs +++ b/rpc/src/types/mod.rs @@ -1,18 +1,3 @@ -use serde::{Serialize, Deserialize, de}; -use serde_json::value::{Value, Serializer, Deserializer}; - mod block; -pub fn to_value(s: &S) -> Value where S: Serialize { - let mut serializer = Serializer::new(); - // should never panic! - s.serialize(&mut serializer).unwrap(); - serializer.unwrap() -} - -pub fn from_value(value: Value) -> Result::Error> where D: Deserialize { - let mut deserialier = Deserializer::new(value); - Deserialize::deserialize(&mut deserialier) -} - pub use self::block::Block; From e068bad4e0488bf090c3590cbe1a171565af627e Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 27 Jan 2016 14:31:43 +0100 Subject: [PATCH 17/19] Revert "temporarily comment out checking zero prefixed int" This reverts commit 500dd1480dbcd38abac150f0d03e116064c8cca2. --- util/src/bytes.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/util/src/bytes.rs b/util/src/bytes.rs index b8d440345..3144dd482 100644 --- a/util/src/bytes.rs +++ b/util/src/bytes.rs @@ -274,8 +274,8 @@ pub enum FromBytesError { DataIsTooShort, /// TODO [debris] Please document me DataIsTooLong, - // Integer-representation is non-canonically prefixed with zero byte(s). - //ZeroPrefixedInt, + /// Integer-representation is non-canonically prefixed with zero byte(s). + ZeroPrefixedInt, } impl StdError for FromBytesError { @@ -312,9 +312,9 @@ macro_rules! impl_uint_from_bytes { match bytes.len() { 0 => Ok(0), l if l <= mem::size_of::<$to>() => { - //if bytes[0] == 0 { - //return Err(FromBytesError::ZeroPrefixedInt) - //} + if bytes[0] == 0 { + return Err(FromBytesError::ZeroPrefixedInt) + } let mut res = 0 as $to; for i in 0..l { let shift = (l - 1 - i) * 8; @@ -349,10 +349,9 @@ macro_rules! impl_uint_from_bytes { ($name: ident) => { impl FromBytes for $name { fn from_bytes(bytes: &[u8]) -> FromBytesResult<$name> { - //if !bytes.is_empty() && bytes[0] == 0 { - //Err(FromBytesError::ZeroPrefixedInt) - //} else - if bytes.len() <= $name::SIZE { + if !bytes.is_empty() && bytes[0] == 0 { + Err(FromBytesError::ZeroPrefixedInt) + } else if bytes.len() <= $name::SIZE { Ok($name::from(bytes)) } else { Err(FromBytesError::DataIsTooLong) From 1402fd5c4c455e95ad0d1ed87fa3f1bf2f028454 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 27 Jan 2016 14:32:10 +0100 Subject: [PATCH 18/19] updated eth filter comment --- rpc/src/traits/eth.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpc/src/traits/eth.rs b/rpc/src/traits/eth.rs index 35b59a91c..63aadbc74 100644 --- a/rpc/src/traits/eth.rs +++ b/rpc/src/traits/eth.rs @@ -44,7 +44,7 @@ pub trait Eth: Sized + Send + Sync + 'static { } } -// TODO: do filters api properly if we commit outselves to polling again... +// TODO: do filters api properly pub trait EthFilter: Sized + Send + Sync + 'static { /// Returns id of new block filter fn new_block_filter(&self, _: Params) -> Result { rpcerr!() } From 7ffe9344ed730b2b6981dc46608a6e147c0f4f01 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 27 Jan 2016 14:43:43 +0100 Subject: [PATCH 19/19] replaced client block_details with block_total_difficulty --- rpc/src/impls/eth.rs | 6 +++--- src/client.rs | 16 +++++++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/rpc/src/impls/eth.rs b/rpc/src/impls/eth.rs index af71ac0d5..ac27111d6 100644 --- a/rpc/src/impls/eth.rs +++ b/rpc/src/impls/eth.rs @@ -69,8 +69,8 @@ impl Eth for EthClient { fn block(&self, params: Params) -> Result { match from_params::<(H256, bool)>(params) { - Ok((hash, _include_txs)) => match (self.client.block_header(&hash), self.client.block_details(&hash)) { - (Some(bytes), Some(details)) => { + Ok((hash, _include_txs)) => match (self.client.block_header(&hash), self.client.block_total_difficulty(&hash)) { + (Some(bytes), Some(total_difficulty)) => { let view = HeaderView::new(&bytes); let block = Block { hash: view.sha3(), @@ -87,7 +87,7 @@ impl Eth for EthClient { logs_bloom: view.log_bloom(), timestamp: U256::from(view.timestamp()), difficulty: view.difficulty(), - total_difficulty: details.total_difficulty, + total_difficulty: total_difficulty, uncles: vec![], transactions: vec![] }; diff --git a/src/client.rs b/src/client.rs index 50c4e3f81..0d0fcae95 100644 --- a/src/client.rs +++ b/src/client.rs @@ -13,7 +13,6 @@ use service::NetSyncMessage; use env_info::LastHashes; use verification::*; use block::*; -use extras::BlockDetails; /// General block status #[derive(Debug)] @@ -67,8 +66,8 @@ pub trait BlockChainClient : Sync + Send { /// Get block status by block header hash. fn block_status(&self, hash: &H256) -> BlockStatus; - /// Get familial details concerning a block. - fn block_details(&self, hash: &H256) -> Option; + /// Get block total difficulty. + fn block_total_difficulty(&self, hash: &H256) -> Option; /// Get raw block header data by block number. fn block_header_at(&self, n: BlockNumber) -> Option; @@ -83,6 +82,9 @@ pub trait BlockChainClient : Sync + Send { /// Get block status by block number. fn block_status_at(&self, n: BlockNumber) -> BlockStatus; + /// Get block total difficulty. + fn block_total_difficulty_at(&self, n: BlockNumber) -> Option; + /// Get a tree route between `from` and `to`. /// See `BlockChain::tree_route`. fn tree_route(&self, from: &H256, to: &H256) -> Option; @@ -321,8 +323,8 @@ impl BlockChainClient for Client { if self.chain.read().unwrap().is_known(&hash) { BlockStatus::InChain } else { BlockStatus::Unknown } } - fn block_details(&self, hash: &H256) -> Option { - self.chain.read().unwrap().block_details(hash) + fn block_total_difficulty(&self, hash: &H256) -> Option { + self.chain.read().unwrap().block_details(hash).map(|d| d.total_difficulty) } fn block_header_at(&self, n: BlockNumber) -> Option { @@ -344,6 +346,10 @@ impl BlockChainClient for Client { } } + fn block_total_difficulty_at(&self, n: BlockNumber) -> Option { + self.chain.read().unwrap().block_hash(n).and_then(|h| self.block_total_difficulty(&h)) + } + fn tree_route(&self, from: &H256, to: &H256) -> Option { self.chain.read().unwrap().tree_route(from.clone(), to.clone()) }