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
use casper_types::CLType;
use crate::arithmetic::ArithmeticsError;
use crate::prelude::*;
use core::any::Any;
/// General error type in Odra framework
#[repr(u16)]
#[derive(Clone, Debug, PartialEq)]
pub enum OdraError {
/// An error that can occur during smart contract execution
ExecutionError(ExecutionError),
/// An internal virtual machine error
VmError(VmError)
}
impl OdraError {
/// Returns the error code.
pub fn code(&self) -> u16 {
match self {
OdraError::ExecutionError(e) => e.code(),
OdraError::VmError(_e) => 123
}
}
/// Creates a new user error with a given code.
pub fn user(code: u16) -> Self {
OdraError::ExecutionError(ExecutionError::User(code))
}
}
impl From<ArithmeticsError> for ExecutionError {
fn from(error: ArithmeticsError) -> Self {
match error {
ArithmeticsError::AdditionOverflow => Self::AdditionOverflow,
ArithmeticsError::SubtractingOverflow => Self::SubtractionOverflow
}
}
}
impl From<ArithmeticsError> for OdraError {
fn from(error: ArithmeticsError) -> Self {
Into::<ExecutionError>::into(error).into()
}
}
impl From<Box<dyn Any + Send>> for OdraError {
fn from(_: Box<dyn Any + Send>) -> Self {
OdraError::VmError(VmError::Panic)
}
}
impl From<casper_types::bytesrepr::Error> for ExecutionError {
fn from(error: casper_types::bytesrepr::Error) -> Self {
match error {
casper_types::bytesrepr::Error::EarlyEndOfStream => Self::EarlyEndOfStream,
casper_types::bytesrepr::Error::Formatting => Self::Formatting,
casper_types::bytesrepr::Error::LeftOverBytes => Self::LeftOverBytes,
casper_types::bytesrepr::Error::OutOfMemory => Self::OutOfMemory,
casper_types::bytesrepr::Error::NotRepresentable => Self::NotRepresentable,
casper_types::bytesrepr::Error::ExceededRecursionDepth => Self::ExceededRecursionDepth,
_ => Self::Formatting
}
}
}
/// An error that can occur during smart contract execution
///
/// It is represented by an error code and a human-readable message.
///
/// Errors codes 0..32767 are available for the user to define custom error
/// in smart contracts.
/// 32768 code is a special code representing a violation of the custom error code space.
///
/// The rest of codes 32769..[u16::MAX](u16::MAX), are used internally by the framework.
#[repr(u16)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ExecutionError {
/// Unwrap error
UnwrapError = 1,
/// Addition overflow
AdditionOverflow = 100,
/// Subtraction overflow
SubtractionOverflow = 101,
/// Method does not accept deposit
NonPayable = 102,
/// Can't transfer tokens to contract.
TransferToContract = 103,
/// Reentrant call detected
ReentrantCall = 104,
/// Contract already installed
ContractAlreadyInstalled = 105,
/// Unknown constructor
UnknownConstructor = 106,
/// Native transfer error
NativeTransferError = 107,
/// Index out of bounds
IndexOutOfBounds = 108,
/// Tried to construct a zero address.
ZeroAddress = 109,
/// Address creation failed
AddressCreationFailed = 110,
/// Early end of stream - deserialization error
EarlyEndOfStream = 111,
/// Formatting error - deserialization error
Formatting = 112,
/// Left over bytes - deserialization error
LeftOverBytes = 113,
/// Out of memory
OutOfMemory = 114,
/// Not representable
NotRepresentable = 115,
/// Exceeded recursion depth
ExceededRecursionDepth = 116,
/// Key not found
KeyNotFound = 117,
/// Could not deserialize signature
CouldNotDeserializeSignature = 118,
/// Type mismatch
TypeMismatch = 119,
/// Could not sign message
CouldNotSignMessage = 120,
/// Maximum code for user errors
MaxUserError = 32767,
/// User error too high. The code should be in range 0..32767.
UserErrorTooHigh = 32768,
/// User error
User(u16)
}
impl ExecutionError {
/// Returns the error code.
pub fn code(&self) -> u16 {
unsafe {
match self {
ExecutionError::User(code) => *code,
ExecutionError::UserErrorTooHigh => 32768,
_ => ExecutionError::UserErrorTooHigh.code() + *(self as *const Self as *const u16)
}
}
}
}
impl From<ExecutionError> for OdraError {
fn from(error: ExecutionError) -> Self {
Self::ExecutionError(error)
}
}
/// An internal virtual machine error
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum VmError {
/// Failed to serialize a value.
Serialization,
/// Failed to deserialize a value.
Deserialization,
/// Exceeded the account balance
BalanceExceeded,
/// Non existing host entrypoint was called.
NoSuchMethod(String),
/// Accessing a contract with an invalid address.
InvalidContractAddress,
/// Error calling a host function in a wrong context.
InvalidContext,
/// Calling a contract with missing entrypoint arguments.
MissingArg,
/// Calling a contract with a wrong argument type.
TypeMismatch {
/// Expected type.
expected: CLType,
/// Found type.
found: CLType
},
/// Non-specified error with a custom message.
Other(String),
/// Unspecified error.
Panic
}
/// Error that can occur while operating on a collection.
pub enum CollectionError {
/// The requested index is bigger than the max collection index.
IndexOutOfBounds
}
impl From<CollectionError> for ExecutionError {
fn from(error: CollectionError) -> Self {
match error {
CollectionError::IndexOutOfBounds => Self::IndexOutOfBounds
}
}
}
impl From<CollectionError> for OdraError {
fn from(error: CollectionError) -> Self {
Into::<ExecutionError>::into(error).into()
}
}
/// Error that can occur while operating on an Address.
#[derive(Clone, Debug, PartialEq)]
pub enum AddressError {
/// Tried to construct a zero address.
ZeroAddress,
/// Tried to construct an address and failed.
AddressCreationError
}
impl From<AddressError> for ExecutionError {
fn from(error: AddressError) -> Self {
match error {
AddressError::ZeroAddress => Self::ZeroAddress,
AddressError::AddressCreationError => Self::AddressCreationFailed
}
}
}
impl From<AddressError> for OdraError {
fn from(error: AddressError) -> Self {
Into::<ExecutionError>::into(error).into()
}
}
/// Event-related errors.
#[derive(Debug, PartialEq, Eq, PartialOrd)]
pub enum EventError {
/// The type of event is different than expected.
UnexpectedType(String),
/// Index of the event is out of bounds.
IndexOutOfBounds,
/// Formatting error while deserializing.
Formatting,
/// Unexpected error while deserializing.
Parsing,
/// Could not extract event name.
CouldntExtractName,
/// Could not extract event data.
CouldntExtractEventData
}
/// Represents the result of a contract call.
pub type OdraResult<T> = Result<T, OdraError>;