From 82373ab7a412e45eea373f52d3035e4ffcdbe860 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 20 Jan 2016 04:19:38 +0100 Subject: [PATCH 01/41] 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/41] 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/41] 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/41] 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/41] 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 b2050fa639f580a2fdff56579ab28aecc417c5bd Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Mon, 25 Jan 2016 16:23:05 +0400 Subject: [PATCH 06/41] untrusted rlp data length check --- util/src/rlp/rlperrors.rs | 2 ++ util/src/rlp/tests.rs | 13 ++++++++++++- util/src/rlp/untrusted_rlp.rs | 7 ++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/util/src/rlp/rlperrors.rs b/util/src/rlp/rlperrors.rs index 97adbced1..18ca928ec 100644 --- a/util/src/rlp/rlperrors.rs +++ b/util/src/rlp/rlperrors.rs @@ -21,6 +21,8 @@ pub enum DecoderError { RlpListLenWithZeroPrefix, /// TODO [debris] Please document me RlpInvalidIndirection, + /// Returned when declared length is inconsistent with data specified after + RlpInconsistentLengthAndData } impl StdError for DecoderError { diff --git a/util/src/rlp/tests.rs b/util/src/rlp/tests.rs index f33cec177..e08901d67 100644 --- a/util/src/rlp/tests.rs +++ b/util/src/rlp/tests.rs @@ -4,7 +4,7 @@ use self::json_tests::rlp as rlptest; use std::{fmt, cmp}; use std::str::FromStr; use rlp; -use rlp::{UntrustedRlp, RlpStream, View, Stream}; +use rlp::{UntrustedRlp, RlpStream, View, Stream, DecoderError}; use uint::U256; #[test] @@ -351,3 +351,14 @@ fn test_decoding_array() { assert_eq!(arr[0], 5); assert_eq!(arr[1], 2); } + +#[test] +fn test_rlp_data_length_check() +{ + let data = vec![0x84, b'c', b'a', b't']; + let rlp = UntrustedRlp::new(&data); + + let as_val: Result = rlp.as_val(); + assert_eq!(Err(DecoderError::RlpInconsistentLengthAndData), as_val); +} + diff --git a/util/src/rlp/untrusted_rlp.rs b/util/src/rlp/untrusted_rlp.rs index 768d058c1..58ab2de60 100644 --- a/util/src/rlp/untrusted_rlp.rs +++ b/util/src/rlp/untrusted_rlp.rs @@ -331,10 +331,15 @@ impl<'a> Decoder for BasicDecoder<'a> { Some(l @ 0...0x7f) => Ok(try!(f(&[l]))), // 0-55 bytes Some(l @ 0x80...0xb7) => { - let d = &bytes[1..(1 + l as usize - 0x80)]; + let last_index_of = 1 + l as usize - 0x80; + if bytes.len() < last_index_of { + return Err(DecoderError::RlpInconsistentLengthAndData); + } + let d = &bytes[1..last_index_of]; if l == 0x81 && d[0] < 0x80 { return Err(DecoderError::RlpInvalidIndirection); } + Ok(try!(f(d))) }, // longer than 55 bytes From e61d1f810ee6d3b87c2df91521272f6197a144d0 Mon Sep 17 00:00:00 2001 From: arkpar Date: Mon, 25 Jan 2016 13:39:15 +0100 Subject: [PATCH 07/41] U256<->H256 conversion --- util/src/hash.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/util/src/hash.rs b/util/src/hash.rs index 252877a24..0e4139f3c 100644 --- a/util/src/hash.rs +++ b/util/src/hash.rs @@ -469,6 +469,18 @@ impl<'_> From<&'_ U256> for H256 { } } +impl From for U256 { + fn from(value: H256) -> U256 { + U256::from(value.bytes()) + } +} + +impl<'_> From<&'_ H256> for U256 { + fn from(value: &'_ H256) -> U256 { + U256::from(value.bytes()) + } +} + impl From for Address { fn from(value: H256) -> Address { unsafe { @@ -562,6 +574,7 @@ pub static ZERO_H256: H256 = H256([0x00; 32]); #[cfg(test)] mod tests { use hash::*; + use uint::*; use std::str::FromStr; #[test] @@ -635,5 +648,18 @@ mod tests { // too short. assert_eq!(H64::from(0), H64::from("0x34567890abcdef")); } + + #[test] + fn from_and_to_u256() { + let u: U256 = x!(0x123456789abcdef0u64); + let h = H256::from(u); + assert_eq!(H256::from(u), H256::from("000000000000000000000000000000000000000000000000123456789abcdef0")); + let h_ref = H256::from(&u); + assert_eq!(h, h_ref); + let r_ref: U256 = From::from(&h); + assert_eq!(r_ref, u); + let r: U256 = From::from(h); + assert_eq!(r, u); + } } From 45059d5119691d11fa155019f8cb02ce140e2af6 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Mon, 25 Jan 2016 16:40:59 +0400 Subject: [PATCH 08/41] untrusted rlp data length check --- .gitignore | 3 ++- util/src/rlp/tests.rs | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index e3f377db9..2cdc945d3 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,5 @@ Cargo.lock /json-tests/target/ - +# jetbrains ide stuff +.idea \ No newline at end of file diff --git a/util/src/rlp/tests.rs b/util/src/rlp/tests.rs index e08901d67..e86cb8ae6 100644 --- a/util/src/rlp/tests.rs +++ b/util/src/rlp/tests.rs @@ -360,5 +360,4 @@ fn test_rlp_data_length_check() let as_val: Result = rlp.as_val(); assert_eq!(Err(DecoderError::RlpInconsistentLengthAndData), as_val); -} - +} \ No newline at end of file From 48d1760b7f598c806d6169891eee30fbf13c94d0 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Mon, 25 Jan 2016 16:54:52 +0400 Subject: [PATCH 09/41] lost spaces --- util/src/rlp/untrusted_rlp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/src/rlp/untrusted_rlp.rs b/util/src/rlp/untrusted_rlp.rs index 58ab2de60..c0cc89df5 100644 --- a/util/src/rlp/untrusted_rlp.rs +++ b/util/src/rlp/untrusted_rlp.rs @@ -333,7 +333,7 @@ impl<'a> Decoder for BasicDecoder<'a> { Some(l @ 0x80...0xb7) => { let last_index_of = 1 + l as usize - 0x80; if bytes.len() < last_index_of { - return Err(DecoderError::RlpInconsistentLengthAndData); + return Err(DecoderError::RlpInconsistentLengthAndData); } let d = &bytes[1..last_index_of]; if l == 0x81 && d[0] < 0x80 { From e592a185ef02e7ccdef1efe062f5a85ac730479a Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Mon, 25 Jan 2016 16:57:41 +0400 Subject: [PATCH 10/41] lost spaces again --- util/src/rlp/untrusted_rlp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/src/rlp/untrusted_rlp.rs b/util/src/rlp/untrusted_rlp.rs index c0cc89df5..7f32ff369 100644 --- a/util/src/rlp/untrusted_rlp.rs +++ b/util/src/rlp/untrusted_rlp.rs @@ -333,7 +333,7 @@ impl<'a> Decoder for BasicDecoder<'a> { Some(l @ 0x80...0xb7) => { let last_index_of = 1 + l as usize - 0x80; if bytes.len() < last_index_of { - return Err(DecoderError::RlpInconsistentLengthAndData); + return Err(DecoderError::RlpInconsistentLengthAndData); } let d = &bytes[1..last_index_of]; if l == 0x81 && d[0] < 0x80 { From 01ea703783a69575ae40e7abf8fbbf9ad3f36a4b Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Mon, 25 Jan 2016 17:27:11 +0400 Subject: [PATCH 11/41] long length checks & indentation --- util/src/rlp/tests.rs | 14 ++++++++++++++ util/src/rlp/untrusted_rlp.rs | 12 ++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/util/src/rlp/tests.rs b/util/src/rlp/tests.rs index e86cb8ae6..737e3ce28 100644 --- a/util/src/rlp/tests.rs +++ b/util/src/rlp/tests.rs @@ -358,6 +358,20 @@ fn test_rlp_data_length_check() let data = vec![0x84, b'c', b'a', b't']; let rlp = UntrustedRlp::new(&data); + let as_val: Result = rlp.as_val(); + assert_eq!(Err(DecoderError::RlpInconsistentLengthAndData), as_val); +} + +#[test] +fn test_rlp_long_data_length_check() +{ + let mut data: Vec = vec![0xb8, 255]; + for _ in 0..253 { + data.push(b'c'); + } + + let rlp = UntrustedRlp::new(&data); + let as_val: Result = rlp.as_val(); assert_eq!(Err(DecoderError::RlpInconsistentLengthAndData), as_val); } \ No newline at end of file diff --git a/util/src/rlp/untrusted_rlp.rs b/util/src/rlp/untrusted_rlp.rs index 7f32ff369..af0fd6ff7 100644 --- a/util/src/rlp/untrusted_rlp.rs +++ b/util/src/rlp/untrusted_rlp.rs @@ -333,7 +333,7 @@ impl<'a> Decoder for BasicDecoder<'a> { Some(l @ 0x80...0xb7) => { let last_index_of = 1 + l as usize - 0x80; if bytes.len() < last_index_of { - return Err(DecoderError::RlpInconsistentLengthAndData); + return Err(DecoderError::RlpInconsistentLengthAndData); } let d = &bytes[1..last_index_of]; if l == 0x81 && d[0] < 0x80 { @@ -346,8 +346,16 @@ impl<'a> Decoder for BasicDecoder<'a> { Some(l @ 0xb8...0xbf) => { let len_of_len = l as usize - 0xb7; let begin_of_value = 1 as usize + len_of_len; + if bytes.len() < begin_of_value { + return Err(DecoderError::RlpInconsistentLengthAndData); + } let len = try!(usize::from_bytes(&bytes[1..begin_of_value])); - Ok(try!(f(&bytes[begin_of_value..begin_of_value + len]))) + + let last_index_of_value = begin_of_value + len; + if bytes.len() < last_index_of_value { + return Err(DecoderError::RlpInconsistentLengthAndData); + } + Ok(try!(f(&bytes[begin_of_value..last_index_of_value]))) } // we are reading value, not a list! _ => Err(DecoderError::RlpExpectedToBeData) From f4ab64fd6bcc7f045f52313dcba3f567013daa0f Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Mon, 25 Jan 2016 18:47:13 +0400 Subject: [PATCH 12/41] happy path test --- util/src/rlp/tests.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/util/src/rlp/tests.rs b/util/src/rlp/tests.rs index 737e3ce28..d522fa80b 100644 --- a/util/src/rlp/tests.rs +++ b/util/src/rlp/tests.rs @@ -374,4 +374,18 @@ fn test_rlp_long_data_length_check() let as_val: Result = rlp.as_val(); assert_eq!(Err(DecoderError::RlpInconsistentLengthAndData), as_val); +} + +#[test] +fn test_the_exact_long_string() +{ + let mut data: Vec = vec![0xb8, 255]; + for _ in 0..255 { + data.push(b'c'); + } + + let rlp = UntrustedRlp::new(&data); + + let as_val: Result = rlp.as_val(); + assert!(as_val.is_ok()); } \ No newline at end of file From 95e96a653f588caa4965c0951168be303b16228b Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Mon, 25 Jan 2016 19:13:36 +0400 Subject: [PATCH 13/41] 2 bytes length check --- util/src/rlp/tests.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/util/src/rlp/tests.rs b/util/src/rlp/tests.rs index d522fa80b..84c00e633 100644 --- a/util/src/rlp/tests.rs +++ b/util/src/rlp/tests.rs @@ -388,4 +388,19 @@ fn test_the_exact_long_string() let as_val: Result = rlp.as_val(); assert!(as_val.is_ok()); -} \ No newline at end of file +} + +#[test] +fn test_rlp_2bytes_data_length_check() +{ + let mut data: Vec = vec![0xb9, 2, 255]; // 512+255 + for _ in 0..700 { + data.push(b'c'); + } + + let rlp = UntrustedRlp::new(&data); + + let as_val: Result = rlp.as_val(); + assert_eq!(Err(DecoderError::RlpInconsistentLengthAndData), as_val); +} + From f38b736c91499da04fbe6b44345181bf89ed63d8 Mon Sep 17 00:00:00 2001 From: debris Date: Mon, 25 Jan 2016 17:45:26 +0100 Subject: [PATCH 14/41] 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 15/41] 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 280749b34cfdce8c110cf93f52aaa677f57c3d72 Mon Sep 17 00:00:00 2001 From: Tomusdrw Date: Tue, 26 Jan 2016 09:13:44 +0100 Subject: [PATCH 16/41] Removing println --- src/executive.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/executive.rs b/src/executive.rs index f5952e530..af738b0f6 100644 --- a/src/executive.rs +++ b/src/executive.rs @@ -166,7 +166,6 @@ impl<'a> Executive<'a> { /// Modifies the substate and the output. /// Returns either gas_left or `evm::Error`. pub fn call(&mut self, params: ActionParams, substate: &mut Substate, mut output: BytesRef) -> evm::Result { - println!("Calling executive. Sender: {}", params.sender); // backup used in case of running out of gas let backup = self.state.clone(); From c66aa52166ab04f51ca90ded67574070e9357622 Mon Sep 17 00:00:00 2001 From: Tomusdrw Date: Tue, 26 Jan 2016 10:15:55 +0100 Subject: [PATCH 17/41] Spawning new thread when we are reaching stack limit --- Cargo.toml | 1 + src/evm/jit.rs | 19 ++++++++++++------- src/evm/tests.rs | 1 + src/executive.rs | 36 +++++++++++++++++++++++++++++------- src/lib.rs | 1 + src/tests/executive.rs | 4 ++-- src/tests/state.rs | 10 +++++----- 7 files changed, 51 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 54b1b406e..ce49d0dd4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ num_cpus = "0.2" docopt = "0.6" docopt_macros = "0.6" ctrlc = "1.0" +crossbeam = "0.1.5" clippy = "0.0.37" [features] diff --git a/src/evm/jit.rs b/src/evm/jit.rs index 9f990155d..e073a380d 100644 --- a/src/evm/jit.rs +++ b/src/evm/jit.rs @@ -64,7 +64,7 @@ impl IntoJit for H256 { for i in 0..self.bytes().len() { let rev = self.bytes().len() - 1 - i; let pos = rev / 8; - ret[pos] += (self.bytes()[i] as u64) << (rev % 8) * 8; + ret[pos] += (self.bytes()[i] as u64) << ((rev % 8) * 8); } evmjit::I256 { words: ret } } @@ -218,9 +218,11 @@ impl<'a> evmjit::Ext for ExtAdapter<'a> { } } - match self.ext.call(&call_gas, + match self.ext.call( + &call_gas, + &self.address, &receive_address, - &value, + Some(value), unsafe { slice::from_raw_parts(in_beg, in_size as usize) }, &code_address, unsafe { slice::from_raw_parts_mut(out_beg, out_size as usize) }) { @@ -262,7 +264,7 @@ impl<'a> evmjit::Ext for ExtAdapter<'a> { } let bytes_ref: &[u8] = slice::from_raw_parts(beg, size as usize); - self.ext.log(topics, bytes_ref.to_vec()); + self.ext.log(topics, bytes_ref); } } @@ -287,8 +289,8 @@ impl evm::Evm for JitEvm { assert!(params.gas <= U256::from(i64::max_value() as u64), "evmjit max gas is 2 ^ 63"); assert!(params.gas_price <= U256::from(i64::max_value() as u64), "evmjit max gas is 2 ^ 63"); - let call_data = params.data.unwrap_or(vec![]); - let code = params.code.unwrap_or(vec![]); + let call_data = params.data.unwrap_or_else(Vec::new); + let code = params.code.unwrap_or_else(Vec::new); let mut data = evmjit::RuntimeDataHandle::new(); data.gas = params.gas.low_u64() as i64; @@ -303,7 +305,10 @@ impl evm::Evm for JitEvm { data.address = params.address.into_jit(); data.caller = params.sender.into_jit(); data.origin = params.origin.into_jit(); - data.call_value = params.value.into_jit(); + data.call_value = match params.value { + ActionValue::Transfer(val) => val.into_jit(), + ActionValue::Apparent(val) => val.into_jit() + }; data.author = ext.env_info().author.clone().into_jit(); data.difficulty = ext.env_info().difficulty.into_jit(); diff --git a/src/evm/tests.rs b/src/evm/tests.rs index ad81cf877..d448ccb3a 100644 --- a/src/evm/tests.rs +++ b/src/evm/tests.rs @@ -215,6 +215,7 @@ fn test_origin(factory: super::Factory) { assert_eq!(ext.store.get(&H256::new()).unwrap(), &H256::from_str("000000000000000000000000cd1722f2947def4cf144679da39c4c32bdc35681").unwrap()); } +// TODO [todr] Fails with Signal 11 on JIT evm_test!{test_sender: test_sender_jit, test_sender_int} fn test_sender(factory: super::Factory) { let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap(); diff --git a/src/executive.rs b/src/executive.rs index f5952e530..d5446b0e9 100644 --- a/src/executive.rs +++ b/src/executive.rs @@ -5,6 +5,12 @@ use engine::*; use evm::{self, Ext}; use externalities::*; use substate::*; +use crossbeam; + +/// Max depth to avoid stack overflow (when it's reached we start a new thread with VM) +/// TODO [todr] We probably need some more sophisticated calculations here (limit on my machine 132) +/// Maybe something like here: https://github.com/ethereum/libethereum/blob/4db169b8504f2b87f7d5a481819cfb959fc65f6c/libethereum/ExtVM.cpp +const MAX_VM_DEPTH_FOR_THREAD: usize = 128; /// Returns new address created from address and given nonce. pub fn contract_address(address: &Address, nonce: &U256) -> Address { @@ -161,12 +167,32 @@ impl<'a> Executive<'a> { Ok(try!(self.finalize(t, substate, res))) } + fn exec_vm(&mut self, params: ActionParams, unconfirmed_substate: &mut Substate, output_policy: OutputPolicy) -> evm::Result { + // Ordinary execution - keep VM in same thread + if (self.depth + 1) % MAX_VM_DEPTH_FOR_THREAD != 0 { + let mut ext = self.as_externalities(OriginInfo::from(¶ms), unconfirmed_substate, output_policy); + let vm_factory = self.engine.vm_factory(); + return vm_factory.create().exec(params, &mut ext); + } + + // Start in new thread to reset stack + // TODO [todr] No thread builder yet, so we need to reset once for a while + // https://github.com/aturon/crossbeam/issues/16 + crossbeam::scope(|scope| { + let mut ext = self.as_externalities(OriginInfo::from(¶ms), unconfirmed_substate, output_policy); + let vm_factory = self.engine.vm_factory(); + + scope.spawn(move || { + vm_factory.create().exec(params, &mut ext) + }) + }).join() + } + /// Calls contract function with given contract params. /// NOTE. It does not finalize the transaction (doesn't do refunds, nor suicides). /// Modifies the substate and the output. /// Returns either gas_left or `evm::Error`. pub fn call(&mut self, params: ActionParams, substate: &mut Substate, mut output: BytesRef) -> evm::Result { - println!("Calling executive. Sender: {}", params.sender); // backup used in case of running out of gas let backup = self.state.clone(); @@ -201,8 +227,7 @@ impl<'a> Executive<'a> { let mut unconfirmed_substate = Substate::new(); let res = { - let mut ext = self.as_externalities(OriginInfo::from(¶ms), &mut unconfirmed_substate, OutputPolicy::Return(output)); - self.engine.vm_factory().create().exec(params, &mut ext) + self.exec_vm(params, &mut unconfirmed_substate, OutputPolicy::Return(output)) }; trace!("exec: sstore-clears={}\n", unconfirmed_substate.sstore_clears_count); @@ -235,8 +260,7 @@ impl<'a> Executive<'a> { } let res = { - let mut ext = self.as_externalities(OriginInfo::from(¶ms), &mut unconfirmed_substate, OutputPolicy::InitContract); - self.engine.vm_factory().create().exec(params, &mut ext) + self.exec_vm(params, &mut unconfirmed_substate, OutputPolicy::InitContract) }; self.enact_result(&res, substate, unconfirmed_substate, backup); res @@ -277,7 +301,6 @@ impl<'a> Executive<'a> { match result { Err(evm::Error::Internal) => Err(ExecutionError::Internal), - // TODO [ToDr] BadJumpDestination @debris - how to handle that? Err(_) => { Ok(Executed { gas: t.gas, @@ -302,7 +325,6 @@ impl<'a> Executive<'a> { } fn enact_result(&mut self, result: &evm::Result, substate: &mut Substate, un_substate: Substate, backup: State) { - // TODO: handle other evm::Errors same as OutOfGas once they are implemented match *result { Err(evm::Error::OutOfGas) | Err(evm::Error::BadJumpDestination {..}) diff --git a/src/lib.rs b/src/lib.rs index 262f8682f..8dd02b3bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -90,6 +90,7 @@ extern crate num_cpus; extern crate evmjit; #[macro_use] extern crate ethcore_util as util; +extern crate crossbeam; // NOTE: Add doc parser exception for these pub declarations. diff --git a/src/tests/executive.rs b/src/tests/executive.rs index a94fd1605..1df1b7eec 100644 --- a/src/tests/executive.rs +++ b/src/tests/executive.rs @@ -271,8 +271,8 @@ fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec { declare_test!{ExecutiveTests_vmArithmeticTest, "VMTests/vmArithmeticTest"} declare_test!{ExecutiveTests_vmBitwiseLogicOperationTest, "VMTests/vmBitwiseLogicOperationTest"} -// this one crashes with some vm internal error. Separately they pass. -declare_test!{ignore => ExecutiveTests_vmBlockInfoTest, "VMTests/vmBlockInfoTest"} +declare_test!{ExecutiveTests_vmBlockInfoTest, "VMTests/vmBlockInfoTest"} + // TODO [todr] Fails with Signal 11 when using JIT declare_test!{ExecutiveTests_vmEnvironmentalInfoTest, "VMTests/vmEnvironmentalInfoTest"} declare_test!{ExecutiveTests_vmIOandFlowOperationsTest, "VMTests/vmIOandFlowOperationsTest"} declare_test!{heavy => ExecutiveTests_vmInputLimits, "VMTests/vmInputLimits"} diff --git a/src/tests/state.rs b/src/tests/state.rs index 75b9b2493..325a8b646 100644 --- a/src/tests/state.rs +++ b/src/tests/state.rs @@ -73,7 +73,7 @@ fn do_json_test(json_data: &[u8]) -> Vec { declare_test!{StateTests_stBlockHashTest, "StateTests/stBlockHashTest"} declare_test!{StateTests_stCallCodes, "StateTests/stCallCodes"} -declare_test!{ignore => StateTests_stCallCreateCallCodeTest, "StateTests/stCallCreateCallCodeTest"} //<< Out of stack +declare_test!{StateTests_stCallCreateCallCodeTest, "StateTests/stCallCreateCallCodeTest"} declare_test!{StateTests_stDelegatecallTest, "StateTests/stDelegatecallTest"} declare_test!{StateTests_stExample, "StateTests/stExample"} declare_test!{StateTests_stInitCodeTest, "StateTests/stInitCodeTest"} @@ -81,12 +81,12 @@ declare_test!{StateTests_stLogTests, "StateTests/stLogTests"} declare_test!{heavy => StateTests_stMemoryStressTest, "StateTests/stMemoryStressTest"} declare_test!{heavy => StateTests_stMemoryTest, "StateTests/stMemoryTest"} declare_test!{StateTests_stPreCompiledContracts, "StateTests/stPreCompiledContracts"} -declare_test!{heavy => StateTests_stQuadraticComplexityTest, "StateTests/stQuadraticComplexityTest"} //<< Too long -declare_test!{ignore => StateTests_stRecursiveCreate, "StateTests/stRecursiveCreate"} //<< Out of stack +declare_test!{heavy => StateTests_stQuadraticComplexityTest, "StateTests/stQuadraticComplexityTest"} +declare_test!{StateTests_stRecursiveCreate, "StateTests/stRecursiveCreate"} declare_test!{StateTests_stRefundTest, "StateTests/stRefundTest"} declare_test!{StateTests_stSolidityTest, "StateTests/stSolidityTest"} -declare_test!{ignore => StateTests_stSpecialTest, "StateTests/stSpecialTest"} //<< Out of Stack -declare_test!{ignore => StateTests_stSystemOperationsTest, "StateTests/stSystemOperationsTest"} //<< Out of stack +declare_test!{StateTests_stSpecialTest, "StateTests/stSpecialTest"} +declare_test!{StateTests_stSystemOperationsTest, "StateTests/stSystemOperationsTest"} declare_test!{StateTests_stTransactionTest, "StateTests/stTransactionTest"} declare_test!{StateTests_stTransitionTest, "StateTests/stTransitionTest"} declare_test!{StateTests_stWalletTest, "StateTests/stWalletTest"} From e9bd52b34a579a4ebefc9b4caaf492db970a521a Mon Sep 17 00:00:00 2001 From: debris Date: Tue, 26 Jan 2016 10:26:18 +0100 Subject: [PATCH 18/41] fixed failing tests --- src/sync/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sync/mod.rs b/src/sync/mod.rs index 78a5d7613..34a1e429d 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs @@ -9,13 +9,13 @@ /// extern crate ethcore; /// use std::env; /// use std::sync::Arc; -/// use util::network::NetworkService; +/// use util::network::{NetworkService, NetworkConfiguration}; /// use ethcore::client::Client; /// use ethcore::sync::EthSync; /// use ethcore::ethereum; /// /// fn main() { -/// let mut service = NetworkService::start().unwrap(); +/// let mut service = NetworkService::start(NetworkConfiguration::new()).unwrap(); /// let dir = env::temp_dir(); /// let client = Client::new(ethereum::new_frontier(), &dir, service.io().channel()).unwrap(); /// EthSync::register(&mut service, client); From adbde5f3dac1abbdd7ba42d41a23038bc140c60c Mon Sep 17 00:00:00 2001 From: Tomusdrw Date: Tue, 26 Jan 2016 10:48:33 +0100 Subject: [PATCH 19/41] Schedule documentation --- src/evm/schedule.rs | 62 ++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/src/evm/schedule.rs b/src/evm/schedule.rs index 70edfceea..d46b7ff11 100644 --- a/src/evm/schedule.rs +++ b/src/evm/schedule.rs @@ -2,67 +2,67 @@ /// Definition of the cost schedule and other parameterisations for the EVM. pub struct Schedule { - /// TODO [Gav Wood] Please document me + /// Does it support exceptional failed code deposit pub exceptional_failed_code_deposit: bool, - /// TODO [Gav Wood] Please document me + /// Does it have a delegate cal pub have_delegate_call: bool, - /// TODO [Tomusdrw] Please document me + /// VM stack limit pub stack_limit: usize, - /// TODO [Gav Wood] Please document me + /// Max number of nested calls/creates pub max_depth: usize, - /// TODO [Gav Wood] Please document me + /// Gas prices for instructions in all tiers pub tier_step_gas: [usize; 8], - /// TODO [Gav Wood] Please document me + /// Gas price for `EXP` opcode pub exp_gas: usize, - /// TODO [Gav Wood] Please document me + /// Additional gas for `EXP` opcode for each byte of exponent pub exp_byte_gas: usize, - /// TODO [Gav Wood] Please document me + /// Gas price for `SHA3` opcode pub sha3_gas: usize, - /// TODO [Gav Wood] Please document me + /// Additional gas for `SHA3` opcode for each word of hashed memory pub sha3_word_gas: usize, - /// TODO [Gav Wood] Please document me + /// Gas price for loading from storage pub sload_gas: usize, - /// TODO [Gav Wood] Please document me + /// Gas price for setting new value to storage (`storage==0`, `new!=0`) pub sstore_set_gas: usize, - /// TODO [Gav Wood] Please document me + /// Gas price for altering value in storage pub sstore_reset_gas: usize, - /// TODO [Gav Wood] Please document me + /// Gas refund for `SSTORE` clearing (when `storage!=0`, `new==0`) pub sstore_refund_gas: usize, - /// TODO [Gav Wood] Please document me + /// Gas price for `JUMPDEST` opcode pub jumpdest_gas: usize, - /// TODO [Gav Wood] Please document me + /// Gas price for `LOG*` pub log_gas: usize, - /// TODO [Gav Wood] Please document me + /// Additional gas for data in `LOG*` pub log_data_gas: usize, - /// TODO [Gav Wood] Please document me + /// Additional gas for each topic in `LOG*` pub log_topic_gas: usize, - /// TODO [Gav Wood] Please document me + /// Gas price for `CREATE` opcode pub create_gas: usize, - /// TODO [Gav Wood] Please document me + /// Gas price for `*CALL*` opcodes pub call_gas: usize, - /// TODO [Gav Wood] Please document me + /// Stipend for transfer for `CALL|CALLCODE` opcode when `value>0` pub call_stipend: usize, - /// TODO [Gav Wood] Please document me + /// Additional gas required for value transfer (`CALL|CALLCODE`) pub call_value_transfer_gas: usize, - /// TODO [Gav Wood] Please document me + /// Additional gas for creating new account (`CALL|CALLCODE`) pub call_new_account_gas: usize, - /// TODO [Gav Wood] Please document me + /// Refund for SUICIDE pub suicide_refund_gas: usize, - /// TODO [Gav Wood] Please document me + /// Gas for used memory pub memory_gas: usize, - /// TODO [Gav Wood] Please document me + /// Coefficient used to convert memory size to gas price for memory pub quad_coeff_div: usize, - /// TODO [Gav Wood] Please document me + /// Cost for contract length when executing `CREATE` pub create_data_gas: usize, - /// TODO [Gav Wood] Please document me + /// Transaction cost pub tx_gas: usize, - /// TODO [Gav Wood] Please document me + /// `CREATE` transaction cost pub tx_create_gas: usize, - /// TODO [Gav Wood] Please document me + /// Additional cost for empty data transaction pub tx_data_zero_gas: usize, - /// TODO [Gav Wood] Please document me + /// Aditional cost for non-empty data transaction pub tx_data_non_zero_gas: usize, - /// TODO [Gav Wood] Please document me + /// Gas price for copying memory pub copy_gas: usize, } From b1282fe1f45ffca0dae5848ea684ef53b28f3dc8 Mon Sep 17 00:00:00 2001 From: debris Date: Tue, 26 Jan 2016 11:37:24 +0100 Subject: [PATCH 20/41] 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 800154a8ae66a941c50a5adacccfe94195831e85 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Tue, 26 Jan 2016 14:39:49 +0400 Subject: [PATCH 21/41] client test creating --- src/tests/client.rs | 20 ++++++++++++++++++++ src/tests/mod.rs | 1 + 2 files changed, 21 insertions(+) create mode 100644 src/tests/client.rs diff --git a/src/tests/client.rs b/src/tests/client.rs new file mode 100644 index 000000000..25c8cb9b0 --- /dev/null +++ b/src/tests/client.rs @@ -0,0 +1,20 @@ +use ethereum; +use client::{BlockChainClient,Client}; +use std::env; +use pod_state::*; + + +#[test] +fn test_client_is_created() { + + let mut spec = ethereum::new_frontier_like_test(); + spec.set_genesis_state(PodState::from_json(test.find("pre").unwrap())); + spec.overwrite_genesis(test.find("genesisBlockHeader").unwrap()); + + let mut dir = env::temp_dir(); + dir.push(H32::random().hex()); + + let client_result = Client::new(spec, &dir, IOChannel::disconnected()); + + assert!(client_result.is_ok()); +} \ No newline at end of file diff --git a/src/tests/mod.rs b/src/tests/mod.rs index c30f7f9b8..c6e38dbd0 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -4,3 +4,4 @@ mod test_common; mod transaction; mod executive; mod state; +mod client; \ No newline at end of file From d27c7f3902efb5e48635e2b307457c0e6fa20a63 Mon Sep 17 00:00:00 2001 From: debris Date: Tue, 26 Jan 2016 11:51:35 +0100 Subject: [PATCH 22/41] 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 46e0a81c52dce6905b3361068d8f63e10524f1d2 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Tue, 26 Jan 2016 14:55:12 +0400 Subject: [PATCH 23/41] more of testing --- res/ethereum/tests | 2 +- src/tests/client.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/res/ethereum/tests b/res/ethereum/tests index dc86e6359..e838fd909 160000 --- a/res/ethereum/tests +++ b/res/ethereum/tests @@ -1 +1 @@ -Subproject commit dc86e6359675440aea59ddb48648a01c799925d8 +Subproject commit e838fd90998fc5502d0b7c9427a4c231f9a6953d diff --git a/src/tests/client.rs b/src/tests/client.rs index 25c8cb9b0..c0f202edd 100644 --- a/src/tests/client.rs +++ b/src/tests/client.rs @@ -2,6 +2,7 @@ use ethereum; use client::{BlockChainClient,Client}; use std::env; use pod_state::*; +use super::test_common::*; #[test] From 7b86f98a7ccbc131f3d4311f1fb104f1d4bc4a83 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Tue, 26 Jan 2016 14:57:43 +0400 Subject: [PATCH 24/41] identations fix --- util/src/rlp/untrusted_rlp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/src/rlp/untrusted_rlp.rs b/util/src/rlp/untrusted_rlp.rs index af0fd6ff7..83ee6b87c 100644 --- a/util/src/rlp/untrusted_rlp.rs +++ b/util/src/rlp/untrusted_rlp.rs @@ -331,7 +331,7 @@ impl<'a> Decoder for BasicDecoder<'a> { Some(l @ 0...0x7f) => Ok(try!(f(&[l]))), // 0-55 bytes Some(l @ 0x80...0xb7) => { - let last_index_of = 1 + l as usize - 0x80; + let last_index_of = 1 + l as usize - 0x80; if bytes.len() < last_index_of { return Err(DecoderError::RlpInconsistentLengthAndData); } From 3ac40b68f862f734871bbc7ca19f9dc840689ea0 Mon Sep 17 00:00:00 2001 From: debris Date: Tue, 26 Jan 2016 13:14:22 +0100 Subject: [PATCH 25/41] 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 81bffd48e89be4d4d2745f69f00c006743fdd2ab Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Tue, 26 Jan 2016 17:33:22 +0400 Subject: [PATCH 26/41] basic client tests working --- src/block.rs | 2 +- src/block_queue.rs | 2 +- src/tests/client.rs | 92 ++++++++++++++++++++++++++++++++++++++------- 3 files changed, 80 insertions(+), 16 deletions(-) diff --git a/src/block.rs b/src/block.rs index 1ff326430..0e928e9b0 100644 --- a/src/block.rs +++ b/src/block.rs @@ -85,7 +85,7 @@ impl IsBlock for Block { /// /// It's a bit like a Vec, eccept that whenever a transaction is pushed, we execute it and /// maintain the system `state()`. We also archive execution receipts in preparation for later block creation. -pub struct OpenBlock<'x, 'y> { + pub struct OpenBlock<'x, 'y> { block: Block, engine: &'x Engine, last_hashes: &'y LastHashes, diff --git a/src/block_queue.rs b/src/block_queue.rs index fa091d0c4..c45a86a8b 100644 --- a/src/block_queue.rs +++ b/src/block_queue.rs @@ -191,7 +191,7 @@ impl BlockQueue { /// Wait for queue to be empty pub fn flush(&mut self) { let mut verification = self.verification.lock().unwrap(); - while !verification.unverified.is_empty() && !verification.verifying.is_empty() { + while !verification.unverified.is_empty() || !verification.verifying.is_empty() { verification = self.empty.wait(verification).unwrap(); } } diff --git a/src/tests/client.rs b/src/tests/client.rs index c0f202edd..c14f2e2f0 100644 --- a/src/tests/client.rs +++ b/src/tests/client.rs @@ -1,21 +1,85 @@ -use ethereum; -use client::{BlockChainClient,Client}; + use client::{BlockChainClient,Client}; use std::env; -use pod_state::*; use super::test_common::*; +use std::path::PathBuf; +use spec::*; + +#[cfg(test)] +fn get_random_temp_dir() -> PathBuf { + let mut dir = env::temp_dir(); + dir.push(H32::random().hex()); + dir +} + +#[cfg(test)] +fn get_test_spec() -> Spec { + Spec::new_test() +} + +#[cfg(test)] +fn get_good_dummy_block() -> Bytes { + let mut block_header = Header::new(); + let test_spec = get_test_spec(); + let test_engine = test_spec.to_engine().unwrap(); + block_header.gas_limit = decode(test_engine.spec().engine_params.get("minGasLimit").unwrap()); + block_header.difficulty = decode(test_engine.spec().engine_params.get("minimumDifficulty").unwrap()); + block_header.timestamp = 40; + block_header.number = 1; + block_header.parent_hash = test_engine.spec().genesis_header().hash(); + block_header.state_root = test_engine.spec().genesis_header().state_root; + + create_test_block(&block_header) +} + +#[cfg(test)] +fn create_test_block(header: &Header) -> Bytes { + let mut rlp = RlpStream::new_list(3); + rlp.append(header); + rlp.append_raw(&rlp::EMPTY_LIST_RLP, 1); + rlp.append_raw(&rlp::EMPTY_LIST_RLP, 1); + rlp.out() +} #[test] -fn test_client_is_created() { - - let mut spec = ethereum::new_frontier_like_test(); - spec.set_genesis_state(PodState::from_json(test.find("pre").unwrap())); - spec.overwrite_genesis(test.find("genesisBlockHeader").unwrap()); - - let mut dir = env::temp_dir(); - dir.push(H32::random().hex()); - - let client_result = Client::new(spec, &dir, IOChannel::disconnected()); - +fn created() { + let client_result = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()); assert!(client_result.is_ok()); +} + +#[test] +fn imports_from_empty() { + let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap(); + client.import_verified_blocks(&IoChannel::disconnected()); + client.flush_queue(); +} + +#[test] +fn imports_good_block() { + + ::env_logger::init().ok(); + + let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap(); + + let good_block = get_good_dummy_block(); + if let Err(_) = client.import_block(good_block) { + panic!("error importing block being good by definition"); + } + + client.flush_queue(); + + client.import_verified_blocks(&IoChannel::disconnected()); + + let block = client.block_header_at(1).unwrap(); + + assert!(!block.is_empty()); +} + +#[test] +fn query_none_block() { + let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap(); + + let non_existant = client.block_header_at(188); + + assert!(non_existant == Option::None); } \ No newline at end of file From 8d30f9fecbd91e132ccf368d4b6eb35ed403718f Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Tue, 26 Jan 2016 17:55:08 +0400 Subject: [PATCH 27/41] bad block import checks --- src/tests/client.rs | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/tests/client.rs b/src/tests/client.rs index c14f2e2f0..a9d212bc1 100644 --- a/src/tests/client.rs +++ b/src/tests/client.rs @@ -31,6 +31,21 @@ fn get_good_dummy_block() -> Bytes { create_test_block(&block_header) } +#[cfg(test)] +fn get_bad_state_dummy_block() -> Bytes { + let mut block_header = Header::new(); + let test_spec = get_test_spec(); + let test_engine = test_spec.to_engine().unwrap(); + block_header.gas_limit = decode(test_engine.spec().engine_params.get("minGasLimit").unwrap()); + block_header.difficulty = decode(test_engine.spec().engine_params.get("minimumDifficulty").unwrap()); + block_header.timestamp = 40; + block_header.number = 1; + block_header.parent_hash = test_engine.spec().genesis_header().hash(); + block_header.state_root = x!(0xbad); + + create_test_block(&block_header) +} + #[cfg(test)] fn create_test_block(header: &Header) -> Bytes { let mut rlp = RlpStream::new_list(3); @@ -40,6 +55,23 @@ fn create_test_block(header: &Header) -> Bytes { rlp.out() } +#[cfg(test)] +fn get_test_client_with_blocks(blocks: Vec) -> Arc { + let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap(); + + for block in &blocks { + if let Err(_) = client.import_block(block.clone()) { + panic!("panic importing block which is well-formed"); + } + } + + client.flush_queue(); + + client.import_verified_blocks(&IoChannel::disconnected()); + + client +} + #[test] fn created() { @@ -81,5 +113,13 @@ fn query_none_block() { let non_existant = client.block_header_at(188); - assert!(non_existant == Option::None); + assert!(non_existant.is_none()); +} + +#[test] +fn query_bad_block() { + let client = get_test_client_with_blocks(vec![get_bad_state_dummy_block()]); + let bad_block:Option = client.block_header_at(1); + + assert!(bad_block.is_none()); } \ No newline at end of file From 253f56afe1f5928f76b1e0efcbe75193f74f0021 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Tue, 26 Jan 2016 18:03:12 +0400 Subject: [PATCH 28/41] hack indent --- src/block.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/block.rs b/src/block.rs index 0e928e9b0..31378e8a6 100644 --- a/src/block.rs +++ b/src/block.rs @@ -108,6 +108,7 @@ pub struct SealedBlock { uncle_bytes: Bytes, } + impl<'x, 'y> OpenBlock<'x, 'y> { /// Create a new OpenBlock ready for transaction pushing. pub fn new<'a, 'b>(engine: &'a Engine, db: JournalDB, parent: &Header, last_hashes: &'b LastHashes, author: Address, extra_data: Bytes) -> OpenBlock<'a, 'b> { From b8c2f9cf15c77257384833e29bdeef9ec809dc61 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Tue, 26 Jan 2016 21:05:25 +0400 Subject: [PATCH 29/41] chain info test --- src/tests/client.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/tests/client.rs b/src/tests/client.rs index a9d212bc1..f6641ff91 100644 --- a/src/tests/client.rs +++ b/src/tests/client.rs @@ -1,4 +1,4 @@ - use client::{BlockChainClient,Client}; +use client::{BlockChainClient,Client}; use std::env; use super::test_common::*; use std::path::PathBuf; @@ -88,9 +88,6 @@ fn imports_from_empty() { #[test] fn imports_good_block() { - - ::env_logger::init().ok(); - let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap(); let good_block = get_good_dummy_block(); @@ -122,4 +119,13 @@ fn query_bad_block() { let bad_block:Option = client.block_header_at(1); assert!(bad_block.is_none()); +} + +#[test] +fn returns_chain_info() { + let dummy_block = get_good_dummy_block(); + let client = get_test_client_with_blocks(vec![dummy_block.clone()]); + let block = BlockView::new(&dummy_block); + let info = client.chain_info(); + assert_eq!(info.best_block_hash, block.header().hash()); } \ No newline at end of file From cd57e480f51072f6920d2fe0431989ae531c7144 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Tue, 26 Jan 2016 21:12:25 +0400 Subject: [PATCH 30/41] fix indentation again --- src/block.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/block.rs b/src/block.rs index 31378e8a6..5691d2bf5 100644 --- a/src/block.rs +++ b/src/block.rs @@ -85,7 +85,7 @@ impl IsBlock for Block { /// /// It's a bit like a Vec, eccept that whenever a transaction is pushed, we execute it and /// maintain the system `state()`. We also archive execution receipts in preparation for later block creation. - pub struct OpenBlock<'x, 'y> { +pub struct OpenBlock<'x, 'y> { block: Block, engine: &'x Engine, last_hashes: &'y LastHashes, From f07fc497c5b0516137617d0cf40bc446293e032a Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Tue, 26 Jan 2016 21:14:06 +0400 Subject: [PATCH 31/41] cleanup --- src/block.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/block.rs b/src/block.rs index 5691d2bf5..1ff326430 100644 --- a/src/block.rs +++ b/src/block.rs @@ -108,7 +108,6 @@ pub struct SealedBlock { uncle_bytes: Bytes, } - impl<'x, 'y> OpenBlock<'x, 'y> { /// Create a new OpenBlock ready for transaction pushing. pub fn new<'a, 'b>(engine: &'a Engine, db: JournalDB, parent: &Header, last_hashes: &'b LastHashes, author: Address, extra_data: Bytes) -> OpenBlock<'a, 'b> { From 91828065f45cf1a543218151397f24f4707859d3 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Tue, 26 Jan 2016 22:06:11 +0400 Subject: [PATCH 32/41] fixed line spaces --- src/tests/client.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/tests/client.rs b/src/tests/client.rs index f6641ff91..eb58699b4 100644 --- a/src/tests/client.rs +++ b/src/tests/client.rs @@ -58,17 +58,13 @@ fn create_test_block(header: &Header) -> Bytes { #[cfg(test)] fn get_test_client_with_blocks(blocks: Vec) -> Arc { let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap(); - for block in &blocks { if let Err(_) = client.import_block(block.clone()) { panic!("panic importing block which is well-formed"); } } - client.flush_queue(); - client.import_verified_blocks(&IoChannel::disconnected()); - client } @@ -89,18 +85,14 @@ fn imports_from_empty() { #[test] fn imports_good_block() { let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap(); - let good_block = get_good_dummy_block(); if let Err(_) = client.import_block(good_block) { panic!("error importing block being good by definition"); } - client.flush_queue(); - client.import_verified_blocks(&IoChannel::disconnected()); let block = client.block_header_at(1).unwrap(); - assert!(!block.is_empty()); } @@ -109,7 +101,6 @@ fn query_none_block() { let client = Client::new(get_test_spec(), &get_random_temp_dir(), IoChannel::disconnected()).unwrap(); let non_existant = client.block_header_at(188); - assert!(non_existant.is_none()); } From 2f42e0eda0af6207d3d7d59b007af02256d88806 Mon Sep 17 00:00:00 2001 From: debris Date: Tue, 26 Jan 2016 19:24:33 +0100 Subject: [PATCH 33/41] 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 34/41] 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 35/41] 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 36/41] 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 37/41] 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 38/41] 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 39/41] 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 40/41] 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 41/41] 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()) }