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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use parking_lot::RwLock;
use std::{collections::HashMap, hash, iter, time::Duration};
use wasm_timer::Instant;
use super::base_pool::Transaction;
const EXPECTED_SIZE: usize = 2048;
pub struct PoolRotator<Hash> {
ban_time: Duration,
banned_until: RwLock<HashMap<Hash, Instant>>,
}
impl<Hash: hash::Hash + Eq> Default for PoolRotator<Hash> {
fn default() -> Self {
Self { ban_time: Duration::from_secs(60 * 30), banned_until: Default::default() }
}
}
impl<Hash: hash::Hash + Eq + Clone> PoolRotator<Hash> {
pub fn is_banned(&self, hash: &Hash) -> bool {
self.banned_until.read().contains_key(hash)
}
pub fn ban(&self, now: &Instant, hashes: impl IntoIterator<Item = Hash>) {
let mut banned = self.banned_until.write();
for hash in hashes {
banned.insert(hash, *now + self.ban_time);
}
if banned.len() > 2 * EXPECTED_SIZE {
while banned.len() > EXPECTED_SIZE {
if let Some(key) = banned.keys().next().cloned() {
banned.remove(&key);
}
}
}
}
pub fn ban_if_stale<Ex>(
&self,
now: &Instant,
current_block: u64,
xt: &Transaction<Hash, Ex>,
) -> bool {
if xt.valid_till > current_block {
return false
}
self.ban(now, iter::once(xt.hash.clone()));
true
}
pub fn clear_timeouts(&self, now: &Instant) {
let mut banned = self.banned_until.write();
banned.retain(|_, &mut v| v >= *now);
}
}
#[cfg(test)]
mod tests {
use super::*;
use sp_runtime::transaction_validity::TransactionSource;
type Hash = u64;
type Ex = ();
fn rotator() -> PoolRotator<Hash> {
PoolRotator { ban_time: Duration::from_millis(10), ..Default::default() }
}
fn tx() -> (Hash, Transaction<Hash, Ex>) {
let hash = 5u64;
let tx = Transaction {
data: (),
bytes: 1,
hash: hash.clone(),
priority: 5,
valid_till: 1,
requires: vec![],
provides: vec![],
propagate: true,
source: TransactionSource::External,
};
(hash, tx)
}
#[test]
fn should_not_ban_if_not_stale() {
let (hash, tx) = tx();
let rotator = rotator();
assert!(!rotator.is_banned(&hash));
let now = Instant::now();
let past_block = 0;
assert!(!rotator.ban_if_stale(&now, past_block, &tx));
assert!(!rotator.is_banned(&hash));
}
#[test]
fn should_ban_stale_extrinsic() {
let (hash, tx) = tx();
let rotator = rotator();
assert!(!rotator.is_banned(&hash));
assert!(rotator.ban_if_stale(&Instant::now(), 1, &tx));
assert!(rotator.is_banned(&hash));
}
#[test]
fn should_clear_banned() {
let (hash, tx) = tx();
let rotator = rotator();
assert!(rotator.ban_if_stale(&Instant::now(), 1, &tx));
assert!(rotator.is_banned(&hash));
let future = Instant::now() + rotator.ban_time + rotator.ban_time;
rotator.clear_timeouts(&future);
assert!(!rotator.is_banned(&hash));
}
#[test]
fn should_garbage_collect() {
fn tx_with(i: u64, valid_till: u64) -> Transaction<Hash, Ex> {
let hash = i;
Transaction {
data: (),
bytes: 2,
hash,
priority: 5,
valid_till,
requires: vec![],
provides: vec![],
propagate: true,
source: TransactionSource::External,
}
}
let rotator = rotator();
let now = Instant::now();
let past_block = 0;
for i in 0..2 * EXPECTED_SIZE {
let tx = tx_with(i as u64, past_block);
assert!(rotator.ban_if_stale(&now, past_block, &tx));
}
assert_eq!(rotator.banned_until.read().len(), 2 * EXPECTED_SIZE);
let tx = tx_with(2 * EXPECTED_SIZE as u64, past_block);
assert!(rotator.ban_if_stale(&now, past_block, &tx));
assert_eq!(rotator.banned_until.read().len(), EXPECTED_SIZE);
}
}