Coverage Report

Created: 2025-07-01 09:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/__w/smoldot/smoldot/repo/lib/src/header/babe.rs
Line
Count
Source
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::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
    }
_RNvMNtNtCsjlkOsLH0Zfj_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: _RNvMNtNtCsc1ywvx6YAnK_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(&self) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
67
17
        let index = iter::once(match self {
68
5
            BabeConsensusLogRef::NextEpochData(_) => [1],
69
11
            BabeConsensusLogRef::OnDisabled(_) => [2],
70
1
            BabeConsensusLogRef::NextConfigData(_) => [3],
71
        });
72
73
17
        let body = match self {
74
5
            BabeConsensusLogRef::NextEpochData(digest) => either::Left(either::Left(
75
5
                digest.scale_encoding().map(either::Left).map(either::Left),
76
5
            )),
77
11
            BabeConsensusLogRef::OnDisabled(digest) => either::Left(either::Right(iter::once(
78
11
                either::Left(either::Right(digest.to_le_bytes())),
79
11
            ))),
80
1
            BabeConsensusLogRef::NextConfigData(digest) => either::Right(
81
1
                iter::once(either::Right(either::Left([1]))).chain(
82
1
                    digest
83
1
                        .scale_encoding()
84
1
                        .map(either::Right)
85
1
                        .map(either::Right),
86
1
                ),
87
1
            ),
88
        };
89
90
17
        index.map(either::Left).chain(body.map(either::Right))
91
17
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB2_19BabeConsensusLogRef14scale_encoding
Line
Count
Source
66
17
    pub fn scale_encoding(&self) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
67
17
        let index = iter::once(match self {
68
5
            BabeConsensusLogRef::NextEpochData(_) => [1],
69
11
            BabeConsensusLogRef::OnDisabled(_) => [2],
70
1
            BabeConsensusLogRef::NextConfigData(_) => [3],
71
        });
72
73
17
        let body = match self {
74
5
            BabeConsensusLogRef::NextEpochData(digest) => either::Left(either::Left(
75
5
                digest.scale_encoding().map(either::Left).map(either::Left),
76
5
            )),
77
11
            BabeConsensusLogRef::OnDisabled(digest) => either::Left(either::Right(iter::once(
78
11
                either::Left(either::Right(digest.to_le_bytes())),
79
11
            ))),
80
1
            BabeConsensusLogRef::NextConfigData(digest) => either::Right(
81
1
                iter::once(either::Right(either::Left([1]))).chain(
82
1
                    digest
83
1
                        .scale_encoding()
84
1
                        .map(either::Right)
85
1
                        .map(either::Right),
86
1
                ),
87
1
            ),
88
        };
89
90
17
        index.map(either::Left).chain(body.map(either::Right))
91
17
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB2_19BabeConsensusLogRef14scale_encoding
92
}
93
94
impl<'a> From<&'a BabeConsensusLog> for BabeConsensusLogRef<'a> {
95
0
    fn from(a: &'a BabeConsensusLog) -> Self {
96
0
        match a {
97
0
            BabeConsensusLog::NextEpochData(v) => BabeConsensusLogRef::NextEpochData(v.into()),
98
0
            BabeConsensusLog::OnDisabled(v) => BabeConsensusLogRef::OnDisabled(*v),
99
0
            BabeConsensusLog::NextConfigData(v) => BabeConsensusLogRef::NextConfigData(*v),
100
        }
101
0
    }
Unexecuted instantiation: _RNvXs_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB4_19BabeConsensusLogRefINtNtCs1p5UDGgVI4d_4core7convert4FromRNtB4_16BabeConsensusLogE4from
Unexecuted instantiation: _RNvXs_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB4_19BabeConsensusLogRefINtNtCs1p5UDGgVI4d_4core7convert4FromRNtB4_16BabeConsensusLogE4from
102
}
103
104
/// A consensus log item for BABE.
105
#[derive(Debug, Clone, PartialEq, Eq)]
106
pub enum BabeConsensusLog {
107
    /// The epoch has changed. This provides information about the _next_
108
    /// epoch - information about the _current_ epoch (i.e. the one we've just
109
    /// entered) should already be available earlier in the chain.
110
    NextEpochData(BabeNextEpoch),
111
    /// Disable the authority with given index.
112
    OnDisabled(u32),
113
    /// The epoch has changed, and the epoch after the current one will
114
    /// enact different epoch configurations.
115
    NextConfigData(BabeNextConfig),
116
}
117
118
impl<'a> From<BabeConsensusLogRef<'a>> for BabeConsensusLog {
119
0
    fn from(a: BabeConsensusLogRef<'a>) -> Self {
120
0
        match a {
121
0
            BabeConsensusLogRef::NextEpochData(v) => BabeConsensusLog::NextEpochData(v.into()),
122
0
            BabeConsensusLogRef::OnDisabled(v) => BabeConsensusLog::OnDisabled(v),
123
0
            BabeConsensusLogRef::NextConfigData(v) => BabeConsensusLog::NextConfigData(v),
124
        }
125
0
    }
Unexecuted instantiation: _RNvXs0_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_16BabeConsensusLogINtNtCs1p5UDGgVI4d_4core7convert4FromNtB5_19BabeConsensusLogRefE4from
Unexecuted instantiation: _RNvXs0_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_16BabeConsensusLogINtNtCs1p5UDGgVI4d_4core7convert4FromNtB5_19BabeConsensusLogRefE4from
126
}
127
128
/// Information about the next epoch. This is broadcast in the first block
129
/// of the epoch.
130
#[derive(Debug, Clone, PartialEq, Eq)]
131
pub struct BabeNextEpochRef<'a> {
132
    /// The authorities.
133
    pub authorities: BabeAuthoritiesIter<'a>,
134
135
    /// The value of randomness to use for the slot-assignment.
136
    pub randomness: &'a [u8; 32],
137
}
138
139
impl<'a> BabeNextEpochRef<'a> {
140
    /// Decodes a [`BabePreDigestRef`] from a slice of bytes.
141
27
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
142
27
        let (slice, authorities_len) =
143
27
            match crate::util::nom_scale_compact_usize::<nom::error::Error<&[u8]>>(slice) {
144
27
                Ok(s) => s,
145
0
                Err(_) => return Err(Error::TooShort),
146
            };
147
148
27
        if slice.len() != authorities_len * 40 + 32 {
149
0
            return Err(Error::TooShort);
150
27
        }
151
152
27
        Ok(BabeNextEpochRef {
153
27
            authorities: BabeAuthoritiesIter(BabeAuthoritiesIterInner::Raw(
154
27
                slice[0..authorities_len * 40].chunks(40),
155
27
            )),
156
27
            randomness: <&[u8; 32]>::try_from(&slice[authorities_len * 40..]).unwrap(),
157
27
        })
158
27
    }
