Coverage Report

Created: 2024-05-16 12:16

/__w/smoldot/smoldot/repo/lib/src/network/codec/grandpa.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
// TODO: document all this
19
20
use crate::finality::{decode, decode::PrecommitRef}; // TODO: weird imports
21
22
use alloc::vec::Vec;
23
use core::{cmp, iter, mem};
24
use nom::Finish as _;
25
26
pub use crate::finality::decode::{CommitMessageRef, UnsignedPrecommitRef};
27
28
#[derive(Debug, Clone, PartialEq, Eq)]
29
pub enum GrandpaNotificationRef<'a> {
30
    Vote(VoteMessageRef<'a>),
31
    Commit(CommitMessageRef<'a>), // TODO: consider weaker type, since in different module
32
    Neighbor(NeighborPacket),
33
    CatchUpRequest(CatchUpRequest),
34
    CatchUp(CatchUpRef<'a>),
35
}
36
37
impl<'a> GrandpaNotificationRef<'a> {
38
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
39
    /// encoding of that object.
40
0
    pub fn scale_encoding(
41
0
        &self,
42
0
        block_number_bytes: usize,
43
0
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
44
0
        match self {
45
0
            GrandpaNotificationRef::Neighbor(n) => iter::once(either::Left(&[2u8]))
46
0
                .chain(n.scale_encoding(block_number_bytes).map(either::Right)),
47
0
            _ => todo!(),
48
        }
49
0
    }
Unexecuted instantiation: _RNvMNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpaNtB2_22GrandpaNotificationRef14scale_encoding
Unexecuted instantiation: _RNvMNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpaNtB2_22GrandpaNotificationRef14scale_encoding
50
}
51
52
#[derive(Debug, Clone, PartialEq, Eq)]
53
pub struct VoteMessageRef<'a> {
54
    pub round_number: u64,
55
    pub set_id: u64,
56
    pub message: MessageRef<'a>,
57
    pub signature: &'a [u8; 64],
58
    pub authority_public_key: &'a [u8; 32],
59
}
60
61
#[derive(Debug, Clone, PartialEq, Eq)]
62
pub enum MessageRef<'a> {
63
    Prevote(UnsignedPrevoteRef<'a>),
64
    Precommit(UnsignedPrecommitRef<'a>),
65
    PrimaryPropose(PrimaryProposeRef<'a>),
66
}
67
68
#[derive(Debug, Clone, PartialEq, Eq)]
69
pub struct UnsignedPrevoteRef<'a> {
70
    pub target_hash: &'a [u8; 32],
71
    pub target_number: u64,
72
}
73
74
#[derive(Debug, Clone, PartialEq, Eq)]
75
pub struct PrimaryProposeRef<'a> {
76
    pub target_hash: &'a [u8; 32],
77
    pub target_number: u64,
78
}
79
80
#[derive(Debug, Clone, PartialEq, Eq)]
81
pub struct NeighborPacket {
82
    pub round_number: u64,
83
    pub set_id: u64,
84
    pub commit_finalized_height: u64,
85
}
86
87
impl NeighborPacket {
88
    /// Returns an iterator to list of buffers which, when concatenated, produces the SCALE
89
    /// encoding of that object.
90
0
    pub fn scale_encoding(
91
0
        &self,
92
0
        block_number_bytes: usize,
93
0
    ) -> impl Iterator<Item = impl AsRef<[u8]> + Clone> + Clone {
94
0
        let mut commit_finalized_height = Vec::with_capacity(cmp::max(
95
0
            block_number_bytes,
96
0
            mem::size_of_val(&self.commit_finalized_height),
97
0
        ));
98
0
        commit_finalized_height.extend(self.commit_finalized_height.to_le_bytes());
99
0
        // TODO: unclear what to do if the block number doesn't fit in `block_number_bytes`
100
0
        debug_assert!(!commit_finalized_height
101
0
            .iter()
102
0
            .skip(block_number_bytes)
103
0
            .any(|b| *b != 0));
Unexecuted instantiation: _RNCNvMs_NtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpaNtB6_14NeighborPacket14scale_encoding0Bc_
Unexecuted instantiation: _RNCNvMs_NtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpaNtB6_14NeighborPacket14scale_encoding0Bc_
104
0
        commit_finalized_height.resize(block_number_bytes, 0);
105
0
106
0
        [
107
0
            either::Right(either::Left([1u8])),
108
0
            either::Left(self.round_number.to_le_bytes()),
109
0
            either::Left(self.set_id.to_le_bytes()),
110
0
            either::Right(either::Right(commit_finalized_height)),
111
0
        ]
112
0
        .into_iter()
113
0
    }
Unexecuted instantiation: _RNvMs_NtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpaNtB4_14NeighborPacket14scale_encoding
Unexecuted instantiation: _RNvMs_NtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpaNtB4_14NeighborPacket14scale_encoding
114
}
115
116
#[derive(Debug, Clone, PartialEq, Eq)]
117
pub struct CatchUpRequest {
118
    pub round_number: u64,
119
    pub set_id: u64,
120
}
121
122
#[derive(Debug, Clone, PartialEq, Eq)]
123
pub struct CatchUpRef<'a> {
124
    pub set_id: u64,
125
    pub round_number: u64,
126
    pub prevotes: Vec<PrevoteRef<'a>>,
127
    pub precommits: Vec<PrecommitRef<'a>>,
128
    pub base_hash: &'a [u8; 32],
129
    pub base_number: u64,
130
}
131
132
#[derive(Debug, Clone, PartialEq, Eq)]
133
pub struct PrevoteRef<'a> {
134
    /// Hash of the block concerned by the pre-vote.
