Coverage Report

Created: 2024-05-16 12:16

/__w/smoldot/smoldot/repo/lib/src/header/babe.rs
Line
Count
Source (jump to first uncovered line)
1
// Smoldot
2
// Copyright (C) 2019-2022  Parity Technologies (UK) Ltd.
3
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
4
5
// This program is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
10
// This program is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
15
// You should have received a copy of the GNU General Public License
16
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
use super::Error;
19
use crate::util;
20
21
use alloc::vec::Vec;
22
use core::{cmp, fmt, iter, slice};
23
24
/// A consensus log item for BABE.
25
#[derive(Debug, Clone, PartialEq, Eq)]
26
pub enum BabeConsensusLogRef<'a> {
27
    /// The epoch has changed. This provides information about the _next_
28
    /// epoch - information about the _current_ epoch (i.e. the one we've just
29
    /// entered) should already be available earlier in the chain.
30
    NextEpochData(BabeNextEpochRef<'a>),
31
    /// Disable the authority with given index.
32
    OnDisabled(u32),
33
    /// The epoch has changed, and the epoch after the current one will
34
    /// enact different epoch configurations.
35
    NextConfigData(BabeNextConfig),
36
}
37
38
impl<'a> BabeConsensusLogRef<'a> {
39
    /// Decodes a [`BabeConsensusLogRef`] from a slice of bytes.
40
51
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
41
51
        Ok(match slice.first() {
42
            Some(1) => {
43
27
                BabeConsensusLogRef::NextEpochData(BabeNextEpochRef::from_slice(&slice[1..])
?0
)
44
            }
45
            Some(2) => {
46
22
                let n = u32::from_le_bytes(
47
22
                    <[u8; 4]>::try_from(&slice[1..]).map_err(|_| 
Error::DigestItemDecodeError0
)
?0
,
Unexecuted instantiation: _RNCNvMNtNtCsN16ciHI6Qf_7smoldot6header4babeNtB4_19BabeConsensusLogRef10from_slice0B8_
Unexecuted instantiation: _RNCNvMNtNtCseuYC0Zibziv_7smoldot6header4babeNtB4_19BabeConsensusLogRef10from_slice0B8_
48
                );
49
22
                BabeConsensusLogRef::OnDisabled(n)
50
            }
51
            Some(3) => {
52
                // The Babe configuration info starts with a version number that is always `1`
53
                // at the moment.
54
2
                if slice.len() < 2 || slice[1] != 1 {
55
0
                    return Err(Error::BadBabeNextConfigVersion);
56
2
                }
57
2
                BabeConsensusLogRef::NextConfigData(BabeNextConfig::from_slice(&slice[2..])
?0
)
58
            }
59
0
            Some(_) => return Err(Error::BadBabeConsensusRefType),
60
0
            None => return Err(Error::TooShort),
61
        })
62
51
    }
_RNvMNtNtCsN16ciHI6Qf_7smoldot6header4babeNtB2_19BabeConsensusLogRef10from_slice
Line
Count
Source
40
51
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
41
51
        Ok(match slice.first() {
42
            Some(1) => {
43
27
                BabeConsensusLogRef::NextEpochData(BabeNextEpochRef::from_slice(&slice[1..])
?0
)
44
            }
45
            Some(2) => {
46
22
                let n = u32::from_le_bytes(
47
22
                    <[u8; 4]>::try_from(&slice[1..]).map_err(|_| Error::DigestItemDecodeError)
?0
,
48
                );
49
22
                BabeConsensusLogRef::OnDisabled(n)
50
            }
51
            Some(3) => {
52
                // The Babe configuration info starts with a version number that is always `1`
53
                // at the moment.
54
2
                if slice.len() < 2 || slice[1] != 1 {
55
0
                    return Err(Error::BadBabeNextConfigVersion);
56
2
                }
57
2
                BabeConsensusLogRef::NextConfigData(BabeNextConfig::from_slice(&slice[2..])
?0
)
58
            }
59
0
            Some(_) => return Err(Error::BadBabeConsensusRefType),
60
0
            None => return Err(Error::TooShort),
61
        })
62
51
    }
Unexecuted instantiation: _RNvMNtNtCseuYC0Zibziv_7smoldot6header4babeNtB2_19BabeConsensusLogRef10from_slice
63
64
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
65
    /// encoding of that object.
66
17
    pub fn scale_encoding(
67
17
        &self,
68
17
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + 'a> + Clone + 'a {
69
17
        let index = iter::once(match self {
70
5
            BabeConsensusLogRef::NextEpochData(_) => [1],
71
11
            BabeConsensusLogRef::OnDisabled(_) => [2],
72
1
            BabeConsensusLogRef::NextConfigData(_) => [3],
73
        });
74
75
17
        let body = match self {
76
5
            BabeConsensusLogRef::NextEpochData(digest) => either::Left(either::Left(
77
5
                digest.scale_encoding().map(either::Left).map(either::Left),
78
5
            )),
79
11
            BabeConsensusLogRef::OnDisabled(digest) => either::Left(either::Right(iter::once(
80
11
                either::Left(either::Right(digest.to_le_bytes())),
81
11
            ))),
82
1
            BabeConsensusLogRef::NextConfigData(digest) => either::Right(
83
1
                iter::once(either::Right(either::Left([1]))).chain(
84
1
                    digest
85
1
                        .scale_encoding()
86
1
                        .map(either::Right)
87
1
                        .map(either::Right),
88
1
                ),
89
1
            ),
90
        };
91
92
17
        index.map(either::Left).chain(body.map(either::Right))
93
17
    }
_RNvMNtNtCsN16ciHI6Qf_7smoldot6header4babeNtB2_19BabeConsensusLogRef14scale_encoding
Line
Count
Source
66
17
    pub fn scale_encoding(
67
17
        &self,
68
17
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + 'a> + Clone + 'a {
69
17
        let index = iter::once(match self {
70
5
            BabeConsensusLogRef::NextEpochData(_) => [1],
71
11
            BabeConsensusLogRef::OnDisabled(_) => [2],
72
1
            BabeConsensusLogRef::NextConfigData(_) => [3],
73
        });
74
75
17
        let body = match self {
76
5
            BabeConsensusLogRef::NextEpochData(digest) => either::Left(either::Left(
77
5
                digest.scale_encoding().map(either::Left).map(either::Left),
78
5
            )),
79
11
            BabeConsensusLogRef::OnDisabled(digest) => either::Left(either::Right(iter::once(
80
11
                either::Left(either::Right(digest.to_le_bytes())),
81
11
            ))),
82
1
            BabeConsensusLogRef::NextConfigData(digest) => either::Right(
83
1
                iter::once(either::Right(either::Left([1]))).chain(
84
1
                    digest
85
1
                        .scale_encoding()
86
1
                        .map(either::Right)
87
1
                        .map(either::Right),
88
1
                ),
89
1
            ),
90
        };
91
92
17
        index.map(either::Left).chain(body.map(either::Right))
93
17
    }
Unexecuted instantiation: _RNvMNtNtCseuYC0Zibziv_7smoldot6header4babeNtB2_19BabeConsensusLogRef14scale_encoding
94
}
95
96
impl<'a> From<&'a BabeConsensusLog> for BabeConsensusLogRef<'a> {
97
0
    fn from(a: &'a BabeConsensusLog) -> Self {
98
0
        match a {
99
0
            BabeConsensusLog::NextEpochData(v) => BabeConsensusLogRef::NextEpochData(v.into()),
100
0
            BabeConsensusLog::OnDisabled(v) => BabeConsensusLogRef::OnDisabled(*v),
101
0
            BabeConsensusLog::NextConfigData(v) => BabeConsensusLogRef::NextConfigData(*v),
102
        }
103
0
    }
Unexecuted instantiation: _RNvXs_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB4_19BabeConsensusLogRefINtNtCsaYZPK01V26L_4core7convert4FromRNtB4_16BabeConsensusLogE4from
Unexecuted instantiation: _RNvXs_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB4_19BabeConsensusLogRefINtNtCsaYZPK01V26L_4core7convert4FromRNtB4_16BabeConsensusLogE4from
104
}
105
106
/// A consensus log item for BABE.
107
#[derive(Debug, Clone, PartialEq, Eq)]
108
pub enum BabeConsensusLog {
109
    /// The epoch has changed. This provides information about the _next_
110
    /// epoch - information about the _current_ epoch (i.e. the one we've just
111
    /// entered) should already be available earlier in the chain.
112
    NextEpochData(BabeNextEpoch),
113
    /// Disable the authority with given index.
114
    OnDisabled(u32),
115
    /// The epoch has changed, and the epoch after the current one will
116
    /// enact different epoch configurations.
117
    NextConfigData(BabeNextConfig),
118
}
119
120
impl<'a> From<BabeConsensusLogRef<'a>> for BabeConsensusLog {
121
0
    fn from(a: BabeConsensusLogRef<'a>) -> Self {
122
0
        match a {
123
0
            BabeConsensusLogRef::NextEpochData(v) => BabeConsensusLog::NextEpochData(v.into()),
124
0
            BabeConsensusLogRef::OnDisabled(v) => BabeConsensusLog::OnDisabled(v),
125
0
            BabeConsensusLogRef::NextConfigData(v) => BabeConsensusLog::NextConfigData(v),
126
        }
127
0
    }
Unexecuted instantiation: _RNvXs0_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_16BabeConsensusLogINtNtCsaYZPK01V26L_4core7convert4FromNtB5_19BabeConsensusLogRefE4from
Unexecuted instantiation: _RNvXs0_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_16BabeConsensusLogINtNtCsaYZPK01V26L_4core7convert4FromNtB5_19BabeConsensusLogRefE4from
128
}
129
130
/// Information about the next epoch. This is broadcast in the first block
131
/// of the epoch.
132
#[derive(Debug, Clone, PartialEq, Eq)]
133
pub struct BabeNextEpochRef<'a> {
134
    /// The authorities.
