referrerpolicy=no-referrer-when-downgrade

polkadot_node_subsystem_types/
messages.rs

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