dpp/errors/
protocol_error.rs

1use thiserror::Error;
2
3use crate::consensus::basic::state_transition::InvalidStateTransitionTypeError;
4use crate::consensus::signature::{
5    InvalidSignaturePublicKeySecurityLevelError, PublicKeyIsDisabledError,
6    UncompressedPublicKeyNotAllowedError,
7};
8use crate::consensus::ConsensusError;
9use crate::data_contract::errors::*;
10use crate::document::errors::*;
11
12#[cfg(any(
13    feature = "state-transition-validation",
14    feature = "state-transition-signing"
15))]
16use crate::state_transition::errors::InvalidIdentityPublicKeyTypeError;
17
18#[cfg(all(feature = "state-transitions", feature = "validation"))]
19use crate::state_transition::errors::StateTransitionError;
20
21#[cfg(any(
22    all(feature = "state-transitions", feature = "validation"),
23    feature = "state-transition-validation",
24    feature = "state-transition-signing",
25    feature = "state-transition-validation"
26))]
27use crate::state_transition::errors::WrongPublicKeyPurposeError;
28
29#[cfg(feature = "state-transition-validation")]
30use crate::state_transition::errors::{
31    InvalidSignaturePublicKeyError, PublicKeyMismatchError, PublicKeySecurityLevelNotMetError,
32    StateTransitionIsNotSignedError,
33};
34use crate::{
35    CompatibleProtocolVersionIsNotDefinedError, DashPlatformProtocolInitError,
36    InvalidVectorSizeError, NonConsensusError, SerdeParsingError,
37};
38
39use dashcore::consensus::encode::Error as DashCoreError;
40
41use crate::tokens::errors::TokenError;
42use crate::version::FeatureVersion;
43use platform_value::{Error as ValueError, Value};
44use platform_version::error::PlatformVersionError;
45
46#[allow(clippy::large_enum_variant)]
47#[derive(Error, Debug)]
48pub enum ProtocolError {
49    #[error("Identifier Error: {0}")]
50    IdentifierError(String),
51    #[error("String Decode Error {0}")]
52    StringDecodeError(String),
53    #[error("Public key data is not set")]
54    EmptyPublicKeyDataError,
55    #[error("Payload reached a {max_size_kbytes}KB limit")]
56    MaxEncodedBytesReachedError {
57        max_size_kbytes: usize,
58        size_hit: usize,
59    },
60    #[error("Encoding Error - {0}")]
61    EncodingError(String),
62    #[error("Decoding Error - {0}")]
63    DecodingError(String),
64    #[error("File not found Error - {0}")]
65    FileNotFound(String),
66
67    /// Platform expected some specific versions
68    #[error(
69    "dpp received not allowed version on {method}, allowed versions: {allowed_versions:?}, received: {received}"
70    )]
71    UnsupportedVersionMismatch {
72        /// method
73        method: String,
74        /// the allowed versions for this method
75        allowed_versions: Vec<FeatureVersion>,
76        /// requested core height
77        received: FeatureVersion,
78    },
79
80    /// Platform expected some specific versions
81    #[error(
82        "dpp unknown version on {method}, known versions: {known_versions:?}, received: {received}"
83    )]
84    UnknownVersionMismatch {
85        /// method
86        method: String,
87        /// the allowed versions for this method
88        known_versions: Vec<FeatureVersion>,
89        /// requested core height
90        received: FeatureVersion,
91    },
92    #[error("current platform version not initialized")]
93    CurrentProtocolVersionNotInitialized,
94    #[error("unknown version error {0}")]
95    UnknownVersionError(String),
96    #[error("unknown protocol version error {0}")]
97    UnknownProtocolVersionError(String),
98    #[error("Not included or invalid protocol version")]
99    NoProtocolVersionError,
100    #[error("Parsing error: {0}")]
101    ParsingError(String),
102
103    #[error(transparent)]
104    ParsingJsonError(#[from] serde_json::Error),
105
106    #[error(transparent)]
107    Error(#[from] anyhow::Error),
108
109    #[error("Invalid key contract bounds error {0}")]
110    InvalidKeyContractBoundsError(String),
111
112    #[error("unknown storage key requirements {0}")]
113    UnknownStorageKeyRequirements(String),
114
115    #[error("unknown contested index resolution {0}")]
116    UnknownContestedIndexResolution(String),
117
118    #[error(transparent)]
119    DataContractError(#[from] DataContractError),
120
121    #[cfg(all(feature = "state-transitions", feature = "validation"))]
122    #[error(transparent)]
123    StateTransitionError(#[from] StateTransitionError),
124
125    #[error("Invalid State Transition Type: {0}")]
126    InvalidStateTransitionType(String),
127
128    #[error(transparent)]
129    PlatformVersionError(#[from] PlatformVersionError),
130
131    #[error(transparent)]
132    ConsensusError(Box<ConsensusError>),
133
134    #[error(transparent)]
135    Document(Box<DocumentError>),
136
137    #[error(transparent)]
138    Token(Box<TokenError>),
139
140    #[error("Generic Error: {0}")]
141    Generic(String),
142
143    #[error("Address witness verification error: {0}")]
144    AddressWitnessError(String),
145
146    #[error("Shielded transaction build error: {0}")]
147    ShieldedBuildError(String),
148
149    #[error("Not supported Error: {0}")]
150    NotSupported(String),
151
152    #[cfg(feature = "message-signing")]
153    #[error("Invalid signing type error: {0}")]
154    InvalidSigningKeyTypeError(String),
155
156    // State Transition Errors
157    #[cfg(any(
158        feature = "state-transition-validation",
159        feature = "state-transition-signing"
160    ))]
161    #[error(transparent)]
162    InvalidIdentityPublicKeyTypeError(InvalidIdentityPublicKeyTypeError),
163    #[cfg(feature = "state-transition-validation")]
164    #[error(transparent)]
165    StateTransitionIsNotSignedError(StateTransitionIsNotSignedError),
166    #[cfg(feature = "state-transition-validation")]
167    #[error(transparent)]
168    PublicKeySecurityLevelNotMetError(PublicKeySecurityLevelNotMetError),
169    #[cfg(any(
170        all(feature = "state-transitions", feature = "validation"),
171        feature = "state-transition-validation",
172        feature = "state-transition-signing",
173        feature = "state-transition-validation"
174    ))]
175    #[error(transparent)]
176    WrongPublicKeyPurposeError(WrongPublicKeyPurposeError),
177    #[cfg(feature = "state-transition-validation")]
178    #[error(transparent)]
179    PublicKeyMismatchError(PublicKeyMismatchError),
180    #[cfg(feature = "state-transition-validation")]
181    #[error(transparent)]
182    InvalidSignaturePublicKeyError(InvalidSignaturePublicKeyError),
183
184    #[error(transparent)]
185    NonConsensusError(#[from] NonConsensusError),
186
187    #[error(transparent)]
188    CompatibleProtocolVersionIsNotDefinedError(#[from] CompatibleProtocolVersionIsNotDefinedError),
189
190    #[error(transparent)]
191    InvalidDocumentTypeError(InvalidDocumentTypeError),
192
193    #[error(transparent)]
194    DataContractNotPresentError(DataContractNotPresentError),
195
196    #[error(transparent)]
197    InvalidSignaturePublicKeySecurityLevelError(InvalidSignaturePublicKeySecurityLevelError),
198
199    #[error(transparent)]
200    InvalidStateTransitionTypeError(InvalidStateTransitionTypeError),
201
202    #[error(transparent)]
203    PublicKeyIsDisabledError(PublicKeyIsDisabledError),
204
205    #[error(transparent)]
206    UncompressedPublicKeyNotAllowedError(UncompressedPublicKeyNotAllowedError),
207
208    #[error(transparent)]
209    IdentityNotPresentError(IdentityNotPresentError),
210
211    /// Error
212    #[error("overflow error: {0}")]
213    Overflow(&'static str),
214
215    #[error("divide by zero error: {0}")]
216    DivideByZero(&'static str),
217
218    /// Error
219    #[error("missing key: {0}")]
220    DesiredKeyWithTypePurposeSecurityLevelMissing(String),
221
222    /// Value error
223    #[error("value error: {0}")]
224    ValueError(#[from] ValueError),
225
226    /// Value error
227    #[error("platform serialization error: {0}")]
228    PlatformSerializationError(String),
229
230    /// Value error
231    #[error("platform deserialization error: {0}")]
232    PlatformDeserializationError(String),
233
234    /// Dash core error
235    #[error("dash core error: {0}")]
236    DashCoreError(#[from] DashCoreError),
237
238    #[error("Invalid Identity: {errors:?}")]
239    InvalidIdentityError {
240        errors: Vec<ConsensusError>,
241        raw_identity: Value,
242    },
243
244    #[error("votes error {0}")]
245    VoteError(String),
246
247    #[error("Public key generation error {0}")]
248    PublicKeyGenerationError(String),
249
250    #[error("group member not found in contract: {0}")]
251    GroupMemberNotFound(String),
252
253    #[error("group not found in contract: {0}")]
254    GroupNotFound(String),
255
256    #[error("corrupted code execution: {0}")]
257    CorruptedCodeExecution(String),
258
259    #[error("corrupted serialization: {0}")]
260    CorruptedSerialization(String),
261
262    #[error("critical corrupted credits code execution: {0}")]
263    CriticalCorruptedCreditsCodeExecution(String),
264
265    #[error(transparent)]
266    InvalidVectorSizeError(InvalidVectorSizeError),
267
268    /// Invalid CBOR error
269    #[error("invalid cbor error: {0}")]
270    InvalidCBOR(String),
271
272    /// BLS signature error
273    #[cfg(feature = "bls-signatures")]
274    #[error(transparent)]
275    BlsError(#[from] dashcore::blsful::BlsError),
276
277    #[error("Private key wrong size: expected 32, got {got}")]
278    PrivateKeySizeError { got: u32 },
279
280    #[error("Private key invalid error: {0}")]
281    InvalidBLSPrivateKeyError(String),
282
283    #[error("Signature wrong size: expected 96, got {got}")]
284    BlsSignatureSizeError { got: u32 },
285
286    /// Error when trying to add two different types of `RewardDistributionMoment`.
287    #[error("Attempted to add incompatible types of RewardDistributionMoment: {0}")]
288    AddingDifferentTypes(String),
289
290    #[error("invalid distribution step error: {0}")]
291    InvalidDistributionStep(&'static str),
292
293    #[error("missing epoch info: {0}")]
294    MissingEpochInfo(String),
295
296    #[error("Invalid BatchedTransitionAction variant: expected {expected}, found {found}")]
297    InvalidBatchedTransitionActionVariant {
298        expected: &'static str,
299        found: &'static str,
300    },
301    #[error(
302        "Invalid verification wrong number of elements: needed {needed}, using {using}, {msg}"
303    )]
304    InvalidVerificationWrongNumberOfElements {
305        needed: u16,
306        using: u16,
307        msg: &'static str,
308    },
309}
310
311impl From<&str> for ProtocolError {
312    fn from(v: &str) -> ProtocolError {
313        ProtocolError::Generic(String::from(v))
314    }
315}
316
317impl From<String> for ProtocolError {
318    fn from(v: String) -> ProtocolError {
319        Self::from(v.as_str())
320    }
321}
322
323impl From<ConsensusError> for ProtocolError {
324    fn from(e: ConsensusError) -> Self {
325        ProtocolError::ConsensusError(Box::new(e))
326    }
327}
328
329impl From<DocumentError> for ProtocolError {
330    fn from(e: DocumentError) -> Self {
331        ProtocolError::Document(Box::new(e))
332    }
333}
334
335impl From<TokenError> for ProtocolError {
336    fn from(e: TokenError) -> Self {
337        ProtocolError::Token(Box::new(e))
338    }
339}
340
341impl From<SerdeParsingError> for ProtocolError {
342    fn from(e: SerdeParsingError) -> Self {
343        ProtocolError::ParsingError(e.to_string())
344    }
345}
346
347impl From<DashPlatformProtocolInitError> for ProtocolError {
348    fn from(e: DashPlatformProtocolInitError) -> Self {
349        ProtocolError::Generic(e.to_string())
350    }
351}
352
353impl From<InvalidVectorSizeError> for ProtocolError {
354    fn from(err: InvalidVectorSizeError) -> Self {
355        Self::InvalidVectorSizeError(err)
356    }
357}