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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#[cfg(test)]
use std::cmp::{Eq, PartialEq};
use std::error::Error;

use derive_more::Display;

/// Overlord consensus error.
#[derive(Debug, Display)]
pub enum ConsensusError {
    ///
    #[display(fmt = "Invalid address")]
    InvalidAddress,
    ///
    ChannelErr(String),
    ///
    #[display(fmt = "Trigger {} SMR error", _0)]
    TriggerSMRErr(String),
    ///
    #[display(fmt = "Monitor {} event error", _0)]
    MonitorEventErr(String),
    ///
    #[display(fmt = "Throw {} event error", _0)]
    ThrowEventErr(String),
    ///
    #[display(fmt = "Proposal error {}", _0)]
    ProposalErr(String),
    ///
    #[display(fmt = "Prevote error {}", _0)]
    PrevoteErr(String),
    ///
    #[display(fmt = "Precommit error {}", _0)]
    PrecommitErr(String),
    ///
    #[display(fmt = "Brake error {}", _0)]
    BrakeErr(String),
    ///
    #[display(fmt = "Self round is {}, vote round is {}", local, vote)]
    RoundDiff {
        ///
        local: u64,
        ///
        vote: u64,
    },
    ///
    #[display(fmt = "Self check not pass {}", _0)]
    SelfCheckErr(String),
    ///
    #[display(fmt = "Correctness error {}", _0)]
    CorrectnessErr(String),
    ///
    #[display(fmt = "Timer error {}", _0)]
    TimerErr(String),
    ///
    #[display(fmt = "State error {}", _0)]
    StateErr(String),
    ///
    #[display(fmt = "Multiple proposal in height {}, round {}", _0, _1)]
    MultiProposal(u64, u64),
    ///
    #[display(fmt = "Storage error {}", _0)]
    StorageErr(String),
    ///
    #[display(fmt = "Save Wal error {}, {}, {} step", height, round, step)]
    SaveWalErr {
        ///
        height: u64,
        ///
        round: u64,
        ///
        step: String,
    },
    ///
    #[display(fmt = "Load Wal error {}", _0)]
    LoadWalErr(String),
    ///
    #[display(fmt = "Crypto error {}", _0)]
    CryptoErr(String),
    ///
    #[display(fmt = "Aggregated signature error {}", _0)]
    AggregatedSignatureErr(String),
    /// Other error.
    #[display(fmt = "Other error {}", _0)]
    Other(String),
}

impl Error for ConsensusError {}

#[cfg(test)]
impl PartialEq for ConsensusError {
    fn eq(&self, other: &Self) -> bool {
        use self::ConsensusError::*;
        match (self, other) {
            // If compare objects are the following types of error, as long as the error type need
            // the same, the details are ignored.
            (InvalidAddress, InvalidAddress)
            | (TriggerSMRErr(_), TriggerSMRErr(_))
            | (MonitorEventErr(_), MonitorEventErr(_))
            | (ThrowEventErr(_), ThrowEventErr(_))
            | (ProposalErr(_), ProposalErr(_))
            | (PrevoteErr(_), PrevoteErr(_))
            | (PrecommitErr(_), PrecommitErr(_))
            | (SelfCheckErr(_), SelfCheckErr(_)) => true,
            // If it is the following two types of errors, in the judgment, the error type need the
            // same, and the error information need the same.
            (RoundDiff { local: m, vote: n }, RoundDiff { local: p, vote: q }) => m == p && n == q,
            (Other(x), Other(y)) | (CorrectnessErr(x), CorrectnessErr(y)) => x == y,
            _ => false,
        }
    }
}

#[cfg(test)]
impl Eq for ConsensusError {}