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();

View File

@ -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<BlockDetails>;
/// Get raw block header data by block number.
fn block_header_at(&self, n: BlockNumber) -> Option<Bytes>;
@ -300,6 +304,10 @@ impl BlockChainClient for Client {
if self.chain.read().unwrap().is_known(&hash) { BlockStatus::InChain } else { BlockStatus::Unknown }
}
fn block_details(&self, hash: &H256) -> Option<BlockDetails> {
self.chain.read().unwrap().block_details(hash)
}
fn block_header_at(&self, n: BlockNumber) -> Option<Bytes> {
self.chain.read().unwrap().block_hash(n).and_then(|h| self.block_header(&h))
}

View File

@ -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<U256>,
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<U256, evm::Error> {
match &mut self.output {
&mut OutputPolicy::Return(BytesRef::Fixed(ref mut slice)) => unsafe {