Skip to main content

avail_rust_core/
consensus.rs

1use crate::substrate::sp_core::vrf::VrfSignature;
2use codec::{Decode, Encode};
3pub use subxt_core::config::substrate::ConsensusEngineId;
4
5pub mod babe {
6	use super::*;
7
8	/// The `ConsensusEngineId` of BABE.
9	pub const BABE_ENGINE_ID: ConsensusEngineId = *b"BABE";
10
11	/// Raw BABE primary slot assignment pre-digest.
12	#[derive(Clone, Encode, Decode, Debug)]
13	pub struct PrimaryPreDigest {
14		/// Authority index
15		pub authority_index: u32,
16		/// Slot
17		pub slot: u64,
18		/// VRF signature
19		pub vrf_signature: VrfSignature,
20	}
21
22	/// BABE secondary slot assignment pre-digest.
23	#[derive(Clone, Encode, Decode, Debug)]
24	pub struct SecondaryPlainPreDigest {
25		/// Authority index
26		///
27		/// This is not strictly-speaking necessary, since the secondary slots
28		/// are assigned based on slot number and epoch randomness. But including
29		/// it makes things easier for higher-level users of the chain data to
30		/// be aware of the author of a secondary-slot block.
31		pub authority_index: u32,
32		/// Slot
33		pub slot: u64,
34	}
35
36	/// BABE secondary deterministic slot assignment with VRF outputs.
37	#[derive(Clone, Encode, Decode, Debug)]
38	pub struct SecondaryVRFPreDigest {
39		/// Authority index
40		pub authority_index: u32,
41		/// Slot
42		pub slot: u64,
43		/// VRF signature
44		pub vrf_signature: VrfSignature,
45	}
46
47	/// A BABE pre-runtime digest. This contains all data required to validate a
48	/// block and for the BABE runtime module. Slots can be assigned to a primary
49	/// (VRF based) and to a secondary (slot number based).
50	#[derive(Clone, Encode, Decode, Debug)]
51	pub enum PreDigest {
52		/// A primary VRF-based slot assignment.
53		#[codec(index = 1)]
54		Primary(PrimaryPreDigest),
55		/// A secondary deterministic slot assignment.
56		#[codec(index = 2)]
57		SecondaryPlain(SecondaryPlainPreDigest),
58		/// A secondary deterministic slot assignment with VRF outputs.
59		#[codec(index = 3)]
60		SecondaryVRF(SecondaryVRFPreDigest),
61	}
62
63	impl PreDigest {
64		/// Returns the slot number of the pre digest.
65		pub fn authority_index(&self) -> u32 {
66			match self {
67				PreDigest::Primary(primary) => primary.authority_index,
68				PreDigest::SecondaryPlain(secondary) => secondary.authority_index,
69				PreDigest::SecondaryVRF(secondary) => secondary.authority_index,
70			}
71		}
72
73		/// Returns the slot of the pre digest.
74		pub fn slot(&self) -> u64 {
75			match self {
76				PreDigest::Primary(primary) => primary.slot,
77				PreDigest::SecondaryPlain(secondary) => secondary.slot,
78				PreDigest::SecondaryVRF(secondary) => secondary.slot,
79			}
80		}
81
82		/// Returns true if this pre-digest is for a primary slot assignment.
83		pub fn is_primary(&self) -> bool {
84			matches!(self, PreDigest::Primary(..))
85		}
86
87		/// Returns the VRF output and proof, if they exist.
88		pub fn vrf_signature(&self) -> Option<&VrfSignature> {
89			match self {
90				PreDigest::Primary(primary) => Some(&primary.vrf_signature),
91				PreDigest::SecondaryVRF(secondary) => Some(&secondary.vrf_signature),
92				PreDigest::SecondaryPlain(_) => None,
93			}
94		}
95	}
96}