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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
use core::{
    fmt::{self, Debug, Formatter},
    u16, u8,
};

use crate::{
    bytesrepr,
    contract_api::turef::AccessRightsError,
    system_contracts::{mint, pos},
    value::{
        account::{AddKeyFailure, RemoveKeyFailure, SetThresholdFailure, UpdateKeyFailure},
        CLValueError,
    },
};

/// All `Error` variants defined in this library other than `Error::User` will convert to a `u32`
/// value less than or equal to `RESERVED_ERROR_MAX`.
const RESERVED_ERROR_MAX: u32 = u16::MAX as u32; // 0..=65535

/// Proof of Stake errors (defined in "contracts/system/pos/src/error.rs") will have this value
/// added to them when being converted to a `u32`.
const POS_ERROR_OFFSET: u32 = RESERVED_ERROR_MAX - u8::MAX as u32; // 65280..=65535

/// Mint errors (defined in "contracts/system/mint/src/error.rs") will have this value
/// added to them when being converted to a `u32`.
const MINT_ERROR_OFFSET: u32 = (POS_ERROR_OFFSET - 1) - u8::MAX as u32; // 65024..=65279

/// Variants to be passed to `runtime::revert()`.
///
/// Variants other than `Error::User` will represent a `u32` in the range `(0, u16::MAX]`, while
/// `Error::User` will represent a `u32` in the range `(u16::MAX, 2 * u16::MAX + 1]`.
///
/// Users can specify a C-style enum and implement `From` to ease usage of
/// `runtime::revert()`, e.g.
/// ```
/// use casperlabs_contract_ffi::contract_api::Error;
///
/// #[repr(u16)]
/// enum FailureCode {
///     Zero = 0,  // 65,536 as an Error::User
///     One,       // 65,537 as an Error::User
///     Two        // 65,538 as an Error::User
/// }
///
/// impl From<FailureCode> for Error {
///     fn from(code: FailureCode) -> Self {
///         Error::User(code as u16)
///     }
/// }
///
/// assert_eq!(Error::User(1), FailureCode::One.into());
/// assert_eq!(65_536, u32::from(Error::from(FailureCode::Zero)));
/// assert_eq!(65_538, u32::from(Error::from(FailureCode::Two)));
/// ```
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Error {
    /// Optional data was unexpectedly `None`.
    None,
    /// Specified argument not provided.
    MissingArgument,
    /// Argument not of correct type.
    InvalidArgument,
    /// Failed to deserialize a value.
    Deserialize,
    /// `read` returned an error.
    Read,
    /// The given key returned a `None` value.
    ValueNotFound,
    /// Failed to find a specified contract.
    ContractNotFound,
    /// A call to `get_key()` returned a failure.
    GetKey,
    /// The `Key` variant was not as expected.
    UnexpectedKeyVariant,
    /// The `Value` variant was not as expected.
    UnexpectedValueVariant,
    /// The `ContractRef` variant was not as expected.
    UnexpectedContractRefVariant,
    /// Invalid purse name given.
    InvalidPurseName,
    /// Invalid purse retrieved.
    InvalidPurse,
    /// Failed to upgrade contract at URef.
    UpgradeContractAtURef,
    /// Failed to transfer motes.
    Transfer,
    /// No access rights.
    NoAccessRights,
    /// A given type could be derived from a `Value`.
    ValueConversion,
    /// A given type could be derived from a `CLValue`.
    CLTypeMismatch,
    /// Early end of stream when deserializing.
    EarlyEndOfStream,
    /// Formatting error.
    FormattingError,
    /// Leftover bytes.
    LeftOverBytes,
    /// Out of memory error.
    OutOfMemoryError,
    /// Unable to add new associated key because maximum amount of keys is reached.
    MaxKeysLimit,
    /// Unable to add new associated key because given key already exists.
    DuplicateKey,
    /// Unable to add/update/remove new associated key due to insufficient permissions.
    PermissionDenied,
    /// Unable to update/remove a key that does exist.
    MissingKey,
    /// Unable to update/remove a key which would violate action threshold constraints.
    ThresholdViolation,
    /// New threshold should be lower or equal than deployment threshold.
    KeyManagementThresholdError,
    /// New threshold should be lower or equal than key management threshold.
    DeploymentThresholdError,
    /// Unable to set action threshold due to insufficient permissions.
    PermissionDeniedError,
    /// New threshold should be lower or equal than total weight of associated keys.
    InsufficientTotalWeight,
    /// Returns when contract tries to obtain URef to a system contract that does not exist.
    InvalidSystemContract,
    /// Failed to create a new purse.
    PurseNotCreated,
    /// An unhandled value, likely representing a bug in the code.
    Unhandled,
    /// Passing a buffer of a size that is too small to complete an operation
    BufferTooSmall,
    /// No data available in the host buffer.
    HostBufferEmpty,
    /// Data in the host buffer is full and should be consumed first by read operation
    HostBufferFull,
    /// Error specific to Mint contract.
    Mint(u8),
    /// Error specific to Proof of Stake contract.
    ProofOfStake(u8),
    /// User-specified value.  The internal `u16` value is added to `u16::MAX as u32 + 1` when an
    /// `Error::User` is converted to a `u32`.
    User(u16),
}

