2016-02-05 13:40:41 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-01-27 17:08:59 +01:00
|
|
|
//! Eth rpc implementation.
|
2016-01-26 11:37:24 +01:00
|
|
|
use std::sync::Arc;
|
2016-02-10 16:28:59 +01:00
|
|
|
use ethsync::{EthSync, SyncState};
|
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-02-10 18:26:06 +01:00
|
|
|
use ethcore::ethereum::denominations::shannon;
|
2016-01-27 17:14:41 +01:00
|
|
|
use v1::traits::{Eth, EthFilter};
|
2016-02-10 16:28:59 +01:00
|
|
|
use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo, Transaction, OptionalValue, Index};
|
2016-01-21 01:19:29 +01:00
|
|
|
|
2016-01-27 17:08:59 +01:00
|
|
|
/// Eth rpc implementation.
|
2016-01-21 01:19:29 +01:00
|
|
|
pub struct EthClient {
|
2016-01-25 13:09:46 +01:00
|
|
|
client: Arc<Client>,
|
2016-02-10 16:28:59 +01:00
|
|
|
sync: Arc<EthSync>
|
2016-01-21 01:19:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EthClient {
|
2016-01-27 17:08:59 +01:00
|
|
|
/// Creates new EthClient.
|
2016-02-10 16:28:59 +01:00
|
|
|
pub fn new(client: Arc<Client>, sync: Arc<EthSync>) -> Self {
|
2016-01-21 01:19:29 +01:00
|
|
|
EthClient {
|
2016-02-10 16:28:59 +01:00
|
|
|
client: client,
|
|
|
|
sync: sync
|
2016-01-21 01:19:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eth for EthClient {
|
|
|
|
fn protocol_version(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
match params {
|
2016-02-10 18:26:06 +01:00
|
|
|
Params::None => to_value(&U256::from(self.sync.status().protocol_version)),
|
2016-01-21 01:19:29 +01:00
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 13:21:34 +01:00
|
|
|
fn syncing(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
match params {
|
2016-02-10 16:28:59 +01:00
|
|
|
Params::None => {
|
|
|
|
let status = self.sync.status();
|
|
|
|
let res = match status.state {
|
|
|
|
SyncState::NotSynced | SyncState::Idle => SyncStatus::None,
|
|
|
|
SyncState::Waiting | SyncState::Blocks | SyncState::NewBlocks => SyncStatus::Info(SyncInfo {
|
|
|
|
starting_block: U256::from(status.start_block_number),
|
2016-02-10 18:03:29 +01:00
|
|
|
current_block: U256::from(self.client.chain_info().best_block_number),
|
2016-02-10 16:28:59 +01:00
|
|
|
highest_block: U256::from(status.highest_block_number.unwrap_or(status.start_block_number))
|
|
|
|
})
|
|
|
|
};
|
|
|
|
to_value(&res)
|
|
|
|
}
|
2016-02-05 13:21:34 +01:00
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: do not hardcode author.
|
2016-01-21 01:19:29 +01:00
|
|
|
fn author(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
match params {
|
2016-01-27 14:25:12 +01:00
|
|
|
Params::None => to_value(&Address::new()),
|
2016-01-21 01:19:29 +01:00
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 13:21:34 +01:00
|
|
|
// TODO: return real value of mining once it's implemented.
|
|
|
|
fn is_mining(&self, params: Params) -> Result<Value, Error> {
|
2016-01-21 11:25:39 +01:00
|
|
|
match params {
|
2016-02-05 13:21:34 +01:00
|
|
|
Params::None => Ok(Value::Bool(false)),
|
2016-01-21 11:25:39 +01:00
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 13:21:34 +01:00
|
|
|
// TODO: return real hashrate once we have mining
|
|
|
|
fn hashrate(&self, params: Params) -> Result<Value, Error> {
|
2016-01-21 01:19:29 +01:00
|
|
|
match params {
|
2016-02-10 18:26:06 +01:00
|
|
|
Params::None => to_value(&U256::zero()),
|
2016-01-21 01:19:29 +01:00
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 13:21:34 +01:00
|
|
|
fn gas_price(&self, params: Params) -> Result<Value, Error> {
|
2016-01-21 01:19:29 +01:00
|
|
|
match params {
|
2016-02-10 18:26:06 +01:00
|
|
|
Params::None => to_value(&(shannon() * U256::from(50))),
|
2016-01-21 01:19:29 +01:00
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
|
|
|
}
|
2016-01-21 11:25:39 +01:00
|
|
|
|
2016-02-05 13:21:34 +01:00
|
|
|
fn block_number(&self, params: Params) -> Result<Value, Error> {
|
2016-01-21 11:25:39 +01:00
|
|
|
match params {
|
2016-02-10 18:26:06 +01:00
|
|
|
Params::None => to_value(&U256::from(self.client.chain_info().best_block_number)),
|
2016-01-21 11:25:39 +01:00
|
|
|
_ => Err(Error::invalid_params())
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 00:42:07 +01:00
|
|
|
|
2016-02-05 13:21:34 +01:00
|
|
|
fn block_transaction_count(&self, params: Params) -> Result<Value, Error> {
|
2016-02-10 10:12:56 +01:00
|
|
|
from_params::<(H256,)>(params)
|
2016-02-10 19:29:27 +01:00
|
|
|
.and_then(|(hash,)| match self.client.block(BlockId::Hash(hash)) {
|
2016-02-05 13:21:34 +01:00
|
|
|
Some(bytes) => to_value(&BlockView::new(&bytes).transactions_count()),
|
|
|
|
None => Ok(Value::Null)
|
2016-02-10 10:12:56 +01:00
|
|
|
})
|
2016-02-05 13:21:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn block_uncles_count(&self, params: Params) -> Result<Value, Error> {
|
2016-02-10 10:12:56 +01:00
|
|
|
from_params::<(H256,)>(params)
|
2016-02-10 19:29:27 +01:00
|
|
|
.and_then(|(hash,)| match self.client.block(BlockId::Hash(hash)) {
|
2016-02-05 13:21:34 +01:00
|
|
|
Some(bytes) => to_value(&BlockView::new(&bytes).uncles_count()),
|
|
|
|
None => Ok(Value::Null)
|
2016-02-10 10:12:56 +01:00
|
|
|
})
|
2016-01-26 00:42:07 +01:00
|
|
|
}
|
2016-01-26 11:37:24 +01:00
|
|
|
|
2016-02-08 10:58:08 +01:00
|
|
|
// TODO: do not ignore block number param
|
|
|
|
fn code_at(&self, params: Params) -> Result<Value, Error> {
|
2016-02-10 10:12:56 +01:00
|
|
|
from_params::<(Address, BlockNumber)>(params)
|
|
|
|
.and_then(|(address, _block_number)| to_value(&self.client.code(&address).map_or_else(Bytes::default, Bytes::new)))
|
2016-02-08 10:58:08 +01:00
|
|
|
}
|
|
|
|
|
2016-01-26 19:24:33 +01:00
|
|
|
fn block(&self, params: Params) -> Result<Value, Error> {
|
2016-02-10 10:12:56 +01:00
|
|
|
from_params::<(H256, bool)>(params)
|
2016-02-10 19:29:27 +01:00
|
|
|
.and_then(|(hash, include_txs)| match (self.client.block(BlockId::Hash(hash.clone())), self.client.block_total_difficulty(BlockId::Hash(hash))) {
|
2016-01-27 14:43:43 +01:00
|
|
|
(Some(bytes), Some(total_difficulty)) => {
|
2016-02-09 16:38:21 +01:00
|
|
|
let block_view = BlockView::new(&bytes);
|
|
|
|
let view = block_view.header_view();
|
2016-01-27 14:25:12 +01:00
|
|
|
let block = Block {
|
2016-02-09 13:17:44 +01:00
|
|
|
hash: OptionalValue::Value(view.sha3()),
|
2016-01-27 14:25:12 +01:00
|
|
|
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(),
|
2016-02-09 13:17:44 +01:00
|
|
|
number: OptionalValue::Value(U256::from(view.number())),
|
2016-01-27 14:25:12 +01:00
|
|
|
gas_used: view.gas_used(),
|
|
|
|
gas_limit: view.gas_limit(),
|
|
|
|
logs_bloom: view.log_bloom(),
|
|
|
|
timestamp: U256::from(view.timestamp()),
|
|
|
|
difficulty: view.difficulty(),
|
2016-01-27 14:43:43 +01:00
|
|
|
total_difficulty: total_difficulty,
|
2016-01-27 14:25:12 +01:00
|
|
|
uncles: vec![],
|
2016-02-08 12:13:05 +01:00
|
|
|
transactions: {
|
|
|
|
if include_txs {
|
2016-02-09 16:38:21 +01:00
|
|
|
BlockTransactions::Full(block_view.localized_transactions().into_iter().map(From::from).collect())
|
2016-02-08 12:13:05 +01:00
|
|
|
} else {
|
2016-02-09 16:38:21 +01:00
|
|
|
BlockTransactions::Hashes(block_view.transaction_hashes())
|
2016-02-08 12:13:05 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
extra_data: Bytes::default()
|
2016-01-27 14:25:12 +01:00
|
|
|
};
|
|
|
|
to_value(&block)
|
|
|
|
},
|
|
|
|
_ => Ok(Value::Null)
|
2016-02-10 11:28:40 +01:00
|
|
|
})
|
2016-01-26 11:37:24 +01:00
|
|
|
}
|
2016-02-09 13:17:44 +01:00
|
|
|
|
2016-02-10 10:12:56 +01:00
|
|
|
fn transaction_by_hash(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(H256,)>(params)
|
2016-02-10 11:28:40 +01:00
|
|
|
.and_then(|(hash,)| match self.client.transaction(TransactionId::Hash(hash)) {
|
2016-02-09 16:38:21 +01:00
|
|
|
Some(t) => to_value(&Transaction::from(t)),
|
2016-02-09 13:17:44 +01:00
|
|
|
None => Ok(Value::Null)
|
2016-02-10 11:28:40 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn transaction_by_block_hash_and_index(&self, params: Params) -> Result<Value, Error> {
|
|
|
|
from_params::<(H256, Index)>(params)
|
2016-02-10 15:15:28 +01:00
|
|
|
.and_then(|(hash, index)| match self.client.transaction(TransactionId::Location(BlockId::Hash(hash), index.value())) {
|
2016-02-10 11:28:40 +01:00
|
|
|
Some(t) => to_value(&Transaction::from(t)),
|
|
|
|
None => Ok(Value::Null)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn transaction_by_block_number_and_index(&self, _params: Params) -> Result<Value, Error> {
|
|
|
|
unimplemented!()
|
2016-02-09 13:17:44 +01:00
|
|
|
}
|
2016-01-21 11:25:39 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 13:17:44 +01:00
|
|
|
|
2016-01-27 17:08:59 +01:00
|
|
|
/// Eth filter rpc implementation.
|
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-27 17:08:59 +01:00
|
|
|
/// Creates new Eth filter client.
|
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> {
|
2016-01-27 14:25:12 +01:00
|
|
|
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
|
|
|
}
|