2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-11-16 10:45:55 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-11-16 10:45:55 +01:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2016-11-16 10:45:55 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-11-16 10:45:55 +01:00
|
|
|
|
2016-11-16 13:37:21 +01:00
|
|
|
use api::TransactionStats;
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{H256, H512};
|
2018-08-09 09:51:48 +02:00
|
|
|
use fastmap::H256FastMap;
|
2016-11-16 10:45:55 +01:00
|
|
|
use std::{
|
|
|
|
collections::{HashMap, HashSet},
|
2018-11-28 11:30:05 +01:00
|
|
|
hash::BuildHasher,
|
2020-08-05 06:08:03 +02:00
|
|
|
};
|
2019-01-04 14:05:46 +01:00
|
|
|
use types::BlockNumber;
|
2016-11-16 10:45:55 +01:00
|
|
|
|
|
|
|
type NodeId = H512;
|
|
|
|
|
2016-11-16 13:37:21 +01:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2016-11-16 10:45:55 +01:00
|
|
|
pub struct Stats {
|
|
|
|
first_seen: BlockNumber,
|
|
|
|
propagated_to: HashMap<NodeId, usize>,
|
|
|
|
}
|
|
|
|
|
2016-11-16 13:37:21 +01:00
|
|
|
impl Stats {
|
|
|
|
pub fn new(number: BlockNumber) -> Self {
|
|
|
|
Stats {
|
|
|
|
first_seen: number,
|
|
|
|
propagated_to: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a Stats> for TransactionStats {
|
|
|
|
fn from(other: &'a Stats) -> Self {
|
|
|
|
TransactionStats {
|
|
|
|
first_seen: other.first_seen,
|
|
|
|
propagated_to: other
|
|
|
|
.propagated_to
|
|
|
|
.iter()
|
|
|
|
.map(|(hash, size)| (*hash, *size))
|
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 10:45:55 +01:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct TransactionsStats {
|
|
|
|
pending_transactions: H256FastMap<Stats>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TransactionsStats {
|
|
|
|
/// Increases number of propagations to given `enodeid`.
|
2017-06-30 11:57:48 +02:00
|
|
|
pub fn propagated(
|
|
|
|
&mut self,
|
|
|
|
hash: &H256,
|
|
|
|
enode_id: Option<NodeId>,
|
|
|
|
current_block_num: BlockNumber,
|
|
|
|
) {
|
2016-11-16 10:45:55 +01:00
|
|
|
let enode_id = enode_id.unwrap_or_default();
|
2017-10-15 15:11:20 +02:00
|
|
|
let stats = self
|
|
|
|
.pending_transactions
|
|
|
|
.entry(*hash)
|
|
|
|
.or_insert_with(|| Stats::new(current_block_num));
|
|
|
|
let count = stats.propagated_to.entry(enode_id).or_insert(0);
|
2016-11-16 10:45:55 +01:00
|
|
|
*count = count.saturating_add(1);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-16 10:45:55 +01:00
|
|
|
/// Returns propagation stats for given hash or `None` if hash is not known.
|
2016-11-16 13:37:21 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
pub fn get(&self, hash: &H256) -> Option<&Stats> {
|
2016-11-16 10:45:55 +01:00
|
|
|
self.pending_transactions.get(hash)
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-16 13:37:21 +01:00
|
|
|
pub fn stats(&self) -> &H256FastMap<Stats> {
|
|
|
|
&self.pending_transactions
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-16 10:45:55 +01:00
|
|
|
/// Retains only transactions present in given `HashSet`.
|
2018-11-28 11:30:05 +01:00
|
|
|
pub fn retain<S: BuildHasher>(&mut self, hashes: &HashSet<H256, S>) {
|
2016-11-16 10:45:55 +01:00
|
|
|
let to_remove = self
|
|
|
|
.pending_transactions
|
|
|
|
.keys()
|
|
|
|
.filter(|hash| !hashes.contains(hash))
|
|
|
|
.cloned()
|
|
|
|
.collect::<Vec<_>>();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-16 10:45:55 +01:00
|
|
|
for hash in to_remove {
|
|
|
|
self.pending_transactions.remove(&hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::{Stats, TransactionsStats};
|
|
|
|
use std::collections::{HashMap, HashSet};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-16 10:45:55 +01:00
|
|
|
#[test]
|
|
|
|
fn should_keep_track_of_propagations() {
|
|
|
|
// given
|
|
|
|
let mut stats = TransactionsStats::default();
|
|
|
|
let hash = 5.into();
|
|
|
|
let enodeid1 = 2.into();
|
|
|
|
let enodeid2 = 5.into();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-16 10:45:55 +01:00
|
|
|
// when
|
2017-06-30 11:57:48 +02:00
|
|
|
stats.propagated(&hash, Some(enodeid1), 5);
|
|
|
|
stats.propagated(&hash, Some(enodeid1), 10);
|
|
|
|
stats.propagated(&hash, Some(enodeid2), 15);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-16 10:45:55 +01:00
|
|
|
// then
|
2016-11-16 13:37:21 +01:00
|
|
|
let stats = stats.get(&hash);
|
2016-11-16 10:45:55 +01:00
|
|
|
assert_eq!(
|
|
|
|
stats,
|
|
|
|
Some(&Stats {
|
2016-11-16 13:37:21 +01:00
|
|
|
first_seen: 5,
|
2016-11-16 10:45:55 +01:00
|
|
|
propagated_to: hash_map![
|
|
|
|
enodeid1 => 2,
|
|
|
|
enodeid2 => 1
|
2016-12-10 14:56:41 +01:00
|
|
|
],
|
2016-11-16 10:45:55 +01:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-16 10:45:55 +01:00
|
|
|
#[test]
|
|
|
|
fn should_remove_hash_from_tracking() {
|
|
|
|
// given
|
|
|
|
let mut stats = TransactionsStats::default();
|
|
|
|
let hash = 5.into();
|
|
|
|
let enodeid1 = 5.into();
|
2017-06-30 11:57:48 +02:00
|
|
|
stats.propagated(&hash, Some(enodeid1), 10);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-16 10:45:55 +01:00
|
|
|
// when
|
|
|
|
stats.retain(&HashSet::new());
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-16 10:45:55 +01:00
|
|
|
// then
|
2016-11-16 13:37:21 +01:00
|
|
|
let stats = stats.get(&hash);
|
2016-11-16 10:45:55 +01:00
|
|
|
assert_eq!(stats, None);
|
|
|
|
}
|
|
|
|
}
|