Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit f8b6488

Browse files
tomusdrwgavofyork
authored andcommitted
Make sure to ban invalid transactions. (#615) (#620)
1 parent 06519fb commit f8b6488

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

substrate/extrinsic-pool/src/pool.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,17 @@ impl<B: ChainApi> Pool<B> {
262262
pub fn remove(&self, hashes: &[B::Hash], is_valid: bool) -> Vec<Option<Arc<VerifiedFor<B>>>> {
263263
let mut pool = self.pool.write();
264264
let mut results = Vec::with_capacity(hashes.len());
265+
266+
// temporarily ban invalid transactions
267+
if !is_valid {
268+
debug!(target: "transaction-pool", "Banning invalid transactions: {:?}", hashes);
269+
self.rotator.ban(&time::Instant::now(), hashes);
270+
}
271+
265272
for hash in hashes {
266273
results.push(pool.remove(hash, is_valid));
267274
}
275+
268276
results
269277
}
270278

@@ -578,4 +586,21 @@ pub mod tests {
578586
let pending: Vec<_> = pool.cull_and_get_pending(&BlockId::number(0), |p| p.map(|a| (*a.sender(), a.original.transfer.nonce)).collect()).unwrap();
579587
assert_eq!(pending, vec![(Alice.to_raw_public().into(), 209), (Alice.to_raw_public().into(), 210)]);
580588
}
589+
590+
#[test]
591+
fn should_ban_invalid_transactions() {
592+
let pool = pool();
593+
let uxt = uxt(Alice, 209);
594+
let hash = *pool.submit_one(&BlockId::number(0), uxt.clone()).unwrap().hash();
595+
pool.remove(&[hash], true);
596+
pool.submit_one(&BlockId::number(0), uxt.clone()).unwrap();
597+
598+
// when
599+
pool.remove(&[hash], false);
600+
let pending: Vec<AccountId> = pool.cull_and_get_pending(&BlockId::number(0), |p| p.map(|a| *a.sender()).collect()).unwrap();
601+
assert_eq!(pending, vec![]);
602+
603+
// then
604+
pool.submit_one(&BlockId::number(0), uxt.clone()).unwrap_err();
605+
}
581606
}

substrate/extrinsic-pool/src/rotator.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@ impl<Hash: hash::Hash + Eq + Clone> PoolRotator<Hash> {
5858
self.banned_until.read().contains_key(hash)
5959
}
6060

61+
/// Bans given set of hashes.
62+
pub fn ban(&self, now: &Instant, hashes: &[Hash]) {
63+
let mut banned = self.banned_until.write();
64+
65+
for hash in hashes {
66+
banned.insert(hash.clone(), *now + self.ban_time);
67+
}
68+
69+
if banned.len() > 2 * EXPECTED_SIZE {
70+
while banned.len() > EXPECTED_SIZE {
71+
if let Some(key) = banned.keys().next().cloned() {
72+
banned.remove(&key);
73+
}
74+
}
75+
}
76+
}
77+
6178
/// Bans extrinsic if it's stale.
6279
///
6380
/// Returns `true` if extrinsic is stale and got banned.
@@ -69,17 +86,7 @@ impl<Hash: hash::Hash + Eq + Clone> PoolRotator<Hash> {
6986
return false;
7087
}
7188

72-
let mut banned = self.banned_until.write();
73-
banned.insert(xt.verified.hash().clone(), *now + self.ban_time);
74-
75-
if banned.len() > 2 * EXPECTED_SIZE {
76-
while banned.len() > EXPECTED_SIZE {
77-
if let Some(key) = banned.keys().next().cloned() {
78-
banned.remove(&key);
79-
}
80-
}
81-
}
82-
89+
self.ban(now, &[xt.verified.hash().clone()]);
8390
true
8491
}
8592

0 commit comments

Comments
 (0)