135
    pub target_hash: &'a [u8; 32],
136
    /// Height of the block concerned by the pre-vote.
137
    pub target_number: u64,
138
139
    /// Ed25519 signature made with [`PrevoteRef::authority_public_key`].
140
    pub signature: &'a [u8; 64],
141
142
    /// Authority that signed the pre-vote. Must be part of the authority set for the
143
    /// justification to be valid.
144
    pub authority_public_key: &'a [u8; 32],
145
}
146
147
/// Attempt to decode the given SCALE-encoded Grandpa notification.
148
1
pub fn decode_grandpa_notification(
149
1
    scale_encoded: &[u8],
150
1
    block_number_bytes: usize,
151
1
) -> Result<GrandpaNotificationRef, DecodeGrandpaNotificationError> {
152
1
    match nom::combinator::all_consuming(nom::combinator::complete(grandpa_notification(
153
1
        block_number_bytes,
154
1
    )))(scale_encoded)
155
1
    .finish()
156
    {
157
1
        Ok((_, notif)) => Ok(notif),
158
0
        Err(err) => Err(DecodeGrandpaNotificationError(err.code)),
159
    }
160
1
}
_RNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa27decode_grandpa_notification
Line
Count
Source
148
1
pub fn decode_grandpa_notification(
149
1
    scale_encoded: &[u8],
150
1
    block_number_bytes: usize,
151
1
) -> Result<GrandpaNotificationRef, DecodeGrandpaNotificationError> {
152
1
    match nom::combinator::all_consuming(nom::combinator::complete(grandpa_notification(
153
1
        block_number_bytes,
154
1
    )))(scale_encoded)
155
1
    .finish()
156
    {
157
1
        Ok((_, notif)) => Ok(notif),
158
0
        Err(err) => Err(DecodeGrandpaNotificationError(err.code)),
159
    }
160
1
}
Unexecuted instantiation: _RNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa27decode_grandpa_notification
161
162
/// Error potentially returned by [`decode_grandpa_notification`].
163
0
#[derive(Debug, derive_more::Display)]
Unexecuted instantiation: _RNvXsK_NtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpaNtB5_30DecodeGrandpaNotificationErrorNtNtCsaYZPK01V26L_4core3fmt7Display3fmt
Unexecuted instantiation: _RNvXsK_NtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpaNtB5_30DecodeGrandpaNotificationErrorNtNtCsaYZPK01V26L_4core3fmt7Display3fmt
164
#[display(fmt = "Failed to decode a Grandpa notification")]
165
pub struct DecodeGrandpaNotificationError(nom::error::ErrorKind);
166
167
// Nom combinators below.
168
169
1
fn grandpa_notification<'a>(
170
1
    block_number_bytes: usize,
