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

fix(light account response): update tx_queue on every account request/response #10545

Merged
merged 1 commit into from
Mar 31, 2019
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
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,12 +40,13 @@ 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 @@ -100,7 +101,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 @@ -215,7 +216,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 @@ -224,14 +231,26 @@ where

reqs.push(request::Account { header: header_ref, 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 @@ -254,7 +273,7 @@ where
let from = req.from.unwrap_or_default();
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
8 changes: 4 additions & 4 deletions rpc/src/v1/impls/light/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ where
}

fn balance(&self, address: H160, num: Option<BlockNumber>) -> BoxFuture<U256> {
Box::new(self.fetcher().account(address, num.unwrap_or_default().to_block_id())
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)))
}

Expand All @@ -305,7 +305,7 @@ where
}

fn transaction_count(&self, address: H160, num: Option<BlockNumber>) -> BoxFuture<U256> {
Box::new(self.fetcher().account(address, num.unwrap_or_default().to_block_id())
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)))
}

Expand Down Expand Up @@ -401,7 +401,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 @@ -411,7 +411,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),
Err(e) => Err(errors::execution(e)),
Expand Down