use jsonrpc 1.1, moved params deserialization to jsonrpc-core

This commit is contained in:
debris 2016-01-27 14:25:12 +01:00
parent bcfe40e743
commit 322c1a6cb2
4 changed files with 33 additions and 62 deletions

View File

@ -11,8 +11,8 @@ authors = ["Marek Kotewicz <marek.kotewicz@gmail.com"]
serde = "0.6.7"
serde_macros = "0.6.10"
serde_json = "0.6.0"
jsonrpc-core = "1.0"
jsonrpc-http-server = "1.0"
jsonrpc-core = "1.1"
jsonrpc-http-server = "1.1"
ethcore-util = { path = "../util" }
ethcore = { path = ".." }

View File

@ -6,7 +6,7 @@ use util::sha3::*;
use ethcore::client::*;
use ethcore::views::*;
use traits::{Eth, EthFilter};
use types::{Block, to_value, from_value};
use types::Block;
pub struct EthClient {
client: Arc<Client>,
@ -30,7 +30,7 @@ impl Eth for EthClient {
fn author(&self, params: Params) -> Result<Value, Error> {
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<Value, Error> {
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::<H256>(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<Value, Error> {
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]))
}
}

View File

@ -34,13 +34,3 @@ pub struct Block {
pub uncles: Vec<U256>,
pub transactions: Vec<U256>
}
#[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);
}

View File

@ -1,18 +1,3 @@
use serde::{Serialize, Deserialize, de};
use serde_json::value::{Value, Serializer, Deserializer};
mod block;
pub fn to_value<S>(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<D>(value: Value) -> Result<D, <Deserializer as de::Deserializer>::Error> where D: Deserialize {
let mut deserialier = Deserializer::new(value);
Deserialize::deserialize(&mut deserialier)
}
pub use self::block::Block;