171
1
) -> impl FnMut(&'a [u8]) -> nom::IResult<&[u8], GrandpaNotificationRef> {
172
1
    nom::error::context(
173
1
        "grandpa_notification",
174
1
        nom::branch::alt((
175
1
            nom::combinator::map(
176
1
                nom::sequence::preceded(
177
1
                    nom::bytes::streaming::tag(&[0]),
178
1
                    vote_message(block_number_bytes),
179
1
                ),
180
1
                GrandpaNotificationRef::Vote,
181
1
            ),
182
1
            nom::combinator::map(
183
1
                nom::sequence::preceded(nom::bytes::streaming::tag(&[1]), move |s| {
184
0
                    decode::decode_partial_grandpa_commit(s, block_number_bytes)
185
0
                        .map(|(a, b)| (b, a))
Unexecuted instantiation: _RNCNCNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa20grandpa_notification00Bb_
Unexecuted instantiation: _RNCNCNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa20grandpa_notification00Bb_
186
0
                        .map_err(|_| {
187
0
                            nom::Err::Failure(nom::error::make_error(
188
0
                                s,
189
0
                                nom::error::ErrorKind::Verify,
190
0
                            ))
191
0
                        })
Unexecuted instantiation: _RNCNCNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa20grandpa_notification0s_0Bb_
Unexecuted instantiation: _RNCNCNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa20grandpa_notification0s_0Bb_
192
1
                }),
Unexecuted instantiation: _RNCNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa20grandpa_notification0B9_
Unexecuted instantiation: _RNCNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa20grandpa_notification0B9_
193
1
                GrandpaNotificationRef::Commit,
194
1
            ),
195
1
            nom::combinator::map(
196
1
                nom::sequence::preceded(
197
1
                    nom::bytes::streaming::tag(&[2]),
198
1
                    neighbor_packet(block_number_bytes),
199
1
                ),
200
1
                GrandpaNotificationRef::Neighbor,
201
1
            ),
202
1
            nom::combinator::map(
203
1
                nom::sequence::preceded(nom::bytes::streaming::tag(&[3]), catch_up_request),
204
1
                GrandpaNotificationRef::CatchUpRequest,
205
1
            ),
206
1
            nom::combinator::map(
207
1
                nom::sequence::preceded(
208
1
                    nom::bytes::streaming::tag(&[4]),
209
1
                    catch_up(block_number_bytes),
210
1
                ),
211
1
                GrandpaNotificationRef::CatchUp,
212
1
            ),
213
1
        )),
214
1
    )
215
1
}
_RNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa20grandpa_notification
Line
Count
Source
169
1
fn grandpa_notification<'a>(
170
1
    block_number_bytes: usize,
171
1
) -> impl FnMut(&'a [u8]) -> nom::IResult<&[u8], GrandpaNotificationRef> {
172
1
    nom::error::context(
173
1
        "grandpa_notification",
174
1
        nom::branch::alt((
175
1
            nom::combinator::map(
176
1
                nom::sequence::preceded(
177
1
                    nom::bytes::streaming::tag(&[0]),
178
1
                    vote_message(block_number_bytes),
179
1
                ),
180
1
                GrandpaNotificationRef::Vote,
181
1
            ),
182
1
            nom::combinator::map(
183
1
                nom::sequence::preceded(nom::bytes::streaming::tag(&[1]), move |s| {
184
                    decode::decode_partial_grandpa_commit(s, block_number_bytes)
185
                        .map(|(a, b)| (b, a))
186
                        .map_err(|_| {
187
                            nom::Err::Failure(nom::error::make_error(
188
                                s,
189
                                nom::error::ErrorKind::Verify,
190
                            ))
191
                        })
192
1
                }),
193
1
                GrandpaNotificationRef::Commit,
194
1
            ),
195
1
            nom::combinator::map(
196
1
                nom::sequence::preceded(
197
1
                    nom::bytes::streaming::tag(&[2]),
198
1
                    neighbor_packet(block_number_bytes),
199
1
                ),
200
1
                GrandpaNotificationRef::Neighbor,
201
1
            ),
202
1
            nom::combinator::map(
203
1
                nom::sequence::preceded(nom::bytes::streaming::tag(&[3]), catch_up_request),
204
1
                GrandpaNotificationRef::CatchUpRequest,
205
1
            ),
206
1
            nom::combinator::map(
207
1
                nom::sequence::preceded(
208
1
                    nom::bytes::streaming::tag(&[4]),
209
1
                    catch_up(block_number_bytes),
210
1
                ),
211
1
                GrandpaNotificationRef::CatchUp,
212
1
            ),
213
1
        )),
214
1
    )
215
1
}
Unexecuted instantiation: _RNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa20grandpa_notification
216
217
1
fn vote_message<'a>(
218
1
    block_number_bytes: usize,
