safety_rules/
error.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8use serde::{Deserialize, Serialize};
9use thiserror::Error;
10
11#[derive(Clone, Debug, Deserialize, Error, PartialEq, Serialize)]
12/// Different reasons for proposal rejection
13pub enum Error {
14    #[error("Provided epoch, {0}, does not match expected epoch, {1}")]
15    IncorrectEpoch(u64, u64),
16    #[error("block has next round that wraps around: {0}")]
17    IncorrectRound(u64),
18    #[error("Provided round, {0}, is incompatible with last voted round, {1}")]
19    IncorrectLastVotedRound(u64, u64),
20    #[error("Provided round, {0}, is incompatible with preferred round, {1}")]
21    IncorrectPreferredRound(u64, u64),
22    #[error("Unable to verify that the new tree extends the parent: {0}")]
23    InvalidAccumulatorExtension(String),
24    #[error("Invalid EpochChangeProof: {0}")]
25    InvalidEpochChangeProof(String),
26    #[error("Internal error: {0}")]
27    InternalError(String),
28    #[error("No next_epoch_state specified in the provided Ledger Info")]
29    InvalidLedgerInfo,
30    #[error("Invalid proposal: {0}")]
31    InvalidProposal(String),
32    #[error("Invalid QC: {0}")]
33    InvalidQuorumCertificate(String),
34    #[error("{0} is not set, SafetyRules is not initialized")]
35    NotInitialized(String),
36    #[error("Data not found in secure storage: {0}")]
37    SecureStorageMissingDataError(String),
38    #[error("Unexpected error returned by secure storage: {0}")]
39    SecureStorageUnexpectedError(String),
40    #[error("Serialization error: {0}")]
41    SerializationError(String),
42    #[error("Validator key not found: {0}")]
43    ValidatorKeyNotFound(String),
44    #[error(
45        "The validator is not in the validator set. Address not in set: {0}"
46    )]
47    ValidatorNotInSet(String),
48    #[error("Vote proposal missing expected signature")]
49    VoteProposalSignatureNotFound,
50}
51
52impl From<bcs::Error> for Error {
53    fn from(error: bcs::Error) -> Self {
54        Self::SerializationError(format!("{}", error))
55    }
56}
57
58impl From<diem_secure_net::Error> for Error {
59    fn from(error: diem_secure_net::Error) -> Self {
60        Self::InternalError(error.to_string())
61    }
62}
63
64impl From<diem_secure_storage::Error> for Error {
65    fn from(error: diem_secure_storage::Error) -> Self {
66        match error {
67            diem_secure_storage::Error::PermissionDenied => {
68                // If a storage error is thrown that indicates a permission
69                // failure, we want to panic immediately to
70                // alert an operator that something has gone
71                // wrong. For example, this error is thrown when a storage
72                // (e.g., vault) token has expired, so it makes
73                // sense to fail fast and require a token
74                // renewal!
75                panic!(
76                    "A permission error was thrown: {:?}. Maybe the storage token needs to be renewed?",
77                    error
78                );
79            }
80            diem_secure_storage::Error::KeyVersionNotFound(_, _)
81            | diem_secure_storage::Error::KeyNotSet(_) => {
82                Self::SecureStorageMissingDataError(error.to_string())
83            }
84            _ => Self::SecureStorageUnexpectedError(error.to_string()),
85        }
86    }
87}