_RNvMs1_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_16BabeNextEpochRef10from_slice
Line
Count
Source
141
27
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
142
27
        let (slice, authorities_len) =
143
27
            match crate::util::nom_scale_compact_usize::<nom::error::Error<&[u8]>>(slice) {
144
27
                Ok(s) => s,
145
0
                Err(_) => return Err(Error::TooShort),
146
            };
147
148
27
        if slice.len() != authorities_len * 40 + 32 {
149
0
            return Err(Error::TooShort);
150
27
        }
151
152
27
        Ok(BabeNextEpochRef {
153
27
            authorities: BabeAuthoritiesIter(BabeAuthoritiesIterInner::Raw(
154
27
                slice[0..authorities_len * 40].chunks(40),
155
27
            )),
156
27
            randomness: <&[u8; 32]>::try_from(&slice[authorities_len * 40..]).unwrap(),
157
27
        })
158
27
    }
Unexecuted instantiation: _RNvMs1_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_16BabeNextEpochRef10from_slice
159
160
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
161
    /// encoding of that object.
162
5
    pub fn scale_encoding(
163
5
        &self,
164
5
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + use<'a>> + Clone + use<'a> {
165
5
        let header = util::encode_scale_compact_usize(self.authorities.len());
166
5
        iter::once(either::Left(header))
167
5
            .chain(
168
5
                self.authorities
169
5
                    .clone()
170
924
                    .
flat_map5
(|a| a.scale_encoding())
_RNCNvMs1_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB7_16BabeNextEpochRef14scale_encoding0Bb_
Line
Count
Source
170
924
                    .flat_map(|a| a.scale_encoding())
Unexecuted instantiation: _RNCNvMs1_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB7_16BabeNextEpochRef14scale_encoding0Bb_
171
1.84k
                    .
map5
(|buf| either::Right(either::Left(buf))),
_RNCNvMs1_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB7_16BabeNextEpochRef14scale_encodings_0Bb_
Line
Count
Source
171
1.84k
                    .map(|buf| either::Right(either::Left(buf))),
Unexecuted instantiation: _RNCNvMs1_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB7_16BabeNextEpochRef14scale_encodings_0Bb_
172
            )
173
5
            .chain(iter::once(either::Right(either::Right(
174
5
                &self.randomness[..],
175
5
            ))))
176
5
    }
_RNvMs1_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_16BabeNextEpochRef14scale_encoding
Line
Count
Source
162
5
    pub fn scale_encoding(
163
5
        &self,
164
5
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + use<'a>> + Clone + use<'a> {
165
5
        let header = util::encode_scale_compact_usize(self.authorities.len());
166
5
        iter::once(either::Left(header))
167
5
            .chain(
168
5
                self.authorities
169
5
                    .clone()
170
5
                    .flat_map(|a| a.scale_encoding())
171
5
                    .map(|buf| either::Right(either::Left(buf))),
172
            )
173
5
            .chain(iter::once(either::Right(either::Right(
174
5
                &self.randomness[..],
175
5
            ))))
176
5
    }
Unexecuted instantiation: _RNvMs1_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_16BabeNextEpochRef14scale_encoding
177
}
178
179
impl<'a> From<&'a BabeNextEpoch> for BabeNextEpochRef<'a> {
180
0
    fn from(a: &'a BabeNextEpoch) -> Self {
181
0
        BabeNextEpochRef {
182
0
            authorities: BabeAuthoritiesIter(BabeAuthoritiesIterInner::List(a.authorities.iter())),
183
0
            randomness: &a.randomness,
184
0
        }
185
0
    }
Unexecuted instantiation: _RNvXs2_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_16BabeNextEpochRefINtNtCs1p5UDGgVI4d_4core7convert4FromRNtB5_13BabeNextEpochE4from
Unexecuted instantiation: _RNvXs2_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_16BabeNextEpochRefINtNtCs1p5UDGgVI4d_4core7convert4FromRNtB5_13BabeNextEpochE4from
186
}
187
188
/// Information about the next epoch. This is broadcast in the first block
189
/// of the epoch.
190
#[derive(Debug, Clone, PartialEq, Eq)]
191
pub struct BabeNextEpoch {
192
    /// The authorities.
193
    pub authorities: Vec<BabeAuthority>,
194
195
    /// The value of randomness to use for the slot-assignment.
196
    pub randomness: [u8; 32],
197
}
198
199
impl<'a> From<BabeNextEpochRef<'a>> for BabeNextEpoch {
200
0
    fn from(a: BabeNextEpochRef<'a>) -> Self {
201
0
        BabeNextEpoch {
202
0
            authorities: a.authorities.map(Into::into).collect(),
203
0
            randomness: *a.randomness,
204
0
        }
205
0
    }
Unexecuted instantiation: _RNvXs3_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_13BabeNextEpochINtNtCs1p5UDGgVI4d_4core7convert4FromNtB5_16BabeNextEpochRefE4from
Unexecuted instantiation: _RNvXs3_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_13BabeNextEpochINtNtCs1p5UDGgVI4d_4core7convert4FromNtB5_16BabeNextEpochRefE4from
206
}
207
208
/// List of authorities in a BABE context.
209
#[derive(Clone)]
210
pub struct BabeAuthoritiesIter<'a>(BabeAuthoritiesIterInner<'a>);
211
212
#[derive(Clone)]
213
enum BabeAuthoritiesIterInner<'a> {
214
    List(slice::Iter<'a, BabeAuthority>),
215
    Raw(slice::Chunks<'a, u8>),
216
}
217
218
impl<'a> BabeAuthoritiesIter<'a> {
219
    /// Builds a new [`BabeAuthoritiesIter`] iterating over the given slice.
220
12
    pub fn from_slice(slice: &'a [BabeAuthority]) -> Self {
221
12
        Self(BabeAuthoritiesIterInner::List(slice.iter()))
222
12
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_19BabeAuthoritiesIter10from_slice
Line
Count
Source
220
12
    pub fn from_slice(slice: &'a [BabeAuthority]) -> Self {
221
12
        Self(BabeAuthoritiesIterInner::List(slice.iter()))
222
12
    }
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_19BabeAuthoritiesIter10from_slice
223
}
224
225
impl<'a> Iterator for BabeAuthoritiesIter<'a> {
226
    type Item = BabeAuthorityRef<'a>;
227
228
966
    fn next(&mut self) -> Option<Self::Item> {
229
966
        match &mut self.0 {
230
23
            BabeAuthoritiesIterInner::List(l) => l.next().map(Into::into),
231
943
            BabeAuthoritiesIterInner::Raw(l) => {
232
943
                let 
item936
= l.next()
?7
;
233
936
                assert_eq!(item.len(), 40);
234
936
                Some(BabeAuthorityRef {
235
936
                    public_key: <&[u8; 32]>::try_from(&item[0..32]).unwrap(),
236
936
                    weight: u64::from_le_bytes(<[u8; 8]>::try_from(&item[32..40]).unwrap()),
237
936
                })
238
            }
239
        }
240
966
    }
_RNvXs5_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_19BabeAuthoritiesIterNtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4next
Line
Count
Source
228
966
    fn next(&mut self) -> Option<Self::Item> {
229
966
        match &mut self.0 {
230
23
            BabeAuthoritiesIterInner::List(l) => l.next().map(Into::into),
231
943
            BabeAuthoritiesIterInner::Raw(l) => {
232
943
                let 
item936
= l.next()
?7
;
233
936
                assert_eq!(item.len(), 40);
234
936
                Some(BabeAuthorityRef {
235
936
                    public_key: <&[u8; 32]>::try_from(&item[0..32]).unwrap(),
236
936
                    weight: u64::from_le_bytes(<[u8; 8]>::try_from(&item[32..40]).unwrap()),
237
936
                })
238
            }
239
        }
240
966
    }
Unexecuted instantiation: _RNvXs5_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_19BabeAuthoritiesIterNtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4next
241
242
11
    fn size_hint(&self) -> (usize, Option<usize>) {
243
11
        match &self.0 {
244
4
            BabeAuthoritiesIterInner::List(l) => l.size_hint(),
245
7
            BabeAuthoritiesIterInner::Raw(l) => l.size_hint(),
246
        }
247
11
    }
_RNvXs5_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_19BabeAuthoritiesIterNtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hint
Line
Count
Source
242
11
    fn size_hint(&self) -> (usize, Option<usize>) {
243
11
        match &self.0 {
244
4
            BabeAuthoritiesIterInner::List(l) => l.size_hint(),
245
7
            BabeAuthoritiesIterInner::Raw(l) => l.size_hint(),
246
        }
247
11
    }
Unexecuted instantiation: _RNvXs5_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_19BabeAuthoritiesIterNtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hint
248
}
249
250
impl<'a> ExactSizeIterator for BabeAuthoritiesIter<'a> {}
251
252
impl<'a> cmp::PartialEq<BabeAuthoritiesIter<'a>> for BabeAuthoritiesIter<'a> {
253
0
    fn eq(&self, other: &BabeAuthoritiesIter<'a>) -> bool {
254
0
        let mut a = self.clone();
255
0
        let mut b = other.clone();
256
        loop {
257
0
            match (a.next(), b.next()) {
258
0
                (Some(a), Some(b)) if a == b => {}
259
0
                (None, None) => return true,
260
0
                _ => return false,
261
            }
262
        }
263
0
    }
Unexecuted instantiation: _RNvXs7_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_19BabeAuthoritiesIterNtNtCs1p5UDGgVI4d_4core3cmp9PartialEq2eq
Unexecuted instantiation: _RNvXs7_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_19BabeAuthoritiesIterNtNtCs1p5UDGgVI4d_4core3cmp9PartialEq2eq
264
}
265
266
impl<'a> cmp::Eq for BabeAuthoritiesIter<'a> {}
267
268
impl<'a> fmt::Debug for BabeAuthoritiesIter<'a> {
269
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
270
0
        f.debug_list().entries(self.clone()).finish()
271
0
    }
Unexecuted instantiation: _RNvXs9_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_19BabeAuthoritiesIterNtNtCs1p5UDGgVI4d_4core3fmt5Debug3fmt
Unexecuted instantiation: _RNvXs9_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_19BabeAuthoritiesIterNtNtCs1p5UDGgVI4d_4core3fmt5Debug3fmt
272
}
273
274
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
275
pub struct BabeAuthorityRef<'a> {
276
    /// Sr25519 public key.