219
1
) -> impl FnMut(&'a [u8]) -> nom::IResult<&[u8], VoteMessageRef> {
220
1
    nom::error::context(
221
1
        "vote_message",
222
1
        nom::combinator::map(
223
1
            nom::sequence::tuple((
224
1
                nom::number::streaming::le_u64,
225
1
                nom::number::streaming::le_u64,
226
1
                message(block_number_bytes),
227
1
                nom::bytes::streaming::take(64u32),
228
1
                nom::bytes::streaming::take(32u32),
229
1
            )),
230
1
            |(round_number, set_id, message, signature, authority_public_key)| VoteMessageRef {
231
0
                round_number,
232
0
                set_id,
233
0
                message,
234
0
                signature: <&[u8; 64]>::try_from(signature).unwrap(),
235
0
                authority_public_key: <&[u8; 32]>::try_from(authority_public_key).unwrap(),
236
1
            },
Unexecuted instantiation: _RNCNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa12vote_message0B9_
Unexecuted instantiation: _RNCNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa12vote_message0B9_
237
1
        ),
238
1
    )
239
1
}
_RNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa12vote_message
Line
Count
Source
217
1
fn vote_message<'a>(
218
1
    block_number_bytes: usize,
219
1
) -> impl FnMut(&'a [u8]) -> nom::IResult<&[u8], VoteMessageRef> {
220
1
    nom::error::context(
221
1
        "vote_message",
222
1
        nom::combinator::map(
223
1
            nom::sequence::tuple((
224
1
                nom::number::streaming::le_u64,
225
1
                nom::number::streaming::le_u64,
226
1
                message(block_number_bytes),
227
1
                nom::bytes::streaming::take(64u32),
228
1
                nom::bytes::streaming::take(32u32),
229
1
            )),
230
1
            |(round_number, set_id, message, signature, authority_public_key)| VoteMessageRef {
231
                round_number,
232
                set_id,
233
                message,
234
                signature: <&[u8; 64]>::try_from(signature).unwrap(),
235
                authority_public_key: <&[u8; 32]>::try_from(authority_public_key).unwrap(),
236
1
            },
237
1
        ),
238
1
    )
239
1
}
Unexecuted instantiation: _RNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa12vote_message
240
241
1
fn message<'a>(
242
1
    block_number_bytes: usize,
243
1
) -> impl FnMut(&'a [u8]) -> nom::IResult<&[u8], MessageRef> {
244
1
    nom::error::context(
245
1
        "message",
246
1
        nom::branch::alt((
247
1
            nom::combinator::map(
248
1
                nom::sequence::preceded(
249
1
                    nom::bytes::streaming::tag(&[0]),
250
1
                    unsigned_prevote(block_number_bytes),
251
1
                ),
252
1
                MessageRef::Prevote,
253
1
            ),
254
1
            nom::combinator::map(
255
1
                nom::sequence::preceded(
256
1
                    nom::bytes::streaming::tag(&[1]),
257
1
                    unsigned_precommit(block_number_bytes),
258
1
                ),
259
1
                MessageRef::Precommit,
260
1
            ),
261
1
            nom::combinator::map(
262
1
                nom::sequence::preceded(
263
1
                    nom::bytes::streaming::tag(&[2]),
264
1
                    primary_propose(block_number_bytes),
265
1
                ),
266
1
                MessageRef::PrimaryPropose,
267
1
            ),
268
1
        )),
269
1
    )
270
1
}
_RNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa7message
Line
Count
Source
241
1
fn message<'a>(
242
1
    block_number_bytes: usize,
