This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Optimize pending transactions filter #9026
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
52eb8a1
rpc: return unordered transactions in pending transactions filter
andresilva 473791d
ethcore: use LruCache for nonce cache
andresilva d5fff50
Revert "ethcore: use LruCache for nonce cache"
tomusdrw a44c271
Use only cached nonces when computing pending hashes.
tomusdrw 30857cf
Give filters their own locks, so that they don't block one another.
tomusdrw cd7a6c7
Fix pending transaction count if not sealing.
tomusdrw cad3a1e
Clear cache only when block is enacted.
tomusdrw 656d03d
Merge branch 'master' into andre/optimize-pending-set-filter
tomusdrw bbf7997
Fix RPC tests.
tomusdrw a02d32a
Merge branch 'master' into andre/optimize-pending-set-filter
tomusdrw 7a504ab
Address review comments.
tomusdrw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
// along with Parity. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use std::time::{Instant, Duration}; | ||
use std::collections::{BTreeMap, HashSet, HashMap}; | ||
use std::collections::{BTreeMap, BTreeSet, HashSet, HashMap}; | ||
use std::sync::Arc; | ||
|
||
use ansi_term::Colour; | ||
|
@@ -851,6 +851,37 @@ impl miner::MinerService for Miner { | |
self.transaction_queue.all_transactions() | ||
} | ||
|
||
fn pending_transactions_hashes<C>(&self, chain: &C) -> BTreeSet<H256> where | ||
C: ChainInfo + Sync, | ||
{ | ||
let chain_info = chain.chain_info(); | ||
|
||
let from_queue = || self.transaction_queue.pending_hashes( | ||
|sender| self.nonce_cache.read().get(sender).cloned(), | ||
); | ||
|
||
let from_pending = || { | ||
self.map_existing_pending_block(|sealing| { | ||
sealing.transactions() | ||
.iter() | ||
.map(|signed| signed.hash()) | ||
.collect() | ||
}, chain_info.best_block_number) | ||
}; | ||
|
||
match self.options.pending_set { | ||
PendingSet::AlwaysQueue => { | ||
from_queue() | ||
}, | ||
PendingSet::AlwaysSealing => { | ||
from_pending().unwrap_or_default() | ||
}, | ||
PendingSet::SealingOrElseQueue => { | ||
from_pending().unwrap_or_else(from_queue) | ||
}, | ||
} | ||
} | ||
|
||
fn ready_transactions<C>(&self, chain: &C, max_len: usize, ordering: miner::PendingOrdering) | ||
-> Vec<Arc<VerifiedTransaction>> | ||
where | ||
|
@@ -1065,13 +1096,18 @@ impl miner::MinerService for Miner { | |
// 2. We ignore blocks that are `invalid` because it doesn't have any meaning in terms of the transactions that | ||
// are in those blocks | ||
|
||
// Clear nonce cache | ||
self.nonce_cache.write().clear(); | ||
let has_new_best_block = enacted.len() > 0; | ||
|
||
if has_new_best_block { | ||
// Clear nonce cache | ||
self.nonce_cache.write().clear(); | ||
} | ||
|
||
// First update gas limit in transaction queue and minimal gas price. | ||
let gas_limit = *chain.best_block_header().gas_limit(); | ||
self.update_transaction_queue_limits(gas_limit); | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stray newline? |
||
// Then import all transactions... | ||
let client = self.pool_client(chain); | ||
{ | ||
|
@@ -1091,10 +1127,12 @@ impl miner::MinerService for Miner { | |
}); | ||
} | ||
|
||
// ...and at the end remove the old ones | ||
self.transaction_queue.cull(client); | ||
if has_new_best_block { | ||
// ...and at the end remove the old ones | ||
self.transaction_queue.cull(client); | ||
} | ||
|
||
if enacted.len() > 0 || (imported.len() > 0 && self.options.reseal_on_uncle) { | ||
if has_new_best_block || (imported.len() > 0 && self.options.reseal_on_uncle) { | ||
// Reset `next_allowed_reseal` in case a block is imported. | ||
// Even if min_period is high, we will always attempt to create | ||
// new pending block. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,6 +129,38 @@ impl txpool::Ready<VerifiedTransaction> for Condition { | |
} | ||
} | ||
|
||
pub struct OptionalState<C> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add some docs here? "Checks readiness of transactions by comparing the nonce to state nonce. If nonce isn't found in provided state nonce store, defaults to the tx nonce and updates the nonce store. Useful for using with a state nonce cache when false positives are allowed." ? |
||
nonces: HashMap<Address, U256>, | ||
state: C, | ||
} | ||
|
||
impl<C> OptionalState<C> { | ||
pub fn new(state: C) -> Self { | ||
OptionalState { | ||
nonces: Default::default(), | ||
state, | ||
} | ||
} | ||
} | ||
|
||
impl<C: Fn(&Address) -> Option<U256>> txpool::Ready<VerifiedTransaction> for OptionalState<C> { | ||
fn is_ready(&mut self, tx: &VerifiedTransaction) -> txpool::Readiness { | ||
let sender = tx.sender(); | ||
let state = &self.state; | ||
let nonce = self.nonces.entry(*sender).or_insert_with(|| { | ||
state(sender).unwrap_or_else(|| tx.transaction.nonce) | ||
}); | ||
match tx.transaction.nonce.cmp(nonce) { | ||
cmp::Ordering::Greater => txpool::Readiness::Future, | ||
cmp::Ordering::Less => txpool::Readiness::Stale, | ||
cmp::Ordering::Equal => { | ||
*nonce = *nonce + 1.into(); | ||
txpool::Readiness::Ready | ||
}, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,19 +16,41 @@ | |
|
||
//! Helper type with all filter state data. | ||
|
||
use std::collections::HashSet; | ||
use std::{ | ||
collections::{BTreeSet, HashSet}, | ||
sync::Arc, | ||
}; | ||
use ethereum_types::H256; | ||
use parking_lot::Mutex; | ||
use v1::types::{Filter, Log}; | ||
|
||
pub type BlockNumber = u64; | ||
|
||
/// Thread-safe filter state. | ||
#[derive(Clone)] | ||
pub struct SyncPollFilter(Arc<Mutex<PollFilter>>); | ||
|
||
impl SyncPollFilter { | ||
/// New `SyncPollFilter` | ||
pub fn new(f: PollFilter) -> Self { | ||
SyncPollFilter(Arc::new(Mutex::new(f))) | ||
} | ||
|
||
/// Modify underlying filter | ||
pub fn modify<F, R>(&self, f: F) -> R where | ||
F: FnOnce(&mut PollFilter) -> R, | ||
{ | ||
f(&mut self.0.lock()) | ||
} | ||
} | ||
|
||
/// Filter state. | ||
#[derive(Clone)] | ||
pub enum PollFilter { | ||
/// Number of last block which client was notified about. | ||
Block(BlockNumber), | ||
/// Hashes of all transactions which client was notified about. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
PendingTransaction(Vec<H256>), | ||
PendingTransaction(BTreeSet<H256>), | ||
/// Number of From block number, last seen block hash, pending logs and log filter itself. | ||
Logs(BlockNumber, Option<H256>, HashSet<Log>, Filter) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be
pending_transaction_hashes
(no double plurals
), see e.g. this.(I am not a native speaker so take it with a grain of salt)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked and we're having both usages. This probably should be unified to a single one in the future:
eth_filter
we havepending_transactions_hashes
.ethcore
's Block's views andencoded
, we havetransaction_hashes
.