Skip to main content

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					},
420					super::v1::AssignmentCertKind::RelayVRFModulo { sample } => {
421						AssignmentCertKindV2::RelayVRFModulo { sample }
422					},
423				},
424				vrf: cert.vrf,
425			}
426		}
427	}
428
429	/// Errors that can occur when trying to convert to/from assignment v1/v2
430	#[derive(Debug)]
431	pub enum AssignmentConversionError {
432		/// Assignment certificate is not supported in v1.
433		CertificateNotSupported,
434	}
435
436	impl TryFrom<AssignmentCertV2> for super::v1::AssignmentCert {
437		type Error = AssignmentConversionError;
438		fn try_from(cert: AssignmentCertV2) -> Result<Self, AssignmentConversionError> {
439			Ok(Self {
440				kind: match cert.kind {
441					AssignmentCertKindV2::RelayVRFDelay { core_index } => {
442						super::v1::AssignmentCertKind::RelayVRFDelay { core_index }
443					},
444					AssignmentCertKindV2::RelayVRFModulo { sample } => {
445						super::v1::AssignmentCertKind::RelayVRFModulo { sample }
446					},
447					// Not supported
448					_ => return Err(AssignmentConversionError::CertificateNotSupported),
449				},
450				vrf: cert.vrf,
451			})
452		}
453	}
454
455	/// An assignment criterion which refers to the candidate under which the assignment is
456	/// relevant by block hash.
457	#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
458	pub struct IndirectAssignmentCertV2 {
459		/// A block hash where the candidate appears.
460		pub block_hash: Hash,
461		/// The validator index.
462		pub validator: ValidatorIndex,
463		/// The cert itself.
464		pub cert: AssignmentCertV2,
465	}
466
467	impl From<super::v1::IndirectAssignmentCert> for IndirectAssignmentCertV2 {
468		fn from(indirect_cert: super::v1::IndirectAssignmentCert) -> Self {
469			Self {
470				block_hash: indirect_cert.block_hash,
471				validator: indirect_cert.validator,
472				cert: indirect_cert.cert.into(),
473			}
474		}
475	}
476
477	impl TryFrom<IndirectAssignmentCertV2> for super::v1::IndirectAssignmentCert {
478		type Error = AssignmentConversionError;
479		fn try_from(
480			indirect_cert: IndirectAssignmentCertV2,
481		) -> Result<Self, AssignmentConversionError> {
482			Ok(Self {
483				block_hash: indirect_cert.block_hash,
484				validator: indirect_cert.validator,
485				cert: indirect_cert.cert.try_into()?,
486			})
487		}
488	}
489
490	impl From<super::v1::IndirectSignedApprovalVote> for IndirectSignedApprovalVoteV2 {
491		fn from(value: super::v1::IndirectSignedApprovalVote) -> Self {
492			Self {
493				block_hash: value.block_hash,
494				validator: value.validator,
495				candidate_indices: value.candidate_index.into(),
496				signature: value.signature,
497			}
498		}
499	}
500
501	/// Errors that can occur when trying to convert to/from approvals v1/v2
502	#[derive(Debug)]
503	pub enum ApprovalConversionError {
504		/// More than one candidate was signed.
505		MoreThanOneCandidate(usize),
506	}
507
508	impl TryFrom<IndirectSignedApprovalVoteV2> for super::v1::IndirectSignedApprovalVote {
509		type Error = ApprovalConversionError;
510
511		fn try_from(value: IndirectSignedApprovalVoteV2) -> Result<Self, Self::Error> {
512			if value.candidate_indices.count_ones() != 1 {
513				return Err(ApprovalConversionError::MoreThanOneCandidate(
514					value.candidate_indices.count_ones(),
515				));
516			}
517			Ok(Self {
518				block_hash: value.block_hash,
519				validator: value.validator,
520				candidate_index: value.candidate_indices.first_one().expect("Qed we checked above")
521					as u32,
522				signature: value.signature,
523			})
524		}
525	}
526
527	/// A signed approval vote which references the candidate indirectly via the block.
528	///
529	/// In practice, we have a look-up from block hash and candidate index to candidate hash,
530	/// so this can be transformed into a `SignedApprovalVote`.
531	#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
532	pub struct IndirectSignedApprovalVoteV2 {
533		/// A block hash where the candidate appears.
534		pub block_hash: Hash,
535		/// The index of the candidate in the list of candidates fully included as-of the block.
536		pub candidate_indices: CandidateBitfield,
537		/// The validator index.
538		pub validator: ValidatorIndex,
539		/// The signature by the validator.
540		pub signature: ValidatorSignature,
541	}
542}
543
544#[cfg(test)]
545mod test {
546	use super::v2::{BitIndex, Bitfield};
547
548	use polkadot_primitives::{CandidateIndex, CoreIndex};
549
550	#[test]
551	fn test_assignment_bitfield_from_vec() {
552		let candidate_indices = vec![1u32, 7, 3, 10, 45, 8, 200, 2];
553		let max_index = *candidate_indices.iter().max().unwrap();
554		let bitfield = Bitfield::try_from(candidate_indices.clone()).unwrap();
555		let candidate_indices =
556			candidate_indices.into_iter().map(|i| BitIndex(i as usize)).collect::<Vec<_>>();
557
558		// Test 1 bits.
559		for index in candidate_indices.clone() {
560			assert!(bitfield.bit_at(index));
561		}
562
563		// Test 0 bits.
564		for index in 0..max_index {
565			if candidate_indices.contains(&BitIndex(index as usize)) {
566				continue;
567			}
568			assert!(!bitfield.bit_at(BitIndex(index as usize)));
569		}
570	}
571
572	#[test]
573	fn test_assignment_bitfield_invariant_msb() {
574		let core_indices = vec![CoreIndex(1), CoreIndex(3), CoreIndex(10), CoreIndex(20)];
575		let mut bitfield = Bitfield::try_from(core_indices.clone()).unwrap();
576		assert!(bitfield.inner_mut().pop().unwrap());
577
578		for i in 0..1024 {
579			assert!(Bitfield::try_from(CoreIndex(i)).unwrap().inner_mut().pop().unwrap());
580			assert!(Bitfield::try_from(i).unwrap().inner_mut().pop().unwrap());
581		}
582	}
583
584	#[test]
585	fn test_assignment_bitfield_basic() {
586		let bitfield = Bitfield::try_from(CoreIndex(0)).unwrap();
587		assert!(bitfield.bit_at(BitIndex(0)));
588		assert!(!bitfield.bit_at(BitIndex(1)));
589		assert_eq!(bitfield.len(), 1);
590
591		let mut bitfield = Bitfield::try_from(20 as CandidateIndex).unwrap();
592		assert!(bitfield.bit_at(BitIndex(20)));
593		assert_eq!(bitfield.inner_mut().count_ones(), 1);
594		assert_eq!(bitfield.len(), 21);
595	}
596}