impl From<bytesrepr::Error> for Error {
    fn from(error: bytesrepr::Error) -> Self {
        match error {
            bytesrepr::Error::EarlyEndOfStream => Error::EarlyEndOfStream,
            bytesrepr::Error::FormattingError => Error::FormattingError,
            bytesrepr::Error::LeftOverBytes => Error::LeftOverBytes,
            bytesrepr::Error::OutOfMemoryError => Error::OutOfMemoryError,
        }
    }
}

impl From<AddKeyFailure> for Error {
    fn from(error: AddKeyFailure) -> Self {
        match error {
            AddKeyFailure::MaxKeysLimit => Error::MaxKeysLimit,
            AddKeyFailure::DuplicateKey => Error::DuplicateKey,
            AddKeyFailure::PermissionDenied => Error::PermissionDenied,
        }
    }
}

impl From<UpdateKeyFailure> for Error {
    fn from(error: UpdateKeyFailure) -> Self {
        match error {
            UpdateKeyFailure::MissingKey => Error::MissingKey,
            UpdateKeyFailure::PermissionDenied => Error::PermissionDenied,
            UpdateKeyFailure::ThresholdViolation => Error::ThresholdViolation,
        }
    }
}

impl From<RemoveKeyFailure> for Error {
    fn from(error: RemoveKeyFailure) -> Self {
        match error {
            RemoveKeyFailure::MissingKey => Error::MissingKey,
            RemoveKeyFailure::PermissionDenied => Error::PermissionDenied,
            RemoveKeyFailure::ThresholdViolation => Error::ThresholdViolation,
        }
    }
}

impl From<SetThresholdFailure> for Error {
    fn from(error: SetThresholdFailure) -> Self {
        match error {
            SetThresholdFailure::KeyManagementThresholdError => Error::KeyManagementThresholdError,
            SetThresholdFailure::DeploymentThresholdError => Error::DeploymentThresholdError,
            SetThresholdFailure::PermissionDeniedError => Error::PermissionDeniedError,
            SetThresholdFailure::InsufficientTotalWeight => Error::InsufficientTotalWeight,
        }
    }
}

impl From<AccessRightsError> for Error {
    fn from(error: AccessRightsError) -> Self {
        match error {
            AccessRightsError::NoAccessRights => Error::NoAccessRights,
        }
    }
}

impl From<CLValueError> for Error {
    fn from(error: CLValueError) -> Self {
        match error {
            CLValueError::Serialization(bytesrepr_error) => bytesrepr_error.into(),
            CLValueError::Type(_) => Error::CLTypeMismatch,
        }
    }
}

impl From<mint::Error> for Error {
    fn from(error: mint::Error) -> Self {
        Error::Mint(error as u8)
    }
}

impl From<pos::Error> for Error {
    fn from(error: pos::Error) -> Self {
        Error::ProofOfStake(error as u8)
    }
}