135
    pub authorities: BabeAuthoritiesIter<'a>,
136
137
    /// The value of randomness to use for the slot-assignment.
138
    pub randomness: &'a [u8; 32],
139
}
140
141
impl<'a> BabeNextEpochRef<'a> {
142
    /// Decodes a [`BabePreDigestRef`] from a slice of bytes.
143
27
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
144
27
        let (slice, authorities_len) =
145
27
            match crate::util::nom_scale_compact_usize::<nom::error::Error<&[u8]>>(slice) {
146
27
                Ok(s) => s,
147
0
                Err(_) => return Err(Error::TooShort),
148
            };
149
150
27
        if slice.len() != authorities_len * 40 + 32 {
151
0
            return Err(Error::TooShort);
152
27
        }
153
27
154
27
        Ok(BabeNextEpochRef {
155
27
            authorities: BabeAuthoritiesIter(BabeAuthoritiesIterInner::Raw(
156
27
                slice[0..authorities_len * 40].chunks(40),
157
27
            )),
158
27
            randomness: <&[u8; 32]>::try_from(&slice[authorities_len * 40..]).unwrap(),
159
27
        })
160
27
    }
_RNvMs1_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_16BabeNextEpochRef10from_slice
Line
Count
Source
143
27
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
144
27
        let (slice, authorities_len) =
145
27
            match crate::util::nom_scale_compact_usize::<nom::error::Error<&[u8]>>(slice) {
146
27
                Ok(s) => s,
147
0
                Err(_) => return Err(Error::TooShort),
148
            };
149
150
27
        if slice.len() != authorities_len * 40 + 32 {
151
0
            return Err(Error::TooShort);
152
27
        }
153
27
154
27
        Ok(BabeNextEpochRef {
155
27
            authorities: BabeAuthoritiesIter(BabeAuthoritiesIterInner::Raw(
156
27
                slice[0..authorities_len * 40].chunks(40),
157
27
            )),
158
27
            randomness: <&[u8; 32]>::try_from(&slice[authorities_len * 40..]).unwrap(),
159
27
        })
160
27
    }
Unexecuted instantiation: _RNvMs1_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_16BabeNextEpochRef10from_slice
161
162
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
163
    /// encoding of that object.
