openethereum/rpc/src/impls/eth.rs

128 lines
3.0 KiB
Rust
Raw Normal View History

2016-01-26 11:37:24 +01:00
use std::sync::Arc;
2016-01-26 19:24:33 +01:00
use jsonrpc_core::*;
2016-01-21 01:19:29 +01:00
use util::hash::*;
2016-01-27 12:31:54 +01:00
use util::uint::*;
use util::sha3::*;
2016-01-21 01:19:29 +01:00
use ethcore::client::*;
2016-01-27 12:31:54 +01:00
use ethcore::views::*;
2016-01-26 13:14:22 +01:00
use traits::{Eth, EthFilter};
use types::Block;
2016-01-21 01:19:29 +01:00
pub struct EthClient {
2016-01-25 13:09:46 +01:00
client: Arc<Client>,
2016-01-21 01:19:29 +01:00
}
impl EthClient {
2016-01-25 13:09:46 +01:00
pub fn new(client: Arc<Client>) -> Self {
2016-01-21 01:19:29 +01:00
EthClient {
client: client
}
}
}
impl Eth for EthClient {
fn protocol_version(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::U64(63)),
_ => Err(Error::invalid_params())
}
}
fn author(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => to_value(&Address::new()),
2016-01-21 01:19:29 +01:00
_ => Err(Error::invalid_params())
}
}
2016-01-21 11:25:39 +01:00
fn gas_price(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::U64(0)),
_ => Err(Error::invalid_params())
}
}
2016-01-21 01:19:29 +01:00
fn block_number(&self, params: Params) -> Result<Value, Error> {
match params {
2016-01-25 13:09:46 +01:00
Params::None => Ok(Value::U64(self.client.chain_info().best_block_number)),
2016-01-21 01:19:29 +01:00
_ => Err(Error::invalid_params())
}
}
fn is_mining(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::Bool(false)),
_ => Err(Error::invalid_params())
}
}
2016-01-21 11:25:39 +01:00
fn hashrate(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => Ok(Value::U64(0)),
_ => Err(Error::invalid_params())
}
}
2016-01-26 00:42:07 +01:00
fn block_transaction_count(&self, _: Params) -> Result<Value, Error> {
Ok(Value::U64(0))
}
2016-01-26 11:37:24 +01:00
2016-01-26 19:24:33 +01:00
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)) {
(Some(bytes), Some(total_difficulty)) => {
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: total_difficulty,
uncles: vec![],
transactions: vec![]
};
to_value(&block)
},
_ => Ok(Value::Null)
},
Err(err) => Err(err)
2016-01-26 19:24:33 +01:00
}
2016-01-26 11:37:24 +01:00
}
2016-01-21 11:25:39 +01:00
}
pub struct EthFilterClient {
2016-01-25 13:09:46 +01:00
client: Arc<Client>
2016-01-21 11:25:39 +01:00
}
impl EthFilterClient {
2016-01-25 13:09:46 +01:00
pub fn new(client: Arc<Client>) -> Self {
2016-01-21 11:25:39 +01:00
EthFilterClient {
client: client
}
}
}
impl EthFilter for EthFilterClient {
fn new_block_filter(&self, _params: Params) -> Result<Value, Error> {
Ok(Value::U64(0))
}
fn new_pending_transaction_filter(&self, _params: Params) -> Result<Value, Error> {
Ok(Value::U64(1))
}
fn filter_changes(&self, _: Params) -> Result<Value, Error> {
to_value(&self.client.chain_info().best_block_hash).map(|v| Value::Array(vec![v]))
2016-01-21 11:25:39 +01:00
}
2016-01-21 01:19:29 +01:00
}