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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
pub use sp_consensus_babe::Slot;
pub use sp_consensus_vrf::schnorrkel::{Randomness, VRFOutput, VRFProof};
use parity_scale_codec::{Decode, Encode};
use polkadot_primitives::v2::{
BlockNumber, CandidateHash, CandidateIndex, CoreIndex, Hash, Header, SessionIndex,
ValidatorIndex, ValidatorSignature,
};
use sp_application_crypto::ByteArray;
use sp_consensus_babe as babe_primitives;
pub type DelayTranche = u32;
pub const RELAY_VRF_STORY_CONTEXT: &[u8] = b"A&V RC-VRF";
pub const RELAY_VRF_MODULO_CONTEXT: &[u8] = b"A&V MOD";
pub const RELAY_VRF_DELAY_CONTEXT: &[u8] = b"A&V DELAY";
pub const ASSIGNED_CORE_CONTEXT: &[u8] = b"A&V ASSIGNED";
pub const CORE_RANDOMNESS_CONTEXT: &[u8] = b"A&V CORE";
pub const TRANCHE_RANDOMNESS_CONTEXT: &[u8] = b"A&V TRANCHE";
#[derive(Debug, Clone, Encode, Decode, PartialEq)]
pub struct RelayVRFStory(pub [u8; 32]);
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
pub enum AssignmentCertKind {
RelayVRFModulo {
sample: u32,
},
RelayVRFDelay {
core_index: CoreIndex,
},
}
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
pub struct AssignmentCert {
pub kind: AssignmentCertKind,
pub vrf: (VRFOutput, VRFProof),
}
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
pub struct IndirectAssignmentCert {
pub block_hash: Hash,
pub validator: ValidatorIndex,
pub cert: AssignmentCert,
}
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
pub struct IndirectSignedApprovalVote {
pub block_hash: Hash,
pub candidate_index: CandidateIndex,
pub validator: ValidatorIndex,
pub signature: ValidatorSignature,
}
#[derive(Debug)]
pub struct BlockApprovalMeta {
pub hash: Hash,
pub number: BlockNumber,
pub parent_hash: Hash,
pub candidates: Vec<CandidateHash>,
pub slot: Slot,
pub session: SessionIndex,
}
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum ApprovalError {
#[error("Schnorrkel signature error")]
SchnorrkelSignature(schnorrkel::errors::SignatureError),
#[error("Authority index {0} out of bounds")]
AuthorityOutOfBounds(usize),
}
pub struct UnsafeVRFOutput {
vrf_output: VRFOutput,
slot: Slot,
authority_index: u32,
}
impl UnsafeVRFOutput {
pub fn slot(&self) -> Slot {
self.slot
}
pub fn compute_randomness(
self,
authorities: &[(babe_primitives::AuthorityId, babe_primitives::BabeAuthorityWeight)],
randomness: &babe_primitives::Randomness,
epoch_index: u64,
) -> Result<RelayVRFStory, ApprovalError> {
let author = match authorities.get(self.authority_index as usize) {
None => return Err(ApprovalError::AuthorityOutOfBounds(self.authority_index as _)),
Some(x) => &x.0,
};
let pubkey = schnorrkel::PublicKey::from_bytes(author.as_slice())
.map_err(ApprovalError::SchnorrkelSignature)?;
let transcript = babe_primitives::make_transcript(randomness, self.slot, epoch_index);
let inout = self
.vrf_output
.0
.attach_input_hash(&pubkey, transcript)
.map_err(ApprovalError::SchnorrkelSignature)?;
Ok(RelayVRFStory(inout.make_bytes(RELAY_VRF_STORY_CONTEXT)))
}
}
pub fn babe_unsafe_vrf_info(header: &Header) -> Option<UnsafeVRFOutput> {
use babe_primitives::digests::{CompatibleDigestItem, PreDigest};
for digest in &header.digest.logs {
if let Some(pre) = digest.as_babe_pre_digest() {
let slot = pre.slot();
let authority_index = pre.authority_index();
let vrf_output = match pre {
PreDigest::Primary(primary) => primary.vrf_output,
PreDigest::SecondaryVRF(secondary) => secondary.vrf_output,
PreDigest::SecondaryPlain(_) => return None,
};
return Some(UnsafeVRFOutput { vrf_output, slot, authority_index })
}
}
None
}