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/sync/all_forks.rs
Line
Count
Source
1
// Substrate-lite
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
//! *All-forks* header and body syncing.
19
//!
20
//! # Overview
21
//!
22
//! This state machine holds:
23
//!
24
//! - A list of sources of blocks, maintained by the API user.
25
//!  - For each source, a list of blocks hashes known by the source.
26
//! - The latest known finalized block.
27
//! - A tree of valid non-finalized blocks that all descend from the latest known finalized block.
28
//! - (if full mode) A list of block headers whose body is currently being downloaded.
29
//! - A list of block header waiting to be verified and whose ancestry with the latest finalized
30
//!   block is currently unknown.
31
//!
32
//! The state machine has the objective to synchronize the tree of non-finalized blocks with its
33
//! equivalent on the sources added by the API user.
34
//!
35
//! Because it is not possible to predict which block in this tree is going to be finalized in
36
//! the future, the entire tree needs to be synchronized.
37
//!
38
//! > **Example**: If the latest finalized block is block number 4, and the tree contains blocks
39
//! >              5, 6, and 7, and a source announces a block 5 that is different from the
40
//! >              locally-known block 5, a block request will be emitted for this block 5, even
41
//! >              if it is certain that this "other" block 5 will not become the local best
42
//! >              block. This is necessary in case it is this other block 5 that will end up
43
//! >              being finalized.
44
//!
45
//! # Bounded and unbounded containers
46
//!
47
//! It is important to limit the memory usage of this state machine no matter how the
48
//! potentially-malicious sources behave.
49
//!
50
//! The state in this state machine can be put into three categories:
51
//!
52
//! - Each source of blocks has a certain fixed-size state associated to it (containing for
53
//!   instance its best block number and height). Each source also has up to one in-flight
54
//!   request, which might incur more memory usage. Managing this additional request is out of
55
//!   scope of this module. The user of this module is expected to limit the number of
56
//!   simultaneous sources.
57
//!
58
//! - A set of verified blocks that descend from the latest finalized block. This set is
59
//!   unbounded. The consensus and finalization algorithms of the chain are supposed to limit
60
//!   the number of possible blocks in this set.
61
//!
62
//! - A set of blocks that can't be verified yet. Receiving a block announce inserts an element
63
//!   in this set. In order to handle situations where a malicious source announces lots of
64
//!   invalid blocks, this set must be bounded. Once it has reached a certain size, the blocks
65
//!   with the highest block number are discarded if their parent is also in this set or being
66
//!   downloaded from a source.
67
//!
68
//! Consequently, and assuming that the number of simultaneous sources is bounded, and that
69
//! the consensus and finalization algorithms of the chain are properly configured, malicious
70
//! sources can't indefinitely grow the state in this state machine.
71
//! Malicious sources, however, can potentially increase the number of block requests required to
72
//! download a long fork. This is, at most, an annoyance, and not a vulnerability.
73
//!
74
75
// TODO: finish ^
76
77
use crate::{
78
    chain::{blocks_tree, chain_information},
79
    finality::decode,
80
    header, verify,
81
};
82
83
use alloc::{borrow::ToOwned as _, boxed::Box, vec::Vec};
84
use core::{cmp, mem, num::NonZero, ops, time::Duration};
85
86
mod disjoint;
87
mod pending_blocks;
88
89
pub mod sources;
90
91
pub use pending_blocks::{RequestId, RequestParams, SourceId};
92
93
/// Configuration for the [`AllForksSync`].
94
#[derive(Debug)]
95
pub struct Config {
96
    /// Information about the latest finalized block and its ancestors.
97
    pub chain_information: chain_information::ValidChainInformation,
98
99
    /// Number of bytes used when encoding/decoding the block number. Influences how various data
100
    /// structures should be parsed.
101
    pub block_number_bytes: usize,
102
103
    /// If `false`, blocks containing digest items with an unknown consensus engine will fail to
104
    /// verify.
105
    ///
106
    /// Note that blocks must always contain digest items that are relevant to the current
107
    /// consensus algorithm. This option controls what happens when blocks contain additional
108
    /// digest items that aren't recognized by the implementation.
109
    ///
110
    /// Passing `true` can lead to blocks being considered as valid when they shouldn't, as these
111
    /// additional digest items could have some logic attached to them that restricts which blocks
112
    /// are valid and which are not.
113
    ///
114
    /// However, since a recognized consensus engine must always be present, both `true` and
115
    /// `false` guarantee that the number of authorable blocks over the network is bounded.
116
    pub allow_unknown_consensus_engines: bool,
117
118
    /// Pre-allocated capacity for the number of block sources.
119
    pub sources_capacity: usize,
120
121
    /// Pre-allocated capacity for the number of blocks between the finalized block and the head
122
    /// of the chain.
123
    ///
124
    /// Should be set to the maximum number of block between two consecutive justifications.
125
    pub blocks_capacity: usize,
126
127
    /// Maximum number of blocks of unknown ancestry to keep in memory. A good default is 1024.
128
    ///
129
    /// When a potential long fork is detected, its blocks are downloaded progressively in
130
    /// descending order until a common ancestor is found.
131
    /// Unfortunately, an attack could generate fake very long forks in order to make the node
132
    /// consume a lot of memory keeping track of the blocks in that fork.
133
    /// In order to avoid this, a limit is added to the number of blocks of unknown ancestry that
134
    /// are kept in memory.
135
    ///
136
    /// Note that the download of long forks will always work no matter this limit. In the worst
137
    /// case scenario, the same blocks will be downloaded multiple times. There is an implicit
138
    /// minimum size equal to the number of sources that have been added to the state machine.
139
    ///
140
    /// Increasing this value has no drawback, except for increasing the maximum possible memory
141
    /// consumption of this state machine.
142
    //
143
    // Implementation note: the size of `disjoint_headers` can temporarily grow above this limit
144
    // due to the internal processing of the state machine.
145
    pub max_disjoint_headers: usize,
146
147
    /// Maximum number of simultaneous pending requests made towards the same block.
148
    ///
149
    /// Should be set according to the failure rate of requests. For example if requests have a
150
    /// `10%` chance of failing, then setting to value to `2` gives a `1%` chance that downloading
151
    /// this block will overall fail and has to be attempted again.
152
    ///
153
    /// Also keep in mind that sources might maliciously take a long time to answer requests. A
154
    /// higher value makes it possible to reduce the risks of the syncing taking a long time
155
    /// because of malicious sources.
156
    ///
157
    /// The higher the value, the more bandwidth is potentially wasted.
158
    pub max_requests_per_block: NonZero<u32>,
159
160
    /// If true, the body of a block is downloaded (if necessary) before a
161
    /// [`ProcessOne::BlockVerify`] is generated.
162
    pub download_bodies: bool,
163
}
164
165
pub struct AllForksSync<TBl, TRq, TSrc> {
166
    /// Data structure containing the non-finalized blocks.
167
    chain: blocks_tree::NonFinalizedTree<TBl>,
168
169
    /// Extra fields. In a separate structure in order to be moved around.
170
    inner: Box<Inner<TBl, TRq, TSrc>>,
171
}
172
173
/// Extra fields. In a separate structure in order to be moved around.
174
struct Inner<TBl, TRq, TSrc> {
175
    blocks: pending_blocks::PendingBlocks<PendingBlock<TBl>, TRq, Source<TSrc>>,
176
}
177
178
struct PendingBlock<TBl> {
179
    header: Option<Vec<u8>>,
180
    /// SCALE-encoded extrinsics of the block. `None` if unknown. Only ever filled
181
    /// if [`Config::download_bodies`] was `true`.
182
    body: Option<Vec<Vec<u8>>>,
183
    user_data: TBl,
184
}
185
186
struct Source<TSrc> {
187
    /// Each source stores between zero and two finality proofs that haven't been verified yet.
188
    ///
189
    /// If more than two finality proofs are received from the same source, only the one with the
190
    /// lowest target block and the one with the highest target block are kept in memory. This is
191
    /// done in order to have a maximum bound to the amount of memory that is allocated per source
192
    /// and avoid DoS attack vectors.
193
    ///
194
    /// The finality proof with the highest target block is the "best" finality proof. However,
195
    /// keeping the finality proof with the lowest target block guarantees that, assuming the
196
    /// source isn't malicious, we will able to make *some* progress in the finality.
197
    unverified_finality_proofs: SourcePendingJustificationProofs,
198
199
    /// Height of the highest finalized block according to that source. `0` if unknown.
200
    finalized_block_number: u64,
201
202
    /// Similar to [`Source::unverified_finality_proofs`]. Contains proofs that have been checked
203
    /// and have been determined to not be verifiable right now.
204
    pending_finality_proofs: SourcePendingJustificationProofs,
205
206
    /// Opaque data chosen by the API user.
207
    user_data: TSrc,
208
}
209
210
enum SourcePendingJustificationProofs {
211
    None,
212
    One {
213
        target_height: u64,
214
        proof: FinalityProofs,
215
    },
216
    Two {
217
        low_target_height: u64,
218
        low_proof: FinalityProofs,
219
        high_target_height: u64,
220
        high_proof: FinalityProofs,
221
    },
222
}
223
224
impl SourcePendingJustificationProofs {
225
21
    fn is_none(&self) -> bool {
226
21
        
matches!0
(self, SourcePendingJustificationProofs::None)
227
21
    }
Unexecuted instantiation: _RNvMNtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksNtB2_32SourcePendingJustificationProofs7is_none
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksNtB2_32SourcePendingJustificationProofs7is_none
Line
Count
Source
225
21
    fn is_none(&self) -> bool {
226
21
        
matches!0
(self, SourcePendingJustificationProofs::None)
227
21
    }
228
229
0
    fn insert(&mut self, new_target_height: u64, new_proof: FinalityProofs) {
230
        // An empty list of justifications is an invalid state.
231
0
        debug_assert!(
232
0
            !matches!(&new_proof, FinalityProofs::Justifications(list) if list.is_empty())
233
        );
234
235
0
        match mem::replace(self, SourcePendingJustificationProofs::None) {
236
0
            SourcePendingJustificationProofs::None => {
237
0
                *self = SourcePendingJustificationProofs::One {
238
0
                    target_height: new_target_height,
239
0
                    proof: new_proof,
240
0
                };
241
0
            }
242
            SourcePendingJustificationProofs::One {
243
0
                target_height,
244
0
                proof,
245
0
            } if target_height < new_target_height => {
246
0
                *self = SourcePendingJustificationProofs::Two {
247
0
                    low_target_height: target_height,
248
0
                    low_proof: proof,
249
0
                    high_target_height: new_target_height,
250
0
                    high_proof: new_proof,
251
0
                };
252
0
            }
253
            SourcePendingJustificationProofs::One {
254
0
                target_height,
255
0
                proof,
256
0
            } if target_height > new_target_height => {
257
0
                *self = SourcePendingJustificationProofs::Two {
258
0
                    low_target_height: new_target_height,
259
0
                    low_proof: new_proof,
260
0
                    high_target_height: target_height,
261
0
                    high_proof: proof,
262
0
                };
263
0
            }
264
0
            SourcePendingJustificationProofs::One { .. } => {
265
0
                *self = SourcePendingJustificationProofs::One {
266
0
                    target_height: new_target_height,
267
0
                    proof: new_proof,
268
0
                };
269
0
            }
270
            SourcePendingJustificationProofs::Two {
271
0
                high_target_height,
272
0
                low_proof,
273
0
                low_target_height,
274
                ..
275
0
            } if new_target_height >= high_target_height => {
276
0
                *self = SourcePendingJustificationProofs::Two {
277
0
                    high_proof: new_proof,
278
0
                    high_target_height: new_target_height,
279
0
                    low_proof,
280
0
                    low_target_height,
281
0
                };
282
0
            }
283
            SourcePendingJustificationProofs::Two {
284
0
                high_proof,
285
0
                high_target_height,
286
0
                low_target_height,
287
                ..
288
0
            } if new_target_height <= low_target_height => {
289
0
                *self = SourcePendingJustificationProofs::Two {
290
0
                    high_proof,
291
0
                    high_target_height,
292
0
                    low_proof: new_proof,
293
0
                    low_target_height: new_target_height,
294
0
                };
295
0
            }
296
0
            val @ SourcePendingJustificationProofs::Two { .. } => {
297
0
                *self = val;
298
0
            }
299
        }
300
0
    }
Unexecuted instantiation: _RNvMNtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksNtB2_32SourcePendingJustificationProofs6insert
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksNtB2_32SourcePendingJustificationProofs6insert
301
302
0
    fn take_one(&mut self) -> Option<FinalityProof> {
303
0
        match mem::replace(self, SourcePendingJustificationProofs::None) {
304
            SourcePendingJustificationProofs::None => {
305
0
                *self = SourcePendingJustificationProofs::None;
306
0
                None
307
            }
308
            SourcePendingJustificationProofs::One {
309
0
                proof: FinalityProofs::GrandpaCommit(commit),
310
                ..
311
            } => {
312
0
                *self = SourcePendingJustificationProofs::None;
313
0
                Some(FinalityProof::GrandpaCommit(commit))
314
            }
315
            SourcePendingJustificationProofs::One {
316
0
                proof: FinalityProofs::Justifications(justifications),
317
                ..
318
0
            } if justifications.len() == 1 => {
319
0
                *self = SourcePendingJustificationProofs::None;
320
0
                let j = justifications.into_iter().next().unwrap();
321
0
                Some(FinalityProof::Justification(j))
322
            }
323
            SourcePendingJustificationProofs::One {
324
0
                target_height,
325
0
                proof: FinalityProofs::Justifications(mut justifications),
326
            } => {
327
0
                let j = justifications.pop().unwrap();
328
0
                *self = SourcePendingJustificationProofs::One {
329
0
                    target_height,
330
0
                    proof: FinalityProofs::Justifications(justifications),
331
0
                };
332
0
                Some(FinalityProof::Justification(j))
333
            }
334
            SourcePendingJustificationProofs::Two {
335
0
                high_proof: FinalityProofs::GrandpaCommit(commit),
336
0
                low_proof,
337
0
                low_target_height,
338
                ..
339
            } => {
340
0
                *self = SourcePendingJustificationProofs::One {
341
0
                    target_height: low_target_height,
342
0
                    proof: low_proof,
343
0
                };
344
0
                Some(FinalityProof::GrandpaCommit(commit))
345
            }
346
            SourcePendingJustificationProofs::Two {
347
0
                high_proof: FinalityProofs::Justifications(justifications),
348
0
                low_proof,
349
0
                low_target_height,
350
                ..
351
0
            } if justifications.len() == 1 => {
352
0
                let j = justifications.into_iter().next().unwrap();
353
0
                *self = SourcePendingJustificationProofs::One {
354
0
                    target_height: low_target_height,
355
0
                    proof: low_proof,
356
0
                };
357
0
                Some(FinalityProof::Justification(j))
358
            }
359
            SourcePendingJustificationProofs::Two {
360
0
                high_proof: FinalityProofs::Justifications(mut justifications),
361
0
                high_target_height,
362
0
                low_proof,
363
0
                low_target_height,
364
            } => {
365
0
                let j = justifications.pop().unwrap();
366
0
                *self = SourcePendingJustificationProofs::Two {
367
0
                    high_proof: FinalityProofs::Justifications(justifications),
368
0
                    high_target_height,
369
0
                    low_proof,
370
0
                    low_target_height,
371
0
                };
372
0
                Some(FinalityProof::Justification(j))
373
            }
374
        }
375
0
    }
Unexecuted instantiation: _RNvMNtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksNtB2_32SourcePendingJustificationProofs8take_one
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksNtB2_32SourcePendingJustificationProofs8take_one
376
377
0
    fn merge(&mut self, other: Self) {
378
0
        match other {
379
0
            SourcePendingJustificationProofs::None => {}
380
            SourcePendingJustificationProofs::One {
381
0
                target_height,
382
0
                proof,
383
0
            } => self.insert(target_height, proof),
384
            SourcePendingJustificationProofs::Two {
385
0
                high_proof,
386
0
                high_target_height,
387
0
                low_proof,
388
0
                low_target_height,
389
0
            } => {
390
0
                self.insert(high_target_height, high_proof);
391
0
                self.insert(low_target_height, low_proof);
392
0
            }
393
        }
394
0
    }
Unexecuted instantiation: _RNvMNtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksNtB2_32SourcePendingJustificationProofs5merge
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksNtB2_32SourcePendingJustificationProofs5merge
395
}
396
397
enum FinalityProofs {
398
    GrandpaCommit(Vec<u8>),
399
    Justifications(Vec<([u8; 4], Vec<u8>)>),
400
}
401
402
enum FinalityProof {
403
    GrandpaCommit(Vec<u8>),
404
    Justification(([u8; 4], Vec<u8>)),
405
}
406
407
impl<TBl, TRq, TSrc> AllForksSync<TBl, TRq, TSrc> {
408
    /// Initializes a new [`AllForksSync`].
409
21
    pub fn new(config: Config) -> Self {
410
21
        let finalized_block_height = config
411
21
            .chain_information
412
21
            .as_ref()
413
21
            .finalized_block_header
414
21
            .number;
415
416
21
        let chain = blocks_tree::NonFinalizedTree::new(blocks_tree::Config {
417
21
            chain_information: config.chain_information,
418
21
            block_number_bytes: config.block_number_bytes,
419
21
            blocks_capacity: config.blocks_capacity,
420
21
            allow_unknown_consensus_engines: config.allow_unknown_consensus_engines,
421
21
        });
422
423
21
        Self {
424
21
            chain,
425
21
            inner: Box::new(Inner {
426
21
                blocks: pending_blocks::PendingBlocks::new(pending_blocks::Config {
427
21
                    blocks_capacity: config.blocks_capacity,
428
21
                    finalized_block_height,
429
21
                    max_requests_per_block: config.max_requests_per_block,
430
21
                    sources_capacity: config.sources_capacity,
431
21
                    download_bodies: config.download_bodies,
432
21
                }),
433
21
            }),
434
21
        }
435
21
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE3newB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE3newCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE3newB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE3newCs7snhGEhbuap_18smoldot_light_wasm
_RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE3newCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
409
2
    pub fn new(config: Config) -> Self {
410
2
        let finalized_block_height = config
411
2
            .chain_information
412
2
            .as_ref()
413
2
            .finalized_block_header
414
2
            .number;
415
416
2
        let chain = blocks_tree::NonFinalizedTree::new(blocks_tree::Config {
417
2
            chain_information: config.chain_information,
418
2
            block_number_bytes: config.block_number_bytes,
419
2
            blocks_capacity: config.blocks_capacity,
420
2
            allow_unknown_consensus_engines: config.allow_unknown_consensus_engines,
421
2
        });
422
423
2
        Self {
424
2
            chain,
425
2
            inner: Box::new(Inner {
426
2
                blocks: pending_blocks::PendingBlocks::new(pending_blocks::Config {
427
2
                    blocks_capacity: config.blocks_capacity,
428
2
                    finalized_block_height,
429
2
                    max_requests_per_block: config.max_requests_per_block,
430
2
                    sources_capacity: config.sources_capacity,
431
2
                    download_bodies: config.download_bodies,
432
2
                }),
433
2
            }),
434
2
        }
435
2
    }
_RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE3newCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
409
19
    pub fn new(config: Config) -> Self {
410
19
        let finalized_block_height = config
411
19
            .chain_information
412
19
            .as_ref()
413
19
            .finalized_block_header
414
19
            .number;
415
416
19
        let chain = blocks_tree::NonFinalizedTree::new(blocks_tree::Config {
417
19
            chain_information: config.chain_information,
418
19
            block_number_bytes: config.block_number_bytes,
419
19
            blocks_capacity: config.blocks_capacity,
420
19
            allow_unknown_consensus_engines: config.allow_unknown_consensus_engines,
421
19
        });
422
423
19
        Self {
424
19
            chain,
425
19
            inner: Box::new(Inner {
426
19
                blocks: pending_blocks::PendingBlocks::new(pending_blocks::Config {
427
19
                    blocks_capacity: config.blocks_capacity,
428
19
                    finalized_block_height,
429
19
                    max_requests_per_block: config.max_requests_per_block,
430
19
                    sources_capacity: config.sources_capacity,
431
19
                    download_bodies: config.download_bodies,
432
19
                }),
433
19
            }),
434
19
        }
435
19
    }
436
437
    /// Returns the value that was initially passed in [`Config::block_number_bytes`].
438
0
    pub fn block_number_bytes(&self) -> usize {
439
0
        self.chain.block_number_bytes()
440
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE18block_number_bytesB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE18block_number_bytesB8_
441
442
    /// Builds a [`chain_information::ChainInformationRef`] struct corresponding to the current
443
    /// latest finalized block. Can later be used to reconstruct a chain.
444
0
    pub fn as_chain_information(&self) -> chain_information::ValidChainInformationRef {
445
0
        self.chain.as_chain_information()
446
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE20as_chain_informationB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE20as_chain_informationCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE20as_chain_informationB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE20as_chain_informationCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE20as_chain_informationCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE20as_chain_informationCs4VrkfB1pvQ3_25json_rpc_general_requests
447
448
    /// Returns the header of the finalized block.
449
0
    pub fn finalized_block_header(&self) -> &[u8] {
450
0
        self.chain.finalized_block_header()
451
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE22finalized_block_headerB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE22finalized_block_headerCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE22finalized_block_headerB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE22finalized_block_headerCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE22finalized_block_headerCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE22finalized_block_headerCs4VrkfB1pvQ3_25json_rpc_general_requests
452
453
    /// Returns the height of the finalized block.
454
0
    pub fn finalized_block_number(&self) -> u64 {
455
0
        self.chain.finalized_block_height()
456
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE22finalized_block_numberB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE22finalized_block_numberCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE22finalized_block_numberB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE22finalized_block_numberCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE22finalized_block_numberCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE22finalized_block_numberCs4VrkfB1pvQ3_25json_rpc_general_requests
457
458
    /// Returns the hash of the finalized block.
459
0
    pub fn finalized_block_hash(&self) -> &[u8; 32] {
460
0
        self.chain.finalized_block_hash()
461
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE20finalized_block_hashB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE20finalized_block_hashCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE20finalized_block_hashB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE20finalized_block_hashCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE20finalized_block_hashCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE20finalized_block_hashCs4VrkfB1pvQ3_25json_rpc_general_requests
462
463
    /// Returns the header of the best block.
464
    ///
465
    /// > **Note**: This value is provided only for informative purposes. Keep in mind that this
466
    /// >           best block might be reverted in the future.
467
0
    pub fn best_block_header(&self) -> &[u8] {
468
0
        self.chain.best_block_header()
469
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE17best_block_headerB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE17best_block_headerB8_
470
471
    /// Returns the number of the best block.
472
    ///
473
    /// > **Note**: This value is provided only for informative purposes. Keep in mind that this
474
    /// >           best block might be reverted in the future.
475
21
    pub fn best_block_number(&self) -> u64 {
476
21
        self.chain.best_block_height()
477
21
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE17best_block_numberB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE17best_block_numberCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE17best_block_numberB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE17best_block_numberCs7snhGEhbuap_18smoldot_light_wasm
_RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE17best_block_numberCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
475
2
    pub fn best_block_number(&self) -> u64 {
476
2
        self.chain.best_block_height()
477
2
    }
_RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE17best_block_numberCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
475
19
    pub fn best_block_number(&self) -> u64 {
476
19
        self.chain.best_block_height()
477
19
    }
478
479
    /// Returns the hash of the best block.
480
    ///
481
    /// > **Note**: This value is provided only for informative purposes. Keep in mind that this
482
    /// >           best block might be reverted in the future.
483
21
    pub fn best_block_hash(&self) -> &[u8; 32] {
484
21
        self.chain.best_block_hash()
485
21
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE15best_block_hashB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE15best_block_hashCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE15best_block_hashB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE15best_block_hashCs7snhGEhbuap_18smoldot_light_wasm
_RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE15best_block_hashCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
483
2
    pub fn best_block_hash(&self) -> &[u8; 32] {
484
2
        self.chain.best_block_hash()
485
2
    }
_RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE15best_block_hashCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
483
19
    pub fn best_block_hash(&self) -> &[u8; 32] {
484
19
        self.chain.best_block_hash()
485
19
    }
486
487
    /// Returns the header of all known non-finalized blocks in the chain without any specific
488
    /// order.
489
0
    pub fn non_finalized_blocks_unordered(&self) -> impl Iterator<Item = header::HeaderRef> {
490
0
        self.chain.iter_unordered()
491
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE30non_finalized_blocks_unorderedB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE30non_finalized_blocks_unorderedB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE30non_finalized_blocks_unorderedCs7snhGEhbuap_18smoldot_light_wasm
492
493
    /// Returns the header of all known non-finalized blocks in the chain.
494
    ///
495
    /// The returned items are guaranteed to be in an order in which the parents are found before
496
    /// their children.
497
0
    pub fn non_finalized_blocks_ancestry_order(&self) -> impl Iterator<Item = header::HeaderRef> {
498
0
        self.chain.iter_ancestry_order()
499
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE35non_finalized_blocks_ancestry_orderB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE35non_finalized_blocks_ancestry_orderCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE35non_finalized_blocks_ancestry_orderB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE35non_finalized_blocks_ancestry_orderCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE35non_finalized_blocks_ancestry_orderCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE35non_finalized_blocks_ancestry_orderCs4VrkfB1pvQ3_25json_rpc_general_requests
500
501
    /// Starts the process of inserting a new source in the [`AllForksSync`].
502
    ///
503
    /// This function doesn't modify the state machine, but only looks at the current state of the
504
    /// block referenced by `best_block_number` and `best_block_hash`. It returns an enum that
505
    /// allows performing the actual insertion.
506
21
    pub fn prepare_add_source(
507
21
        &mut self,
508
21
        best_block_number: u64,
509
21
        best_block_hash: [u8; 32],
510
21
    ) -> AddSource<TBl, TRq, TSrc> {
511
21
        if best_block_number <= self.chain.finalized_block_height() {
512
21
            return AddSource::OldBestBlock(AddSourceOldBlock {
513
21
                inner: self,
514
21
                best_block_hash,
515
21
                best_block_number,
516
21
            });
517
0
        }
518
519
0
        let best_block_already_verified = self.chain.contains_non_finalized_block(&best_block_hash);
520
0
        let best_block_in_disjoints_list = self
521
0
            .inner
522
0
            .blocks
523
0
            .contains_unverified_block(best_block_number, &best_block_hash);
524
525
0
        match (best_block_already_verified, best_block_in_disjoints_list) {
526
0
            (false, false) => AddSource::UnknownBestBlock(AddSourceUnknown {
527
0
                inner: self,
528
0
                best_block_hash,
529
0
                best_block_number,
530
0
            }),
531
0
            (true, false) => AddSource::BestBlockAlreadyVerified(AddSourceKnown {
532
0
                inner: self,
533
0
                best_block_hash,
534
0
                best_block_number,
535
0
            }),
536
0
            (false, true) => AddSource::BestBlockPendingVerification(AddSourceKnown {
537
0
                inner: self,
538
0
                best_block_hash,
539
0
                best_block_number,
540
0
            }),
541
0
            (true, true) => unreachable!(),
542
        }
543
21
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE18prepare_add_sourceB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE18prepare_add_sourceCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE18prepare_add_sourceB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE18prepare_add_sourceCs7snhGEhbuap_18smoldot_light_wasm
_RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE18prepare_add_sourceCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
506
2
    pub fn prepare_add_source(
507
2
        &mut self,
508
2
        best_block_number: u64,
509
2
        best_block_hash: [u8; 32],
510
2
    ) -> AddSource<TBl, TRq, TSrc> {
511
2
        if best_block_number <= self.chain.finalized_block_height() {
512
2
            return AddSource::OldBestBlock(AddSourceOldBlock {
513
2
                inner: self,
514
2
                best_block_hash,
515
2
                best_block_number,
516
2
            });
517
0
        }
518
519
0
        let best_block_already_verified = self.chain.contains_non_finalized_block(&best_block_hash);
520
0
        let best_block_in_disjoints_list = self
521
0
            .inner
522
0
            .blocks
523
0
            .contains_unverified_block(best_block_number, &best_block_hash);
524
525
0
        match (best_block_already_verified, best_block_in_disjoints_list) {
526
0
            (false, false) => AddSource::UnknownBestBlock(AddSourceUnknown {
527
0
                inner: self,
528
0
                best_block_hash,
529
0
                best_block_number,
530
0
            }),
531
0
            (true, false) => AddSource::BestBlockAlreadyVerified(AddSourceKnown {
532
0
                inner: self,
533
0
                best_block_hash,
534
0
                best_block_number,
535
0
            }),
536
0
            (false, true) => AddSource::BestBlockPendingVerification(AddSourceKnown {
537
0
                inner: self,
538
0
                best_block_hash,
539
0
                best_block_number,
540
0
            }),
541
0
            (true, true) => unreachable!(),
542
        }
543
2
    }
_RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE18prepare_add_sourceCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
506
19
    pub fn prepare_add_source(
507
19
        &mut self,
508
19
        best_block_number: u64,
509
19
        best_block_hash: [u8; 32],
510
19
    ) -> AddSource<TBl, TRq, TSrc> {
511
19
        if best_block_number <= self.chain.finalized_block_height() {
512
19
            return AddSource::OldBestBlock(AddSourceOldBlock {
513
19
                inner: self,
514
19
                best_block_hash,
515
19
                best_block_number,
516
19
            });
517
0
        }
518
519
0
        let best_block_already_verified = self.chain.contains_non_finalized_block(&best_block_hash);
520
0
        let best_block_in_disjoints_list = self
521
0
            .inner
522
0
            .blocks
523
0
            .contains_unverified_block(best_block_number, &best_block_hash);
524
525
0
        match (best_block_already_verified, best_block_in_disjoints_list) {
526
0
            (false, false) => AddSource::UnknownBestBlock(AddSourceUnknown {
527
0
                inner: self,
528
0
                best_block_hash,
529
0
                best_block_number,
530
0
            }),
531
0
            (true, false) => AddSource::BestBlockAlreadyVerified(AddSourceKnown {
532
0
                inner: self,
533
0
                best_block_hash,
534
0
                best_block_number,
535
0
            }),
536
0
            (false, true) => AddSource::BestBlockPendingVerification(AddSourceKnown {
537
0
                inner: self,
538
0
                best_block_hash,
539
0
                best_block_number,
540
0
            }),
541
0
            (true, true) => unreachable!(),
542
        }
543
19
    }
544
545
    /// Removes the source from the [`AllForksSync`].
546
    ///
547
    /// Removing the source implicitly cancels the request that is associated to it (if any).
548
    ///
549
    /// Returns the user data that was originally passed when inserting the source, plus an
550
    /// `Option`.
551
    /// If this `Option` is `Some`, it contains a request that must be started towards the source
552
    /// indicated by the [`SourceId`].
553
    ///
554
    /// > **Note**: For example, if the source that has just been removed was performing an
555
    /// >           ancestry search, the `Option` might contain that same ancestry search.
556
    ///
557
    /// # Panic
558
    ///
559
    /// Panics if the [`SourceId`] is out of range.
560
    ///
561
0
    pub fn remove_source(
562
0
        &mut self,
563
0
        source_id: SourceId,
564
0
    ) -> (TSrc, impl Iterator<Item = (RequestId, RequestParams, TRq)>) {
565
0
        let (user_data, iter) = self.inner.blocks.remove_source(source_id);
566
0
        (user_data.user_data, iter)
567
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE13remove_sourceB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE13remove_sourceCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE13remove_sourceB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE13remove_sourceCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE13remove_sourceCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE13remove_sourceCs4VrkfB1pvQ3_25json_rpc_general_requests
568
569
    /// Returns the list of sources in this state machine.
570
0
    pub fn sources(&self) -> impl ExactSizeIterator<Item = SourceId> {
571
0
        self.inner.blocks.sources()
572
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE7sourcesB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE7sourcesCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE7sourcesB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE7sourcesCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE7sourcesCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE7sourcesCs4VrkfB1pvQ3_25json_rpc_general_requests
573
574
    /// Returns true if the source has earlier announced the block passed as parameter or one of
575
    /// its descendants.
576
    ///
577
    /// Also returns true if the requested block is inferior or equal to the known finalized block
578
    /// and the source has announced a block higher or equal to the known finalized block.
579
    ///
580
    /// # Panic
581
    ///
582
    /// Panics if the [`SourceId`] is out of range.
583
    ///
584
    /// Panics if `height` is inferior or equal to the finalized block height. Finalized blocks
585
    /// are intentionally not tracked by this data structure, and panicking when asking for a
586
    /// potentially-finalized block prevents potentially confusing or erroneous situations.
587
    ///
588
0
    pub fn source_knows_non_finalized_block(
589
0
        &self,
590
0
        source_id: SourceId,
591
0
        height: u64,
592
0
        hash: &[u8; 32],
593
0
    ) -> bool {
594
0
        self.inner
595
0
            .blocks
596
0
            .source_knows_non_finalized_block(source_id, height, hash)
597
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE32source_knows_non_finalized_blockB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE32source_knows_non_finalized_blockB8_
598
599
    /// Returns the list of sources for which [`AllForksSync::source_knows_non_finalized_block`]
600
    /// would return `true`.
601
    ///
602
    /// # Panic
603
    ///
604
    /// Panics if `height` is inferior or equal to the finalized block height. Finalized blocks
605
    /// are intentionally not tracked by this data structure, and panicking when asking for a
606
    /// potentially-finalized block prevents potentially confusing or erroneous situations.
607
    ///
608
0
    pub fn knows_non_finalized_block<'a>(
609
0
        &'a self,
610
0
        height: u64,
611
0
        hash: &[u8; 32],
612
0
    ) -> impl Iterator<Item = SourceId> + use<'a, TBl, TRq, TSrc> {
613
0
        self.inner.blocks.knows_non_finalized_block(height, hash)
614
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE25knows_non_finalized_blockB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE25knows_non_finalized_blockCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE25knows_non_finalized_blockB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE25knows_non_finalized_blockCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE25knows_non_finalized_blockCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE25knows_non_finalized_blockCs4VrkfB1pvQ3_25json_rpc_general_requests
615
616
    /// Registers a new block that the source is aware of.
617
    ///
618
    /// Has no effect if `height` is inferior or equal to the finalized block height, or if the
619
    /// source was already known to know this block.
620
    ///
621
    /// The block does not need to be known by the data structure.
622
    ///
623
    /// This is automatically done for the blocks added through [`AllForksSync::block_announce`],
624
    /// [`AllForksSync::prepare_add_source`] or [`FinishRequest::add_block`].
625
    ///
626
    /// # Panic
627
    ///
628
    /// Panics if the [`SourceId`] is out of range.
629
    ///
630
0
    pub fn add_known_block_to_source(&mut self, source_id: SourceId, height: u64, hash: [u8; 32]) {
631
0
        self.inner
632
0
            .blocks
633
0
            .add_known_block_to_source(source_id, height, hash);
634
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE25add_known_block_to_sourceB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE25add_known_block_to_sourceCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE25add_known_block_to_sourceB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE25add_known_block_to_sourceCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE25add_known_block_to_sourceCs4VrkfB1pvQ3_25json_rpc_general_requests
635
636
    /// Returns the current best block of the given source.
637
    ///
638
    /// This corresponds either the latest call to [`AllForksSync::block_announce`] where
639
    /// `is_best` was `true`, or to the parameter passed to [`AllForksSync::prepare_add_source`].
640
    ///
641
    /// # Panic
642
    ///
643
    /// Panics if the [`SourceId`] is invalid.
644
    ///
645
0
    pub fn source_best_block(&self, source_id: SourceId) -> (u64, &[u8; 32]) {
646
0
        self.inner.blocks.source_best_block(source_id)
647
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE17source_best_blockB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE17source_best_blockCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE17source_best_blockB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE17source_best_blockCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE17source_best_blockCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE17source_best_blockCs4VrkfB1pvQ3_25json_rpc_general_requests
648
649
    /// Returns the number of ongoing requests that concern this source.
650
    ///
651
    /// # Panic
652
    ///
653
    /// Panics if the [`SourceId`] is invalid.
654
    ///
655
0
    pub fn source_num_ongoing_requests(&self, source_id: SourceId) -> usize {
656
0
        self.inner.blocks.source_num_ongoing_requests(source_id)
657
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE27source_num_ongoing_requestsB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE27source_num_ongoing_requestsB8_
658
659
    /// Returns the details of a request to start towards a source.
660
    ///
661
    /// This method doesn't modify the state machine in any way. [`AllForksSync::add_request`]
662
    /// must be called in order for the request to actually be marked as started.
663
43
    pub fn desired_requests(&self) -> impl Iterator<Item = (SourceId, &TSrc, RequestParams)> {
664
        // Query justifications of blocks that are necessary in order for finality to progress
665
        // against sources that have reported these blocks as finalized.
666
        // TODO: make it clear in the API docs that justifications should be requested as part of a request
667
        // TODO: this is O(n)
668
43
        let justification_requests =
669
43
            self.chain
670
43
                .finality_checkpoints()
671
43
                .flat_map(move |(block_height, block_hash)| 
{0
672
0
                    self.inner
673
0
                        .blocks
674
0
                        .sources()
675
0
                        .filter(move |s| {
676
                            // We assume that all sources have the same finalized blocks and thus
677
                            // don't check hashes.
678
0
                            self.inner.blocks[*s].unverified_finality_proofs.is_none()
679
0
                                && self.inner.blocks[*s].finalized_block_number >= block_height
680
0
                        })
Unexecuted instantiation: _RNCNCNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB8_12AllForksSyncpppE16desired_requests00Bc_
Unexecuted instantiation: _RNCNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtBa_3all20AllForksRequestExtraNtB2Z_19AllForksSourceExtraE16desired_requests00CscoAnRPySggw_6author
Unexecuted instantiation: _RNCNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_12AllForksSyncpppE16desired_requests00Bc_
Unexecuted instantiation: _RNCNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtBa_3all20AllForksRequestExtraNtB1N_19AllForksSourceExtraE16desired_requests00Cs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNCNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtBa_3all20AllForksRequestExtraNtB2Z_19AllForksSourceExtraE16desired_requests00CsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNCNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtBa_3all20AllForksRequestExtraNtB2Z_19AllForksSourceExtraE16desired_requests00Cs4VrkfB1pvQ3_25json_rpc_general_requests
681
0
                        .map(move |source_id| {
682
0
                            (
683
0
                                source_id,
684
0
                                &self.inner.blocks[source_id].user_data,
685
0
                                RequestParams {
686
0
                                    first_block_hash: *block_hash,
687
0
                                    first_block_height: block_height,
688
0
                                    num_blocks: NonZero::<u64>::new(1).unwrap(),
689
0
                                },
690
0
                            )
691
0
                        })
Unexecuted instantiation: _RNCNCNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB8_12AllForksSyncpppE16desired_requests0s_0Bc_
Unexecuted instantiation: _RNCNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtBa_3all20AllForksRequestExtraNtB2Z_19AllForksSourceExtraE16desired_requests0s_0CscoAnRPySggw_6author
Unexecuted instantiation: _RNCNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_12AllForksSyncpppE16desired_requests0s_0Bc_
Unexecuted instantiation: _RNCNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtBa_3all20AllForksRequestExtraNtB1N_19AllForksSourceExtraE16desired_requests0s_0Cs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNCNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtBa_3all20AllForksRequestExtraNtB2Z_19AllForksSourceExtraE16desired_requests0s_0CsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNCNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtBa_3all20AllForksRequestExtraNtB2Z_19AllForksSourceExtraE16desired_requests0s_0Cs4VrkfB1pvQ3_25json_rpc_general_requests
692
0
                });
Unexecuted instantiation: _RNCNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB6_12AllForksSyncpppE16desired_requests0Ba_
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraE16desired_requests0CscoAnRPySggw_6author
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncpppE16desired_requests0Ba_
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB8_3all20AllForksRequestExtraNtB1L_19AllForksSourceExtraE16desired_requests0Cs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraE16desired_requests0CsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraE16desired_requests0Cs4VrkfB1pvQ3_25json_rpc_general_requests
693
694
43
        let block_requests = self
695
43
            .inner
696
43
            .blocks
697
43
            .desired_requests()
698
43
            .filter(move |rq| 
{0
699
0
                !self
700
0
                    .chain
701
0
                    .contains_non_finalized_block(&rq.request_params.first_block_hash)
702
0
            })
Unexecuted instantiation: _RNCNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB6_12AllForksSyncpppE16desired_requestss_0Ba_
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraE16desired_requestss_0CscoAnRPySggw_6author
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncpppE16desired_requestss_0Ba_
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB8_3all20AllForksRequestExtraNtB1L_19AllForksSourceExtraE16desired_requestss_0Cs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraE16desired_requestss_0CsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraE16desired_requestss_0Cs4VrkfB1pvQ3_25json_rpc_general_requests
703
43
            .map(move |rq| 
{0
704
0
                (
705
0
                    rq.source_id,
706
0
                    &self.inner.blocks[rq.source_id].user_data,
707
0
                    rq.request_params,
708
0
                )
709
0
            });
Unexecuted instantiation: _RNCNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB6_12AllForksSyncpppE16desired_requestss0_0Ba_
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraE16desired_requestss0_0CscoAnRPySggw_6author
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncpppE16desired_requestss0_0Ba_
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB8_3all20AllForksRequestExtraNtB1L_19AllForksSourceExtraE16desired_requestss0_0Cs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraE16desired_requestss0_0CsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraE16desired_requestss0_0Cs4VrkfB1pvQ3_25json_rpc_general_requests
710
711
43
        justification_requests.chain(block_requests)
712
43
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE16desired_requestsB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE16desired_requestsCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE16desired_requestsB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE16desired_requestsCs7snhGEhbuap_18smoldot_light_wasm
_RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE16desired_requestsCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
663
4
    pub fn desired_requests(&self) -> impl Iterator<Item = (SourceId, &TSrc, RequestParams)> {
664
        // Query justifications of blocks that are necessary in order for finality to progress
665
        // against sources that have reported these blocks as finalized.
666
        // TODO: make it clear in the API docs that justifications should be requested as part of a request
667
        // TODO: this is O(n)
668
4
        let justification_requests =
669
4
            self.chain
670
4
                .finality_checkpoints()
671
4
                .flat_map(move |(block_height, block_hash)| {
672
                    self.inner
673
                        .blocks
674
                        .sources()
675
                        .filter(move |s| {
676
                            // We assume that all sources have the same finalized blocks and thus
677
                            // don't check hashes.
678
                            self.inner.blocks[*s].unverified_finality_proofs.is_none()
679
                                && self.inner.blocks[*s].finalized_block_number >= block_height
680
                        })
681
                        .map(move |source_id| {
682
                            (
683
                                source_id,
684
                                &self.inner.blocks[source_id].user_data,
685
                                RequestParams {
686
                                    first_block_hash: *block_hash,
687
                                    first_block_height: block_height,
688
                                    num_blocks: NonZero::<u64>::new(1).unwrap(),
689
                                },
690
                            )
691
                        })
692
                });
693
694
4
        let block_requests = self
695
4
            .inner
696
4
            .blocks
697
4
            .desired_requests()
698
4
            .filter(move |rq| {
699
                !self
700
                    .chain
701
                    .contains_non_finalized_block(&rq.request_params.first_block_hash)
702
            })
703
4
            .map(move |rq| {
704
                (
705
                    rq.source_id,
706
                    &self.inner.blocks[rq.source_id].user_data,
707
                    rq.request_params,
708
                )
709
            });
710
711
4
        justification_requests.chain(block_requests)
712
4
    }
_RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE16desired_requestsCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
663
39
    pub fn desired_requests(&self) -> impl Iterator<Item = (SourceId, &TSrc, RequestParams)> {
664
        // Query justifications of blocks that are necessary in order for finality to progress
665
        // against sources that have reported these blocks as finalized.
666
        // TODO: make it clear in the API docs that justifications should be requested as part of a request
667
        // TODO: this is O(n)
668
39
        let justification_requests =
669
39
            self.chain
670
39
                .finality_checkpoints()
671
39
                .flat_map(move |(block_height, block_hash)| {
672
                    self.inner
673
                        .blocks
674
                        .sources()
675
                        .filter(move |s| {
676
                            // We assume that all sources have the same finalized blocks and thus
677
                            // don't check hashes.
678
                            self.inner.blocks[*s].unverified_finality_proofs.is_none()
679
                                && self.inner.blocks[*s].finalized_block_number >= block_height
680
                        })
681
                        .map(move |source_id| {
682
                            (
683
                                source_id,
684
                                &self.inner.blocks[source_id].user_data,
685
                                RequestParams {
686
                                    first_block_hash: *block_hash,
687
                                    first_block_height: block_height,
688
                                    num_blocks: NonZero::<u64>::new(1).unwrap(),
689
                                },
690
                            )
691
                        })
692
                });
693
694
39
        let block_requests = self
695
39
            .inner
696
39
            .blocks
697
39
            .desired_requests()
698
39
            .filter(move |rq| {
699
                !self
700
                    .chain
701
                    .contains_non_finalized_block(&rq.request_params.first_block_hash)
702
            })
703
39
            .map(move |rq| {
704
                (
705
                    rq.source_id,
706
                    &self.inner.blocks[rq.source_id].user_data,
707
                    rq.request_params,
708
                )
709
            });
710
711
39
        justification_requests.chain(block_requests)
712
39
    }
713
714
    /// Inserts a new request in the data structure.
715
    ///
716
    /// > **Note**: The request doesn't necessarily have to match a request returned by
717
    /// >           [`AllForksSync::desired_requests`].
718
    ///
719
    /// # Panic
720
    ///
721
    /// Panics if the [`SourceId`] is out of range.
722
    ///
723
0
    pub fn add_request(
724
0
        &mut self,
725
0
        source_id: SourceId,
726
0
        detail: RequestParams,
727
0
        user_data: TRq,
728
0
    ) -> RequestId {
729
0
        self.inner.blocks.add_request(source_id, detail, user_data)
730
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE11add_requestB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE11add_requestCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE11add_requestB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE11add_requestCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE11add_requestCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE11add_requestCs4VrkfB1pvQ3_25json_rpc_general_requests
731
732
    /// Returns a list of requests that are considered obsolete and can be removed using
733
    /// [`AllForksSync::finish_request`].
734
    ///
735
    /// A request becomes obsolete if the state of the request blocks changes in such a way that
736
    /// they don't need to be requested anymore. The response to the request will be useless.
737
    ///
738
    /// > **Note**: It is in no way mandatory to actually call this function and cancel the
739
    /// >           requests that are returned.
740
0
    pub fn obsolete_requests(&self) -> impl Iterator<Item = (RequestId, &TRq)> {
741
        // TODO: requests meant to query justifications only are considered obsolete by the underlying state machine, which right now is okay because the underlying state machine is pretty loose in its definition of obsolete
742
0
        self.inner.blocks.obsolete_requests()
743
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE17obsolete_requestsB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE17obsolete_requestsB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE17obsolete_requestsCs7snhGEhbuap_18smoldot_light_wasm
744
745
    /// Returns the [`SourceId`] that is expected to fulfill the given request.
746
    ///
747
    /// # Panic
748
    ///
749
    /// Panics if the [`RequestId`] is invalid.
750
    ///
751
0
    pub fn request_source_id(&self, request_id: RequestId) -> SourceId {
752
0
        self.inner.blocks.request_source_id(request_id)
753
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE17request_source_idB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE17request_source_idB8_
754
755
    /// Call in response to a request being successful or failing.
756
    ///
757
    /// This state machine doesn't differentiate between successful or failed requests. If a
758
    /// request has failed, call this function and immediately call [`FinishRequest::finish`].
759
    /// Additionally, it is allow to insert fewer blocks than the number indicated in
760
    /// [`RequestParams::num_blocks`].
761
    ///
762
    /// The added blocks are expected to be sorted in decreasing order. The first block should be
763
    /// the block with the hash that was referred by [`RequestParams::first_block_hash`]. Each
764
    /// subsequent element is then expected to be the parent of the previous one.
765
    ///
766
    /// # Panic
767
    ///
768
    /// Panics if the [`RequestId`] is invalid.
769
    ///
770
0
    pub fn finish_request(
771
0
        &mut self,
772
0
        request_id: RequestId,
773
0
    ) -> (TRq, FinishRequest<TBl, TRq, TSrc>) {
774
        // Sets the `occupation` of `source_id` back to `AllSync`.
775
        let (
776
            pending_blocks::RequestParams {
777
0
                first_block_hash: requested_block_hash,
778
0
                first_block_height: requested_block_height,
779
                ..
780
            },
781
0
            source_id,
782
0
            request_user_data,
783
0
        ) = self.inner.blocks.remove_request(request_id);
784
785
0
        (
786
0
            request_user_data,
787
0
            FinishRequest {
788
0
                inner: self,
789
0
                source_id,
790
0
                any_progress: false,
791
0
                index_in_response: 0,
792
0
                requested_block_hash,
793
0
                requested_block_height,
794
0
                expected_next_hash: requested_block_hash,
795
0
                expected_next_height: requested_block_height,
796
0
            },
797
0
        )
798
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE14finish_requestB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE14finish_requestCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE14finish_requestB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE14finish_requestCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE14finish_requestCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE14finish_requestCs4VrkfB1pvQ3_25json_rpc_general_requests
799
800
    /// Update the source with a newly-announced block.
801
    ///
802
    /// > **Note**: This information is normally reported by the source itself. In the case of a
803
    /// >           a networking peer, call this when the source sent a block announce.
804
    ///
805
    /// # Panic
806
    ///
807
    /// Panics if `source_id` is invalid.
808
    ///
809
0
    pub fn block_announce(
810
0
        &mut self,
811
0
        source_id: SourceId,
812
0
        announced_scale_encoded_header: Vec<u8>,
813
0
        is_best: bool,
814
0
    ) -> BlockAnnounceOutcome<TBl, TRq, TSrc> {
815
0
        let announced_header = match header::decode(
816
0
            &announced_scale_encoded_header,
817
0
            self.chain.block_number_bytes(),
818
0
        ) {
819
0
            Ok(h) => h,
820
0
            Err(error) => return BlockAnnounceOutcome::InvalidHeader(error),
821
        };
822
823
0
        let announced_header_number = announced_header.number;
824
0
        let announced_header_parent_hash = *announced_header.parent_hash;
825
0
        let announced_header_hash = announced_header.hash(self.chain.block_number_bytes());
826
827
        // It is assumed that all sources will eventually agree on the same finalized chain. If
828
        // the block number is lower or equal than the locally-finalized block number, it is
829
        // assumed that this source is simply late compared to the local node, and that the block
830
        // that has been received is either part of the finalized chain or belongs to a fork that
831
        // will get discarded by this source in the future.
832
0
        if announced_header_number <= self.chain.finalized_block_height() {
833
            // Even if the block is below the finalized block, we still need to set it as the
834
            // best block of this source, if anything for API consistency purposes.
835
0
            if is_best {
836
0
                self.inner.blocks.add_known_block_to_source_and_set_best(
837
0
                    source_id,
838
0
                    announced_header_number,
839
0
                    announced_header_hash,
840
0
                );
841
0
            }
842
843
0
            return BlockAnnounceOutcome::TooOld {
844
0
                announce_block_height: announced_header_number,
845
0
                finalized_block_height: self.chain.finalized_block_height(),
846
0
            };
847
0
        }
848
849
        // If the block is already part of the local tree of blocks, nothing more to do.
850
0
        if self
851
0
            .chain
852
0
            .contains_non_finalized_block(&announced_header_hash)
853
        {
854
0
            return BlockAnnounceOutcome::AlreadyVerified(AnnouncedBlockKnown {
855
0
                inner: self,
856
0
                announced_header_hash,
857
0
                announced_header_number,
858
0
                announced_header_parent_hash,
859
0
                announced_header_encoded: announced_scale_encoded_header,
860
0
                source_id,
861
0
                is_in_chain: true,
862
0
                is_best,
863
0
            });
864
0
        }
865
866
        // At this point, we have excluded blocks that are already part of the chain or too old.
867
        // We insert the block in the list of unverified blocks so as to treat all blocks the
868
        // same.
869
0
        if !self
870
0
            .inner
871
0
            .blocks
872
0
            .contains_unverified_block(announced_header_number, &announced_header_hash)
873
        {
874
0
            BlockAnnounceOutcome::Unknown(AnnouncedBlockUnknown {
875
0
                inner: self,
876
0
                announced_header_hash,
877
0
                announced_header_number,
878
0
                announced_header_parent_hash,
879
0
                announced_header_encoded: announced_scale_encoded_header,
880
0
                source_id,
881
0
                is_best,
882
0
            })
883
        } else {
884
0
            BlockAnnounceOutcome::AlreadyPending(AnnouncedBlockKnown {
885
0
                inner: self,
886
0
                announced_header_hash,
887
0
                announced_header_number,
888
0
                announced_header_parent_hash,
889
0
                announced_header_encoded: announced_scale_encoded_header,
890
0
                is_in_chain: false,
891
0
                source_id,
892
0
                is_best,
893
0
            })
894
        }
895
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE14block_announceB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE14block_announceCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE14block_announceB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE14block_announceCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE14block_announceCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE14block_announceCs4VrkfB1pvQ3_25json_rpc_general_requests
896
897
    /// Update the finalized block height of the given source.
898
    ///
899
    /// # Panic
900
    ///
901
    /// Panics if `source_id` is invalid.
902
    ///
903
0
    pub fn update_source_finality_state(
904
0
        &mut self,
905
0
        source_id: SourceId,
906
0
        finalized_block_height: u64,
907
0
    ) {
908
0
        let source = &mut self.inner.blocks[source_id];
909
0
        source.finalized_block_number =
910
0
            cmp::max(source.finalized_block_number, finalized_block_height);
911
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE28update_source_finality_stateB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE28update_source_finality_stateCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE28update_source_finality_stateB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE28update_source_finality_stateCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE28update_source_finality_stateCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE28update_source_finality_stateCs4VrkfB1pvQ3_25json_rpc_general_requests
912
913
    /// Update the state machine with a Grandpa commit message received from the network.
914
    ///
915
    /// This function only inserts the commit message into the state machine, and does not
916
    /// immediately verify it.
917
    ///
918
    /// # Panic
919
    ///
920
    /// Panics if `source_id` is invalid.
921
    ///
922
0
    pub fn grandpa_commit_message(
923
0
        &mut self,
924
0
        source_id: SourceId,
925
0
        scale_encoded_commit: Vec<u8>,
926
0
    ) -> GrandpaCommitMessageOutcome {
927
0
        let source = &mut self.inner.blocks[source_id];
928
929
0
        let block_number = match decode::decode_grandpa_commit(
930
0
            &scale_encoded_commit,
931
0
            self.chain.block_number_bytes(),
932
0
        ) {
933
0
            Ok(msg) => msg.target_number,
934
0
            Err(_) => return GrandpaCommitMessageOutcome::ParseError,
935
        };
936
937
        // The finalized block number of the source is increased even if the commit message
938
        // isn't known to be valid yet.
939
0
        source.finalized_block_number = cmp::max(source.finalized_block_number, block_number);
940
941
0
        source.unverified_finality_proofs.insert(
942
0
            block_number,
943
0
            FinalityProofs::GrandpaCommit(scale_encoded_commit),
944
        );
945
946
0
        GrandpaCommitMessageOutcome::Queued
947
0
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE22grandpa_commit_messageB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE22grandpa_commit_messageB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE22grandpa_commit_messageCs7snhGEhbuap_18smoldot_light_wasm
948
949
    /// Process the next block in the queue of verification.
950
    ///
951
    /// This method takes ownership of the [`AllForksSync`] and starts a verification
952
    /// process. The [`AllForksSync`] is yielded back at the end of this process.
953
21
    pub fn process_one(mut self) -> ProcessOne<TBl, TRq, TSrc> {
954
        // Try to find a block to verify.
955
        // All blocks are always verified before verifying justifications, in order to guarantee
956
        // that the block that a justification targets has already been verified.
957
        // TODO: revisit that ^ as advancing finality should have priority over advancing the chain
958
21
        let block_to_verify = self.inner.blocks.unverified_leaves().find(|block| 
{0
959
0
            block.parent_block_hash == *self.chain.finalized_block_hash()
960
0
                || self
961
0
                    .chain
962
0
                    .contains_non_finalized_block(&block.parent_block_hash)
963
0
        });
Unexecuted instantiation: _RNCNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB6_12AllForksSyncpppE11process_one0Ba_
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraE11process_one0CscoAnRPySggw_6author
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncpppE11process_one0Ba_
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB8_3all20AllForksRequestExtraNtB1L_19AllForksSourceExtraE11process_one0Cs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraE11process_one0CsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraE11process_one0Cs4VrkfB1pvQ3_25json_rpc_general_requests
964
21
        if let Some(
block0
) = block_to_verify {
965
0
            return ProcessOne::BlockVerify(BlockVerify {
966
0
                parent: self,
967
0
                block_to_verify: block,
968
0
            });
969
21
        }
970
971
        // Try to find a justification to verify.
972
        // TODO: O(n)
973
21
        let source_id_with_finality_proof = self
974
21
            .inner
975
21
            .blocks
976
21
            .sources()
977
21
            .find(|id| !self.inner.blocks[*id].unverified_finality_proofs.is_none());
Unexecuted instantiation: _RNCNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB6_12AllForksSyncpppE11process_ones_0Ba_
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraE11process_ones_0CscoAnRPySggw_6author
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncpppE11process_ones_0Ba_
Unexecuted instantiation: _RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB8_3all20AllForksRequestExtraNtB1L_19AllForksSourceExtraE11process_ones_0Cs7snhGEhbuap_18smoldot_light_wasm
_RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraE11process_ones_0CsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
977
2
            .find(|id| !self.inner.blocks[*id].unverified_finality_proofs.is_none());
_RNCNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraE11process_ones_0Cs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
977
19
            .find(|id| !self.inner.blocks[*id].unverified_finality_proofs.is_none());
978
21
        if let Some(
source_id_with_finality_proof0
) = source_id_with_finality_proof {
979
0
            let finality_proof_to_verify = self.inner.blocks[source_id_with_finality_proof]
980
0
                .unverified_finality_proofs
981
0
                .take_one()
982
0
                .unwrap(); // `take()` always returns `Some` because we've checked `is_none()` above
983
0
            return ProcessOne::FinalityProofVerify(FinalityProofVerify {
984
0
                parent: self,
985
0
                source_id: source_id_with_finality_proof,
986
0
                finality_proof_to_verify,
987
0
            });
988
21
        }
989
990
21
        ProcessOne::AllSync { sync: self }
991
21
    }
Unexecuted instantiation: _RNvMs_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE11process_oneB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE11process_oneCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncpppE11process_oneB8_
Unexecuted instantiation: _RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB6_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE11process_oneCs7snhGEhbuap_18smoldot_light_wasm
_RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE11process_oneCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
953
2
    pub fn process_one(mut self) -> ProcessOne<TBl, TRq, TSrc> {
954
        // Try to find a block to verify.
955
        // All blocks are always verified before verifying justifications, in order to guarantee
956
        // that the block that a justification targets has already been verified.
957
        // TODO: revisit that ^ as advancing finality should have priority over advancing the chain
958
2
        let block_to_verify = self.inner.blocks.unverified_leaves().find(|block| {
959
            block.parent_block_hash == *self.chain.finalized_block_hash()
960
                || self
961
                    .chain
962
                    .contains_non_finalized_block(&block.parent_block_hash)
963
        });
964
2
        if let Some(
block0
) = block_to_verify {
965
0
            return ProcessOne::BlockVerify(BlockVerify {
966
0
                parent: self,
967
0
                block_to_verify: block,
968
0
            });
969
2
        }
970
971
        // Try to find a justification to verify.
972
        // TODO: O(n)
973
2
        let source_id_with_finality_proof = self
974
2
            .inner
975
2
            .blocks
976
2
            .sources()
977
2
            .find(|id| !self.inner.blocks[*id].unverified_finality_proofs.is_none());
978
2
        if let Some(
source_id_with_finality_proof0
) = source_id_with_finality_proof {
979
0
            let finality_proof_to_verify = self.inner.blocks[source_id_with_finality_proof]
980
0
                .unverified_finality_proofs
981
0
                .take_one()
982
0
                .unwrap(); // `take()` always returns `Some` because we've checked `is_none()` above
983
0
            return ProcessOne::FinalityProofVerify(FinalityProofVerify {
984
0
                parent: self,
985
0
                source_id: source_id_with_finality_proof,
986
0
                finality_proof_to_verify,
987
0
            });
988
2
        }
989
990
2
        ProcessOne::AllSync { sync: self }
991
2
    }
_RNvMs_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB4_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB6_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE11process_oneCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
953
19
    pub fn process_one(mut self) -> ProcessOne<TBl, TRq, TSrc> {
954
        // Try to find a block to verify.
955
        // All blocks are always verified before verifying justifications, in order to guarantee
956
        // that the block that a justification targets has already been verified.
957
        // TODO: revisit that ^ as advancing finality should have priority over advancing the chain
958
19
        let block_to_verify = self.inner.blocks.unverified_leaves().find(|block| {
959
            block.parent_block_hash == *self.chain.finalized_block_hash()
960
                || self
961
                    .chain
962
                    .contains_non_finalized_block(&block.parent_block_hash)
963
        });
964
19
        if let Some(
block0
) = block_to_verify {
965
0
            return ProcessOne::BlockVerify(BlockVerify {
966
0
                parent: self,
967
0
                block_to_verify: block,
968
0
            });
969
19
        }
970
971
        // Try to find a justification to verify.
972
        // TODO: O(n)
973
19
        let source_id_with_finality_proof = self
974
19
            .inner
975
19
            .blocks
976
19
            .sources()
977
19
            .find(|id| !self.inner.blocks[*id].unverified_finality_proofs.is_none());
978
19
        if let Some(
source_id_with_finality_proof0
) = source_id_with_finality_proof {
979
0
            let finality_proof_to_verify = self.inner.blocks[source_id_with_finality_proof]
980
0
                .unverified_finality_proofs
981
0
                .take_one()
982
0
                .unwrap(); // `take()` always returns `Some` because we've checked `is_none()` above
983
0
            return ProcessOne::FinalityProofVerify(FinalityProofVerify {
984
0
                parent: self,
985
0
                source_id: source_id_with_finality_proof,
986
0
                finality_proof_to_verify,
987
0
            });
988
19
        }
989
990
19
        ProcessOne::AllSync { sync: self }
991
19
    }
992
}
993
994
impl<TBl, TRq, TSrc> ops::Index<SourceId> for AllForksSync<TBl, TRq, TSrc> {
995
    type Output = TSrc;
996
997
    #[track_caller]
998
0
    fn index(&self, id: SourceId) -> &TSrc {
999
0
        &self.inner.blocks[id].user_data
1000
0
    }
Unexecuted instantiation: _RNvXININtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forkss0_0pppEINtB5_12AllForksSyncpppEINtNtNtCs1p5UDGgVI4d_4core3ops5index5IndexNtNtB5_7sources8SourceIdE5indexB9_
Unexecuted instantiation: _RNvXs0_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2W_19AllForksSourceExtraEINtNtNtB19_3ops5index5IndexNtNtB5_7sources8SourceIdE5indexCscoAnRPySggw_6author
Unexecuted instantiation: _RNvXININtNtCsc1ywvx6YAnK_7smoldot4sync9all_forkss0_0pppEINtB5_12AllForksSyncpppEINtNtNtCs1p5UDGgVI4d_4core3ops5index5IndexNtNtB5_7sources8SourceIdE5indexB9_
Unexecuted instantiation: _RNvXs0_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1K_19AllForksSourceExtraEINtNtNtB19_3ops5index5IndexNtNtB5_7sources8SourceIdE5indexCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvXs0_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2W_19AllForksSourceExtraEINtNtNtB19_3ops5index5IndexNtNtB5_7sources8SourceIdE5indexCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvXs0_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2W_19AllForksSourceExtraEINtNtNtB19_3ops5index5IndexNtNtB5_7sources8SourceIdE5indexCs4VrkfB1pvQ3_25json_rpc_general_requests
1001
}
1002
1003
impl<TBl, TRq, TSrc> ops::IndexMut<SourceId> for AllForksSync<TBl, TRq, TSrc> {
1004
    #[track_caller]
1005
0
    fn index_mut(&mut self, id: SourceId) -> &mut TSrc {
1006
0
        &mut self.inner.blocks[id].user_data
1007
0
    }
Unexecuted instantiation: _RNvXININtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forkss1_0pppEINtB5_12AllForksSyncpppEINtNtNtCs1p5UDGgVI4d_4core3ops5index8IndexMutNtNtB5_7sources8SourceIdE9index_mutB9_
Unexecuted instantiation: _RNvXININtNtCsc1ywvx6YAnK_7smoldot4sync9all_forkss1_0pppEINtB5_12AllForksSyncpppEINtNtNtCs1p5UDGgVI4d_4core3ops5index8IndexMutNtNtB5_7sources8SourceIdE9index_mutB9_
1008
}
1009
1010
impl<'a, TBl, TRq, TSrc> ops::Index<(u64, &'a [u8; 32])> for AllForksSync<TBl, TRq, TSrc> {
1011
    type Output = TBl;
1012
1013
    #[track_caller]
1014
0
    fn index(&self, (block_height, block_hash): (u64, &'a [u8; 32])) -> &TBl {
1015
0
        if let Some(block) = self.chain.non_finalized_block_user_data(block_hash) {
1016
0
            return block;
1017
0
        }
1018
1019
0
        &self
1020
0
            .inner
1021
0
            .blocks
1022
0
            .unverified_block_user_data(block_height, block_hash)
1023
0
            .user_data
1024
0
    }
Unexecuted instantiation: _RNvXININtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forkss2_0pppEINtB5_12AllForksSyncpppEINtNtNtCs1p5UDGgVI4d_4core3ops5index5IndexTyRAhj20_EE5indexB9_
Unexecuted instantiation: _RNvXs2_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2W_19AllForksSourceExtraEINtNtNtB19_3ops5index5IndexTyRAhj20_EE5indexCscoAnRPySggw_6author
Unexecuted instantiation: _RNvXININtNtCsc1ywvx6YAnK_7smoldot4sync9all_forkss2_0pppEINtB5_12AllForksSyncpppEINtNtNtCs1p5UDGgVI4d_4core3ops5index5IndexTyRAhj20_EE5indexB9_
Unexecuted instantiation: _RNvXs2_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2W_19AllForksSourceExtraEINtNtNtB19_3ops5index5IndexTyRAhj20_EE5indexCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvXs2_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2W_19AllForksSourceExtraEINtNtNtB19_3ops5index5IndexTyRAhj20_EE5indexCs4VrkfB1pvQ3_25json_rpc_general_requests
1025
}
1026
1027
impl<'a, TBl, TRq, TSrc> ops::IndexMut<(u64, &'a [u8; 32])> for AllForksSync<TBl, TRq, TSrc> {
1028
    #[track_caller]
1029
0
    fn index_mut(&mut self, (block_height, block_hash): (u64, &'a [u8; 32])) -> &mut TBl {
1030
0
        if let Some(block) = self.chain.non_finalized_block_user_data_mut(block_hash) {
1031
0
            return block;
1032
0
        }
1033
1034
0
        &mut self
1035
0
            .inner
1036
0
            .blocks
1037
0
            .unverified_block_user_data_mut(block_height, block_hash)
1038
0
            .user_data
1039
0
    }
Unexecuted instantiation: _RNvXININtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forkss3_0pppEINtB5_12AllForksSyncpppEINtNtNtCs1p5UDGgVI4d_4core3ops5index8IndexMutTyRAhj20_EE9index_mutB9_
Unexecuted instantiation: _RNvXs3_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2W_19AllForksSourceExtraEINtNtNtB19_3ops5index8IndexMutTyRAhj20_EE9index_mutCscoAnRPySggw_6author
Unexecuted instantiation: _RNvXININtNtCsc1ywvx6YAnK_7smoldot4sync9all_forkss3_0pppEINtB5_12AllForksSyncpppEINtNtNtCs1p5UDGgVI4d_4core3ops5index8IndexMutTyRAhj20_EE9index_mutB9_
Unexecuted instantiation: _RNvXs3_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1K_19AllForksSourceExtraEINtNtNtB19_3ops5index8IndexMutTyRAhj20_EE9index_mutCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvXs3_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2W_19AllForksSourceExtraEINtNtNtB19_3ops5index8IndexMutTyRAhj20_EE9index_mutCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvXs3_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_12AllForksSyncINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2W_19AllForksSourceExtraEINtNtNtB19_3ops5index8IndexMutTyRAhj20_EE9index_mutCs4VrkfB1pvQ3_25json_rpc_general_requests
1040
}
1041
1042
/// See [`AllForksSync::finish_request`].
1043
pub struct FinishRequest<'a, TBl, TRq, TSrc> {
1044
    inner: &'a mut AllForksSync<TBl, TRq, TSrc>,
1045
1046
    /// Source that has sent the request that is being answered.
1047
    source_id: SourceId,
1048
1049
    /// Set to true if any block at all have been added.
1050
    any_progress: bool,
1051
1052
    /// Number of blocks added before through that data structure.
1053
    index_in_response: usize,
1054
1055
    /// Hash of the block that was initially request.
1056
    requested_block_hash: [u8; 32],
1057
    /// Height of the block that was initially request.
1058
    requested_block_height: u64,
1059
1060
    /// The next block to add should have a hash equal to this one.
1061
    expected_next_hash: [u8; 32],
1062
    /// The next block to add should have a height equal to this one.
1063
    expected_next_height: u64,
1064
}
1065
1066
impl<'a, TBl, TRq, TSrc> FinishRequest<'a, TBl, TRq, TSrc> {
1067
    /// Adds a block coming from the response that the source has provided.
1068
    ///
1069
    /// On success, the [`FinishRequest`] is turned into an [`AddBlock`]. The block is
1070
    /// inserted in the state machine only after one of the methods in [`AddBlock`] is added.
1071
    ///
1072
    /// If an error is returned, the [`FinishRequest`] is turned back again into a
1073
    /// [`AllForksSync`], but all the blocks that have already been added are retained.
1074
    ///
1075
    /// If [`Config::download_bodies`] was `false`, the content of `scale_encoded_extrinsics`
1076
    /// is ignored.
1077
0
    pub fn add_block(
1078
0
        mut self,
1079
0
        scale_encoded_header: Vec<u8>,
1080
0
        scale_encoded_extrinsics: Vec<Vec<u8>>,
1081
0
        scale_encoded_justifications: impl Iterator<Item = ([u8; 4], impl AsRef<[u8]>)>,
1082
0
    ) -> Result<AddBlock<'a, TBl, TRq, TSrc>, AncestrySearchResponseError> {
1083
        // Compare expected with actual hash.
1084
        // This ensure that each header being processed is the parent of the previous one.
1085
0
        if self.expected_next_hash != header::hash_from_scale_encoded_header(&scale_encoded_header)
1086
        {
1087
0
            return Err(AncestrySearchResponseError::UnexpectedBlock);
1088
0
        }
1089
1090
        // Invalid headers are erroneous.
1091
0
        let decoded_header =
1092
0
            match header::decode(&scale_encoded_header, self.inner.chain.block_number_bytes()) {
1093
0
                Ok(h) => h,
1094
0
                Err(err) => return Err(AncestrySearchResponseError::InvalidHeader(err)),
1095
            };
1096
1097
        // Also compare the block numbers.
1098
        // The utility of checking the height (even though we've already checked the hash) is
1099
        // questionable, but considering that blocks are identified with their combination of
1100
        // hash and number, checking both the hash and number might prevent malicious sources
1101
        // from introducing state inconsistenties, even though it's unclear how that could happen.
1102
0
        if self.expected_next_height != decoded_header.number {
1103
0
            return Err(AncestrySearchResponseError::UnexpectedBlock);
1104
0
        }
1105
1106
        // Check whether the SCALE-encoded extrinsics match the extrinsics root found in
1107
        // the header.
1108
0
        if self.inner.inner.blocks.downloading_bodies() {
1109
0
            let calculated = header::extrinsics_root(&scale_encoded_extrinsics);
1110
0
            if calculated != *decoded_header.extrinsics_root {
1111
0
                return Err(AncestrySearchResponseError::ExtrinsicsRootMismatch);
1112
0
            }
1113
0
        }
1114
1115
        // At this point, the source has given us correct blocks, and we consider the response
1116
        // as a whole to be useful.
1117
0
        self.any_progress = true;
1118
1119
        // It is assumed that all sources will eventually agree on the same finalized chain. If
1120
        // the block number is lower or equal than the locally-finalized block number, it is
1121
        // assumed that this source is simply late compared to the local node, and that the block
1122
        // that has been received is either part of the finalized chain or belongs to a fork that
1123
        // will get discarded by this source in the future.
1124
0
        if decoded_header.number <= self.inner.chain.finalized_block_height() {
1125
0
            return Err(AncestrySearchResponseError::TooOld);
1126
0
        }
1127
1128
        // Convert the justifications in an "owned" format, because we're likely going to store
1129
        // them.
1130
0
        let justifications = scale_encoded_justifications
1131
0
            .map(|(e, j)| (e, j.as_ref().to_owned()))
Unexecuted instantiation: _RNCINvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB8_13FinishRequestpppE9add_blockppE0Bc_
Unexecuted instantiation: _RNCINvMs4_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtBa_3all20AllForksRequestExtraNtB30_19AllForksSourceExtraE9add_blockINtNtCsaFPxhswmqCN_5alloc3vec3VechEINtNtNtNtB1d_4iter8adapters3map3MapINtNtB4a_9into_iter8IntoIterNtB30_13JustificationENCINvMB30_INtB30_7AllSyncuIB19_NtB1M_17NetworkSourceInfoEB1K_E23blocks_request_responseIB4H_IB5g_NtNtNtNtBc_7network5codec13block_request9BlockDataENCNCNvMs_B1M_NtB1M_14SyncBackground3run0sc_0EE0EE0CscoAnRPySggw_6author
Unexecuted instantiation: _RNCINvMs4_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtBa_3all20AllForksRequestExtraNtB30_19AllForksSourceExtraE9add_blockINtNtCsaFPxhswmqCN_5alloc3vec3VechEINtNtNtNtB1d_4iter8adapters3map3MapINtNtB4a_9into_iter8IntoIterNtB30_13JustificationENCINvMB30_INtB30_7AllSyncuIB19_NtB1M_17NetworkSourceInfoEB1K_E23blocks_request_responseINtNtNtB4N_7sources4once4OnceINtB30_24BlockRequestSuccessBlockB1K_EEE0EE0CscoAnRPySggw_6author
Unexecuted instantiation: _RNCINvMs4_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_13FinishRequestpppE9add_blockppE0Bc_
Unexecuted instantiation: _RNCINvMs4_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtBa_3all20AllForksRequestExtraNtB1O_19AllForksSourceExtraE9add_blockINtNtCsaFPxhswmqCN_5alloc3vec3VechEINtNtNtNtB1d_4iter8adapters3map3MapINtNtB2Y_9into_iter8IntoIterNtB1O_13JustificationENCINvMB1O_INtB1O_7AllSyncNtNtCsdLp0VRM2NUW_12futures_util9abortable11AbortHandleTNtNtNtBc_6libp2p7peer_id6PeerIdNtNtNtNtBc_7network5codec15block_announces4RoleEuE23blocks_request_responseINtNtB3z_10filter_map9FilterMapIB44_NtNtB6J_13block_request9BlockDataENCNCINvNtNtCs508CmrSPkZh_13smoldot_light12sync_service10standalone22start_standalone_chainNtNtCs7snhGEhbuap_18smoldot_light_wasm8platform11PlatformRefE0sn_0EE0EE0Baw_
Unexecuted instantiation: _RNCINvMs4_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtBa_3all20AllForksRequestExtraNtB30_19AllForksSourceExtraE9add_blockINtNtCsaFPxhswmqCN_5alloc3vec3VechEINtNtNtNtB1d_4iter8adapters3map3MapINtNtB4a_9into_iter8IntoIterNtB30_13JustificationENCINvMB30_INtB30_7AllSyncuIB19_NtB1M_17NetworkSourceInfoEB1K_E23blocks_request_responseIB4H_IB5g_NtNtNtNtBc_7network5codec13block_request9BlockDataENCNCNvMs_B1M_NtB1M_14SyncBackground3run0sc_0EE0EE0CsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNCINvMs4_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtBa_3all20AllForksRequestExtraNtB30_19AllForksSourceExtraE9add_blockINtNtCsaFPxhswmqCN_5alloc3vec3VechEINtNtNtNtB1d_4iter8adapters3map3MapINtNtB4a_9into_iter8IntoIterNtB30_13JustificationENCINvMB30_INtB30_7AllSyncuIB19_NtB1M_17NetworkSourceInfoEB1K_E23blocks_request_responseINtNtNtB4N_7sources4once4OnceINtB30_24BlockRequestSuccessBlockB1K_EEE0EE0CsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNCINvMs4_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtBa_3all20AllForksRequestExtraNtB30_19AllForksSourceExtraE9add_blockINtNtCsaFPxhswmqCN_5alloc3vec3VechEINtNtNtNtB1d_4iter8adapters3map3MapINtNtB4a_9into_iter8IntoIterNtB30_13JustificationENCINvMB30_INtB30_7AllSyncuIB19_NtB1M_17NetworkSourceInfoEB1K_E23blocks_request_responseIB4H_IB5g_NtNtNtNtBc_7network5codec13block_request9BlockDataENCNCNvMs_B1M_NtB1M_14SyncBackground3run0sc_0EE0EE0Cs4VrkfB1pvQ3_25json_rpc_general_requests
Unexecuted instantiation: _RNCINvMs4_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB8_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtBa_3all20AllForksRequestExtraNtB30_19AllForksSourceExtraE9add_blockINtNtCsaFPxhswmqCN_5alloc3vec3VechEINtNtNtNtB1d_4iter8adapters3map3MapINtNtB4a_9into_iter8IntoIterNtB30_13JustificationENCINvMB30_INtB30_7AllSyncuIB19_NtB1M_17NetworkSourceInfoEB1K_E23blocks_request_responseINtNtNtB4N_7sources4once4OnceINtB30_24BlockRequestSuccessBlockB1K_EEE0EE0Cs4VrkfB1pvQ3_25json_rpc_general_requests
1132
0
            .collect::<Vec<_>>();
1133
1134
        // If the block is already part of the local tree of blocks, nothing more to do.
1135
        // Note that the block body is silently discarded, as in the API only non-verified blocks
1136
        // exhibit a body.
1137
0
        if self
1138
0
            .inner
1139
0
            .chain
1140
0
            .contains_non_finalized_block(&self.expected_next_hash)
1141
        {
1142
0
            if !justifications.is_empty() {
1143
0
                self.inner.inner.blocks[self.source_id]
1144
0
                    .unverified_finality_proofs
1145
0
                    .insert(
1146
0
                        decoded_header.number,
1147
0
                        FinalityProofs::Justifications(justifications),
1148
0
                    );
1149
0
            }
1150
1151
0
            return Ok(AddBlock::AlreadyInChain(AddBlockOccupied {
1152
0
                inner: self,
1153
0
                block_number: decoded_header.number,
1154
0
                block_parent_hash: *decoded_header.parent_hash,
1155
0
                block_header: scale_encoded_header,
1156
0
                is_verified: true,
1157
0
            }));
1158
0
        }
1159
1160
        // Block is not part of the finalized chain.
1161
        // TODO: also give possibility to update user data
1162
0
        if decoded_header.number == self.inner.chain.finalized_block_height() + 1
1163
0
            && *decoded_header.parent_hash != *self.inner.chain.finalized_block_hash()
1164
        {
1165
            // TODO: remove_verify_failed
1166
            // Block isn't part of the finalized chain.
1167
            // This doesn't necessarily mean that the source and the local node disagree
1168
            // on the finalized chain. It is possible that the finalized block has been
1169
            // updated between the moment the request was emitted and the moment the
1170
            // response is received.
1171
0
            let error = AncestrySearchResponseError::NotFinalizedChain {
1172
0
                discarded_unverified_block_headers: Vec::new(), // TODO: not properly implemented /!\
1173
0
            };
1174
0
            return Err(error);
1175
0
        }
1176
1177
        // At this point, we have excluded blocks that are already part of the chain or too old.
1178
        // We insert the block in the list of unverified blocks so as to treat all blocks the
1179
        // same.
1180
0
        if !self
1181
0
            .inner
1182
0
            .inner
1183
0
            .blocks
1184
0
            .contains_unverified_block(decoded_header.number, &self.expected_next_hash)
1185
        {
1186
0
            Ok(AddBlock::UnknownBlock(AddBlockVacant {
1187
0
                inner: self,
1188
0
                block_number: decoded_header.number,
1189
0
                block_parent_hash: *decoded_header.parent_hash,
1190
0
                block_header: scale_encoded_header,
1191
0
                scale_encoded_extrinsics,
1192
0
                justifications,
1193
0
            }))
1194
        } else {
1195
0
            if !justifications.is_empty() {
1196
0
                self.inner.inner.blocks[self.source_id]
1197
0
                    .unverified_finality_proofs
1198
0
                    .insert(
1199
0
                        decoded_header.number,
1200
0
                        FinalityProofs::Justifications(justifications),
1201
0
                    );
1202
0
            }
1203
1204
0
            if self.inner.inner.blocks.downloading_bodies() {
1205
0
                self.inner
1206
0
                    .inner
1207
0
                    .blocks
1208
0
                    .set_unverified_block_header_body_known(
1209
0
                        decoded_header.number,
1210
0
                        &self.expected_next_hash,
1211
0
                        *decoded_header.parent_hash,
1212
0
                    );
1213
0
                self.inner
1214
0
                    .inner
1215
0
                    .blocks
1216
0
                    .unverified_block_user_data_mut(decoded_header.number, &self.expected_next_hash)
1217
0
                    .body = Some(scale_encoded_extrinsics);
1218
0
            }
1219
1220
0
            Ok(AddBlock::AlreadyPending(AddBlockOccupied {
1221
0
                inner: self,
1222
0
                block_number: decoded_header.number,
1223
0
                block_parent_hash: *decoded_header.parent_hash,
1224
0
                block_header: scale_encoded_header,
1225
0
                is_verified: false,
1226
0
            }))
1227
        }
1228
0
    }
Unexecuted instantiation: _RINvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB6_13FinishRequestpppE9add_blockppEBa_
Unexecuted instantiation: _RINvMs4_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2Y_19AllForksSourceExtraE9add_blockINtNtCsaFPxhswmqCN_5alloc3vec3VechEINtNtNtNtB1b_4iter8adapters3map3MapINtNtB48_9into_iter8IntoIterNtB2Y_13JustificationENCINvMB2Y_INtB2Y_7AllSyncuIB17_NtB1K_17NetworkSourceInfoEB1I_E23blocks_request_responseIB4F_IB5e_NtNtNtNtBa_7network5codec13block_request9BlockDataENCNCNvMs_B1K_NtB1K_14SyncBackground3run0sc_0EE0EECscoAnRPySggw_6author
Unexecuted instantiation: _RINvMs4_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2Y_19AllForksSourceExtraE9add_blockINtNtCsaFPxhswmqCN_5alloc3vec3VechEINtNtNtNtB1b_4iter8adapters3map3MapINtNtB48_9into_iter8IntoIterNtB2Y_13JustificationENCINvMB2Y_INtB2Y_7AllSyncuIB17_NtB1K_17NetworkSourceInfoEB1I_E23blocks_request_responseINtNtNtB4L_7sources4once4OnceINtB2Y_24BlockRequestSuccessBlockB1I_EEE0EECscoAnRPySggw_6author
Unexecuted instantiation: _RINvMs4_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_13FinishRequestpppE9add_blockppEBa_
Unexecuted instantiation: _RINvMs4_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB8_3all20AllForksRequestExtraNtB1M_19AllForksSourceExtraE9add_blockINtNtCsaFPxhswmqCN_5alloc3vec3VechEINtNtNtNtB1b_4iter8adapters3map3MapINtNtB2W_9into_iter8IntoIterNtB1M_13JustificationENCINvMB1M_INtB1M_7AllSyncNtNtCsdLp0VRM2NUW_12futures_util9abortable11AbortHandleTNtNtNtBa_6libp2p7peer_id6PeerIdNtNtNtNtBa_7network5codec15block_announces4RoleEuE23blocks_request_responseINtNtB3x_10filter_map9FilterMapIB42_NtNtB6H_13block_request9BlockDataENCNCINvNtNtCs508CmrSPkZh_13smoldot_light12sync_service10standalone22start_standalone_chainNtNtCs7snhGEhbuap_18smoldot_light_wasm8platform11PlatformRefE0sn_0EE0EEBau_
Unexecuted instantiation: _RINvMs4_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2Y_19AllForksSourceExtraE9add_blockINtNtCsaFPxhswmqCN_5alloc3vec3VechEINtNtNtNtB1b_4iter8adapters3map3MapINtNtB48_9into_iter8IntoIterNtB2Y_13JustificationENCINvMB2Y_INtB2Y_7AllSyncuIB17_NtB1K_17NetworkSourceInfoEB1I_E23blocks_request_responseIB4F_IB5e_NtNtNtNtBa_7network5codec13block_request9BlockDataENCNCNvMs_B1K_NtB1K_14SyncBackground3run0sc_0EE0EECsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RINvMs4_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2Y_19AllForksSourceExtraE9add_blockINtNtCsaFPxhswmqCN_5alloc3vec3VechEINtNtNtNtB1b_4iter8adapters3map3MapINtNtB48_9into_iter8IntoIterNtB2Y_13JustificationENCINvMB2Y_INtB2Y_7AllSyncuIB17_NtB1K_17NetworkSourceInfoEB1I_E23blocks_request_responseINtNtNtB4L_7sources4once4OnceINtB2Y_24BlockRequestSuccessBlockB1I_EEE0EECsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RINvMs4_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2Y_19AllForksSourceExtraE9add_blockINtNtCsaFPxhswmqCN_5alloc3vec3VechEINtNtNtNtB1b_4iter8adapters3map3MapINtNtB48_9into_iter8IntoIterNtB2Y_13JustificationENCINvMB2Y_INtB2Y_7AllSyncuIB17_NtB1K_17NetworkSourceInfoEB1I_E23blocks_request_responseIB4F_IB5e_NtNtNtNtBa_7network5codec13block_request9BlockDataENCNCNvMs_B1K_NtB1K_14SyncBackground3run0sc_0EE0EECs4VrkfB1pvQ3_25json_rpc_general_requests
Unexecuted instantiation: _RINvMs4_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB6_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB8_3all20AllForksRequestExtraNtB2Y_19AllForksSourceExtraE9add_blockINtNtCsaFPxhswmqCN_5alloc3vec3VechEINtNtNtNtB1b_4iter8adapters3map3MapINtNtB48_9into_iter8IntoIterNtB2Y_13JustificationENCINvMB2Y_INtB2Y_7AllSyncuIB17_NtB1K_17NetworkSourceInfoEB1I_E23blocks_request_responseINtNtNtB4L_7sources4once4OnceINtB2Y_24BlockRequestSuccessBlockB1I_EEE0EECs4VrkfB1pvQ3_25json_rpc_general_requests
1229
1230
    /// Notifies of the end of the response, and returns back the [`AllForksSync`].
1231
    ///
1232
    /// It is legal to insert fewer blocks than the number of blocks that were requested through
1233
    /// [`RequestParams::num_blocks`].
1234
    /// However, if no block has been added at all (i.e. the response is empty), then the source
1235
    /// of the request is marked as bad.
1236
    ///
1237
    /// > **Note**: Network protocols have a limit to the size of their response, meaning that all
1238
    /// >           the requested blocks might not fit in a single response. For this reason, it
1239
    /// >           is legal for a response to be shorter than expected.
1240
0
    pub fn finish(self) {
1241
0
        drop(self);
1242
0
    }
Unexecuted instantiation: _RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_13FinishRequestpppE6finishB9_
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_13FinishRequestpppE6finishB9_
1243
}
1244
1245
impl<'a, TBl, TRq, TSrc> Drop for FinishRequest<'a, TBl, TRq, TSrc> {
1246
0
    fn drop(&mut self) {
1247
        // If this is reached, then none of the blocks the source has sent back were useful.
1248
0
        if !self.any_progress {
1249
0
            // Assume that the source doesn't know this block, as it is apparently unable to
1250
0
            // serve it anyway. This avoids sending the same request to the same source over and
1251
0
            // over again.
1252
0
            self.inner.inner.blocks.remove_known_block_of_source(
1253
0
                self.source_id,
1254
0
                self.requested_block_height,
1255
0
                &self.requested_block_hash,
1256
0
            );
1257
0
        }
1258
0
    }
Unexecuted instantiation: _RNvXININtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forkss5_0pppEINtB5_13FinishRequestpppENtNtNtCs1p5UDGgVI4d_4core3ops4drop4Drop4dropB9_
Unexecuted instantiation: _RNvXs5_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraENtNtNtB1a_3ops4drop4Drop4dropCscoAnRPySggw_6author
Unexecuted instantiation: _RNvXININtNtCsc1ywvx6YAnK_7smoldot4sync9all_forkss5_0pppEINtB5_13FinishRequestpppENtNtNtCs1p5UDGgVI4d_4core3ops4drop4Drop4dropB9_
Unexecuted instantiation: _RNvXs5_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1L_19AllForksSourceExtraENtNtNtB1a_3ops4drop4Drop4dropCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvXs5_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraENtNtNtB1a_3ops4drop4Drop4dropCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvXs5_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_13FinishRequestINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2X_19AllForksSourceExtraENtNtNtB1a_3ops4drop4Drop4dropCs4VrkfB1pvQ3_25json_rpc_general_requests
1259
}
1260
1261
/// Result of calling [`FinishRequest::add_block`].
1262
pub enum AddBlock<'a, TBl, TRq, TSrc> {
1263
    /// The block is already in the list of unverified blocks.
1264
    AlreadyPending(AddBlockOccupied<'a, TBl, TRq, TSrc>),
1265
1266
    /// The block hasn't been heard of before.
1267
    UnknownBlock(AddBlockVacant<'a, TBl, TRq, TSrc>),
1268
1269
    /// The block is already in the list of verified blocks.
1270
    ///
1271
    /// This can happen for example if a block announce or different ancestry search response has
1272
    /// been processed in between the request and response.
1273
    AlreadyInChain(AddBlockOccupied<'a, TBl, TRq, TSrc>),
1274
}
1275
1276
/// See [`FinishRequest::add_block`] and [`AddBlock`].
1277
pub struct AddBlockOccupied<'a, TBl, TRq, TSrc> {
1278
    inner: FinishRequest<'a, TBl, TRq, TSrc>,
1279
    block_header: Vec<u8>,
1280
    block_number: u64,
1281
    block_parent_hash: [u8; 32],
1282
    is_verified: bool,
1283
}
1284
1285
impl<'a, TBl, TRq, TSrc> AddBlockOccupied<'a, TBl, TRq, TSrc> {
1286
    /// Gives access to the user data of the block.
1287
0
    pub fn user_data_mut(&mut self) -> &mut TBl {
1288
0
        if self.is_verified {
1289
0
            &mut self.inner.inner.chain[&self.inner.expected_next_hash]
1290
        } else {
1291
0
            &mut self
1292
0
                .inner
1293
0
                .inner
1294
0
                .inner
1295
0
                .blocks
1296
0
                .unverified_block_user_data_mut(self.block_number, &self.inner.expected_next_hash)
1297
0
                .user_data
1298
        }
1299
0
    }
Unexecuted instantiation: _RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_16AddBlockOccupiedpppE13user_data_mutB9_
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_16AddBlockOccupiedpppE13user_data_mutB9_
1300
1301
    /// Replace the existing user data of the block.
1302
    ///
1303
    /// Returns an object that allows continuing inserting blocks, plus the former user data that
1304
    /// was overwritten by the new one.
1305
0
    pub fn replace(mut self, user_data: TBl) -> (FinishRequest<'a, TBl, TRq, TSrc>, TBl) {
1306
        // Update the view the state machine maintains for this source.
1307
0
        self.inner.inner.inner.blocks.add_known_block_to_source(
1308
0
            self.inner.source_id,
1309
0
            self.block_number,
1310
0
            self.inner.expected_next_hash,
1311
        );
1312
1313
        // Source also knows the parent of the announced block.
1314
        // TODO: do this for the entire chain of blocks if it is known locally?
1315
0
        self.inner.inner.inner.blocks.add_known_block_to_source(
1316
0
            self.inner.source_id,
1317
0
            self.block_number - 1,
1318
0
            self.block_parent_hash,
1319
        );
1320
1321
0
        let former_user_data = if self.is_verified {
1322
0
            mem::replace(
1323
0
                &mut self.inner.inner.chain[&self.inner.expected_next_hash],
1324
0
                user_data,
1325
            )
1326
        } else {
1327
0
            self.inner
1328
0
                .inner
1329
0
                .inner
1330
0
                .blocks
1331
0
                .set_unverified_block_header_known(
1332
0
                    self.block_number,
1333
0
                    &self.inner.expected_next_hash,
1334
0
                    self.block_parent_hash,
1335
                );
1336
1337
0
            let block_user_data = self
1338
0
                .inner
1339
0
                .inner
1340
0
                .inner
1341
0
                .blocks
1342
0
                .unverified_block_user_data_mut(self.block_number, &self.inner.expected_next_hash);
1343
0
            if block_user_data.header.is_none() {
1344
0
                block_user_data.header = Some(self.block_header);
1345
0
                // TODO: copying bytes :-/
1346
0
            }
1347
1348
0
            mem::replace(&mut block_user_data.user_data, user_data)
1349
        };
1350
1351
        // Update the state machine for the next iteration.
1352
        // Note: this can't be reached if `expected_next_height` is 0, because that should have
1353
        // resulted either in `NotFinalizedChain` or `AlreadyInChain`, both of which return early.
1354
0
        self.inner.expected_next_hash = self.block_parent_hash;
1355
0
        self.inner.expected_next_height -= 1;
1356
0
        self.inner.index_in_response += 1;
1357
0
        (self.inner, former_user_data)
1358
0
    }
Unexecuted instantiation: _RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_16AddBlockOccupiedpppE7replaceB9_
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_16AddBlockOccupiedINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB30_19AllForksSourceExtraE7replaceCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_16AddBlockOccupiedpppE7replaceB9_
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_16AddBlockOccupiedINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1O_19AllForksSourceExtraE7replaceCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_16AddBlockOccupiedINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB30_19AllForksSourceExtraE7replaceCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_16AddBlockOccupiedINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB30_19AllForksSourceExtraE7replaceCs4VrkfB1pvQ3_25json_rpc_general_requests
1359
}
1360
1361
/// See [`FinishRequest::add_block`] and [`AddBlock`].
1362
pub struct AddBlockVacant<'a, TBl, TRq, TSrc> {
1363
    inner: FinishRequest<'a, TBl, TRq, TSrc>,
1364
    block_header: Vec<u8>,
1365
    block_number: u64,
1366
    block_parent_hash: [u8; 32],
1367
    justifications: Vec<([u8; 4], Vec<u8>)>,
1368
    scale_encoded_extrinsics: Vec<Vec<u8>>,
1369
}
1370
1371
impl<'a, TBl, TRq, TSrc> AddBlockVacant<'a, TBl, TRq, TSrc> {
1372
    /// Insert the block in the state machine, with the given user data.
1373
0
    pub fn insert(mut self, user_data: TBl) -> FinishRequest<'a, TBl, TRq, TSrc> {
1374
        // Update the view the state machine maintains for this source.
1375
0
        self.inner.inner.inner.blocks.add_known_block_to_source(
1376
0
            self.inner.source_id,
1377
0
            self.block_number,
1378
0
            self.inner.expected_next_hash,
1379
        );
1380
1381
        // Source also knows the parent of the announced block.
1382
        // TODO: do this for the entire chain of blocks if it is known locally?
1383
0
        self.inner.inner.inner.blocks.add_known_block_to_source(
1384
0
            self.inner.source_id,
1385
0
            self.block_number - 1,
1386
0
            self.block_parent_hash,
1387
        );
1388
1389
0
        self.inner.inner.inner.blocks.insert_unverified_block(
1390
0
            self.block_number,
1391
0
            self.inner.expected_next_hash,
1392
0
            if self.inner.inner.inner.blocks.downloading_bodies() {
1393
0
                pending_blocks::UnverifiedBlockState::HeaderBody {
1394
0
                    parent_hash: self.block_parent_hash,
1395
0
                }
1396
            } else {
1397
0
                pending_blocks::UnverifiedBlockState::Header {
1398
0
                    parent_hash: self.block_parent_hash,
1399
0
                }
1400
            },
1401
0
            PendingBlock {
1402
0
                header: Some(self.block_header),
1403
0
                body: Some(self.scale_encoded_extrinsics),
1404
0
                user_data,
1405
0
            },
1406
        );
1407
1408
0
        if !self.justifications.is_empty() {
1409
0
            self.inner.inner.inner.blocks[self.inner.source_id]
1410
0
                .unverified_finality_proofs
1411
0
                .insert(
1412
0
                    self.block_number,
1413
0
                    FinalityProofs::Justifications(self.justifications),
1414
0
                );
1415
0
        }
1416
1417
        // If there are too many blocks stored in the blocks list, remove unnecessary ones.
1418
        // Not doing this could lead to an explosion of the size of the collections.
1419
        // TODO: removing blocks should only be done explicitly through an API endpoint, because we want to store user datas in unverified blocks too; see https://github.com/paritytech/smoldot/issues/1572
1420
0
        while self.inner.inner.inner.blocks.num_unverified_blocks() >= 100 {
1421
            // TODO: arbitrary constant
1422
0
            let (height, hash) = match self
1423
0
                .inner
1424
0
                .inner
1425
0
                .inner
1426
0
                .blocks
1427
0
                .unnecessary_unverified_blocks()
1428
0
                .next()
1429
            {
1430
0
                Some((n, h)) => (n, *h),
1431
0
                None => break,
1432
            };
1433
1434
            // TODO: restore this block of code; it is extremely complicated because it is unclear which source-block combinations we can add and keep without making memory usage explode
1435
            /*self.inner
1436
            .inner
1437
            .inner
1438
            .blocks
1439
            .remove_sources_known_block(height, &hash);*/
1440
0
            self.inner
1441
0
                .inner
1442
0
                .inner
1443
0
                .blocks
1444
0
                .remove_unverified_block(height, &hash);
1445
        }
1446
1447
        // Update the state machine for the next iteration.
1448
        // Note: this can't be reached if `expected_next_height` is 0, because that should have
1449
        // resulted either in `NotFinalizedChain` or `AlreadyInChain`, both of which return early.
1450
0
        self.inner.expected_next_hash = self.block_parent_hash;
1451
0
        self.inner.expected_next_height -= 1;
1452
0
        self.inner.index_in_response += 1;
1453
0
        self.inner
1454
0
    }
Unexecuted instantiation: _RNvMs7_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_14AddBlockVacantpppE6insertB9_
Unexecuted instantiation: _RNvMs7_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_14AddBlockVacantINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2Y_19AllForksSourceExtraE6insertCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs7_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_14AddBlockVacantpppE6insertB9_
Unexecuted instantiation: _RNvMs7_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_14AddBlockVacantINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1M_19AllForksSourceExtraE6insertCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMs7_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_14AddBlockVacantINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2Y_19AllForksSourceExtraE6insertCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs7_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_14AddBlockVacantINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2Y_19AllForksSourceExtraE6insertCs4VrkfB1pvQ3_25json_rpc_general_requests
1455
}
1456
1457
/// Outcome of calling [`AllForksSync::block_announce`].
1458
pub enum BlockAnnounceOutcome<'a, TBl, TRq, TSrc> {
1459
    /// Announced block is too old to be part of the finalized chain.
1460
    ///
1461
    /// It is assumed that all sources will eventually agree on the same finalized chain. Blocks
1462
    /// whose height is inferior to the height of the latest known finalized block should simply
1463
    /// be ignored. Whether or not this old block is indeed part of the finalized block isn't
1464
    /// verified, and it is assumed that the source is simply late.
1465
    ///
1466
    /// If the announced block was the source's best block, the state machine has been updated to
1467
    /// take this information into account.
1468
    TooOld {
1469
        /// Height of the announced block.
1470
        announce_block_height: u64,
1471
        /// Height of the currently finalized block.
1472
        finalized_block_height: u64,
1473
    },
1474
1475
    /// Announced block has already been successfully verified and is part of the non-finalized
1476
    /// chain.
1477
    AlreadyVerified(AnnouncedBlockKnown<'a, TBl, TRq, TSrc>),
1478
1479
    /// Announced block is already known by the state machine but hasn't been verified yet.
1480
    AlreadyPending(AnnouncedBlockKnown<'a, TBl, TRq, TSrc>),
1481
1482
    /// Announced block isn't in the state machine.
1483
    Unknown(AnnouncedBlockUnknown<'a, TBl, TRq, TSrc>),
1484
1485
    /// Failed to decode announce header.
1486
    InvalidHeader(header::Error),
1487
}
1488
1489
/// See [`BlockAnnounceOutcome`] and [`AllForksSync::block_announce`].
1490
#[must_use]
1491
pub struct AnnouncedBlockKnown<'a, TBl, TRq, TSrc> {
1492
    inner: &'a mut AllForksSync<TBl, TRq, TSrc>,
1493
    announced_header_hash: [u8; 32],
1494
    announced_header_parent_hash: [u8; 32],
1495
    announced_header_number: u64,
1496
    announced_header_encoded: Vec<u8>,
1497
    is_in_chain: bool,
1498
    is_best: bool,
1499
    source_id: SourceId,
1500
}
1501
1502
impl<'a, TBl, TRq, TSrc> AnnouncedBlockKnown<'a, TBl, TRq, TSrc> {
1503
    /// Returns the parent hash of the announced block.
1504
0
    pub fn parent_hash(&self) -> &[u8; 32] {
1505
0
        &self.announced_header_parent_hash
1506
0
    }
Unexecuted instantiation: _RNvMs8_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19AnnouncedBlockKnownpppE11parent_hashB9_
Unexecuted instantiation: _RNvMs8_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19AnnouncedBlockKnownpppE11parent_hashB9_
Unexecuted instantiation: _RNvMs8_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19AnnouncedBlockKnownINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1R_19AllForksSourceExtraE11parent_hashCs7snhGEhbuap_18smoldot_light_wasm
1507
1508
    /// Returns the height of the announced block.
1509
0
    pub fn height(&self) -> u64 {
1510
0
        self.announced_header_number
1511
0
    }
Unexecuted instantiation: _RNvMs8_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19AnnouncedBlockKnownpppE6heightB9_
Unexecuted instantiation: _RNvMs8_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19AnnouncedBlockKnownpppE6heightB9_
Unexecuted instantiation: _RNvMs8_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19AnnouncedBlockKnownINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1R_19AllForksSourceExtraE6heightCs7snhGEhbuap_18smoldot_light_wasm
1512
1513
    /// Returns the hash of the announced block.
1514
0
    pub fn hash(&self) -> &[u8; 32] {
1515
0
        &self.announced_header_hash
1516
0
    }
Unexecuted instantiation: _RNvMs8_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19AnnouncedBlockKnownpppE4hashB9_
Unexecuted instantiation: _RNvMs8_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19AnnouncedBlockKnownpppE4hashB9_
Unexecuted instantiation: _RNvMs8_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19AnnouncedBlockKnownINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1R_19AllForksSourceExtraE4hashCs7snhGEhbuap_18smoldot_light_wasm
1517
1518
    /// Gives access to the user data of the block.
1519
0
    pub fn user_data_mut(&mut self) -> &mut TBl {
1520
0
        if self.is_in_chain {
1521
0
            &mut self.inner.chain[&self.announced_header_hash]
1522
        } else {
1523
0
            &mut self
1524
0
                .inner
1525
0
                .inner
1526
0
                .blocks
1527
0
                .unverified_block_user_data_mut(
1528
0
                    self.announced_header_number,
1529
0
                    &self.announced_header_hash,
1530
0
                )
1531
0
                .user_data
1532
        }
1533
0
    }
Unexecuted instantiation: _RNvMs8_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19AnnouncedBlockKnownpppE13user_data_mutB9_
Unexecuted instantiation: _RNvMs8_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19AnnouncedBlockKnownpppE13user_data_mutB9_
1534
1535
    /// Updates the state machine to keep track of the fact that this source knows this block.
1536
    /// If the announced block is the source's best block, also updates this information.
1537
0
    pub fn update_source_and_block(self) {
1538
        // No matter what is done below, start by updating the view the state machine maintains
1539
        // for this source.
1540
0
        if self.is_best {
1541
0
            self.inner
1542
0
                .inner
1543
0
                .blocks
1544
0
                .add_known_block_to_source_and_set_best(
1545
0
                    self.source_id,
1546
0
                    self.announced_header_number,
1547
0
                    self.announced_header_hash,
1548
0
                );
1549
0
        } else {
1550
0
            self.inner.inner.blocks.add_known_block_to_source(
1551
0
                self.source_id,
1552
0
                self.announced_header_number,
1553
0
                self.announced_header_hash,
1554
0
            );
1555
0
        }
1556
1557
        // Source also knows the parent of the announced block.
1558
0
        self.inner.inner.blocks.add_known_block_to_source(
1559
0
            self.source_id,
1560
0
            self.announced_header_number - 1,
1561
0
            self.announced_header_parent_hash,
1562
        );
1563
1564
0
        if !self.is_in_chain {
1565
0
            self.inner.inner.blocks.set_unverified_block_header_known(
1566
0
                self.announced_header_number,
1567
0
                &self.announced_header_hash,
1568
0
                self.announced_header_parent_hash,
1569
            );
1570
1571
0
            let block_user_data = self.inner.inner.blocks.unverified_block_user_data_mut(
1572
0
                self.announced_header_number,
1573
0
                &self.announced_header_hash,
1574
            );
1575
0
            if block_user_data.header.is_none() {
1576
0
                block_user_data.header = Some(self.announced_header_encoded);
1577
0
            }
1578
1579
            // Mark block as bad if it is not part of the finalized chain.
1580
            // This might not have been known before, as the header might not have been known.
1581
0
            if self.announced_header_number == self.inner.chain.finalized_block_height() + 1
1582
0
                && self.announced_header_parent_hash != *self.inner.chain.finalized_block_hash()
1583
0
            {
1584
0
                self.inner.inner.blocks.mark_unverified_block_as_bad(
1585
0
                    self.announced_header_number,
1586
0
                    &self.announced_header_hash,
1587
0
                );
1588
0
            }
1589
0
        }
1590
1591
        // TODO: if pending_blocks.num_blocks() > some_max { remove uninteresting block }
1592
0
    }
Unexecuted instantiation: _RNvMs8_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19AnnouncedBlockKnownpppE23update_source_and_blockB9_
Unexecuted instantiation: _RNvMs8_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19AnnouncedBlockKnownINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE23update_source_and_blockCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs8_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19AnnouncedBlockKnownpppE23update_source_and_blockB9_
Unexecuted instantiation: _RNvMs8_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19AnnouncedBlockKnownINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1R_19AllForksSourceExtraE23update_source_and_blockCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMs8_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19AnnouncedBlockKnownINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE23update_source_and_blockCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs8_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19AnnouncedBlockKnownINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE23update_source_and_blockCs4VrkfB1pvQ3_25json_rpc_general_requests
1593
}
1594
1595
/// See [`BlockAnnounceOutcome`] and [`AllForksSync::block_announce`].
1596
#[must_use]
1597
pub struct AnnouncedBlockUnknown<'a, TBl, TRq, TSrc> {
1598
    inner: &'a mut AllForksSync<TBl, TRq, TSrc>,
1599
    announced_header_hash: [u8; 32],
1600
    announced_header_parent_hash: [u8; 32],
1601
    announced_header_number: u64,
1602
    announced_header_encoded: Vec<u8>,
1603
    is_best: bool,
1604
    source_id: SourceId,
1605
}
1606
1607
impl<'a, TBl, TRq, TSrc> AnnouncedBlockUnknown<'a, TBl, TRq, TSrc> {
1608
    /// Returns the parent hash of the announced block.
1609
0
    pub fn parent_hash(&self) -> &[u8; 32] {
1610
0
        &self.announced_header_parent_hash
1611
0
    }
Unexecuted instantiation: _RNvMs9_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_21AnnouncedBlockUnknownpppE11parent_hashB9_
Unexecuted instantiation: _RNvMs9_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_21AnnouncedBlockUnknownpppE11parent_hashB9_
Unexecuted instantiation: _RNvMs9_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_21AnnouncedBlockUnknownINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1T_19AllForksSourceExtraE11parent_hashCs7snhGEhbuap_18smoldot_light_wasm
1612
1613
    /// Returns the height of the announced block.
1614
0
    pub fn height(&self) -> u64 {
1615
0
        self.announced_header_number
1616
0
    }
Unexecuted instantiation: _RNvMs9_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_21AnnouncedBlockUnknownpppE6heightB9_
Unexecuted instantiation: _RNvMs9_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_21AnnouncedBlockUnknownpppE6heightB9_
Unexecuted instantiation: _RNvMs9_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_21AnnouncedBlockUnknownINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1T_19AllForksSourceExtraE6heightCs7snhGEhbuap_18smoldot_light_wasm
1617
1618
    /// Returns the hash of the announced block.
1619
0
    pub fn hash(&self) -> &[u8; 32] {
1620
0
        &self.announced_header_hash
1621
0
    }
Unexecuted instantiation: _RNvMs9_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_21AnnouncedBlockUnknownpppE4hashB9_
Unexecuted instantiation: _RNvMs9_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_21AnnouncedBlockUnknownpppE4hashB9_
Unexecuted instantiation: _RNvMs9_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_21AnnouncedBlockUnknownINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1T_19AllForksSourceExtraE4hashCs7snhGEhbuap_18smoldot_light_wasm
1622
1623
    /// Inserts the block in the state machine and keeps track of the fact that this source knows
1624
    /// this block.
1625
    ///
1626
    /// If the announced block is the source's best block, also updates this information.
1627
0
    pub fn insert_and_update_source(self, user_data: TBl) {
1628
        // No matter what is done below, start by updating the view the state machine maintains
1629
        // for this source.
1630
0
        if self.is_best {
1631
0
            self.inner
1632
0
                .inner
1633
0
                .blocks
1634
0
                .add_known_block_to_source_and_set_best(
1635
0
                    self.source_id,
1636
0
                    self.announced_header_number,
1637
0
                    self.announced_header_hash,
1638
0
                );
1639
0
        } else {
1640
0
            self.inner.inner.blocks.add_known_block_to_source(
1641
0
                self.source_id,
1642
0
                self.announced_header_number,
1643
0
                self.announced_header_hash,
1644
0
            );
1645
0
        }
1646
1647
        // Source also knows the parent of the announced block.
1648
0
        self.inner.inner.blocks.add_known_block_to_source(
1649
0
            self.source_id,
1650
0
            self.announced_header_number - 1,
1651
0
            self.announced_header_parent_hash,
1652
        );
1653
1654
0
        self.inner.inner.blocks.insert_unverified_block(
1655
0
            self.announced_header_number,
1656
0
            self.announced_header_hash,
1657
0
            pending_blocks::UnverifiedBlockState::Header {
1658
0
                parent_hash: self.announced_header_parent_hash,
1659
0
            },
1660
0
            PendingBlock {
1661
0
                header: Some(self.announced_header_encoded),
1662
0
                body: None,
1663
0
                user_data,
1664
0
            },
1665
        );
1666
1667
        // If there are too many blocks stored in the blocks list, remove unnecessary ones.
1668
        // Not doing this could lead to an explosion of the size of the collections.
1669
        // TODO: removing blocks should only be done explicitly through an API endpoint, because we want to store user datas in unverified blocks too; see https://github.com/paritytech/smoldot/issues/1572
1670
0
        while self.inner.inner.blocks.num_unverified_blocks() >= 100 {
1671
            // TODO: arbitrary constant
1672
0
            let (height, hash) = match self
1673
0
                .inner
1674
0
                .inner
1675
0
                .blocks
1676
0
                .unnecessary_unverified_blocks()
1677
0
                .next()
1678
            {
1679
0
                Some((n, h)) => (n, *h),
1680
0
                None => break,
1681
            };
1682
1683
            // TODO: restore this block of code; it is extremely complicated because it is unclear which source-block combinations we can add and keep without making memory usage explode
1684
            /*self.inner
1685
            .inner
1686
            .blocks
1687
            .remove_sources_known_block(height, &hash);*/
1688
0
            self.inner
1689
0
                .inner
1690
0
                .blocks
1691
0
                .remove_unverified_block(height, &hash);
1692
        }
1693
1694
        // TODO: if pending_blocks.num_blocks() > some_max { remove uninteresting block }
1695
0
    }
Unexecuted instantiation: _RNvMs9_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_21AnnouncedBlockUnknownpppE24insert_and_update_sourceB9_
Unexecuted instantiation: _RNvMs9_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_21AnnouncedBlockUnknownINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB35_19AllForksSourceExtraE24insert_and_update_sourceCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs9_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_21AnnouncedBlockUnknownpppE24insert_and_update_sourceB9_
Unexecuted instantiation: _RNvMs9_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_21AnnouncedBlockUnknownINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1T_19AllForksSourceExtraE24insert_and_update_sourceCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMs9_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_21AnnouncedBlockUnknownINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB35_19AllForksSourceExtraE24insert_and_update_sourceCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs9_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_21AnnouncedBlockUnknownINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB35_19AllForksSourceExtraE24insert_and_update_sourceCs4VrkfB1pvQ3_25json_rpc_general_requests
1696
}
1697
1698
/// Error when adding a block using [`FinishRequest::add_block`].
1699
pub enum AncestrySearchResponseError {
1700
    /// Failed to decode block header.
1701
    InvalidHeader(header::Error),
1702
1703
    /// Provided block isn't a block that we expect to be added.
1704
    ///
1705
    /// If this is the first block, then it doesn't correspond to the block that has been
1706
    /// requested. If this is not the first block, then it doesn't correspond to the parent of
1707
    /// the previous block that has been added.
1708
    UnexpectedBlock,
1709
1710
    /// List of SCALE-encoded extrinsics doesn't match the extrinsics root found in the header.
1711
    ///
1712
    /// This can only happen if [`Config::download_bodies`] was `true`.
1713
    ExtrinsicsRootMismatch,
1714
1715
    /// The block height is equal to the locally-known finalized block height, but its hash isn't
1716
    /// the same.
1717
    ///
1718
    /// This doesn't necessarily mean that the source is malicious or uses a different chain. It
1719
    /// is possible for this to legitimately happen, for example if the finalized chain has been
1720
    /// updated while the ancestry search was in progress.
1721
    NotFinalizedChain {
1722
        /// List of block headers that were pending verification and that have now been discarded
1723
        /// since it has been found out that they don't belong to the finalized chain.
1724
        discarded_unverified_block_headers: Vec<Vec<u8>>,
1725
    },
1726
1727
    /// Height of the block is below the height of the finalized block.
1728
    ///
1729
    /// Note that in most situation the previous block should have returned a
1730
    /// [`AncestrySearchResponseError::NotFinalizedChain`] as we notice that its height is equal
1731
    /// to the finalized block's height but hash is different.
1732
    /// However, a [`AncestrySearchResponseError::TooOld`] can still happen in some niche
1733
    /// situations, such as an update to the finalized block height above the first block of the
1734
    /// request.
1735
    TooOld,
1736
}
1737
1738
/// Outcome of calling [`AllForksSync::prepare_add_source`].
1739
#[must_use]
1740
pub enum AddSource<'a, TBl, TRq, TSrc> {
1741
    /// The best block of the source is older or equal to the local latest finalized block. This
1742
    /// block isn't tracked by the state machine.
1743
    OldBestBlock(AddSourceOldBlock<'a, TBl, TRq, TSrc>),
1744
1745
    /// The best block of the source has already been verified by this state machine.
1746
    BestBlockAlreadyVerified(AddSourceKnown<'a, TBl, TRq, TSrc>),
1747
1748
    /// The best block of the source is already known to this state machine but hasn't been
1749
    /// verified yet.
1750
    BestBlockPendingVerification(AddSourceKnown<'a, TBl, TRq, TSrc>),
1751
1752
    /// The best block of the source isn't in this state machine yet and needs to be inserted.
1753
    UnknownBestBlock(AddSourceUnknown<'a, TBl, TRq, TSrc>),
1754
}
1755
1756
/// See [`AddSource`] and [`AllForksSync::prepare_add_source`].
1757
#[must_use]
1758
pub struct AddSourceOldBlock<'a, TBl, TRq, TSrc> {
1759
    inner: &'a mut AllForksSync<TBl, TRq, TSrc>,
1760
    best_block_number: u64,
1761
    best_block_hash: [u8; 32],
1762
}
1763
1764
impl<'a, TBl, TRq, TSrc> AddSourceOldBlock<'a, TBl, TRq, TSrc> {
1765
    /// Inserts a new source in the state machine.
1766
    ///
1767
    /// Returns the newly-allocated identifier for that source.
1768
    ///
1769
    /// The `user_data` parameter is opaque and decided entirely by the user. It can later be
1770
    /// retrieved using the `Index` trait implementation of the [`AllForksSync`].
1771
21
    pub fn add_source(self, source_user_data: TSrc) -> SourceId {
1772
21
        self.inner.inner.blocks.add_source(
1773
21
            Source {
1774
21
                user_data: source_user_data,
1775
21
                unverified_finality_proofs: SourcePendingJustificationProofs::None,
1776
21
                finalized_block_number: 0,
1777
21
                pending_finality_proofs: SourcePendingJustificationProofs::None,
1778
21
            },
1779
21
            self.best_block_number,
1780
21
            self.best_block_hash,
1781
        )
1782
21
    }
Unexecuted instantiation: _RNvMsa_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_17AddSourceOldBlockpppE10add_sourceB9_
Unexecuted instantiation: _RNvMsa_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_17AddSourceOldBlockINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB31_19AllForksSourceExtraE10add_sourceCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMsa_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_17AddSourceOldBlockpppE10add_sourceB9_
Unexecuted instantiation: _RNvMsa_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_17AddSourceOldBlockINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1P_19AllForksSourceExtraE10add_sourceCs7snhGEhbuap_18smoldot_light_wasm
_RNvMsa_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_17AddSourceOldBlockINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB31_19AllForksSourceExtraE10add_sourceCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1771
2
    pub fn add_source(self, source_user_data: TSrc) -> SourceId {
1772
2
        self.inner.inner.blocks.add_source(
1773
2
            Source {
1774
2
                user_data: source_user_data,
1775
2
                unverified_finality_proofs: SourcePendingJustificationProofs::None,
1776
2
                finalized_block_number: 0,
1777
2
                pending_finality_proofs: SourcePendingJustificationProofs::None,
1778
2
            },
1779
2
            self.best_block_number,
1780
2
            self.best_block_hash,
1781
        )
1782
2
    }
_RNvMsa_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_17AddSourceOldBlockINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB31_19AllForksSourceExtraE10add_sourceCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1771
19
    pub fn add_source(self, source_user_data: TSrc) -> SourceId {
1772
19
        self.inner.inner.blocks.add_source(
1773
19
            Source {
1774
19
                user_data: source_user_data,
1775
19
                unverified_finality_proofs: SourcePendingJustificationProofs::None,
1776
19
                finalized_block_number: 0,
1777
19
                pending_finality_proofs: SourcePendingJustificationProofs::None,
1778
19
            },
1779
19
            self.best_block_number,
1780
19
            self.best_block_hash,
1781
        )
1782
19
    }
1783
}
1784
1785
/// See [`AddSource`] and [`AllForksSync::prepare_add_source`].
1786
#[must_use]
1787
pub struct AddSourceKnown<'a, TBl, TRq, TSrc> {
1788
    inner: &'a mut AllForksSync<TBl, TRq, TSrc>,
1789
    best_block_number: u64,
1790
    best_block_hash: [u8; 32],
1791
}
1792
1793
impl<'a, TBl, TRq, TSrc> AddSourceKnown<'a, TBl, TRq, TSrc> {
1794
    /// Gives access to the user data of the block.
1795
0
    pub fn user_data_mut(&mut self) -> &mut TBl {
1796
0
        if let Some(block_access) = self
1797
0
            .inner
1798
0
            .chain
1799
0
            .non_finalized_block_user_data_mut(&self.best_block_hash)
1800
        {
1801
0
            block_access
1802
        } else {
1803
0
            &mut self
1804
0
                .inner
1805
0
                .inner
1806
0
                .blocks
1807
0
                .unverified_block_user_data_mut(self.best_block_number, &self.best_block_hash)
1808
0
                .user_data
1809
        }
1810
0
    }
Unexecuted instantiation: _RNvMsb_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_14AddSourceKnownpppE13user_data_mutB9_
Unexecuted instantiation: _RNvMsb_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_14AddSourceKnownpppE13user_data_mutB9_
1811
1812
    /// Inserts a new source in the state machine.
1813
    ///
1814
    /// Returns the newly-allocated identifier for that source.
1815
    ///
1816
    /// The `user_data` parameter is opaque and decided entirely by the user. It can later be
1817
    /// retrieved using the `Index` trait implementation of the [`AllForksSync`].
1818
0
    pub fn add_source(self, source_user_data: TSrc) -> SourceId {
1819
0
        self.inner.inner.blocks.add_source(
1820
0
            Source {
1821
0
                user_data: source_user_data,
1822
0
                unverified_finality_proofs: SourcePendingJustificationProofs::None,
1823
0
                finalized_block_number: 0,
1824
0
                pending_finality_proofs: SourcePendingJustificationProofs::None,
1825
0
            },
1826
0
            self.best_block_number,
1827
0
            self.best_block_hash,
1828
        )
1829
0
    }
Unexecuted instantiation: _RNvMsb_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_14AddSourceKnownpppE10add_sourceB9_
Unexecuted instantiation: _RNvMsb_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_14AddSourceKnownINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2Y_19AllForksSourceExtraE10add_sourceCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMsb_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_14AddSourceKnownpppE10add_sourceB9_
Unexecuted instantiation: _RNvMsb_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_14AddSourceKnownINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1M_19AllForksSourceExtraE10add_sourceCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMsb_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_14AddSourceKnownINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2Y_19AllForksSourceExtraE10add_sourceCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMsb_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_14AddSourceKnownINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2Y_19AllForksSourceExtraE10add_sourceCs4VrkfB1pvQ3_25json_rpc_general_requests
1830
}
1831
1832
/// See [`AddSource`] and [`AllForksSync::prepare_add_source`].
1833
#[must_use]
1834
pub struct AddSourceUnknown<'a, TBl, TRq, TSrc> {
1835
    inner: &'a mut AllForksSync<TBl, TRq, TSrc>,
1836
    best_block_number: u64,
1837
    best_block_hash: [u8; 32],
1838
}
1839
1840
impl<'a, TBl, TRq, TSrc> AddSourceUnknown<'a, TBl, TRq, TSrc> {
1841
    /// Inserts a new source in the state machine, plus the best block of that source.
1842
    ///
1843
    /// Returns the newly-allocated identifier for that source.
1844
    ///
1845
    /// The `source_user_data` parameter is opaque and decided entirely by the user. It can later
1846
    /// be retrieved using the `Index` trait implementation of the [`AllForksSync`].
1847
    ///
1848
    /// The `best_block_user_data` parameter is opaque and decided entirely by the user and is
1849
    /// associated with the best block of the newly-added source.
1850
0
    pub fn add_source_and_insert_block(
1851
0
        self,
1852
0
        source_user_data: TSrc,
1853
0
        best_block_user_data: TBl,
1854
0
    ) -> SourceId {
1855
0
        let source_id = self.inner.inner.blocks.add_source(
1856
0
            Source {
1857
0
                user_data: source_user_data,
1858
0
                unverified_finality_proofs: SourcePendingJustificationProofs::None,
1859
0
                finalized_block_number: 0,
1860
0
                pending_finality_proofs: SourcePendingJustificationProofs::None,
1861
0
            },
1862
0
            self.best_block_number,
1863
0
            self.best_block_hash,
1864
        );
1865
1866
0
        self.inner.inner.blocks.insert_unverified_block(
1867
0
            self.best_block_number,
1868
0
            self.best_block_hash,
1869
0
            pending_blocks::UnverifiedBlockState::HeightHash,
1870
0
            PendingBlock {
1871
0
                header: None,
1872
0
                body: None,
1873
0
                user_data: best_block_user_data,
1874
0
            },
1875
        );
1876
1877
0
        source_id
1878
0
    }
Unexecuted instantiation: _RNvMsc_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_16AddSourceUnknownpppE27add_source_and_insert_blockB9_
Unexecuted instantiation: _RNvMsc_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_16AddSourceUnknownINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB30_19AllForksSourceExtraE27add_source_and_insert_blockCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMsc_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_16AddSourceUnknownpppE27add_source_and_insert_blockB9_
Unexecuted instantiation: _RNvMsc_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_16AddSourceUnknownINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1O_19AllForksSourceExtraE27add_source_and_insert_blockCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMsc_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_16AddSourceUnknownINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB30_19AllForksSourceExtraE27add_source_and_insert_blockCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMsc_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_16AddSourceUnknownINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB30_19AllForksSourceExtraE27add_source_and_insert_blockCs4VrkfB1pvQ3_25json_rpc_general_requests
1879
}
1880
1881
/// Block verification to be performed.
1882
///
1883
/// Internally holds the [`AllForksSync`].
1884
pub struct BlockVerify<TBl, TRq, TSrc> {
1885
    parent: AllForksSync<TBl, TRq, TSrc>,
1886
    /// Block that can be verified.
1887
    block_to_verify: pending_blocks::TreeRoot,
1888
}
1889
1890
impl<TBl, TRq, TSrc> BlockVerify<TBl, TRq, TSrc> {
1891
    /// Returns the hash of the block to be verified.
1892
0
    pub fn hash(&self) -> &[u8; 32] {
1893
0
        &self.block_to_verify.block_hash
1894
0
    }
Unexecuted instantiation: _RNvMsd_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_11BlockVerifypppE4hashB9_
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_11BlockVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE4hashCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_11BlockVerifypppE4hashB9_
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_11BlockVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE4hashCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_11BlockVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE4hashCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_11BlockVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE4hashCs4VrkfB1pvQ3_25json_rpc_general_requests
1895
1896
    /// Returns the list of SCALE-encoded extrinsics of the block to verify.
1897
    ///
1898
    /// This is `Some` if and only if [`Config::download_bodies`] is `true`
1899
0
    pub fn scale_encoded_extrinsics(
1900
0
        &self,
1901
0
    ) -> Option<impl ExactSizeIterator<Item = impl AsRef<[u8]> + Clone> + Clone> {
1902
0
        if self.parent.inner.blocks.downloading_bodies() {
1903
            Some(
1904
0
                self.parent
1905
0
                    .inner
1906
0
                    .blocks
1907
0
                    .unverified_block_user_data(
1908
0
                        self.block_to_verify.block_number,
1909
0
                        &self.block_to_verify.block_hash,
1910
0
                    )
1911
0
                    .body
1912
0
                    .as_ref()
1913
                    // The block shouldn't have been proposed for verification if it doesn't
1914
                    // have its body available.
1915
0
                    .unwrap_or_else(|| unreachable!())
1916
0
                    .iter(),
1917
            )
1918
        } else {
1919
0
            None
1920
        }
1921
0
    }
Unexecuted instantiation: _RNvMsd_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_11BlockVerifypppE24scale_encoded_extrinsicsB9_
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_11BlockVerifypppE24scale_encoded_extrinsicsB9_
1922
1923
    /// Returns the SCALE-encoded header of the block about to be verified.
1924
0
    pub fn scale_encoded_header(&self) -> &[u8] {
1925
0
        self.parent
1926
0
            .inner
1927
0
            .blocks
1928
0
            .unverified_block_user_data(
1929
0
                self.block_to_verify.block_number,
1930
0
                &self.block_to_verify.block_hash,
1931
0
            )
1932
0
            .header
1933
0
            .as_ref()
1934
0
            .unwrap()
1935
0
    }
Unexecuted instantiation: _RNvMsd_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_11BlockVerifypppE20scale_encoded_headerB9_
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_11BlockVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE20scale_encoded_headerCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_11BlockVerifypppE20scale_encoded_headerB9_
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_11BlockVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE20scale_encoded_headerCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_11BlockVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE20scale_encoded_headerCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_11BlockVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE20scale_encoded_headerCs4VrkfB1pvQ3_25json_rpc_general_requests
1936
1937
    /// Perform the verification.
1938
0
    pub fn verify_header(
1939
0
        mut self,
1940
0
        now_from_unix_epoch: Duration,
1941
0
    ) -> HeaderVerifyOutcome<TBl, TRq, TSrc> {
1942
0
        let to_verify_scale_encoded_header = self.scale_encoded_header().to_owned(); // TODO: overhead
1943
1944
0
        let result = match self
1945
0
            .parent
1946
0
            .chain
1947
0
            .verify_header(to_verify_scale_encoded_header, now_from_unix_epoch)
1948
        {
1949
            Ok(blocks_tree::HeaderVerifySuccess::Verified {
1950
0
                verified_header,
1951
0
                is_new_best,
1952
            }) => {
1953
                // Block is valid!
1954
0
                Ok((verified_header, is_new_best))
1955
            }
1956
0
            Err(blocks_tree::HeaderVerifyError::VerificationFailed(error)) => {
1957
                // Remove the block from `pending_blocks`.
1958
0
                self.parent.inner.blocks.mark_unverified_block_as_bad(
1959
0
                    self.block_to_verify.block_number,
1960
0
                    &self.block_to_verify.block_hash,
1961
                );
1962
1963
0
                Err(HeaderVerifyError::VerificationFailed(error))
1964
            }
1965
            Err(blocks_tree::HeaderVerifyError::ConsensusMismatch) => {
1966
                // Remove the block from `pending_blocks`.
1967
0
                self.parent.inner.blocks.mark_unverified_block_as_bad(
1968
0
                    self.block_to_verify.block_number,
1969
0
                    &self.block_to_verify.block_hash,
1970
                );
1971
1972
0
                Err(HeaderVerifyError::ConsensusMismatch)
1973
            }
1974
            Err(blocks_tree::HeaderVerifyError::UnknownConsensusEngine) => {
1975
                // Remove the block from `pending_blocks`.
1976
0
                self.parent.inner.blocks.mark_unverified_block_as_bad(
1977
0
                    self.block_to_verify.block_number,
1978
0
                    &self.block_to_verify.block_hash,
1979
                );
1980
1981
0
                Err(HeaderVerifyError::UnknownConsensusEngine)
1982
            }
1983
            Ok(blocks_tree::HeaderVerifySuccess::Duplicate)
1984
            | Err(
1985
                blocks_tree::HeaderVerifyError::BadParent { .. }
1986
                | blocks_tree::HeaderVerifyError::InvalidHeader(_),
1987
0
            ) => unreachable!(),
1988
        };
1989
1990
0
        match result {
1991
0
            Ok((verified_header, is_new_best)) => HeaderVerifyOutcome::Success {
1992
0
                is_new_best,
1993
0
                success: HeaderVerifySuccess {
1994
0
                    parent: self.parent,
1995
0
                    block_to_verify: self.block_to_verify,
1996
0
                    verified_header,
1997
0
                },
1998
0
            },
1999
0
            Err(error) => HeaderVerifyOutcome::Error {
2000
0
                sync: self.parent,
2001
0
                error,
2002
0
            },
2003
        }
2004
0
    }
Unexecuted instantiation: _RNvMsd_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_11BlockVerifypppE13verify_headerB9_
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_11BlockVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE13verify_headerCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_11BlockVerifypppE13verify_headerB9_
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_11BlockVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1J_19AllForksSourceExtraE13verify_headerCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_11BlockVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE13verify_headerCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_11BlockVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB2V_19AllForksSourceExtraE13verify_headerCs4VrkfB1pvQ3_25json_rpc_general_requests
2005
2006
    /// Do not actually proceed with the verification.
2007
0
    pub fn cancel(self) -> AllForksSync<TBl, TRq, TSrc> {
2008
0
        self.parent
2009
0
    }
Unexecuted instantiation: _RNvMsd_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_11BlockVerifypppE6cancelB9_
Unexecuted instantiation: _RNvMsd_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_11BlockVerifypppE6cancelB9_
2010
}
2011
2012
/// Header verification successful.
2013
///
2014
/// Internally holds the [`AllForksSync`].
2015
pub struct HeaderVerifySuccess<TBl, TRq, TSrc> {
2016
    parent: AllForksSync<TBl, TRq, TSrc>,
2017
    block_to_verify: pending_blocks::TreeRoot,
2018
    verified_header: blocks_tree::VerifiedHeader,
2019
}
2020
2021
impl<TBl, TRq, TSrc> HeaderVerifySuccess<TBl, TRq, TSrc> {
2022
    /// Returns the height of the block that was verified.
2023
0
    pub fn height(&self) -> u64 {
2024
0
        self.block_to_verify.block_number
2025
0
    }
Unexecuted instantiation: _RNvMse_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE6heightB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE6heightCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE6heightB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1R_19AllForksSourceExtraE6heightCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE6heightCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE6heightCs4VrkfB1pvQ3_25json_rpc_general_requests
2026
2027
    /// Returns the hash of the block that was verified.
2028
0
    pub fn hash(&self) -> &[u8; 32] {
2029
0
        &self.block_to_verify.block_hash
2030
0
    }
Unexecuted instantiation: _RNvMse_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE4hashB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE4hashCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE4hashB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE4hashCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE4hashCs4VrkfB1pvQ3_25json_rpc_general_requests
2031
2032
    /// Returns the hash of the parent of the block that was verified.
2033
0
    pub fn parent_hash(&self) -> &[u8; 32] {
2034
0
        &self.block_to_verify.parent_block_hash
2035
0
    }
Unexecuted instantiation: _RNvMse_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE11parent_hashB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE11parent_hashCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE11parent_hashB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE11parent_hashCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE11parent_hashCs4VrkfB1pvQ3_25json_rpc_general_requests
2036
2037
    /// Returns the user data of the parent of the block to be verified, or `None` if the parent
2038
    /// is the finalized block.
2039
0
    pub fn parent_user_data(&self) -> Option<&TBl> {
2040
0
        self.parent
2041
0
            .chain
2042
0
            .non_finalized_block_user_data(&self.block_to_verify.parent_block_hash)
2043
0
    }
Unexecuted instantiation: _RNvMse_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE16parent_user_dataB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE16parent_user_dataCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE16parent_user_dataB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE16parent_user_dataCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE16parent_user_dataCs4VrkfB1pvQ3_25json_rpc_general_requests
2044
2045
    /// Returns the SCALE-encoded header of the block that was verified.
2046
0
    pub fn scale_encoded_header(&self) -> &[u8] {
2047
0
        self.verified_header.scale_encoded_header()
2048
0
    }
Unexecuted instantiation: _RNvMse_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE20scale_encoded_headerB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE20scale_encoded_headerCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE20scale_encoded_headerB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE20scale_encoded_headerCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE20scale_encoded_headerCs4VrkfB1pvQ3_25json_rpc_general_requests
2049
2050
    /// Returns the list of SCALE-encoded extrinsics of the block to verify.
2051
    ///
2052
    /// This is `Some` if and only if [`Config::download_bodies`] is `true`
2053
0
    pub fn scale_encoded_extrinsics(
2054
0
        &self,
2055
0
    ) -> Option<impl ExactSizeIterator<Item = impl AsRef<[u8]> + Clone> + Clone> {
2056
0
        if self.parent.inner.blocks.downloading_bodies() {
2057
            Some(
2058
0
                self.parent
2059
0
                    .inner
2060
0
                    .blocks
2061
0
                    .unverified_block_user_data(
2062
0
                        self.block_to_verify.block_number,
2063
0
                        &self.block_to_verify.block_hash,
2064
0
                    )
2065
0
                    .body
2066
0
                    .as_ref()
2067
                    // The block shouldn't have been proposed for verification if it doesn't
2068
                    // have its body available.
2069
0
                    .unwrap_or_else(|| unreachable!())
2070
0
                    .iter(),
2071
            )
2072
        } else {
2073
0
            None
2074
        }
2075
0
    }
Unexecuted instantiation: _RNvMse_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE24scale_encoded_extrinsicsB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE24scale_encoded_extrinsicsCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE24scale_encoded_extrinsicsB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE24scale_encoded_extrinsicsCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE24scale_encoded_extrinsicsCs4VrkfB1pvQ3_25json_rpc_general_requests
2076
2077
    /// Returns the SCALE-encoded header of the block that was verified.
2078
0
    pub fn parent_scale_encoded_header(&self) -> &[u8] {
2079
0
        if self.block_to_verify.parent_block_hash == *self.parent.chain.finalized_block_hash() {
2080
0
            self.parent.chain.finalized_block_header()
2081
        } else {
2082
0
            self.parent
2083
0
                .chain
2084
0
                .non_finalized_block_header(&self.block_to_verify.parent_block_hash)
2085
0
                .unwrap()
2086
        }
2087
0
    }
Unexecuted instantiation: _RNvMse_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE27parent_scale_encoded_headerB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE27parent_scale_encoded_headerB9_
2088
2089
    /// Cancel the block verification.
2090
0
    pub fn cancel(self) -> AllForksSync<TBl, TRq, TSrc> {
2091
0
        self.parent
2092
0
    }
Unexecuted instantiation: _RNvMse_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE6cancelB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE6cancelCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE6cancelB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE6cancelCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE6cancelCs4VrkfB1pvQ3_25json_rpc_general_requests
2093
2094
    /// Reject the block and mark it as bad.
2095
0
    pub fn reject_bad_block(mut self) -> AllForksSync<TBl, TRq, TSrc> {
2096
        // Remove the block from `pending_blocks`.
2097
0
        self.parent.inner.blocks.mark_unverified_block_as_bad(
2098
0
            self.block_to_verify.block_number,
2099
0
            &self.block_to_verify.block_hash,
2100
        );
2101
2102
0
        self.parent
2103
0
    }
Unexecuted instantiation: _RNvMse_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE16reject_bad_blockB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE16reject_bad_blockCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE16reject_bad_blockB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE16reject_bad_blockCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE16reject_bad_blockCs4VrkfB1pvQ3_25json_rpc_general_requests
2104
2105
    /// Finish inserting the block header.
2106
0
    pub fn finish(mut self) -> AllForksSync<TBl, TRq, TSrc> {
2107
        // Remove the block from `pending_blocks`.
2108
0
        let pending_block = self.parent.inner.blocks.remove_unverified_block(
2109
0
            self.block_to_verify.block_number,
2110
0
            &self.block_to_verify.block_hash,
2111
        );
2112
2113
        // Now insert the block in `chain`.
2114
0
        self.parent
2115
0
            .chain
2116
0
            .insert_verified_header(self.verified_header, pending_block.user_data);
2117
2118
        // Because a new block is now in the chain, all the previously-unverifiable
2119
        // finality proofs might have now become verifiable.
2120
        // TODO: this way of doing it is correct but quite inefficient
2121
0
        for source in self.parent.inner.blocks.sources_user_data_iter_mut() {
2122
0
            let pending = mem::replace(
2123
0
                &mut source.pending_finality_proofs,
2124
0
                SourcePendingJustificationProofs::None,
2125
            );
2126
2127
0
            source.unverified_finality_proofs.merge(pending)
2128
        }
2129
2130
0
        self.parent
2131
0
    }
Unexecuted instantiation: _RNvMse_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE6finishB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE6finishCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccesspppE6finishB9_
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1R_19AllForksSourceExtraE6finishCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE6finishCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMse_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19HeaderVerifySuccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE6finishCs4VrkfB1pvQ3_25json_rpc_general_requests
2132
}
2133
2134
/// Finality proof verification to be performed.
2135
///
2136
/// Internally holds the [`AllForksSync`].
2137
pub struct FinalityProofVerify<TBl, TRq, TSrc> {
2138
    parent: AllForksSync<TBl, TRq, TSrc>,
2139
    /// Source that has sent the finality proof.
2140
    source_id: SourceId,
2141
    /// Justification and its consensus engine id, or commit that can be verified.
2142
    finality_proof_to_verify: FinalityProof,
2143
}
2144
2145
impl<TBl, TRq, TSrc> FinalityProofVerify<TBl, TRq, TSrc> {
2146
    /// Returns the source the justification was obtained from.
2147
0
    pub fn sender(&self) -> (SourceId, &TSrc) {
2148
0
        (self.source_id, &self.parent[self.source_id])
2149
0
    }
Unexecuted instantiation: _RNvMsf_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19FinalityProofVerifypppE6senderB9_
Unexecuted instantiation: _RNvMsf_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19FinalityProofVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE6senderCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMsf_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19FinalityProofVerifypppE6senderB9_
Unexecuted instantiation: _RNvMsf_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19FinalityProofVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1R_19AllForksSourceExtraE6senderCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMsf_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19FinalityProofVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE6senderCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMsf_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19FinalityProofVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE6senderCs4VrkfB1pvQ3_25json_rpc_general_requests
2150
2151
    /// Perform the verification.
2152
    ///
2153
    /// A randomness seed must be provided and will be used during the verification. Note that the
2154
    /// verification is nonetheless deterministic.
2155
0
    pub fn perform(
2156
0
        mut self,
2157
0
        randomness_seed: [u8; 32],
2158
0
    ) -> (
2159
0
        AllForksSync<TBl, TRq, TSrc>,
2160
0
        FinalityProofVerifyOutcome<TBl>,
2161
0
    ) {
2162
0
        let finality_apply = match self.finality_proof_to_verify {
2163
0
            FinalityProof::GrandpaCommit(scale_encoded_commit) => {
2164
0
                match self
2165
0
                    .parent
2166
0
                    .chain
2167
0
                    .verify_grandpa_commit_message(&scale_encoded_commit, randomness_seed)
2168
                {
2169
0
                    Ok(finality_apply) => finality_apply,
2170
2171
                    // In case where the commit message concerns a block older or equal to the
2172
                    // finalized block, the operation is silently considered successful.
2173
                    Err(blocks_tree::CommitVerifyError::FinalityVerify(
2174
                        blocks_tree::FinalityVerifyError::EqualToFinalized
2175
                        | blocks_tree::FinalityVerifyError::BelowFinalized,
2176
0
                    )) => return (self.parent, FinalityProofVerifyOutcome::AlreadyFinalized),
2177
2178
                    // The commit can't be verified yet.
2179
                    Err(
2180
                        blocks_tree::CommitVerifyError::FinalityVerify(
2181
                            blocks_tree::FinalityVerifyError::UnknownTargetBlock {
2182
0
                                block_number,
2183
                                ..
2184
                            },
2185
                        )
2186
                        | blocks_tree::CommitVerifyError::FinalityVerify(
2187
                            blocks_tree::FinalityVerifyError::TooFarAhead {
2188
0
                                justification_block_number: block_number,
2189
                                ..
2190
                            },
2191
                        )
2192
                        | blocks_tree::CommitVerifyError::NotEnoughKnownBlocks {
2193
0
                            target_block_number: block_number,
2194
                        },
2195
                    ) => {
2196
0
                        self.parent.inner.blocks[self.source_id]
2197
0
                            .pending_finality_proofs
2198
0
                            .insert(
2199
0
                                block_number,
2200
0
                                FinalityProofs::GrandpaCommit(scale_encoded_commit),
2201
                            );
2202
0
                        return (
2203
0
                            self.parent,
2204
0
                            FinalityProofVerifyOutcome::GrandpaCommitPending,
2205
0
                        );
2206
                    }
2207
2208
                    // Any other error means that the commit is invalid.
2209
0
                    Err(err) => {
2210
0
                        return (
2211
0
                            self.parent,
2212
0
                            FinalityProofVerifyOutcome::GrandpaCommitError(err),
2213
0
                        );
2214
                    }
2215
                }
2216
            }
2217
2218
0
            FinalityProof::Justification((consensus_engine_id, scale_encoded_justification)) => {
2219
0
                match self.parent.chain.verify_justification(
2220
0
                    consensus_engine_id,
2221
0
                    &scale_encoded_justification,
2222
0
                    randomness_seed,
2223
                ) {
2224
0
                    Ok(finality_apply) => finality_apply,
2225
2226
                    // In case where the commit message concerns a block older or equal to the
2227
                    // finalized block, the operation is silently considered successful.
2228
                    Err(blocks_tree::JustificationVerifyError::FinalityVerify(
2229
                        blocks_tree::FinalityVerifyError::EqualToFinalized
2230
                        | blocks_tree::FinalityVerifyError::BelowFinalized,
2231
0
                    )) => return (self.parent, FinalityProofVerifyOutcome::AlreadyFinalized),
2232
2233
                    // Note that, contrary to commits, there's no such thing as a justification
2234
                    // that can't be verified yet.
2235
0
                    Err(err) => {
2236
0
                        return (
2237
0
                            self.parent,
2238
0
                            FinalityProofVerifyOutcome::JustificationError(err),
2239
0
                        );
2240
                    }
2241
                }
2242
            }
2243
        };
2244
2245
        // Commit or justification successfully verified.
2246
        // Update the local state with the newly-finalized block.
2247
2248
0
        let finalized_blocks_iter = finality_apply.apply();
2249
0
        let updates_best_block = finalized_blocks_iter.updates_best_block();
2250
0
        let mut finalized_blocks = Vec::new();
2251
0
        let mut pruned_blocks = Vec::new();
2252
        // TODO: a bit weird to perform a conversion here
2253
0
        for block in finalized_blocks_iter {
2254
0
            if matches!(block.ty, blocks_tree::RemovedBlockType::Finalized) {
2255
0
                finalized_blocks.push(RemovedBlock {
2256
0
                    block_hash: block.block_hash,
2257
0
                    block_number: block.block_number,
2258
0
                    user_data: block.user_data,
2259
0
                    scale_encoded_header: block.scale_encoded_header,
2260
0
                });
2261
0
            } else {
2262
0
                pruned_blocks.push(RemovedBlock {
2263
0
                    block_hash: block.block_hash,
2264
0
                    block_number: block.block_number,
2265
0
                    user_data: block.user_data,
2266
0
                    scale_encoded_header: block.scale_encoded_header,
2267
0
                });
2268
0
            }
2269
        }
2270
0
        let _finalized_blocks = self
2271
0
            .parent
2272
0
            .inner
2273
0
            .blocks
2274
0
            .set_finalized_block_height(finalized_blocks.last().unwrap().block_number);
2275
2276
0
        (
2277
0
            self.parent,
2278
0
            FinalityProofVerifyOutcome::NewFinalized {
2279
0
                finalized_blocks_newest_to_oldest: finalized_blocks,
2280
0
                pruned_blocks,
2281
0
                updates_best_block,
2282
0
            },
2283
0
        )
2284
0
    }
Unexecuted instantiation: _RNvMsf_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19FinalityProofVerifypppE7performB9_
Unexecuted instantiation: _RNvMsf_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19FinalityProofVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE7performCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMsf_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19FinalityProofVerifypppE7performB9_
Unexecuted instantiation: _RNvMsf_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19FinalityProofVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionuENtNtB7_3all20AllForksRequestExtraNtB1R_19AllForksSourceExtraE7performCs7snhGEhbuap_18smoldot_light_wasm
Unexecuted instantiation: _RNvMsf_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19FinalityProofVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE7performCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMsf_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19FinalityProofVerifyINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtCsfFWJyR6nd6r_17smoldot_full_node17consensus_service17NonFinalizedBlockENtNtB7_3all20AllForksRequestExtraNtB33_19AllForksSourceExtraE7performCs4VrkfB1pvQ3_25json_rpc_general_requests
2285
2286
    /// Do not actually proceed with the verification.
2287
0
    pub fn cancel(self) -> AllForksSync<TBl, TRq, TSrc> {
2288
0
        self.parent
2289
0
    }
Unexecuted instantiation: _RNvMsf_NtNtCsjlkOsLH0Zfj_7smoldot4sync9all_forksINtB5_19FinalityProofVerifypppE6cancelB9_
Unexecuted instantiation: _RNvMsf_NtNtCsc1ywvx6YAnK_7smoldot4sync9all_forksINtB5_19FinalityProofVerifypppE6cancelB9_
2290
}
2291
2292
/// See [`AllForksSync::grandpa_commit_message`].
2293
#[derive(Debug, Clone)]
2294
pub enum GrandpaCommitMessageOutcome {
2295
    /// Failed to parse message. Commit has been silently discarded.
2296
    ParseError, // TODO: should probably contain the error, but difficult due to lifetimes in said error
2297
    /// Message has been queued for later verification.
2298
    Queued,
2299
}
2300
2301
/// State of the processing of blocks.
2302
pub enum ProcessOne<TBl, TRq, TSrc> {
2303
    /// No processing is necessary.
2304
    ///
2305
    /// Calling [`AllForksSync::process_one`] again is unnecessary.
2306
    AllSync {
2307
        /// The state machine.
2308
        /// The [`AllForksSync::process_one`] method takes ownership of the [`AllForksSync`]. This
2309
        /// field yields it back.
2310
        sync: AllForksSync<TBl, TRq, TSrc>,
2311
    },
2312
2313
    /// A block is ready for verification.
2314
    BlockVerify(BlockVerify<TBl, TRq, TSrc>),
2315
2316
    /// A justification is ready for verification.
2317
    FinalityProofVerify(FinalityProofVerify<TBl, TRq, TSrc>),
2318
}
2319
2320
/// Outcome of calling [`BlockVerify::verify_header`].
2321
pub enum HeaderVerifyOutcome<TBl, TRq, TSrc> {
2322
    /// Header has been successfully verified.
2323
    Success {
2324
        /// True if the newly-verified block is considered the new best block.
2325
        is_new_best: bool,
2326
        success: HeaderVerifySuccess<TBl, TRq, TSrc>,
2327
    },
2328
2329
    /// Header verification failed.
2330
    Error {
2331
        /// State machine yielded back. Use to continue the processing.
2332
        sync: AllForksSync<TBl, TRq, TSrc>,
2333
        /// Error that happened.
2334
        error: HeaderVerifyError,
2335
    },
2336
}
2337
2338
/// Error that can happen when verifying a block header.
2339
#[derive(Debug, derive_more::Display, derive_more::Error)]
2340
pub enum HeaderVerifyError {
2341
    /// Block can't be verified as it uses an unknown consensus engine.
2342
    UnknownConsensusEngine,
2343
    /// Block uses a different consensus than the rest of the chain.
2344
    ConsensusMismatch,
2345
    /// The block verification has failed. The block is invalid and should be thrown away.
2346
    #[display("{_0}")]
2347
    VerificationFailed(verify::header_only::Error),
2348
}
2349
2350
/// Information about the outcome of verifying a finality proof.
2351
#[derive(Debug)]
2352
pub enum FinalityProofVerifyOutcome<TBl> {
2353
    /// Verification successful. The block and all its ancestors is now finalized.
2354
    NewFinalized {
2355
        /// List of finalized blocks, in decreasing block number.
2356
        finalized_blocks_newest_to_oldest: Vec<RemovedBlock<TBl>>,
2357
        /// List of blocks that aren't descendant of the latest finalized block, in an unspecified order.
2358
        pruned_blocks: Vec<RemovedBlock<TBl>>,
2359
        /// If `true`, this operation modifies the best block of the non-finalized chain.
2360
        /// This can happen if the previous best block isn't a descendant of the now finalized
2361
        /// block.
2362
        updates_best_block: bool,
2363
    },
2364
    /// Finality proof concerns block that was already finalized.
2365
    AlreadyFinalized,
2366
    /// GrandPa commit cannot be verified yet and has been stored for later.
2367
    GrandpaCommitPending,
2368
    /// Problem while verifying justification.
2369
    JustificationError(blocks_tree::JustificationVerifyError),
2370
    /// Problem while verifying GrandPa commit.
2371
    GrandpaCommitError(blocks_tree::CommitVerifyError),
2372
}
2373
2374
/// See [`FinalityProofVerifyOutcome`].
2375
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2376
pub struct RemovedBlock<TBl> {
2377
    /// Hash of the block.
2378
    pub block_hash: [u8; 32],
2379
    /// Height of the block.
2380
    pub block_number: u64,
2381
    /// User data that was associated with that block.
2382
    pub user_data: TBl,
2383
    /// SCALE-encoded header of the block.
2384
    pub scale_encoded_header: Vec<u8>,
2385
}