openethereum/rpc/src/v1/impls/light/eth.rs

278 lines
8.4 KiB
Rust
Raw Normal View History

2017-02-03 16:20:43 +01:00
// Copyright 2015-2017 Parity Technologies (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/>.
//! Eth RPC interface for the light client.
use std::sync::Arc;
use jsonrpc_core::Error;
use jsonrpc_macros::Trailing;
use light::client::Client as LightClient;
use light::cht;
use light::on_demand::{request, OnDemand};
use light::net::LightProtocol;
2017-02-03 17:41:25 +01:00
use ethcore::account_provider::{AccountProvider, DappId};
2017-02-03 16:20:43 +01:00
use ethcore::encoded;
use ethcore::ids::BlockId;
use ethsync::LightSync;
2017-02-03 17:41:25 +01:00
use futures::{future, Future, BoxFuture};
2017-02-03 16:20:43 +01:00
use v1::helpers::{CallRequest as CRequest, errors, limit_logs};
use v1::helpers::dispatch::{dispatch_transaction, default_gas_price};
use v1::helpers::block_import::is_major_importing;
use v1::traits::Eth;
use v1::types::{
RichBlock, Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo,
Transaction, CallRequest, Index, Filter, Log, Receipt, Work,
H64 as RpcH64, H256 as RpcH256, H160 as RpcH160, U256 as RpcU256,
};
use v1::metadata::Metadata;
/// Light client `ETH` RPC.
pub struct EthClient {
sync: Arc<LightSync>,
client: Arc<LightClient>,
on_demand: Arc<OnDemand>,
accounts: Arc<AccountProvider>,
}
2017-02-03 17:41:25 +01:00
// helper for a specific kind of internal error.
fn err_no_context() -> Error {
errors::internal("network service detached", "")
}
2017-02-03 16:20:43 +01:00
impl EthClient {
/// Create a new `EthClient` with a handle to the light sync instance, client,
/// and on-demand request service, which is assumed to be attached as a handler.
pub fn new(
sync: Arc<LightSync>,
client: Arc<LightClient>,
on_demand: Arc<OnDemand>,
accounts: Arc<AccountProvider>,
) -> Self {
EthClient {
sync: sync,
client: client,
on_demand: on_demand,
accounts: accounts,
}
}
/// Get a block header from the on demand service or client, or error.
fn header(&self, id: BlockId) -> BoxFuture<encoded::Header, Error> {
if let Some(h) = self.client.get_header(id) {
return future::ok(h).boxed()
}
let maybe_future = match id {
BlockId::Number(n) => {
2017-02-03 17:41:25 +01:00
let cht_root = cht::block_to_cht_number(n).and_then(|cn| self.client.cht_root(cn as usize));
2017-02-03 16:20:43 +01:00
match cht_root {
None => return future::err(errors::unknown_block()).boxed(),
Some(root) => {
let req = request::HeaderByNumber {
num: n,
cht_root: root,
};
2017-02-03 17:41:25 +01:00
self.sync.with_context(|ctx| self.on_demand.header_by_number(ctx, req))
2017-02-03 16:20:43 +01:00
}
}
}
BlockId::Hash(h) => {
2017-02-03 17:41:25 +01:00
self.sync.with_context(|ctx| self.on_demand.header_by_hash(ctx, request::HeaderByHash(h)))
2017-02-03 16:20:43 +01:00
}
_ => None, // latest, earliest, and pending will have all already returned.
};
match maybe_future {
2017-02-03 17:41:25 +01:00
Some(recv) => recv.map_err(errors::from_on_demand_error).boxed(),
None => future::err(err_no_context()).boxed()
2017-02-03 16:20:43 +01:00
}
}
}
impl Eth for EthClient {
type Metadata = Metadata;
fn protocol_version(&self) -> Result<String, Error> {
Ok(format!("{}", ::light::net::MAX_PROTOCOL_VERSION))
}
fn syncing(&self) -> Result<SyncStatus, Error> {
rpc_unimplemented!()
}
fn author(&self, _meta: Self::Metadata) -> BoxFuture<RpcH160, Error> {
2017-02-03 17:41:25 +01:00
future::ok(Default::default()).boxed()
2017-02-03 16:20:43 +01:00
}
fn is_mining(&self) -> Result<bool, Error> {
Ok(false)
}
fn hashrate(&self) -> Result<RpcU256, Error> {
Ok(Default::default())
}
fn gas_price(&self) -> Result<RpcU256, Error> {
Ok(Default::default())
}
fn accounts(&self, meta: Metadata) -> BoxFuture<Vec<RpcH160>, Error> {
2017-02-03 17:41:25 +01:00
let dapp: DappId = meta.dapp_id.unwrap_or_default().into();
2017-02-03 16:20:43 +01:00
let accounts = self.accounts
.note_dapp_used(dapp.clone())
.and_then(|_| self.accounts.dapps_addresses(dapp))
2017-02-03 17:41:25 +01:00
.map_err(|e| errors::internal("Could not fetch accounts.", e))
.map(|accs| accs.into_iter().map(Into::<RpcH160>::into).collect());
2017-02-03 16:20:43 +01:00
future::done(accounts).boxed()
}
fn block_number(&self) -> Result<RpcU256, Error> {
Ok(self.client.chain_info().best_block_number.into())
}
fn balance(&self, address: RpcH160, num: Trailing<BlockNumber>) -> BoxFuture<RpcU256, Error> {
let address = address.into();
let sync = self.sync.clone();
let on_demand = self.on_demand.clone();
2017-02-03 17:41:25 +01:00
self.header(num.0.into()).and_then(move |header| {
2017-02-03 16:20:43 +01:00
sync.with_context(|ctx| on_demand.account(ctx, request::Account {
header: header,
address: address,
}))
2017-02-03 17:41:25 +01:00
.map(|x| x.map_err(errors::from_on_demand_error).boxed())
.unwrap_or_else(|| future::err(err_no_context()).boxed())
}).map(|acc| acc.balance.into()).boxed()
}
fn storage_at(&self, address: RpcH160, key: RpcU256, num: Trailing<BlockNumber>) -> BoxFuture<RpcH256, Error> {
future::err(errors::unimplemented(None)).boxed()
}
fn block_by_hash(&self, hash: RpcH256, include_txs: bool) -> BoxFuture<Option<RichBlock>, Error> {
future::err(errors::unimplemented(None)).boxed()
}
fn block_by_number(&self, num: BlockNumber, include_txs: bool) -> BoxFuture<Option<RichBlock>, Error> {
future::err(errors::unimplemented(None)).boxed()
}
fn transaction_count(&self, address: RpcH160, num: Trailing<BlockNumber>) -> BoxFuture<RpcU256, Error> {
future::err(errors::unimplemented(None)).boxed()
}
fn block_transaction_count_by_hash(&self, hash: RpcH256) -> BoxFuture<Option<RpcU256>, Error> {
future::err(errors::unimplemented(None)).boxed()
}
fn block_transaction_count_by_number(&self, num: BlockNumber) -> BoxFuture<Option<RpcU256>, Error> {
future::err(errors::unimplemented(None)).boxed()
}
fn block_uncles_count_by_hash(&self, hash: RpcH256) -> BoxFuture<Option<RpcU256>, Error> {
future::err(errors::unimplemented(None)).boxed()
}
fn block_uncles_count_by_number(&self, num: BlockNumber) -> BoxFuture<Option<RpcU256>, Error> {
future::err(errors::unimplemented(None)).boxed()
}
fn code_at(&self, address: RpcH160, num: Trailing<BlockNumber>) -> BoxFuture<Bytes, Error> {
future::err(errors::unimplemented(None)).boxed()
}
fn send_raw_transaction(&self, raw: Bytes) -> Result<RpcH256, Error> {
Err(errors::unimplemented(None))
}
fn submit_transaction(&self, raw: Bytes) -> Result<RpcH256, Error> {
Err(errors::unimplemented(None))
}
fn call(&self, req: CallRequest, num: Trailing<BlockNumber>) -> Result<Bytes, Error> {
Err(errors::unimplemented(None))
}
fn estimate_gas(&self, req: CallRequest, num: Trailing<BlockNumber>) -> Result<RpcU256, Error> {
Err(errors::unimplemented(None))
}
fn transaction_by_hash(&self, hash: RpcH256) -> Result<Option<Transaction>, Error> {
Err(errors::unimplemented(None))
}
fn transaction_by_block_hash_and_index(&self, hash: RpcH256, idx: Index) -> Result<Option<Transaction>, Error> {
Err(errors::unimplemented(None))
}
fn transaction_by_block_number_and_index(&self, num: BlockNumber, idx: Index) -> Result<Option<Transaction>, Error> {
Err(errors::unimplemented(None))
}
fn transaction_receipt(&self, hash: RpcH256) -> Result<Option<Receipt>, Error> {
Err(errors::unimplemented(None))
}
fn uncle_by_block_hash_and_index(&self, hash: RpcH256, idx: Index) -> Result<Option<RichBlock>, Error> {
Err(errors::unimplemented(None))
}
fn uncle_by_block_number_and_index(&self, num: BlockNumber, idx: Index) -> Result<Option<RichBlock>, Error> {
Err(errors::unimplemented(None))
}
fn compilers(&self) -> Result<Vec<String>, Error> {
Err(errors::unimplemented(None))
}
fn compile_lll(&self, _code: String) -> Result<Bytes, Error> {
Err(errors::unimplemented(None))
}
fn compile_solidity(&self, _code: String) -> Result<Bytes, Error> {
Err(errors::unimplemented(None))
}
fn compile_serpent(&self, _code: String) -> Result<Bytes, Error> {
Err(errors::unimplemented(None))
}
fn logs(&self, _filter: Filter) -> Result<Vec<Log>, Error> {
Err(errors::unimplemented(None))
}
fn work(&self, _timeout: Trailing<u64>) -> Result<Work, Error> {
Err(errors::unimplemented(None))
}
fn submit_work(&self, _nonce: RpcH64, _pow_hash: RpcH256, _mix_hash: RpcH256) -> Result<bool, Error> {
Err(errors::unimplemented(None))
}
fn submit_hashrate(&self, _rate: RpcU256, _id: RpcH256) -> Result<bool, Error> {
Err(errors::unimplemented(None))
2017-02-03 16:20:43 +01:00
}
}