277
    pub public_key: &'a [u8; 32],
278
    /// Arbitrary number indicating the weight of the authority.
279
    ///
280
    /// This value can only be compared to other weight values.
281
    // TODO: should be NonZero<u64>; requires deep changes in decoding code though
282
    pub weight: u64,
283
}
284
285
impl<'a> BabeAuthorityRef<'a> {
286
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
287
    /// encoding of that object.
288
924
    pub fn scale_encoding(
289
924
        &self,
290
924
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + use<'a>> + Clone + use<'a> {
291
924
        iter::once(either::Right(self.public_key))
292
924
            .chain(iter::once(either::Left(self.weight.to_le_bytes())))
293
924
    }
_RNvMsa_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_16BabeAuthorityRef14scale_encoding
Line
Count
Source
288
924
    pub fn scale_encoding(
289
924
        &self,
290
924
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + use<'a>> + Clone + use<'a> {
291
924
        iter::once(either::Right(self.public_key))
292
924
            .chain(iter::once(either::Left(self.weight.to_le_bytes())))
293
924
    }
Unexecuted instantiation: _RNvMsa_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_16BabeAuthorityRef14scale_encoding
294
}
295
296
impl<'a> From<&'a BabeAuthority> for BabeAuthorityRef<'a> {
297
21
    fn from(a: &'a BabeAuthority) -> Self {
298
21
        BabeAuthorityRef {
299
21
            public_key: &a.public_key,
300
21
            weight: a.weight,
301
21
        }
302
21
    }
_RNvXsb_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_16BabeAuthorityRefINtNtCs1p5UDGgVI4d_4core7convert4FromRNtB5_13BabeAuthorityE4from
Line
Count
Source
297
21
    fn from(a: &'a BabeAuthority) -> Self {
298
21
        BabeAuthorityRef {
299
21
            public_key: &a.public_key,
300
21
            weight: a.weight,
301
21
        }
302
21
    }
Unexecuted instantiation: _RNvXsb_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_16BabeAuthorityRefINtNtCs1p5UDGgVI4d_4core7convert4FromRNtB5_13BabeAuthorityE4from
303
}
304
305
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
306
pub struct BabeAuthority {
307
    /// Sr25519 public key.
308
    pub public_key: [u8; 32],
309
    /// Arbitrary number indicating the weight of the authority.
310
    ///
311
    /// This value can only be compared to other weight values.
312
    // TODO: should be NonZero<u64>; requires deep changes in decoding code though
313
    pub weight: u64,
314
}
315
316
impl<'a> From<BabeAuthorityRef<'a>> for BabeAuthority {
317
12
    fn from(a: BabeAuthorityRef<'a>) -> Self {
318
12
        BabeAuthority {
319
12
            public_key: *a.public_key,
320
12
            weight: a.weight,
321
12
        }
322
12
    }
_RNvXsc_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_13BabeAuthorityINtNtCs1p5UDGgVI4d_4core7convert4FromNtB5_16BabeAuthorityRefE4from
Line
Count
Source
317
12
    fn from(a: BabeAuthorityRef<'a>) -> Self {
318
12
        BabeAuthority {
319
12
            public_key: *a.public_key,
320
12
            weight: a.weight,
321
12
        }
322
12
    }
Unexecuted instantiation: _RNvXsc_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_13BabeAuthorityINtNtCs1p5UDGgVI4d_4core7convert4FromNtB5_16BabeAuthorityRefE4from
323
}
324
325
/// Information about the next epoch config, if changed. This is broadcast in the first
326
/// block of the epoch, and applies using the same rules as `NextEpochDescriptor`.
327
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
328
pub struct BabeNextConfig {
329
    /// Value of `c` in `BabeEpochConfiguration`.
330
    pub c: (u64, u64),
331
    /// Value of `allowed_slots` in `BabeEpochConfiguration`.
332
    pub allowed_slots: BabeAllowedSlots,
333
}
334
335
impl BabeNextConfig {
336
    /// Decodes a [`BabeNextConfig`] from a slice of bytes.
337
6
    pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
338
6
        if slice.len() < 16 {
339
0
            return Err(Error::TooShort);
340
6
        }
341
342
6
        let c0 = u64::from_le_bytes(<[u8; 8]>::try_from(&slice[..8]).unwrap());
343
6
        let c1 = u64::from_le_bytes(<[u8; 8]>::try_from(&slice[8..16]).unwrap());
344
6
        let allowed_slots = BabeAllowedSlots::from_slice(&slice[16..])
?0
;
345
346
6
        Ok(BabeNextConfig {
347
6
            c: (c0, c1),
348
6
            allowed_slots,
349
6
        })
350
6
    }
_RNvMsd_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_14BabeNextConfig10from_slice
Line
Count
Source
337
6
    pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
338
6
        if slice.len() < 16 {
339
0
            return Err(Error::TooShort);
340
6
        }
341
342
6
        let c0 = u64::from_le_bytes(<[u8; 8]>::try_from(&slice[..8]).unwrap());
343
6
        let c1 = u64::from_le_bytes(<[u8; 8]>::try_from(&slice[8..16]).unwrap());
344
6
        let allowed_slots = BabeAllowedSlots::from_slice(&slice[16..])
?0
;
345
346
6
        Ok(BabeNextConfig {
347
6
            c: (c0, c1),
348
6
            allowed_slots,
349
6
        })
350
6
    }
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_14BabeNextConfig10from_slice
351
352
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
353
    /// encoding of that object.
