transaction_pool/replace.rs
1// Copyright 2020 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! When queue limits are reached, decide whether to replace an existing transaction from the pool
10
11use crate::{pool::Transaction, scoring::Choice};
12
13/// Encapsulates a transaction to be compared, along with pooled transactions from the same sender
14pub struct ReplaceTransaction<'a, T> {
15 /// The transaction to be compared for replacement
16 pub transaction: &'a Transaction<T>,
17 /// Other transactions currently in the pool for the same sender
18 pub pooled_by_sender: Option<&'a [Transaction<T>]>,
19}
20
21impl<'a, T> ReplaceTransaction<'a, T> {
22 /// Creates a new `ReplaceTransaction`
23 pub fn new(transaction: &'a Transaction<T>, pooled_by_sender: Option<&'a [Transaction<T>]>) -> Self {
24 ReplaceTransaction { transaction, pooled_by_sender }
25 }
26}
27
28impl<'a, T> ::std::ops::Deref for ReplaceTransaction<'a, T> {
29 type Target = Transaction<T>;
30 fn deref(&self) -> &Self::Target {
31 &self.transaction
32 }
33}
34
35/// Chooses whether a new transaction should replace an existing transaction if the pool is full.
36pub trait ShouldReplace<T> {
37 /// Decides if `new` should push out `old` transaction from the pool.
38 ///
39 /// NOTE returning `InsertNew` here can lead to some transactions being accepted above pool limits.
40 fn should_replace(&self, old: &ReplaceTransaction<'_, T>, new: &ReplaceTransaction<'_, T>) -> Choice;
41}