1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// 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.

// Parity is distributed in the hope that it will be useful,
// 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
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.

use api::TransactionStats;
use std::collections::{HashSet, HashMap};
use util::{H256, H512};
use util::hash::H256FastMap;

type NodeId = H512;
type BlockNumber = u64;

#[derive(Debug, PartialEq, Clone)]
pub struct Stats {
	first_seen: BlockNumber,
	propagated_to: HashMap<NodeId, usize>,
}

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(),
		}
	}
}

#[derive(Debug, Default)]
pub struct TransactionsStats {
	pending_transactions: H256FastMap<Stats>,
}

impl TransactionsStats {
	/// Increases number of propagations to given `enodeid`.
	pub fn propagated(&mut self, hash: H256, enode_id: Option<NodeId>, current_block_num: BlockNumber) {
		let enode_id = enode_id.unwrap_or_default();
		let mut stats = self.pending_transactions.entry(hash).or_insert_with(|| Stats::new(current_block_num));
		let mut count = stats.propagated_to.entry(enode_id).or_insert(0);
		*count = count.saturating_add(1);
	}

	/// Returns propagation stats for given hash or `None` if hash is not known.
	#[cfg(test)]
	pub fn get(&self, hash: &H256) -> Option<&Stats> {
		self.pending_transactions.get(hash)
	}

	pub fn stats(&self) -> &H256FastMap<Stats> {
		&self.pending_transactions
	}

	/// Retains only transactions present in given `HashSet`.
	pub fn retain(&mut self, hashes: &HashSet<H256>) {
		let to_remove = self.pending_transactions.keys()
			.filter(|hash| !hashes.contains(hash))
			.cloned()
			.collect::<Vec<_>>();

		for hash in to_remove {
			self.pending_transactions.remove(&hash);
		}
	}
}

#[cfg(test)]
mod tests {

	use std::collections::{HashMap, HashSet};
	use super::{Stats, TransactionsStats};

	#[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();

		// when
		stats.propagated(hash, Some(enodeid1), 5);
		stats.propagated(hash, Some(enodeid1), 10);
		stats.propagated(hash, Some(enodeid2), 15);

		// then
		let stats = stats.get(&hash);
		assert_eq!(stats, Some(&Stats {
			first_seen: 5,
			propagated_to: hash_map![
				enodeid1 => 2,
				enodeid2 => 1
			],
		}));
	}

	#[test]
	fn should_remove_hash_from_tracking() {
		// given
		let mut stats = TransactionsStats::default();
		let hash = 5.into();
		let enodeid1 = 5.into();
		stats.propagated(hash, Some(enodeid1), 10);

		// when
		stats.retain(&HashSet::new());

		// then
		let stats = stats.get(&hash);
		assert_eq!(stats, None);
	}
}