referrerpolicy=no-referrer-when-downgrade

sc_transaction_pool/graph/
listener.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::{collections::HashMap, fmt::Debug, hash};

use linked_hash_map::LinkedHashMap;
use log::trace;

use super::{watcher, BlockHash, ChainApi, ExtrinsicHash};

static LOG_TARGET: &str = "txpool::watcher";

/// The `EventHandler` trait provides a mechanism for clients to respond to various
/// transaction-related events. It offers a set of callback methods that are invoked by the
/// transaction pool's event dispatcher to notify about changes in the status of transactions.
///
/// This trait can be implemented by any component that needs to respond to transaction lifecycle
/// events, enabling custom logic and handling of these events.
pub trait EventHandler<C: ChainApi> {
	/// Called when a transaction is broadcasted.
	fn broadcasted(&self, _hash: ExtrinsicHash<C>, _peers: Vec<String>) {}

	/// Called when a transaction is ready for execution.
	fn ready(&self, _tx: ExtrinsicHash<C>) {}

	/// Called when a transaction is deemed to be executable in the future.
	fn future(&self, _tx: ExtrinsicHash<C>) {}

	/// Called when transaction pool limits result in a transaction being affected.
	fn limits_enforced(&self, _tx: ExtrinsicHash<C>) {}

	/// Called when a transaction is replaced by another.
	fn usurped(&self, _tx: ExtrinsicHash<C>, _by: ExtrinsicHash<C>) {}

	/// Called when a transaction is dropped from the pool.
	fn dropped(&self, _tx: ExtrinsicHash<C>) {}

	/// Called when a transaction is found to be invalid.
	fn invalid(&self, _tx: ExtrinsicHash<C>) {}

	/// Called when a transaction was pruned from the pool due to its presence in imported block.
	fn pruned(&self, _tx: ExtrinsicHash<C>, _block_hash: BlockHash<C>, _tx_index: usize) {}

	/// Called when a transaction is retracted from inclusion in a block.
	fn retracted(&self, _tx: ExtrinsicHash<C>, _block_hash: BlockHash<C>) {}

	/// Called when a transaction has not been finalized within a timeout period.
	fn finality_timeout(&self, _tx: ExtrinsicHash<C>, _hash: BlockHash<C>) {}

	/// Called when a transaction is finalized in a block.
	fn finalized(&self, _tx: ExtrinsicHash<C>, _block_hash: BlockHash<C>, _tx_index: usize) {}
}

impl<C: ChainApi> EventHandler<C> for () {}

/// The `EventDispatcher` struct is responsible for dispatching transaction-related events from the
/// validated pool to interested observers and an optional event handler. It acts as the primary
/// liaison between the transaction pool and clients that are monitoring transaction statuses.
pub struct EventDispatcher<H: hash::Hash + Eq, C: ChainApi, L: EventHandler<C>> {
	/// Map containing per-transaction sinks for emitting transaction status events.
	watchers: HashMap<H, watcher::Sender<H, BlockHash<C>>>,
	finality_watchers: LinkedHashMap<ExtrinsicHash<C>, Vec<H>>,

	/// Optional event handler (listener) that will be notified about all transactions status
	/// changes from the pool.
	event_handler: Option<L>,
}

/// Maximum number of blocks awaiting finality at any time.
const MAX_FINALITY_WATCHERS: usize = 512;

impl<H: hash::Hash + Eq + Debug, C: ChainApi, L: EventHandler<C>> Default
	for EventDispatcher<H, C, L>
{
	fn default() -> Self {
		Self {
			watchers: Default::default(),
			finality_watchers: Default::default(),
			event_handler: None,
		}
	}
}

impl<C: ChainApi, L: EventHandler<C>> EventDispatcher<ExtrinsicHash<C>, C, L> {
	/// Creates a new instance with provided event handler.
	pub fn new_with_event_handler(event_handler: Option<L>) -> Self {
		Self { event_handler, ..Default::default() }
	}

	fn fire<F>(&mut self, hash: &ExtrinsicHash<C>, fun: F)
	where
		F: FnOnce(&mut watcher::Sender<ExtrinsicHash<C>, ExtrinsicHash<C>>),
	{
		let clean = if let Some(h) = self.watchers.get_mut(hash) {
			fun(h);
			h.is_done()
		} else {
			false
		};

		if clean {
			self.watchers.remove(hash);
		}
	}

	/// Creates a new watcher for given verified extrinsic.
	///
	/// The watcher can be used to subscribe to life-cycle events of that extrinsic.
	pub fn create_watcher(
		&mut self,
		hash: ExtrinsicHash<C>,
	) -> watcher::Watcher<ExtrinsicHash<C>, ExtrinsicHash<C>> {
		let sender = self.watchers.entry(hash).or_insert_with(watcher::Sender::default);
		sender.new_watcher(hash)
	}