164
5
    pub fn scale_encoding(
165
5
        &self,
166
5
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + 'a> + Clone + 'a {
167
5
        let header = util::encode_scale_compact_usize(self.authorities.len());
168
5
        iter::once(either::Left(header))
169
5
            .chain(
170
5
                self.authorities
171
5
                    .clone()
172
924
                    .flat_map(|a| a.scale_encoding())
_RNCNvMs1_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB7_16BabeNextEpochRef14scale_encoding0Bb_
Line
Count
Source
172
924
                    .flat_map(|a| a.scale_encoding())
Unexecuted instantiation: _RNCNvMs1_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB7_16BabeNextEpochRef14scale_encoding0Bb_
173
1.84k
                    .map(|buf| either::Right(either::Left(buf))),
_RNCNvMs1_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB7_16BabeNextEpochRef14scale_encodings_0Bb_
Line
Count
Source
173
1.84k
                    .map(|buf| either::Right(either::Left(buf))),
Unexecuted instantiation: _RNCNvMs1_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB7_16BabeNextEpochRef14scale_encodings_0Bb_
174
5
            )
175
5
            .chain(iter::once(either::Right(either::Right(
176
5
                &self.randomness[..],
177
5
            ))))
178
5
    }
_RNvMs1_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_16BabeNextEpochRef14scale_encoding
Line
Count
Source
164
5
    pub fn scale_encoding(
165
5
        &self,
166
5
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + 'a> + Clone + 'a {
167
5
        let header = util::encode_scale_compact_usize(self.authorities.len());
168
5
        iter::once(either::Left(header))
169
5
            .chain(
170
5
                self.authorities
171
5
                    .clone()
172
5
                    .flat_map(|a| a.scale_encoding())
173
5
                    .map(|buf| either::Right(either::Left(buf))),
174
5
            )
175
5
            .chain(iter::once(either::Right(either::Right(
176
5
                &self.randomness[..],
177
5
            ))))
178
5
    }
Unexecuted instantiation: _RNvMs1_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_16BabeNextEpochRef14scale_encoding
179
}
180
181
impl<'a> From<&'a BabeNextEpoch> for BabeNextEpochRef<'a> {
182
0
    fn from(a: &'a BabeNextEpoch) -> Self {
183
0
        BabeNextEpochRef {
184
0
            authorities: BabeAuthoritiesIter(BabeAuthoritiesIterInner::List(a.authorities.iter())),
185
0
            randomness: &a.randomness,
186
0
        }
187
0
    }
Unexecuted instantiation: _RNvXs2_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_16BabeNextEpochRefINtNtCsaYZPK01V26L_4core7convert4FromRNtB5_13BabeNextEpochE4from
Unexecuted instantiation: _RNvXs2_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_16BabeNextEpochRefINtNtCsaYZPK01V26L_4core7convert4FromRNtB5_13BabeNextEpochE4from
188
}
189
190
/// Information about the next epoch. This is broadcast in the first block
191
/// of the epoch.
192
#[derive(Debug, Clone, PartialEq, Eq)]
193
pub struct BabeNextEpoch {
194
    /// The authorities.
195
    pub authorities: Vec<BabeAuthority>,
196
197
    /// The value of randomness to use for the slot-assignment.
198
    pub randomness: [u8; 32],
199
}
200
201
impl<'a> From<BabeNextEpochRef<'a>> for BabeNextEpoch {
202
0
    fn from(a: BabeNextEpochRef<'a>) -> Self {
203
0
        BabeNextEpoch {
204
0
            authorities: a.authorities.map(Into::into).collect(),
205
0
            randomness: *a.randomness,
206
0
        }
207
0
    }
Unexecuted instantiation: _RNvXs3_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_13BabeNextEpochINtNtCsaYZPK01V26L_4core7convert4FromNtB5_16BabeNextEpochRefE4from
Unexecuted instantiation: _RNvXs3_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_13BabeNextEpochINtNtCsaYZPK01V26L_4core7convert4FromNtB5_16BabeNextEpochRefE4from
208
}
209
210
/// List of authorities in a BABE context.
211
#[derive(Clone)]
212
pub struct BabeAuthoritiesIter<'a>(BabeAuthoritiesIterInner<'a>);
213
214
#[derive(Clone)]
215
enum BabeAuthoritiesIterInner<'a> {
216
    List(slice::Iter<'a, BabeAuthority>),
217
    Raw(slice::Chunks<'a, u8>),
218
}
219
220
impl<'a> BabeAuthoritiesIter<'a> {
221
    /// Builds a new [`BabeAuthoritiesIter`] iterating over the given slice.
222
12
    pub fn from_slice(slice: &'a [BabeAuthority]) -> Self {
223
12
        Self(BabeAuthoritiesIterInner::List(slice.iter()))
224
12
    }
_RNvMs4_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_19BabeAuthoritiesIter10from_slice
Line
Count
Source
222
12
    pub fn from_slice(slice: &'a [BabeAuthority]) -> Self {
223
12
        Self(BabeAuthoritiesIterInner::List(slice.iter()))
224
12
    }
Unexecuted instantiation: _RNvMs4_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_19BabeAuthoritiesIter10from_slice
225
}
226
227
impl<'a> Iterator for BabeAuthoritiesIter<'a> {
228
    type Item = BabeAuthorityRef<'a>;
229
230
966
    fn next(&mut self) -> Option<Self::Item> {
231
966
        match &mut self.0 {
232
23
            BabeAuthoritiesIterInner::List(l) => l.next().map(Into::into),
233
943
            BabeAuthoritiesIterInner::Raw(l) => {
234
943
                let 
item936
= l.next()
?7
;
235
936
                assert_eq!(item.len(), 40);
236
936
                Some(BabeAuthorityRef {
237
936
                    public_key: <&[u8; 32]>::try_from(&item[0..32]).unwrap(),
238
936
                    weight: u64::from_le_bytes(<[u8; 8]>::try_from(&item[32..40]).unwrap()),
239
936
                })
240
            }
241
        }
242
966
    }
_RNvXs5_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_19BabeAuthoritiesIterNtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4next
Line
Count
Source
230
966
    fn next(&mut self) -> Option<Self::Item> {
231
966
        match &mut self.0 {
232
23
            BabeAuthoritiesIterInner::List(l) => l.next().map(Into::into),
233
943
            BabeAuthoritiesIterInner::Raw(l) => {
234
943
                let 
item936
= l.next()
?7
;
235
936
                assert_eq!(item.len(), 40);
236
936
                Some(BabeAuthorityRef {
237
936
                    public_key: <&[u8; 32]>::try_from(&item[0..32]).unwrap(),
238
936
                    weight: u64::from_le_bytes(<[u8; 8]>::try_from(&item[32..40]).unwrap()),
239
936
                })
240
            }
241
        }
242
966
    }
Unexecuted instantiation: _RNvXs5_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_19BabeAuthoritiesIterNtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4next
243
244
11
    fn size_hint(&self) -> (usize, Option<usize>) {
245
11
        match &self.0 {
246
4
            BabeAuthoritiesIterInner::List(l) => l.size_hint(),
247
7
            BabeAuthoritiesIterInner::Raw(l) => l.size_hint(),
248
        }
249
11
    }
_RNvXs5_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_19BabeAuthoritiesIterNtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hint
Line
Count
Source
244
11
    fn size_hint(&self) -> (usize, Option<usize>) {
245
11
        match &self.0 {
246
4
            BabeAuthoritiesIterInner::List(l) => l.size_hint(),
247
7
            BabeAuthoritiesIterInner::Raw(l) => l.size_hint(),
248
        }
249
11
    }
Unexecuted instantiation: _RNvXs5_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_19BabeAuthoritiesIterNtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hint
250
}
251
252
impl<'a> ExactSizeIterator for BabeAuthoritiesIter<'a> {}
253
254
impl<'a> cmp::PartialEq<BabeAuthoritiesIter<'a>> for BabeAuthoritiesIter<'a> {
255
0
    fn eq(&self, other: &BabeAuthoritiesIter<'a>) -> bool {
256
0
        let mut a = self.clone();
257
0
        let mut b = other.clone();
258
        loop {
259
0
            match (a.next(), b.next()) {
260
0
                (Some(a), Some(b)) if a == b => {}
261
0
                (None, None) => return true,
262
0
                _ => return false,
263
            }
264
        }
265
0
    }
Unexecuted instantiation: _RNvXs7_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_19BabeAuthoritiesIterNtNtCsaYZPK01V26L_4core3cmp9PartialEq2eq
Unexecuted instantiation: _RNvXs7_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_19BabeAuthoritiesIterNtNtCsaYZPK01V26L_4core3cmp9PartialEq2eq
266
}
267
268
impl<'a> cmp::Eq for BabeAuthoritiesIter<'a> {}
269
270
impl<'a> fmt::Debug for BabeAuthoritiesIter<'a> {
271
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
272
0
        f.debug_list().entries(self.clone()).finish()
273
0
    }
Unexecuted instantiation: _RNvXs9_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_19BabeAuthoritiesIterNtNtCsaYZPK01V26L_4core3fmt5Debug3fmt
Unexecuted instantiation: _RNvXs9_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_19BabeAuthoritiesIterNtNtCsaYZPK01V26L_4core3fmt5Debug3fmt
274
}
275
276
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
277
pub struct BabeAuthorityRef<'a> {
278
    /// Sr25519 public key.