243
1
) -> impl FnMut(&'a [u8]) -> nom::IResult<&[u8], MessageRef> {
244
1
    nom::error::context(
245
1
        "message",
246
1
        nom::branch::alt((
247
1
            nom::combinator::map(
248
1
                nom::sequence::preceded(
249
1
                    nom::bytes::streaming::tag(&[0]),
250
1
                    unsigned_prevote(block_number_bytes),
251
1
                ),
252
1
                MessageRef::Prevote,
253
1
            ),
254
1
            nom::combinator::map(
255
1
                nom::sequence::preceded(
256
1
                    nom::bytes::streaming::tag(&[1]),
257
1
                    unsigned_precommit(block_number_bytes),
258
1
                ),
259
1
                MessageRef::Precommit,
260
1
            ),
261
1
            nom::combinator::map(
262
1
                nom::sequence::preceded(
263
1
                    nom::bytes::streaming::tag(&[2]),
264
1
                    primary_propose(block_number_bytes),
265
1
                ),
266
1
                MessageRef::PrimaryPropose,
267
1
            ),
268
1
        )),
269
1
    )
270
1
}
Unexecuted instantiation: _RNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa7message
271
272
1
fn unsigned_prevote<'a>(
273
1
    block_number_bytes: usize,
274
1
) -> impl FnMut(&'a [u8]) -> nom::IResult<&[u8], UnsignedPrevoteRef> {
275
1
    nom::error::context(
276
1
        "unsigned_prevote",
277
1
        nom::combinator::map(
278
1
            nom::sequence::tuple((
279
1
                nom::bytes::streaming::take(32u32),
280
1
                crate::util::nom_varsize_number_decode_u64(block_number_bytes),
281
1
            )),
282
1
            |(target_hash, target_number)| UnsignedPrevoteRef {
283
0
                target_hash: <&[u8; 32]>::try_from(target_hash).unwrap(),
284
0
                target_number,
285
1
            },
Unexecuted instantiation: _RNCNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa16unsigned_prevote0B9_
Unexecuted instantiation: _RNCNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa16unsigned_prevote0B9_
286
1
        ),
287
1
    )
288
1
}
_RNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa16unsigned_prevote
Line
Count
Source
272
1
fn unsigned_prevote<'a>(
273
1
    block_number_bytes: usize,
274
1
) -> impl FnMut(&'a [u8]) -> nom::IResult<&[u8], UnsignedPrevoteRef> {
275
1
    nom::error::context(
276
1
        "unsigned_prevote",
277
1
        nom::combinator::map(
278
1
            nom::sequence::tuple((
279
1
                nom::bytes::streaming::take(32u32),
280
1
                crate::util::nom_varsize_number_decode_u64(block_number_bytes),
281
1
            )),
282
1
            |(target_hash, target_number)| UnsignedPrevoteRef {
283
                target_hash: <&[u8; 32]>::try_from(target_hash).unwrap(),
284
                target_number,
285
1
            },
286
1
        ),
287
1
    )
288
1
}
Unexecuted instantiation: _RNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa16unsigned_prevote
289
290
1
fn unsigned_precommit<'a>(
291
1
    block_number_bytes: usize,
292
1
) -> impl FnMut(&'a [u8]) -> nom::IResult<&[u8], UnsignedPrecommitRef> {
293
1
    nom::error::context(
294
1
        "unsigned_precommit",
295
1
        nom::combinator::map(
296
1
            nom::sequence::tuple((
297
1
                nom::bytes::streaming::take(32u32),
298
1
                crate::util::nom_varsize_number_decode_u64(block_number_bytes),
299
1
            )),
300
1
            |(target_hash, target_number)| UnsignedPrecommitRef {
301
0
                target_hash: <&[u8; 32]>::try_from(target_hash).unwrap(),
302
0
                target_number,
303
1
            },
Unexecuted instantiation: _RNCNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa18unsigned_precommit0B9_
Unexecuted instantiation: _RNCNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa18unsigned_precommit0B9_
304
1
        ),
305
1
    )
306
1
}
_RNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa18unsigned_precommit
Line
Count
Source
290
1
fn unsigned_precommit<'a>(
291
1
    block_number_bytes: usize,