impl From<Error> for u32 {
    fn from(error: Error) -> Self {
        match error {
            Error::None => 1,
            Error::MissingArgument => 2,
            Error::InvalidArgument => 3,
            Error::Deserialize => 4,
            Error::Read => 5,
            Error::ValueNotFound => 6,
            Error::ContractNotFound => 7,
            Error::GetKey => 8,
            Error::UnexpectedKeyVariant => 9,
            Error::UnexpectedValueVariant => 10,
            Error::UnexpectedContractRefVariant => 11,
            Error::InvalidPurseName => 12,
            Error::InvalidPurse => 13,
            Error::UpgradeContractAtURef => 14,
            Error::Transfer => 15,
            Error::NoAccessRights => 16,
            Error::ValueConversion => 17,
            Error::CLTypeMismatch => 18,
            Error::EarlyEndOfStream => 19,
            Error::FormattingError => 20,
            Error::LeftOverBytes => 21,
            Error::OutOfMemoryError => 22,
            Error::MaxKeysLimit => 23,
            Error::DuplicateKey => 24,
            Error::PermissionDenied => 25,
            Error::MissingKey => 26,
            Error::ThresholdViolation => 27,
            Error::KeyManagementThresholdError => 28,
            Error::DeploymentThresholdError => 29,
            Error::PermissionDeniedError => 30,
            Error::InsufficientTotalWeight => 31,
            Error::InvalidSystemContract => 32,
            Error::PurseNotCreated => 33,
            Error::Unhandled => 34,
            Error::BufferTooSmall => 35,
            Error::HostBufferEmpty => 36,
            Error::HostBufferFull => 37,
            Error::Mint(value) => MINT_ERROR_OFFSET + u32::from(value),
            Error::ProofOfStake(value) => POS_ERROR_OFFSET + u32::from(value),
            Error::User(value) => RESERVED_ERROR_MAX + 1 + u32::from(value),
        }
    }
}

impl Debug for Error {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Error::None => write!(f, "Error::None")?,
            Error::MissingArgument => write!(f, "Error::MissingArgument")?,
            Error::InvalidArgument => write!(f, "Error::InvalidArgument")?,
            Error::Deserialize => write!(f, "Error::Deserialize")?,
            Error::Read => write!(f, "Error::Read")?,
            Error::ValueNotFound => write!(f, "Error::ValueNotFound")?,
            Error::ContractNotFound => write!(f, "Error::ContractNotFound")?,
            Error::GetKey => write!(f, "Error::GetKey")?,
            Error::UnexpectedKeyVariant => write!(f, "Error::UnexpectedKeyVariant")?,
            Error::UnexpectedValueVariant => write!(f, "Error::UnexpectedValueVariant")?,
            Error::UnexpectedContractRefVariant => {
                write!(f, "Error::UnexpectedContractRefVariant")?
            }
            Error::InvalidPurseName => write!(f, "Error::InvalidPurseName")?,
            Error::InvalidPurse => write!(f, "Error::InvalidPurse")?,
            Error::UpgradeContractAtURef => write!(f, "Error::UpgradeContractAtURef")?,
            Error::Transfer => write!(f, "Error::Transfer")?,
            Error::NoAccessRights => write!(f, "Error::NoAccessRights")?,
            Error::ValueConversion => write!(f, "Error::ValueConversion")?,
            Error::CLTypeMismatch => write!(f, "Error::CLTypeMismatch")?,
            Error::EarlyEndOfStream => write!(f, "Error::EarlyEndOfStream")?,
            Error::FormattingError => write!(f, "Error::FormattingError")?,
            Error::LeftOverBytes => write!(f, "Error::LeftOverBytes")?,
            Error::OutOfMemoryError => write!(f, "Error::OutOfMemoryError")?,
            Error::MaxKeysLimit => write!(f, "Error::MaxKeysLimit")?,
            Error::DuplicateKey => write!(f, "Error::DuplicateKey")?,
            Error::PermissionDenied => write!(f, "Error::PermissionDenied")?,
            Error::MissingKey => write!(f, "Error::MissingKey")?,
            Error::ThresholdViolation => write!(f, "Error::ThresholdViolation")?,
            Error::KeyManagementThresholdError => write!(f, "Error::KeyManagementThresholdError")?,
            Error::DeploymentThresholdError => write!(f, "Error::DeploymentThresholdError")?,
            Error::PermissionDeniedError => write!(f, "Error::PermissionDeniedError")?,
            Error::InsufficientTotalWeight => write!(f, "Error::InsufficientTotalWeight")?,
            Error::InvalidSystemContract => write!(f, "Error::InvalidSystemContract")?,
            Error::PurseNotCreated => write!(f, "Error::PurseNotCreated")?,
            Error::Unhandled => write!(f, "Error::Unhandled")?,
            Error::BufferTooSmall => write!(f, "Error::BufferTooSmall")?,
            Error::HostBufferEmpty => write!(f, "Error::HostBufferEmpty")?,
            Error::HostBufferFull => write!(f, "Error::HostBufferFull")?,
            Error::Mint(value) => write!(f, "Error::Mint({})", value)?,
            Error::ProofOfStake(value) => write!(f, "Error::ProofOfStake({})", value)?,
            Error::User(value) => write!(f, "Error::User({})", value)?,
        }
        write!(f, " [{}]", u32::from(*self))
    }
}

