From 432c0d59c465e3ef96031a6f98fa354bd66c614d Mon Sep 17 00:00:00 2001 From: debris Date: Fri, 5 Feb 2016 13:21:34 +0100 Subject: [PATCH] few additional rpc eth methods --- ethcore/src/views.rs | 10 ++++++++ rpc/src/v1/impls/eth.rs | 55 ++++++++++++++++++++++++++++++---------- rpc/src/v1/traits/eth.rs | 4 +++ rpc/src/v1/types/mod.rs | 2 ++ rpc/src/v1/types/sync.rs | 11 ++++++++ 5 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 rpc/src/v1/types/sync.rs diff --git a/ethcore/src/views.rs b/ethcore/src/views.rs index e1c704625..5117309c3 100644 --- a/ethcore/src/views.rs +++ b/ethcore/src/views.rs @@ -139,6 +139,11 @@ impl<'a> BlockView<'a> { self.rlp.val_at(1) } + /// Return number of transactions in given block, without deserializing them. + pub fn transactions_count(&self) -> usize { + self.rlp.at(1).iter().count() + } + /// Return List of transactions in given block. pub fn transaction_views(&self) -> Vec { self.rlp.at(1).iter().map(TransactionView::new_from_rlp).collect() @@ -154,6 +159,11 @@ impl<'a> BlockView<'a> { self.rlp.val_at(2) } + /// Return number of uncles in given block, without deserializing them. + pub fn uncles_count(&self) -> usize { + self.rlp.at(2).iter().count() + } + /// Return List of transactions in given block. pub fn uncle_views(&self) -> Vec { self.rlp.at(2).iter().map(HeaderView::new_from_rlp).collect() diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 46718601b..c1cd327df 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -7,7 +7,7 @@ use util::sha3::*; use ethcore::client::*; use ethcore::views::*; use v1::traits::{Eth, EthFilter}; -use v1::types::Block; +use v1::types::{Block, SyncStatus}; /// Eth rpc implementation. pub struct EthClient { @@ -24,6 +24,7 @@ impl EthClient { } impl Eth for EthClient { + // TODO: do not hardcode protocol version fn protocol_version(&self, params: Params) -> Result { match params { Params::None => Ok(Value::U64(63)), @@ -31,6 +32,15 @@ impl Eth for EthClient { } } + // TODO: do no hardcode default sync status + fn syncing(&self, params: Params) -> Result { + match params { + Params::None => to_value(&SyncStatus::default()), + _ => Err(Error::invalid_params()) + } + } + + // TODO: do not hardcode author. fn author(&self, params: Params) -> Result { match params { Params::None => to_value(&Address::new()), @@ -38,6 +48,23 @@ impl Eth for EthClient { } } + // TODO: return real value of mining once it's implemented. + fn is_mining(&self, params: Params) -> Result { + match params { + Params::None => Ok(Value::Bool(false)), + _ => Err(Error::invalid_params()) + } + } + + // TODO: return real hashrate once we have mining + fn hashrate(&self, params: Params) -> Result { + match params { + Params::None => Ok(Value::U64(0)), + _ => Err(Error::invalid_params()) + } + } + + // TODO: do not hardode gas_price fn gas_price(&self, params: Params) -> Result { match params { Params::None => Ok(Value::U64(0)), @@ -52,24 +79,26 @@ impl Eth for EthClient { } } - fn is_mining(&self, params: Params) -> Result { - match params { - Params::None => Ok(Value::Bool(false)), - _ => Err(Error::invalid_params()) + fn block_transaction_count(&self, params: Params) -> Result { + match from_params::(params) { + Ok(hash) => match self.client.block(&hash) { + Some(bytes) => to_value(&BlockView::new(&bytes).transactions_count()), + None => Ok(Value::Null) + }, + Err(err) => Err(err) } } - fn hashrate(&self, params: Params) -> Result { - match params { - Params::None => Ok(Value::U64(0)), - _ => Err(Error::invalid_params()) + fn block_uncles_count(&self, params: Params) -> Result { + match from_params::(params) { + Ok(hash) => match self.client.block(&hash) { + Some(bytes) => to_value(&BlockView::new(&bytes).uncles_count()), + None => Ok(Value::Null) + }, + Err(err) => Err(err) } } - fn block_transaction_count(&self, _: Params) -> Result { - Ok(Value::U64(0)) - } - 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_total_difficulty(&hash)) { diff --git a/rpc/src/v1/traits/eth.rs b/rpc/src/v1/traits/eth.rs index 3dcdfdf05..e9134d84a 100644 --- a/rpc/src/v1/traits/eth.rs +++ b/rpc/src/v1/traits/eth.rs @@ -7,6 +7,9 @@ pub trait Eth: Sized + Send + Sync + 'static { /// Returns protocol version. fn protocol_version(&self, _: Params) -> Result { rpc_unimplemented!() } + /// Returns an object with data about the sync status or false. (wtf?) + fn syncing(&self, _: Params) -> Result { rpc_unimplemented!() } + /// Returns the number of hashes per second that the node is mining with. fn hashrate(&self, _: Params) -> Result { rpc_unimplemented!() } @@ -92,6 +95,7 @@ pub trait Eth: Sized + Send + Sync + 'static { 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_syncing", Eth::syncing); delegate.add_method("eth_hashrate", Eth::hashrate); delegate.add_method("eth_coinbase", Eth::author); delegate.add_method("eth_mining", Eth::is_mining); diff --git a/rpc/src/v1/types/mod.rs b/rpc/src/v1/types/mod.rs index 7be32e84d..b0ffff2ae 100644 --- a/rpc/src/v1/types/mod.rs +++ b/rpc/src/v1/types/mod.rs @@ -1,3 +1,5 @@ mod block; +mod sync; pub use self::block::Block; +pub use self::sync::SyncStatus; diff --git a/rpc/src/v1/types/sync.rs b/rpc/src/v1/types/sync.rs new file mode 100644 index 000000000..b13b7167a --- /dev/null +++ b/rpc/src/v1/types/sync.rs @@ -0,0 +1,11 @@ +use util::hash::*; + +#[derive(Default, Debug, Serialize)] +pub struct SyncStatus { + #[serde(rename="startingBlock")] + pub starting_block: H256, + #[serde(rename="currentBlock")] + pub current_block: H256, + #[serde(rename="highestBlock")] + pub highest_block: H256, +}