354
1
    pub fn scale_encoding(&self) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
355
1
        iter::once(either::Left(self.c.0.to_le_bytes()))
356
1
            .chain(iter::once(either::Left(self.c.1.to_le_bytes())))
357
1
            .chain(self.allowed_slots.scale_encoding().map(either::Right))
358
1
    }
_RNvMsd_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_14BabeNextConfig14scale_encoding
Line
Count
Source
354
1
    pub fn scale_encoding(&self) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
355
1
        iter::once(either::Left(self.c.0.to_le_bytes()))
356
1
            .chain(iter::once(either::Left(self.c.1.to_le_bytes())))
357
1
            .chain(self.allowed_slots.scale_encoding().map(either::Right))
358
1
    }
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_14BabeNextConfig14scale_encoding
359
}
360
361
/// Types of allowed slots.
362
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
363
pub enum BabeAllowedSlots {
364
    /// Only allow primary slot claims.
365
    PrimarySlots,
366
    /// Allow primary and secondary plain slot claims.
367
    PrimaryAndSecondaryPlainSlots,
368
    /// Allow primary and secondary VRF slot claims.
369
    PrimaryAndSecondaryVrfSlots,
370
}
371
372
impl BabeAllowedSlots {
373
    /// Decodes a [`BabeAllowedSlots`] from a slice of bytes.
374
7
    pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
375
7
        Ok(match slice.first() {
376
0
            Some(0) => BabeAllowedSlots::PrimarySlots,
377
0
            Some(1) => BabeAllowedSlots::PrimaryAndSecondaryPlainSlots,
378
7
            Some(2) => BabeAllowedSlots::PrimaryAndSecondaryVrfSlots,
379
0
            _ => return Err(Error::BadBabePreDigestRefType),
380
        })
381
7
    }
_RNvMse_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_16BabeAllowedSlots10from_slice
Line
Count
Source
374
7
    pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
375
7
        Ok(match slice.first() {
376
0
            Some(0) => BabeAllowedSlots::PrimarySlots,
377
0
            Some(1) => BabeAllowedSlots::PrimaryAndSecondaryPlainSlots,
378
7
            Some(2) => BabeAllowedSlots::PrimaryAndSecondaryVrfSlots,
379
0
            _ => return Err(Error::BadBabePreDigestRefType),
380
        })
381
7
    }
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_16BabeAllowedSlots10from_slice
382
383
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
384
    /// encoding of that object.
385
1
    pub fn scale_encoding(&self) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
386
1
        iter::once(match self {
387
0
            BabeAllowedSlots::PrimarySlots => [0],
388
0
            BabeAllowedSlots::PrimaryAndSecondaryPlainSlots => [1],
389
1
            BabeAllowedSlots::PrimaryAndSecondaryVrfSlots => [2],
390
        })
391
1
    }
_RNvMse_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_16BabeAllowedSlots14scale_encoding
Line
Count
Source
385
1
    pub fn scale_encoding(&self) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
386
1
        iter::once(match self {
387
0
            BabeAllowedSlots::PrimarySlots => [0],
388
0
            BabeAllowedSlots::PrimaryAndSecondaryPlainSlots => [1],
389
1
            BabeAllowedSlots::PrimaryAndSecondaryVrfSlots => [2],
390
        })
391
1
    }
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_16BabeAllowedSlots14scale_encoding
392
}
393
394
/// A BABE pre-runtime digest. This contains all data required to validate a
395
/// block and for the BABE runtime module. Slots can be assigned to a primary
396
/// (VRF based) and to a secondary (slot number based).
397
#[derive(Clone, Debug, PartialEq, Eq)]
398
pub enum BabePreDigestRef<'a> {
399
    /// A primary VRF-based slot assignment.