292
1
) -> impl FnMut(&'a [u8]) -> nom::IResult<&[u8], UnsignedPrecommitRef> {
293
1
    nom::error::context(
294
1
        "unsigned_precommit",
295
1
        nom::combinator::map(
296
1
            nom::sequence::tuple((
297
1
                nom::bytes::streaming::take(32u32),
298
1
                crate::util::nom_varsize_number_decode_u64(block_number_bytes),
299
1
            )),
300
1
            |(target_hash, target_number)| UnsignedPrecommitRef {
301
                target_hash: <&[u8; 32]>::try_from(target_hash).unwrap(),
302
                target_number,
303
1
            },
304
1
        ),
305
1
    )
306
1
}
Unexecuted instantiation: _RNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa18unsigned_precommit
307
308
1
fn primary_propose<'a>(
309
1
    block_number_bytes: usize,
310
1
) -> impl FnMut(&'a [u8]) -> nom::IResult<&[u8], PrimaryProposeRef> {
311
1
    nom::error::context(
312
1
        "primary_propose",
313
1
        nom::combinator::map(
314
1
            nom::sequence::tuple((
315
1
                nom::bytes::streaming::take(32u32),
316
1
                crate::util::nom_varsize_number_decode_u64(block_number_bytes),
317
1
            )),
318
1
            |(target_hash, target_number)| PrimaryProposeRef {
319
0
                target_hash: <&[u8; 32]>::try_from(target_hash).unwrap(),
320
0
                target_number,
321
1
            },
Unexecuted instantiation: _RNCNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa15primary_propose0B9_
Unexecuted instantiation: _RNCNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa15primary_propose0B9_
322
1
        ),
323
1
    )
324
1
}
_RNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa15primary_propose
Line
Count
Source
308
1
fn primary_propose<'a>(
309
1
    block_number_bytes: usize,
310
1
) -> impl FnMut(&'a [u8]) -> nom::IResult<&[u8], PrimaryProposeRef> {
311
1
    nom::error::context(
312
1
        "primary_propose",
313
1
        nom::combinator::map(
314
1
            nom::sequence::tuple((
315
1
                nom::bytes::streaming::take(32u32),
316
1
                crate::util::nom_varsize_number_decode_u64(block_number_bytes),
317
1
            )),
318
1
            |(target_hash, target_number)| PrimaryProposeRef {
319
                target_hash: <&[u8; 32]>::try_from(target_hash).unwrap(),
320
                target_number,
321
1
            },
322
1
        ),
323
1
    )
324
1
}
Unexecuted instantiation: _RNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa15primary_propose
325
326
1
fn neighbor_packet<'a>(
327
1
    block_number_bytes: usize,
328
1
) -> impl FnMut(&'a [u8]) -> nom::IResult<&[u8], NeighborPacket> {
329
1
    nom::error::context(
330
1
        "neighbor_packet",
331
1
        nom::combinator::map(
332
1
            nom::sequence::preceded(
333
1
                nom::bytes::streaming::tag(&[1]),
334
1
                nom::sequence::tuple((
335
1
                    nom::number::streaming::le_u64,
336
1
                    nom::number::streaming::le_u64,
337
1
                    crate::util::nom_varsize_number_decode_u64(block_number_bytes),
338
1
                )),
339
1
            ),
340
1
            |(round_number, set_id, commit_finalized_height)| NeighborPacket {
341
1
                round_number,
342
1
                set_id,
343
1
                commit_finalized_height,
344
1
            },
_RNCNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa15neighbor_packet0B9_
Line
Count
Source
340
1
            |(round_number, set_id, commit_finalized_height)| NeighborPacket {
341
1
                round_number,
342
1
                set_id,
343
1
                commit_finalized_height,
344
1
            },
Unexecuted instantiation: _RNCNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa15neighbor_packet0B9_
345
1
        ),
346
1
    )
347
1
}
_RNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa15neighbor_packet
Line
Count
Source
326
1
fn neighbor_packet<'a>(
327
1
    block_number_bytes: usize,
