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/network/codec.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
//! Encoding and decoding of messages of the protocols used by Polkadot/Substrate.
19
20
// TODO: expand docs
21
22
use alloc::{borrow::Cow, string::String};
23
use core::{fmt, iter};
24
25
// Implementation note: each protocol goes into a different sub-module whose content is
26
// re-exported here.
27
28
mod block_announces;
29
mod block_request;
30
mod grandpa;
31
mod grandpa_warp_sync;
32
mod identify;
33
mod kademlia;
34
mod state_request;
35
mod storage_call_proof;
36
37
pub use self::block_announces::*;
38
pub use self::block_request::*;
39
pub use self::grandpa::*;
40
pub use self::grandpa_warp_sync::*;
41
pub use self::identify::*;
42
pub use self::kademlia::*;
43
pub use self::state_request::*;
44
pub use self::storage_call_proof::*;
45
46
/// Name of a protocol that is part of the Substrate/Polkadot networking.
47
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
48
pub enum ProtocolName<'a> {
49
    Identify,
50
    Ping,
51
    BlockAnnounces {
52
        genesis_hash: [u8; 32],
53
        fork_id: Option<&'a str>,
54
    },
55
    Transactions {
56
        genesis_hash: [u8; 32],
57
        fork_id: Option<&'a str>,
58
    },
59
    Grandpa {
60
        genesis_hash: [u8; 32],
61
        fork_id: Option<&'a str>,
62
    },
63
    Sync {
64
        genesis_hash: [u8; 32],
65
        fork_id: Option<&'a str>,
66
    },
67
    Light {
68
        genesis_hash: [u8; 32],
69
        fork_id: Option<&'a str>,
70
    },
71
    Kad {
72
        genesis_hash: [u8; 32],
73
        fork_id: Option<&'a str>,
74
    },
75
    SyncWarp {
76
        genesis_hash: [u8; 32],
77
        fork_id: Option<&'a str>,
78
    },
79
    State {
80
        genesis_hash: [u8; 32],
81
        fork_id: Option<&'a str>,
82
    },
83
}
84
85
impl<'a> fmt::Debug for ProtocolName<'a> {
86
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
87
0
        fmt::Display::fmt(self, f)
88
0
    }
Unexecuted instantiation: _RNvXNtNtCsjlkOsLH0Zfj_7smoldot7network5codecNtB2_12ProtocolNameNtNtCs1p5UDGgVI4d_4core3fmt5Debug3fmt
Unexecuted instantiation: _RNvXNtNtCsc1ywvx6YAnK_7smoldot7network5codecNtB2_12ProtocolNameNtNtCs1p5UDGgVI4d_4core3fmt5Debug3fmt
89
}
90
91
impl<'a> fmt::Display for ProtocolName<'a> {
92
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
93
0
        for chunk in encode_protocol_name(*self) {
94
0
            f.write_str(chunk.as_ref())?;
95
        }
96
0
        Ok(())
97
0
    }