279
    pub public_key: &'a [u8; 32],
280
    /// Arbitrary number indicating the weight of the authority.
281
    ///
282
    /// This value can only be compared to other weight values.
283
    // TODO: should be NonZeroU64; requires deep changes in decoding code though
284
    pub weight: u64,
285
}
286
287
impl<'a> BabeAuthorityRef<'a> {
288
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
289
    /// encoding of that object.
290
924
    pub fn scale_encoding(
291
924
        &self,
292
924
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + 'a> + Clone + 'a {
293
924
        iter::once(either::Right(self.public_key))
294
924
            .chain(iter::once(either::Left(self.weight.to_le_bytes())))
295
924
    }
_RNvMsa_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_16BabeAuthorityRef14scale_encoding
Line
Count
Source
290
924
    pub fn scale_encoding(
291
924
        &self,
292
924
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + 'a> + Clone + 'a {
293
924
        iter::once(either::Right(self.public_key))
294
924
            .chain(iter::once(either::Left(self.weight.to_le_bytes())))
295
924
    }
Unexecuted instantiation: _RNvMsa_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_16BabeAuthorityRef14scale_encoding
296
}
297
298
impl<'a> From<&'a BabeAuthority> for BabeAuthorityRef<'a> {
299
21
    fn from(a: &'a BabeAuthority) -> Self {
300
21
        BabeAuthorityRef {
301
21
            public_key: &a.public_key,
302
21
            weight: a.weight,
303
21
        }
304
21
    }
_RNvXsb_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_16BabeAuthorityRefINtNtCsaYZPK01V26L_4core7convert4FromRNtB5_13BabeAuthorityE4from
Line
Count
Source
299
21
    fn from(a: &'a BabeAuthority) -> Self {
300
21
        BabeAuthorityRef {
301
21
            public_key: &a.public_key,
302
21
            weight: a.weight,
303
21
        }
304
21
    }
Unexecuted instantiation: _RNvXsb_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_16BabeAuthorityRefINtNtCsaYZPK01V26L_4core7convert4FromRNtB5_13BabeAuthorityE4from
305
}
306
307
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
308
pub struct BabeAuthority {
309
    /// Sr25519 public key.
310
    pub public_key: [u8; 32],
311
    /// Arbitrary number indicating the weight of the authority.
312
    ///
313
    /// This value can only be compared to other weight values.
314
    // TODO: should be NonZeroU64; requires deep changes in decoding code though
315
    pub weight: u64,
316
}
317
318
impl<'a> From<BabeAuthorityRef<'a>> for BabeAuthority {
319
12
    fn from(a: BabeAuthorityRef<'a>) -> Self {
320
12
        BabeAuthority {
321
12
            public_key: *a.public_key,
322
12
            weight: a.weight,
323
12
        }
324
12
    }
_RNvXsc_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_13BabeAuthorityINtNtCsaYZPK01V26L_4core7convert4FromNtB5_16BabeAuthorityRefE4from
Line
Count
Source
319
12
    fn from(a: BabeAuthorityRef<'a>) -> Self {
320
12
        BabeAuthority {
321
12
            public_key: *a.public_key,
322
12
            weight: a.weight,
323
12
        }
324
12
    }
Unexecuted instantiation: _RNvXsc_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_13BabeAuthorityINtNtCsaYZPK01V26L_4core7convert4FromNtB5_16BabeAuthorityRefE4from
325
}
326
327
/// Information about the next epoch config, if changed. This is broadcast in the first
328
/// block of the epoch, and applies using the same rules as `NextEpochDescriptor`.
329
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
330
pub struct BabeNextConfig {
331
    /// Value of `c` in `BabeEpochConfiguration`.
332
    pub c: (u64, u64),
333
    /// Value of `allowed_slots` in `BabeEpochConfiguration`.
334
    pub allowed_slots: BabeAllowedSlots,
335
}
336
337
impl BabeNextConfig {
338
    /// Decodes a [`BabeNextConfig`] from a slice of bytes.
339
6
    pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
340
6
        if slice.len() < 16 {
341
0
            return Err(Error::TooShort);
342
6
        }
343
6
344
6
        let c0 = u64::from_le_bytes(<[u8; 8]>::try_from(&slice[..8]).unwrap());
345
6
        let c1 = u64::from_le_bytes(<[u8; 8]>::try_from(&slice[8..16]).unwrap());
346
6
        let allowed_slots = BabeAllowedSlots::from_slice(&slice[16..])
?0
;
347
348
6
        Ok(BabeNextConfig {
349
6
            c: (c0, c1),
350
6
            allowed_slots,
351
6
        })
352
6
    }
_RNvMsd_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_14BabeNextConfig10from_slice
Line
Count
Source
339
6
    pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
340
6
        if slice.len() < 16 {
341
0
            return Err(Error::TooShort);
342
6
        }
343
6
344
6
        let c0 = u64::from_le_bytes(<[u8; 8]>::try_from(&slice[..8]).unwrap());
345
6
        let c1 = u64::from_le_bytes(<[u8; 8]>::try_from(&slice[8..16]).unwrap());
346
6
        let allowed_slots = BabeAllowedSlots::from_slice(&slice[16..])
?0
;
347
348
6
        Ok(BabeNextConfig {
349
6
            c: (c0, c1),
350
6
            allowed_slots,
351
6
        })
352
6
    }
Unexecuted instantiation: _RNvMsd_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_14BabeNextConfig10from_slice
353
354
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
355
    /// encoding of that object.
356
1
    pub fn scale_encoding(&self) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
357
1
        iter::once(either::Left(self.c.0.to_le_bytes()))
358
1
            .chain(iter::once(either::Left(self.c.1.to_le_bytes())))
359
1
            .chain(self.allowed_slots.scale_encoding().map(either::Right))
360
1
    }
_RNvMsd_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_14BabeNextConfig14scale_encoding
Line
Count
Source
356
1
    pub fn scale_encoding(&self) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
357
1
        iter::once(either::Left(self.c.0.to_le_bytes()))
358
1
            .chain(iter::once(either::Left(self.c.1.to_le_bytes())))
359
1
            .chain(self.allowed_slots.scale_encoding().map(either::Right))
360
1
    }
Unexecuted instantiation: _RNvMsd_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_14BabeNextConfig14scale_encoding
361
}
362
363
/// Types of allowed slots.
364
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
365
pub enum BabeAllowedSlots {
366
    /// Only allow primary slot claims.
367
    PrimarySlots,
368
    /// Allow primary and secondary plain slot claims.
369
    PrimaryAndSecondaryPlainSlots,
370
    /// Allow primary and secondary VRF slot claims.
371
    PrimaryAndSecondaryVrfSlots,
372
}
373
374
impl BabeAllowedSlots {
375
    /// Decodes a [`BabeAllowedSlots`] from a slice of bytes.
376
7
    pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
377
7
        Ok(match slice.first() {
378
0
            Some(0) => BabeAllowedSlots::PrimarySlots,
379
0
            Some(1) => BabeAllowedSlots::PrimaryAndSecondaryPlainSlots,
380
7
            Some(2) => BabeAllowedSlots::PrimaryAndSecondaryVrfSlots,
381
0
            _ => return Err(Error::BadBabePreDigestRefType),
382
        })
383
7
    }