pub fn i32_from(result: Result<(), Error>) -> i32 {
    match result {
        Ok(()) => 0,
        Err(error) => u32::from(error) as i32,
    }
}

pub fn result_from(value: i32) -> Result<(), Error> {
    match value {
        0 => Ok(()),
        1 => Err(Error::None),
        2 => Err(Error::MissingArgument),
        3 => Err(Error::InvalidArgument),
        4 => Err(Error::Deserialize),
        5 => Err(Error::Read),
        6 => Err(Error::ValueNotFound),
        7 => Err(Error::ContractNotFound),
        8 => Err(Error::GetKey),
        9 => Err(Error::UnexpectedKeyVariant),
        10 => Err(Error::UnexpectedValueVariant),
        11 => Err(Error::UnexpectedContractRefVariant),
        12 => Err(Error::InvalidPurseName),
        13 => Err(Error::InvalidPurse),
        14 => Err(Error::UpgradeContractAtURef),
        15 => Err(Error::Transfer),
        16 => Err(Error::NoAccessRights),
        17 => Err(Error::ValueConversion),
        18 => Err(Error::CLTypeMismatch),
        19 => Err(Error::EarlyEndOfStream),
        20 => Err(Error::FormattingError),
        21 => Err(Error::LeftOverBytes),
        22 => Err(Error::OutOfMemoryError),
        23 => Err(Error::MaxKeysLimit),
        24 => Err(Error::DuplicateKey),
        25 => Err(Error::PermissionDenied),
        26 => Err(Error::MissingKey),
        27 => Err(Error::ThresholdViolation),
        28 => Err(Error::KeyManagementThresholdError),
        29 => Err(Error::DeploymentThresholdError),
        30 => Err(Error::PermissionDeniedError),
        31 => Err(Error::InsufficientTotalWeight),
        32 => Err(Error::InvalidSystemContract),
        33 => Err(Error::PurseNotCreated),
        34 => Err(Error::Unhandled),
        35 => Err(Error::BufferTooSmall),
        36 => Err(Error::HostBufferEmpty),
        37 => Err(Error::HostBufferFull),
        _ => {
            if value > RESERVED_ERROR_MAX as i32 && value <= (2 * RESERVED_ERROR_MAX + 1) as i32 {
                Err(Error::User(value as u16))
            } else if value >= POS_ERROR_OFFSET as i32 && value <= RESERVED_ERROR_MAX as i32 {
                Err(Error::ProofOfStake(value as u8))
            } else if value >= MINT_ERROR_OFFSET as i32 && value < POS_ERROR_OFFSET as i32 {
                Err(Error::Mint(value as u8))
            } else {
                Err(Error::Unhandled)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use alloc::format;
    use core::{i32, u16, u8};

    use super::*;

    fn round_trip(result: Result<(), Error>) {
        let code = i32_from(result);
        assert_eq!(result, result_from(code));
    }

    #[test]
    fn error() {
        assert_eq!(65_024_u32, Error::Mint(0).into()); // MINT_ERROR_OFFSET == 65,024
        assert_eq!(65_279_u32, Error::Mint(u8::MAX).into());
        assert_eq!(65_280_u32, Error::ProofOfStake(0).into()); // POS_ERROR_OFFSET == 65,280
        assert_eq!(65_535_u32, Error::ProofOfStake(u8::MAX).into());
        assert_eq!(65_536_u32, Error::User(0).into()); // u16::MAX + 1
        assert_eq!(131_071_u32, Error::User(u16::MAX).into()); // 2 * u16::MAX + 1

        assert_eq!("Error::GetKey [8]", &format!("{:?}", Error::GetKey));
        assert_eq!("Error::Mint(0) [65024]", &format!("{:?}", Error::Mint(0)));
        assert_eq!(
            "Error::Mint(255) [65279]",
            &format!("{:?}", Error::Mint(u8::MAX))
        );
        assert_eq!(
            "Error::ProofOfStake(0) [65280]",
            &format!("{:?}", Error::ProofOfStake(0))
        );
        assert_eq!(
            "Error::ProofOfStake(255) [65535]",
            &format!("{:?}", Error::ProofOfStake(u8::MAX))
        );
        assert_eq!("Error::User(0) [65536]", &format!("{:?}", Error::User(0)));
        assert_eq!(
            "Error::User(65535) [131071]",
            &format!("{:?}", Error::User(u16::MAX))
        );

        assert_eq!(Err(Error::Unhandled), result_from(i32::MAX));
        assert_eq!(
            Err(Error::Unhandled),
            result_from(MINT_ERROR_OFFSET as i32 - 1)
        );
        assert_eq!(Err(Error::Unhandled), result_from(-1));
        assert_eq!(Err(Error::Unhandled), result_from(i32::MIN));

        round_trip(Ok(()));
        round_trip(Err(Error::None));
        round_trip(Err(Error::MissingArgument));
        round_trip(Err(Error::InvalidArgument));
        round_trip(Err(Error::Deserialize));
        round_trip(Err(Error::Read));
        round_trip(Err(Error::ValueNotFound));
        round_trip(Err(Error::ContractNotFound));
        round_trip(Err(Error::GetKey));
        round_trip(Err(Error::UnexpectedKeyVariant));
        round_trip(Err(Error::UnexpectedValueVariant));
        round_trip(Err(Error::UnexpectedContractRefVariant));
        round_trip(Err(Error::InvalidPurseName));
        round_trip(Err(Error::InvalidPurse));
        round_trip(Err(Error::UpgradeContractAtURef));
        round_trip(Err(Error::Transfer));
        round_trip(Err(Error::NoAccessRights));
        round_trip(Err(Error::ValueConversion));
        round_trip(Err(Error::CLTypeMismatch));
        round_trip(Err(Error::EarlyEndOfStream));
        round_trip(Err(Error::FormattingError));
        round_trip(Err(Error::LeftOverBytes));
        round_trip(Err(Error::OutOfMemoryError));
        round_trip(Err(Error::MaxKeysLimit));
        round_trip(Err(Error::DuplicateKey));
        round_trip(Err(Error::PermissionDenied));
        round_trip(Err(Error::MissingKey));
        round_trip(Err(Error::ThresholdViolation));
        round_trip(Err(Error::KeyManagementThresholdError));
        round_trip(Err(Error::DeploymentThresholdError));
        round_trip(Err(Error::PermissionDeniedError));
        round_trip(Err(Error::InsufficientTotalWeight));
        round_trip(Err(Error::InvalidSystemContract));
        round_trip(Err(Error::PurseNotCreated));
        round_trip(Err(Error::Unhandled));
        round_trip(Err(Error::BufferTooSmall));
        round_trip(Err(Error::HostBufferEmpty));
        round_trip(Err(Error::HostBufferFull));
        round_trip(Err(Error::Mint(0)));
        round_trip(Err(Error::Mint(u8::MAX)));
        round_trip(Err(Error::ProofOfStake(0)));
        round_trip(Err(Error::ProofOfStake(u8::MAX)));
        round_trip(Err(Error::User(0)));
        round_trip(Err(Error::User(u16::MAX)));
    }
}