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

[forwardport] Make sure to ban invalid transactions. (#615) #620

Merged
merged 1 commit into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions substrate/extrinsic-pool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,17 @@ impl<B: ChainApi> Pool<B> {
pub fn remove(&self, hashes: &[B::Hash], is_valid: bool) -> Vec<Option<Arc<VerifiedFor<B>>>> {
let mut pool = self.pool.write();
let mut results = Vec::with_capacity(hashes.len());

// temporarily ban invalid transactions
if !is_valid {
debug!(target: "transaction-pool", "Banning invalid transactions: {:?}", hashes);
self.rotator.ban(&time::Instant::now(), hashes);
}

for hash in hashes {
results.push(pool.remove(hash, is_valid));
}

results
}

Expand Down Expand Up @@ -578,4 +586,21 @@ pub mod tests {
let pending: Vec<_> = pool.cull_and_get_pending(&BlockId::number(0), |p| p.map(|a| (*a.sender(), a.original.transfer.nonce)).collect()).unwrap();
assert_eq!(pending, vec![(Alice.to_raw_public().into(), 209), (Alice.to_raw_public().into(), 210)]);
}

#[test]
fn should_ban_invalid_transactions() {
let pool = pool();
let uxt = uxt(Alice, 209);
let hash = *pool.submit_one(&BlockId::number(0), uxt.clone()).unwrap().hash();
pool.remove(&[hash], true);
pool.submit_one(&BlockId::number(0), uxt.clone()).unwrap();

// when
pool.remove(&[hash], false);
let pending: Vec<AccountId> = pool.cull_and_get_pending(&BlockId::number(0), |p| p.map(|a| *a.sender()).collect()).unwrap();
assert_eq!(pending, vec![]);

// then
pool.submit_one(&BlockId::number(0), uxt.clone()).unwrap_err();
}
}
29 changes: 18 additions & 11 deletions substrate/extrinsic-pool/src/rotator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ impl<Hash: hash::Hash + Eq + Clone> PoolRotator<Hash> {
self.banned_until.read().contains_key(hash)
}

/// Bans given set of hashes.
pub fn ban(&self, now: &Instant, hashes: &[Hash]) {
let mut banned = self.banned_until.write();

for hash in hashes {
banned.insert(hash.clone(), *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);
}
}
}
}

/// Bans extrinsic if it's stale.
///
/// Returns `true` if extrinsic is stale and got banned.
Expand All @@ -69,17 +86,7 @@ impl<Hash: hash::Hash + Eq + Clone> PoolRotator<Hash> {
return false;
}

let mut banned = self.banned_until.write();
banned.insert(xt.verified.hash().clone(), *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);
}
}
}

self.ban(now, &[xt.verified.hash().clone()]);
true
}

Expand Down