1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#[cfg(feature = "std")]
use super::{BABE_ENGINE_ID, AuthoritySignature};
use super::{AuthorityId, AuthorityIndex, SlotNumber, BabeAuthorityWeight};
#[cfg(feature = "std")]
use sp_runtime::{DigestItem, generic::OpaqueDigestItemId};
#[cfg(feature = "std")]
use std::{fmt::Debug, convert::{TryFrom, TryInto}};
use codec::{Decode, Encode};
#[cfg(feature = "std")]
use codec::Codec;
use sp_std::vec::Vec;
use sp_runtime::RuntimeDebug;
use sp_consensus_vrf::schnorrkel::{self, Randomness};
#[cfg(feature = "std")]
use sp_consensus_vrf::schnorrkel::SignatureError;
#[derive(Clone, RuntimeDebug, Encode, Decode)]
pub struct RawPrimaryPreDigest<VRFOutput=schnorrkel::RawVRFOutput, VRFProof=schnorrkel::RawVRFProof> {
pub authority_index: super::AuthorityIndex,
pub slot_number: SlotNumber,
pub vrf_output: VRFOutput,
pub vrf_proof: VRFProof,
}
#[cfg(feature = "std")]
pub type PrimaryPreDigest = RawPrimaryPreDigest<schnorrkel::VRFOutput, schnorrkel::VRFProof>;
#[cfg(feature = "std")]
impl TryFrom<RawPrimaryPreDigest> for PrimaryPreDigest {
type Error = SignatureError;
fn try_from(raw: RawPrimaryPreDigest) -> Result<PrimaryPreDigest, SignatureError> {
Ok(PrimaryPreDigest {
authority_index: raw.authority_index,
slot_number: raw.slot_number,
vrf_output: raw.vrf_output.try_into()?,
vrf_proof: raw.vrf_proof.try_into()?,
})
}
}
#[derive(Clone, RuntimeDebug, Encode, Decode)]
pub struct SecondaryPreDigest {
pub authority_index: super::AuthorityIndex,
pub slot_number: SlotNumber,
}
#[derive(Clone, RuntimeDebug, Encode, Decode)]
pub enum RawPreDigest<VRFOutput=schnorrkel::RawVRFOutput, VRFProof=schnorrkel::RawVRFProof> {
#[codec(index = "1")]
Primary(RawPrimaryPreDigest<VRFOutput, VRFProof>),
#[codec(index = "2")]
Secondary(SecondaryPreDigest),
}
#[cfg(feature = "std")]
pub type PreDigest = RawPreDigest<schnorrkel::VRFOutput, schnorrkel::VRFProof>;
impl<VRFOutput, VRFProof> RawPreDigest<VRFOutput, VRFProof> {
pub fn authority_index(&self) -> AuthorityIndex {
match self {
RawPreDigest::Primary(primary) => primary.authority_index,
RawPreDigest::Secondary(secondary) => secondary.authority_index,
}
}
pub fn slot_number(&self) -> SlotNumber {
match self {
RawPreDigest::Primary(primary) => primary.slot_number,
RawPreDigest::Secondary(secondary) => secondary.slot_number,
}
}
pub fn added_weight(&self) -> crate::BabeBlockWeight {
match self {
RawPreDigest::Primary(_) => 1,
RawPreDigest::Secondary(_) => 0,
}
}
}
#[cfg(feature = "std")]
impl TryFrom<RawPreDigest> for PreDigest {
type Error = SignatureError;
fn try_from(raw: RawPreDigest) -> Result<PreDigest, SignatureError> {
Ok(match raw {
RawPreDigest::Primary(primary) => PreDigest::Primary(primary.try_into()?),
RawPreDigest::Secondary(secondary) => PreDigest::Secondary(secondary),
})
}
}
#[derive(Decode, Encode, Default, PartialEq, Eq, Clone, RuntimeDebug)]
pub struct NextEpochDescriptor {
pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,
pub randomness: Randomness,
}
#[cfg(feature = "std")]
pub trait CompatibleDigestItem: Sized {
fn babe_pre_digest(seal: PreDigest) -> Self;
fn as_babe_pre_digest(&self) -> Option<PreDigest>;
fn babe_seal(signature: AuthoritySignature) -> Self;
fn as_babe_seal(&self) -> Option<AuthoritySignature>;
fn as_next_epoch_descriptor(&self) -> Option<NextEpochDescriptor>;
}
#[cfg(feature = "std")]
impl<Hash> CompatibleDigestItem for DigestItem<Hash> where
Hash: Debug + Send + Sync + Eq + Clone + Codec + 'static
{
fn babe_pre_digest(digest: PreDigest) -> Self {
DigestItem::PreRuntime(BABE_ENGINE_ID, digest.encode())
}
fn as_babe_pre_digest(&self) -> Option<PreDigest> {
self.try_to(OpaqueDigestItemId::PreRuntime(&BABE_ENGINE_ID))
}
fn babe_seal(signature: AuthoritySignature) -> Self {
DigestItem::Seal(BABE_ENGINE_ID, signature.encode())
}
fn as_babe_seal(&self) -> Option<AuthoritySignature> {
self.try_to(OpaqueDigestItemId::Seal(&BABE_ENGINE_ID))
}
fn as_next_epoch_descriptor(&self) -> Option<NextEpochDescriptor> {
self.try_to(OpaqueDigestItemId::Consensus(&BABE_ENGINE_ID))
.and_then(|x: super::ConsensusLog| match x {
super::ConsensusLog::NextEpochData(n) => Some(n),
_ => None,
})
}
}