328
1
) -> impl FnMut(&'a [u8]) -> nom::IResult<&[u8], NeighborPacket> {
329
1
    nom::error::context(
330
1
        "neighbor_packet",
331
1
        nom::combinator::map(
332
1
            nom::sequence::preceded(
333
1
                nom::bytes::streaming::tag(&[1]),
334
1
                nom::sequence::tuple((
335
1
                    nom::number::streaming::le_u64,
336
1
                    nom::number::streaming::le_u64,
337
1
                    crate::util::nom_varsize_number_decode_u64(block_number_bytes),
338
1
                )),
339
1
            ),
340
1
            |(round_number, set_id, commit_finalized_height)| NeighborPacket {
341
                round_number,
342
                set_id,
343
                commit_finalized_height,
344
1
            },
345
1
        ),
346
1
    )
347
1
}
Unexecuted instantiation: _RNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa15neighbor_packet
348
349
0
fn catch_up_request(bytes: &[u8]) -> nom::IResult<&[u8], CatchUpRequest> {
350
0
    nom::error::context(
351
0
        "catch_up_request",
352
0
        nom::combinator::map(
353
0
            nom::sequence::tuple((
354
0
                nom::number::streaming::le_u64,
355
0
                nom::number::streaming::le_u64,
356
0
            )),
357
0
            |(round_number, set_id)| CatchUpRequest {
358
0
                round_number,
359
0
                set_id,
360
0
            },
Unexecuted instantiation: _RNCNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa16catch_up_request0B9_
Unexecuted instantiation: _RNCNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa16catch_up_request0B9_
361
0
        ),
362
0
    )(bytes)
363
0
}
Unexecuted instantiation: _RNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa16catch_up_request
Unexecuted instantiation: _RNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa16catch_up_request
364
365
1
fn catch_up<'a>(
366
1
    block_number_bytes: usize,
367
1
) -> impl FnMut(&'a [u8]) -> nom::IResult<&[u8], CatchUpRef> {
368
1
    nom::error::context(
369
1
        "catch_up",
370
1
        nom::combinator::map(
371
1
            nom::sequence::tuple((
372
1
                nom::number::streaming::le_u64,
373
1
                nom::number::streaming::le_u64,
374
1
                nom::combinator::flat_map(crate::util::nom_scale_compact_usize, move |num_elems| {
375
0
                    nom::multi::many_m_n(num_elems, num_elems, prevote(block_number_bytes))
376
1
                }),
Unexecuted instantiation: _RNCNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa8catch_up0B9_
Unexecuted instantiation: _RNCNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa8catch_up0B9_
377
1
                nom::combinator::flat_map(crate::util::nom_scale_compact_usize, move |num_elems| {
378
0
                    nom::multi::many_m_n(num_elems, num_elems, move |s| {
379
0
                        crate::finality::decode::PrecommitRef::decode_partial(s, block_number_bytes)
380
0
                            .map(|(a, b)| (b, a))
Unexecuted instantiation: _RNCNCNCNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa8catch_ups_000Bd_
Unexecuted instantiation: _RNCNCNCNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa8catch_ups_000Bd_
381
0
                            .map_err(|_| {
382
0
                                nom::Err::Failure(nom::error::make_error(
383
0
                                    s,
384
0
                                    nom::error::ErrorKind::Verify,
385
0
                                ))
386
0
                            })
Unexecuted instantiation: _RNCNCNCNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa8catch_ups_00s_0Bd_
Unexecuted instantiation: _RNCNCNCNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa8catch_ups_00s_0Bd_
387
0
                    })
Unexecuted instantiation: _RNCNCNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa8catch_ups_00Bb_
Unexecuted instantiation: _RNCNCNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa8catch_ups_00Bb_
388
1
                }),
Unexecuted instantiation: _RNCNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa8catch_ups_0B9_
Unexecuted instantiation: _RNCNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa8catch_ups_0B9_
389
1
                nom::bytes::streaming::take(32u32),
390
1
                crate::util::nom_varsize_number_decode_u64(block_number_bytes),
391
1
            )),
392
1
            |(set_id, round_number, prevotes, precommits, base_hash, base_number)| CatchUpRef {
393
0
                set_id,
394
0
                round_number,
395
0
                prevotes,
396
0
                precommits,
397
0
                base_hash: <&[u8; 32]>::try_from(base_hash).unwrap(),
398
0
                base_number,
399
1
            },
Unexecuted instantiation: _RNCNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa8catch_ups0_0B9_
Unexecuted instantiation: _RNCNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa8catch_ups0_0B9_
400
1
        ),
401
1
    )
402
1
}
_RNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa8catch_up
Line
Count
Source
365
1
fn catch_up<'a>(
366
1
    block_number_bytes: usize,