400
    Primary(BabePrimaryPreDigestRef<'a>),
401
    /// A secondary deterministic slot assignment.
402
    SecondaryPlain(BabeSecondaryPlainPreDigest),
403
    /// A secondary deterministic slot assignment with VRF outputs.
404
    SecondaryVRF(BabeSecondaryVRFPreDigestRef<'a>),
405
}
406
407
impl<'a> BabePreDigestRef<'a> {
408
    /// Decodes a [`BabePreDigestRef`] from a slice of bytes.
409
55
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
410
55
        Ok(match slice.first() {
411
29
            Some(1) => BabePreDigestRef::Primary(BabePrimaryPreDigestRef::from_slice(&slice[1..])
?0
),
412
25
            Some(2) => BabePreDigestRef::SecondaryPlain(BabeSecondaryPlainPreDigest::from_slice(
413
25
                &slice[1..],
414
0
            )?),
415
1
            Some(3) => BabePreDigestRef::SecondaryVRF(BabeSecondaryVRFPreDigestRef::from_slice(
416
1
                &slice[1..],
417
0
            )?),
418
0
            Some(_) => return Err(Error::BadBabePreDigestRefType),
419
0
            None => return Err(Error::TooShort),
420
        })
421
55
    }
_RNvMsf_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_16BabePreDigestRef10from_slice
Line
Count
Source
409
55
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
410
55
        Ok(match slice.first() {
411
29
            Some(1) => BabePreDigestRef::Primary(BabePrimaryPreDigestRef::from_slice(&slice[1..])
?0
),
412
25
            Some(2) => BabePreDigestRef::SecondaryPlain(BabeSecondaryPlainPreDigest::from_slice(
413
25
                &slice[1..],
414
0
            )?),
415
1
            Some(3) => BabePreDigestRef::SecondaryVRF(BabeSecondaryVRFPreDigestRef::from_slice(
416
1
                &slice[1..],
417
0
            )?),
418
0
            Some(_) => return Err(Error::BadBabePreDigestRefType),
419
0
            None => return Err(Error::TooShort),
420
        })
421
55
    }
Unexecuted instantiation: _RNvMsf_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_16BabePreDigestRef10from_slice
422
423
    /// Returns `true` for [`BabePreDigestRef::Primary`].
424
0
    pub fn is_primary(&self) -> bool {
425
0
        matches!(self, BabePreDigestRef::Primary(_))
426
0
    }
Unexecuted instantiation: _RNvMsf_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_16BabePreDigestRef10is_primary
Unexecuted instantiation: _RNvMsf_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_16BabePreDigestRef10is_primary
427
428
    /// Returns the slot number stored in the header.
429
2
    pub fn slot_number(&self) -> u64 {
430
2
        match self {
431
1
            BabePreDigestRef::Primary(digest) => digest.slot_number,
432
1
            BabePreDigestRef::SecondaryPlain(digest) => digest.slot_number,
433
0
            BabePreDigestRef::SecondaryVRF(digest) => digest.slot_number,
434
        }
435
2
    }
_RNvMsf_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_16BabePreDigestRef11slot_number
Line
Count
Source
429
2
    pub fn slot_number(&self) -> u64 {
430
2
        match self {
431
1
            BabePreDigestRef::Primary(digest) => digest.slot_number,
432
1
            BabePreDigestRef::SecondaryPlain(digest) => digest.slot_number,
433
0
            BabePreDigestRef::SecondaryVRF(digest) => digest.slot_number,
434
        }
435
2
    }
Unexecuted instantiation: _RNvMsf_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_16BabePreDigestRef11slot_number
436
437
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
438
    /// encoding of that object.
439
7
    pub fn scale_encoding(&self) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
440
7
        let index = iter::once(match self {
441
3
            BabePreDigestRef::Primary(_) => [1],
442
4
            BabePreDigestRef::SecondaryPlain(_) => [2],
443
0
            BabePreDigestRef::SecondaryVRF(_) => [3],
444
        });
445
446
7
        let body = match self {
447
3
            BabePreDigestRef::Primary(digest) => either::Left(either::Left(
448
3
                digest
449
3
                    .scale_encoding()
450
12
                    .
map3
(|buf| either::Left(either::Left(buf))),
_RNCNvMsf_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB7_16BabePreDigestRef14scale_encoding0Bb_
Line
Count
Source
450
12
                    .map(|buf| either::Left(either::Left(buf))),
Unexecuted instantiation: _RNCNvMsf_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB7_16BabePreDigestRef14scale_encoding0Bb_
451
            )),
452
4
            BabePreDigestRef::SecondaryPlain(digest) => either::Left(either::Right(
453
4
                digest
454
4
                    .scale_encoding()
455
8
                    .
map4
(|buf| either::Left(either::Right(buf))),
_RNCNvMsf_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB7_16BabePreDigestRef14scale_encodings_0Bb_
Line
Count
Source
455
8
                    .map(|buf| either::Left(either::Right(buf))),
Unexecuted instantiation: _RNCNvMsf_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB7_16BabePreDigestRef14scale_encodings_0Bb_
456
            )),
457
0
            BabePreDigestRef::SecondaryVRF(digest) => {
458
0
                either::Right(digest.scale_encoding().map(either::Right))
459
            }
460
        };
461
462
7
        index.map(either::Left).chain(body.map(either::Right))
463
7
    }
_RNvMsf_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_16BabePreDigestRef14scale_encoding
Line
Count
Source
439
7
    pub fn scale_encoding(&self) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
440
7
        let index = iter::once(match self {
441
3
            BabePreDigestRef::Primary(_) => [1],
442
4
            BabePreDigestRef::SecondaryPlain(_) => [2],
443
0
            BabePreDigestRef::SecondaryVRF(_) => [3],
444
        });
445
446
7
        let body = match self {
447
3
            BabePreDigestRef::Primary(digest) => either::Left(either::Left(
448
3
                digest
449
3
                    .scale_encoding()
450
3
                    .map(|buf| either::Left(either::Left(buf))),
451
            )),
452
4
            BabePreDigestRef::SecondaryPlain(digest) => either::Left(either::Right(
453
4
                digest
454
4
                    .scale_encoding()
455
4
                    .map(|buf| either::Left(either::Right(buf))),
456
            )),
457
0
            BabePreDigestRef::SecondaryVRF(digest) => {
458
0
                either::Right(digest.scale_encoding().map(either::Right))
459
            }
460
        };
461
462
7
        index.map(either::Left).chain(body.map(either::Right))
463
7
    }
