transaction_pool/
listener.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
9use crate::error::Error;
10use std::{
11	fmt::{Debug, LowerHex},
12	sync::Arc,
13};
14
15/// Transaction pool listener.
16///
17/// Listener is being notified about status of every transaction in the pool.
18pub trait Listener<T> {
19	/// The transaction has been successfully added to the pool.
20	/// If second argument is `Some` the transaction has took place of some other transaction
21	/// which was already in pool.
22	/// NOTE: You won't be notified about drop of `old` transaction separately.
23	fn added(&mut self, _tx: &Arc<T>, _old: Option<&Arc<T>>) {}
24
25	/// The transaction was rejected from the pool.
26	/// It means that it was too cheap to replace any transaction already in the pool.
27	fn rejected<H: Debug + LowerHex>(&mut self, _tx: &Arc<T>, _reason: &Error<H>) {}
28
29	/// The transaction was pushed out from the pool because of the limit.
30	fn dropped(&mut self, _tx: &Arc<T>, _by: Option<&T>) {}
31
32	/// The transaction was marked as invalid by executor.
33	fn invalid(&mut self, _tx: &Arc<T>) {}
34
35	/// The transaction has been canceled.
36	fn canceled(&mut self, _tx: &Arc<T>) {}
37
38	/// The transaction has been culled from the pool.
39	fn culled(&mut self, _tx: &Arc<T>) {}
40}
41
42/// A no-op implementation of `Listener`.
43#[derive(Debug)]
44pub struct NoopListener;
45impl<T> Listener<T> for NoopListener {}
46
47impl<T, A, B> Listener<T> for (A, B)
48where
49	A: Listener<T>,
50	B: Listener<T>,
51{
52	fn added(&mut self, tx: &Arc<T>, old: Option<&Arc<T>>) {
53		self.0.added(tx, old);
54		self.1.added(tx, old);
55	}
56
57	fn rejected<H: Debug + LowerHex>(&mut self, tx: &Arc<T>, reason: &Error<H>) {
58		self.0.rejected(tx, reason);
59		self.1.rejected(tx, reason);
60	}
61
62	fn dropped(&mut self, tx: &Arc<T>, by: Option<&T>) {
63		self.0.dropped(tx, by);
64		self.1.dropped(tx, by);
65	}
66
67	fn invalid(&mut self, tx: &Arc<T>) {
68		self.0.invalid(tx);
69		self.1.invalid(tx);
70	}
71
72	fn canceled(&mut self, tx: &Arc<T>) {
73		self.0.canceled(tx);
74		self.1.canceled(tx);
75	}
76
77	fn culled(&mut self, tx: &Arc<T>) {
78		self.0.culled(tx);
79		self.1.culled(tx);
80	}
81}