367
1
) -> impl FnMut(&'a [u8]) -> nom::IResult<&[u8], CatchUpRef> {
368
1
    nom::error::context(
369
1
        "catch_up",
370
1
        nom::combinator::map(
371
1
            nom::sequence::tuple((
372
1
                nom::number::streaming::le_u64,
373
1
                nom::number::streaming::le_u64,
374
1
                nom::combinator::flat_map(crate::util::nom_scale_compact_usize, move |num_elems| {
375
                    nom::multi::many_m_n(num_elems, num_elems, prevote(block_number_bytes))
376
1
                }),
377
1
                nom::combinator::flat_map(crate::util::nom_scale_compact_usize, move |num_elems| {
378
                    nom::multi::many_m_n(num_elems, num_elems, move |s| {
379
                        crate::finality::decode::PrecommitRef::decode_partial(s, block_number_bytes)
380
                            .map(|(a, b)| (b, a))
381
                            .map_err(|_| {
382
                                nom::Err::Failure(nom::error::make_error(
383
                                    s,
384
                                    nom::error::ErrorKind::Verify,
385
                                ))
386
                            })
387
                    })
388
1
                }),
389
1
                nom::bytes::streaming::take(32u32),
390
1
                crate::util::nom_varsize_number_decode_u64(block_number_bytes),
391
1
            )),
392
1
            |(set_id, round_number, prevotes, precommits, base_hash, base_number)| CatchUpRef {
393
                set_id,
394
                round_number,
395
                prevotes,
396
                precommits,
397
                base_hash: <&[u8; 32]>::try_from(base_hash).unwrap(),
398
                base_number,
399
1
            },
400
1
        ),
401
1
    )
402
1
}
Unexecuted instantiation: _RNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa8catch_up
403
404
0
fn prevote<'a>(
405
0
    block_number_bytes: usize,
406
0
) -> impl FnMut(&'a [u8]) -> nom::IResult<&[u8], PrevoteRef> {
407
0
    nom::error::context(
408
0
        "prevote",
409
0
        nom::combinator::map(
410
0
            nom::sequence::tuple((
411
0
                nom::bytes::streaming::take(32u32),
412
0
                crate::util::nom_varsize_number_decode_u64(block_number_bytes),
413
0
                nom::bytes::streaming::take(64u32),
414
0
                nom::bytes::streaming::take(32u32),
415
0
            )),
416
0
            |(target_hash, target_number, signature, authority_public_key)| PrevoteRef {
417
0
                target_hash: <&[u8; 32]>::try_from(target_hash).unwrap(),
418
0
                target_number,
419
0
                signature: <&[u8; 64]>::try_from(signature).unwrap(),
420
0
                authority_public_key: <&[u8; 32]>::try_from(authority_public_key).unwrap(),
421
0
            },
Unexecuted instantiation: _RNCNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa7prevote0B9_
Unexecuted instantiation: _RNCNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa7prevote0B9_
422
0
        ),
423
0
    )
424
0
}
Unexecuted instantiation: _RNvNtNtNtCsN16ciHI6Qf_7smoldot7network5codec7grandpa7prevote
Unexecuted instantiation: _RNvNtNtNtCseuYC0Zibziv_7smoldot7network5codec7grandpa7prevote
425
426
#[cfg(test)]
427
mod tests {
428
    #[test]
429
1
    fn basic_decode_neighbor() {
430
1
        let actual = super::decode_grandpa_notification(
431
1
            &[
432
1
                2, 1, 87, 14, 0, 0, 0, 0, 0, 0, 162, 13, 0, 0, 0, 0, 0, 0, 49, 231, 77, 0,
433
1
            ],
434
1
            4,
435
1
        )
436
1
        .unwrap();
437
1
438
1
        let expected = super::GrandpaNotificationRef::Neighbor(super::NeighborPacket {
439
1
            round_number: 3671,
440
1
            set_id: 3490,
441
1
            commit_finalized_height: 5_105_457,
442
1
        });
443
1
444
1
        assert_eq!(actual, expected);
445
1
    }
446
}