sp_consensus_babe/
digests.rs1use super::{
21 AllowedSlots, AuthorityId, AuthorityIndex, AuthoritySignature, BabeAuthorityWeight,
22 BabeEpochConfiguration, Randomness, Slot, BABE_ENGINE_ID,
23};
24
25#[cfg(not(feature = "std"))]
26use alloc::vec::Vec;
27use sp_core::sr25519::vrf::VrfSignature;
28use sp_runtime::{DigestItem, RuntimeDebug};
29
30use codec::{Decode, Encode, MaxEncodedLen};
31use scale_info::TypeInfo;
32
33#[derive(Clone, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
35pub struct PrimaryPreDigest {
36 pub authority_index: super::AuthorityIndex,
38 pub slot: Slot,
40 pub vrf_signature: VrfSignature,
42}
43
44#[derive(Clone, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
46pub struct SecondaryPlainPreDigest {
47 pub authority_index: super::AuthorityIndex,
54 pub slot: Slot,
56}
57
58#[derive(Clone, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
60pub struct SecondaryVRFPreDigest {
61 pub authority_index: super::AuthorityIndex,
63 pub slot: Slot,
65 pub vrf_signature: VrfSignature,
67}
68
69#[derive(Clone, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
73pub enum PreDigest {
74 #[codec(index = 1)]
76 Primary(PrimaryPreDigest),
77 #[codec(index = 2)]
79 SecondaryPlain(SecondaryPlainPreDigest),
80 #[codec(index = 3)]
82 SecondaryVRF(SecondaryVRFPreDigest),
83}
84
85impl PreDigest {
86 pub fn authority_index(&self) -> AuthorityIndex {
88 match self {
89 PreDigest::Primary(primary) => primary.authority_index,
90 PreDigest::SecondaryPlain(secondary) => secondary.authority_index,
91 PreDigest::SecondaryVRF(secondary) => secondary.authority_index,
92 }
93 }
94
95 pub fn slot(&self) -> Slot {
97 match self {
98 PreDigest::Primary(primary) => primary.slot,
99 PreDigest::SecondaryPlain(secondary) => secondary.slot,
100 PreDigest::SecondaryVRF(secondary) => secondary.slot,
101 }
102 }
103
104 pub fn is_primary(&self) -> bool {
106 matches!(self, PreDigest::Primary(..))
107 }
108
109 pub fn added_weight(&self) -> crate::BabeBlockWeight {
112 match self {
113 PreDigest::Primary(_) => 1,
114 PreDigest::SecondaryPlain(_) | PreDigest::SecondaryVRF(_) => 0,
115 }
116 }
117
118 pub fn vrf_signature(&self) -> Option<&VrfSignature> {
120 match self {
121 PreDigest::Primary(primary) => Some(&primary.vrf_signature),
122 PreDigest::SecondaryVRF(secondary) => Some(&secondary.vrf_signature),
123 PreDigest::SecondaryPlain(_) => None,
124 }
125 }
126}
127
128#[derive(Decode, Encode, PartialEq, Eq, Clone, RuntimeDebug)]
131pub struct NextEpochDescriptor {
132 pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,
134
135 pub randomness: Randomness,
137}
138
139#[derive(
142 Decode, Encode, PartialEq, Eq, Clone, RuntimeDebug, MaxEncodedLen, scale_info::TypeInfo,
143)]
144pub enum NextConfigDescriptor {
145 #[codec(index = 1)]
147 V1 {
148 c: (u64, u64),
150 allowed_slots: AllowedSlots,
152 },
153}
154
155impl From<NextConfigDescriptor> for BabeEpochConfiguration {
156 fn from(desc: NextConfigDescriptor) -> Self {
157 match desc {
158 NextConfigDescriptor::V1 { c, allowed_slots } => Self { c, allowed_slots },
159 }
160 }
161}
162
163pub trait CompatibleDigestItem: Sized {
165 fn babe_pre_digest(seal: PreDigest) -> Self;
167
168 fn as_babe_pre_digest(&self) -> Option<PreDigest>;
170
171 fn babe_seal(signature: AuthoritySignature) -> Self;
173
174 fn as_babe_seal(&self) -> Option<AuthoritySignature>;
176
177 fn as_next_epoch_descriptor(&self) -> Option<NextEpochDescriptor>;
179
180 fn as_next_config_descriptor(&self) -> Option<NextConfigDescriptor>;
182}
183
184impl CompatibleDigestItem for DigestItem {
185 fn babe_pre_digest(digest: PreDigest) -> Self {
186 DigestItem::PreRuntime(BABE_ENGINE_ID, digest.encode())
187 }
188
189 fn as_babe_pre_digest(&self) -> Option<PreDigest> {
190 self.pre_runtime_try_to(&BABE_ENGINE_ID)
191 }
192
193 fn babe_seal(signature: AuthoritySignature) -> Self {
194 DigestItem::Seal(BABE_ENGINE_ID, signature.encode())
195 }
196
197 fn as_babe_seal(&self) -> Option<AuthoritySignature> {
198 self.seal_try_to(&BABE_ENGINE_ID)
199 }
200
201 fn as_next_epoch_descriptor(&self) -> Option<NextEpochDescriptor> {
202 self.consensus_try_to(&BABE_ENGINE_ID)
203 .and_then(|x: super::ConsensusLog| match x {
204 super::ConsensusLog::NextEpochData(n) => Some(n),
205 _ => None,
206 })
207 }
208
209 fn as_next_config_descriptor(&self) -> Option<NextConfigDescriptor> {
210 self.consensus_try_to(&BABE_ENGINE_ID)
211 .and_then(|x: super::ConsensusLog| match x {
212 super::ConsensusLog::NextConfigData(n) => Some(n),
213 _ => None,
214 })
215 }
216}