_RNvMse_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_16BabeAllowedSlots10from_slice
Line
Count
Source
376
7
    pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
377
7
        Ok(match slice.first() {
378
0
            Some(0) => BabeAllowedSlots::PrimarySlots,
379
0
            Some(1) => BabeAllowedSlots::PrimaryAndSecondaryPlainSlots,
380
7
            Some(2) => BabeAllowedSlots::PrimaryAndSecondaryVrfSlots,
381
0
            _ => return Err(Error::BadBabePreDigestRefType),
382
        })
383
7
    }
Unexecuted instantiation: _RNvMse_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_16BabeAllowedSlots10from_slice
384
385
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
386
    /// encoding of that object.
387
1
    pub fn scale_encoding(&self) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
388
1
        iter::once(match self {
389
0
            BabeAllowedSlots::PrimarySlots => [0],
390
0
            BabeAllowedSlots::PrimaryAndSecondaryPlainSlots => [1],
391
1
            BabeAllowedSlots::PrimaryAndSecondaryVrfSlots => [2],
392
        })
393
1
    }
_RNvMse_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_16BabeAllowedSlots14scale_encoding
Line
Count
Source
387
1
    pub fn scale_encoding(&self) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
388
1
        iter::once(match self {
389
0
            BabeAllowedSlots::PrimarySlots => [0],
390
0
            BabeAllowedSlots::PrimaryAndSecondaryPlainSlots => [1],
391
1
            BabeAllowedSlots::PrimaryAndSecondaryVrfSlots => [2],
392
        })
393
1
    }
Unexecuted instantiation: _RNvMse_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_16BabeAllowedSlots14scale_encoding
394
}
395
396
/// A BABE pre-runtime digest. This contains all data required to validate a
397
/// block and for the BABE runtime module. Slots can be assigned to a primary
398
/// (VRF based) and to a secondary (slot number based).
399
#[derive(Clone, Debug, PartialEq, Eq)]
400
pub enum BabePreDigestRef<'a> {
401
    /// A primary VRF-based slot assignment.
402
    Primary(BabePrimaryPreDigestRef<'a>),
403
    /// A secondary deterministic slot assignment.
404
    SecondaryPlain(BabeSecondaryPlainPreDigest),
405
    /// A secondary deterministic slot assignment with VRF outputs.
406
    SecondaryVRF(BabeSecondaryVRFPreDigestRef<'a>),
407
}
408
409
impl<'a> BabePreDigestRef<'a> {
410
    /// Decodes a [`BabePreDigestRef`] from a slice of bytes.
411
55
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
412
55
        Ok(match slice.first() {
413
29
            Some(1) => BabePreDigestRef::Primary(BabePrimaryPreDigestRef::from_slice(&slice[1..])
?0
),
414
25
            Some(2) => BabePreDigestRef::SecondaryPlain(BabeSecondaryPlainPreDigest::from_slice(
415
25
                &slice[1..],
416
25
            )
?0
),
417
1
            Some(3) => BabePreDigestRef::SecondaryVRF(BabeSecondaryVRFPreDigestRef::from_slice(
418
1
                &slice[1..],
419
1
            )
?0
),
420
0
            Some(_) => return Err(Error::BadBabePreDigestRefType),
421
0
            None => return Err(Error::TooShort),
422
        })
423
55
    }
_RNvMsf_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_16BabePreDigestRef10from_slice
Line
Count
Source
411
55
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
412
55
        Ok(match slice.first() {
413
29
            Some(1) => BabePreDigestRef::Primary(BabePrimaryPreDigestRef::from_slice(&slice[1..])
?0
),
414
25
            Some(2) => BabePreDigestRef::SecondaryPlain(BabeSecondaryPlainPreDigest::from_slice(
415
25
                &slice[1..],
416
25
            )
?0
),
417
1
            Some(3) => BabePreDigestRef::SecondaryVRF(BabeSecondaryVRFPreDigestRef::from_slice(
418
1
                &slice[1..],
419
1
            )
?0
),
420
0
            Some(_) => return Err(Error::BadBabePreDigestRefType),
421
0
            None => return Err(Error::TooShort),
422
        })
423
55
    }
Unexecuted instantiation: _RNvMsf_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_16BabePreDigestRef10from_slice
424
425
    /// Returns `true` for [`BabePreDigestRef::Primary`].
426
0
    pub fn is_primary(&self) -> bool {
427
0
        matches!(self, BabePreDigestRef::Primary(_))
428
0
    }
Unexecuted instantiation: _RNvMsf_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_16BabePreDigestRef10is_primary
Unexecuted instantiation: _RNvMsf_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_16BabePreDigestRef10is_primary
429
430
    /// Returns the slot number stored in the header.
431
2
    pub fn slot_number(&self) -> u64 {
432
2
        match self {
433
1
            BabePreDigestRef::Primary(digest) => digest.slot_number,
434
1
            BabePreDigestRef::SecondaryPlain(digest) => digest.slot_number,
435
0
            BabePreDigestRef::SecondaryVRF(digest) => digest.slot_number,
436
        }
437
2
    }
_RNvMsf_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_16BabePreDigestRef11slot_number
Line
Count
Source
431
2
    pub fn slot_number(&self) -> u64 {
432
2
        match self {
433
1
            BabePreDigestRef::Primary(digest) => digest.slot_number,
434
1
            BabePreDigestRef::SecondaryPlain(digest) => digest.slot_number,
435
0
            BabePreDigestRef::SecondaryVRF(digest) => digest.slot_number,
436
        }
437
2
    }
Unexecuted instantiation: _RNvMsf_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_16BabePreDigestRef11slot_number
438
439
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
440
    /// encoding of that object.
