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("Internal error: {0}")]
25    InternalError(String),
26    #[error("Invalid proposal: {0}")]
27    InvalidProposal(String),
28    #[error("Invalid QC: {0}")]
29    InvalidQuorumCertificate(String),
30    #[error("{0} is not set, SafetyRules is not initialized")]
31    NotInitialized(String),
32    #[error("Data not found in secure storage: {0}")]
33    SecureStorageMissingDataError(String),
34    #[error("Unexpected error returned by secure storage: {0}")]
35    SecureStorageUnexpectedError(String),
36    #[error("Serialization error: {0}")]
37    SerializationError(String),
38    #[error("Validator key not found: {0}")]
39    ValidatorKeyNotFound(String),
40    #[error(
41        "The validator is not in the validator set. Address not in set: {0}"
42    )]
43    ValidatorNotInSet(String),
44    #[error("Vote proposal missing expected signature")]
45    VoteProposalSignatureNotFound,
46}
47
48impl From<bcs::Error> for Error {
49    fn from(error: bcs::Error) -> Self {
50        Self::SerializationError(format!("{}", error))
51    }
52}
53
54impl From<diem_secure_storage::Error> for Error {
55    fn from(error: diem_secure_storage::Error) -> Self {
56        match error {
57            diem_secure_storage::Error::PermissionDenied => {
58                // If a storage error is thrown that indicates a permission
59                // failure, we want to panic immediately to
60                // alert an operator that something has gone
61                // wrong. For example, this error is thrown when a storage
62                // (e.g., vault) token has expired, so it makes
63                // sense to fail fast and require a token
64                // renewal!
65                panic!(
66                    "A permission error was thrown: {:?}. Maybe the storage token needs to be renewed?",
67                    error
68                );
69            }
70            diem_secure_storage::Error::KeyVersionNotFound(_, _)
71            | diem_secure_storage::Error::KeyNotSet(_) => {
72                Self::SecureStorageMissingDataError(error.to_string())
73            }
74            _ => Self::SecureStorageUnexpectedError(error.to_string()),
75        }
76    }
77}