Unexecuted instantiation: _RNvXs_NtNtCsjlkOsLH0Zfj_7smoldot7network5codecNtB4_12ProtocolNameNtNtCs1p5UDGgVI4d_4core3fmt7Display3fmt
Unexecuted instantiation: _RNvXs_NtNtCsc1ywvx6YAnK_7smoldot7network5codecNtB4_12ProtocolNameNtNtCs1p5UDGgVI4d_4core3fmt7Display3fmt
98
}
99
100
/// Turns a [`ProtocolName`] into its string version. Returns a list of objects that, when
101
/// concatenated together, forms the string version of the [`ProtocolName`].
102
0
pub fn encode_protocol_name(protocol: ProtocolName) -> impl Iterator<Item = impl AsRef<str>> {
103
0
    let (genesis_hash, fork_id, base_protocol_name) = match protocol {
104
0
        ProtocolName::Identify => return either::Left(iter::once(Cow::Borrowed("/ipfs/id/1.0.0"))),
105
0
        ProtocolName::Ping => return either::Left(iter::once(Cow::Borrowed("/ipfs/ping/1.0.0"))),
106
        ProtocolName::BlockAnnounces {
107
0
            genesis_hash,
108
0
            fork_id,
109
0
        } => (genesis_hash, fork_id, "block-announces/1"),
110
        ProtocolName::Transactions {
111
0
            genesis_hash,
112
0
            fork_id,
113
0
        } => (genesis_hash, fork_id, "transactions/1"),
114
        ProtocolName::Grandpa {
115
0
            genesis_hash,
116
0
            fork_id,
117
0
        } => (genesis_hash, fork_id, "grandpa/1"),
118
        ProtocolName::Sync {
119
0
            genesis_hash,
120
0
            fork_id,
121
0
        } => (genesis_hash, fork_id, "sync/2"),
122
        ProtocolName::Light {
123
0
            genesis_hash,
124
0
            fork_id,
125
0
        } => (genesis_hash, fork_id, "light/2"),
126
        ProtocolName::Kad {
127
0
            genesis_hash,
128
0
            fork_id,
129
0
        } => (genesis_hash, fork_id, "kad"),
130
        ProtocolName::SyncWarp {
131
0
            genesis_hash,
132
0
            fork_id,
133
0
        } => (genesis_hash, fork_id, "sync/warp"),
134
        ProtocolName::State {
135
0
            genesis_hash,
136
0
            fork_id,
137
0
        } => (genesis_hash, fork_id, "state/2"),
138
    };
139
140
0
    let genesis_hash = hex::encode(genesis_hash);
141
142
0
    if let Some(fork_id) = fork_id {
143
0
        either::Right(either::Right(
144
0
            [
145
0
                Cow::Borrowed("/"),
146
0
                Cow::Owned(genesis_hash),
147
0
                Cow::Borrowed("/"),
148
0
                Cow::Borrowed(fork_id),
149
0
                Cow::Borrowed("/"),
150
0
                Cow::Borrowed(base_protocol_name),
151
0
            ]
152
0
            .into_iter(),
153
0
        ))
154
    } else {
155
0
        either::Right(either::Left(
156
0
            [
157
0
                Cow::Borrowed("/"),
158
0
                Cow::Owned(genesis_hash),
159
0
                Cow::Borrowed("/"),
160
0
                Cow::Borrowed(base_protocol_name),
161
0
            ]
162
0
            .into_iter(),
163
0
        ))
164
    }
165
0
}
Unexecuted instantiation: _RNvNtNtCsjlkOsLH0Zfj_7smoldot7network5codec20encode_protocol_name
Unexecuted instantiation: _RNvNtNtCsc1ywvx6YAnK_7smoldot7network5codec20encode_protocol_name
166
167
/// Turns a [`ProtocolName`] into a string.
168
0
pub fn encode_protocol_name_string(protocol: ProtocolName) -> String {
169
0
    encode_protocol_name(protocol).fold(String::with_capacity(128), |mut a, b| {
170
0
        a.push_str(b.as_ref());
171
0
        a
172
0
    })
Unexecuted instantiation: _RNCNvNtNtCsjlkOsLH0Zfj_7smoldot7network5codec27encode_protocol_name_string0B7_
Unexecuted instantiation: _RNCNvNtNtCsc1ywvx6YAnK_7smoldot7network5codec27encode_protocol_name_string0B7_
173
0
}
Unexecuted instantiation: _RNvNtNtCsjlkOsLH0Zfj_7smoldot7network5codec27encode_protocol_name_string
Unexecuted instantiation: _RNvNtNtCsc1ywvx6YAnK_7smoldot7network5codec27encode_protocol_name_string
174
175
/// Decodes a protocol name into its components.
176
///
177
/// Returns an error if the protocol name isn't recognized.
178
0
pub fn decode_protocol_name(name: &str) -> Result<ProtocolName, ()> {
179
0
    nom::Parser::parse(
180
0
        &mut nom::combinator::all_consuming(nom::branch::alt((
181
0
            nom::combinator::map(nom::bytes::complete::tag("/ipfs/id/1.0.0"), |_| {
182
0
                ProtocolName::Identify
183
0
            }),
Unexecuted instantiation: _RNCNvNtNtCsjlkOsLH0Zfj_7smoldot7network5codec20decode_protocol_name0B7_
Unexecuted instantiation: _RNCNvNtNtCsc1ywvx6YAnK_7smoldot7network5codec20decode_protocol_name0B7_
184
0
            nom::combinator::map(nom::bytes::complete::tag("/ipfs/ping/1.0.0"), |_| {
185
0
                ProtocolName::Ping
186
0
            }),
Unexecuted instantiation: _RNCNvNtNtCsjlkOsLH0Zfj_7smoldot7network5codec20decode_protocol_names_0B7_
Unexecuted instantiation: _RNCNvNtNtCsc1ywvx6YAnK_7smoldot7network5codec20decode_protocol_names_0B7_
187
0
            nom::combinator::map(
188
0
                (
189
0
                    nom::bytes::complete::tag("/"),
190
0
                    genesis_hash,
191
0
                    nom::bytes::complete::tag("/"),
192
0
                    protocol_ty,
193
0
                ),
194
0
                |(_, genesis_hash, _, protocol_ty)| {
195
0
                    protocol_ty_to_real_protocol(protocol_ty, genesis_hash, None)
196
0
                },
Unexecuted instantiation: _RNCNvNtNtCsjlkOsLH0Zfj_7smoldot7network5codec20decode_protocol_names0_0B7_
Unexecuted instantiation: _RNCNvNtNtCsc1ywvx6YAnK_7smoldot7network5codec20decode_protocol_names0_0B7_
197
            ),
198
0
            nom::combinator::map(
199
0
                (
200
0
                    nom::bytes::complete::tag("/"),
201
0
                    genesis_hash,
202
0
                    nom::bytes::complete::tag("/"),
203
0
                    nom::bytes::complete::take_until("/"),
204
0
                    nom::bytes::complete::tag("/"),
205
0
                    protocol_ty,
206
0
                ),
207
0
                |(_, genesis_hash, _, fork_id, _, protocol_ty)| {
208
0
                    protocol_ty_to_real_protocol(protocol_ty, genesis_hash, Some(fork_id))
209
0
                },
Unexecuted instantiation: _RNCNvNtNtCsjlkOsLH0Zfj_7smoldot7network5codec20decode_protocol_names1_0B7_
Unexecuted instantiation: _RNCNvNtNtCsc1ywvx6YAnK_7smoldot7network5codec20decode_protocol_names1_0B7_
210
            ),
211
        ))),
212
0
        name,
213
    )
214
0
    .map(|(_, parse_result)| parse_result)
215
0
    .map_err(|_| ())
216
0
}
Unexecuted instantiation: _RNvNtNtCsjlkOsLH0Zfj_7smoldot7network5codec20decode_protocol_name
Unexecuted instantiation: _RNvNtNtCsc1ywvx6YAnK_7smoldot7network5codec20decode_protocol_name
217
218
0
fn genesis_hash(name: &str) -> nom::IResult<&str, [u8; 32]> {
219
0
    nom::Parser::parse(
220
0
        &mut nom::combinator::map_opt(nom::bytes::complete::take(64u32), |hash| {
221
0
            hex::decode(hash)
222
0
                .ok()
223
0
                .map(|hash| <[u8; 32]>::try_from(hash).unwrap_or_else(|_| unreachable!()))
Unexecuted instantiation: _RNCNCNvNtNtCsjlkOsLH0Zfj_7smoldot7network5codec12genesis_hash00B9_
Unexecuted instantiation: _RNCNCNvNtNtCsc1ywvx6YAnK_7smoldot7network5codec12genesis_hash00B9_
224
0
        }),
Unexecuted instantiation: _RNCNvNtNtCsjlkOsLH0Zfj_7smoldot7network5codec12genesis_hash0B7_
Unexecuted instantiation: _RNCNvNtNtCsc1ywvx6YAnK_7smoldot7network5codec12genesis_hash0B7_
225
0
        name,
226
    )
227
0
}
Unexecuted instantiation: _RNvNtNtCsjlkOsLH0Zfj_7smoldot7network5codec12genesis_hash
Unexecuted instantiation: _RNvNtNtCsc1ywvx6YAnK_7smoldot7network5codec12genesis_hash
228
229
enum ProtocolTy {
230
    BlockAnnounces,
231
    Transactions,
232
    Grandpa,
233
    Sync,
234
    Light,
235
    Kad,
236
    SyncWarp,
237
    State,
238
}
239
240
0
fn protocol_ty(name: &str) -> nom::IResult<&str, ProtocolTy> {
241
0
    nom::Parser::parse(
242
0
        &mut nom::branch::alt((
243
0
            nom::combinator::map(nom::bytes::complete::tag("block-announces/1"), |_| {
244
0
                ProtocolTy::BlockAnnounces
245
0
            }),
Unexecuted instantiation: _RNCNvNtNtCsjlkOsLH0Zfj_7smoldot7network5codec11protocol_ty0B7_
Unexecuted instantiation: _RNCNvNtNtCsc1ywvx6YAnK_7smoldot7network5codec11protocol_ty0B7_
246
0
            nom::combinator::map(nom::bytes::complete::tag("transactions/1"), |_| {
247
0
                ProtocolTy::Transactions
248
0
            }),
Unexecuted instantiation: _RNCNvNtNtCsjlkOsLH0Zfj_7smoldot7network5codec11protocol_tys_0B7_
Unexecuted instantiation: _RNCNvNtNtCsc1ywvx6YAnK_7smoldot7network5codec11protocol_tys_0B7_
249
0
            nom::combinator::map(nom::bytes::complete::tag("grandpa/1"), |_| {
250
0
                ProtocolTy::Grandpa
251
0
            }),
Unexecuted instantiation: _RNCNvNtNtCsjlkOsLH0Zfj_7smoldot7network5codec11protocol_tys0_0B7_
Unexecuted instantiation: _RNCNvNtNtCsc1ywvx6YAnK_7smoldot7network5codec11protocol_tys0_0B7_
252
0
            nom::combinator::map(nom::bytes::complete::tag("sync/2"), |_| ProtocolTy::Sync),
253
0
            nom::combinator::map(nom::bytes::complete::tag("light/2"), |_| ProtocolTy::Light),
254
0
            nom::combinator::map(nom::bytes::complete::tag("kad"), |_| ProtocolTy::Kad),
255
0
            nom::combinator::map(nom::bytes::complete::tag("sync/warp"), |_| {
256
0
                ProtocolTy::SyncWarp
257
0
            }),
Unexecuted instantiation: _RNCNvNtNtCsjlkOsLH0Zfj_7smoldot7network5codec11protocol_tys4_0B7_
Unexecuted instantiation: _RNCNvNtNtCsc1ywvx6YAnK_7smoldot7network5codec11protocol_tys4_0B7_
258
0
            nom::combinator::map(nom::bytes::complete::tag("state/2"), |_| ProtocolTy::State),
259
        )),
260
0
        name,
261
    )
262
0
}
Unexecuted instantiation: _RNvNtNtCsjlkOsLH0Zfj_7smoldot7network5codec11protocol_ty
Unexecuted instantiation: _RNvNtNtCsc1ywvx6YAnK_7smoldot7network5codec11protocol_ty
263
264
0
fn protocol_ty_to_real_protocol(
265
0
    ty: ProtocolTy,
266
0
    genesis_hash: [u8; 32],
267
0
    fork_id: Option<&str>,
268
0
) -> ProtocolName {
269
0
    match ty {
270
0
        ProtocolTy::BlockAnnounces => ProtocolName::BlockAnnounces {
271
0
            genesis_hash,
272
0
            fork_id,
273
0
        },
274
0
        ProtocolTy::Transactions => ProtocolName::Transactions {
275
0
            genesis_hash,
276
0
            fork_id,
277
0
        },
278
0
        ProtocolTy::Grandpa => ProtocolName::Grandpa {
279
0
            genesis_hash,
280
0
            fork_id,
281
0
        },
282
0
        ProtocolTy::Sync => ProtocolName::Sync {
283
0
            genesis_hash,
284
0
            fork_id,
285
0
        },
286
0
        ProtocolTy::Light => ProtocolName::Light {
287
0
            genesis_hash,
288
0
            fork_id,
289
0
        },
290
0
        ProtocolTy::Kad => ProtocolName::Kad {
291
0
            genesis_hash,
292
0
            fork_id,
293
0
        },
294
0
        ProtocolTy::SyncWarp => ProtocolName::SyncWarp {
295
0
            genesis_hash,
296
0
            fork_id,
297
0
        },
298
0
        ProtocolTy::State => ProtocolName::State {
299
0
            genesis_hash,
300
0
            fork_id,
301
0
        },
302
    }
303
0
}
Unexecuted instantiation: _RNvNtNtCsjlkOsLH0Zfj_7smoldot7network5codec28protocol_ty_to_real_protocol
Unexecuted instantiation: _RNvNtNtCsc1ywvx6YAnK_7smoldot7network5codec28protocol_ty_to_real_protocol
304
305
// TODO: tests for the protocol names