few additional rpc eth methods

This commit is contained in:
debris 2016-02-05 13:21:34 +01:00
parent 4af8adc89a
commit 432c0d59c4
5 changed files with 69 additions and 13 deletions

View File

@ -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<TransactionView> {
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<HeaderView> {
self.rlp.at(2).iter().map(HeaderView::new_from_rlp).collect()

View File

@ -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<Value, Error> {
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<Value, Error> {
match params {
Params::None => to_value(&SyncStatus::default()),
_ => Err(Error::invalid_params())
}
}
// TODO: do not hardcode author.
fn author(&self, params: Params) -> Result<Value, Error> {
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<Value, Error> {
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<Value, Error> {
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<Value, Error> {
match params {
Params::None => Ok(Value::U64(0)),
@ -52,24 +79,26 @@ impl Eth for EthClient {
}
}
fn is_mining(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::Bool(false)),
_ => Err(Error::invalid_params())
fn block_transaction_count(&self, params: Params) -> Result<Value, Error> {
match from_params::<H256>(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<Value, Error> {
match params {
Params::None => Ok(Value::U64(0)),
_ => Err(Error::invalid_params())
fn block_uncles_count(&self, params: Params) -> Result<Value, Error> {
match from_params::<H256>(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<Value, Error> {
Ok(Value::U64(0))
}
fn block(&self, params: Params) -> Result<Value, Error> {
match from_params::<(H256, bool)>(params) {
Ok((hash, _include_txs)) => match (self.client.block_header(&hash), self.client.block_total_difficulty(&hash)) {

View File

@ -7,6 +7,9 @@ pub trait Eth: Sized + Send + Sync + 'static {
/// Returns protocol version.
fn protocol_version(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns an object with data about the sync status or false. (wtf?)
fn syncing(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns the number of hashes per second that the node is mining with.
fn hashrate(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
@ -92,6 +95,7 @@ pub trait Eth: Sized + Send + Sync + 'static {
fn to_delegate(self) -> IoDelegate<Self> {
let mut delegate = IoDelegate::new(Arc::new(self));
delegate.add_method("eth_protocolVersion", Eth::protocol_version);
delegate.add_method("eth_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);

View File

@ -1,3 +1,5 @@
mod block;
mod sync;
pub use self::block::Block;
pub use self::sync::SyncStatus;

11
rpc/src/v1/types/sync.rs Normal file
View File

@ -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,
}