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		#[cfg(test)]
300		pub fn inner_mut(&mut self) -> &mut BitVec<u8, bitvec::order::Lsb0> {
301			&mut self.0
302		}
303
304		/// Returns the inner bitfield and consumes `self`.
305		pub fn into_inner(self) -> BitVec<u8, bitvec::order::Lsb0> {
306			self.0
307		}
308	}
309
310	impl AsBitIndex for CandidateIndex {
311		fn as_bit_index(&self) -> BitIndex {
312			BitIndex(*self as usize)
313		}
314	}
315
316	impl AsBitIndex for CoreIndex {
317		fn as_bit_index(&self) -> BitIndex {
318			BitIndex(self.0 as usize)
319		}
320	}
321
322	impl AsBitIndex for usize {
323		fn as_bit_index(&self) -> BitIndex {
324			BitIndex(*self)
325		}
326	}
327
328	impl<T> From<T> for Bitfield<T>
329	where
330		T: AsBitIndex,
331	{
332		fn from(value: T) -> Self {
333			Self(
334				{
335					let mut bv = bitvec::bitvec![u8, Lsb0; 0; value.as_bit_index().0 + 1];
336					bv.set(value.as_bit_index().0, true);
337					bv
338				},
339				Default::default(),
340			)
341		}
342	}
343
344	impl<T> TryFrom<Vec<T>> for Bitfield<T>
345	where
346		T: Into<Bitfield<T>>,
347	{
348		type Error = BitfieldError;
349
350		fn try_from(mut value: Vec<T>) -> Result<Self, Self::Error> {
351			if value.is_empty() {
352				return Err(BitfieldError::NullAssignment)
353			}
354
355			let initial_bitfield =
356				value.pop().expect("Just checked above it's not empty; qed").into();
357
358			Ok(Self(
359				value.into_iter().fold(initial_bitfield.0, |initial_bitfield, element| {
360					let mut bitfield: Bitfield<T> = element.into();
361					bitfield
362						.0
363						.resize(std::cmp::max(initial_bitfield.len(), bitfield.0.len()), false);
364					bitfield.0.bitor(initial_bitfield)
365				}),
366				Default::default(),
367			))
368		}
369	}
370
371	/// Certificate is changed compared to `AssignmentCertKind`:
372	/// - introduced RelayVRFModuloCompact
373	#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
374	pub enum AssignmentCertKindV2 {
375		/// Multiple assignment stories based on the VRF that authorized the relay-chain block
376		/// where the candidates were included.
377		///
378		/// The context is [`super::v2::RELAY_VRF_MODULO_CONTEXT`]
379		#[codec(index = 0)]
380		RelayVRFModuloCompact {
381			/// A bitfield representing the core indices claimed by this assignment.
382			core_bitfield: CoreBitfield,
383		},
384		/// An assignment story based on the VRF that authorized the relay-chain block where the
385		/// candidate was included combined with the index of a particular core.
386		///
387		/// The context is [`super::v1::RELAY_VRF_DELAY_CONTEXT`]
388		#[codec(index = 1)]
389		RelayVRFDelay {
390			/// The core index chosen in this cert.
391			core_index: CoreIndex,
392		},
393		/// Deprecated assignment. Soon to be removed.
394		///  An assignment story based on the VRF that authorized the relay-chain block where the
395		/// candidate was included combined with a sample number.
396		///
397		/// The context used to produce bytes is [`super::v1::RELAY_VRF_MODULO_CONTEXT`]
398		#[codec(index = 2)]
399		RelayVRFModulo {
400			/// The sample number used in this cert.
401			sample: u32,
402		},
403	}
404
405	/// A certification of assignment.
406	#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
407	pub struct AssignmentCertV2 {
408		/// The criterion which is claimed to be met by this cert.
409		pub kind: AssignmentCertKindV2,
410		/// The VRF showing the criterion is met.
411		pub vrf: VrfSignature,
412	}
413
414	impl From<super::v1::AssignmentCert> for AssignmentCertV2 {
415		fn from(cert: super::v1::AssignmentCert) -> Self {
416			Self {
417				kind: match cert.kind {
418					super::v1::AssignmentCertKind::RelayVRFDelay { core_index } =>
419						AssignmentCertKindV2::RelayVRFDelay { core_index },
420					super::v1::AssignmentCertKind::RelayVRFModulo { sample } =>
421						AssignmentCertKindV2::RelayVRFModulo { sample },
422				},
423				vrf: cert.vrf,
424			}
425		}
426	}
427
428	/// Errors that can occur when trying to convert to/from assignment v1/v2
429	#[derive(Debug)]
430	pub enum AssignmentConversionError {
431		/// Assignment certificate is not supported in v1.
432		CertificateNotSupported,
433	}
434
435	impl TryFrom<AssignmentCertV2> for super::v1::AssignmentCert {
436		type Error = AssignmentConversionError;
437		fn try_from(cert: AssignmentCertV2) -> Result<Self, AssignmentConversionError> {
438			Ok(Self {
439				kind: match cert.kind {
440					AssignmentCertKindV2::RelayVRFDelay { core_index } =>
441						super::v1::AssignmentCertKind::RelayVRFDelay { core_index },
442					AssignmentCertKindV2::RelayVRFModulo { sample } =>
443						super::v1::AssignmentCertKind::RelayVRFModulo { sample },
444					// Not supported
445					_ => return Err(AssignmentConversionError::CertificateNotSupported),
446				},
447				vrf: cert.vrf,
448			})
449		}
450	}
451
452	/// An assignment criterion which refers to the candidate under which the assignment is
453	/// relevant by block hash.
454	#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
455	pub struct IndirectAssignmentCertV2 {
456		/// A block hash where the candidate appears.
457		pub block_hash: Hash,
458		/// The validator index.
459		pub validator: ValidatorIndex,
460		/// The cert itself.
461		pub cert: AssignmentCertV2,
462	}
463
464	impl From<super::v1::IndirectAssignmentCert> for IndirectAssignmentCertV2 {
465		fn from(indirect_cert: super::v1::IndirectAssignmentCert) -> Self {
466			Self {
467				block_hash: indirect_cert.block_hash,
468				validator: indirect_cert.validator,
469				cert: indirect_cert.cert.into(),
470			}
471		}
472	}
473
474	impl TryFrom<IndirectAssignmentCertV2> for super::v1::IndirectAssignmentCert {
475		type Error = AssignmentConversionError;
476		fn try_from(
477			indirect_cert: IndirectAssignmentCertV2,
478		) -> Result<Self, AssignmentConversionError> {
479			Ok(Self {
480				block_hash: indirect_cert.block_hash,
481				validator: indirect_cert.validator,
482				cert: indirect_cert.cert.try_into()?,
483			})
484		}
485	}
486
487	impl From<super::v1::IndirectSignedApprovalVote> for IndirectSignedApprovalVoteV2 {
488		fn from(value: super::v1::IndirectSignedApprovalVote) -> Self {
489			Self {
490				block_hash: value.block_hash,
491				validator: value.validator,
492				candidate_indices: value.candidate_index.into(),
493				signature: value.signature,
494			}
495		}
496	}
497
498	/// Errors that can occur when trying to convert to/from approvals v1/v2
499	#[derive(Debug)]
500	pub enum ApprovalConversionError {
501		/// More than one candidate was signed.
502		MoreThanOneCandidate(usize),
503	}
504
505	impl TryFrom<IndirectSignedApprovalVoteV2> for super::v1::IndirectSignedApprovalVote {
506		type Error = ApprovalConversionError;
507
508		fn try_from(value: IndirectSignedApprovalVoteV2) -> Result<Self, Self::Error> {
509			if value.candidate_indices.count_ones() != 1 {
510				return Err(ApprovalConversionError::MoreThanOneCandidate(
511					value.candidate_indices.count_ones(),
512				))
513			}
514			Ok(Self {
515				block_hash: value.block_hash,
516				validator: value.validator,
517				candidate_index: value.candidate_indices.first_one().expect("Qed we checked above")
518					as u32,
519				signature: value.signature,
520			})
521		}
522	}
523
524	/// A signed approval vote which references the candidate indirectly via the block.
525	///
526	/// In practice, we have a look-up from block hash and candidate index to candidate hash,
527	/// so this can be transformed into a `SignedApprovalVote`.
528	#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
529	pub struct IndirectSignedApprovalVoteV2 {
530		/// A block hash where the candidate appears.
531		pub block_hash: Hash,
532		/// The index of the candidate in the list of candidates fully included as-of the block.
533		pub candidate_indices: CandidateBitfield,
534		/// The validator index.
535		pub validator: ValidatorIndex,
536		/// The signature by the validator.
537		pub signature: ValidatorSignature,
538	}
539}
540
541#[cfg(test)]
542mod test {
543	use super::v2::{BitIndex, Bitfield};
544
545	use polkadot_primitives::{CandidateIndex, CoreIndex};
546
547	#[test]
548	fn test_assignment_bitfield_from_vec() {
549		let candidate_indices = vec![1u32, 7, 3, 10, 45, 8, 200, 2];
550		let max_index = *candidate_indices.iter().max().unwrap();
551		let bitfield = Bitfield::try_from(candidate_indices.clone()).unwrap();
552		let candidate_indices =
553			candidate_indices.into_iter().map(|i| BitIndex(i as usize)).collect::<Vec<_>>();
554
555		// Test 1 bits.
556		for index in candidate_indices.clone() {
557			assert!(bitfield.bit_at(index));
558		}
559
560		// Test 0 bits.
561		for index in 0..max_index {
562			if candidate_indices.contains(&BitIndex(index as usize)) {
563				continue
564			}
565			assert!(!bitfield.bit_at(BitIndex(index as usize)));
566		}
567	}
568
569	#[test]
570	fn test_assignment_bitfield_invariant_msb() {
571		let core_indices = vec![CoreIndex(1), CoreIndex(3), CoreIndex(10), CoreIndex(20)];
572		let mut bitfield = Bitfield::try_from(core_indices.clone()).unwrap();
573		assert!(bitfield.inner_mut().pop().unwrap());
574
575		for i in 0..1024 {
576			assert!(Bitfield::try_from(CoreIndex(i)).unwrap().inner_mut().pop().unwrap());
577			assert!(Bitfield::try_from(i).unwrap().inner_mut().pop().unwrap());
578		}
579	}
580
581	#[test]
582	fn test_assignment_bitfield_basic() {
583		let bitfield = Bitfield::try_from(CoreIndex(0)).unwrap();
584		assert!(bitfield.bit_at(BitIndex(0)));
585		assert!(!bitfield.bit_at(BitIndex(1)));
586		assert_eq!(bitfield.len(), 1);
587
588		let mut bitfield = Bitfield::try_from(20 as CandidateIndex).unwrap();
589		assert!(bitfield.bit_at(BitIndex(20)));
590		assert_eq!(bitfield.inner_mut().count_ones(), 1);
591		assert_eq!(bitfield.len(), 21);
592	}
593}