From e460421deb91bd385e4e01c1f927e282aad05607 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Fri, 3 Feb 2017 17:53:48 +0100 Subject: [PATCH] generic basic account fetcher, nonce RPC --- rpc/src/v1/impls/light/eth.rs | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/rpc/src/v1/impls/light/eth.rs b/rpc/src/v1/impls/light/eth.rs index 9b48131ce..d546ad27e 100644 --- a/rpc/src/v1/impls/light/eth.rs +++ b/rpc/src/v1/impls/light/eth.rs @@ -27,6 +27,7 @@ use light::on_demand::{request, OnDemand}; use light::net::LightProtocol; use ethcore::account_provider::{AccountProvider, DappId}; +use ethcore::basic_account::BasicAccount; use ethcore::encoded; use ethcore::ids::BlockId; use ethsync::LightSync; @@ -44,6 +45,8 @@ use v1::types::{ }; use v1::metadata::Metadata; +use util::Address; + /// Light client `ETH` RPC. pub struct EthClient { sync: Arc, @@ -106,6 +109,20 @@ impl EthClient { None => future::err(err_no_context()).boxed() } } + + // helper for getting account info. + fn account(&self, address: Address, id: BlockId) -> BoxFuture { + let (sync, on_demand) = (self.sync.clone(), self.on_demand.clone()); + + self.header(id).and_then(move |header| { + sync.with_context(|ctx| on_demand.account(ctx, request::Account { + header: header, + address: address, + })) + .map(|x| x.map_err(errors::from_on_demand_error).boxed()) + .unwrap_or_else(|| future::err(err_no_context()).boxed()) + }).boxed() + } } impl Eth for EthClient { @@ -152,19 +169,7 @@ impl Eth for EthClient { } fn balance(&self, address: RpcH160, num: Trailing) -> BoxFuture { - let address = address.into(); - - let sync = self.sync.clone(); - let on_demand = self.on_demand.clone(); - - self.header(num.0.into()).and_then(move |header| { - sync.with_context(|ctx| on_demand.account(ctx, request::Account { - header: header, - address: address, - })) - .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() + self.account(address.into(), num.0.into()).map(|acc| acc.balance.into()).boxed() } fn storage_at(&self, address: RpcH160, key: RpcU256, num: Trailing) -> BoxFuture { @@ -180,7 +185,7 @@ impl Eth for EthClient { } fn transaction_count(&self, address: RpcH160, num: Trailing) -> BoxFuture { - future::err(errors::unimplemented(None)).boxed() + self.account(address.into(), num.0.into()).map(|acc| acc.nonce.into()).boxed() } fn block_transaction_count_by_hash(&self, hash: RpcH256) -> BoxFuture, Error> {