massa_bootstrap/
messages.rs

1// Copyright (c) 2022 MASSA LABS <info@massa.net>
2
3use crate::settings::BootstrapServerMessageDeserializerArgs;
4use massa_consensus_exports::bootstrapable_graph::{
5    BootstrapableGraph, BootstrapableGraphDeserializer, BootstrapableGraphSerializer,
6};
7
8use massa_db_exports::StreamBatch;
9use massa_ledger_exports::max_ledger_db_key_length;
10
11use massa_models::block::BlockDeserializerArgs;
12use massa_models::block_id::{BlockId, BlockIdDeserializer, BlockIdSerializer};
13
14use massa_models::config::{
15    bootstrap_batch_allocation_budget, BOOTSTRAP_BATCH_ENTRY_OVERHEAD,
16    MAX_BOOTSTRAP_MESSAGE_FROM_SERVER_SIZE, MAX_BYTECODE_LENGTH,
17};
18use massa_models::prehash::PreHashSet;
19use massa_models::serialization::{
20    PreHashSetDeserializer, PreHashSetSerializer, VecU8Deserializer, VecU8Serializer,
21};
22use massa_models::slot::{Slot, SlotDeserializer, SlotSerializer};
23use massa_models::streaming_step::{
24    StreamingStep, StreamingStepDeserializer, StreamingStepSerializer,
25};
26use massa_models::version::{Version, VersionDeserializer, VersionSerializer};
27use massa_protocol_exports::{
28    BootstrapPeers, BootstrapPeersDeserializer, BootstrapPeersSerializer,
29};
30use massa_serialization::{
31    BoolDeserializer, BoolSerializer, Deserializer, OptionDeserializer, OptionSerializer,
32    SerializeError, Serializer, U32VarIntDeserializer, U32VarIntSerializer, U64VarIntDeserializer,
33    U64VarIntSerializer,
34};
35use std::collections::BTreeMap;
36
37use massa_time::{MassaTime, MassaTimeDeserializer, MassaTimeSerializer};
38use nom::error::context;
39use nom::multi::{length_data, length_value};
40use nom::sequence::tuple;
41use nom::Parser;
42use nom::{
43    error::{ContextError, ParseError},
44    IResult,
45};
46use num_enum::{IntoPrimitive, TryFromPrimitive};
47use std::convert::TryInto;
48use std::ops::Bound::{Excluded, Included};
49
50/// Upper bound of the serialization overhead of a bytecode stored in the state DB:
51/// its version and its length, both u64 varints (at most 10 bytes each).
52const BYTECODE_DB_VALUE_OVERHEAD: u64 = 2 * 10;
53
54/// Maximum length of a raw state DB value streamed during bootstrap. Besides datastore
55/// values, the state holds serialized bytecodes, which are slightly larger than
56/// `MAX_BYTECODE_LENGTH`.
57pub(crate) fn max_state_db_value_length(max_datastore_value_length: u64) -> u64 {
58    max_datastore_value_length.max(MAX_BYTECODE_LENGTH.saturating_add(BYTECODE_DB_VALUE_OVERHEAD))
59}
60
61/// Messages used during bootstrap by server
62#[derive(Debug, Clone)]
63#[allow(clippy::large_enum_variant)]
64pub enum BootstrapServerMessage {
65    /// Sync clocks
66    BootstrapTime {
67        /// The current time on the bootstrap server.
68        server_time: MassaTime,
69        /// The version of the bootstrap server.
70        version: Version,
71    },
72    /// Bootstrap peers
73    BootstrapPeers {
74        /// Server peers
75        peers: BootstrapPeers,
76    },
77    /// Part of final state and consensus
78    BootstrapPart {
79        /// Slot the state changes are attached to
80        slot: Slot,
81        /// Part of the state in a serialized way
82        state_part: StreamBatch<Slot>,
83        /// Part of the state (specific to versioning) in a serialized way
84        versioning_part: StreamBatch<Slot>,
85        /// Part of the consensus graph
86        consensus_part: BootstrapableGraph,
87        /// Outdated block ids in the current consensus graph bootstrap
88        consensus_outdated_ids: PreHashSet<BlockId>,
89        /// Last Start Period for network restart management
90        last_start_period: Option<u64>,
91        /// Last Slot before downtime for network restart management
92        last_slot_before_downtime: Option<Option<Slot>>,
93    },
94    /// Message sent when the final state and consensus bootstrap are finished
95    BootstrapFinished,
96    /// Slot sent to get state changes is too old
97    SlotTooOld,
98    /// Bootstrap error
99    BootstrapError {
100        /// Error message
101        error: String,
102    },
103}
104
105#[allow(clippy::to_string_trait_impl)]
106impl ToString for BootstrapServerMessage {
107    fn to_string(&self) -> String {
108        match self {
109            BootstrapServerMessage::BootstrapTime { .. } => "BootstrapTime".to_string(),
110            BootstrapServerMessage::BootstrapPeers { .. } => "BootstrapPeers".to_string(),
111            BootstrapServerMessage::BootstrapPart { .. } => "BootstrapPart".to_string(),
112            BootstrapServerMessage::BootstrapFinished => "BootstrapFinished".to_string(),
113            BootstrapServerMessage::SlotTooOld => "SlotTooOld".to_string(),
114            BootstrapServerMessage::BootstrapError { error } => {
115                format!("BootstrapError {{ error: {} }}", error)
116            }
117        }
118    }
119}
120
121#[derive(IntoPrimitive, Debug, Eq, PartialEq, TryFromPrimitive)]
122#[repr(u32)]
123enum MessageServerTypeId {
124    BootstrapTime = 0u32,
125    Peers = 1u32,
126    FinalStatePart = 2u32,
127    FinalStateFinished = 3u32,
128    SlotTooOld = 4u32,
129    BootstrapError = 5u32,
130}
131
132/// Serializer for `BootstrapServerMessage`
133pub struct BootstrapServerMessageSerializer {
134    u32_serializer: U32VarIntSerializer,
135    u64_serializer: U64VarIntSerializer,
136    time_serializer: MassaTimeSerializer,
137    version_serializer: VersionSerializer,
138    peers_serializer: BootstrapPeersSerializer,
139    bootstrapable_graph_serializer: BootstrapableGraphSerializer,
140    block_id_set_serializer: PreHashSetSerializer<BlockId, BlockIdSerializer>,
141    vec_u8_serializer: VecU8Serializer,
142    opt_vec_u8_serializer: OptionSerializer<Vec<u8>, VecU8Serializer>,
143    slot_serializer: SlotSerializer,
144    opt_last_start_period_serializer: OptionSerializer<u64, U64VarIntSerializer>,
145    opt_last_slot_before_downtime_serializer:
146        OptionSerializer<Option<Slot>, OptionSerializer<Slot, SlotSerializer>>,
147}
148
149impl Default for BootstrapServerMessageSerializer {
150    fn default() -> Self {
151        Self::new()
152    }
153}
154
155impl BootstrapServerMessageSerializer {
156    /// Creates a new `BootstrapServerMessageSerializer`
157    pub fn new() -> Self {
158        Self {
159            u32_serializer: U32VarIntSerializer::new(),
160            u64_serializer: U64VarIntSerializer::new(),
161            time_serializer: MassaTimeSerializer::new(),
162            version_serializer: VersionSerializer::new(),
163            peers_serializer: BootstrapPeersSerializer::new(),
164            bootstrapable_graph_serializer: BootstrapableGraphSerializer::new(),
165            block_id_set_serializer: PreHashSetSerializer::new(BlockIdSerializer::new()),
166            vec_u8_serializer: VecU8Serializer::new(),
167            opt_vec_u8_serializer: OptionSerializer::new(VecU8Serializer::new()),
168            slot_serializer: SlotSerializer::new(),
169            opt_last_start_period_serializer: OptionSerializer::new(U64VarIntSerializer::new()),
170            opt_last_slot_before_downtime_serializer: OptionSerializer::new(OptionSerializer::new(
171                SlotSerializer::new(),
172            )),
173        }
174    }
175}
176
177impl Serializer<BootstrapServerMessage> for BootstrapServerMessageSerializer {
178    /// ## Example
179    /// ```rust
180    /// use massa_bootstrap::{BootstrapServerMessage, BootstrapServerMessageSerializer};
181    /// use massa_serialization::Serializer;
182    /// use massa_time::MassaTime;
183    /// use massa_models::version::Version;
184    /// use std::str::FromStr;
185    ///
186    /// let message_serializer = BootstrapServerMessageSerializer::new();
187    /// let bootstrap_server_message = BootstrapServerMessage::BootstrapTime {
188    ///    server_time: MassaTime::from_millis(0),
189    ///    version: Version::from_str("TEST.1.10").unwrap(),
190    /// };
191    /// let mut message_serialized = Vec::new();
192    /// message_serializer.serialize(&bootstrap_server_message, &mut message_serialized).unwrap();
193    /// ```
194    fn serialize(
195        &self,
196        value: &BootstrapServerMessage,
197        buffer: &mut Vec<u8>,
198    ) -> Result<(), SerializeError> {
199        match value {
200            BootstrapServerMessage::BootstrapTime {
201                server_time,
202                version,
203            } => {
204                self.u32_serializer
205                    .serialize(&u32::from(MessageServerTypeId::BootstrapTime), buffer)?;
206                self.time_serializer.serialize(server_time, buffer)?;
207                self.version_serializer.serialize(version, buffer)?;
208            }
209            BootstrapServerMessage::BootstrapPeers { peers } => {
210                self.u32_serializer
211                    .serialize(&u32::from(MessageServerTypeId::Peers), buffer)?;
212                self.peers_serializer.serialize(peers, buffer)?;
213            }
214            BootstrapServerMessage::BootstrapPart {
215                slot,
216                state_part,
217                versioning_part,
218                consensus_part,
219                consensus_outdated_ids,
220                last_start_period,
221                last_slot_before_downtime,
222            } => {
223                // message type
224                self.u32_serializer
225                    .serialize(&u32::from(MessageServerTypeId::FinalStatePart), buffer)?;
226                // slot
227                self.slot_serializer.serialize(slot, buffer)?;
228                // state new_elements
229                let mut state_new_element_buffer: Vec<u8> = Vec::new();
230                for (key, value) in state_part.new_elements.iter() {
231                    self.vec_u8_serializer
232                        .serialize(key, &mut state_new_element_buffer)?;
233                    self.vec_u8_serializer
234                        .serialize(value, &mut state_new_element_buffer)?;
235                }
236                self.u64_serializer.serialize(
237                    &state_new_element_buffer
238                        .len()
239                        .try_into()
240                        .expect("Overflow of state new_elements len"),
241                    buffer,
242                )?;
243                buffer.extend(state_new_element_buffer);
244                // state updates
245                let mut state_updates_buffer: Vec<u8> = Vec::new();
246                for (key, value) in state_part.updates_on_previous_elements.iter() {
247                    self.vec_u8_serializer
248                        .serialize(key, &mut state_updates_buffer)?;
249                    self.opt_vec_u8_serializer
250                        .serialize(value, &mut state_updates_buffer)?;
251                }
252                self.u64_serializer.serialize(
253                    &state_updates_buffer
254                        .len()
255                        .try_into()
256                        .expect("Overflow of state updates len"),
257                    buffer,
258                )?;
259                buffer.extend(state_updates_buffer);
260                self.slot_serializer
261                    .serialize(&state_part.change_id, buffer)?;
262                // versioning new_elements
263                let mut versioning_new_element_buffer: Vec<u8> = Vec::new();
264                for (key, value) in versioning_part.new_elements.iter() {
265                    self.vec_u8_serializer
266                        .serialize(key, &mut versioning_new_element_buffer)?;
267                    self.vec_u8_serializer
268                        .serialize(value, &mut versioning_new_element_buffer)?;
269                }
270                self.u64_serializer.serialize(
271                    &versioning_new_element_buffer
272                        .len()
273                        .try_into()
274                        .expect("Overflow of versioning new_elements len"),
275                    buffer,
276                )?;
277                buffer.extend(versioning_new_element_buffer);
278                // versioning updates
279                let mut versioning_updates_buffer: Vec<u8> = Vec::new();
280                for (key, value) in versioning_part.updates_on_previous_elements.iter() {
281                    self.vec_u8_serializer
282                        .serialize(key, &mut versioning_updates_buffer)?;
283                    self.opt_vec_u8_serializer
284                        .serialize(value, &mut versioning_updates_buffer)?;
285                }
286                self.u64_serializer.serialize(
287                    &versioning_updates_buffer
288                        .len()
289                        .try_into()
290                        .expect("Overflow of versioning updates len"),
291                    buffer,
292                )?;
293                buffer.extend(versioning_updates_buffer);
294                self.slot_serializer
295                    .serialize(&versioning_part.change_id, buffer)?;
296                // consensus graph
297                self.bootstrapable_graph_serializer
298                    .serialize(consensus_part, buffer)?;
299                // consensus outdated ids
300                self.block_id_set_serializer
301                    .serialize(consensus_outdated_ids, buffer)?;
302                // initial state
303                self.opt_last_start_period_serializer
304                    .serialize(last_start_period, buffer)?;
305                // initial state
306                self.opt_last_slot_before_downtime_serializer
307                    .serialize(last_slot_before_downtime, buffer)?;
308            }
309            BootstrapServerMessage::BootstrapFinished => {
310                self.u32_serializer
311                    .serialize(&u32::from(MessageServerTypeId::FinalStateFinished), buffer)?;
312            }
313            BootstrapServerMessage::SlotTooOld => {
314                self.u32_serializer
315                    .serialize(&u32::from(MessageServerTypeId::SlotTooOld), buffer)?;
316            }
317            BootstrapServerMessage::BootstrapError { error } => {
318                self.u32_serializer
319                    .serialize(&u32::from(MessageServerTypeId::BootstrapError), buffer)?;
320                self.u32_serializer.serialize(
321                    &error.len().try_into().map_err(|_| {
322                        SerializeError::GeneralError("Fail to convert usize to u32".to_string())
323                    })?,
324                    buffer,
325                )?;
326                buffer.extend(error.as_bytes())
327            }
328        }
329        Ok(())
330    }
331}
332
333/// Deserializer for `BootstrapServerMessage`
334pub struct BootstrapServerMessageDeserializer {
335    message_id_deserializer: U32VarIntDeserializer,
336    time_deserializer: MassaTimeDeserializer,
337    version_deserializer: VersionDeserializer,
338    peers_deserializer: BootstrapPeersDeserializer,
339    state_new_elements_length_deserializer: U64VarIntDeserializer,
340    versioning_part_new_elements_length_deserializer: U64VarIntDeserializer,
341    stream_batch_updates_length_deserializer: U64VarIntDeserializer,
342    datastore_key_deserializer: VecU8Deserializer,
343    datastore_val_deserializer: VecU8Deserializer,
344    opt_vec_u8_deserializer: OptionDeserializer<Vec<u8>, VecU8Deserializer>,
345    bootstrapable_graph_deserializer: BootstrapableGraphDeserializer,
346    block_id_set_deserializer: PreHashSetDeserializer<BlockId, BlockIdDeserializer>,
347    length_bootstrap_error: U64VarIntDeserializer,
348    slot_deserializer: SlotDeserializer,
349    opt_last_start_period_deserializer: OptionDeserializer<u64, U64VarIntDeserializer>,
350    opt_last_slot_before_downtime_deserializer:
351        OptionDeserializer<Option<Slot>, OptionDeserializer<Slot, SlotDeserializer>>,
352    state_batch_budget: usize,
353    versioning_batch_budget: usize,
354    updates_batch_budget: usize,
355}
356
357impl BootstrapServerMessageDeserializer {
358    /// Builds a deserializer that applies `last_start_period` to bootstrap block header
359    /// deserialization so genesis vs non-genesis parent-count rules are enforced while
360    /// parsing. Pass `None` for the first part; the server sends restart metadata once on
361    /// that part (empty consensus graph), then the client reuses the cached value.
362    pub fn with_last_start_period(
363        args: BootstrapServerMessageDeserializerArgs,
364        last_start_period: Option<u64>,
365    ) -> Self {
366        let mut block_args: BlockDeserializerArgs = (&args).into();
367        block_args.last_start_period = last_start_period;
368        Self {
369            message_id_deserializer: U32VarIntDeserializer::new(Included(0), Included(u32::MAX)),
370            time_deserializer: MassaTimeDeserializer::new((
371                Included(MassaTime::from_millis(0)),
372                Included(MassaTime::from_millis(u64::MAX)),
373            )),
374            version_deserializer: VersionDeserializer::new(),
375            peers_deserializer: BootstrapPeersDeserializer::new(
376                args.max_advertise_length,
377                args.max_listeners_per_peer,
378            ),
379            // Streamed state keys/values are raw DB entries: a ledger datastore DB key is
380            // longer than the datastore key itself, and a bytecode DB value is longer than
381            // the bytecode, so bound them by their serialized maximums.
382            datastore_key_deserializer: VecU8Deserializer::new(
383                Included(0),
384                Included(max_ledger_db_key_length(args.max_datastore_key_length)),
385            ),
386            datastore_val_deserializer: VecU8Deserializer::new(
387                Included(0),
388                Included(max_state_db_value_length(args.max_datastore_value_length)),
389            ),
390            opt_vec_u8_deserializer: OptionDeserializer::new(VecU8Deserializer::new(
391                Included(0),
392                Included(max_state_db_value_length(args.max_datastore_value_length)),
393            )),
394            bootstrapable_graph_deserializer: BootstrapableGraphDeserializer::new(
395                block_args,
396                args.max_bootstrap_blocks_length,
397            ),
398            block_id_set_deserializer: PreHashSetDeserializer::new(
399                BlockIdDeserializer::new(),
400                Included(0),
401                Included(args.max_bootstrap_blocks_length.into()),
402            ),
403            length_bootstrap_error: U64VarIntDeserializer::new(
404                Included(0),
405                Included(args.max_bootstrap_error_length),
406            ),
407            versioning_part_new_elements_length_deserializer: U64VarIntDeserializer::new(
408                Included(0),
409                Included(args.max_versioning_elements_size.into()),
410            ),
411            state_new_elements_length_deserializer: U64VarIntDeserializer::new(
412                Included(0),
413                Included(args.max_final_state_elements_size.into()),
414            ),
415            // The updates section is deliberately allowed to be large (see the
416            // `MAX_BOOTSTRAP_MESSAGE_FROM_SERVER_SIZE` doc: update sizes are not capped by
417            // the per-part limits), but it can never exceed the whole message, so bound it
418            // to the maximum message size instead of `u64::MAX`.
419            stream_batch_updates_length_deserializer: U64VarIntDeserializer::new(
420                Included(0),
421                Included(MAX_BOOTSTRAP_MESSAGE_FROM_SERVER_SIZE as u64),
422            ),
423            slot_deserializer: SlotDeserializer::new(
424                (Included(0), Included(u64::MAX)),
425                (Included(0), Excluded(args.thread_count)),
426            ),
427            opt_last_start_period_deserializer: OptionDeserializer::new(
428                U64VarIntDeserializer::new(Included(u64::MIN), Included(u64::MAX)),
429            ),
430            opt_last_slot_before_downtime_deserializer: OptionDeserializer::new(
431                OptionDeserializer::new(SlotDeserializer::new(
432                    (Included(0), Included(u64::MAX)),
433                    (Included(0), Excluded(args.thread_count)),
434                )),
435            ),
436            state_batch_budget: args.max_final_state_batch_allocation as usize,
437            versioning_batch_budget: args.max_versioning_batch_allocation as usize,
438            // The updates sections are deliberately not capped by the per-part size limits (see
439            // MAX_BOOTSTRAP_MESSAGE_FROM_SERVER_SIZE), so their budget derives from the only
440            // bound they do have: the whole message.
441            updates_batch_budget: bootstrap_batch_allocation_budget(
442                MAX_BOOTSTRAP_MESSAGE_FROM_SERVER_SIZE as usize,
443            ),
444        }
445    }
446
447    /// Deserialize a map of `(key, value)` pairs, bounding the in-memory footprint of the result.
448    ///
449    /// Charges every parsed pair the bytes it occupied on the wire plus
450    /// [`BOOTSTRAP_BATCH_ENTRY_OVERHEAD`], and fails once `budget` is spent. Bounding the section
451    /// in wire bytes alone is not enough: an entry can cost two bytes on the wire and two orders
452    /// of magnitude more as a map node, so a flood of tiny entries amplifies well past the size
453    /// the section announced. Pairs are charged as parsed rather than as inserted, so a
454    /// duplicate-key flood is charged too.
455    ///
456    /// The map is folded during parsing rather than collected into an intermediate `Vec` first:
457    /// these sections can be large, so skipping the extra full-size allocation roughly halves the
458    /// peak memory of parsing one.
459    fn deserialize_kv_map<'a, V, E, F>(
460        &self,
461        mut input: &'a [u8],
462        mut parse_entry: F,
463        budget: usize,
464    ) -> IResult<&'a [u8], BTreeMap<Vec<u8>, V>, E>
465    where
466        E: ParseError<&'a [u8]> + ContextError<&'a [u8]>,
467        F: FnMut(&'a [u8]) -> IResult<&'a [u8], (Vec<u8>, V), E>,
468    {
469        let mut acc = BTreeMap::new();
470        let mut footprint = 0usize;
471        while !input.is_empty() {
472            let (rest, (key, value)) = parse_entry(input)?;
473            footprint = footprint
474                .saturating_add(input.len().saturating_sub(rest.len()))
475                .saturating_add(BOOTSTRAP_BATCH_ENTRY_OVERHEAD);
476            if footprint > budget {
477                return Err(nom::Err::Failure(ContextError::add_context(
478                    input,
479                    "bootstrap batch over its allocation budget",
480                    ParseError::from_error_kind(input, nom::error::ErrorKind::Count),
481                )));
482            }
483            acc.insert(key, value);
484            input = rest;
485        }
486        Ok((input, acc))
487    }
488}
489
490impl Deserializer<BootstrapServerMessage> for BootstrapServerMessageDeserializer {
491    /// ## Example
492    /// ```rust
493    /// use massa_bootstrap::{BootstrapServerMessage, BootstrapServerMessageSerializer, BootstrapServerMessageDeserializer};
494    /// use massa_bootstrap::BootstrapServerMessageDeserializerArgs;
495    /// use massa_serialization::{Serializer, Deserializer, DeserializeError};
496    /// use massa_time::MassaTime;
497    /// use massa_models::version::Version;
498    /// use std::str::FromStr;
499    /// use massa_models::config::CHAINID;
500    ///
501    /// let message_serializer = BootstrapServerMessageSerializer::new();
502    /// let args = BootstrapServerMessageDeserializerArgs {
503    ///     thread_count: 32, endorsement_count: 16,
504    ///     max_listeners_per_peer: 1000,
505    ///     max_advertise_length: 1000, max_bootstrap_blocks_length: 1000,
506    ///     max_operations_per_block: 1000, max_versioning_elements_size: 1000,
507    ///     max_ledger_changes_count: 1000, max_datastore_key_length: 255,
508    ///     max_datastore_value_length: 1000,
509    ///     max_final_state_elements_size: 1000,
510    ///     max_final_state_batch_allocation: 1_000_000, max_versioning_batch_allocation: 1_000_000,
511    ///     max_datastore_entry_count: 1000, max_bootstrap_error_length: 1000, max_changes_slot_count: 1000,
512    ///     max_rolls_length: 1000, max_production_stats_length: 1000, max_credits_length: 1000,
513    ///     max_executed_ops_length: 1000, max_ops_changes_length: 1000,
514    ///     mip_store_stats_block_considered: 100,
515    ///     max_denunciations_per_block_header: 128, max_denunciation_changes_length: 1000,
516    ///     chain_id: *CHAINID
517    /// };
518    /// let message_deserializer = BootstrapServerMessageDeserializer::with_last_start_period(args, None);
519    /// let bootstrap_server_message = BootstrapServerMessage::BootstrapTime {
520    ///    server_time: MassaTime::from_millis(0),
521    ///    version: Version::from_str("TEST.1.10").unwrap(),
522    /// };
523    /// let mut message_serialized = Vec::new();
524    /// message_serializer.serialize(&bootstrap_server_message, &mut message_serialized).unwrap();
525    /// let (rest, message_deserialized) = message_deserializer.deserialize::<DeserializeError>(&message_serialized).unwrap();
526    /// match message_deserialized {
527    ///     BootstrapServerMessage::BootstrapTime {
528    ///        server_time,
529    ///        version,
530    ///    } => {
531    ///     assert_eq!(server_time, MassaTime::from_millis(0));
532    ///     assert_eq!(version, Version::from_str("TEST.1.10").unwrap());
533    ///   }
534    ///   _ => panic!("Unexpected message"),
535    /// }
536    /// assert_eq!(rest.len(), 0);
537    /// ```
538    fn deserialize<'a, E: nom::error::ParseError<&'a [u8]> + nom::error::ContextError<&'a [u8]>>(
539        &self,
540        buffer: &'a [u8],
541    ) -> IResult<&'a [u8], BootstrapServerMessage, E> {
542        context("Failed BootstrapServerMessage deserialization", |buffer| {
543            let (input, id) = context("Failed id deserialization", |input| {
544                self.message_id_deserializer.deserialize(input)
545            })
546            .map(|id| {
547                MessageServerTypeId::try_from(id).map_err(|_| {
548                    nom::Err::Error(ParseError::from_error_kind(
549                        buffer,
550                        nom::error::ErrorKind::Eof,
551                    ))
552                })
553            })
554            .parse(buffer)?;
555            match id? {
556                MessageServerTypeId::BootstrapTime => tuple((
557                    context("Failed server_time deserialization", |input| {
558                        self.time_deserializer.deserialize(input)
559                    }),
560                    context("Failed version deserialization", |input| {
561                        self.version_deserializer.deserialize(input)
562                    }),
563                ))
564                .map(
565                    |(server_time, version)| BootstrapServerMessage::BootstrapTime {
566                        server_time,
567                        version,
568                    },
569                )
570                .parse(input),
571                MessageServerTypeId::Peers => context("Failed peers deserialization", |input| {
572                    self.peers_deserializer.deserialize(input)
573                })
574                .map(|peers| BootstrapServerMessage::BootstrapPeers { peers })
575                .parse(input),
576                MessageServerTypeId::FinalStatePart => tuple((
577                    context("Failed slot deserialization", |input| {
578                        self.slot_deserializer.deserialize(input)
579                    }),
580                    context(
581                        "Failed state_part deserialization",
582                        tuple((
583                            context(
584                                "Failed new_elements deserialization",
585                                length_value(
586                                    context("Failed length deserialization", |input| {
587                                        self.state_new_elements_length_deserializer
588                                            .deserialize(input)
589                                    }),
590                                    |input| {
591                                        self.deserialize_kv_map(
592                                            input,
593                                            |input| {
594                                                tuple((
595                                                    |input| {
596                                                        self.datastore_key_deserializer
597                                                            .deserialize(input)
598                                                    },
599                                                    |input| {
600                                                        self.datastore_val_deserializer
601                                                            .deserialize(input)
602                                                    },
603                                                ))
604                                                .parse(input)
605                                            },
606                                            self.state_batch_budget,
607                                        )
608                                    },
609                                ),
610                            ),
611                            context(
612                                "Failed updates deserialization",
613                                length_value(
614                                    context("Failed length deserialization", |input| {
615                                        self.stream_batch_updates_length_deserializer
616                                            .deserialize(input)
617                                    }),
618                                    |input| {
619                                        self.deserialize_kv_map(
620                                            input,
621                                            |input| {
622                                                tuple((
623                                                    |input| {
624                                                        self.datastore_key_deserializer
625                                                            .deserialize(input)
626                                                    },
627                                                    |input| {
628                                                        self.opt_vec_u8_deserializer
629                                                            .deserialize(input)
630                                                    },
631                                                ))
632                                                .parse(input)
633                                            },
634                                            self.updates_batch_budget,
635                                        )
636                                    },
637                                ),
638                            ),
639                            context("Failed slot deserialization", |input| {
640                                self.slot_deserializer.deserialize(input)
641                            }),
642                        )),
643                    ),
644                    context(
645                        "Failed versioning_part deserialization",
646                        tuple((
647                            context(
648                                "Failed new_elements deserialization",
649                                length_value(
650                                    context("Failed length deserialization", |input| {
651                                        self.versioning_part_new_elements_length_deserializer
652                                            .deserialize(input)
653                                    }),
654                                    |input| {
655                                        self.deserialize_kv_map(
656                                            input,
657                                            |input| {
658                                                tuple((
659                                                    |input| {
660                                                        self.datastore_key_deserializer
661                                                            .deserialize(input)
662                                                    },
663                                                    |input| {
664                                                        self.datastore_val_deserializer
665                                                            .deserialize(input)
666                                                    },
667                                                ))
668                                                .parse(input)
669                                            },
670                                            self.versioning_batch_budget,
671                                        )
672                                    },
673                                ),
674                            ),
675                            context(
676                                "Failed updates deserialization",
677                                length_value(
678                                    context("Failed length deserialization", |input| {
679                                        self.stream_batch_updates_length_deserializer
680                                            .deserialize(input)
681                                    }),
682                                    |input| {
683                                        self.deserialize_kv_map(
684                                            input,
685                                            |input| {
686                                                tuple((
687                                                    |input| {
688                                                        self.datastore_key_deserializer
689                                                            .deserialize(input)
690                                                    },
691                                                    |input| {
692                                                        self.opt_vec_u8_deserializer
693                                                            .deserialize(input)
694                                                    },
695                                                ))
696                                                .parse(input)
697                                            },
698                                            self.updates_batch_budget,
699                                        )
700                                    },
701                                ),
702                            ),
703                            context("Failed slot deserialization", |input| {
704                                self.slot_deserializer.deserialize(input)
705                            }),
706                        )),
707                    ),
708                    context("Failed consensus_part deserialization", |input| {
709                        self.bootstrapable_graph_deserializer.deserialize(input)
710                    }),
711                    context("Failed consensus_outdated_ids deserialization", |input| {
712                        self.block_id_set_deserializer.deserialize(input)
713                    }),
714                    context("Failed last_start_period deserialization", |input| {
715                        self.opt_last_start_period_deserializer.deserialize(input)
716                    }),
717                    context(
718                        "Failed last_slot_before_downtime deserialization",
719                        |input| {
720                            self.opt_last_slot_before_downtime_deserializer
721                                .deserialize(input)
722                        },
723                    ),
724                ))
725                .map(
726                    |(
727                        slot,
728                        (state_part_new_elems, state_part_updates, state_part_change_id),
729                        (
730                            versioning_part_new_elems,
731                            versioning_part_updates,
732                            versioning_part_change_id,
733                        ),
734                        consensus_part,
735                        consensus_outdated_ids,
736                        last_start_period,
737                        last_slot_before_downtime,
738                    )| {
739                        let state_part = StreamBatch::<Slot> {
740                            // both already `BTreeMap`s (folded during parsing)
741                            new_elements: state_part_new_elems,
742                            updates_on_previous_elements: state_part_updates,
743                            change_id: state_part_change_id,
744                        };
745                        let versioning_part = StreamBatch::<Slot> {
746                            // both already `BTreeMap`s (folded during parsing)
747                            new_elements: versioning_part_new_elems,
748                            updates_on_previous_elements: versioning_part_updates,
749                            change_id: versioning_part_change_id,
750                        };
751
752                        BootstrapServerMessage::BootstrapPart {
753                            slot,
754                            state_part,
755                            versioning_part,
756                            consensus_part,
757                            consensus_outdated_ids,
758                            last_start_period,
759                            last_slot_before_downtime,
760                        }
761                    },
762                )
763                .parse(input),
764                MessageServerTypeId::FinalStateFinished => {
765                    Ok((input, BootstrapServerMessage::BootstrapFinished))
766                }
767                MessageServerTypeId::SlotTooOld => Ok((input, BootstrapServerMessage::SlotTooOld)),
768                MessageServerTypeId::BootstrapError => context(
769                    "Failed BootstrapError deserialization",
770                    length_data(context("Failed length deserialization", |input| {
771                        self.length_bootstrap_error.deserialize(input)
772                    })),
773                )
774                .map(|error| BootstrapServerMessage::BootstrapError {
775                    error: String::from_utf8_lossy(error).into_owned(),
776                })
777                .parse(input),
778            }
779        })
780        .parse(buffer)
781    }
782}
783
784/// Messages used during bootstrap by client
785#[derive(Debug, Clone)]
786#[allow(clippy::large_enum_variant)]
787pub enum BootstrapClientMessage {
788    /// Ask for bootstrap peers
789    AskBootstrapPeers,
790    /// Ask for a final state and consensus part
791    AskBootstrapPart {
792        /// Slot we are attached to for changes
793        last_slot: Option<Slot>,
794        /// Last received state key
795        last_state_step: StreamingStep<Vec<u8>>,
796        /// Last received versioning key
797        last_versioning_step: StreamingStep<Vec<u8>>,
798        /// Last received consensus block slot
799        last_consensus_step: StreamingStep<PreHashSet<BlockId>>,
800        /// Should be true only for the first part, false later
801        send_last_start_period: bool,
802    },
803    /// Bootstrap error
804    BootstrapError {
805        /// Error message
806        error: String,
807    },
808    /// Bootstrap succeed
809    BootstrapSuccess,
810}
811
812#[derive(IntoPrimitive, Debug, Eq, PartialEq, TryFromPrimitive)]
813#[repr(u32)]
814enum MessageClientTypeId {
815    AskBootstrapPeers = 0u32,
816    AskFinalStatePart = 1u32,
817    BootstrapError = 2u32,
818    BootstrapSuccess = 3u32,
819}
820
821/// Serializer for `BootstrapClientMessage`
822pub struct BootstrapClientMessageSerializer {
823    u32_serializer: U32VarIntSerializer,
824    slot_serializer: SlotSerializer,
825    state_step_serializer: StreamingStepSerializer<Vec<u8>, VecU8Serializer>,
826    block_ids_step_serializer: StreamingStepSerializer<
827        PreHashSet<BlockId>,
828        PreHashSetSerializer<BlockId, BlockIdSerializer>,
829    >,
830    bool_serializer: BoolSerializer,
831}
832
833impl BootstrapClientMessageSerializer {
834    /// Creates a new `BootstrapClientMessageSerializer`
835    pub fn new() -> Self {
836        Self {
837            u32_serializer: U32VarIntSerializer::new(),
838            slot_serializer: SlotSerializer::new(),
839            state_step_serializer: StreamingStepSerializer::new(VecU8Serializer::new()),
840            block_ids_step_serializer: StreamingStepSerializer::new(PreHashSetSerializer::new(
841                BlockIdSerializer::new(),
842            )),
843            bool_serializer: BoolSerializer::new(),
844        }
845    }
846}
847
848impl Default for BootstrapClientMessageSerializer {
849    fn default() -> Self {
850        Self::new()
851    }
852}
853
854impl Serializer<BootstrapClientMessage> for BootstrapClientMessageSerializer {
855    /// ## Example
856    /// ```rust
857    /// use massa_bootstrap::{BootstrapClientMessage, BootstrapClientMessageSerializer};
858    /// use massa_serialization::Serializer;
859    /// use massa_time::MassaTime;
860    /// use massa_models::version::Version;
861    /// use std::str::FromStr;
862    ///
863    /// let message_serializer = BootstrapClientMessageSerializer::new();
864    /// let bootstrap_server_message = BootstrapClientMessage::AskBootstrapPeers;
865    /// let mut message_serialized = Vec::new();
866    /// message_serializer.serialize(&bootstrap_server_message, &mut message_serialized).unwrap();
867    /// ```
868    fn serialize(
869        &self,
870        value: &BootstrapClientMessage,
871        buffer: &mut Vec<u8>,
872    ) -> Result<(), SerializeError> {
873        match value {
874            BootstrapClientMessage::AskBootstrapPeers => {
875                self.u32_serializer
876                    .serialize(&u32::from(MessageClientTypeId::AskBootstrapPeers), buffer)?;
877            }
878            BootstrapClientMessage::AskBootstrapPart {
879                last_slot,
880                last_state_step,
881                last_versioning_step,
882                last_consensus_step,
883                send_last_start_period,
884            } => {
885                self.u32_serializer
886                    .serialize(&u32::from(MessageClientTypeId::AskFinalStatePart), buffer)?;
887                if let Some(slot) = last_slot {
888                    self.slot_serializer.serialize(slot, buffer)?;
889                    self.state_step_serializer
890                        .serialize(last_state_step, buffer)?;
891                    self.state_step_serializer
892                        .serialize(last_versioning_step, buffer)?;
893                    self.block_ids_step_serializer
894                        .serialize(last_consensus_step, buffer)?;
895                    self.bool_serializer
896                        .serialize(send_last_start_period, buffer)?;
897                }
898            }
899            BootstrapClientMessage::BootstrapError { error } => {
900                self.u32_serializer
901                    .serialize(&u32::from(MessageClientTypeId::BootstrapError), buffer)?;
902                self.u32_serializer.serialize(
903                    &error.len().try_into().map_err(|_| {
904                        SerializeError::GeneralError("Fail to convert usize to u32".to_string())
905                    })?,
906                    buffer,
907                )?;
908                buffer.extend(error.as_bytes())
909            }
910            BootstrapClientMessage::BootstrapSuccess => {
911                self.u32_serializer
912                    .serialize(&u32::from(MessageClientTypeId::BootstrapSuccess), buffer)?;
913            }
914        }
915        Ok(())
916    }
917}
918
919/// Deserializer for `BootstrapClientMessage`
920pub struct BootstrapClientMessageDeserializer {
921    id_deserializer: U32VarIntDeserializer,
922    length_error_deserializer: U32VarIntDeserializer,
923    slot_deserializer: SlotDeserializer,
924    state_step_deserializer: StreamingStepDeserializer<Vec<u8>, VecU8Deserializer>,
925    block_ids_step_deserializer: StreamingStepDeserializer<
926        PreHashSet<BlockId>,
927        PreHashSetDeserializer<BlockId, BlockIdDeserializer>,
928    >,
929    bool_deserializer: BoolDeserializer,
930}
931
932impl BootstrapClientMessageDeserializer {
933    /// Creates a new `BootstrapClientMessageDeserializer`
934    pub fn new(
935        thread_count: u8,
936        max_datastore_key_length: u8,
937        max_consensus_block_ids: u64,
938    ) -> Self {
939        Self {
940            id_deserializer: U32VarIntDeserializer::new(Included(0), Included(u32::MAX)),
941            length_error_deserializer: U32VarIntDeserializer::new(Included(0), Included(u32::MAX)),
942            slot_deserializer: SlotDeserializer::new(
943                (Included(0), Included(u64::MAX)),
944                (Included(0), Excluded(thread_count)),
945            ),
946            // The cursor is the last raw state DB key received, see `datastore_key_deserializer`
947            state_step_deserializer: StreamingStepDeserializer::new(VecU8Deserializer::new(
948                Included(0),
949                Included(max_ledger_db_key_length(max_datastore_key_length)),
950            )),
951            block_ids_step_deserializer: StreamingStepDeserializer::new(
952                PreHashSetDeserializer::new(
953                    BlockIdDeserializer::new(),
954                    Included(0),
955                    Included(max_consensus_block_ids),
956                ),
957            ),
958            bool_deserializer: BoolDeserializer::new(),
959        }
960    }
961}
962
963impl Deserializer<BootstrapClientMessage> for BootstrapClientMessageDeserializer {
964    /// ## Example
965    /// ```rust
966    /// use massa_bootstrap::{BootstrapClientMessage, BootstrapClientMessageSerializer, BootstrapClientMessageDeserializer};
967    /// use massa_serialization::{Serializer, Deserializer, DeserializeError};
968    /// use massa_time::MassaTime;
969    /// use massa_models::version::Version;
970    /// use std::str::FromStr;
971    ///
972    /// let message_serializer = BootstrapClientMessageSerializer::new();
973    /// let message_deserializer = BootstrapClientMessageDeserializer::new(32, 255, 50);
974    /// let bootstrap_server_message = BootstrapClientMessage::AskBootstrapPeers;
975    /// let mut message_serialized = Vec::new();
976    /// message_serializer.serialize(&bootstrap_server_message, &mut message_serialized).unwrap();
977    /// let (rest, message_deserialized) = message_deserializer.deserialize::<DeserializeError>(&message_serialized).unwrap();
978    /// match message_deserialized {
979    ///     BootstrapClientMessage::AskBootstrapPeers => (),
980    ///   _ => panic!("Unexpected message"),
981    /// };
982    /// assert_eq!(rest.len(), 0);
983    /// ```
984    fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
985        &self,
986        buffer: &'a [u8],
987    ) -> IResult<&'a [u8], BootstrapClientMessage, E> {
988        context("Failed BootstrapClientMessage deserialization", |buffer| {
989            let (input, id) = context("Failed id deserialization", |input| {
990                self.id_deserializer.deserialize(input)
991            })
992            .map(|id| {
993                MessageClientTypeId::try_from(id).map_err(|_| {
994                    nom::Err::Error(ParseError::from_error_kind(
995                        buffer,
996                        nom::error::ErrorKind::Eof,
997                    ))
998                })
999            })
1000            .parse(buffer)?;
1001            match id? {
1002                MessageClientTypeId::AskBootstrapPeers => {
1003                    Ok((input, BootstrapClientMessage::AskBootstrapPeers))
1004                }
1005                MessageClientTypeId::AskFinalStatePart => {
1006                    if input.is_empty() {
1007                        Ok((
1008                            input,
1009                            BootstrapClientMessage::AskBootstrapPart {
1010                                last_slot: None,
1011                                last_state_step: StreamingStep::Started,
1012                                last_versioning_step: StreamingStep::Started,
1013                                last_consensus_step: StreamingStep::Started,
1014                                send_last_start_period: true,
1015                            },
1016                        ))
1017                    } else {
1018                        tuple((
1019                            context("Failed last_slot deserialization", |input| {
1020                                self.slot_deserializer.deserialize(input)
1021                            }),
1022                            context("Failed last_state_step deserialization", |input| {
1023                                self.state_step_deserializer.deserialize(input)
1024                            }),
1025                            context("Failed last_versioning_step deserialization", |input| {
1026                                self.state_step_deserializer.deserialize(input)
1027                            }),
1028                            context("Failed last_consensus_step deserialization", |input| {
1029                                self.block_ids_step_deserializer.deserialize(input)
1030                            }),
1031                            context("Failed send_last_start_period deserialization", |input| {
1032                                self.bool_deserializer.deserialize(input)
1033                            }),
1034                        ))
1035                        .map(
1036                            |(
1037                                last_slot,
1038                                last_state_step,
1039                                last_versioning_step,
1040                                last_consensus_step,
1041                                send_last_start_period,
1042                            )| {
1043                                BootstrapClientMessage::AskBootstrapPart {
1044                                    last_slot: Some(last_slot),
1045                                    last_state_step,
1046                                    last_versioning_step,
1047                                    last_consensus_step,
1048                                    send_last_start_period,
1049                                }
1050                            },
1051                        )
1052                        .parse(input)
1053                    }
1054                }
1055                MessageClientTypeId::BootstrapError => context(
1056                    "Failed BootstrapError deserialization",
1057                    length_data(context("Failed length deserialization", |input| {
1058                        self.length_error_deserializer.deserialize(input)
1059                    })),
1060                )
1061                .map(|error| BootstrapClientMessage::BootstrapError {
1062                    error: String::from_utf8_lossy(error).into_owned(),
1063                })
1064                .parse(input),
1065                MessageClientTypeId::BootstrapSuccess => {
1066                    Ok((input, BootstrapClientMessage::BootstrapSuccess))
1067                }
1068            }
1069        })
1070        .parse(buffer)
1071    }
1072}