Unexecuted instantiation: _RNvMsf_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_16BabePreDigestRef14scale_encoding
464
}
465
466
impl<'a> From<BabePreDigestRef<'a>> for BabePreDigest {
467
2
    fn from(a: BabePreDigestRef<'a>) -> Self {
468
2
        match a {
469
2
            BabePreDigestRef::Primary(a) => BabePreDigest::Primary(a.into()),
470
0
            BabePreDigestRef::SecondaryPlain(a) => BabePreDigest::SecondaryPlain(a),
471
0
            BabePreDigestRef::SecondaryVRF(a) => BabePreDigest::SecondaryVRF(a.into()),
472
        }
473
2
    }
_RNvXsg_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_13BabePreDigestINtNtCs1p5UDGgVI4d_4core7convert4FromNtB5_16BabePreDigestRefE4from
Line
Count
Source
467
2
    fn from(a: BabePreDigestRef<'a>) -> Self {
468
2
        match a {
469
2
            BabePreDigestRef::Primary(a) => BabePreDigest::Primary(a.into()),
470
0
            BabePreDigestRef::SecondaryPlain(a) => BabePreDigest::SecondaryPlain(a),
471
0
            BabePreDigestRef::SecondaryVRF(a) => BabePreDigest::SecondaryVRF(a.into()),
472
        }
473
2
    }
Unexecuted instantiation: _RNvXsg_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_13BabePreDigestINtNtCs1p5UDGgVI4d_4core7convert4FromNtB5_16BabePreDigestRefE4from
474
}
475
476
/// A BABE pre-runtime digest. This contains all data required to validate a
477
/// block and for the BABE runtime module. Slots can be assigned to a primary
478
/// (VRF based) and to a secondary (slot number based).
479
#[derive(Debug, Clone)]
480
pub enum BabePreDigest {
481
    /// A primary VRF-based slot assignment.
482
    Primary(BabePrimaryPreDigest),
483
    /// A secondary deterministic slot assignment.
484
    SecondaryPlain(BabeSecondaryPlainPreDigest),
485
    /// A secondary deterministic slot assignment with VRF outputs.
486
    SecondaryVRF(BabeSecondaryVRFPreDigest),
487
}
488
489
impl<'a> From<&'a BabePreDigest> for BabePreDigestRef<'a> {
490
0
    fn from(a: &'a BabePreDigest) -> Self {
491
0
        match a {
492
0
            BabePreDigest::Primary(a) => BabePreDigestRef::Primary(a.into()),
493
0
            BabePreDigest::SecondaryPlain(a) => BabePreDigestRef::SecondaryPlain(a.clone()),
494
0
            BabePreDigest::SecondaryVRF(a) => BabePreDigestRef::SecondaryVRF(a.into()),
495
        }
496
0
    }
Unexecuted instantiation: _RNvXsh_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_16BabePreDigestRefINtNtCs1p5UDGgVI4d_4core7convert4FromRNtB5_13BabePreDigestE4from
Unexecuted instantiation: _RNvXsh_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_16BabePreDigestRefINtNtCs1p5UDGgVI4d_4core7convert4FromRNtB5_13BabePreDigestE4from
497
}
498
499
/// Raw BABE primary slot assignment pre-digest.
500
#[derive(Debug, Clone)]
501
pub struct BabePrimaryPreDigestRef<'a> {
502
    /// Authority index
503
    pub authority_index: u32,
504
    /// Slot number
505
    pub slot_number: u64,
506
    /// VRF output
507
    pub vrf_output: &'a [u8; 32],
508
    /// VRF proof
509
    pub vrf_proof: &'a [u8; 64],
510
}
511
512
impl<'a> BabePrimaryPreDigestRef<'a> {
513
    /// Decodes a [`BabePrimaryPreDigestRef`] from a slice of bytes.
514
29
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
515
29
        if slice.len() != 4 + 8 + 32 + 64 {
516
0
            return Err(Error::TooShort);
517
29
        }
518
519
29
        Ok(BabePrimaryPreDigestRef {
520
29
            authority_index: u32::from_le_bytes(<[u8; 4]>::try_from(&slice[0..4]).unwrap()),
521
29
            slot_number: u64::from_le_bytes(<[u8; 8]>::try_from(&slice[4..12]).unwrap()),
522
29
            vrf_output: TryFrom::try_from(&slice[12..44]).unwrap(),
523
29
            vrf_proof: TryFrom::try_from(&slice[44..108]).unwrap(),
524
29
        })
525
29
    }
_RNvMsi_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_23BabePrimaryPreDigestRef10from_slice
Line
Count
Source
514
29
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
515
29
        if slice.len() != 4 + 8 + 32 + 64 {
516
0
            return Err(Error::TooShort);
517
29
        }
518
519
29
        Ok(BabePrimaryPreDigestRef {
520
29
            authority_index: u32::from_le_bytes(<[u8; 4]>::try_from(&slice[0..4]).unwrap()),
521
29
            slot_number: u64::from_le_bytes(<[u8; 8]>::try_from(&slice[4..12]).unwrap()),
522
29
            vrf_output: TryFrom::try_from(&slice[12..44]).unwrap(),
523
29
            vrf_proof: TryFrom::try_from(&slice[44..108]).unwrap(),
524
29
        })
525
29
    }
Unexecuted instantiation: _RNvMsi_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_23BabePrimaryPreDigestRef10from_slice
526
527
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
528
    /// encoding of that object.
