referrerpolicy=no-referrer-when-downgrade

sc_consensus_grandpa/
authorities.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//! Utilities for dealing with authorities, authority sets, and handoffs.
20
21use std::{cmp::Ord, fmt::Debug, ops::Add};
22
23use codec::{Decode, Encode};
24use finality_grandpa::voter_set::VoterSet;
25use fork_tree::{FilterAction, ForkTree};
26use log::debug;
27use parking_lot::MappedMutexGuard;
28use sc_consensus::shared_data::{SharedData, SharedDataLocked};
29use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_INFO};
30use sp_consensus_grandpa::{AuthorityId, AuthorityList};
31
32use crate::{SetId, LOG_TARGET};
33
34/// Error type returned on operations on the `AuthoritySet`.
35#[derive(Debug, thiserror::Error)]
36pub enum Error<N, E> {
37	#[error("Invalid authority set, either empty or with an authority weight set to 0.")]
38	InvalidAuthoritySet,
39	#[error("Client error during ancestry lookup: {0}")]
40	Client(E),
41	#[error("Duplicate authority set change.")]
42	DuplicateAuthoritySetChange,
43	#[error("Multiple pending forced authority set changes are not allowed.")]
44	MultiplePendingForcedAuthoritySetChanges,
45	#[error(
46		"A pending forced authority set change could not be applied since it must be applied \
47		after the pending standard change at #{0}"
48	)]
49	ForcedAuthoritySetChangeDependencyUnsatisfied(N),
50	#[error("Invalid operation in the pending changes tree: {0}")]
51	ForkTree(fork_tree::Error<E>),
52}
53
54impl<N, E> From<fork_tree::Error<E>> for Error<N, E> {
55	fn from(err: fork_tree::Error<E>) -> Error<N, E> {
56		match err {
57			fork_tree::Error::Client(err) => Error::Client(err),
58			fork_tree::Error::Duplicate => Error::DuplicateAuthoritySetChange,
59			err => Error::ForkTree(err),
60		}
61	}
62}
63
64impl<N, E: std::error::Error> From<E> for Error<N, E> {
65	fn from(err: E) -> Error<N, E> {
66		Error::Client(err)
67	}
68}
69
70/// A shared authority set.
71pub struct SharedAuthoritySet<H, N> {
72	inner: SharedData<AuthoritySet<H, N>>,
73}
74
75impl<H, N> Clone for SharedAuthoritySet<H, N> {
76	fn clone(&self) -> Self {
77		SharedAuthoritySet { inner: self.inner.clone() }
78	}
79}
80
81impl<H, N> SharedAuthoritySet<H, N> {
82	/// Returns access to the [`AuthoritySet`].
83	pub(crate) fn inner(&self) -> MappedMutexGuard<AuthoritySet<H, N>> {
84		self.inner.shared_data()
85	}
86
87	/// Returns access to the [`AuthoritySet`] and locks it.
88	///
89	/// For more information see [`SharedDataLocked`].
90	pub(crate) fn inner_locked(&self) -> SharedDataLocked<AuthoritySet<H, N>> {
91		self.inner.shared_data_locked()
92	}
93}
94
95impl<H: Eq, N> SharedAuthoritySet<H, N>
96where
97	N: Add<Output = N> + Ord + Clone + Debug,
98	H: Clone + Debug,
99{
100	/// Get the earliest limit-block number that's higher or equal to the given
101	/// min number, if any.
102	pub(crate) fn current_limit(&self, min: N) -> Option<N> {
103		self.inner().current_limit(min)
104	}
105
106	/// Get the current set ID. This is incremented every time the set changes.
107	pub fn set_id(&self) -> u64 {
108		self.inner().set_id
109	}
110
111	/// Get the current authorities and their weights (for the current set ID).
112	pub fn current_authorities(&self) -> VoterSet<AuthorityId> {
113		VoterSet::new(self.inner().current_authorities.iter().cloned()).expect(
114			"current_authorities is non-empty and weights are non-zero; \
115			 constructor and all mutating operations on `AuthoritySet` ensure this; \
116			 qed.",
117		)
118	}
119
120	/// Clone the inner `AuthoritySet`.
121	pub fn clone_inner(&self) -> AuthoritySet<H, N> {
122		self.inner().clone()
123	}
124
125	/// Clone the inner `AuthoritySetChanges`.
126	pub fn authority_set_changes(&self) -> AuthoritySetChanges<N> {
127		self.inner().authority_set_changes.clone()
128	}
129}
130
131impl<H, N> From<AuthoritySet<H, N>> for SharedAuthoritySet<H, N> {
132	fn from(set: AuthoritySet<H, N>) -> Self {
133		SharedAuthoritySet { inner: SharedData::new(set) }
134	}
135}
136
137/// Status of the set after changes were applied.
138#[derive(Debug)]
139pub(crate) struct Status<H, N> {
140	/// Whether internal changes were made.
141	pub(crate) changed: bool,
142	/// `Some` when underlying authority set has changed, containing the
143	/// block where that set changed.
144	pub(crate) new_set_block: Option<(H, N)>,
145}
146
147/// A set of authorities.
148#[derive(Debug, Clone, Encode, Decode, PartialEq)]
149pub struct AuthoritySet<H, N> {
150	/// The current active authorities.
151	pub(crate) current_authorities: AuthorityList,
152	/// The current set id.
153	pub(crate) set_id: u64,
154	/// Tree of pending standard changes across forks. Standard changes are
155	/// enacted on finality and must be enacted (i.e. finalized) in-order across
156	/// a given branch
157	pub(crate) pending_standard_changes: ForkTree<H, N, PendingChange<H, N>>,
158	/// Pending forced changes across different forks (at most one per fork).
159	/// Forced changes are enacted on block depth (not finality), for this
160	/// reason only one forced change should exist per fork. When trying to
161	/// apply forced changes we keep track of any pending standard changes that
162	/// they may depend on, this is done by making sure that any pending change
163	/// that is an ancestor of the forced changed and its effective block number
164	/// is lower than the last finalized block (as signaled in the forced
165	/// change) must be applied beforehand.
166	pending_forced_changes: Vec<PendingChange<H, N>>,
167	/// Track at which blocks the set id changed. This is useful when we need to prove finality for
168	/// a given block since we can figure out what set the block belongs to and when the set
169	/// started/ended.
170	pub(crate) authority_set_changes: AuthoritySetChanges<N>,
171}
172
173impl<H, N> AuthoritySet<H, N>
174where
175	H: PartialEq,
176	N: Ord + Clone,
177{
178	// authority sets must be non-empty and all weights must be greater than 0
179	fn invalid_authority_list(authorities: &AuthorityList) -> bool {
180		authorities.is_empty() || authorities.iter().any(|(_, w)| *w == 0)
181	}
182
183	/// Get a genesis set with given authorities.
184	pub(crate) fn genesis(initial: AuthorityList) -> Option<Self> {
185		if Self::invalid_authority_list(&initial) {
186			return None
187		}
188
189		Some(AuthoritySet {
190			current_authorities: initial,
191			set_id: 0,
192			pending_standard_changes: ForkTree::new(),
193			pending_forced_changes: Vec::new(),
194			authority_set_changes: AuthoritySetChanges::empty(),
195		})
196	}
197
198	/// Create a new authority set.
199	pub(crate) fn new(
200		authorities: AuthorityList,
201		set_id: u64,
202		pending_standard_changes: ForkTree<H, N, PendingChange<H, N>>,
203		pending_forced_changes: Vec<PendingChange<H, N>>,
204		authority_set_changes: AuthoritySetChanges<N>,
205	) -> Option<Self> {
206		if Self::invalid_authority_list(&authorities) {
207			return None
208		}
209
210		Some(AuthoritySet {
211			current_authorities: authorities,
212			set_id,
213			pending_standard_changes,
214			pending_forced_changes,
215			authority_set_changes,
216		})
217	}
218
219	/// Get the current set id and a reference to the current authority set.
220	pub(crate) fn current(&self) -> (u64, &[(AuthorityId, u64)]) {
221		(self.set_id, &self.current_authorities[..])
222	}
223
224	/// Revert to a specified block given its `hash` and `number`.
225	/// This removes all the authority set changes that were announced after
226	/// the revert point.
227	/// Revert point is identified by `number` and `hash`.
228	pub(crate) fn revert<F, E>(&mut self, hash: H, number: N, is_descendent_of: &F)
229	where
230		F: Fn(&H, &H) -> Result<bool, E>,
231	{
232		let filter = |node_hash: &H, node_num: &N, _: &PendingChange<H, N>| {
233			if number >= *node_num &&
234				(is_descendent_of(node_hash, &hash).unwrap_or_default() || *node_hash == hash)
235			{
236				// Continue the search in this subtree.
237				FilterAction::KeepNode
238			} else if number < *node_num && is_descendent_of(&hash, node_hash).unwrap_or_default() {
239				// Found a node to be removed.
240				FilterAction::Remove
241			} else {
242				// Not a parent or child of the one we're looking for, stop processing this branch.
243				FilterAction::KeepTree
244			}
245		};
246
247		// Remove standard changes.
248		let _ = self.pending_standard_changes.drain_filter(&filter);
249
250		// Remove forced changes.
251		self.pending_forced_changes
252			.retain(|change| !is_descendent_of(&hash, &change.canon_hash).unwrap_or_default());
253	}
254}
255
256impl<H: Eq, N> AuthoritySet<H, N>
257where
258	N: Add<Output = N> + Ord + Clone + Debug,
259	H: Clone + Debug,
260{
261	/// Returns the block hash and height at which the next pending change in
262	/// the given chain (i.e. it includes `best_hash`) was signalled, `None` if
263	/// there are no pending changes for the given chain.
264	///
265	/// This is useful since we know that when a change is signalled the
266	/// underlying runtime authority set management module (e.g. session module)
267	/// has updated its internal state (e.g. a new session started).
268	pub(crate) fn next_change<F, E>(
269		&self,
270		best_hash: &H,
271		is_descendent_of: &F,
272	) -> Result<Option<(H, N)>, Error<N, E>>
273	where
274		F: Fn(&H, &H) -> Result<bool, E>,
275		E: std::error::Error,
276	{
277		let mut forced = None;
278		for change in &self.pending_forced_changes {
279			if is_descendent_of(&change.canon_hash, best_hash)? {
280				forced = Some((change.canon_hash.clone(), change.canon_height.clone()));
281				break
282			}
283		}
284
285		let mut standard = None;
286		for (_, _, change) in self.pending_standard_changes.roots() {
287			if is_descendent_of(&change.canon_hash, best_hash)? {
288				standard = Some((change.canon_hash.clone(), change.canon_height.clone()));
289				break
290			}
291		}
292
293		let earliest = match (forced, standard) {
294			(Some(forced), Some(standard)) =>
295				Some(if forced.1 < standard.1 { forced } else { standard }),
296			(Some(forced), None) => Some(forced),
297			(None, Some(standard)) => Some(standard),
298			(None, None) => None,
299		};
300
301		Ok(earliest)
302	}
303
304	fn add_standard_change<F, E>(
305		&mut self,
306		pending: PendingChange<H, N>,
307		is_descendent_of: &F,
308	) -> Result<(), Error<N, E>>
309	where
310		F: Fn(&H, &H) -> Result<bool, E>,
311		E: std::error::Error,
312	{
313		let hash = pending.canon_hash.clone();
314		let number = pending.canon_height.clone();
315
316		debug!(
317			target: LOG_TARGET,
318			"Inserting potential standard set change signaled at block {:?} (delayed by {:?} blocks).",
319			(&number, &hash),
320			pending.delay,
321		);
322
323		self.pending_standard_changes.import(hash, number, pending, is_descendent_of)?;
324
325		debug!(
326			target: LOG_TARGET,
327			"There are now {} alternatives for the next pending standard change (roots), and a \
328			 total of {} pending standard changes (across all forks).",
329			self.pending_standard_changes.roots().count(),
330			self.pending_standard_changes.iter().count(),
331		);
332
333		Ok(())
334	}
335
336	fn add_forced_change<F, E>(
337		&mut self,
338		pending: PendingChange<H, N>,
339		is_descendent_of: &F,
340	) -> Result<(), Error<N, E>>
341	where
342		F: Fn(&H, &H) -> Result<bool, E>,
343		E: std::error::Error,
344	{
345		for change in &self.pending_forced_changes {
346			if change.canon_hash == pending.canon_hash {
347				return Err(Error::DuplicateAuthoritySetChange)
348			}
349
350			if is_descendent_of(&change.canon_hash, &pending.canon_hash)? {
351				return Err(Error::MultiplePendingForcedAuthoritySetChanges)
352			}
353		}
354
355		// ordered first by effective number and then by signal-block number.
356		let key = (pending.effective_number(), pending.canon_height.clone());
357		let idx = self
358			.pending_forced_changes
359			.binary_search_by_key(&key, |change| {
360				(change.effective_number(), change.canon_height.clone())
361			})
362			.unwrap_or_else(|i| i);
363
364		debug!(
365			target: LOG_TARGET,
366			"Inserting potential forced set change at block {:?} (delayed by {:?} blocks).",
367			(&pending.canon_height, &pending.canon_hash),
368			pending.delay,
369		);
370
371		self.pending_forced_changes.insert(idx, pending);
372
373		debug!(
374			target: LOG_TARGET,
375			"There are now {} pending forced changes.",
376			self.pending_forced_changes.len()
377		);
378
379		Ok(())
380	}
381
382	/// Note an upcoming pending transition. Multiple pending standard changes
383	/// on the same branch can be added as long as they don't overlap. Forced
384	/// changes are restricted to one per fork. This method assumes that changes
385	/// on the same branch will be added in-order. The given function
386	/// `is_descendent_of` should return `true` if the second hash (target) is a
387	/// descendent of the first hash (base).
388	pub(crate) fn add_pending_change<F, E>(
389		&mut self,
390		pending: PendingChange<H, N>,
391		is_descendent_of: &F,
392	) -> Result<(), Error<N, E>>
393	where
394		F: Fn(&H, &H) -> Result<bool, E>,
395		E: std::error::Error,
396	{
397		if Self::invalid_authority_list(&pending.next_authorities) {
398			return Err(Error::InvalidAuthoritySet)
399		}
400
401		match pending.delay_kind {
402			DelayKind::Best { .. } => self.add_forced_change(pending, is_descendent_of),
403			DelayKind::Finalized => self.add_standard_change(pending, is_descendent_of),
404		}
405	}
406
407	/// Inspect pending changes. Standard pending changes are iterated first,
408	/// and the changes in the tree are traversed in pre-order, afterwards all
409	/// forced changes are iterated.
410	pub(crate) fn pending_changes(&self) -> impl Iterator<Item = &PendingChange<H, N>> {
411		self.pending_standard_changes
412			.iter()
413			.map(|(_, _, c)| c)
414			.chain(self.pending_forced_changes.iter())
415	}
416
417	/// Get the earliest limit-block number, if any. If there are pending changes across
418	/// different forks, this method will return the earliest effective number (across the
419	/// different branches) that is higher or equal to the given min number.
420	///
421	/// Only standard changes are taken into account for the current
422	/// limit, since any existing forced change should preclude the voter from voting.
423	pub(crate) fn current_limit(&self, min: N) -> Option<N> {
424		self.pending_standard_changes
425			.roots()
426			.filter(|&(_, _, c)| c.effective_number() >= min)
427			.min_by_key(|&(_, _, c)| c.effective_number())
428			.map(|(_, _, c)| c.effective_number())
429	}
430
431	/// Apply or prune any pending transitions based on a best-block trigger.
432	///
433	/// Returns `Ok((median, new_set))` when a forced change has occurred. The
434	/// median represents the median last finalized block at the time the change
435	/// was signaled, and it should be used as the canon block when starting the
436	/// new grandpa voter. Only alters the internal state in this case.
437	///
438	/// These transitions are always forced and do not lead to justifications
439	/// which light clients can follow.
440	///
441	/// Forced changes can only be applied after all pending standard changes
442	/// that it depends on have been applied. If any pending standard change
443	/// exists that is an ancestor of a given forced changed and which effective
444	/// block number is lower than the last finalized block (as defined by the
445	/// forced change), then the forced change cannot be applied. An error will
446	/// be returned in that case which will prevent block import.
447	pub(crate) fn apply_forced_changes<F, E>(
448		&self,
449		best_hash: H,
450		best_number: N,
451		is_descendent_of: &F,
452		initial_sync: bool,
453		telemetry: Option<TelemetryHandle>,
454	) -> Result<Option<(N, Self)>, Error<N, E>>
455	where
456		F: Fn(&H, &H) -> Result<bool, E>,
457		E: std::error::Error,
458	{
459		let mut new_set = None;
460
461		for change in self
462			.pending_forced_changes
463			.iter()
464			.take_while(|c| c.effective_number() <= best_number) // to prevent iterating too far
465			.filter(|c| c.effective_number() == best_number)
466		{
467			// check if the given best block is in the same branch as
468			// the block that signaled the change.
469			if change.canon_hash == best_hash || is_descendent_of(&change.canon_hash, &best_hash)? {
470				let median_last_finalized = match change.delay_kind {
471					DelayKind::Best { ref median_last_finalized } => median_last_finalized.clone(),
472					_ => unreachable!(
473						"pending_forced_changes only contains forced changes; forced changes have delay kind Best; qed."
474					),
475				};
476
477				// check if there's any pending standard change that we depend on
478				for (_, _, standard_change) in self.pending_standard_changes.roots() {
479					if standard_change.effective_number() <= median_last_finalized &&
480						is_descendent_of(&standard_change.canon_hash, &change.canon_hash)?
481					{
482						log::info!(target: LOG_TARGET,
483							"Not applying authority set change forced at block #{:?}, due to pending standard change at block #{:?}",
484							change.canon_height,
485							standard_change.effective_number(),
486						);
487
488						return Err(Error::ForcedAuthoritySetChangeDependencyUnsatisfied(
489							standard_change.effective_number(),
490						))
491					}
492				}
493
494				// apply this change: make the set canonical
495				grandpa_log!(
496					initial_sync,
497					"👴 Applying authority set change forced at block #{:?}",
498					change.canon_height,
499				);
500
501				telemetry!(
502					telemetry;
503					CONSENSUS_INFO;
504					"afg.applying_forced_authority_set_change";
505					"block" => ?change.canon_height
506				);
507
508				let mut authority_set_changes = self.authority_set_changes.clone();
509				authority_set_changes.append(self.set_id, median_last_finalized.clone());
510
511				new_set = Some((
512					median_last_finalized,
513					AuthoritySet {
514						current_authorities: change.next_authorities.clone(),
515						set_id: self.set_id + 1,
516						pending_standard_changes: ForkTree::new(), // new set, new changes.
517						pending_forced_changes: Vec::new(),
518						authority_set_changes,
519					},
520				));
521
522				break
523			}
524		}
525
526		// we don't wipe forced changes until another change is applied, hence
527		// why we return a new set instead of mutating.
528		Ok(new_set)
529	}
530
531	/// Apply or prune any pending transitions based on a finality trigger. This
532	/// method ensures that if there are multiple changes in the same branch,
533	/// finalizing this block won't finalize past multiple transitions (i.e.
534	/// transitions must be finalized in-order). The given function
535	/// `is_descendent_of` should return `true` if the second hash (target) is a
536	/// descendent of the first hash (base).
537	///
538	/// When the set has changed, the return value will be `Ok(Some((H, N)))`
539	/// which is the canonical block where the set last changed (i.e. the given
540	/// hash and number).
541	pub(crate) fn apply_standard_changes<F, E>(
542		&mut self,
543		finalized_hash: H,
544		finalized_number: N,
545		is_descendent_of: &F,
546		initial_sync: bool,
547		telemetry: Option<&TelemetryHandle>,
548	) -> Result<Status<H, N>, Error<N, E>>
549	where
550		F: Fn(&H, &H) -> Result<bool, E>,
551		E: std::error::Error,
552	{
553		let mut status = Status { changed: false, new_set_block: None };
554
555		match self.pending_standard_changes.finalize_with_descendent_if(
556			&finalized_hash,
557			finalized_number.clone(),
558			is_descendent_of,
559			|change| change.effective_number() <= finalized_number,
560		)? {
561			fork_tree::FinalizationResult::Changed(change) => {
562				status.changed = true;
563
564				let pending_forced_changes = std::mem::take(&mut self.pending_forced_changes);
565
566				// we will keep all forced changes for any later blocks and that are a
567				// descendent of the finalized block (i.e. they are part of this branch).
568				for change in pending_forced_changes {
569					if change.effective_number() > finalized_number &&
570						is_descendent_of(&finalized_hash, &change.canon_hash)?
571					{
572						self.pending_forced_changes.push(change)
573					}
574				}
575
576				if let Some(change) = change {
577					grandpa_log!(
578						initial_sync,
579						"👴 Applying authority set change scheduled at block #{:?}",
580						change.canon_height,
581					);
582					telemetry!(
583						telemetry;
584						CONSENSUS_INFO;
585						"afg.applying_scheduled_authority_set_change";
586						"block" => ?change.canon_height
587					);
588
589					// Store the set_id together with the last block_number for the set
590					self.authority_set_changes.append(self.set_id, finalized_number.clone());
591
592					self.current_authorities = change.next_authorities;
593					self.set_id += 1;
594
595					status.new_set_block = Some((finalized_hash, finalized_number));
596				}
597			},
598			fork_tree::FinalizationResult::Unchanged => {},
599		}
600
601		Ok(status)
602	}
603
604	/// Check whether the given finalized block number enacts any standard
605	/// authority set change (without triggering it), ensuring that if there are
606	/// multiple changes in the same branch, finalizing this block won't
607	/// finalize past multiple transitions (i.e. transitions must be finalized
608	/// in-order). Returns `Some(true)` if the block being finalized enacts a
609	/// change that can be immediately applied, `Some(false)` if the block being
610	/// finalized enacts a change but it cannot be applied yet since there are
611	/// other dependent changes, and `None` if no change is enacted. The given
612	/// function `is_descendent_of` should return `true` if the second hash
613	/// (target) is a descendent of the first hash (base).
614	pub fn enacts_standard_change<F, E>(
615		&self,
616		finalized_hash: H,
617		finalized_number: N,
618		is_descendent_of: &F,
619	) -> Result<Option<bool>, Error<N, E>>
620	where
621		F: Fn(&H, &H) -> Result<bool, E>,
622		E: std::error::Error,
623	{
624		self.pending_standard_changes
625			.finalizes_any_with_descendent_if(
626				&finalized_hash,
627				finalized_number.clone(),
628				is_descendent_of,
629				|change| change.effective_number() == finalized_number,
630			)
631			.map_err(Error::ForkTree)
632	}
633}
634
635/// Kinds of delays for pending changes.
636#[derive(Debug, Clone, Encode, Decode, PartialEq)]
637pub enum DelayKind<N> {
638	/// Depth in finalized chain.
639	Finalized,
640	/// Depth in best chain. The median last finalized block is calculated at the time the
641	/// change was signaled.
642	Best { median_last_finalized: N },
643}
644
645/// A pending change to the authority set.
646///
647/// This will be applied when the announcing block is at some depth within
648/// the finalized or unfinalized chain.
649#[derive(Debug, Clone, Encode, PartialEq)]
650pub struct PendingChange<H, N> {
651	/// The new authorities and weights to apply.
652	pub(crate) next_authorities: AuthorityList,
653	/// How deep in the chain the announcing block must be
654	/// before the change is applied.
655	pub(crate) delay: N,
656	/// The announcing block's height.
657	pub(crate) canon_height: N,
658	/// The announcing block's hash.
659	pub(crate) canon_hash: H,
660	/// The delay kind.
661	pub(crate) delay_kind: DelayKind<N>,
662}
663
664impl<H: Decode, N: Decode> Decode for PendingChange<H, N> {
665	fn decode<I: codec::Input>(value: &mut I) -> Result<Self, codec::Error> {
666		let next_authorities = Decode::decode(value)?;
667		let delay = Decode::decode(value)?;
668		let canon_height = Decode::decode(value)?;
669		let canon_hash = Decode::decode(value)?;
670
671		let delay_kind = DelayKind::decode(value).unwrap_or(DelayKind::Finalized);
672
673		Ok(PendingChange { next_authorities, delay, canon_height, canon_hash, delay_kind })
674	}
675}
676
677impl<H, N: Add<Output = N> + Clone> PendingChange<H, N> {
678	/// Returns the effective number this change will be applied at.
679	pub fn effective_number(&self) -> N {
680		self.canon_height.clone() + self.delay.clone()
681	}
682}
683
684/// Tracks historical authority set changes. We store the block numbers for the last block
685/// of each authority set, once they have been finalized. These blocks are guaranteed to
686/// have a justification unless they were triggered by a forced change.
687#[derive(Debug, Encode, Decode, Clone, PartialEq)]
688pub struct AuthoritySetChanges<N>(Vec<(u64, N)>);
689
690/// The response when querying for a set id for a specific block. Either we get a set id
691/// together with a block number for the last block in the set, or that the requested block is in
692/// the latest set, or that we don't know what set id the given block belongs to.
693#[derive(Debug, PartialEq)]
694pub enum AuthoritySetChangeId<N> {
695	/// The requested block is in the latest set.
696	Latest,
697	/// Tuple containing the set id and the last block number of that set.
698	Set(SetId, N),
699	/// We don't know which set id the request block belongs to (this can only happen due to
700	/// missing data).
701	Unknown,
702}
703
704impl<N> From<Vec<(u64, N)>> for AuthoritySetChanges<N> {
705	fn from(changes: Vec<(u64, N)>) -> AuthoritySetChanges<N> {
706		AuthoritySetChanges(changes)
707	}
708}
709
710impl<N: Ord + Clone> AuthoritySetChanges<N> {
711	pub(crate) fn empty() -> Self {
712		Self(Default::default())
713	}
714
715	pub(crate) fn append(&mut self, set_id: u64, block_number: N) {
716		self.0.push((set_id, block_number));
717	}
718
719	pub(crate) fn get_set_id(&self, block_number: N) -> AuthoritySetChangeId<N> {
720		if self
721			.0
722			.last()
723			.map(|last_auth_change| last_auth_change.1 < block_number)
724			.unwrap_or(false)
725		{
726			return AuthoritySetChangeId::Latest
727		}
728
729		let idx = self
730			.0
731			.binary_search_by_key(&block_number, |(_, n)| n.clone())
732			.unwrap_or_else(|b| b);
733
734		if idx < self.0.len() {
735			let (set_id, block_number) = self.0[idx].clone();
736
737			// if this is the first index but not the first set id then we are missing data.
738			if idx == 0 && set_id != 0 {
739				return AuthoritySetChangeId::Unknown
740			}
741
742			AuthoritySetChangeId::Set(set_id, block_number)
743		} else {
744			AuthoritySetChangeId::Unknown
745		}
746	}
747
748	pub(crate) fn insert(&mut self, block_number: N) {
749		let idx = self
750			.0
751			.binary_search_by_key(&block_number, |(_, n)| n.clone())
752			.unwrap_or_else(|b| b);
753
754		let set_id = if idx == 0 { 0 } else { self.0[idx - 1].0 + 1 };
755		assert!(idx == self.0.len() || self.0[idx].0 != set_id);
756		self.0.insert(idx, (set_id, block_number));
757	}
758
759	/// Returns an iterator over all historical authority set changes starting at the given block
760	/// number (excluded). The iterator yields a tuple representing the set id and the block number
761	/// of the last block in that set.
762	pub fn iter_from(&self, block_number: N) -> Option<impl Iterator<Item = &(u64, N)>> {
763		let idx = self
764			.0
765			.binary_search_by_key(&block_number, |(_, n)| n.clone())
766			// if there was a change at the given block number then we should start on the next
767			// index since we want to exclude the current block number
768			.map(|n| n + 1)
769			.unwrap_or_else(|b| b);
770
771		if idx < self.0.len() {
772			let (set_id, _) = self.0[idx].clone();
773
774			// if this is the first index but not the first set id then we are missing data.
775			if idx == 0 && set_id != 0 {
776				return None
777			}
778		}
779
780		Some(self.0[idx..].iter())
781	}
782}
783
784#[cfg(test)]
785mod tests {
786	use super::*;
787	use sp_core::crypto::{ByteArray, UncheckedFrom};
788
789	fn static_is_descendent_of<A>(value: bool) -> impl Fn(&A, &A) -> Result<bool, std::io::Error> {
790		move |_, _| Ok(value)
791	}
792
793	fn is_descendent_of<A, F>(f: F) -> impl Fn(&A, &A) -> Result<bool, std::io::Error>
794	where
795		F: Fn(&A, &A) -> bool,
796	{
797		move |base, hash| Ok(f(base, hash))
798	}
799
800	#[test]
801	fn current_limit_filters_min() {
802		let current_authorities = vec![(AuthorityId::from_slice(&[1; 32]).unwrap(), 1)];
803
804		let mut authorities = AuthoritySet {
805			current_authorities: current_authorities.clone(),
806			set_id: 0,
807			pending_standard_changes: ForkTree::new(),
808			pending_forced_changes: Vec::new(),
809			authority_set_changes: AuthoritySetChanges::empty(),
810		};
811
812		let change = |height| PendingChange {
813			next_authorities: current_authorities.clone(),
814			delay: 0,
815			canon_height: height,
816			canon_hash: height.to_string(),
817			delay_kind: DelayKind::Finalized,
818		};
819
820		let is_descendent_of = static_is_descendent_of(false);
821
822		authorities.add_pending_change(change(1), &is_descendent_of).unwrap();
823		authorities.add_pending_change(change(2), &is_descendent_of).unwrap();
824
825		assert_eq!(authorities.current_limit(0), Some(1));
826
827		assert_eq!(authorities.current_limit(1), Some(1));
828
829		assert_eq!(authorities.current_limit(2), Some(2));
830
831		assert_eq!(authorities.current_limit(3), None);
832	}
833
834	#[test]
835	fn changes_iterated_in_pre_order() {
836		let current_authorities = vec![(AuthorityId::from_slice(&[1; 32]).unwrap(), 1)];
837
838		let mut authorities = AuthoritySet {
839			current_authorities: current_authorities.clone(),
840			set_id: 0,
841			pending_standard_changes: ForkTree::new(),
842			pending_forced_changes: Vec::new(),
843			authority_set_changes: AuthoritySetChanges::empty(),
844		};
845
846		let change_a = PendingChange {
847			next_authorities: current_authorities.clone(),
848			delay: 10,
849			canon_height: 5,
850			canon_hash: "hash_a",
851			delay_kind: DelayKind::Finalized,
852		};
853
854		let change_b = PendingChange {
855			next_authorities: current_authorities.clone(),
856			delay: 0,
857			canon_height: 5,
858			canon_hash: "hash_b",
859			delay_kind: DelayKind::Finalized,
860		};
861
862		let change_c = PendingChange {
863			next_authorities: current_authorities.clone(),
864			delay: 5,
865			canon_height: 10,
866			canon_hash: "hash_c",
867			delay_kind: DelayKind::Finalized,
868		};
869
870		authorities
871			.add_pending_change(change_a.clone(), &static_is_descendent_of(false))
872			.unwrap();
873		authorities
874			.add_pending_change(change_b.clone(), &static_is_descendent_of(false))
875			.unwrap();
876		authorities
877			.add_pending_change(
878				change_c.clone(),
879				&is_descendent_of(|base, hash| match (*base, *hash) {
880					("hash_a", "hash_c") => true,
881					("hash_b", "hash_c") => false,
882					_ => unreachable!(),
883				}),
884			)
885			.unwrap();
886
887		// forced changes are iterated last
888		let change_d = PendingChange {
889			next_authorities: current_authorities.clone(),
890			delay: 2,
891			canon_height: 1,
892			canon_hash: "hash_d",
893			delay_kind: DelayKind::Best { median_last_finalized: 0 },
894		};
895
896		let change_e = PendingChange {
897			next_authorities: current_authorities.clone(),
898			delay: 2,
899			canon_height: 0,
900			canon_hash: "hash_e",
901			delay_kind: DelayKind::Best { median_last_finalized: 0 },
902		};
903
904		authorities
905			.add_pending_change(change_d.clone(), &static_is_descendent_of(false))
906			.unwrap();
907		authorities
908			.add_pending_change(change_e.clone(), &static_is_descendent_of(false))
909			.unwrap();
910
911		// ordered by subtree depth
912		assert_eq!(
913			authorities.pending_changes().collect::<Vec<_>>(),
914			vec![&change_a, &change_c, &change_b, &change_e, &change_d],
915		);
916	}
917
918	#[test]
919	fn apply_change() {
920		let mut authorities = AuthoritySet {
921			current_authorities: Vec::new(),
922			set_id: 0,
923			pending_standard_changes: ForkTree::new(),
924			pending_forced_changes: Vec::new(),
925			authority_set_changes: AuthoritySetChanges::empty(),
926		};
927
928		let set_a = vec![(AuthorityId::from_slice(&[1; 32]).unwrap(), 5)];
929		let set_b = vec![(AuthorityId::from_slice(&[2; 32]).unwrap(), 5)];
930
931		// two competing changes at the same height on different forks
932		let change_a = PendingChange {
933			next_authorities: set_a.clone(),
934			delay: 10,
935			canon_height: 5,
936			canon_hash: "hash_a",
937			delay_kind: DelayKind::Finalized,
938		};
939
940		let change_b = PendingChange {
941			next_authorities: set_b.clone(),
942			delay: 10,
943			canon_height: 5,
944			canon_hash: "hash_b",
945			delay_kind: DelayKind::Finalized,
946		};
947
948		authorities
949			.add_pending_change(change_a.clone(), &static_is_descendent_of(true))
950			.unwrap();
951		authorities
952			.add_pending_change(change_b.clone(), &static_is_descendent_of(true))
953			.unwrap();
954
955		assert_eq!(authorities.pending_changes().collect::<Vec<_>>(), vec![&change_a, &change_b]);
956
957		// finalizing "hash_c" won't enact the change signaled at "hash_a" but it will prune out
958		// "hash_b"
959		let status = authorities
960			.apply_standard_changes(
961				"hash_c",
962				11,
963				&is_descendent_of(|base, hash| match (*base, *hash) {
964					("hash_a", "hash_c") => true,
965					("hash_b", "hash_c") => false,
966					_ => unreachable!(),
967				}),
968				false,
969				None,
970			)
971			.unwrap();
972
973		assert!(status.changed);
974		assert_eq!(status.new_set_block, None);
975		assert_eq!(authorities.pending_changes().collect::<Vec<_>>(), vec![&change_a]);
976		assert_eq!(authorities.authority_set_changes, AuthoritySetChanges::empty());
977
978		// finalizing "hash_d" will enact the change signaled at "hash_a"
979		let status = authorities
980			.apply_standard_changes(
981				"hash_d",
982				15,
983				&is_descendent_of(|base, hash| match (*base, *hash) {
984					("hash_a", "hash_d") => true,
985					_ => unreachable!(),
986				}),
987				false,
988				None,
989			)
990			.unwrap();
991
992		assert!(status.changed);
993		assert_eq!(status.new_set_block, Some(("hash_d", 15)));
994
995		assert_eq!(authorities.current_authorities, set_a);
996		assert_eq!(authorities.set_id, 1);
997		assert_eq!(authorities.pending_changes().count(), 0);
998		assert_eq!(authorities.authority_set_changes, AuthoritySetChanges(vec![(0, 15)]));
999	}
1000
1001	#[test]
1002	fn disallow_multiple_changes_being_finalized_at_once() {
1003		let mut authorities = AuthoritySet {
1004			current_authorities: Vec::new(),
1005			set_id: 0,
1006			pending_standard_changes: ForkTree::new(),
1007			pending_forced_changes: Vec::new(),
1008			authority_set_changes: AuthoritySetChanges::empty(),
1009		};
1010
1011		let set_a = vec![(AuthorityId::from_slice(&[1; 32]).unwrap(), 5)];
1012		let set_c = vec![(AuthorityId::from_slice(&[2; 32]).unwrap(), 5)];
1013
1014		// two competing changes at the same height on different forks
1015		let change_a = PendingChange {
1016			next_authorities: set_a.clone(),
1017			delay: 10,
1018			canon_height: 5,
1019			canon_hash: "hash_a",
1020			delay_kind: DelayKind::Finalized,
1021		};
1022
1023		let change_c = PendingChange {
1024			next_authorities: set_c.clone(),
1025			delay: 10,
1026			canon_height: 30,
1027			canon_hash: "hash_c",
1028			delay_kind: DelayKind::Finalized,
1029		};
1030
1031		authorities
1032			.add_pending_change(change_a.clone(), &static_is_descendent_of(true))
1033			.unwrap();
1034		authorities
1035			.add_pending_change(change_c.clone(), &static_is_descendent_of(true))
1036			.unwrap();
1037
1038		let is_descendent_of = is_descendent_of(|base, hash| match (*base, *hash) {
1039			("hash_a", "hash_b") => true,
1040			("hash_a", "hash_c") => true,
1041			("hash_a", "hash_d") => true,
1042
1043			("hash_c", "hash_b") => false,
1044			("hash_c", "hash_d") => true,
1045
1046			("hash_b", "hash_c") => true,
1047			_ => unreachable!(),
1048		});
1049
1050		// trying to finalize past `change_c` without finalizing `change_a` first
1051		assert!(matches!(
1052			authorities.apply_standard_changes("hash_d", 40, &is_descendent_of, false, None),
1053			Err(Error::ForkTree(fork_tree::Error::UnfinalizedAncestor))
1054		));
1055		assert_eq!(authorities.authority_set_changes, AuthoritySetChanges::empty());
1056
1057		let status = authorities
1058			.apply_standard_changes("hash_b", 15, &is_descendent_of, false, None)
1059			.unwrap();
1060
1061		assert!(status.changed);
1062		assert_eq!(status.new_set_block, Some(("hash_b", 15)));
1063
1064		assert_eq!(authorities.current_authorities, set_a);
1065		assert_eq!(authorities.set_id, 1);
1066		assert_eq!(authorities.authority_set_changes, AuthoritySetChanges(vec![(0, 15)]));
1067
1068		// after finalizing `change_a` it should be possible to finalize `change_c`
1069		let status = authorities
1070			.apply_standard_changes("hash_d", 40, &is_descendent_of, false, None)
1071			.unwrap();
1072
1073		assert!(status.changed);
1074		assert_eq!(status.new_set_block, Some(("hash_d", 40)));
1075
1076		assert_eq!(authorities.current_authorities, set_c);
1077		assert_eq!(authorities.set_id, 2);
1078		assert_eq!(authorities.authority_set_changes, AuthoritySetChanges(vec![(0, 15), (1, 40)]));
1079	}
1080
1081	#[test]
1082	fn enacts_standard_change_works() {
1083		let mut authorities = AuthoritySet {
1084			current_authorities: Vec::new(),
1085			set_id: 0,
1086			pending_standard_changes: ForkTree::new(),
1087			pending_forced_changes: Vec::new(),
1088			authority_set_changes: AuthoritySetChanges::empty(),
1089		};
1090
1091		let set_a = vec![(AuthorityId::from_slice(&[1; 32]).unwrap(), 5)];
1092
1093		let change_a = PendingChange {
1094			next_authorities: set_a.clone(),
1095			delay: 10,
1096			canon_height: 5,
1097			canon_hash: "hash_a",
1098			delay_kind: DelayKind::Finalized,
1099		};
1100
1101		let change_b = PendingChange {
1102			next_authorities: set_a.clone(),
1103			delay: 10,
1104			canon_height: 20,
1105			canon_hash: "hash_b",
1106			delay_kind: DelayKind::Finalized,
1107		};
1108
1109		authorities
1110			.add_pending_change(change_a.clone(), &static_is_descendent_of(false))
1111			.unwrap();
1112		authorities
1113			.add_pending_change(change_b.clone(), &static_is_descendent_of(true))
1114			.unwrap();
1115
1116		let is_descendent_of = is_descendent_of(|base, hash| match (*base, *hash) {
1117			("hash_a", "hash_d") => true,
1118			("hash_a", "hash_e") => true,
1119			("hash_b", "hash_d") => true,
1120			("hash_b", "hash_e") => true,
1121			("hash_a", "hash_c") => false,
1122			("hash_b", "hash_c") => false,
1123			_ => unreachable!(),
1124		});
1125
1126		// "hash_c" won't finalize the existing change since it isn't a descendent
1127		assert_eq!(
1128			authorities.enacts_standard_change("hash_c", 15, &is_descendent_of).unwrap(),
1129			None,
1130		);
1131
1132		// "hash_d" at depth 14 won't work either
1133		assert_eq!(
1134			authorities.enacts_standard_change("hash_d", 14, &is_descendent_of).unwrap(),
1135			None,
1136		);
1137
1138		// but it should work at depth 15 (change height + depth)
1139		assert_eq!(
1140			authorities.enacts_standard_change("hash_d", 15, &is_descendent_of).unwrap(),
1141			Some(true),
1142		);
1143
1144		// finalizing "hash_e" at depth 20 will trigger change at "hash_b", but
1145		// it can't be applied yet since "hash_a" must be applied first
1146		assert_eq!(
1147			authorities.enacts_standard_change("hash_e", 30, &is_descendent_of).unwrap(),
1148			Some(false),
1149		);
1150	}
1151
1152	#[test]
1153	fn forced_changes() {
1154		let mut authorities = AuthoritySet {
1155			current_authorities: Vec::new(),
1156			set_id: 0,
1157			pending_standard_changes: ForkTree::new(),
1158			pending_forced_changes: Vec::new(),
1159			authority_set_changes: AuthoritySetChanges::empty(),
1160		};
1161
1162		let set_a = vec![(AuthorityId::from_slice(&[1; 32]).unwrap(), 5)];
1163		let set_b = vec![(AuthorityId::from_slice(&[2; 32]).unwrap(), 5)];
1164
1165		let change_a = PendingChange {
1166			next_authorities: set_a.clone(),
1167			delay: 10,
1168			canon_height: 5,
1169			canon_hash: "hash_a",
1170			delay_kind: DelayKind::Best { median_last_finalized: 42 },
1171		};
1172
1173		let change_b = PendingChange {
1174			next_authorities: set_b.clone(),
1175			delay: 10,
1176			canon_height: 5,
1177			canon_hash: "hash_b",
1178			delay_kind: DelayKind::Best { median_last_finalized: 0 },
1179		};
1180
1181		authorities
1182			.add_pending_change(change_a, &static_is_descendent_of(false))
1183			.unwrap();
1184		authorities
1185			.add_pending_change(change_b.clone(), &static_is_descendent_of(false))
1186			.unwrap();
1187
1188		// no duplicates are allowed
1189		assert!(matches!(
1190			authorities.add_pending_change(change_b, &static_is_descendent_of(false)),
1191			Err(Error::DuplicateAuthoritySetChange)
1192		));
1193
1194		// there's an effective change triggered at block 15 but not a standard one.
1195		// so this should do nothing.
1196		assert_eq!(
1197			authorities
1198				.enacts_standard_change("hash_c", 15, &static_is_descendent_of(true))
1199				.unwrap(),
1200			None,
1201		);
1202
1203		// there can only be one pending forced change per fork
1204		let change_c = PendingChange {
1205			next_authorities: set_b.clone(),
1206			delay: 3,
1207			canon_height: 8,
1208			canon_hash: "hash_a8",
1209			delay_kind: DelayKind::Best { median_last_finalized: 0 },
1210		};
1211
1212		let is_descendent_of_a = is_descendent_of(|base: &&str, _| base.starts_with("hash_a"));
1213
1214		assert!(matches!(
1215			authorities.add_pending_change(change_c, &is_descendent_of_a),
1216			Err(Error::MultiplePendingForcedAuthoritySetChanges)
1217		));
1218
1219		// let's try and apply the forced changes.
1220		// too early and there's no forced changes to apply.
1221		assert!(authorities
1222			.apply_forced_changes("hash_a10", 10, &static_is_descendent_of(true), false, None)
1223			.unwrap()
1224			.is_none());
1225
1226		// too late.
1227		assert!(authorities
1228			.apply_forced_changes("hash_a16", 16, &is_descendent_of_a, false, None)
1229			.unwrap()
1230			.is_none());
1231
1232		// on time -- chooses the right change for this fork.
1233		assert_eq!(
1234			authorities
1235				.apply_forced_changes("hash_a15", 15, &is_descendent_of_a, false, None)
1236				.unwrap()
1237				.unwrap(),
1238			(
1239				42,
1240				AuthoritySet {
1241					current_authorities: set_a,
1242					set_id: 1,
1243					pending_standard_changes: ForkTree::new(),
1244					pending_forced_changes: Vec::new(),
1245					authority_set_changes: AuthoritySetChanges(vec![(0, 42)]),
1246				},
1247			)
1248		);
1249	}
1250
1251	#[test]
1252	fn forced_changes_with_no_delay() {
1253		// NOTE: this is a regression test
1254		let mut authorities = AuthoritySet {
1255			current_authorities: Vec::new(),
1256			set_id: 0,
1257			pending_standard_changes: ForkTree::new(),
1258			pending_forced_changes: Vec::new(),
1259			authority_set_changes: AuthoritySetChanges::empty(),
1260		};
1261
1262		let set_a = vec![(AuthorityId::from_slice(&[1; 32]).unwrap(), 5)];
1263
1264		// we create a forced change with no delay
1265		let change_a = PendingChange {
1266			next_authorities: set_a.clone(),
1267			delay: 0,
1268			canon_height: 5,
1269			canon_hash: "hash_a",
1270			delay_kind: DelayKind::Best { median_last_finalized: 0 },
1271		};
1272
1273		// and import it
1274		authorities
1275			.add_pending_change(change_a, &static_is_descendent_of(false))
1276			.unwrap();
1277
1278		// it should be enacted at the same block that signaled it
1279		assert!(authorities
1280			.apply_forced_changes("hash_a", 5, &static_is_descendent_of(false), false, None)
1281			.unwrap()
1282			.is_some());
1283	}
1284
1285	#[test]
1286	fn forced_changes_blocked_by_standard_changes() {
1287		let set_a = vec![(AuthorityId::from_slice(&[1; 32]).unwrap(), 1)];
1288
1289		let mut authorities = AuthoritySet {
1290			current_authorities: set_a.clone(),
1291			set_id: 0,
1292			pending_standard_changes: ForkTree::new(),
1293			pending_forced_changes: Vec::new(),
1294			authority_set_changes: AuthoritySetChanges::empty(),
1295		};
1296
1297		// effective at #15
1298		let change_a = PendingChange {
1299			next_authorities: set_a.clone(),
1300			delay: 5,
1301			canon_height: 10,
1302			canon_hash: "hash_a",
1303			delay_kind: DelayKind::Finalized,
1304		};
1305
1306		// effective #20
1307		let change_b = PendingChange {
1308			next_authorities: set_a.clone(),
1309			delay: 0,
1310			canon_height: 20,
1311			canon_hash: "hash_b",
1312			delay_kind: DelayKind::Finalized,
1313		};
1314
1315		// effective at #35
1316		let change_c = PendingChange {
1317			next_authorities: set_a.clone(),
1318			delay: 5,
1319			canon_height: 30,
1320			canon_hash: "hash_c",
1321			delay_kind: DelayKind::Finalized,
1322		};
1323
1324		// add some pending standard changes all on the same fork
1325		authorities
1326			.add_pending_change(change_a, &static_is_descendent_of(true))
1327			.unwrap();
1328		authorities
1329			.add_pending_change(change_b, &static_is_descendent_of(true))
1330			.unwrap();
1331		authorities
1332			.add_pending_change(change_c, &static_is_descendent_of(true))
1333			.unwrap();
1334
1335		// effective at #45
1336		let change_d = PendingChange {
1337			next_authorities: set_a.clone(),
1338			delay: 5,
1339			canon_height: 40,
1340			canon_hash: "hash_d",
1341			delay_kind: DelayKind::Best { median_last_finalized: 31 },
1342		};
1343
1344		// now add a forced change on the same fork
1345		authorities
1346			.add_pending_change(change_d, &static_is_descendent_of(true))
1347			.unwrap();
1348
1349		// the forced change cannot be applied since the pending changes it depends on
1350		// have not been applied yet.
1351		assert!(matches!(
1352			authorities.apply_forced_changes(
1353				"hash_d45",
1354				45,
1355				&static_is_descendent_of(true),
1356				false,
1357				None
1358			),
1359			Err(Error::ForcedAuthoritySetChangeDependencyUnsatisfied(15))
1360		));
1361		assert_eq!(authorities.authority_set_changes, AuthoritySetChanges::empty());
1362
1363		// we apply the first pending standard change at #15
1364		authorities
1365			.apply_standard_changes("hash_a15", 15, &static_is_descendent_of(true), false, None)
1366			.unwrap();
1367		assert_eq!(authorities.authority_set_changes, AuthoritySetChanges(vec![(0, 15)]));
1368
1369		// but the forced change still depends on the next standard change
1370		assert!(matches!(
1371			authorities.apply_forced_changes(
1372				"hash_d",
1373				45,
1374				&static_is_descendent_of(true),
1375				false,
1376				None
1377			),
1378			Err(Error::ForcedAuthoritySetChangeDependencyUnsatisfied(20))
1379		));
1380		assert_eq!(authorities.authority_set_changes, AuthoritySetChanges(vec![(0, 15)]));
1381
1382		// we apply the pending standard change at #20
1383		authorities
1384			.apply_standard_changes("hash_b", 20, &static_is_descendent_of(true), false, None)
1385			.unwrap();
1386		assert_eq!(authorities.authority_set_changes, AuthoritySetChanges(vec![(0, 15), (1, 20)]));
1387
1388		// afterwards the forced change at #45 can already be applied since it signals
1389		// that finality stalled at #31, and the next pending standard change is effective
1390		// at #35. subsequent forced changes on the same branch must be kept
1391		assert_eq!(
1392			authorities
1393				.apply_forced_changes("hash_d", 45, &static_is_descendent_of(true), false, None)
1394				.unwrap()
1395				.unwrap(),
1396			(
1397				31,
1398				AuthoritySet {
1399					current_authorities: set_a.clone(),
1400					set_id: 3,
1401					pending_standard_changes: ForkTree::new(),
1402					pending_forced_changes: Vec::new(),
1403					authority_set_changes: AuthoritySetChanges(vec![(0, 15), (1, 20), (2, 31)]),
1404				}
1405			),
1406		);
1407		assert_eq!(authorities.authority_set_changes, AuthoritySetChanges(vec![(0, 15), (1, 20)]));
1408	}
1409
1410	#[test]
1411	fn next_change_works() {
1412		let current_authorities = vec![(AuthorityId::from_slice(&[1; 32]).unwrap(), 1)];
1413
1414		let mut authorities = AuthoritySet {
1415			current_authorities: current_authorities.clone(),
1416			set_id: 0,
1417			pending_standard_changes: ForkTree::new(),
1418			pending_forced_changes: Vec::new(),
1419			authority_set_changes: AuthoritySetChanges::empty(),
1420		};
1421
1422		let new_set = current_authorities.clone();
1423
1424		// We have three pending changes with 2 possible roots that are enacted
1425		// immediately on finality (i.e. standard changes).
1426		let change_a0 = PendingChange {
1427			next_authorities: new_set.clone(),
1428			delay: 0,
1429			canon_height: 5,
1430			canon_hash: "hash_a0",
1431			delay_kind: DelayKind::Finalized,
1432		};
1433
1434		let change_a1 = PendingChange {
1435			next_authorities: new_set.clone(),
1436			delay: 0,
1437			canon_height: 10,
1438			canon_hash: "hash_a1",
1439			delay_kind: DelayKind::Finalized,
1440		};
1441
1442		let change_b = PendingChange {
1443			next_authorities: new_set.clone(),
1444			delay: 0,
1445			canon_height: 4,
1446			canon_hash: "hash_b",
1447			delay_kind: DelayKind::Finalized,
1448		};
1449
1450		// A0 (#5) <- A10 (#8) <- A1 (#10) <- best_a
1451		// B (#4) <- best_b
1452		let is_descendent_of = is_descendent_of(|base, hash| match (*base, *hash) {
1453			("hash_a0", "hash_a1") => true,
1454			("hash_a0", "best_a") => true,
1455			("hash_a1", "best_a") => true,
1456			("hash_a10", "best_a") => true,
1457			("hash_b", "best_b") => true,
1458			_ => false,
1459		});
1460
1461		// add the three pending changes
1462		authorities.add_pending_change(change_b, &is_descendent_of).unwrap();
1463		authorities.add_pending_change(change_a0, &is_descendent_of).unwrap();
1464		authorities.add_pending_change(change_a1, &is_descendent_of).unwrap();
1465
1466		// the earliest change at block `best_a` should be the change at A0 (#5)
1467		assert_eq!(
1468			authorities.next_change(&"best_a", &is_descendent_of).unwrap(),
1469			Some(("hash_a0", 5)),
1470		);
1471
1472		// the earliest change at block `best_b` should be the change at B (#4)
1473		assert_eq!(
1474			authorities.next_change(&"best_b", &is_descendent_of).unwrap(),
1475			Some(("hash_b", 4)),
1476		);
1477
1478		// we apply the change at A0 which should prune it and the fork at B
1479		authorities
1480			.apply_standard_changes("hash_a0", 5, &is_descendent_of, false, None)
1481			.unwrap();
1482
1483		// the next change is now at A1 (#10)
1484		assert_eq!(
1485			authorities.next_change(&"best_a", &is_descendent_of).unwrap(),
1486			Some(("hash_a1", 10)),
1487		);
1488
1489		// there's no longer any pending change at `best_b` fork
1490		assert_eq!(authorities.next_change(&"best_b", &is_descendent_of).unwrap(), None);
1491
1492		// we a forced change at A10 (#8)
1493		let change_a10 = PendingChange {
1494			next_authorities: new_set.clone(),
1495			delay: 0,
1496			canon_height: 8,
1497			canon_hash: "hash_a10",
1498			delay_kind: DelayKind::Best { median_last_finalized: 0 },
1499		};
1500
1501		authorities
1502			.add_pending_change(change_a10, &static_is_descendent_of(false))
1503			.unwrap();
1504
1505		// it should take precedence over the change at A1 (#10)
1506		assert_eq!(
1507			authorities.next_change(&"best_a", &is_descendent_of).unwrap(),
1508			Some(("hash_a10", 8)),
1509		);
1510	}
1511
1512	#[test]
1513	fn maintains_authority_list_invariants() {
1514		// empty authority lists are invalid
1515		assert_eq!(AuthoritySet::<(), ()>::genesis(vec![]), None);
1516		assert_eq!(
1517			AuthoritySet::<(), ()>::new(
1518				vec![],
1519				0,
1520				ForkTree::new(),
1521				Vec::new(),
1522				AuthoritySetChanges::empty(),
1523			),
1524			None,
1525		);
1526
1527		let invalid_authorities_weight = vec![
1528			(AuthorityId::from_slice(&[1; 32]).unwrap(), 5),
1529			(AuthorityId::from_slice(&[2; 32]).unwrap(), 0),
1530		];
1531
1532		// authority weight of zero is invalid
1533		assert_eq!(AuthoritySet::<(), ()>::genesis(invalid_authorities_weight.clone()), None);
1534		assert_eq!(
1535			AuthoritySet::<(), ()>::new(
1536				invalid_authorities_weight.clone(),
1537				0,
1538				ForkTree::new(),
1539				Vec::new(),
1540				AuthoritySetChanges::empty(),
1541			),
1542			None,
1543		);
1544
1545		let mut authority_set =
1546			AuthoritySet::<(), u64>::genesis(vec![(AuthorityId::unchecked_from([1; 32]), 5)])
1547				.unwrap();
1548
1549		let invalid_change_empty_authorities = PendingChange {
1550			next_authorities: vec![],
1551			delay: 10,
1552			canon_height: 5,
1553			canon_hash: (),
1554			delay_kind: DelayKind::Finalized,
1555		};
1556
1557		// pending change contains an empty authority set
1558		assert!(matches!(
1559			authority_set.add_pending_change(
1560				invalid_change_empty_authorities.clone(),
1561				&static_is_descendent_of(false)
1562			),
1563			Err(Error::InvalidAuthoritySet)
1564		));
1565
1566		let invalid_change_authorities_weight = PendingChange {
1567			next_authorities: invalid_authorities_weight,
1568			delay: 10,
1569			canon_height: 5,
1570			canon_hash: (),
1571			delay_kind: DelayKind::Best { median_last_finalized: 0 },
1572		};
1573
1574		// pending change contains an an authority set
1575		// where one authority has weight of 0
1576		assert!(matches!(
1577			authority_set.add_pending_change(
1578				invalid_change_authorities_weight,
1579				&static_is_descendent_of(false)
1580			),
1581			Err(Error::InvalidAuthoritySet)
1582		));
1583	}
1584
1585	#[test]
1586	fn cleans_up_stale_forced_changes_when_applying_standard_change() {
1587		let current_authorities = vec![(AuthorityId::from_slice(&[1; 32]).unwrap(), 1)];
1588
1589		let mut authorities = AuthoritySet {
1590			current_authorities: current_authorities.clone(),
1591			set_id: 0,
1592			pending_standard_changes: ForkTree::new(),
1593			pending_forced_changes: Vec::new(),
1594			authority_set_changes: AuthoritySetChanges::empty(),
1595		};
1596
1597		let new_set = current_authorities.clone();
1598
1599		// Create the following pending changes tree:
1600		//
1601		//               [#C3]
1602		//              /
1603		//             /- (#C2)
1604		//            /
1605		// (#A) - (#B) - [#C1]
1606		//            \
1607		//             (#C0) - [#D]
1608		//
1609		// () - Standard change
1610		// [] - Forced change
1611
1612		let is_descendent_of = {
1613			let hashes = vec!["B", "C0", "C1", "C2", "C3", "D"];
1614			is_descendent_of(move |base, hash| match (*base, *hash) {
1615				("B", "B") => false, // required to have the simpler case below
1616				("A", b) | ("B", b) => hashes.iter().any(|h| *h == b),
1617				("C0", "D") => true,
1618				_ => false,
1619			})
1620		};
1621
1622		let mut add_pending_change = |canon_height, canon_hash, forced| {
1623			let change = PendingChange {
1624				next_authorities: new_set.clone(),
1625				delay: 0,
1626				canon_height,
1627				canon_hash,
1628				delay_kind: if forced {
1629					DelayKind::Best { median_last_finalized: 0 }
1630				} else {
1631					DelayKind::Finalized
1632				},
1633			};
1634
1635			authorities.add_pending_change(change, &is_descendent_of).unwrap();
1636		};
1637
1638		add_pending_change(5, "A", false);
1639		add_pending_change(10, "B", false);
1640		add_pending_change(15, "C0", false);
1641		add_pending_change(15, "C1", true);
1642		add_pending_change(15, "C2", false);
1643		add_pending_change(15, "C3", true);
1644		add_pending_change(20, "D", true);
1645
1646		// applying the standard change at A should not prune anything
1647		// other then the change that was applied
1648		authorities
1649			.apply_standard_changes("A", 5, &is_descendent_of, false, None)
1650			.unwrap();
1651
1652		assert_eq!(authorities.pending_changes().count(), 6);
1653
1654		// same for B
1655		authorities
1656			.apply_standard_changes("B", 10, &is_descendent_of, false, None)
1657			.unwrap();
1658
1659		assert_eq!(authorities.pending_changes().count(), 5);
1660
1661		let authorities2 = authorities.clone();
1662
1663		// finalizing C2 should clear all forced changes
1664		authorities
1665			.apply_standard_changes("C2", 15, &is_descendent_of, false, None)
1666			.unwrap();
1667
1668		assert_eq!(authorities.pending_forced_changes.len(), 0);
1669
1670		// finalizing C0 should clear all forced changes but D
1671		let mut authorities = authorities2;
1672		authorities
1673			.apply_standard_changes("C0", 15, &is_descendent_of, false, None)
1674			.unwrap();
1675
1676		assert_eq!(authorities.pending_forced_changes.len(), 1);
1677		assert_eq!(authorities.pending_forced_changes.first().unwrap().canon_hash, "D");
1678	}
1679
1680	#[test]
1681	fn authority_set_changes_insert() {
1682		let mut authority_set_changes = AuthoritySetChanges::empty();
1683		authority_set_changes.append(0, 41);
1684		authority_set_changes.append(1, 81);
1685		authority_set_changes.append(4, 121);
1686
1687		authority_set_changes.insert(101);
1688		assert_eq!(authority_set_changes.get_set_id(100), AuthoritySetChangeId::Set(2, 101));
1689		assert_eq!(authority_set_changes.get_set_id(101), AuthoritySetChangeId::Set(2, 101));
1690	}
1691
1692	#[test]
1693	fn authority_set_changes_for_complete_data() {
1694		let mut authority_set_changes = AuthoritySetChanges::empty();
1695		authority_set_changes.append(0, 41);
1696		authority_set_changes.append(1, 81);
1697		authority_set_changes.append(2, 121);
1698
1699		assert_eq!(authority_set_changes.get_set_id(20), AuthoritySetChangeId::Set(0, 41));
1700		assert_eq!(authority_set_changes.get_set_id(40), AuthoritySetChangeId::Set(0, 41));
1701		assert_eq!(authority_set_changes.get_set_id(41), AuthoritySetChangeId::Set(0, 41));
1702		assert_eq!(authority_set_changes.get_set_id(42), AuthoritySetChangeId::Set(1, 81));
1703		assert_eq!(authority_set_changes.get_set_id(141), AuthoritySetChangeId::Latest);
1704	}
1705
1706	#[test]
1707	fn authority_set_changes_for_incomplete_data() {
1708		let mut authority_set_changes = AuthoritySetChanges::empty();
1709		authority_set_changes.append(2, 41);
1710		authority_set_changes.append(3, 81);
1711		authority_set_changes.append(4, 121);
1712
1713		assert_eq!(authority_set_changes.get_set_id(20), AuthoritySetChangeId::Unknown);
1714		assert_eq!(authority_set_changes.get_set_id(40), AuthoritySetChangeId::Unknown);
1715		assert_eq!(authority_set_changes.get_set_id(41), AuthoritySetChangeId::Unknown);
1716		assert_eq!(authority_set_changes.get_set_id(42), AuthoritySetChangeId::Set(3, 81));
1717		assert_eq!(authority_set_changes.get_set_id(141), AuthoritySetChangeId::Latest);
1718	}
1719
1720	#[test]
1721	fn iter_from_works() {
1722		let mut authority_set_changes = AuthoritySetChanges::empty();
1723		authority_set_changes.append(1, 41);
1724		authority_set_changes.append(2, 81);
1725
1726		// we are missing the data for the first set, therefore we should return `None`
1727		assert_eq!(None, authority_set_changes.iter_from(40).map(|it| it.collect::<Vec<_>>()));
1728
1729		// after adding the data for the first set the same query should work
1730		let mut authority_set_changes = AuthoritySetChanges::empty();
1731		authority_set_changes.append(0, 21);
1732		authority_set_changes.append(1, 41);
1733		authority_set_changes.append(2, 81);
1734		authority_set_changes.append(3, 121);
1735
1736		assert_eq!(
1737			Some(vec![(1, 41), (2, 81), (3, 121)]),
1738			authority_set_changes.iter_from(40).map(|it| it.cloned().collect::<Vec<_>>()),
1739		);
1740
1741		assert_eq!(
1742			Some(vec![(2, 81), (3, 121)]),
1743			authority_set_changes.iter_from(41).map(|it| it.cloned().collect::<Vec<_>>()),
1744		);
1745
1746		assert_eq!(0, authority_set_changes.iter_from(121).unwrap().count());
1747
1748		assert_eq!(0, authority_set_changes.iter_from(200).unwrap().count());
1749	}
1750}