441
7
    pub fn scale_encoding(
442
7
        &self,
443
7
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + 'a> + Clone + 'a {
444
7
        let index = iter::once(match self {
445
3
            BabePreDigestRef::Primary(_) => [1],
446
4
            BabePreDigestRef::SecondaryPlain(_) => [2],
447
0
            BabePreDigestRef::SecondaryVRF(_) => [3],
448
        });
449
450
7
        let body = match self {
451
3
            BabePreDigestRef::Primary(digest) => either::Left(either::Left(
452
3
                digest
453
3
                    .scale_encoding()
454
12
                    .map(|buf| either::Left(either::Left(buf))),
_RNCNvMsf_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB7_16BabePreDigestRef14scale_encoding0Bb_
Line
Count
Source
454
12
                    .map(|buf| either::Left(either::Left(buf))),
Unexecuted instantiation: _RNCNvMsf_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB7_16BabePreDigestRef14scale_encoding0Bb_
455
3
            )),
456
4
            BabePreDigestRef::SecondaryPlain(digest) => either::Left(either::Right(
457
4
                digest
458
4
                    .scale_encoding()
459
8
                    .map(|buf| either::Left(either::Right(buf))),
_RNCNvMsf_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB7_16BabePreDigestRef14scale_encodings_0Bb_
Line
Count
Source
459
8
                    .map(|buf| either::Left(either::Right(buf))),
Unexecuted instantiation: _RNCNvMsf_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB7_16BabePreDigestRef14scale_encodings_0Bb_
460
4
            )),
461
0
            BabePreDigestRef::SecondaryVRF(digest) => {
462
0
                either::Right(digest.scale_encoding().map(either::Right))
463
            }
464
        };
465
466
7
        index.map(either::Left).chain(body.map(either::Right))
467
7
    }
_RNvMsf_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_16BabePreDigestRef14scale_encoding
Line
Count
Source
441
7
    pub fn scale_encoding(
442
7
        &self,
443
7
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + 'a> + Clone + 'a {
444
7
        let index = iter::once(match self {
445
3
            BabePreDigestRef::Primary(_) => [1],
446
4
            BabePreDigestRef::SecondaryPlain(_) => [2],
447
0
            BabePreDigestRef::SecondaryVRF(_) => [3],
448
        });
449
450
7
        let body = match self {
451
3
            BabePreDigestRef::Primary(digest) => either::Left(either::Left(
452
3
                digest
453
3
                    .scale_encoding()
454
3
                    .map(|buf| either::Left(either::Left(buf))),
455
3
            )),
456
4
            BabePreDigestRef::SecondaryPlain(digest) => either::Left(either::Right(
457
4
                digest
458
4
                    .scale_encoding()
459
4
                    .map(|buf| either::Left(either::Right(buf))),
460
4
            )),
461
0
            BabePreDigestRef::SecondaryVRF(digest) => {
462
0
                either::Right(digest.scale_encoding().map(either::Right))
463
            }
464
        };
465
466
7
        index.map(either::Left).chain(body.map(either::Right))
467
7
    }
Unexecuted instantiation: _RNvMsf_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_16BabePreDigestRef14scale_encoding
468
}
469
470
impl<'a> From<BabePreDigestRef<'a>> for BabePreDigest {
471
2
    fn from(a: BabePreDigestRef<'a>) -> Self {
472
2
        match a {
473
2
            BabePreDigestRef::Primary(a) => BabePreDigest::Primary(a.into()),
474
0
            BabePreDigestRef::SecondaryPlain(a) => BabePreDigest::SecondaryPlain(a),
475
0
            BabePreDigestRef::SecondaryVRF(a) => BabePreDigest::SecondaryVRF(a.into()),
476
        }
477
2
    }
_RNvXsg_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_13BabePreDigestINtNtCsaYZPK01V26L_4core7convert4FromNtB5_16BabePreDigestRefE4from
Line
Count
Source
471
2
    fn from(a: BabePreDigestRef<'a>) -> Self {
472
2
        match a {
473
2
            BabePreDigestRef::Primary(a) => BabePreDigest::Primary(a.into()),
474
0
            BabePreDigestRef::SecondaryPlain(a) => BabePreDigest::SecondaryPlain(a),
475
0
            BabePreDigestRef::SecondaryVRF(a) => BabePreDigest::SecondaryVRF(a.into()),
476
        }
477
2
    }
Unexecuted instantiation: _RNvXsg_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_13BabePreDigestINtNtCsaYZPK01V26L_4core7convert4FromNtB5_16BabePreDigestRefE4from
478
}
479
480
/// A BABE pre-runtime digest. This contains all data required to validate a
481
/// block and for the BABE runtime module. Slots can be assigned to a primary
482
/// (VRF based) and to a secondary (slot number based).
483
#[derive(Debug, Clone)]
484
pub enum BabePreDigest {
485
    /// A primary VRF-based slot assignment.
486
    Primary(BabePrimaryPreDigest),
487
    /// A secondary deterministic slot assignment.
488
    SecondaryPlain(BabeSecondaryPlainPreDigest),
489
    /// A secondary deterministic slot assignment with VRF outputs.
490
    SecondaryVRF(BabeSecondaryVRFPreDigest),
491
}
492
493
impl<'a> From<&'a BabePreDigest> for BabePreDigestRef<'a> {
494
0
    fn from(a: &'a BabePreDigest) -> Self {
495
0
        match a {
496
0
            BabePreDigest::Primary(a) => BabePreDigestRef::Primary(a.into()),
497
0
            BabePreDigest::SecondaryPlain(a) => BabePreDigestRef::SecondaryPlain(a.clone()),
498
0
            BabePreDigest::SecondaryVRF(a) => BabePreDigestRef::SecondaryVRF(a.into()),
499
        }
500
0
    }
Unexecuted instantiation: _RNvXsh_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_16BabePreDigestRefINtNtCsaYZPK01V26L_4core7convert4FromRNtB5_13BabePreDigestE4from
Unexecuted instantiation: _RNvXsh_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_16BabePreDigestRefINtNtCsaYZPK01V26L_4core7convert4FromRNtB5_13BabePreDigestE4from
501
}
502
503
/// Raw BABE primary slot assignment pre-digest.
504
#[derive(Debug, Clone)]
505
pub struct BabePrimaryPreDigestRef<'a> {
506
    /// Authority index
507
    pub authority_index: u32,
508
    /// Slot number
509
    pub slot_number: u64,
510
    /// VRF output
511
    pub vrf_output: &'a [u8; 32],
512
    /// VRF proof
513
    pub vrf_proof: &'a [u8; 64],
514
}
515
516
impl<'a> BabePrimaryPreDigestRef<'a> {
517
    /// Decodes a [`BabePrimaryPreDigestRef`] from a slice of bytes.
518
29
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
519
29
        if slice.len() != 4 + 8 + 32 + 64 {
520
0
            return Err(Error::TooShort);
521
29
        }
522
29
523
29
        Ok(BabePrimaryPreDigestRef {
524
29
            authority_index: u32::from_le_bytes(<[u8; 4]>::try_from(&slice[0..4]).unwrap()),
525
29
            slot_number: u64::from_le_bytes(<[u8; 8]>::try_from(&slice[4..12]).unwrap()),
526
29
            vrf_output: TryFrom::try_from(&slice[12..44]).unwrap(),
527
29
            vrf_proof: TryFrom::try_from(&slice[44..108]).unwrap(),
528
29
        })
529
29
    }
_RNvMsi_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_23BabePrimaryPreDigestRef10from_slice
Line
Count
Source
518
29
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
519
29
        if slice.len() != 4 + 8 + 32 + 64 {
520
0
            return Err(Error::TooShort);
521
29
        }
522
29
523
29
        Ok(BabePrimaryPreDigestRef {
524
29
            authority_index: u32::from_le_bytes(<[u8; 4]>::try_from(&slice[0..4]).unwrap()),
525
29
            slot_number: u64::from_le_bytes(<[u8; 8]>::try_from(&slice[4..12]).unwrap()),
526
29
            vrf_output: TryFrom::try_from(&slice[12..44]).unwrap(),
527
29
            vrf_proof: TryFrom::try_from(&slice[44..108]).unwrap(),
528
29
        })
529
29
    }
Unexecuted instantiation: _RNvMsi_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_23BabePrimaryPreDigestRef10from_slice
530
531
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
532
    /// encoding of that object.
