fix(light eth_gasPrice): ask network if not in cache (#10535)

* fix(light eth_gasPrice): ask N/W if not in cache

* fix(bad rebase)
This commit is contained in:
Niklas Adolfsson 2019-03-31 10:40:33 +02:00 committed by Talha Cross
parent 7b2afdfc8c
commit 95236d25b2
4 changed files with 26 additions and 22 deletions

View File

@ -43,7 +43,7 @@ use light::request::Field;
use sync::{LightNetworkDispatcher, ManageNetwork, LightSyncProvider}; use sync::{LightNetworkDispatcher, ManageNetwork, LightSyncProvider};
use ethereum_types::Address; use ethereum_types::{Address, U256};
use hash::H256; use hash::H256;
use parking_lot::Mutex; use parking_lot::Mutex;
use fastmap::H256FastMap; 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 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 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<S, OD>(dispatch: &Arc<dispatch::LightDispatcher<S, OD>>) -> impl Iterator<Item=PendingTransaction> pub fn light_all_transactions<S, OD>(dispatch: &Arc<dispatch::LightDispatcher<S, OD>>) -> impl Iterator<Item=PendingTransaction>
where where
@ -231,7 +232,6 @@ where
/// Helper for getting proved execution. /// Helper for getting proved execution.
pub fn proved_read_only_execution(&self, req: CallRequest, num: Option<BlockNumber>) -> impl Future<Item = ExecutionResult, Error = Error> + Send { pub fn proved_read_only_execution(&self, req: CallRequest, num: Option<BlockNumber>) -> impl Future<Item = ExecutionResult, Error = Error> + Send {
const DEFAULT_GAS_PRICE: u64 = 21_000;
// (21000 G_transaction + 32000 G_create + some marginal to allow a few operations) // (21000 G_transaction + 32000 G_create + some marginal to allow a few operations)
const START_GAS: u64 = 60_000; 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))), 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 { let gas_price_fut = match req.gas_price {
Some(price) => Either::A(future::ok(price)), Some(price) => Either::A(future::ok(price)),
None => Either::B(dispatch::light::fetch_gas_price_corpus( None => Either::B(self.gas_price()),
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(),
}))
}; };
// if nonce resolves, this should too since it'll be in the LRU-cache. // 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<Item = U256, Error = Error> + 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. /// Get a block itself. Fails on unknown block ID.
pub fn block(&self, id: BlockId) -> impl Future<Item = encoded::Block, Error = Error> + Send { pub fn block(&self, id: BlockId) -> impl Future<Item = encoded::Block, Error = Error> + Send {
let mut reqs = Vec::new(); let mut reqs = Vec::new();

View File

@ -552,8 +552,8 @@ impl<C, SN: ?Sized, S: ?Sized, M, EM, T: StateInfo + 'static> Eth for EthClient<
Ok(self.external_miner.hashrate()) Ok(self.external_miner.hashrate())
} }
fn gas_price(&self) -> Result<U256> { fn gas_price(&self) -> BoxFuture<U256> {
Ok(default_gas_price(&*self.client, &*self.miner, self.options.gas_price_percentile)) Box::new(future::ok(default_gas_price(&*self.client, &*self.miner, self.options.gas_price_percentile)))
} }
fn accounts(&self) -> Result<Vec<H160>> { fn accounts(&self) -> Result<Vec<H160>> {

View File

@ -38,8 +38,7 @@ use types::filter::Filter as EthcoreFilter;
use types::ids::BlockId; use types::ids::BlockId;
use v1::impls::eth_filter::Filterable; use v1::impls::eth_filter::Filterable;
use v1::helpers::{errors, limit_logs}; use v1::helpers::{errors, limit_logs, SyncPollFilter, PollManager};
use v1::helpers::{SyncPollFilter, PollManager};
use v1::helpers::deprecated::{self, DeprecationNotice}; use v1::helpers::deprecated::{self, DeprecationNotice};
use v1::helpers::light_fetch::{self, LightFetch}; use v1::helpers::light_fetch::{self, LightFetch};
use v1::traits::Eth; use v1::traits::Eth;
@ -271,11 +270,8 @@ where
Ok(Default::default()) Ok(Default::default())
} }
fn gas_price(&self) -> Result<U256> { fn gas_price(&self) -> BoxFuture<U256> {
Ok(self.cache.lock().gas_price_corpus() Box::new(self.fetcher().gas_price())
.and_then(|c| c.percentile(self.gas_price_percentile).cloned())
.map(U256::from)
.unwrap_or_else(Default::default))
} }
fn accounts(&self) -> Result<Vec<H160>> { fn accounts(&self) -> Result<Vec<H160>> {

View File

@ -56,7 +56,7 @@ pub trait Eth {
/// Returns current gas_price. /// Returns current gas_price.
#[rpc(name = "eth_gasPrice")] #[rpc(name = "eth_gasPrice")]
fn gas_price(&self) -> Result<U256>; fn gas_price(&self) -> BoxFuture<U256>;
/// Returns accounts list. /// Returns accounts list.
#[rpc(name = "eth_accounts")] #[rpc(name = "eth_accounts")]