pezkuwi_node_subsystem_types/
messages.rs

1// Copyright (C) Parity Technologies (UK) Ltd. and Dijital Kurdistan Tech Institute
2// This file is part of Pezkuwi.
3
4// Pezkuwi is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Pezkuwi is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Pezkuwi.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Message types for the overseer and subsystems.
18//!
19//! These messages are intended to define the protocol by which different subsystems communicate
20//! with each other and signals that they receive from an overseer to coordinate their work.
21//! This is intended for use with the `pezkuwi-overseer` crate.
22//!
23//! Subsystems' APIs are defined separately from their implementation, leading to easier mocking.
24
25use futures::channel::oneshot;
26use pezsc_network::{Multiaddr, ReputationChange};
27use thiserror::Error;
28
29pub use pezsc_network::IfDisconnected;
30
31use pezkuwi_node_network_protocol::{
32	self as net_protocol, peer_set::PeerSet, request_response::Requests, PeerId,
33};
34use pezkuwi_pez_node_primitives::{
35	approval::{
36		v1::{BlockApprovalMeta, DelayTranche},
37		v2::{CandidateBitfield, IndirectAssignmentCertV2, IndirectSignedApprovalVoteV2},
38	},
39	AvailableData, BabeEpoch, BlockWeight, CandidateVotes, CollationGenerationConfig,
40	CollationSecondedSignal, DisputeMessage, DisputeStatus, ErasureChunk, PoV,
41	SignedDisputeStatement, SignedFullStatement, SignedFullStatementWithPVD, SubmitCollationParams,
42	ValidationResult,
43};
44use pezkuwi_primitives::{
45	self,
46	async_backing::{self, Constraints},
47	slashing, ApprovalVotingParams, AuthorityDiscoveryId, BackedCandidate, BlockNumber,
48	CandidateCommitments, CandidateEvent, CandidateHash, CandidateIndex,
49	CandidateReceiptV2 as CandidateReceipt,
50	CommittedCandidateReceiptV2 as CommittedCandidateReceipt, CoreIndex, CoreState, DisputeState,
51	ExecutorParams, GroupIndex, GroupRotationInfo, Hash, HeadData, Header as BlockHeader,
52	Id as ParaId, InboundDownwardMessage, InboundHrmpMessage, MultiDisputeStatementSet,
53	NodeFeatures, OccupiedCoreAssumption, PersistedValidationData, PvfCheckStatement,
54	PvfExecKind as RuntimePvfExecKind, SessionIndex, SessionInfo, SignedAvailabilityBitfield,
55	SignedAvailabilityBitfields, ValidationCode, ValidationCodeHash, ValidatorId, ValidatorIndex,
56	ValidatorSignature,
57};
58use pezkuwi_statement_table::v2::Misbehavior;
59use std::{
60	collections::{BTreeMap, HashMap, HashSet, VecDeque},
61	sync::Arc,
62};
63
64/// Network events as transmitted to other subsystems, wrapped in their message types.
65pub mod network_bridge_event;
66pub use network_bridge_event::NetworkBridgeEvent;
67
68/// A request to the candidate backing subsystem to check whether
69/// we can second this candidate.
70#[derive(Debug, Copy, Clone)]
71pub struct CanSecondRequest {
72	/// Para id of the candidate.
73	pub candidate_para_id: ParaId,
74	/// The relay-parent of the candidate.
75	pub candidate_relay_parent: Hash,
76	/// Hash of the candidate.
77	pub candidate_hash: CandidateHash,
78	/// Parent head data hash.
79	pub parent_head_data_hash: Hash,
80}
81
82/// Messages received by the Candidate Backing subsystem.
83#[derive(Debug)]
84pub enum CandidateBackingMessage {
85	/// Requests a set of backable candidates attested by the subsystem.
86	///
87	/// The order of candidates of the same para must be preserved in the response.
88	/// If a backed candidate of a para cannot be retrieved, the response should not contain any
89	/// candidates of the same para that follow it in the input vector. In other words, assuming
90	/// candidates are supplied in dependency order, we must ensure that this dependency order is
91	/// preserved.
92	GetBackableCandidates(
93		HashMap<ParaId, Vec<(CandidateHash, Hash)>>,
94		oneshot::Sender<HashMap<ParaId, Vec<BackedCandidate>>>,
95	),
96	/// Request the subsystem to check whether it's allowed to second given candidate.
97	/// The rule is to only fetch collations that can either be directly chained to any
98	/// FragmentChain in the view or there is at least one FragmentChain where this candidate is a
99	/// potentially unconnected candidate (we predict that it may become connected to a
100	/// FragmentChain in the future).
101	///
102	/// Always responds with `false` if async backing is disabled for candidate's relay
103	/// parent.
104	CanSecond(CanSecondRequest, oneshot::Sender<bool>),
105	/// Note that the Candidate Backing subsystem should second the given candidate in the context
106	/// of the given relay-parent (ref. by hash). This candidate must be validated.
107	Second(Hash, CandidateReceipt, PersistedValidationData, PoV),
108	/// Note a validator's statement about a particular candidate in the context of the given
109	/// relay-parent. Disagreements about validity must be escalated to a broader check by the
110	/// Disputes Subsystem, though that escalation is deferred until the approval voting stage to
111	/// guarantee availability. Agreements are simply tallied until a quorum is reached.
112	Statement(Hash, SignedFullStatementWithPVD),
113}
114
115/// Blanket error for validation failing for internal reasons.
116#[derive(Debug, Error)]
117#[error("Validation failed with {0:?}")]
118pub struct ValidationFailed(pub String);
119
120/// The outcome of the candidate-validation's PVF pre-check request.
121#[derive(Debug, PartialEq)]
122pub enum PreCheckOutcome {
123	/// The PVF has been compiled successfully within the given constraints.
124	Valid,
125	/// The PVF could not be compiled. This variant is used when the candidate-validation subsystem
126	/// can be sure that the PVF is invalid. To give a couple of examples: a PVF that cannot be
127	/// decompressed or that does not represent a structurally valid WebAssembly file.
128	Invalid,
129	/// This variant is used when the PVF cannot be compiled but for other reasons that are not
130	/// included into [`PreCheckOutcome::Invalid`]. This variant can indicate that the PVF in
131	/// question is invalid, however it is not necessary that PVF that received this judgement
132	/// is invalid.
133	///
134	/// For example, if during compilation the preparation worker was killed we cannot be sure why
135	/// it happened: because the PVF was malicious made the worker to use too much memory or its
136	/// because the host machine is under severe memory pressure and it decided to kill the worker.
137	Failed,
138}
139
140/// Messages received by the Validation subsystem.
141///
142/// ## Validation Requests
143///
144/// Validation requests made to the subsystem should return an error only on internal error.
145/// Otherwise, they should return either `Ok(ValidationResult::Valid(_))`
146/// or `Ok(ValidationResult::Invalid)`.
147#[derive(Debug)]
148pub enum CandidateValidationMessage {
149	/// Validate a candidate with provided, exhaustive parameters for validation.
150	///
151	/// Explicitly provide the `PersistedValidationData` and `ValidationCode` so this can do full
152	/// validation without needing to access the state of the relay-chain.
153	///
154	/// This request doesn't involve acceptance criteria checking, therefore only useful for the
155	/// cases where the validity of the candidate is established. This is the case for the typical
156	/// use-case: secondary checkers would use this request relying on the full prior checks
157	/// performed by the relay-chain.
158	ValidateFromExhaustive {
159		/// Persisted validation data
160		validation_data: PersistedValidationData,
161		/// Validation code
162		validation_code: ValidationCode,
163		/// The candidate receipt
164		candidate_receipt: CandidateReceipt,
165		/// The proof-of-validity
166		pov: Arc<PoV>,
167		/// Session's executor parameters
168		executor_params: ExecutorParams,
169		/// Execution kind, used for timeouts and retries (backing/approvals)
170		exec_kind: PvfExecKind,
171		/// The sending side of the response channel
172		response_sender: oneshot::Sender<Result<ValidationResult, ValidationFailed>>,
173	},
174	/// Try to compile the given validation code and send back
175	/// the outcome.
176	///
177	/// The validation code is specified by the hash and will be queried from the runtime API at
178	/// the given relay-parent.
179	PreCheck {
180		/// Relay-parent
181		relay_parent: Hash,
182		/// Validation code hash
183		validation_code_hash: ValidationCodeHash,
184		/// The sending side of the response channel
185		response_sender: oneshot::Sender<PreCheckOutcome>,
186	},
187}
188
189/// Extends primitives::PvfExecKind, which is a runtime parameter we don't want to change,
190/// to separate and prioritize execution jobs by request type.
191#[derive(Debug, Clone, Copy, PartialEq, Eq)]
192pub enum PvfExecKind {
193	/// For dispute requests
194	Dispute,
195	/// For approval requests
196	Approval,
197	/// For backing requests from system teyrchains. With relay parent hash
198	BackingSystemParas(Hash),
199	/// For backing requests. With relay parent hash
200	Backing(Hash),
201}
202
203impl PvfExecKind {
204	/// Converts priority level to &str
205	pub fn as_str(&self) -> &str {
206		match *self {
207			Self::Dispute => "dispute",
208			Self::Approval => "approval",
209			Self::BackingSystemParas(_) => "backing_system_paras",
210			Self::Backing(_) => "backing",
211		}
212	}
213}
214
215impl From<PvfExecKind> for RuntimePvfExecKind {
216	fn from(exec: PvfExecKind) -> Self {
217		match exec {
218			PvfExecKind::Dispute => RuntimePvfExecKind::Approval,
219			PvfExecKind::Approval => RuntimePvfExecKind::Approval,
220			PvfExecKind::BackingSystemParas(_) => RuntimePvfExecKind::Backing,
221			PvfExecKind::Backing(_) => RuntimePvfExecKind::Backing,
222		}
223	}
224}
225
226/// Messages received by the Collator Protocol subsystem.
227#[derive(Debug, derive_more::From)]
228pub enum CollatorProtocolMessage {
229	/// Signal to the collator protocol that it should connect to validators with the expectation
230	/// of collating on the given para. This is only expected to be called once, early on, if at
231	/// all, and only by the Collation Generation subsystem. As such, it will overwrite the value
232	/// of the previous signal.
233	///
234	/// This should be sent before any `DistributeCollation` message.
235	CollateOn(ParaId),
236	/// Provide a collation to distribute to validators with an optional result sender.
237	DistributeCollation {
238		/// The receipt of the candidate.
239		candidate_receipt: CandidateReceipt,
240		/// The hash of the parent head-data.
241		/// Here to avoid computing the hash of the parent head data twice.
242		parent_head_data_hash: Hash,
243		/// Proof of validity.
244		pov: PoV,
245		/// This parent head-data is needed for elastic scaling.
246		parent_head_data: HeadData,
247		/// The result sender should be informed when at least one teyrchain validator seconded the
248		/// collation. It is also completely okay to just drop the sender.
249		result_sender: Option<oneshot::Sender<CollationSecondedSignal>>,
250		/// The core index where the candidate should be backed.
251		core_index: CoreIndex,
252	},
253	/// Get a network bridge update.
254	#[from]
255	NetworkBridgeUpdate(NetworkBridgeEvent<net_protocol::CollatorProtocolMessage>),
256	/// We recommended a particular candidate to be seconded, but it was invalid; penalize the
257	/// collator.
258	///
259	/// The hash is the relay parent.
260	Invalid(Hash, CandidateReceipt),
261	/// The candidate we recommended to be seconded was validated successfully.
262	///
263	/// The hash is the relay parent.
264	Seconded(Hash, SignedFullStatement),
265	/// A message sent by Pezcumulus consensus engine to the collator protocol to
266	/// pre-connect to backing groups at all allowed relay parents.
267	ConnectToBackingGroups,
268	/// A message sent by Pezcumulus consensus engine to the collator protocol to
269	/// disconnect from backing groups.
270	DisconnectFromBackingGroups,
271}
272
273impl Default for CollatorProtocolMessage {
274	fn default() -> Self {
275		Self::CollateOn(Default::default())
276	}
277}
278
279/// Messages received by the dispute coordinator subsystem.
280///
281/// NOTE: Any response oneshots might get cancelled if the `DisputeCoordinator` was not yet
282/// properly initialized for some reason.
283#[derive(Debug)]
284pub enum DisputeCoordinatorMessage {
285	/// Import statements by validators about a candidate.
286	///
287	/// The subsystem will silently discard ancient statements or sets of only dispute-specific
288	/// statements for candidates that are previously unknown to the subsystem. The former is
289	/// simply because ancient data is not relevant and the latter is as a DoS prevention
290	/// mechanism. Both backing and approval statements already undergo anti-DoS procedures in
291	/// their respective subsystems, but statements cast specifically for disputes are not
292	/// necessarily relevant to any candidate the system is already aware of and thus present a DoS
293	/// vector. Our expectation is that nodes will notify each other of disputes over the network
294	/// by providing (at least) 2 conflicting statements, of which one is either a backing or
295	/// validation statement.
296	///
297	/// This does not do any checking of the message signature.
298	ImportStatements {
299		/// The candidate receipt itself.
300		candidate_receipt: CandidateReceipt,
301		/// The session the candidate appears in.
302		session: SessionIndex,
303		/// Statements, with signatures checked, by validators participating in disputes.
304		///
305		/// The validator index passed alongside each statement should correspond to the index
306		/// of the validator in the set.
307		statements: Vec<(SignedDisputeStatement, ValidatorIndex)>,
308		/// Inform the requester once we finished importing (if a sender was provided).
309		///
310		/// This is:
311		/// - we discarded the votes because
312		/// 		- they were ancient or otherwise invalid (result: `InvalidImport`)
313		/// 		- or we were not able to recover availability for an unknown candidate (result:
314		///		`InvalidImport`)
315		/// 		- or were known already (in that case the result will still be `ValidImport`)
316		/// - or we recorded them because (`ValidImport`)
317		/// 		- we cast our own vote already on that dispute
318		/// 		- or we have approval votes on that candidate
319		/// 		- or other explicit votes on that candidate already recorded
320		/// 		- or recovered availability for the candidate
321		/// 		- or the imported statements are backing/approval votes, which are always accepted.
322		pending_confirmation: Option<oneshot::Sender<ImportStatementsResult>>,
323	},
324	/// Fetch a list of all recent disputes the coordinator is aware of.
325	/// These are disputes which have occurred any time in recent sessions,
326	/// and which may have already concluded.
327	RecentDisputes(oneshot::Sender<BTreeMap<(SessionIndex, CandidateHash), DisputeStatus>>),
328	/// Fetch a list of all active disputes that the coordinator is aware of.
329	/// These disputes are either not yet concluded or recently concluded.
330	ActiveDisputes(oneshot::Sender<BTreeMap<(SessionIndex, CandidateHash), DisputeStatus>>),
331	/// Get candidate votes for a candidate.
332	QueryCandidateVotes(
333		Vec<(SessionIndex, CandidateHash)>,
334		oneshot::Sender<Vec<(SessionIndex, CandidateHash, CandidateVotes)>>,
335	),
336	/// Sign and issue local dispute votes. A value of `true` indicates validity, and `false`
337	/// invalidity.
338	IssueLocalStatement(SessionIndex, CandidateHash, CandidateReceipt, bool),
339	/// Determine the highest undisputed block within the given chain, based on where candidates
340	/// were included. If even the base block should not be finalized due to a dispute,
341	/// then `None` should be returned on the channel.
342	///
343	/// The block descriptions begin counting upwards from the block after the given `base_number`.
344	/// The `base_number` is typically the number of the last finalized block but may be slightly
345	/// higher. This block is inevitably going to be finalized so it is not accounted for by this
346	/// function.
347	DetermineUndisputedChain {
348		/// The lowest possible block to vote on.
349		base: (BlockNumber, Hash),
350		/// Descriptions of all the blocks counting upwards from the block after the base number
351		block_descriptions: Vec<BlockDescription>,
352		/// The block to vote on, might be base in case there is no better.
353		tx: oneshot::Sender<(BlockNumber, Hash)>,
354	},
355}
356
357/// The result of `DisputeCoordinatorMessage::ImportStatements`.
358#[derive(Copy, Clone, Debug, PartialEq, Eq)]
359pub enum ImportStatementsResult {
360	/// Import was invalid (candidate was not available)  and the sending peer should get banned.
361	InvalidImport,
362	/// Import was valid and can be confirmed to peer.
363	ValidImport,
364}
365
366/// Messages going to the dispute distribution subsystem.
367#[derive(Debug)]
368pub enum DisputeDistributionMessage {
369	/// Tell dispute distribution to distribute an explicit dispute statement to
370	/// validators.
371	SendDispute(DisputeMessage),
372}
373
374/// Messages received from other subsystems.
375#[derive(Debug)]
376pub enum NetworkBridgeRxMessage {
377	/// Inform the distribution subsystems about the new
378	/// gossip network topology formed.
379	///
380	/// The only reason to have this here, is the availability of the
381	/// authority discovery service, otherwise, the `GossipSupport`
382	/// subsystem would make more sense.
383	NewGossipTopology {
384		/// The session info this gossip topology is concerned with.
385		session: SessionIndex,
386		/// Our validator index in the session, if any.
387		local_index: Option<ValidatorIndex>,
388		/// The canonical shuffling of validators for the session.
389		canonical_shuffling: Vec<(AuthorityDiscoveryId, ValidatorIndex)>,
390		/// The reverse mapping of `canonical_shuffling`: from validator index
391		/// to the index in `canonical_shuffling`
392		shuffled_indices: Vec<usize>,
393	},
394	/// Inform the distribution subsystems about `AuthorityDiscoveryId` key rotations.
395	UpdatedAuthorityIds {
396		/// The `PeerId` of the peer that updated its `AuthorityDiscoveryId`s.
397		peer_id: PeerId,
398		/// The updated authority discovery keys of the peer.
399		authority_ids: HashSet<AuthorityDiscoveryId>,
400	},
401}
402
403/// Type of peer reporting
404#[derive(Debug)]
405pub enum ReportPeerMessage {
406	/// Single peer report about malicious actions which should be sent right away
407	Single(PeerId, ReputationChange),
408	/// Delayed report for other actions.
409	Batch(HashMap<PeerId, i32>),
410}
411
412/// Messages received from other subsystems by the network bridge subsystem.
413#[derive(Debug)]
414pub enum NetworkBridgeTxMessage {
415	/// Report a peer for their actions.
416	ReportPeer(ReportPeerMessage),
417
418	/// Disconnect peers from the given peer-set without affecting their reputation.
419	DisconnectPeers(Vec<PeerId>, PeerSet),
420
421	/// Send a message to one or more peers on the validation peer-set.
422	SendValidationMessage(Vec<PeerId>, net_protocol::VersionedValidationProtocol),
423
424	/// Send a message to one or more peers on the collation peer-set.
425	SendCollationMessage(Vec<PeerId>, net_protocol::VersionedCollationProtocol),
426
427	/// Send a batch of validation messages.
428	///
429	/// NOTE: Messages will be processed in order (at least statement distribution relies on this).
430	SendValidationMessages(Vec<(Vec<PeerId>, net_protocol::VersionedValidationProtocol)>),
431
432	/// Send a batch of collation messages.
433	///
434	/// NOTE: Messages will be processed in order.
435	SendCollationMessages(Vec<(Vec<PeerId>, net_protocol::VersionedCollationProtocol)>),
436
437	/// Send requests via bizinikiwi request/response.
438	/// Second parameter, tells what to do if we are not yet connected to the peer.
439	SendRequests(Vec<Requests>, IfDisconnected),
440
441	/// Connect to peers who represent the given `validator_ids`.
442	///
443	/// Also ask the network to stay connected to these peers at least
444	/// until a new request is issued.
445	///
446	/// Because it overrides the previous request, it must be ensured
447	/// that `validator_ids` include all peers the subsystems
448	/// are interested in (per `PeerSet`).
449	///
450	/// A caller can learn about validator connections by listening to the
451	/// `PeerConnected` events from the network bridge.
452	ConnectToValidators {
453		/// Ids of the validators to connect to.
454		validator_ids: Vec<AuthorityDiscoveryId>,
455		/// The underlying protocol to use for this request.
456		peer_set: PeerSet,
457		/// Sends back the number of `AuthorityDiscoveryId`s which
458		/// authority discovery has failed to resolve.
459		failed: oneshot::Sender<usize>,
460	},
461	/// Alternative to `ConnectToValidators` in case you already know the `Multiaddrs` you want to
462	/// be connected to.
463	ConnectToResolvedValidators {
464		/// Each entry corresponds to the addresses of an already resolved validator.
465		validator_addrs: Vec<HashSet<Multiaddr>>,
466		/// The peer set we want the connection on.
467		peer_set: PeerSet,
468	},
469
470	/// Extends the known validators set with new peers we already know the `Multiaddrs`, this is
471	/// usually needed for validators that change their address mid-session. It is usually called
472	/// after a ConnectToResolvedValidators at the beginning of the session.
473	AddToResolvedValidators {
474		/// Each entry corresponds to the addresses of an already resolved validator.
475		validator_addrs: Vec<HashSet<Multiaddr>>,
476		/// The peer set we want the connection on.
477		peer_set: PeerSet,
478	},
479}
480
481/// Availability Distribution Message.
482#[derive(Debug)]
483pub enum AvailabilityDistributionMessage {
484	/// Instruct availability distribution to fetch a remote PoV.
485	///
486	/// NOTE: The result of this fetch is not yet locally validated and could be bogus.
487	FetchPoV {
488		/// The relay parent giving the necessary context.
489		relay_parent: Hash,
490		/// Validator to fetch the PoV from.
491		from_validator: ValidatorIndex,
492		/// The id of the teyrchain that produced this PoV.
493		/// This field is only used to provide more context when logging errors
494		/// from the `AvailabilityDistribution` subsystem.
495		para_id: ParaId,
496		/// Candidate hash to fetch the PoV for.
497		candidate_hash: CandidateHash,
498		/// Expected hash of the PoV, a PoV not matching this hash will be rejected.
499		pov_hash: Hash,
500		/// Sender for getting back the result of this fetch.
501		///
502		/// The sender will be canceled if the fetching failed for some reason.
503		tx: oneshot::Sender<PoV>,
504	},
505}
506
507/// Availability Recovery Message.
508#[derive(Debug, derive_more::From)]
509pub enum AvailabilityRecoveryMessage {
510	/// Recover available data from validators on the network.
511	RecoverAvailableData(
512		CandidateReceipt,
513		SessionIndex,
514		Option<GroupIndex>, // Optional backing group to request from first.
515		Option<CoreIndex>,  /* A `CoreIndex` needs to be specified for the recovery process to
516		                     * prefer systematic chunk recovery. */
517		oneshot::Sender<Result<AvailableData, crate::errors::RecoveryError>>,
518	),
519}
520
521/// Bitfield distribution message.
522#[derive(Debug, derive_more::From)]
523pub enum BitfieldDistributionMessage {
524	/// Distribute a bitfield via gossip to other validators.
525	DistributeBitfield(Hash, SignedAvailabilityBitfield),
526
527	/// Event from the network bridge.
528	#[from]
529	NetworkBridgeUpdate(NetworkBridgeEvent<net_protocol::BitfieldDistributionMessage>),
530}
531
532/// Availability store subsystem message.
533#[derive(Debug)]
534pub enum AvailabilityStoreMessage {
535	/// Query a `AvailableData` from the AV store.
536	QueryAvailableData(CandidateHash, oneshot::Sender<Option<AvailableData>>),
537
538	/// Query whether a `AvailableData` exists within the AV Store.
539	///
540	/// This is useful in cases when existence
541	/// matters, but we don't want to necessarily pass around multiple
542	/// megabytes of data to get a single bit of information.
543	QueryDataAvailability(CandidateHash, oneshot::Sender<bool>),
544
545	/// Query an `ErasureChunk` from the AV store by the candidate hash and validator index.
546	QueryChunk(CandidateHash, ValidatorIndex, oneshot::Sender<Option<ErasureChunk>>),
547
548	/// Get the size of an `ErasureChunk` from the AV store by the candidate hash.
549	QueryChunkSize(CandidateHash, oneshot::Sender<Option<usize>>),
550
551	/// Query all chunks that we have for the given candidate hash.
552	QueryAllChunks(CandidateHash, oneshot::Sender<Vec<(ValidatorIndex, ErasureChunk)>>),
553
554	/// Query whether an `ErasureChunk` exists within the AV Store.
555	///
556	/// This is useful in cases like bitfield signing, when existence
557	/// matters, but we don't want to necessarily pass around large
558	/// quantities of data to get a single bit of information.
559	QueryChunkAvailability(CandidateHash, ValidatorIndex, oneshot::Sender<bool>),
560
561	/// Store an `ErasureChunk` in the AV store.
562	///
563	/// Return `Ok(())` if the store operation succeeded, `Err(())` if it failed.
564	StoreChunk {
565		/// A hash of the candidate this chunk belongs to.
566		candidate_hash: CandidateHash,
567		/// Validator index. May not be equal to the chunk index.
568		validator_index: ValidatorIndex,
569		/// The chunk itself.
570		chunk: ErasureChunk,
571		/// Sending side of the channel to send result to.
572		tx: oneshot::Sender<Result<(), ()>>,
573	},
574
575	/// Computes and checks the erasure root of `AvailableData` before storing all of its chunks in
576	/// the AV store.
577	///
578	/// Return `Ok(())` if the store operation succeeded, `Err(StoreAvailableData)` if it failed.
579	StoreAvailableData {
580		/// A hash of the candidate this `available_data` belongs to.
581		candidate_hash: CandidateHash,
582		/// The number of validators in the session.
583		n_validators: u32,
584		/// The `AvailableData` itself.
585		available_data: AvailableData,
586		/// Erasure root we expect to get after chunking.
587		expected_erasure_root: Hash,
588		/// Core index where the candidate was backed.
589		core_index: CoreIndex,
590		/// Node features at the candidate relay parent. Used for computing the validator->chunk
591		/// mapping.
592		node_features: NodeFeatures,
593		/// Sending side of the channel to send result to.
594		tx: oneshot::Sender<Result<(), StoreAvailableDataError>>,
595	},
596}
597
598/// The error result type of a [`AvailabilityStoreMessage::StoreAvailableData`] request.
599#[derive(Error, Debug, Clone, PartialEq, Eq)]
600#[allow(missing_docs)]
601pub enum StoreAvailableDataError {
602	#[error("The computed erasure root did not match expected one")]
603	InvalidErasureRoot,
604}
605
606/// A response channel for the result of a chain API request.
607pub type ChainApiResponseChannel<T> = oneshot::Sender<Result<T, crate::errors::ChainApiError>>;
608
609/// Chain API request subsystem message.
610#[derive(Debug)]
611pub enum ChainApiMessage {
612	/// Request the block number by hash.
613	/// Returns `None` if a block with the given hash is not present in the db.
614	BlockNumber(Hash, ChainApiResponseChannel<Option<BlockNumber>>),
615	/// Request the block header by hash.
616	/// Returns `None` if a block with the given hash is not present in the db.
617	BlockHeader(Hash, ChainApiResponseChannel<Option<BlockHeader>>),
618	/// Get the cumulative weight of the given block, by hash.
619	/// If the block or weight is unknown, this returns `None`.
620	///
621	/// Note: this is the weight within the low-level fork-choice rule,
622	/// not the high-level one implemented in the chain-selection subsystem.
623	///
624	/// Weight is used for comparing blocks in a fork-choice rule.
625	BlockWeight(Hash, ChainApiResponseChannel<Option<BlockWeight>>),
626	/// Request the finalized block hash by number.
627	/// Returns `None` if a block with the given number is not present in the db.
628	/// Note: the caller must ensure the block is finalized.
629	FinalizedBlockHash(BlockNumber, ChainApiResponseChannel<Option<Hash>>),
630	/// Request the last finalized block number.
631	/// This request always succeeds.
632	FinalizedBlockNumber(ChainApiResponseChannel<BlockNumber>),
633	/// Request the `k` ancestor block hashes of a block with the given hash.
634	/// The response channel may return a `Vec` of size up to `k`
635	/// filled with ancestors hashes with the following order:
636	/// `parent`, `grandparent`, ... up to the hash of genesis block
637	/// with number 0, including it.
638	Ancestors {
639		/// The hash of the block in question.
640		hash: Hash,
641		/// The number of ancestors to request.
642		k: usize,
643		/// The response channel.
644		response_channel: ChainApiResponseChannel<Vec<Hash>>,
645	},
646}
647
648/// Chain selection subsystem messages
649#[derive(Debug)]
650pub enum ChainSelectionMessage {
651	/// Signal to the chain selection subsystem that a specific block has been approved.
652	Approved(Hash),
653	/// Request the leaves in descending order by score.
654	Leaves(oneshot::Sender<Vec<Hash>>),
655	/// Request the best leaf containing the given block in its ancestry. Return `None` if
656	/// there is no such leaf.
657	BestLeafContaining(Hash, oneshot::Sender<Option<Hash>>),
658	/// The passed blocks must be marked as reverted, and their children must be marked
659	/// as non-viable.
660	RevertBlocks(Vec<(BlockNumber, Hash)>),
661}
662
663/// A sender for the result of a runtime API request.
664pub type RuntimeApiSender<T> = oneshot::Sender<Result<T, crate::errors::RuntimeApiError>>;
665
666/// A request to the Runtime API subsystem.
667#[derive(Debug)]
668pub enum RuntimeApiRequest {
669	/// Get the version of the runtime API, if any.
670	Version(RuntimeApiSender<u32>),
671	/// Get the next, current and some previous authority discovery set deduplicated.
672	Authorities(RuntimeApiSender<Vec<AuthorityDiscoveryId>>),
673	/// Get the current validator set.
674	Validators(RuntimeApiSender<Vec<ValidatorId>>),
675	/// Get the validator groups and group rotation info.
676	ValidatorGroups(RuntimeApiSender<(Vec<Vec<ValidatorIndex>>, GroupRotationInfo)>),
677	/// Get information on all availability cores.
678	AvailabilityCores(RuntimeApiSender<Vec<CoreState>>),
679	/// Get the persisted validation data for a particular para, taking the given
680	/// `OccupiedCoreAssumption`, which will inform on how the validation data should be computed
681	/// if the para currently occupies a core.
682	PersistedValidationData(
683		ParaId,
684		OccupiedCoreAssumption,
685		RuntimeApiSender<Option<PersistedValidationData>>,
686	),
687	/// Get the persisted validation data for a particular para along with the current validation
688	/// code hash, matching the data hash against an expected one.
689	AssumedValidationData(
690		ParaId,
691		Hash,
692		RuntimeApiSender<Option<(PersistedValidationData, ValidationCodeHash)>>,
693	),
694	/// Sends back `true` if the validation outputs pass all acceptance criteria checks.
695	CheckValidationOutputs(
696		ParaId,
697		pezkuwi_primitives::CandidateCommitments,
698		RuntimeApiSender<bool>,
699	),
700	/// Get the session index that a child of the block will have.
701	SessionIndexForChild(RuntimeApiSender<SessionIndex>),
702	/// Get the validation code for a para, taking the given `OccupiedCoreAssumption`, which
703	/// will inform on how the validation data should be computed if the para currently
704	/// occupies a core.
705	ValidationCode(ParaId, OccupiedCoreAssumption, RuntimeApiSender<Option<ValidationCode>>),
706	/// Get validation code by its hash, either past, current or future code can be returned, as
707	/// long as state is still available.
708	ValidationCodeByHash(ValidationCodeHash, RuntimeApiSender<Option<ValidationCode>>),
709	/// Get the candidate pending availability for a particular teyrchain by teyrchain / core
710	/// index
711	CandidatePendingAvailability(ParaId, RuntimeApiSender<Option<CommittedCandidateReceipt>>),
712	/// Get all events concerning candidates (backing, inclusion, time-out) in the parent of
713	/// the block in whose state this request is executed.
714	CandidateEvents(RuntimeApiSender<Vec<CandidateEvent>>),
715	/// Get the execution environment parameter set by session index
716	SessionExecutorParams(SessionIndex, RuntimeApiSender<Option<ExecutorParams>>),
717	/// Get the session info for the given session, if stored.
718	SessionInfo(SessionIndex, RuntimeApiSender<Option<SessionInfo>>),
719	/// Get all the pending inbound messages in the downward message queue for a para.
720	DmqContents(ParaId, RuntimeApiSender<Vec<InboundDownwardMessage<BlockNumber>>>),
721	/// Get the contents of all channels addressed to the given recipient. Channels that have no
722	/// messages in them are also included.
723	InboundHrmpChannelsContents(
724		ParaId,
725		RuntimeApiSender<BTreeMap<ParaId, Vec<InboundHrmpMessage<BlockNumber>>>>,
726	),
727	/// Get information about the BABE epoch the block was included in.
728	CurrentBabeEpoch(RuntimeApiSender<BabeEpoch>),
729	/// Get all disputes in relation to a relay parent.
730	FetchOnChainVotes(RuntimeApiSender<Option<pezkuwi_primitives::ScrapedOnChainVotes>>),
731	/// Submits a PVF pre-checking statement into the transaction pool.
732	SubmitPvfCheckStatement(PvfCheckStatement, ValidatorSignature, RuntimeApiSender<()>),
733	/// Returns code hashes of PVFs that require pre-checking by validators in the active set.
734	PvfsRequirePrecheck(RuntimeApiSender<Vec<ValidationCodeHash>>),
735	/// Get the validation code used by the specified para, taking the given
736	/// `OccupiedCoreAssumption`, which will inform on how the validation data should be computed
737	/// if the para currently occupies a core.
738	ValidationCodeHash(
739		ParaId,
740		OccupiedCoreAssumption,
741		RuntimeApiSender<Option<ValidationCodeHash>>,
742	),
743	/// Returns all on-chain disputes at given block number. Available in `v3`.
744	Disputes(RuntimeApiSender<Vec<(SessionIndex, CandidateHash, DisputeState<BlockNumber>)>>),
745	/// Returns a list of validators that lost a past session dispute and need to be slashed.
746	/// `V5`
747	UnappliedSlashes(
748		RuntimeApiSender<Vec<(SessionIndex, CandidateHash, slashing::LegacyPendingSlashes)>>,
749	),
750	/// Returns a merkle proof of a validator session key.
751	/// `V5`
752	KeyOwnershipProof(ValidatorId, RuntimeApiSender<Option<slashing::OpaqueKeyOwnershipProof>>),
753	/// Submits an unsigned extrinsic to slash validator who lost a past session dispute.
754	/// `V5`
755	SubmitReportDisputeLost(
756		slashing::DisputeProof,
757		slashing::OpaqueKeyOwnershipProof,
758		RuntimeApiSender<Option<()>>,
759	),
760	/// Get the minimum required backing votes.
761	MinimumBackingVotes(SessionIndex, RuntimeApiSender<u32>),
762	/// Returns all disabled validators at a given block height.
763	DisabledValidators(RuntimeApiSender<Vec<ValidatorIndex>>),
764	/// Get the backing state of the given para.
765	ParaBackingState(ParaId, RuntimeApiSender<Option<async_backing::BackingState>>),
766	/// Get candidate's acceptance limitations for asynchronous backing for a relay parent.
767	///
768	/// If it's not supported by the Runtime, the async backing is said to be disabled.
769	AsyncBackingParams(RuntimeApiSender<async_backing::AsyncBackingParams>),
770	/// Get the node features.
771	NodeFeatures(SessionIndex, RuntimeApiSender<NodeFeatures>),
772	/// Approval voting params
773	/// `V10`
774	ApprovalVotingParams(SessionIndex, RuntimeApiSender<ApprovalVotingParams>),
775	/// Fetch the `ClaimQueue` from scheduler pezpallet
776	/// `V11`
777	ClaimQueue(RuntimeApiSender<BTreeMap<CoreIndex, VecDeque<ParaId>>>),
778	/// Get the candidates pending availability for a particular teyrchain
779	/// `V11`
780	CandidatesPendingAvailability(ParaId, RuntimeApiSender<Vec<CommittedCandidateReceipt>>),
781	/// Get the backing constraints for a particular teyrchain.
782	/// `V12`
783	BackingConstraints(ParaId, RuntimeApiSender<Option<Constraints>>),
784	/// Get the lookahead from the scheduler params.
785	/// `V12`
786	SchedulingLookahead(SessionIndex, RuntimeApiSender<u32>),
787	/// Get the maximum uncompressed code size.
788	/// `V12`
789	ValidationCodeBombLimit(SessionIndex, RuntimeApiSender<u32>),
790	/// Get the paraids at the relay parent.
791	/// `V14`
792	ParaIds(SessionIndex, RuntimeApiSender<Vec<ParaId>>),
793	/// Returns a list of validators that lost a past session dispute and need to be slashed (v2).
794	/// `V15`
795	UnappliedSlashesV2(
796		RuntimeApiSender<Vec<(SessionIndex, CandidateHash, slashing::PendingSlashes)>>,
797	),
798}
799
800impl RuntimeApiRequest {
801	/// Runtime version requirements for each message
802
803	/// `Disputes`
804	pub const DISPUTES_RUNTIME_REQUIREMENT: u32 = 3;
805
806	/// `ExecutorParams`
807	pub const EXECUTOR_PARAMS_RUNTIME_REQUIREMENT: u32 = 4;
808
809	/// `UnappliedSlashes`
810	pub const UNAPPLIED_SLASHES_RUNTIME_REQUIREMENT: u32 = 5;
811
812	/// `KeyOwnershipProof`
813	pub const KEY_OWNERSHIP_PROOF_RUNTIME_REQUIREMENT: u32 = 5;
814
815	/// `SubmitReportDisputeLost`
816	pub const SUBMIT_REPORT_DISPUTE_LOST_RUNTIME_REQUIREMENT: u32 = 5;
817
818	/// `MinimumBackingVotes`
819	pub const MINIMUM_BACKING_VOTES_RUNTIME_REQUIREMENT: u32 = 6;
820
821	/// Minimum version to enable asynchronous backing: `AsyncBackingParams` and `ParaBackingState`.
822	pub const ASYNC_BACKING_STATE_RUNTIME_REQUIREMENT: u32 = 7;
823
824	/// `DisabledValidators`
825	pub const DISABLED_VALIDATORS_RUNTIME_REQUIREMENT: u32 = 8;
826
827	/// `Node features`
828	pub const NODE_FEATURES_RUNTIME_REQUIREMENT: u32 = 9;
829
830	/// `approval_voting_params`
831	pub const APPROVAL_VOTING_PARAMS_REQUIREMENT: u32 = 10;
832
833	/// `ClaimQueue`
834	pub const CLAIM_QUEUE_RUNTIME_REQUIREMENT: u32 = 11;
835
836	/// `candidates_pending_availability`
837	pub const CANDIDATES_PENDING_AVAILABILITY_RUNTIME_REQUIREMENT: u32 = 11;
838
839	/// `ValidationCodeBombLimit`
840	pub const VALIDATION_CODE_BOMB_LIMIT_RUNTIME_REQUIREMENT: u32 = 12;
841
842	/// `backing_constraints`
843	pub const CONSTRAINTS_RUNTIME_REQUIREMENT: u32 = 13;
844
845	/// `SchedulingLookahead`
846	pub const SCHEDULING_LOOKAHEAD_RUNTIME_REQUIREMENT: u32 = 13;
847
848	/// `ParaIds`
849	pub const PARAIDS_RUNTIME_REQUIREMENT: u32 = 14;
850
851	/// `UnappliedSlashesV2`
852	pub const UNAPPLIED_SLASHES_V2_RUNTIME_REQUIREMENT: u32 = 15;
853}
854
855/// A message to the Runtime API subsystem.
856#[derive(Debug)]
857pub enum RuntimeApiMessage {
858	/// Make a request of the runtime API against the post-state of the given relay-parent.
859	Request(Hash, RuntimeApiRequest),
860}
861
862/// Statement distribution message.
863#[derive(Debug, derive_more::From)]
864pub enum StatementDistributionMessage {
865	/// We have originated a signed statement in the context of
866	/// given relay-parent hash and it should be distributed to other validators.
867	Share(Hash, SignedFullStatementWithPVD),
868	/// The candidate received enough validity votes from the backing group.
869	///
870	/// If the candidate is backed as a result of a local statement, this message MUST
871	/// be preceded by a `Share` message for that statement. This ensures that Statement
872	/// Distribution is always aware of full candidates prior to receiving the `Backed`
873	/// notification, even when the group size is 1 and the candidate is seconded locally.
874	Backed(CandidateHash),
875	/// Event from the network bridge.
876	#[from]
877	NetworkBridgeUpdate(NetworkBridgeEvent<net_protocol::StatementDistributionMessage>),
878}
879
880/// This data becomes intrinsics or extrinsics which should be included in a future relay chain
881/// block.
882// It needs to be clonable because multiple potential block authors can request copies.
883#[derive(Debug, Clone)]
884pub enum ProvisionableData {
885	/// This bitfield indicates the availability of various candidate blocks.
886	Bitfield(Hash, SignedAvailabilityBitfield),
887	/// Misbehavior reports are self-contained proofs of validator misbehavior.
888	MisbehaviorReport(Hash, ValidatorIndex, Misbehavior),
889	/// Disputes trigger a broad dispute resolution process.
890	Dispute(Hash, ValidatorSignature),
891}
892
893/// Inherent data returned by the provisioner
894#[derive(Debug, Clone)]
895pub struct ProvisionerInherentData {
896	/// Signed bitfields.
897	pub bitfields: SignedAvailabilityBitfields,
898	/// Backed candidates.
899	pub backed_candidates: Vec<BackedCandidate>,
900	/// Dispute statement sets.
901	pub disputes: MultiDisputeStatementSet,
902}
903
904/// Message to the Provisioner.
905///
906/// In all cases, the Hash is that of the relay parent.
907#[derive(Debug)]
908pub enum ProvisionerMessage {
909	/// This message allows external subsystems to request the set of bitfields and backed
910	/// candidates associated with a particular potential block hash.
911	///
912	/// This is expected to be used by a proposer, to inject that information into the
913	/// `InherentData` where it can be assembled into the `ParaInherent`.
914	RequestInherentData(Hash, oneshot::Sender<ProvisionerInherentData>),
915	/// This data should become part of a relay chain block
916	ProvisionableData(Hash, ProvisionableData),
917}
918
919/// Message to the Collation Generation subsystem.
920#[derive(Debug)]
921pub enum CollationGenerationMessage {
922	/// Initialize the collation generation subsystem.
923	Initialize(CollationGenerationConfig),
924	/// Reinitialize the collation generation subsystem, overriding the existing config.
925	Reinitialize(CollationGenerationConfig),
926	/// Submit a collation to the subsystem. This will package it into a signed
927	/// [`CommittedCandidateReceipt`] and distribute along the network to validators.
928	///
929	/// If sent before `Initialize`, this will be ignored.
930	SubmitCollation(SubmitCollationParams),
931}
932
933/// The result type of [`ApprovalVotingMessage::ImportAssignment`] request.
934#[derive(Debug, Clone, PartialEq, Eq)]
935pub enum AssignmentCheckResult {
936	/// The vote was accepted and should be propagated onwards.
937	Accepted,
938	/// The vote was valid but duplicate and should not be propagated onwards.
939	AcceptedDuplicate,
940	/// The vote was valid but too far in the future to accept right now.
941	TooFarInFuture,
942	/// The vote was bad and should be ignored, reporting the peer who propagated it.
943	Bad(AssignmentCheckError),
944}
945
946/// The error result type of [`ApprovalVotingMessage::ImportAssignment`] request.
947#[derive(Error, Debug, Clone, PartialEq, Eq)]
948#[allow(missing_docs)]
949pub enum AssignmentCheckError {
950	#[error("Unknown block: {0:?}")]
951	UnknownBlock(Hash),
952	#[error("Unknown session index: {0}")]
953	UnknownSessionIndex(SessionIndex),
954	#[error("Invalid candidate index: {0}")]
955	InvalidCandidateIndex(CandidateIndex),
956	#[error("Invalid candidate {0}: {1:?}")]
957	InvalidCandidate(CandidateIndex, CandidateHash),
958	#[error("Invalid cert: {0:?}, reason: {1}")]
959	InvalidCert(ValidatorIndex, String),
960	#[error("Internal state mismatch: {0:?}, {1:?}")]
961	Internal(Hash, CandidateHash),
962	#[error("Oversized candidate or core bitfield >= {0}")]
963	InvalidBitfield(usize),
964}
965
966/// The result type of [`ApprovalVotingMessage::ImportApproval`] request.
967#[derive(Debug, Clone, PartialEq, Eq)]
968pub enum ApprovalCheckResult {
969	/// The vote was accepted and should be propagated onwards.
970	Accepted,
971	/// The vote was bad and should be ignored, reporting the peer who propagated it.
972	Bad(ApprovalCheckError),
973}
974
975/// The error result type of [`ApprovalVotingMessage::ImportApproval`] request.
976#[derive(Error, Debug, Clone, PartialEq, Eq)]
977#[allow(missing_docs)]
978pub enum ApprovalCheckError {
979	#[error("Unknown block: {0:?}")]
980	UnknownBlock(Hash),
981	#[error("Unknown session index: {0}")]
982	UnknownSessionIndex(SessionIndex),
983	#[error("Invalid candidate index: {0}")]
984	InvalidCandidateIndex(CandidateIndex),
985	#[error("Invalid validator index: {0:?}")]
986	InvalidValidatorIndex(ValidatorIndex),
987	#[error("Invalid candidate {0}: {1:?}")]
988	InvalidCandidate(CandidateIndex, CandidateHash),
989	#[error("Invalid signature: {0:?}")]
990	InvalidSignature(ValidatorIndex),
991	#[error("No assignment for {0:?}")]
992	NoAssignment(ValidatorIndex),
993	#[error("Internal state mismatch: {0:?}, {1:?}")]
994	Internal(Hash, CandidateHash),
995}
996
997/// Describes a relay-chain block by the para-chain candidates
998/// it includes.
999#[derive(Clone, Debug)]
1000pub struct BlockDescription {
1001	/// The relay-chain block hash.
1002	pub block_hash: Hash,
1003	/// The session index of this block.
1004	pub session: SessionIndex,
1005	/// The set of para-chain candidates.
1006	pub candidates: Vec<CandidateHash>,
1007}
1008
1009/// Message to the approval voting parallel subsystem running both approval-distribution and
1010/// approval-voting logic in parallel. This is a combination of all the messages ApprovalVoting and
1011/// ApprovalDistribution subsystems can receive.
1012///
1013/// The reason this exists is, so that we can keep both modes of running in the same pezkuwi
1014/// binary, based on the value of `--approval-voting-parallel-enabled`, we decide if we run with two
1015/// different subsystems for approval-distribution and approval-voting or run the approval-voting
1016/// parallel which has several parallel workers for the approval-distribution and a worker for
1017/// approval-voting.
1018///
1019/// This is meant to be a temporary state until we can safely remove running the two subsystems
1020/// individually.
1021#[derive(Debug, derive_more::From)]
1022pub enum ApprovalVotingParallelMessage {
1023	/// Gets mapped into `ApprovalVotingMessage::ApprovedAncestor`
1024	ApprovedAncestor(Hash, BlockNumber, oneshot::Sender<Option<HighestApprovedAncestorBlock>>),
1025
1026	/// Gets mapped into `ApprovalVotingMessage::GetApprovalSignaturesForCandidate`
1027	GetApprovalSignaturesForCandidate(
1028		CandidateHash,
1029		oneshot::Sender<HashMap<ValidatorIndex, (Vec<CandidateHash>, ValidatorSignature)>>,
1030	),
1031	/// Gets mapped into `ApprovalDistributionMessage::NewBlocks`
1032	NewBlocks(Vec<BlockApprovalMeta>),
1033	/// Gets mapped into `ApprovalDistributionMessage::DistributeAssignment`
1034	DistributeAssignment(IndirectAssignmentCertV2, CandidateBitfield),
1035	/// Gets mapped into `ApprovalDistributionMessage::DistributeApproval`
1036	DistributeApproval(IndirectSignedApprovalVoteV2),
1037	/// An update from the network bridge, gets mapped into
1038	/// `ApprovalDistributionMessage::NetworkBridgeUpdate`
1039	#[from]
1040	NetworkBridgeUpdate(NetworkBridgeEvent<net_protocol::ApprovalDistributionMessage>),
1041
1042	/// Gets mapped into `ApprovalDistributionMessage::GetApprovalSignatures`
1043	GetApprovalSignatures(
1044		HashSet<(Hash, CandidateIndex)>,
1045		oneshot::Sender<HashMap<ValidatorIndex, (Hash, Vec<CandidateIndex>, ValidatorSignature)>>,
1046	),
1047	/// Gets mapped into `ApprovalDistributionMessage::ApprovalCheckingLagUpdate`
1048	ApprovalCheckingLagUpdate(BlockNumber),
1049}
1050
1051impl TryFrom<ApprovalVotingParallelMessage> for ApprovalVotingMessage {
1052	type Error = ();
1053
1054	fn try_from(msg: ApprovalVotingParallelMessage) -> Result<Self, Self::Error> {
1055		match msg {
1056			ApprovalVotingParallelMessage::ApprovedAncestor(hash, number, tx) => {
1057				Ok(ApprovalVotingMessage::ApprovedAncestor(hash, number, tx))
1058			},
1059			ApprovalVotingParallelMessage::GetApprovalSignaturesForCandidate(candidate, tx) => {
1060				Ok(ApprovalVotingMessage::GetApprovalSignaturesForCandidate(candidate, tx))
1061			},
1062			_ => Err(()),
1063		}
1064	}
1065}
1066
1067impl TryFrom<ApprovalVotingParallelMessage> for ApprovalDistributionMessage {
1068	type Error = ();
1069
1070	fn try_from(msg: ApprovalVotingParallelMessage) -> Result<Self, Self::Error> {
1071		match msg {
1072			ApprovalVotingParallelMessage::NewBlocks(blocks) => {
1073				Ok(ApprovalDistributionMessage::NewBlocks(blocks))
1074			},
1075			ApprovalVotingParallelMessage::DistributeAssignment(assignment, claimed_cores) => {
1076				Ok(ApprovalDistributionMessage::DistributeAssignment(assignment, claimed_cores))
1077			},
1078			ApprovalVotingParallelMessage::DistributeApproval(vote) => {
1079				Ok(ApprovalDistributionMessage::DistributeApproval(vote))
1080			},
1081			ApprovalVotingParallelMessage::NetworkBridgeUpdate(msg) => {
1082				Ok(ApprovalDistributionMessage::NetworkBridgeUpdate(msg))
1083			},
1084			ApprovalVotingParallelMessage::GetApprovalSignatures(candidate_indicies, tx) => {
1085				Ok(ApprovalDistributionMessage::GetApprovalSignatures(candidate_indicies, tx))
1086			},
1087			ApprovalVotingParallelMessage::ApprovalCheckingLagUpdate(lag) => {
1088				Ok(ApprovalDistributionMessage::ApprovalCheckingLagUpdate(lag))
1089			},
1090			_ => Err(()),
1091		}
1092	}
1093}
1094
1095impl From<ApprovalDistributionMessage> for ApprovalVotingParallelMessage {
1096	fn from(msg: ApprovalDistributionMessage) -> Self {
1097		match msg {
1098			ApprovalDistributionMessage::NewBlocks(blocks) => {
1099				ApprovalVotingParallelMessage::NewBlocks(blocks)
1100			},
1101			ApprovalDistributionMessage::DistributeAssignment(cert, bitfield) => {
1102				ApprovalVotingParallelMessage::DistributeAssignment(cert, bitfield)
1103			},
1104			ApprovalDistributionMessage::DistributeApproval(vote) => {
1105				ApprovalVotingParallelMessage::DistributeApproval(vote)
1106			},
1107			ApprovalDistributionMessage::NetworkBridgeUpdate(msg) => {
1108				ApprovalVotingParallelMessage::NetworkBridgeUpdate(msg)
1109			},
1110			ApprovalDistributionMessage::GetApprovalSignatures(candidate_indicies, tx) => {
1111				ApprovalVotingParallelMessage::GetApprovalSignatures(candidate_indicies, tx)
1112			},
1113			ApprovalDistributionMessage::ApprovalCheckingLagUpdate(lag) => {
1114				ApprovalVotingParallelMessage::ApprovalCheckingLagUpdate(lag)
1115			},
1116		}
1117	}
1118}
1119
1120/// Response type to `ApprovalVotingMessage::ApprovedAncestor`.
1121#[derive(Clone, Debug)]
1122pub struct HighestApprovedAncestorBlock {
1123	/// The block hash of the highest viable ancestor.
1124	pub hash: Hash,
1125	/// The block number of the highest viable ancestor.
1126	pub number: BlockNumber,
1127	/// Block descriptions in the direct path between the
1128	/// initially provided hash and the highest viable ancestor.
1129	/// Primarily for use with `DetermineUndisputedChain`.
1130	/// Must be sorted from lowest to highest block number.
1131	pub descriptions: Vec<BlockDescription>,
1132}
1133
1134/// A checked indirect assignment, the crypto for the cert has been validated
1135/// and the `candidate_bitfield` is correctly claimed at `delay_tranche`.
1136#[derive(Debug)]
1137pub struct CheckedIndirectAssignment {
1138	assignment: IndirectAssignmentCertV2,
1139	candidate_indices: CandidateBitfield,
1140	tranche: DelayTranche,
1141}
1142
1143impl CheckedIndirectAssignment {
1144	/// Builds a checked assignment from an assignment that was checked to be valid for the
1145	/// `claimed_candidate_indices` at the give tranche
1146	pub fn from_checked(
1147		assignment: IndirectAssignmentCertV2,
1148		claimed_candidate_indices: CandidateBitfield,
1149		tranche: DelayTranche,
1150	) -> Self {
1151		Self { assignment, candidate_indices: claimed_candidate_indices, tranche }
1152	}
1153
1154	/// Returns the indirect assignment.
1155	pub fn assignment(&self) -> &IndirectAssignmentCertV2 {
1156		&self.assignment
1157	}
1158
1159	/// Returns the candidate bitfield claimed by the assignment.
1160	pub fn candidate_indices(&self) -> &CandidateBitfield {
1161		&self.candidate_indices
1162	}
1163
1164	/// Returns the tranche this assignment is claimed at.
1165	pub fn tranche(&self) -> DelayTranche {
1166		self.tranche
1167	}
1168}
1169
1170/// A checked indirect signed approval vote.
1171///
1172/// The crypto for the vote has been validated and the signature can be trusted as being valid and
1173/// to correspond to the `validator_index` inside the structure.
1174#[derive(Debug, derive_more::Deref, derive_more::Into)]
1175pub struct CheckedIndirectSignedApprovalVote(IndirectSignedApprovalVoteV2);
1176
1177impl CheckedIndirectSignedApprovalVote {
1178	/// Builds a checked vote from a vote that was checked to be valid and correctly signed.
1179	pub fn from_checked(vote: IndirectSignedApprovalVoteV2) -> Self {
1180		Self(vote)
1181	}
1182}
1183
1184/// Message to the Approval Voting subsystem.
1185#[derive(Debug)]
1186pub enum ApprovalVotingMessage {
1187	/// Import an assignment into the approval-voting database.
1188	///
1189	/// Should not be sent unless the block hash is known and the VRF assignment checks out.
1190	ImportAssignment(CheckedIndirectAssignment, Option<oneshot::Sender<AssignmentCheckResult>>),
1191	/// Import an approval vote into approval-voting database
1192	///
1193	/// Should not be sent unless the block hash within the indirect vote is known, vote is
1194	/// correctly signed and we had a previous assignment for the candidate.
1195	ImportApproval(CheckedIndirectSignedApprovalVote, Option<oneshot::Sender<ApprovalCheckResult>>),
1196	/// Returns the highest possible ancestor hash of the provided block hash which is
1197	/// acceptable to vote on finality for.
1198	/// The `BlockNumber` provided is the number of the block's ancestor which is the
1199	/// earliest possible vote.
1200	///
1201	/// It can also return the same block hash, if that is acceptable to vote upon.
1202	/// Return `None` if the input hash is unrecognized.
1203	ApprovedAncestor(Hash, BlockNumber, oneshot::Sender<Option<HighestApprovedAncestorBlock>>),
1204
1205	/// Retrieve all available approval signatures for a candidate from approval-voting.
1206	///
1207	/// This message involves a linear search for candidates on each relay chain fork and also
1208	/// requires calling into `approval-distribution`: Calls should be infrequent and bounded.
1209	GetApprovalSignaturesForCandidate(
1210		CandidateHash,
1211		oneshot::Sender<HashMap<ValidatorIndex, (Vec<CandidateHash>, ValidatorSignature)>>,
1212	),
1213}
1214
1215/// Message to the Approval Distribution subsystem.
1216#[derive(Debug, derive_more::From)]
1217pub enum ApprovalDistributionMessage {
1218	/// Notify the `ApprovalDistribution` subsystem about new blocks
1219	/// and the candidates contained within them.
1220	NewBlocks(Vec<BlockApprovalMeta>),
1221	/// Distribute an assignment cert from the local validator. The cert is assumed
1222	/// to be valid, relevant, and for the given relay-parent and validator index.
1223	DistributeAssignment(IndirectAssignmentCertV2, CandidateBitfield),
1224	/// Distribute an approval vote for the local validator. The approval vote is assumed to be
1225	/// valid, relevant, and the corresponding approval already issued.
1226	/// If not, the subsystem is free to drop the message.
1227	DistributeApproval(IndirectSignedApprovalVoteV2),
1228	/// An update from the network bridge.
1229	#[from]
1230	NetworkBridgeUpdate(NetworkBridgeEvent<net_protocol::ApprovalDistributionMessage>),
1231
1232	/// Get all approval signatures for all chains a candidate appeared in.
1233	GetApprovalSignatures(
1234		HashSet<(Hash, CandidateIndex)>,
1235		oneshot::Sender<HashMap<ValidatorIndex, (Hash, Vec<CandidateIndex>, ValidatorSignature)>>,
1236	),
1237	/// Approval checking lag update measured in blocks.
1238	ApprovalCheckingLagUpdate(BlockNumber),
1239}
1240
1241/// Message to the Gossip Support subsystem.
1242#[derive(Debug, derive_more::From)]
1243pub enum GossipSupportMessage {
1244	/// Dummy constructor, so we can receive networking events.
1245	#[from]
1246	NetworkBridgeUpdate(NetworkBridgeEvent<net_protocol::GossipSupportNetworkMessage>),
1247}
1248
1249/// Request introduction of a seconded candidate into the prospective teyrchains subsystem.
1250#[derive(Debug, PartialEq, Eq, Clone)]
1251pub struct IntroduceSecondedCandidateRequest {
1252	/// The para-id of the candidate.
1253	pub candidate_para: ParaId,
1254	/// The candidate receipt itself.
1255	pub candidate_receipt: CommittedCandidateReceipt,
1256	/// The persisted validation data of the candidate.
1257	pub persisted_validation_data: PersistedValidationData,
1258}
1259
1260/// A hypothetical candidate to be evaluated for potential/actual membership
1261/// in the prospective teyrchains subsystem.
1262///
1263/// Hypothetical candidates are either complete or incomplete.
1264/// Complete candidates have already had their (potentially heavy)
1265/// candidate receipt fetched, while incomplete candidates are simply
1266/// claims about properties that a fetched candidate would have.
1267///
1268/// Complete candidates can be evaluated more strictly than incomplete candidates.
1269#[derive(Debug, PartialEq, Eq, Clone)]
1270pub enum HypotheticalCandidate {
1271	/// A complete candidate.
1272	Complete {
1273		/// The hash of the candidate.
1274		candidate_hash: CandidateHash,
1275		/// The receipt of the candidate.
1276		receipt: Arc<CommittedCandidateReceipt>,
1277		/// The persisted validation data of the candidate.
1278		persisted_validation_data: PersistedValidationData,
1279	},
1280	/// An incomplete candidate.
1281	Incomplete {
1282		/// The claimed hash of the candidate.
1283		candidate_hash: CandidateHash,
1284		/// The claimed para-ID of the candidate.
1285		candidate_para: ParaId,
1286		/// The claimed head-data hash of the candidate.
1287		parent_head_data_hash: Hash,
1288		/// The claimed relay parent of the candidate.
1289		candidate_relay_parent: Hash,
1290	},
1291}
1292
1293impl HypotheticalCandidate {
1294	/// Get the `CandidateHash` of the hypothetical candidate.
1295	pub fn candidate_hash(&self) -> CandidateHash {
1296		match *self {
1297			HypotheticalCandidate::Complete { candidate_hash, .. } => candidate_hash,
1298			HypotheticalCandidate::Incomplete { candidate_hash, .. } => candidate_hash,
1299		}
1300	}
1301
1302	/// Get the `ParaId` of the hypothetical candidate.
1303	pub fn candidate_para(&self) -> ParaId {
1304		match *self {
1305			HypotheticalCandidate::Complete { ref receipt, .. } => receipt.descriptor.para_id(),
1306			HypotheticalCandidate::Incomplete { candidate_para, .. } => candidate_para,
1307		}
1308	}
1309
1310	/// Get parent head data hash of the hypothetical candidate.
1311	pub fn parent_head_data_hash(&self) -> Hash {
1312		match *self {
1313			HypotheticalCandidate::Complete { ref persisted_validation_data, .. } => {
1314				persisted_validation_data.parent_head.hash()
1315			},
1316			HypotheticalCandidate::Incomplete { parent_head_data_hash, .. } => {
1317				parent_head_data_hash
1318			},
1319		}
1320	}
1321
1322	/// Get candidate's relay parent.
1323	pub fn relay_parent(&self) -> Hash {
1324		match *self {
1325			HypotheticalCandidate::Complete { ref receipt, .. } => {
1326				receipt.descriptor.relay_parent()
1327			},
1328			HypotheticalCandidate::Incomplete { candidate_relay_parent, .. } => {
1329				candidate_relay_parent
1330			},
1331		}
1332	}
1333
1334	/// Get the output head data hash, if the candidate is complete.
1335	pub fn output_head_data_hash(&self) -> Option<Hash> {
1336		match *self {
1337			HypotheticalCandidate::Complete { ref receipt, .. } => {
1338				Some(receipt.descriptor.para_head())
1339			},
1340			HypotheticalCandidate::Incomplete { .. } => None,
1341		}
1342	}
1343
1344	/// Get the candidate commitments, if the candidate is complete.
1345	pub fn commitments(&self) -> Option<&CandidateCommitments> {
1346		match *self {
1347			HypotheticalCandidate::Complete { ref receipt, .. } => Some(&receipt.commitments),
1348			HypotheticalCandidate::Incomplete { .. } => None,
1349		}
1350	}
1351
1352	/// Get the persisted validation data, if the candidate is complete.
1353	pub fn persisted_validation_data(&self) -> Option<&PersistedValidationData> {
1354		match *self {
1355			HypotheticalCandidate::Complete { ref persisted_validation_data, .. } => {
1356				Some(persisted_validation_data)
1357			},
1358			HypotheticalCandidate::Incomplete { .. } => None,
1359		}
1360	}
1361
1362	/// Get the validation code hash, if the candidate is complete.
1363	pub fn validation_code_hash(&self) -> Option<ValidationCodeHash> {
1364		match *self {
1365			HypotheticalCandidate::Complete { ref receipt, .. } => {
1366				Some(receipt.descriptor.validation_code_hash())
1367			},
1368			HypotheticalCandidate::Incomplete { .. } => None,
1369		}
1370	}
1371}
1372
1373/// Request specifying which candidates are either already included
1374/// or might become included in fragment chain under a given active leaf (or any active leaf if
1375/// `fragment_chain_relay_parent` is `None`).
1376#[derive(Debug, PartialEq, Eq, Clone)]
1377pub struct HypotheticalMembershipRequest {
1378	/// Candidates, in arbitrary order, which should be checked for
1379	/// hypothetical/actual membership in fragment chains.
1380	pub candidates: Vec<HypotheticalCandidate>,
1381	/// Either a specific fragment chain to check, otherwise all.
1382	pub fragment_chain_relay_parent: Option<Hash>,
1383}
1384
1385/// A request for the persisted validation data stored in the prospective
1386/// teyrchains subsystem.
1387#[derive(Debug)]
1388pub struct ProspectiveValidationDataRequest {
1389	/// The para-id of the candidate.
1390	pub para_id: ParaId,
1391	/// The relay-parent of the candidate.
1392	pub candidate_relay_parent: Hash,
1393	/// The parent head-data.
1394	pub parent_head_data: ParentHeadData,
1395}
1396
1397/// The parent head-data hash with optional data itself.
1398#[derive(Debug, Clone)]
1399pub enum ParentHeadData {
1400	/// Parent head-data hash.
1401	OnlyHash(Hash),
1402	/// Parent head-data along with its hash.
1403	WithData {
1404		/// This will be provided for collations with elastic scaling enabled.
1405		head_data: HeadData,
1406		/// Parent head-data hash.
1407		hash: Hash,
1408	},
1409}
1410
1411impl ParentHeadData {
1412	/// Return the hash of the parent head-data.
1413	pub fn hash(&self) -> Hash {
1414		match self {
1415			ParentHeadData::OnlyHash(hash) => *hash,
1416			ParentHeadData::WithData { hash, .. } => *hash,
1417		}
1418	}
1419}
1420
1421/// Indicates the relay-parents whose fragment chain a candidate
1422/// is present in or can be added in (right now or in the future).
1423pub type HypotheticalMembership = Vec<Hash>;
1424
1425/// A collection of ancestor candidates of a teyrchain.
1426pub type Ancestors = HashSet<CandidateHash>;
1427
1428/// Messages sent to the Prospective Teyrchains subsystem.
1429#[derive(Debug)]
1430pub enum ProspectiveTeyrchainsMessage {
1431	/// Inform the Prospective Teyrchains Subsystem of a new seconded candidate.
1432	///
1433	/// The response sender returns false if the candidate was rejected by prospective teyrchains,
1434	/// true otherwise (if it was accepted or already present)
1435	IntroduceSecondedCandidate(IntroduceSecondedCandidateRequest, oneshot::Sender<bool>),
1436	/// Inform the Prospective Teyrchains Subsystem that a previously introduced candidate
1437	/// has been backed. This requires that the candidate was successfully introduced in
1438	/// the past.
1439	CandidateBacked(ParaId, CandidateHash),
1440	/// Try getting N backable candidate hashes along with their relay parents for the given
1441	/// teyrchain, under the given relay-parent hash, which is a descendant of the given ancestors.
1442	/// Timed out ancestors should not be included in the collection.
1443	/// N should represent the number of scheduled cores of this ParaId.
1444	/// A timed out ancestor frees the cores of all of its descendants, so if there's a hole in the
1445	/// supplied ancestor path, we'll get candidates that backfill those timed out slots first. It
1446	/// may also return less/no candidates, if there aren't enough backable candidates recorded.
1447	GetBackableCandidates(
1448		Hash,
1449		ParaId,
1450		u32,
1451		Ancestors,
1452		oneshot::Sender<Vec<(CandidateHash, Hash)>>,
1453	),
1454	/// Get the hypothetical or actual membership of candidates with the given properties
1455	/// under the specified active leave's fragment chain.
1456	///
1457	/// For each candidate, we return a vector of leaves where the candidate is present or could be
1458	/// added. "Could be added" either means that the candidate can be added to the chain right now
1459	/// or could be added in the future (we may not have its ancestors yet).
1460	/// Note that even if we think it could be added in the future, we may find out that it was
1461	/// invalid, as time passes.
1462	/// If an active leaf is not in the vector, it means that there's no
1463	/// chance this candidate will become valid under that leaf in the future.
1464	///
1465	/// If `fragment_chain_relay_parent` in the request is `Some()`, the return vector can only
1466	/// contain this relay parent (or none).
1467	GetHypotheticalMembership(
1468		HypotheticalMembershipRequest,
1469		oneshot::Sender<Vec<(HypotheticalCandidate, HypotheticalMembership)>>,
1470	),
1471	/// Get the minimum accepted relay-parent number for each para in the fragment chain
1472	/// for the given relay-chain block hash.
1473	///
1474	/// That is, if the block hash is known and is an active leaf, this returns the
1475	/// minimum relay-parent block number in the same branch of the relay chain which
1476	/// is accepted in the fragment chain for each para-id.
1477	///
1478	/// If the block hash is not an active leaf, this will return an empty vector.
1479	///
1480	/// Para-IDs which are omitted from this list can be assumed to have no
1481	/// valid candidate relay-parents under the given relay-chain block hash.
1482	///
1483	/// Para-IDs are returned in no particular order.
1484	GetMinimumRelayParents(Hash, oneshot::Sender<Vec<(ParaId, BlockNumber)>>),
1485	/// Get the validation data of some prospective candidate. The candidate doesn't need
1486	/// to be part of any fragment chain, but this only succeeds if the parent head-data and
1487	/// relay-parent are part of the `CandidateStorage` (meaning that it's a candidate which is
1488	/// part of some fragment chain or which prospective-teyrchains predicted will become part of
1489	/// some fragment chain).
1490	GetProspectiveValidationData(
1491		ProspectiveValidationDataRequest,
1492		oneshot::Sender<Option<PersistedValidationData>>,
1493	),
1494}