Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
fix(light account response): update tx_queue (#10545)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasad1 committed Apr 1, 2019
1 parent 9414477 commit 449a782
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
33 changes: 26 additions & 7 deletions rpc/src/v1/helpers/light_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ use light::on_demand::{
};
use light::on_demand::error::Error as OnDemandError;
use light::request::Field;
use light::TransactionQueue;


use sync::{LightNetworkDispatcher, ManageNetwork, LightSyncProvider};

use ethereum_types::{Address, U256};
use hash::H256;
use parking_lot::Mutex;
use parking_lot::{Mutex, RwLock};
use fastmap::H256FastMap;
use std::collections::BTreeMap;
use types::transaction::{Action, Transaction as EthTransaction, PendingTransaction, SignedTransaction, LocalizedTransaction};
Expand Down Expand Up @@ -96,7 +97,7 @@ where
on_demand: self.on_demand.clone(),
sync: self.sync.clone(),
cache: self.cache.clone(),
gas_price_percentile: self.gas_price_percentile
gas_price_percentile: self.gas_price_percentile,
}
}
}
Expand Down Expand Up @@ -210,7 +211,13 @@ where

/// Helper for getting account info at a given block.
/// `None` indicates the account doesn't exist at the given block.
pub fn account(&self, address: Address, id: BlockId) -> impl Future<Item = Option<BasicAccount>, Error = Error> + Send {
pub fn account(
&self,
address: Address,
id: BlockId,
tx_queue: Arc<RwLock<TransactionQueue>>
) -> impl Future<Item = Option<BasicAccount>, Error = Error> + Send {

let mut reqs = Vec::new();
let header_ref = match self.make_header_requests(id, &mut reqs) {
Ok(r) => r,
Expand All @@ -219,14 +226,26 @@ where

reqs.push(request::Account { header: header_ref, address: address }.into());

Either::B(self.send_requests(reqs, |mut res|match res.pop() {
Some(OnDemandResponse::Account(acc)) => acc,
Either::B(self.send_requests(reqs, move |mut res| match res.pop() {
Some(OnDemandResponse::Account(maybe_account)) => {
if let Some(ref acc) = maybe_account {
let mut txq = tx_queue.write();
txq.cull(address, acc.nonce);
}
maybe_account
}
_ => panic!(WRONG_RESPONSE_AMOUNT_TYPE_PROOF),
}))
}

/// 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>,
txq: Arc<RwLock<TransactionQueue>>
) -> impl Future<Item = ExecutionResult, Error = Error> + Send {

// (21000 G_transaction + 32000 G_create + some marginal to allow a few operations)
const START_GAS: u64 = 60_000;

Expand All @@ -249,7 +268,7 @@ where
let from = req.from.unwrap_or_else(|| Address::zero());
let nonce_fut = match req.nonce {
Some(nonce) => Either::A(future::ok(Some(nonce))),
None => Either::B(self.account(from, id).map(|acc| acc.map(|a| a.nonce))),
None => Either::B(self.account(from, id, txq).map(|acc| acc.map(|a| a.nonce))),
};

let gas_price_fut = match req.gas_price {
Expand Down
12 changes: 6 additions & 6 deletions rpc/src/v1/impls/light/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ where
}

fn balance(&self, address: H160, num: Option<BlockNumber>) -> BoxFuture<U256> {
Box::new(self.fetcher().account(address.into(), num.unwrap_or_default().to_block_id())
.map(|acc| acc.map_or(0.into(), |a| a.balance).into()))
Box::new(self.fetcher().account(address, num.unwrap_or_default().to_block_id(), self.transaction_queue.clone())
.map(|acc| acc.map_or(0.into(), |a| a.balance)))
}

fn storage_at(&self, _address: H160, _key: U256, _num: Option<BlockNumber>) -> BoxFuture<H256> {
Expand All @@ -302,8 +302,8 @@ where
}

fn transaction_count(&self, address: H160, num: Option<BlockNumber>) -> BoxFuture<U256> {
Box::new(self.fetcher().account(address.into(), num.unwrap_or_default().to_block_id())
.map(|acc| acc.map_or(0.into(), |a| a.nonce).into()))
Box::new(self.fetcher().account(address, num.unwrap_or_default().to_block_id(), self.transaction_queue.clone())
.map(|acc| acc.map_or(0.into(), |a| a.nonce)))
}

fn block_transaction_count_by_hash(&self, hash: H256) -> BoxFuture<Option<U256>> {
Expand Down Expand Up @@ -398,7 +398,7 @@ where
}

fn call(&self, req: CallRequest, num: Option<BlockNumber>) -> BoxFuture<Bytes> {
Box::new(self.fetcher().proved_read_only_execution(req, num).and_then(|res| {
Box::new(self.fetcher().proved_read_only_execution(req, num, self.transaction_queue.clone()).and_then(|res| {
match res {
Ok(exec) => Ok(exec.output.into()),
Err(e) => Err(errors::execution(e)),
Expand All @@ -408,7 +408,7 @@ where

fn estimate_gas(&self, req: CallRequest, num: Option<BlockNumber>) -> BoxFuture<U256> {
// TODO: binary chop for more accurate estimates.
Box::new(self.fetcher().proved_read_only_execution(req, num).and_then(|res| {
Box::new(self.fetcher().proved_read_only_execution(req, num, self.transaction_queue.clone()).and_then(|res| {
match res {
Ok(exec) => Ok((exec.refunded + exec.gas_used).into()),
Err(e) => Err(errors::execution(e)),
Expand Down

0 comments on commit 449a782

Please sign in to comment.