529
3
    pub fn scale_encoding(
530
3
        &self,
531
3
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + use<'a>> + Clone + use<'a> {
532
3
        let header = iter::once(either::Left(self.authority_index.to_le_bytes()))
533
3
            .chain(iter::once(either::Right(self.slot_number.to_le_bytes())))
534
3
            .map(either::Left);
535
536
3
        header
537
3
            .chain(iter::once(either::Right(&self.vrf_output[..])))
538
3
            .chain(iter::once(either::Right(&self.vrf_proof[..])))
539
3
    }
_RNvMsi_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_23BabePrimaryPreDigestRef14scale_encoding
Line
Count
Source
529
3
    pub fn scale_encoding(
530
3
        &self,
531
3
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + use<'a>> + Clone + use<'a> {
532
3
        let header = iter::once(either::Left(self.authority_index.to_le_bytes()))
533
3
            .chain(iter::once(either::Right(self.slot_number.to_le_bytes())))
534
3
            .map(either::Left);
535
536
3
        header
537
3
            .chain(iter::once(either::Right(&self.vrf_output[..])))
538
3
            .chain(iter::once(either::Right(&self.vrf_proof[..])))
539
3
    }
Unexecuted instantiation: _RNvMsi_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_23BabePrimaryPreDigestRef14scale_encoding
540
}
541
542
impl<'a> cmp::PartialEq<BabePrimaryPreDigestRef<'a>> for BabePrimaryPreDigestRef<'a> {
543
0
    fn eq(&self, other: &BabePrimaryPreDigestRef<'a>) -> bool {
544
0
        self.authority_index == other.authority_index
545
0
            && self.slot_number == other.slot_number
546
0
            && self.vrf_output[..] == other.vrf_output[..]
547
0
            && self.vrf_proof[..] == other.vrf_proof[..]
548
0
    }
Unexecuted instantiation: _RNvXsj_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_23BabePrimaryPreDigestRefNtNtCs1p5UDGgVI4d_4core3cmp9PartialEq2eq
Unexecuted instantiation: _RNvXsj_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_23BabePrimaryPreDigestRefNtNtCs1p5UDGgVI4d_4core3cmp9PartialEq2eq
549
}
550
551
impl<'a> cmp::Eq for BabePrimaryPreDigestRef<'a> {}
552
553
impl<'a> From<&'a BabePrimaryPreDigest> for BabePrimaryPreDigestRef<'a> {
554
0
    fn from(a: &'a BabePrimaryPreDigest) -> Self {
555
0
        BabePrimaryPreDigestRef {
556
0
            authority_index: a.authority_index,
557
0
            slot_number: a.slot_number,
558
0
            vrf_output: &a.vrf_output,
559
0
            vrf_proof: &a.vrf_proof,
560
0
        }
561
0
    }
Unexecuted instantiation: _RNvXsl_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_23BabePrimaryPreDigestRefINtNtCs1p5UDGgVI4d_4core7convert4FromRNtB5_20BabePrimaryPreDigestE4from
Unexecuted instantiation: _RNvXsl_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_23BabePrimaryPreDigestRefINtNtCs1p5UDGgVI4d_4core7convert4FromRNtB5_20BabePrimaryPreDigestE4from
562
}
563
564
/// Raw BABE primary slot assignment pre-digest.
565
#[derive(Debug, Clone)]
566
pub struct BabePrimaryPreDigest {
567
    /// Authority index
568
    pub authority_index: u32,
569
    /// Slot number
570
    pub slot_number: u64,
571
    /// VRF output
572
    pub vrf_output: [u8; 32],
573
    /// VRF proof
574
    pub vrf_proof: [u8; 64],
575
}
576
577
impl<'a> From<BabePrimaryPreDigestRef<'a>> for BabePrimaryPreDigest {
578
2
    fn from(a: BabePrimaryPreDigestRef<'a>) -> Self {
579
2
        BabePrimaryPreDigest {
580
2
            authority_index: a.authority_index,
581
2
            slot_number: a.slot_number,
582
2
            vrf_output: *a.vrf_output,
583
2
            vrf_proof: *a.vrf_proof,
584
2
        }
585
2
    }
_RNvXsm_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_20BabePrimaryPreDigestINtNtCs1p5UDGgVI4d_4core7convert4FromNtB5_23BabePrimaryPreDigestRefE4from
Line
Count
Source
578
2
    fn from(a: BabePrimaryPreDigestRef<'a>) -> Self {
579
2
        BabePrimaryPreDigest {
580
2
            authority_index: a.authority_index,
581
2
            slot_number: a.slot_number,
582
2
            vrf_output: *a.vrf_output,
583
2
            vrf_proof: *a.vrf_proof,
584
2
        }
585
2
    }
Unexecuted instantiation: _RNvXsm_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_20BabePrimaryPreDigestINtNtCs1p5UDGgVI4d_4core7convert4FromNtB5_23BabePrimaryPreDigestRefE4from
586
}
587
588
/// BABE secondary slot assignment pre-digest.
589
#[derive(Clone, Debug, PartialEq, Eq)]
590
pub struct BabeSecondaryPlainPreDigest {
591
    /// Authority index
592
    ///
593
    /// This is not strictly-speaking necessary, since the secondary slots
594
    /// are assigned based on slot number and epoch randomness. But including
595
    /// it makes things easier for higher-level users of the chain data to
596
    /// be aware of the author of a secondary-slot block.
597
    pub authority_index: u32,
598
599
    /// Slot number
600
    pub slot_number: u64,
601
}
602
603
impl BabeSecondaryPlainPreDigest {
604
    /// Decodes a [`BabeSecondaryPlainPreDigest`] from a slice of bytes.
605
25
    pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
606
25
        if slice.len() != 12 {
607
0
            return Err(Error::DigestItemDecodeError);
608
25
        }
609
610
25
        let authority_index = u32::from_le_bytes(<[u8; 4]>::try_from(&slice[..4]).unwrap());
611
25
        let slot_number = u64::from_le_bytes(<[u8; 8]>::try_from(&slice[4..]).unwrap());
612
613
25
        Ok(BabeSecondaryPlainPreDigest {
614
25
            authority_index,
615
25
            slot_number,
616
25
        })
617
25
    }
_RNvMsn_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_27BabeSecondaryPlainPreDigest10from_slice
Line
Count
Source
605
25
    pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
606
25
        if slice.len() != 12 {
607
0
            return Err(Error::DigestItemDecodeError);
608
25
        }
609
610
25
        let authority_index = u32::from_le_bytes(<[u8; 4]>::try_from(&slice[..4]).unwrap());
611
25
        let slot_number = u64::from_le_bytes(<[u8; 8]>::try_from(&slice[4..]).unwrap());
612
613
25
        Ok(BabeSecondaryPlainPreDigest {
614
25
            authority_index,
615
25
            slot_number,
616
25
        })
617
25
    }
Unexecuted instantiation: _RNvMsn_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_27BabeSecondaryPlainPreDigest10from_slice
618
619
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
620
    /// encoding of that object.
621
4
    pub fn scale_encoding(&self) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
622
4
        iter::once(either::Left(self.authority_index.to_le_bytes()))
623
4
            .chain(iter::once(either::Right(self.slot_number.to_le_bytes())))
624
4
    }
_RNvMsn_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_27BabeSecondaryPlainPreDigest14scale_encoding
Line
Count
Source
621
4
    pub fn scale_encoding(&self) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
622
4
        iter::once(either::Left(self.authority_index.to_le_bytes()))
623
4
            .chain(iter::once(either::Right(self.slot_number.to_le_bytes())))
624
4
    }
Unexecuted instantiation: _RNvMsn_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_27BabeSecondaryPlainPreDigest14scale_encoding
625
}
626
627
/// BABE secondary deterministic slot assignment with VRF outputs.
628
#[derive(Debug, Clone)]
629
pub struct BabeSecondaryVRFPreDigestRef<'a> {
630
    /// Authority index
