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

Commit

Permalink
Make sure to ban invalid transactions. (#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomusdrw authored and gavofyork committed Aug 28, 2018
1 parent 452726a commit 878ca8e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
24 changes: 24 additions & 0 deletions polkadot/transaction-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ A: PolkadotApi,

/// Remove a set of transactions idenitified by hashes.
pub fn remove(&self, hashes: &[Hash], is_valid: bool) -> Vec<Option<Arc<VerifiedTransaction>>> {
// temporarily ban invalid transactions
if !is_valid {
debug!(target: "transaction-pool", "Banning invalid transactions: {:?}", hashes);
self.rotator.ban(&Instant::now(), hashes);
}

self.inner.remove(hashes, is_valid)
}
}
Expand Down Expand Up @@ -763,6 +769,24 @@ mod tests {

let pending: Vec<_> = pool.cull_and_get_pending(BlockId::number(1), |p| p.map(|a| (a.sender(), a.index())).collect()).unwrap();
assert_eq!(pending, vec![]);
}

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

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

// then
pool.import_unchecked_extrinsic(BlockId::number(0), uxt.clone()).unwrap_err();
}

}
24 changes: 16 additions & 8 deletions polkadot/transaction-pool/src/rotator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,32 @@ impl PoolRotator {
self.banned_until.read().contains_key(hash)
}

/// Bans extrinsic if it's stale.
///
/// Returns `true` if extrinsic is stale and got banned.
pub fn ban_if_stale(&self, now: &Instant, tx: &VerifiedTransaction) -> bool {
if &tx.valid_till > now {
return false;
/// 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, *now + self.ban_time);
}

let mut banned = self.banned_until.write();
banned.insert(*tx.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);
}
}
}
}

/// Bans extrinsic if it's stale.
///
/// Returns `true` if extrinsic is stale and got banned.
pub fn ban_if_stale(&self, now: &Instant, tx: &VerifiedTransaction) -> bool {
if &tx.valid_till > now {
return false;
}

self.ban(now, &[*tx.hash()]);
true
}

Expand Down

0 comments on commit 878ca8e

Please sign in to comment.