	/// Notify the listeners about the extrinsic broadcast.
	pub fn broadcasted(&mut self, hash: &ExtrinsicHash<C>, peers: Vec<String>) {
		trace!(target: LOG_TARGET, "[{:?}] Broadcasted", hash);
		self.fire(hash, |watcher| watcher.broadcast(peers.clone()));
		self.event_handler.as_ref().map(|l| l.broadcasted(*hash, peers));
	}

	/// New transaction was added to the ready pool or promoted from the future pool.
	pub fn ready(&mut self, tx: &ExtrinsicHash<C>, old: Option<&ExtrinsicHash<C>>) {
		trace!(target: LOG_TARGET, "[{:?}] Ready (replaced with {:?})", *tx, old);
		self.fire(tx, |watcher| watcher.ready());
		if let Some(old) = old {
			self.fire(old, |watcher| watcher.usurped(*tx));
		}

		self.event_handler.as_ref().map(|l| l.ready(*tx));
	}

	/// New transaction was added to the future pool.
	pub fn future(&mut self, tx: &ExtrinsicHash<C>) {
		trace!(target: LOG_TARGET, "[{:?}] Future", tx);
		self.fire(tx, |watcher| watcher.future());

		self.event_handler.as_ref().map(|l| l.future(*tx));
	}

	/// Transaction was dropped from the pool because of enforcing the limit.
	pub fn limits_enforced(&mut self, tx: &ExtrinsicHash<C>) {
		trace!(target: LOG_TARGET, "[{:?}] Dropped (limits enforced)", tx);
		self.fire(tx, |watcher| watcher.limit_enforced());

		self.event_handler.as_ref().map(|l| l.limits_enforced(*tx));
	}

	/// Transaction was replaced with other extrinsic.
	pub fn usurped(&mut self, tx: &ExtrinsicHash<C>, by: &ExtrinsicHash<C>) {
		trace!(target: LOG_TARGET, "[{:?}] Dropped (replaced with {:?})", tx, by);
		self.fire(tx, |watcher| watcher.usurped(*by));

		self.event_handler.as_ref().map(|l| l.usurped(*tx, *by));
	}

	/// Transaction was dropped from the pool because of the failure during the resubmission of
	/// revalidate transactions or failure during pruning tags.
	pub fn dropped(&mut self, tx: &ExtrinsicHash<C>) {
		trace!(target: LOG_TARGET, "[{:?}] Dropped", tx);
		self.fire(tx, |watcher| watcher.dropped());
		self.event_handler.as_ref().map(|l| l.dropped(*tx));
	}

	/// Transaction was removed as invalid.
	pub fn invalid(&mut self, tx: &ExtrinsicHash<C>) {
		trace!(target: LOG_TARGET, "[{:?}] Extrinsic invalid", tx);
		self.fire(tx, |watcher| watcher.invalid());
		self.event_handler.as_ref().map(|l| l.invalid(*tx));
	}

	/// Transaction was pruned from the pool.
	pub fn pruned(&mut self, block_hash: BlockHash<C>, tx: &ExtrinsicHash<C>) {
		trace!(target: LOG_TARGET, "[{:?}] Pruned at {:?}", tx, block_hash);
		// Get the transactions included in the given block hash.
		let txs = self.finality_watchers.entry(block_hash).or_insert(vec![]);
		txs.push(*tx);
		// Current transaction is the last one included.
		let tx_index = txs.len() - 1;

		self.fire(tx, |watcher| watcher.in_block(block_hash, tx_index));
		self.event_handler.as_ref().map(|l| l.pruned(*tx, block_hash, tx_index));

		while self.finality_watchers.len() > MAX_FINALITY_WATCHERS {
			if let Some((hash, txs)) = self.finality_watchers.pop_front() {
				for tx in txs {
					self.fire(&tx, |watcher| watcher.finality_timeout(hash));
					self.event_handler.as_ref().map(|l| l.finality_timeout(tx, block_hash));
				}
			}
		}
	}

	/// The block this transaction was included in has been retracted.
	pub fn retracted(&mut self, block_hash: BlockHash<C>) {
		if let Some(hashes) = self.finality_watchers.remove(&block_hash) {
			for hash in hashes {
				self.fire(&hash, |watcher| watcher.retracted(block_hash));
				self.event_handler.as_ref().map(|l| l.retracted(hash, block_hash));
			}
		}
	}

	/// Notify all watchers that transactions have been finalized
	pub fn finalized(&mut self, block_hash: BlockHash<C>) {
		if let Some(hashes) = self.finality_watchers.remove(&block_hash) {
			for (tx_index, hash) in hashes.into_iter().enumerate() {
				log::trace!(
					target: LOG_TARGET,
					"[{:?}] Sent finalization event (block {:?})",
					hash,
					block_hash,
				);
				self.fire(&hash, |watcher| watcher.finalized(block_hash, tx_index));
				self.event_handler.as_ref().map(|l| l.finalized(hash, block_hash, tx_index));
			}
		}
	}

	/// Provides hashes of all watched transactions.
	pub fn watched_transactions(&self) -> impl Iterator<Item = &ExtrinsicHash<C>> {
		self.watchers.keys()
	}
}