referrerpolicy=no-referrer-when-downgrade

sc_mixnet/
extrinsic_queue.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! [`ExtrinsicQueue`] is a queue for extrinsics received from the mixnet. These extrinsics are
20//! explicitly delayed by a random amount, to decorrelate the times at which they are received from
21//! the times at which they are broadcast to peers.
22
23use mixnet::reply_manager::ReplyContext;
24use std::{cmp::Ordering, collections::BinaryHeap, time::Instant};
25
26/// An extrinsic that should be submitted to the transaction pool after `deadline`. `Eq` and `Ord`
27/// are implemented for this to support use in `BinaryHeap`s. Only `deadline` is compared.
28struct DelayedExtrinsic<E> {
29	/// When the extrinsic should actually be submitted to the pool.
30	deadline: Instant,
31	extrinsic: E,
32	reply_context: ReplyContext,
33}
34
35impl<E> PartialEq for DelayedExtrinsic<E> {
36	fn eq(&self, other: &Self) -> bool {
37		self.deadline == other.deadline
38	}
39}
40
41impl<E> Eq for DelayedExtrinsic<E> {}
42
43impl<E> PartialOrd for DelayedExtrinsic<E> {
44	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
45		Some(self.cmp(other))
46	}
47}
48
49impl<E> Ord for DelayedExtrinsic<E> {
50	fn cmp(&self, other: &Self) -> Ordering {
51		// Extrinsics with the earliest deadline considered greatest
52		self.deadline.cmp(&other.deadline).reverse()
53	}
54}
55
56pub struct ExtrinsicQueue<E> {
57	capacity: usize,
58	queue: BinaryHeap<DelayedExtrinsic<E>>,
59	next_deadline_changed: bool,
60}
61
62impl<E> ExtrinsicQueue<E> {
63	pub fn new(capacity: usize) -> Self {
64		Self { capacity, queue: BinaryHeap::with_capacity(capacity), next_deadline_changed: false }
65	}
66
67	pub fn next_deadline(&self) -> Option<Instant> {
68		self.queue.peek().map(|extrinsic| extrinsic.deadline)
69	}
70
71	pub fn next_deadline_changed(&mut self) -> bool {
72		let changed = self.next_deadline_changed;
73		self.next_deadline_changed = false;
74		changed
75	}
76
77	pub fn has_space(&self) -> bool {
78		self.queue.len() < self.capacity
79	}
80
81	pub fn insert(&mut self, deadline: Instant, extrinsic: E, reply_context: ReplyContext) {
82		debug_assert!(self.has_space());
83		let prev_deadline = self.next_deadline();
84		self.queue.push(DelayedExtrinsic { deadline, extrinsic, reply_context });
85		if self.next_deadline() != prev_deadline {
86			self.next_deadline_changed = true;
87		}
88	}
89
90	pub fn pop(&mut self) -> Option<(E, ReplyContext)> {
91		self.next_deadline_changed = true;
92		self.queue.pop().map(|extrinsic| (extrinsic.extrinsic, extrinsic.reply_context))
93	}
94}