start eth RPC API for light client

This commit is contained in:
Robert Habermeier 2017-02-03 16:20:43 +01:00
parent 1dc957e3a5
commit 9328067eac
8 changed files with 186 additions and 2 deletions

1
Cargo.lock generated
View File

@ -570,6 +570,7 @@ dependencies = [
"ethcore-devtools 1.6.0", "ethcore-devtools 1.6.0",
"ethcore-io 1.6.0", "ethcore-io 1.6.0",
"ethcore-ipc 1.6.0", "ethcore-ipc 1.6.0",
"ethcore-light 1.6.0",
"ethcore-util 1.6.0", "ethcore-util 1.6.0",
"ethcrypto 0.1.0", "ethcrypto 0.1.0",
"ethjson 0.1.0", "ethjson 0.1.0",

View File

@ -32,6 +32,7 @@ ethash = { path = "../ethash" }
ethsync = { path = "../sync" } ethsync = { path = "../sync" }
ethjson = { path = "../json" } ethjson = { path = "../json" }
ethcore-devtools = { path = "../devtools" } ethcore-devtools = { path = "../devtools" }
ethcore-light = { path = "../ethcore/light" }
parity-updater = { path = "../updater" } parity-updater = { path = "../updater" }
rlp = { path = "../util/rlp" } rlp = { path = "../util/rlp" }
fetch = { path = "../util/fetch" } fetch = { path = "../util/fetch" }

View File

@ -33,6 +33,7 @@ extern crate ethcrypto as crypto;
extern crate ethstore; extern crate ethstore;
extern crate ethsync; extern crate ethsync;
extern crate ethash; extern crate ethash;
extern crate ethcore_light as light;
extern crate transient_hashmap; extern crate transient_hashmap;
extern crate jsonrpc_ipc_server as ipc; extern crate jsonrpc_ipc_server as ipc;
extern crate ethcore_ipc; extern crate ethcore_ipc;

View File

@ -385,7 +385,7 @@ impl<C, SN: ?Sized, S: ?Sized, M, EM> Eth for EthClient<C, SN, S, M, EM> where
Ok(RpcU256::from(take_weak!(self.client).chain_info().best_block_number)) Ok(RpcU256::from(take_weak!(self.client).chain_info().best_block_number))
} }
fn balance(&self, address: RpcH160, num: Trailing<BlockNumber>) -> Result<RpcU256, Error> { fn balance(&self, address: RpcH160, num: Trailing<BlockNumber>) -> BoxFuture<RpcU256, Error> {
self.active()?; self.active()?;
let address = address.into(); let address = address.into();

View File

@ -0,0 +1,161 @@
// 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;
use ethcore::account_provider::AccountProvider;
use ethcore::encoded;
use ethcore::ids::BlockId;
use ethsync::LightSync;
use futures::{future, BoxFuture};
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>,
}
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) => {
let cht_root = cht::block_to_cht_number(n).and_then(|cn| self.client.cht_root(cn));
match cht_root {
None => return future::err(errors::unknown_block()).boxed(),
Some(root) => {
let req = request::HeaderByNumber {
num: n,
cht_root: root,
};
self.sync.with_context(|ctx| self.on_demand.header_by_number(req))
}
}
}
BlockId::Hash(h) => {
self.sync.with_context(|ctx| self.on_demand.header_by_hash(request::HeaderByHash(h)))
}
_ => None, // latest, earliest, and pending will have all already returned.
};
match maybe_future {
Some(recv) => recv.boxed(),
None => future::err(errors::internal("network service detached", "")).boxed()
}
}
}
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> {
future::ok(Default::default())
}
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> {
let dapp = meta.dapp_id.unwrap_or_default();
let accounts = self.accounts
.note_dapp_used(dapp.clone())
.and_then(|_| self.accounts.dapps_addresses(dapp))
.map_err(|e| errors::internal("Could not fetch accounts.", e));
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();
self.header(num.0.into()).then(move |header| {
sync.with_context(|ctx| on_demand.account(ctx, request::Account {
header: header,
address: address,
}))
})
}
}

View File

@ -0,0 +1,19 @@
// 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/>.
//! RPC implementations for the light client.
mod eth;

View File

@ -32,6 +32,7 @@ mod parity;
mod parity_accounts; mod parity_accounts;
mod parity_set; mod parity_set;
mod personal; mod personal;
mod light;
mod signer; mod signer;
mod signing; mod signing;
mod signing_unsafe; mod signing_unsafe;

View File

@ -63,7 +63,7 @@ build_rpc_trait! {
/// Returns balance of the given account. /// Returns balance of the given account.
#[rpc(name = "eth_getBalance")] #[rpc(name = "eth_getBalance")]
fn balance(&self, H160, Trailing<BlockNumber>) -> Result<U256, Error>; fn balance(&self, H160, Trailing<BlockNumber>) -> BoxFuture<U256, Error>;
/// Returns content of the storage at given address. /// Returns content of the storage at given address.
#[rpc(name = "eth_getStorageAt")] #[rpc(name = "eth_getStorageAt")]