533
3
    pub fn scale_encoding(
534
3
        &self,
535
3
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + 'a> + Clone + 'a {
536
3
        let header = iter::once(either::Left(self.authority_index.to_le_bytes()))
537
3
            .chain(iter::once(either::Right(self.slot_number.to_le_bytes())))
538
3
            .map(either::Left);
539
3
540
3
        header
541
3
            .chain(iter::once(either::Right(&self.vrf_output[..])))
542
3
            .chain(iter::once(either::Right(&self.vrf_proof[..])))
543
3
    }
_RNvMsi_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_23BabePrimaryPreDigestRef14scale_encoding
Line
Count
Source
533
3
    pub fn scale_encoding(
534
3
        &self,
535
3
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + 'a> + Clone + 'a {
536
3
        let header = iter::once(either::Left(self.authority_index.to_le_bytes()))
537
3
            .chain(iter::once(either::Right(self.slot_number.to_le_bytes())))
538
3
            .map(either::Left);
539
3
540
3
        header
541
3
            .chain(iter::once(either::Right(&self.vrf_output[..])))
542
3
            .chain(iter::once(either::Right(&self.vrf_proof[..])))
543
3
    }
Unexecuted instantiation: _RNvMsi_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_23BabePrimaryPreDigestRef14scale_encoding
544
}
545
546
impl<'a> cmp::PartialEq<BabePrimaryPreDigestRef<'a>> for BabePrimaryPreDigestRef<'a> {
547
0
    fn eq(&self, other: &BabePrimaryPreDigestRef<'a>) -> bool {
548
0
        self.authority_index == other.authority_index
549
0
            && self.slot_number == other.slot_number
550
0
            && self.vrf_output[..] == other.vrf_output[..]
551
0
            && self.vrf_proof[..] == other.vrf_proof[..]
552
0
    }
Unexecuted instantiation: _RNvXsj_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_23BabePrimaryPreDigestRefNtNtCsaYZPK01V26L_4core3cmp9PartialEq2eq
Unexecuted instantiation: _RNvXsj_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_23BabePrimaryPreDigestRefNtNtCsaYZPK01V26L_4core3cmp9PartialEq2eq
553
}
554
555
impl<'a> cmp::Eq for BabePrimaryPreDigestRef<'a> {}
556
557
impl<'a> From<&'a BabePrimaryPreDigest> for BabePrimaryPreDigestRef<'a> {
558
0
    fn from(a: &'a BabePrimaryPreDigest) -> Self {
559
0
        BabePrimaryPreDigestRef {
560
0
            authority_index: a.authority_index,
561
0
            slot_number: a.slot_number,
562
0
            vrf_output: &a.vrf_output,
563
0
            vrf_proof: &a.vrf_proof,
564
0
        }
565
0
    }
Unexecuted instantiation: _RNvXsl_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_23BabePrimaryPreDigestRefINtNtCsaYZPK01V26L_4core7convert4FromRNtB5_20BabePrimaryPreDigestE4from
Unexecuted instantiation: _RNvXsl_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_23BabePrimaryPreDigestRefINtNtCsaYZPK01V26L_4core7convert4FromRNtB5_20BabePrimaryPreDigestE4from
566
}
567
568
/// Raw BABE primary slot assignment pre-digest.
569
#[derive(Debug, Clone)]
570
pub struct BabePrimaryPreDigest {
571
    /// Authority index
572
    pub authority_index: u32,
573
    /// Slot number
574
    pub slot_number: u64,
575
    /// VRF output
576
    pub vrf_output: [u8; 32],
577
    /// VRF proof
578
    pub vrf_proof: [u8; 64],
579
}
580
581
impl<'a> From<BabePrimaryPreDigestRef<'a>> for BabePrimaryPreDigest {
582
2
    fn from(a: BabePrimaryPreDigestRef<'a>) -> Self {
583
2
        BabePrimaryPreDigest {
584
2
            authority_index: a.authority_index,
585
2
            slot_number: a.slot_number,
586
2
            vrf_output: *a.vrf_output,
587
2
            vrf_proof: *a.vrf_proof,
588
2
        }
589
2
    }
_RNvXsm_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_20BabePrimaryPreDigestINtNtCsaYZPK01V26L_4core7convert4FromNtB5_23BabePrimaryPreDigestRefE4from
Line
Count
Source
582
2
    fn from(a: BabePrimaryPreDigestRef<'a>) -> Self {
583
2
        BabePrimaryPreDigest {
584
2
            authority_index: a.authority_index,
585
2
            slot_number: a.slot_number,
586
2
            vrf_output: *a.vrf_output,
587
2
            vrf_proof: *a.vrf_proof,
588
2
        }
589
2
    }
Unexecuted instantiation: _RNvXsm_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_20BabePrimaryPreDigestINtNtCsaYZPK01V26L_4core7convert4FromNtB5_23BabePrimaryPreDigestRefE4from
590
}
591
592
/// BABE secondary slot assignment pre-digest.
593
#[derive(Clone, Debug, PartialEq, Eq)]
594
pub struct BabeSecondaryPlainPreDigest {
595
    /// Authority index
596
    ///
597
    /// This is not strictly-speaking necessary, since the secondary slots
598
    /// are assigned based on slot number and epoch randomness. But including
599
    /// it makes things easier for higher-level users of the chain data to
600
    /// be aware of the author of a secondary-slot block.
601
    pub authority_index: u32,
602
603
    /// Slot number
604
    pub slot_number: u64,
605
}
606
607
impl BabeSecondaryPlainPreDigest {
608
    /// Decodes a [`BabeSecondaryPlainPreDigest`] from a slice of bytes.
609
25
    pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
610
25
        if slice.len() != 12 {
611
0
            return Err(Error::DigestItemDecodeError);
612
25
        }
613
25
614
25
        let authority_index = u32::from_le_bytes(<[u8; 4]>::try_from(&slice[..4]).unwrap());
615
25
        let slot_number = u64::from_le_bytes(<[u8; 8]>::try_from(&slice[4..]).unwrap());
616
25
617
25
        Ok(BabeSecondaryPlainPreDigest {
618
25
            authority_index,
619
25
            slot_number,
620
25
        })
621
25
    }
_RNvMsn_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_27BabeSecondaryPlainPreDigest10from_slice
Line
Count
Source
609
25
    pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
610
25
        if slice.len() != 12 {
611
0
            return Err(Error::DigestItemDecodeError);
612
25
        }
613
25
614
25
        let authority_index = u32::from_le_bytes(<[u8; 4]>::try_from(&slice[..4]).unwrap());
615
25
        let slot_number = u64::from_le_bytes(<[u8; 8]>::try_from(&slice[4..]).unwrap());
616
25
617
25
        Ok(BabeSecondaryPlainPreDigest {
618
25
            authority_index,
619
25
            slot_number,
620
25
        })
621
25
    }
Unexecuted instantiation: _RNvMsn_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_27BabeSecondaryPlainPreDigest10from_slice
622
623
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
624
    /// encoding of that object.
625
4
    pub fn scale_encoding(&self) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
626
4
        iter::once(either::Left(self.authority_index.to_le_bytes()))
627
4
            .chain(iter::once(either::Right(self.slot_number.to_le_bytes())))
628
4
    }
