1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2022 MASSA LABS <info@massa.net>

use std::io::ErrorKind;

use crate::messages::{BootstrapClientMessage, BootstrapServerMessage};
use displaydoc::Display;
use massa_consensus_exports::error::ConsensusError;
use massa_final_state::FinalStateError;
use massa_hash::MassaHashError;
use massa_pos_exports::PosError;
use massa_protocol_exports::ProtocolError;
use massa_serialization::SerializeError;
use massa_time::TimeError;
use thiserror::Error;

#[non_exhaustive]
#[derive(Display, Error, Debug)]
/// Encapsulates the various failure contexts for the bootstrap process, both client and server side
pub enum BootstrapError {
    /// Bootstrap IO error: {0}
    IoError(std::io::Error),
    /// Bootstrap process timeout: {0}
    TimedOut(std::io::Error),
    /// general bootstrap error: {0}
    GeneralError(String),
    /// deserialization error: {0}
    DeserializeError(String),
    /// serialization error: {0}
    SerializationError(String),
    /// models error: {0}
    ModelsError(#[from] massa_models::error::ModelsError),
    /// serialize error: {0}
    SerializeError(#[from] SerializeError),
    /// unexpected message received from server: {0:?}
    UnexpectedServerMessage(BootstrapServerMessage),
    /// unexpected message received from client: {0:?}
    UnexpectedClientMessage(Box<BootstrapClientMessage>),
    /// connection with bootstrap node dropped
    UnexpectedConnectionDrop,
    /// `massa_hash` error: {0}
    MassaHashError(#[from] MassaHashError),
    /// `massa_consensus` error: {0}
    MassaConsensusError(#[from] ConsensusError),
    /// `massa_signature` error {0}
    MassaSignatureError(#[from] massa_signature::MassaSignatureError),
    /// time error: {0}
    TimeError(#[from] TimeError),
    /// protocol error: {0}
    ProtocolError(#[from] ProtocolError),
    /// final state error: {0}
    FinalStateError(#[from] FinalStateError),
    /// Proof-of-Stake error: {0}
    PoSError(#[from] PosError),
    /// missing keypair file
    MissingKeyError,
    /// incompatible version: {0}
    IncompatibleVersionError(String),
    /// Received error: {0}
    ReceivedError(String),
    /// clock error: {0}
    ClockError(String),
    /// fail to init the list from file : {0}
    InitListError(String),
    /// IP {0} is blacklisted
    BlackListed(String),
    /// IP {0} is not in the whitelist
    WhiteListed(String),
    /// The bootstrap process ended prematurely - e.g. too much time elapsed
    Interrupted(String),
}

/// # Platform-specific behavior
///
/// Platforms may return a different error code whenever a read times out as
/// a result of setting this option. For example Unix typically returns an
/// error of the kind [`ErrorKind::WouldBlock`], but Windows may return [`ErrorKind::TimedOut`].)
impl From<std::io::Error> for BootstrapError {
    fn from(e: std::io::Error) -> Self {
        if e.kind() == ErrorKind::TimedOut || e.kind() == ErrorKind::WouldBlock {
            BootstrapError::TimedOut(e)
        } else {
            BootstrapError::IoError(e)
        }
    }
}