diff --git a/rpc/src/v1/helpers/light_fetch.rs b/rpc/src/v1/helpers/light_fetch.rs index f50796473..7167a7316 100644 --- a/rpc/src/v1/helpers/light_fetch.rs +++ b/rpc/src/v1/helpers/light_fetch.rs @@ -43,7 +43,7 @@ use light::request::Field; use sync::{LightNetworkDispatcher, ManageNetwork, LightSyncProvider}; -use ethereum_types::Address; +use ethereum_types::{Address, U256}; use hash::H256; use parking_lot::Mutex; use fastmap::H256FastMap; @@ -55,6 +55,7 @@ use v1::types::{BlockNumber, CallRequest, Log, Transaction}; const NO_INVALID_BACK_REFS_PROOF: &str = "Fails only on invalid back-references; back-references here known to be valid; qed"; const WRONG_RESPONSE_AMOUNT_TYPE_PROOF: &str = "responses correspond directly with requests in amount and type; qed"; +const DEFAULT_GAS_PRICE: u64 = 21_000; pub fn light_all_transactions(dispatch: &Arc>) -> impl Iterator where @@ -231,7 +232,6 @@ where /// Helper for getting proved execution. pub fn proved_read_only_execution(&self, req: CallRequest, num: Option) -> impl Future + Send { - const DEFAULT_GAS_PRICE: u64 = 21_000; // (21000 G_transaction + 32000 G_create + some marginal to allow a few operations) const START_GAS: u64 = 60_000; @@ -257,18 +257,9 @@ where None => Either::B(self.account(from, id).map(|acc| acc.map(|a| a.nonce))), }; - let gas_price_percentile = self.gas_price_percentile; let gas_price_fut = match req.gas_price { Some(price) => Either::A(future::ok(price)), - None => Either::B(dispatch::light::fetch_gas_price_corpus( - self.sync.clone(), - self.client.clone(), - self.on_demand.clone(), - self.cache.clone(), - ).map(move |corp| match corp.percentile(gas_price_percentile) { - Some(percentile) => *percentile, - None => DEFAULT_GAS_PRICE.into(), - })) + None => Either::B(self.gas_price()), }; // if nonce resolves, this should too since it'll be in the LRU-cache. @@ -307,6 +298,23 @@ where })) } + /// Helper to fetch the corpus gas price from 1) the cache 2) the network then it tries to estimate the percentile + /// using `gas_price_percentile` if the estimated percentile is zero the `DEFAULT_GAS_PRICE` is returned + pub fn gas_price(&self) -> impl Future + Send { + let gas_price_percentile = self.gas_price_percentile; + + dispatch::light::fetch_gas_price_corpus( + self.sync.clone(), + self.client.clone(), + self.on_demand.clone(), + self.cache.clone(), + ) + .map(move |corp| { + corp.percentile(gas_price_percentile) + .map_or_else(|| DEFAULT_GAS_PRICE.into(), |percentile| *percentile) + }) + } + /// Get a block itself. Fails on unknown block ID. pub fn block(&self, id: BlockId) -> impl Future + Send { let mut reqs = Vec::new(); diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 1b5fdf447..32f9c1c57 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -552,8 +552,8 @@ impl Eth for EthClient< Ok(self.external_miner.hashrate()) } - fn gas_price(&self) -> Result { - Ok(default_gas_price(&*self.client, &*self.miner, self.options.gas_price_percentile)) + fn gas_price(&self) -> BoxFuture { + Box::new(future::ok(default_gas_price(&*self.client, &*self.miner, self.options.gas_price_percentile))) } fn accounts(&self) -> Result> { diff --git a/rpc/src/v1/impls/light/eth.rs b/rpc/src/v1/impls/light/eth.rs index b3e183c1c..cd17b0673 100644 --- a/rpc/src/v1/impls/light/eth.rs +++ b/rpc/src/v1/impls/light/eth.rs @@ -38,8 +38,7 @@ use types::filter::Filter as EthcoreFilter; use types::ids::BlockId; use v1::impls::eth_filter::Filterable; -use v1::helpers::{errors, limit_logs}; -use v1::helpers::{SyncPollFilter, PollManager}; +use v1::helpers::{errors, limit_logs, SyncPollFilter, PollManager}; use v1::helpers::deprecated::{self, DeprecationNotice}; use v1::helpers::light_fetch::{self, LightFetch}; use v1::traits::Eth; @@ -271,11 +270,8 @@ where Ok(Default::default()) } - fn gas_price(&self) -> Result { - Ok(self.cache.lock().gas_price_corpus() - .and_then(|c| c.percentile(self.gas_price_percentile).cloned()) - .map(U256::from) - .unwrap_or_else(Default::default)) + fn gas_price(&self) -> BoxFuture { + Box::new(self.fetcher().gas_price()) } fn accounts(&self) -> Result> { diff --git a/rpc/src/v1/traits/eth.rs b/rpc/src/v1/traits/eth.rs index 14f8fb69e..69a37ae52 100644 --- a/rpc/src/v1/traits/eth.rs +++ b/rpc/src/v1/traits/eth.rs @@ -56,7 +56,7 @@ pub trait Eth { /// Returns current gas_price. #[rpc(name = "eth_gasPrice")] - fn gas_price(&self) -> Result; + fn gas_price(&self) -> BoxFuture; /// Returns accounts list. #[rpc(name = "eth_accounts")]