_RNvMsn_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_27BabeSecondaryPlainPreDigest14scale_encoding
Line
Count
Source
625
4
    pub fn scale_encoding(&self) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
626
4
        iter::once(either::Left(self.authority_index.to_le_bytes()))
627
4
            .chain(iter::once(either::Right(self.slot_number.to_le_bytes())))
628
4
    }
Unexecuted instantiation: _RNvMsn_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_27BabeSecondaryPlainPreDigest14scale_encoding
629
}
630
631
/// BABE secondary deterministic slot assignment with VRF outputs.
632
#[derive(Debug, Clone)]
633
pub struct BabeSecondaryVRFPreDigestRef<'a> {
634
    /// Authority index
635
    pub authority_index: u32,
636
    /// Slot number
637
    pub slot_number: u64,
638
    /// VRF output
639
    pub vrf_output: &'a [u8; 32],
640
    /// VRF proof
641
    pub vrf_proof: &'a [u8; 64],
642
}
643
644
impl<'a> BabeSecondaryVRFPreDigestRef<'a> {
645
    /// Decodes a [`BabeSecondaryVRFPreDigestRef`] from a slice of bytes.
646
1
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
647
1
        if slice.len() != 4 + 8 + 32 + 64 {
648
0
            return Err(Error::TooShort);
649
1
        }
650
1
651
1
        Ok(BabeSecondaryVRFPreDigestRef {
652
1
            authority_index: u32::from_le_bytes(<[u8; 4]>::try_from(&slice[0..4]).unwrap()),
653
1
            slot_number: u64::from_le_bytes(<[u8; 8]>::try_from(&slice[4..12]).unwrap()),
654
1
            vrf_output: TryFrom::try_from(&slice[12..44]).unwrap(),
655
1
            vrf_proof: TryFrom::try_from(&slice[44..108]).unwrap(),
656
1
        })
657
1
    }
_RNvMso_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_28BabeSecondaryVRFPreDigestRef10from_slice
Line
Count
Source
646
1
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
647
1
        if slice.len() != 4 + 8 + 32 + 64 {
648
0
            return Err(Error::TooShort);
649
1
        }
650
1
651
1
        Ok(BabeSecondaryVRFPreDigestRef {
652
1
            authority_index: u32::from_le_bytes(<[u8; 4]>::try_from(&slice[0..4]).unwrap()),
653
1
            slot_number: u64::from_le_bytes(<[u8; 8]>::try_from(&slice[4..12]).unwrap()),
654
1
            vrf_output: TryFrom::try_from(&slice[12..44]).unwrap(),
655
1
            vrf_proof: TryFrom::try_from(&slice[44..108]).unwrap(),
656
1
        })
657
1
    }
Unexecuted instantiation: _RNvMso_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_28BabeSecondaryVRFPreDigestRef10from_slice
658
659
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
660
    /// encoding of that object.
661
0
    pub fn scale_encoding(
662
0
        &self,
663
0
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + 'a> + Clone + 'a {
664
0
        let header = iter::once(either::Left(self.authority_index.to_le_bytes()))
665
0
            .chain(iter::once(either::Right(self.slot_number.to_le_bytes())))
666
0
            .map(either::Left);
667
0
668
0
        header
669
0
            .chain(iter::once(either::Right(&self.vrf_output[..])))
670
0
            .chain(iter::once(either::Right(&self.vrf_proof[..])))
671
0
    }
Unexecuted instantiation: _RNvMso_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_28BabeSecondaryVRFPreDigestRef14scale_encoding
Unexecuted instantiation: _RNvMso_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_28BabeSecondaryVRFPreDigestRef14scale_encoding
672
}
673
674
impl<'a> cmp::PartialEq<BabeSecondaryVRFPreDigestRef<'a>> for BabeSecondaryVRFPreDigestRef<'a> {
675
0
    fn eq(&self, other: &BabeSecondaryVRFPreDigestRef<'a>) -> bool {
676
0
        self.authority_index == other.authority_index
677
0
            && self.slot_number == other.slot_number
678
0
            && self.vrf_output[..] == other.vrf_output[..]
679
0
            && self.vrf_proof[..] == other.vrf_proof[..]
680
0
    }
Unexecuted instantiation: _RNvXsp_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_28BabeSecondaryVRFPreDigestRefNtNtCsaYZPK01V26L_4core3cmp9PartialEq2eq
Unexecuted instantiation: _RNvXsp_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_28BabeSecondaryVRFPreDigestRefNtNtCsaYZPK01V26L_4core3cmp9PartialEq2eq
681
}
682
683
impl<'a> cmp::Eq for BabeSecondaryVRFPreDigestRef<'a> {}
684
685
impl<'a> From<&'a BabeSecondaryVRFPreDigest> for BabeSecondaryVRFPreDigestRef<'a> {
686
0
    fn from(a: &'a BabeSecondaryVRFPreDigest) -> Self {
687
0
        BabeSecondaryVRFPreDigestRef {
688
0
            authority_index: a.authority_index,
689
0
            slot_number: a.slot_number,
690
0
            vrf_output: &a.vrf_output,
691
0
            vrf_proof: &a.vrf_proof,
692
0
        }
693
0
    }
Unexecuted instantiation: _RNvXsr_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_28BabeSecondaryVRFPreDigestRefINtNtCsaYZPK01V26L_4core7convert4FromRNtB5_25BabeSecondaryVRFPreDigestE4from
Unexecuted instantiation: _RNvXsr_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_28BabeSecondaryVRFPreDigestRefINtNtCsaYZPK01V26L_4core7convert4FromRNtB5_25BabeSecondaryVRFPreDigestE4from
694
}
695
696
/// BABE secondary deterministic slot assignment with VRF outputs.
697
#[derive(Debug, Clone)]
698
pub struct BabeSecondaryVRFPreDigest {
699
    /// Authority index
700
    pub authority_index: u32,
701
    /// Slot number
702
    pub slot_number: u64,
703
    /// VRF output
704
    pub vrf_output: [u8; 32],
705
    /// VRF proof
706
    pub vrf_proof: [u8; 64],
707
}
708
709
impl<'a> From<BabeSecondaryVRFPreDigestRef<'a>> for BabeSecondaryVRFPreDigest {
710
0
    fn from(a: BabeSecondaryVRFPreDigestRef<'a>) -> Self {
711
0
        BabeSecondaryVRFPreDigest {
712
0
            authority_index: a.authority_index,
713
0
            slot_number: a.slot_number,
714
0
            vrf_output: *a.vrf_output,
715
0
            vrf_proof: *a.vrf_proof,
716
0
        }
717
0
    }
Unexecuted instantiation: _RNvXss_NtNtCsN16ciHI6Qf_7smoldot6header4babeNtB5_25BabeSecondaryVRFPreDigestINtNtCsaYZPK01V26L_4core7convert4FromNtB5_28BabeSecondaryVRFPreDigestRefE4from
Unexecuted instantiation: _RNvXss_NtNtCseuYC0Zibziv_7smoldot6header4babeNtB5_25BabeSecondaryVRFPreDigestINtNtCsaYZPK01V26L_4core7convert4FromNtB5_28BabeSecondaryVRFPreDigestRefE4from
718
}