Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions rpc/src/v1/helpers/light_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<S, OD>(dispatch: &Arc<dispatch::LightDispatcher<S, OD>>) -> impl Iterator<Item=PendingTransaction>
where
Expand Down Expand Up @@ -231,7 +232,6 @@ where

/// Helper for getting proved execution.
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)
const START_GAS: u64 = 60_000;

Expand All @@ -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(
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted this to a helper method instead

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.
Expand Down Expand Up @@ -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.
pub fn block(&self, id: BlockId) -> impl Future<Item = encoded::Block, Error = Error> + Send {
let mut reqs = Vec::new();
Expand Down
4 changes: 2 additions & 2 deletions rpc/src/v1/impls/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,8 @@ impl<C, SN: ?Sized, S: ?Sized, M, EM, T: StateInfo + 'static> Eth for EthClient<
Ok(self.external_miner.hashrate())
}

fn gas_price(&self) -> Result<U256> {
Ok(default_gas_price(&*self.client, &*self.miner, self.options.gas_price_percentile))
fn gas_price(&self) -> BoxFuture<U256> {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is change is needed to have the API as the light-client

Box::new(future::ok(default_gas_price(&*self.client, &*self.miner, self.options.gas_price_percentile)))
}

fn accounts(&self) -> Result<Vec<H160>> {
Expand Down
10 changes: 3 additions & 7 deletions rpc/src/v1/impls/light/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -271,11 +270,8 @@ where
Ok(Default::default())
}

fn gas_price(&self) -> Result<U256> {
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<U256> {
Box::new(self.fetcher().gas_price())
Copy link
Copy Markdown
Collaborator Author

@niklasad1 niklasad1 Mar 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, dispatch::light::fetch_gas_price_corpus will read cache first before trying to perform a N/W request(s)

However, it will cost a few extra Arc::clone to construct LightFetch instead of directly call the cache

}

fn accounts(&self) -> Result<Vec<H160>> {
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/v1/traits/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub trait Eth {

/// Returns current gas_price.
#[rpc(name = "eth_gasPrice")]
fn gas_price(&self) -> Result<U256>;
fn gas_price(&self) -> BoxFuture<U256>;

/// Returns accounts list.
#[rpc(name = "eth_accounts")]
Expand Down