referrerpolicy=no-referrer-when-downgrade

polkadot_node_primitives/approval/
mod.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//! Types relevant for approval.
18
19/// Criteria for assignment.
20pub mod criteria;
21
22/// Time utilities for approval voting.
23pub mod time;
24
25/// A list of primitives introduced in v1.
26pub mod v1 {
27	use sp_consensus_babe as babe_primitives;
28	pub use sp_consensus_babe::{
29		Randomness, Slot, VrfPreOutput, VrfProof, VrfSignature, VrfTranscript,
30	};
31
32	use codec::{Decode, Encode};
33	use polkadot_primitives::{
34		BlockNumber, CandidateHash, CandidateIndex, CoreIndex, GroupIndex, Hash, Header,
35		SessionIndex, ValidatorIndex, ValidatorSignature,
36	};
37	use sp_application_crypto::ByteArray;
38
39	/// Validators assigning to check a particular candidate are split up into tranches.
40	/// Earlier tranches of validators check first, with later tranches serving as backup.
41	pub type DelayTranche = u32;
42
43	/// A static context used to compute the Relay VRF story based on the
44	/// VRF output included in the header-chain.
45	pub const RELAY_VRF_STORY_CONTEXT: &[u8] = b"A&V RC-VRF";
46
47	/// A static context used for all relay-vrf-modulo VRFs.
48	pub const RELAY_VRF_MODULO_CONTEXT: &[u8] = b"A&V MOD";
49
50	/// A static context used for all relay-vrf-modulo VRFs.
51	pub const RELAY_VRF_DELAY_CONTEXT: &[u8] = b"A&V DELAY";
52
53	/// A static context used for transcripts indicating assigned availability core.
54	pub const ASSIGNED_CORE_CONTEXT: &[u8] = b"A&V ASSIGNED";
55
56	/// A static context associated with producing randomness for a core.
57	pub const CORE_RANDOMNESS_CONTEXT: &[u8] = b"A&V CORE";
58
59	/// A static context associated with producing randomness for a tranche.
60	pub const TRANCHE_RANDOMNESS_CONTEXT: &[u8] = b"A&V TRANCHE";
61
62	/// random bytes derived from the VRF submitted within the block by the
63	/// block author as a credential and used as input to approval assignment criteria.
64	#[derive(Debug, Clone, Encode, Decode, PartialEq)]
65	pub struct RelayVRFStory(pub [u8; 32]);
66
67	/// Different kinds of input data or criteria that can prove a validator's assignment
68	/// to check a particular parachain.
69	#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
70	pub enum AssignmentCertKind {
71		/// An assignment story based on the VRF that authorized the relay-chain block where the
72		/// candidate was included combined with a sample number.
73		///
74		/// The context used to produce bytes is [`RELAY_VRF_MODULO_CONTEXT`]
75		RelayVRFModulo {
76			/// The sample number used in this cert.
77			sample: u32,
78		},
79		/// An assignment story based on the VRF that authorized the relay-chain block where the
80		/// candidate was included combined with the index of a particular core.
81		///
82		/// The context is [`RELAY_VRF_DELAY_CONTEXT`]
83		RelayVRFDelay {
84			/// The core index chosen in this cert.
85			core_index: CoreIndex,
86		},
87	}
88
89	/// A certification of assignment.
90	#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
91	pub struct AssignmentCert {
92		/// The criterion which is claimed to be met by this cert.
93		pub kind: AssignmentCertKind,
94		/// The VRF signature showing the criterion is met.
95		pub vrf: VrfSignature,
96	}
97
98	/// An assignment criterion which refers to the candidate under which the assignment is
99	/// relevant by block hash.
100	#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
101	pub struct IndirectAssignmentCert {
102		/// A block hash where the candidate appears.
103		pub block_hash: Hash,
104		/// The validator index.
105		pub validator: ValidatorIndex,
106		/// The cert itself.
107		pub cert: AssignmentCert,
108	}
109
110	/// A signed approval vote which references the candidate indirectly via the block.
111	///
112	/// In practice, we have a look-up from block hash and candidate index to candidate hash,
113	/// so this can be transformed into a `SignedApprovalVote`.
114	#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
115	pub struct IndirectSignedApprovalVote {
116		/// A block hash where the candidate appears.
117		pub block_hash: Hash,
118		/// The index of the candidate in the list of candidates fully included as-of the block.
119		pub candidate_index: CandidateIndex,
120		/// The validator index.
121		pub validator: ValidatorIndex,
122		/// The signature by the validator.
123		pub signature: ValidatorSignature,
124	}
125
126	/// Metadata about a block which is now live in the approval protocol.
127	#[derive(Debug, Clone)]
128	pub struct BlockApprovalMeta {
129		/// The hash of the block.
130		pub hash: Hash,
131		/// The number of the block.
132		pub number: BlockNumber,
133		/// The hash of the parent block.
134		pub parent_hash: Hash,
135		/// The candidates included by the block.
136		/// Note that these are not the same as the candidates that appear within the block body.
137		pub candidates: Vec<(CandidateHash, CoreIndex, GroupIndex)>,
138		/// The consensus slot of the block.
139		pub slot: Slot,
140		/// The session of the block.
141		pub session: SessionIndex,
142		/// The vrf story.
143		pub vrf_story: RelayVRFStory,
144	}
145
146	/// Errors that can occur during the approvals protocol.
147	#[derive(Debug, thiserror::Error)]
148	#[allow(missing_docs)]
149	pub enum ApprovalError {
150		#[error("Schnorrkel signature error")]
151		SchnorrkelSignature(schnorrkel::errors::SignatureError),
152		#[error("Authority index {0} out of bounds")]
153		AuthorityOutOfBounds(usize),
154	}
155
156	/// An unsafe VRF pre-output. Provide BABE Epoch info to create a `RelayVRFStory`.
157	pub struct UnsafeVRFPreOutput {
158		vrf_pre_output: VrfPreOutput,
159		slot: Slot,
160		authority_index: u32,
161	}
162
163	impl UnsafeVRFPreOutput {
164		/// Get the slot.
165		pub fn slot(&self) -> Slot {
166			self.slot
167		}
168
169		/// Compute the randomness associated with this VRF output.
170		pub fn compute_randomness(
171			self,
172			authorities: &[(babe_primitives::AuthorityId, babe_primitives::BabeAuthorityWeight)],
173			randomness: &babe_primitives::Randomness,
174			epoch_index: u64,
175		) -> Result<RelayVRFStory, ApprovalError> {
176			let author = match authorities.get(self.authority_index as usize) {
177				None => return Err(ApprovalError::AuthorityOutOfBounds(self.authority_index as _)),
178				Some(x) => &x.0,
179			};
180
181			let pubkey = schnorrkel::PublicKey::from_bytes(author.as_slice())
182				.map_err(ApprovalError::SchnorrkelSignature)?;
183
184			let transcript =
185				sp_consensus_babe::make_vrf_transcript(randomness, self.slot, epoch_index);
186
187			let inout = self
188				.vrf_pre_output
189				.0
190				.attach_input_hash(&pubkey, transcript.0)
191				.map_err(ApprovalError::SchnorrkelSignature)?;
192			Ok(RelayVRFStory(inout.make_bytes(super::v1::RELAY_VRF_STORY_CONTEXT)))
193		}
194	}
195
196	/// Extract the slot number and relay VRF from a header.
197	///
198	/// This fails if either there is no BABE `PreRuntime` digest or
199	/// the digest has type `SecondaryPlain`, which Substrate nodes do
200	/// not produce or accept anymore.
201	pub fn babe_unsafe_vrf_info(header: &Header) -> Option<UnsafeVRFPreOutput> {
202		use babe_primitives::digests::CompatibleDigestItem;
203
204		for digest in &header.digest.logs {
205			if let Some(pre) = digest.as_babe_pre_digest() {
206				let slot = pre.slot();
207				let authority_index = pre.authority_index();
208
209				return pre.vrf_signature().map(|sig| UnsafeVRFPreOutput {
210					vrf_pre_output: sig.pre_output.clone(),
211					slot,
212					authority_index,
213				})
214			}
215		}
216
217		None
218	}
219}
220
221/// A list of primitives introduced by v2.
222pub mod v2 {
223	use codec::{Decode, Encode};
224	pub use sp_consensus_babe::{
225		Randomness, Slot, VrfPreOutput, VrfProof, VrfSignature, VrfTranscript,
226	};
227	use std::ops::BitOr;
228
229	use bitvec::{prelude::Lsb0, vec::BitVec};
230	use polkadot_primitives::{
231		CandidateIndex, CoreIndex, Hash, ValidatorIndex, ValidatorSignature,
232	};
233
234	/// A static context associated with producing randomness for a core.
235	pub const CORE_RANDOMNESS_CONTEXT: &[u8] = b"A&V CORE v2";
236	/// A static context associated with producing randomness for v2 multi-core assignments.
237	pub const ASSIGNED_CORE_CONTEXT: &[u8] = b"A&V ASSIGNED v2";
238	/// A static context used for all relay-vrf-modulo VRFs for v2 multi-core assignments.
239	pub const RELAY_VRF_MODULO_CONTEXT: &[u8] = b"A&V MOD v2";
240	/// A read-only bitvec wrapper
241	#[derive(Clone, Debug, Encode, Decode, Hash, PartialEq, Eq)]
242	pub struct Bitfield<T>(BitVec<u8, bitvec::order::Lsb0>, std::marker::PhantomData<T>);
243
244	/// A `read-only`, `non-zero` bitfield.
245	/// Each 1 bit identifies a candidate by the bitfield bit index.
246	pub type CandidateBitfield = Bitfield<CandidateIndex>;
247	/// A bitfield of core assignments.
248	pub type CoreBitfield = Bitfield<CoreIndex>;
249
250	/// Errors that can occur when creating and manipulating bitfields.
251	#[derive(Debug)]
252	pub enum BitfieldError {
253		/// All bits are zero.
254		NullAssignment,
255	}
256
257	/// A bit index in `Bitfield`.
258	#[cfg_attr(test, derive(PartialEq, Clone))]
259	pub struct BitIndex(pub usize);
260
261	/// Helper trait to convert primitives to `BitIndex`.
262	pub trait AsBitIndex {
263		/// Returns the index of the corresponding bit in `Bitfield`.
264		fn as_bit_index(&self) -> BitIndex;
265	}
266
267	impl<T> Bitfield<T> {
268		/// Returns the bit value at specified `index`. If `index` is greater than bitfield size,
269		/// returns `false`.
270		pub fn bit_at(&self, index: BitIndex) -> bool {
271			if self.0.len() <= index.0 {
272				false
273			} else {
274				self.0[index.0]
275			}
276		}
277
278		/// Returns number of bits.
279		pub fn len(&self) -> usize {
280			self.0.len()
281		}
282
283		/// Returns the number of 1 bits.
284		pub fn count_ones(&self) -> usize {
285			self.0.count_ones()
286		}
287
288		/// Returns the index of the first 1 bit.
289		pub fn first_one(&self) -> Option<usize> {
290			self.0.first_one()
291		}
292
293		/// Returns an iterator over inner bits.
294		pub fn iter_ones(&self) -> bitvec::slice::IterOnes<'_, u8, bitvec::order::Lsb0> {
295			self.0.iter_ones()
296		}
297
298		/// For testing purpose, we want a inner mutable ref.
299		pub fn inner_mut(&mut self) -> &mut BitVec<u8, bitvec::order::Lsb0> {
300			&mut self.0
301		}
302
303		/// Returns the inner bitfield and consumes `self`.
304		pub fn into_inner(self) -> BitVec<u8, bitvec::order::Lsb0> {
305			self.0
306		}
307	}
308
309	impl AsBitIndex for CandidateIndex {
310		fn as_bit_index(&self) -> BitIndex {
311			BitIndex(*self as usize)
312		}
313	}
314
315	impl AsBitIndex for CoreIndex {
316		fn as_bit_index(&self) -> BitIndex {
317			BitIndex(self.0 as usize)
318		}
319	}
320
321	impl AsBitIndex for usize {
322		fn as_bit_index(&self) -> BitIndex {
323			BitIndex(*self)
324		}
325	}
326
327	impl<T> From<T> for Bitfield<T>
328	where
329		T: AsBitIndex,
330	{
331		fn from(value: T) -> Self {
332			Self(
333				{
334					let mut bv = bitvec::bitvec![u8, Lsb0; 0; value.as_bit_index().0 + 1];
335					bv.set(value.as_bit_index().0, true);
336					bv
337				},
338				Default::default(),
339			)
340		}
341	}
342
343	impl<T> TryFrom<Vec<T>> for Bitfield<T>
344	where
345		T: Into<Bitfield<T>>,
346	{
347		type Error = BitfieldError;
348
349		fn try_from(mut value: Vec<T>) -> Result<Self, Self::Error> {
350			if value.is_empty() {
351				return Err(BitfieldError::NullAssignment)
352			}
353
354			let initial_bitfield =
355				value.pop().expect("Just checked above it's not empty; qed").into();
356
357			Ok(Self(
358				value.into_iter().fold(initial_bitfield.0, |initial_bitfield, element| {
359					let mut bitfield: Bitfield<T> = element.into();
360					bitfield
361						.0
362						.resize(std::cmp::max(initial_bitfield.len(), bitfield.0.len()), false);
363					bitfield.0.bitor(initial_bitfield)
364				}),
365				Default::default(),
366			))
367		}
368	}
369
370	/// Certificate is changed compared to `AssignmentCertKind`:
371	/// - introduced RelayVRFModuloCompact
372	#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
373	pub enum AssignmentCertKindV2 {
374		/// Multiple assignment stories based on the VRF that authorized the relay-chain block
375		/// where the candidates were included.
376		///
377		/// The context is [`super::v2::RELAY_VRF_MODULO_CONTEXT`]
378		#[codec(index = 0)]
379		RelayVRFModuloCompact {
380			/// A bitfield representing the core indices claimed by this assignment.
381			core_bitfield: CoreBitfield,
382		},
383		/// An assignment story based on the VRF that authorized the relay-chain block where the
384		/// candidate was included combined with the index of a particular core.
385		///
386		/// The context is [`super::v1::RELAY_VRF_DELAY_CONTEXT`]
387		#[codec(index = 1)]
388		RelayVRFDelay {
389			/// The core index chosen in this cert.
390			core_index: CoreIndex,
391		},
392		/// Deprecated assignment. Soon to be removed.
393		///  An assignment story based on the VRF that authorized the relay-chain block where the
394		/// candidate was included combined with a sample number.
395		///
396		/// The context used to produce bytes is [`super::v1::RELAY_VRF_MODULO_CONTEXT`]
397		#[codec(index = 2)]
398		RelayVRFModulo {
399			/// The sample number used in this cert.
400			sample: u32,
401		},
402	}
403
404	/// A certification of assignment.
405	#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
406	pub struct AssignmentCertV2 {
407		/// The criterion which is claimed to be met by this cert.
408		pub kind: AssignmentCertKindV2,
409		/// The VRF showing the criterion is met.
410		pub vrf: VrfSignature,
411	}
412
413	impl From<super::v1::AssignmentCert> for AssignmentCertV2 {
414		fn from(cert: super::v1::AssignmentCert) -> Self {
415			Self {
416				kind: match cert.kind {
417					super::v1::AssignmentCertKind::RelayVRFDelay { core_index } =>
418						AssignmentCertKindV2::RelayVRFDelay { core_index },
419					super::v1::AssignmentCertKind::RelayVRFModulo { sample } =>
420						AssignmentCertKindV2::RelayVRFModulo { sample },
421				},
422				vrf: cert.vrf,
423			}
424		}
425	}
426
427	/// Errors that can occur when trying to convert to/from assignment v1/v2
428	#[derive(Debug)]
429	pub enum AssignmentConversionError {
430		/// Assignment certificate is not supported in v1.
431		CertificateNotSupported,
432	}
433
434	impl TryFrom<AssignmentCertV2> for super::v1::AssignmentCert {
435		type Error = AssignmentConversionError;
436		fn try_from(cert: AssignmentCertV2) -> Result<Self, AssignmentConversionError> {
437			Ok(Self {
438				kind: match cert.kind {
439					AssignmentCertKindV2::RelayVRFDelay { core_index } =>
440						super::v1::AssignmentCertKind::RelayVRFDelay { core_index },
441					AssignmentCertKindV2::RelayVRFModulo { sample } =>
442						super::v1::AssignmentCertKind::RelayVRFModulo { sample },
443					// Not supported
444					_ => return Err(AssignmentConversionError::CertificateNotSupported),
445				},
446				vrf: cert.vrf,
447			})
448		}
449	}
450
451	/// An assignment criterion which refers to the candidate under which the assignment is
452	/// relevant by block hash.
453	#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
454	pub struct IndirectAssignmentCertV2 {
455		/// A block hash where the candidate appears.
456		pub block_hash: Hash,
457		/// The validator index.
458		pub validator: ValidatorIndex,
459		/// The cert itself.
460		pub cert: AssignmentCertV2,
461	}
462
463	impl From<super::v1::IndirectAssignmentCert> for IndirectAssignmentCertV2 {
464		fn from(indirect_cert: super::v1::IndirectAssignmentCert) -> Self {
465			Self {
466				block_hash: indirect_cert.block_hash,
467				validator: indirect_cert.validator,
468				cert: indirect_cert.cert.into(),
469			}
470		}
471	}
472
473	impl TryFrom<IndirectAssignmentCertV2> for super::v1::IndirectAssignmentCert {
474		type Error = AssignmentConversionError;
475		fn try_from(
476			indirect_cert: IndirectAssignmentCertV2,
477		) -> Result<Self, AssignmentConversionError> {
478			Ok(Self {
479				block_hash: indirect_cert.block_hash,
480				validator: indirect_cert.validator,
481				cert: indirect_cert.cert.try_into()?,
482			})
483		}
484	}
485
486	impl From<super::v1::IndirectSignedApprovalVote> for IndirectSignedApprovalVoteV2 {
487		fn from(value: super::v1::IndirectSignedApprovalVote) -> Self {
488			Self {
489				block_hash: value.block_hash,
490				validator: value.validator,
491				candidate_indices: value.candidate_index.into(),
492				signature: value.signature,
493			}
494		}
495	}
496
497	/// Errors that can occur when trying to convert to/from approvals v1/v2
498	#[derive(Debug)]
499	pub enum ApprovalConversionError {
500		/// More than one candidate was signed.
501		MoreThanOneCandidate(usize),
502	}
503
504	impl TryFrom<IndirectSignedApprovalVoteV2> for super::v1::IndirectSignedApprovalVote {
505		type Error = ApprovalConversionError;
506
507		fn try_from(value: IndirectSignedApprovalVoteV2) -> Result<Self, Self::Error> {
508			if value.candidate_indices.count_ones() != 1 {
509				return Err(ApprovalConversionError::MoreThanOneCandidate(
510					value.candidate_indices.count_ones(),
511				))
512			}
513			Ok(Self {
514				block_hash: value.block_hash,
515				validator: value.validator,
516				candidate_index: value.candidate_indices.first_one().expect("Qed we checked above")
517					as u32,
518				signature: value.signature,
519			})
520		}
521	}
522
523	/// A signed approval vote which references the candidate indirectly via the block.
524	///
525	/// In practice, we have a look-up from block hash and candidate index to candidate hash,
526	/// so this can be transformed into a `SignedApprovalVote`.
527	#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
528	pub struct IndirectSignedApprovalVoteV2 {
529		/// A block hash where the candidate appears.
530		pub block_hash: Hash,
531		/// The index of the candidate in the list of candidates fully included as-of the block.
532		pub candidate_indices: CandidateBitfield,
533		/// The validator index.
534		pub validator: ValidatorIndex,
535		/// The signature by the validator.
536		pub signature: ValidatorSignature,
537	}
538}
539
540#[cfg(test)]
541mod test {
542	use super::v2::{BitIndex, Bitfield};
543
544	use polkadot_primitives::{CandidateIndex, CoreIndex};
545
546	#[test]
547	fn test_assignment_bitfield_from_vec() {
548		let candidate_indices = vec![1u32, 7, 3, 10, 45, 8, 200, 2];
549		let max_index = *candidate_indices.iter().max().unwrap();
550		let bitfield = Bitfield::try_from(candidate_indices.clone()).unwrap();
551		let candidate_indices =
552			candidate_indices.into_iter().map(|i| BitIndex(i as usize)).collect::<Vec<_>>();
553
554		// Test 1 bits.
555		for index in candidate_indices.clone() {
556			assert!(bitfield.bit_at(index));
557		}
558
559		// Test 0 bits.
560		for index in 0..max_index {
561			if candidate_indices.contains(&BitIndex(index as usize)) {
562				continue
563			}
564			assert!(!bitfield.bit_at(BitIndex(index as usize)));
565		}
566	}
567
568	#[test]
569	fn test_assignment_bitfield_invariant_msb() {
570		let core_indices = vec![CoreIndex(1), CoreIndex(3), CoreIndex(10), CoreIndex(20)];
571		let mut bitfield = Bitfield::try_from(core_indices.clone()).unwrap();
572		assert!(bitfield.inner_mut().pop().unwrap());
573
574		for i in 0..1024 {
575			assert!(Bitfield::try_from(CoreIndex(i)).unwrap().inner_mut().pop().unwrap());
576			assert!(Bitfield::try_from(i).unwrap().inner_mut().pop().unwrap());
577		}
578	}
579
580	#[test]
581	fn test_assignment_bitfield_basic() {
582		let bitfield = Bitfield::try_from(CoreIndex(0)).unwrap();
583		assert!(bitfield.bit_at(BitIndex(0)));
584		assert!(!bitfield.bit_at(BitIndex(1)));
585		assert_eq!(bitfield.len(), 1);
586
587		let mut bitfield = Bitfield::try_from(20 as CandidateIndex).unwrap();
588		assert!(bitfield.bit_at(BitIndex(20)));
589		assert_eq!(bitfield.inner_mut().count_ones(), 1);
590		assert_eq!(bitfield.len(), 21);
591	}
592}