Skip to main content

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    /// External signer (e.g. Swift Keychain-backed
144    /// `MnemonicResolverCoreSigner`) reported a failure or returned a
145    /// non-conformant result. This is a distinct surface from
146    /// [`Self::Generic`] so callers (FFI layer, retry policies) can
147    /// distinguish a signer-side failure from a generic protocol
148    /// invariant.
149    ///
150    /// Examples of failures that surface here:
151    /// - The signer's `sign_ecdsa` future returned an error (resolver
152    ///   miss, derivation failure, invalid mnemonic, etc.).
153    /// - The signer returned a signature whose recovery id does not
154    ///   match the returned public key (invariant violation by a
155    ///   non-conformant signer — distinct from a soft failure, but
156    ///   surfaced uniformly here so the FFI layer doesn't need to
157    ///   pattern-match on signer-specific shapes).
158    #[error("External signer error: {0}")]
159    ExternalSignerError(String),
160
161    #[error("Address witness verification error: {0}")]
162    AddressWitnessError(String),
163
164    #[error("Shielded transaction build error: {0}")]
165    ShieldedBuildError(String),
166
167    #[error("Not supported Error: {0}")]
168    NotSupported(String),
169
170    #[cfg(feature = "message-signing")]
171    #[error("Invalid signing type error: {0}")]
172    InvalidSigningKeyTypeError(String),
173
174    // State Transition Errors
175    #[cfg(any(
176        feature = "state-transition-validation",
177        feature = "state-transition-signing"
178    ))]
179    #[error(transparent)]
180    InvalidIdentityPublicKeyTypeError(InvalidIdentityPublicKeyTypeError),
181    #[cfg(feature = "state-transition-validation")]
182    #[error(transparent)]
183    StateTransitionIsNotSignedError(StateTransitionIsNotSignedError),
184    #[cfg(feature = "state-transition-validation")]
185    #[error(transparent)]
186    PublicKeySecurityLevelNotMetError(PublicKeySecurityLevelNotMetError),
187    #[cfg(any(
188        all(feature = "state-transitions", feature = "validation"),
189        feature = "state-transition-validation",
190        feature = "state-transition-signing",
191        feature = "state-transition-validation"
192    ))]
193    #[error(transparent)]
194    WrongPublicKeyPurposeError(WrongPublicKeyPurposeError),
195    #[cfg(feature = "state-transition-validation")]
196    #[error(transparent)]
197    PublicKeyMismatchError(PublicKeyMismatchError),
198    #[cfg(feature = "state-transition-validation")]
199    #[error(transparent)]
200    InvalidSignaturePublicKeyError(InvalidSignaturePublicKeyError),
201
202    #[error(transparent)]
203    NonConsensusError(#[from] NonConsensusError),
204
205    #[error(transparent)]
206    CompatibleProtocolVersionIsNotDefinedError(#[from] CompatibleProtocolVersionIsNotDefinedError),
207
208    #[error(transparent)]
209    InvalidDocumentTypeError(InvalidDocumentTypeError),
210
211    #[error(transparent)]
212    DataContractNotPresentError(DataContractNotPresentError),
213
214    #[error(transparent)]
215    InvalidSignaturePublicKeySecurityLevelError(InvalidSignaturePublicKeySecurityLevelError),
216
217    #[error(transparent)]
218    InvalidStateTransitionTypeError(InvalidStateTransitionTypeError),
219
220    #[error(transparent)]
221    PublicKeyIsDisabledError(PublicKeyIsDisabledError),
222
223    #[error(transparent)]
224    UncompressedPublicKeyNotAllowedError(UncompressedPublicKeyNotAllowedError),
225
226    #[error(transparent)]
227    IdentityNotPresentError(IdentityNotPresentError),
228
229    /// Error
230    #[error("overflow error: {0}")]
231    Overflow(&'static str),
232
233    #[error("divide by zero error: {0}")]
234    DivideByZero(&'static str),
235
236    /// Error
237    #[error("missing key: {0}")]
238    DesiredKeyWithTypePurposeSecurityLevelMissing(String),
239
240    /// Value error
241    #[error("value error: {0}")]
242    ValueError(#[from] ValueError),
243
244    /// Value error
245    #[error("platform serialization error: {0}")]
246    PlatformSerializationError(String),
247
248    /// Value error
249    #[error("platform deserialization error: {0}")]
250    PlatformDeserializationError(String),
251
252    /// Dash core error
253    #[error("dash core error: {0}")]
254    DashCoreError(#[from] DashCoreError),
255
256    #[error("Invalid Identity: {errors:?}")]
257    InvalidIdentityError {
258        errors: Vec<ConsensusError>,
259        raw_identity: Value,
260    },
261
262    #[error("votes error {0}")]
263    VoteError(String),
264
265    #[error("Public key generation error {0}")]
266    PublicKeyGenerationError(String),
267
268    #[error("group member not found in contract: {0}")]
269    GroupMemberNotFound(String),
270
271    #[error("group not found in contract: {0}")]
272    GroupNotFound(String),
273
274    #[error("corrupted code execution: {0}")]
275    CorruptedCodeExecution(String),
276
277    #[error("corrupted serialization: {0}")]
278    CorruptedSerialization(String),
279
280    #[error("critical corrupted credits code execution: {0}")]
281    CriticalCorruptedCreditsCodeExecution(String),
282
283    #[error(transparent)]
284    InvalidVectorSizeError(InvalidVectorSizeError),
285
286    /// Invalid CBOR error
287    #[error("invalid cbor error: {0}")]
288    InvalidCBOR(String),
289
290    /// BLS signature error
291    #[cfg(feature = "bls-signatures")]
292    #[error(transparent)]
293    BlsError(#[from] dashcore::blsful::BlsError),
294
295    #[error("Private key wrong size: expected 32, got {got}")]
296    PrivateKeySizeError { got: u32 },
297
298    #[error("Private key invalid error: {0}")]
299    InvalidBLSPrivateKeyError(String),
300
301    #[error("Signature wrong size: expected 96, got {got}")]
302    BlsSignatureSizeError { got: u32 },
303
304    /// Error when trying to add two different types of `RewardDistributionMoment`.
305    #[error("Attempted to add incompatible types of RewardDistributionMoment: {0}")]
306    AddingDifferentTypes(String),
307
308    #[error("invalid distribution step error: {0}")]
309    InvalidDistributionStep(&'static str),
310
311    #[error("missing epoch info: {0}")]
312    MissingEpochInfo(String),
313
314    #[error("Invalid BatchedTransitionAction variant: expected {expected}, found {found}")]
315    InvalidBatchedTransitionActionVariant {
316        expected: &'static str,
317        found: &'static str,
318    },
319    #[error(
320        "Invalid verification wrong number of elements: needed {needed}, using {using}, {msg}"
321    )]
322    InvalidVerificationWrongNumberOfElements {
323        needed: u16,
324        using: u16,
325        msg: &'static str,
326    },
327}
328
329impl From<&str> for ProtocolError {
330    fn from(v: &str) -> ProtocolError {
331        ProtocolError::Generic(String::from(v))
332    }
333}
334
335impl From<String> for ProtocolError {
336    fn from(v: String) -> ProtocolError {
337        Self::from(v.as_str())
338    }
339}
340
341impl From<ConsensusError> for ProtocolError {
342    fn from(e: ConsensusError) -> Self {
343        ProtocolError::ConsensusError(Box::new(e))
344    }
345}
346
347impl From<DocumentError> for ProtocolError {
348    fn from(e: DocumentError) -> Self {
349        ProtocolError::Document(Box::new(e))
350    }
351}
352
353impl From<TokenError> for ProtocolError {
354    fn from(e: TokenError) -> Self {
355        ProtocolError::Token(Box::new(e))
356    }
357}
358
359impl From<SerdeParsingError> for ProtocolError {
360    fn from(e: SerdeParsingError) -> Self {
361        ProtocolError::ParsingError(e.to_string())
362    }
363}
364
365impl From<DashPlatformProtocolInitError> for ProtocolError {
366    fn from(e: DashPlatformProtocolInitError) -> Self {
367        ProtocolError::Generic(e.to_string())
368    }
369}
370
371impl From<InvalidVectorSizeError> for ProtocolError {
372    fn from(err: InvalidVectorSizeError) -> Self {
373        Self::InvalidVectorSizeError(err)
374    }
375}