block visible on netstats

This commit is contained in:
debris
2016-01-27 12:31:54 +01:00
parent 2f42e0eda0
commit 5865139206
6 changed files with 73 additions and 43 deletions

View File

@@ -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<Client>,
@@ -28,7 +30,7 @@ impl Eth for EthClient {
fn author(&self, params: Params) -> Result<Value, Error> {
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<Value, Error> {
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::<H256>(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<Value, Error> {
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)]))
}
}

View File

@@ -10,7 +10,8 @@ impl Web3Client {
impl Web3 for Web3Client {
fn client_version(&self, params: Params) -> Result<Value, Error> {
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())
}
}

View File

@@ -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<u8>,
#[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<U256>,
pub transactions: Vec<U256>
}
#[test]

View File

@@ -3,7 +3,7 @@ use serde_json::value::{Value, Serializer, Deserializer};
mod block;
pub fn as_value<S>(s: &S) -> Value where S: Serialize {
pub fn to_value<S>(s: &S) -> Value where S: Serialize {
let mut serializer = Serializer::new();
// should never panic!
s.serialize(&mut serializer).unwrap();