631
    pub authority_index: u32,
632
    /// Slot number
633
    pub slot_number: u64,
634
    /// VRF output
635
    pub vrf_output: &'a [u8; 32],
636
    /// VRF proof
637
    pub vrf_proof: &'a [u8; 64],
638
}
639
640
impl<'a> BabeSecondaryVRFPreDigestRef<'a> {
641
    /// Decodes a [`BabeSecondaryVRFPreDigestRef`] from a slice of bytes.
642
1
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
643
1
        if slice.len() != 4 + 8 + 32 + 64 {
644
0
            return Err(Error::TooShort);
645
1
        }
646
647
1
        Ok(BabeSecondaryVRFPreDigestRef {
648
1
            authority_index: u32::from_le_bytes(<[u8; 4]>::try_from(&slice[0..4]).unwrap()),
649
1
            slot_number: u64::from_le_bytes(<[u8; 8]>::try_from(&slice[4..12]).unwrap()),
650
1
            vrf_output: TryFrom::try_from(&slice[12..44]).unwrap(),
651
1
            vrf_proof: TryFrom::try_from(&slice[44..108]).unwrap(),
652
1
        })
653
1
    }
_RNvMso_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_28BabeSecondaryVRFPreDigestRef10from_slice
Line
Count
Source
642
1
    pub fn from_slice(slice: &'a [u8]) -> Result<Self, Error> {
643
1
        if slice.len() != 4 + 8 + 32 + 64 {
644
0
            return Err(Error::TooShort);
645
1
        }
646
647
1
        Ok(BabeSecondaryVRFPreDigestRef {
648
1
            authority_index: u32::from_le_bytes(<[u8; 4]>::try_from(&slice[0..4]).unwrap()),
649
1
            slot_number: u64::from_le_bytes(<[u8; 8]>::try_from(&slice[4..12]).unwrap()),
650
1
            vrf_output: TryFrom::try_from(&slice[12..44]).unwrap(),
651
1
            vrf_proof: TryFrom::try_from(&slice[44..108]).unwrap(),
652
1
        })
653
1
    }
Unexecuted instantiation: _RNvMso_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_28BabeSecondaryVRFPreDigestRef10from_slice
654
655
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
656
    /// encoding of that object.
657
0
    pub fn scale_encoding(
658
0
        &self,
659
0
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone + use<'a>> + Clone + use<'a> {
660
0
        let header = iter::once(either::Left(self.authority_index.to_le_bytes()))
661
0
            .chain(iter::once(either::Right(self.slot_number.to_le_bytes())))
662
0
            .map(either::Left);
663
664
0
        header
665
0
            .chain(iter::once(either::Right(&self.vrf_output[..])))
666
0
            .chain(iter::once(either::Right(&self.vrf_proof[..])))
667
0
    }
Unexecuted instantiation: _RNvMso_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_28BabeSecondaryVRFPreDigestRef14scale_encoding
Unexecuted instantiation: _RNvMso_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_28BabeSecondaryVRFPreDigestRef14scale_encoding
668
}
669
670
impl<'a> cmp::PartialEq<BabeSecondaryVRFPreDigestRef<'a>> for BabeSecondaryVRFPreDigestRef<'a> {
671
0
    fn eq(&self, other: &BabeSecondaryVRFPreDigestRef<'a>) -> bool {
672
0
        self.authority_index == other.authority_index
673
0
            && self.slot_number == other.slot_number
674
0
            && self.vrf_output[..] == other.vrf_output[..]
675
0
            && self.vrf_proof[..] == other.vrf_proof[..]
676
0
    }
Unexecuted instantiation: _RNvXsp_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_28BabeSecondaryVRFPreDigestRefNtNtCs1p5UDGgVI4d_4core3cmp9PartialEq2eq
Unexecuted instantiation: _RNvXsp_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_28BabeSecondaryVRFPreDigestRefNtNtCs1p5UDGgVI4d_4core3cmp9PartialEq2eq
677
}
678
679
impl<'a> cmp::Eq for BabeSecondaryVRFPreDigestRef<'a> {}
680
681
impl<'a> From<&'a BabeSecondaryVRFPreDigest> for BabeSecondaryVRFPreDigestRef<'a> {
682
0
    fn from(a: &'a BabeSecondaryVRFPreDigest) -> Self {
683
0
        BabeSecondaryVRFPreDigestRef {
684
0
            authority_index: a.authority_index,
685
0
            slot_number: a.slot_number,
686
0
            vrf_output: &a.vrf_output,
687
0
            vrf_proof: &a.vrf_proof,
688
0
        }
689
0
    }
Unexecuted instantiation: _RNvXsr_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_28BabeSecondaryVRFPreDigestRefINtNtCs1p5UDGgVI4d_4core7convert4FromRNtB5_25BabeSecondaryVRFPreDigestE4from
Unexecuted instantiation: _RNvXsr_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_28BabeSecondaryVRFPreDigestRefINtNtCs1p5UDGgVI4d_4core7convert4FromRNtB5_25BabeSecondaryVRFPreDigestE4from
690
}
691
692
/// BABE secondary deterministic slot assignment with VRF outputs.
693
#[derive(Debug, Clone)]
694
pub struct BabeSecondaryVRFPreDigest {
695
    /// Authority index
696
    pub authority_index: u32,
697
    /// Slot number
698
    pub slot_number: u64,
699
    /// VRF output
700
    pub vrf_output: [u8; 32],
701
    /// VRF proof
702
    pub vrf_proof: [u8; 64],
703
}
704
705
impl<'a> From<BabeSecondaryVRFPreDigestRef<'a>> for BabeSecondaryVRFPreDigest {
706
0
    fn from(a: BabeSecondaryVRFPreDigestRef<'a>) -> Self {
707
0
        BabeSecondaryVRFPreDigest {
708
0
            authority_index: a.authority_index,
709
0
            slot_number: a.slot_number,
710
0
            vrf_output: *a.vrf_output,
711
0
            vrf_proof: *a.vrf_proof,
712
0
        }
713
0
    }
Unexecuted instantiation: _RNvXss_NtNtCsjlkOsLH0Zfj_7smoldot6header4babeNtB5_25BabeSecondaryVRFPreDigestINtNtCs1p5UDGgVI4d_4core7convert4FromNtB5_28BabeSecondaryVRFPreDigestRefE4from
Unexecuted instantiation: _RNvXss_NtNtCsc1ywvx6YAnK_7smoldot6header4babeNtB5_25BabeSecondaryVRFPreDigestINtNtCs1p5UDGgVI4d_4core7convert4FromNtB5_28BabeSecondaryVRFPreDigestRefE4from
714
}