diff --git a/engine/src/connector.rs b/engine/src/connector.rs index 4fac3a218..96fe98b99 100644 --- a/engine/src/connector.rs +++ b/engine/src/connector.rs @@ -725,13 +725,14 @@ pub fn get_metadata(io: &I) -> Option { } pub mod error { + use crate::errors; use aurora_engine_types::types::address::error::AddressError; use aurora_engine_types::types::balance::error::BalanceOverflowError; use crate::deposit_event::error::ParseOnTransferMessageError; use crate::{deposit_event, fungible_token}; - const PROOF_EXIST: &[u8; 15] = b"ERR_PROOF_EXIST"; + const PROOF_EXIST: &[u8; 15] = errors::ERR_PROOF_EXIST; #[cfg_attr(not(target_arch = "wasm32"), derive(Debug))] pub enum StorageReadError { @@ -742,8 +743,8 @@ pub mod error { impl AsRef<[u8]> for StorageReadError { fn as_ref(&self) -> &[u8] { match self { - Self::KeyNotFound => b"ERR_CONNECTOR_STORAGE_KEY_NOT_FOUND", - Self::BorshDeserialize => b"ERR_FAILED_DESERIALIZE_CONNECTOR_DATA", + Self::KeyNotFound => errors::ERR_CONNECTOR_STORAGE_KEY_NOT_FOUND, + Self::BorshDeserialize => errors::ERR_FAILED_DESERIALIZE_CONNECTOR_DATA, } } } @@ -764,7 +765,7 @@ pub mod error { Self::Paused => crate::admin_controlled::ERR_PAUSED.as_bytes(), Self::ProofParseFailed => super::ERR_FAILED_PARSE.as_bytes(), Self::EventParseFailed(e) => e.as_ref(), - Self::CustodianAddressMismatch => b"ERR_WRONG_EVENT_ADDRESS", + Self::CustodianAddressMismatch => errors::ERR_WRONG_EVENT_ADDRESS, Self::InsufficientAmountForFee => super::ERR_NOT_ENOUGH_BALANCE_FOR_FEE.as_bytes(), Self::InvalidAddress(e) => e.as_ref(), } @@ -871,7 +872,7 @@ pub mod error { impl AsRef<[u8]> for InitContractError { fn as_ref(&self) -> &[u8] { match self { - Self::AlreadyInitialized => b"ERR_CONTRACT_INITIALIZED", + Self::AlreadyInitialized => errors::ERR_CONTRACT_INITIALIZED, Self::InvalidCustodianAddress(e) => e.as_ref(), } } diff --git a/engine/src/deposit_event.rs b/engine/src/deposit_event.rs index 815182f03..055e139bd 100644 --- a/engine/src/deposit_event.rs +++ b/engine/src/deposit_event.rs @@ -303,6 +303,7 @@ impl DepositedEvent { pub mod error { use super::*; + use crate::errors; #[derive(Debug)] pub enum DecodeError { @@ -312,8 +313,8 @@ pub mod error { impl AsRef<[u8]> for DecodeError { fn as_ref(&self) -> &[u8] { match self { - Self::RlpFailed => b"ERR_RLP_FAILED", - Self::SchemaMismatch => b"ERR_PARSE_DEPOSIT_EVENT", + Self::RlpFailed => errors::ERR_RLP_FAILED, + Self::SchemaMismatch => errors::ERR_PARSE_DEPOSIT_EVENT, } } } @@ -329,8 +330,8 @@ pub mod error { impl AsRef<[u8]> for ParseEventMessageError { fn as_ref(&self) -> &[u8] { match self { - Self::TooManyParts => b"ERR_INVALID_EVENT_MESSAGE_FORMAT", - Self::InvalidAccount => b"ERR_INVALID_ACCOUNT_ID", + Self::TooManyParts => errors::ERR_INVALID_EVENT_MESSAGE_FORMAT, + Self::InvalidAccount => errors::ERR_INVALID_ACCOUNT_ID, Self::EthAddressValidationError(e) => e.as_ref(), Self::ParseMessageError(e) => e.as_ref(), } @@ -356,11 +357,11 @@ pub mod error { fn as_ref(&self) -> &[u8] { match self { Self::LogParseFailed(e) => e.as_ref(), - Self::InvalidSender => b"ERR_INVALID_SENDER", - Self::InvalidAmount => b"ERR_INVALID_AMOUNT", - Self::InvalidFee => b"ERR_INVALID_FEE", + Self::InvalidSender => errors::ERR_INVALID_SENDER, + Self::InvalidAmount => errors::ERR_INVALID_AMOUNT, + Self::InvalidFee => errors::ERR_INVALID_FEE, Self::MessageParseFailed(e) => e.as_ref(), - Self::OverflowNumber => b"ERR_OVERFLOW_NUMBER", + Self::OverflowNumber => errors::ERR_OVERFLOW_NUMBER, } } } @@ -377,11 +378,11 @@ pub mod error { impl AsRef<[u8]> for ParseOnTransferMessageError { fn as_ref(&self) -> &[u8] { match self { - Self::TooManyParts => b"ERR_INVALID_ON_TRANSFER_MESSAGE_FORMAT", - Self::InvalidHexData => b"ERR_INVALID_ON_TRANSFER_MESSAGE_HEX", - Self::WrongMessageFormat => b"ERR_INVALID_ON_TRANSFER_MESSAGE_DATA", - Self::InvalidAccount => b"ERR_INVALID_ACCOUNT_ID", - Self::OverflowNumber => b"ERR_OVERFLOW_NUMBER", + Self::TooManyParts => errors::ERR_INVALID_ON_TRANSFER_MESSAGE_FORMAT, + Self::InvalidHexData => errors::ERR_INVALID_ON_TRANSFER_MESSAGE_HEX, + Self::WrongMessageFormat => errors::ERR_INVALID_ON_TRANSFER_MESSAGE_DATA, + Self::InvalidAccount => errors::ERR_INVALID_ACCOUNT_ID, + Self::OverflowNumber => errors::ERR_OVERFLOW_NUMBER, } } } diff --git a/engine/src/engine.rs b/engine/src/engine.rs index d8ea9a0b7..9febed31c 100644 --- a/engine/src/engine.rs +++ b/engine/src/engine.rs @@ -5,6 +5,7 @@ use evm::executor; use evm::{Config, CreateScheme, ExitError, ExitFatal, ExitReason}; use crate::connector::EthConnectorContract; +use crate::errors; use crate::map::BijectionMap; use aurora_engine_sdk::caching::FullCache; use aurora_engine_sdk::env::Env; @@ -114,31 +115,31 @@ impl EngineErrorKind { pub fn as_bytes(&self) -> &[u8] { use EngineErrorKind::*; match self { - EvmError(ExitError::StackUnderflow) => b"ERR_STACK_UNDERFLOW", - EvmError(ExitError::StackOverflow) => b"ERR_STACK_OVERFLOW", - EvmError(ExitError::InvalidJump) => b"ERR_INVALID_JUMP", - EvmError(ExitError::InvalidRange) => b"ERR_INVALID_RANGE", - EvmError(ExitError::DesignatedInvalid) => b"ERR_DESIGNATED_INVALID", - EvmError(ExitError::CallTooDeep) => b"ERR_CALL_TOO_DEEP", - EvmError(ExitError::CreateCollision) => b"ERR_CREATE_COLLISION", - EvmError(ExitError::CreateContractLimit) => b"ERR_CREATE_CONTRACT_LIMIT", - EvmError(ExitError::OutOfOffset) => b"ERR_OUT_OF_OFFSET", - EvmError(ExitError::OutOfGas) => b"ERR_OUT_OF_GAS", - EvmError(ExitError::OutOfFund) => b"ERR_OUT_OF_FUND", + EvmError(ExitError::StackUnderflow) => errors::ERR_STACK_UNDERFLOW, + EvmError(ExitError::StackOverflow) => errors::ERR_STACK_OVERFLOW, + EvmError(ExitError::InvalidJump) => errors::ERR_INVALID_JUMP, + EvmError(ExitError::InvalidRange) => errors::ERR_INVALID_RANGE, + EvmError(ExitError::DesignatedInvalid) => errors::ERR_DESIGNATED_INVALID, + EvmError(ExitError::CallTooDeep) => errors::ERR_CALL_TOO_DEEP, + EvmError(ExitError::CreateCollision) => errors::ERR_CREATE_COLLISION, + EvmError(ExitError::CreateContractLimit) => errors::ERR_CREATE_CONTRACT_LIMIT, + EvmError(ExitError::OutOfOffset) => errors::ERR_OUT_OF_OFFSET, + EvmError(ExitError::OutOfGas) => errors::ERR_OUT_OF_GAS, + EvmError(ExitError::OutOfFund) => errors::ERR_OUT_OF_FUND, EvmError(ExitError::Other(m)) => m.as_bytes(), EvmError(_) => unreachable!(), // unused misc - EvmFatal(ExitFatal::NotSupported) => b"ERR_NOT_SUPPORTED", - EvmFatal(ExitFatal::UnhandledInterrupt) => b"ERR_UNHANDLED_INTERRUPT", + EvmFatal(ExitFatal::NotSupported) => errors::ERR_NOT_SUPPORTED, + EvmFatal(ExitFatal::UnhandledInterrupt) => errors::ERR_UNHANDLED_INTERRUPT, EvmFatal(ExitFatal::Other(m)) => m.as_bytes(), EvmFatal(_) => unreachable!(), // unused misc - IncorrectNonce => b"ERR_INCORRECT_NONCE", + IncorrectNonce => errors::ERR_INCORRECT_NONCE, FailedTransactionParse(e) => e.as_ref(), - InvalidChainId => b"ERR_INVALID_CHAIN_ID", - InvalidSignature => b"ERR_INVALID_ECDSA_SIGNATURE", - IntrinsicGasNotMet => b"ERR_INTRINSIC_GAS", - MaxPriorityGasFeeTooLarge => b"ERR_MAX_PRIORITY_FEE_GREATER", + InvalidChainId => errors::ERR_INVALID_CHAIN_ID, + InvalidSignature => errors::ERR_INVALID_ECDSA_SIGNATURE, + IntrinsicGasNotMet => errors::ERR_INTRINSIC_GAS, + MaxPriorityGasFeeTooLarge => errors::ERR_MAX_PRIORITY_FEE_GREATER, GasPayment(e) => e.as_ref(), - GasOverflow => b"ERR_GAS_OVERFLOW", + GasOverflow => errors::ERR_GAS_OVERFLOW, } } } @@ -190,7 +191,7 @@ pub struct BalanceOverflow; impl AsRef<[u8]> for BalanceOverflow { fn as_ref(&self) -> &[u8] { - b"ERR_BALANCE_OVERFLOW" + errors::ERR_BALANCE_OVERFLOW } } @@ -210,8 +211,8 @@ impl AsRef<[u8]> for GasPaymentError { fn as_ref(&self) -> &[u8] { match self { Self::BalanceOverflow(overflow) => overflow.as_ref(), - Self::EthAmountOverflow => b"ERR_GAS_ETH_AMOUNT_OVERFLOW", - Self::OutOfFund => b"ERR_OUT_OF_FUND", + Self::EthAmountOverflow => errors::ERR_GAS_ETH_AMOUNT_OVERFLOW, + Self::OutOfFund => errors::ERR_OUT_OF_FUND, } } } @@ -267,7 +268,7 @@ pub struct AddressParseError; impl AsRef<[u8]> for AddressParseError { fn as_ref(&self) -> &[u8] { - b"ERR_PARSE_ADDRESS" + errors::ERR_PARSE_ADDRESS } } @@ -340,8 +341,8 @@ pub enum EngineStateError { impl AsRef<[u8]> for EngineStateError { fn as_ref(&self) -> &[u8] { match self { - Self::NotFound => b"ERR_STATE_NOT_FOUND", - Self::DeserializationFailed => b"ERR_STATE_CORRUPTED", + Self::NotFound => errors::ERR_STATE_NOT_FOUND, + Self::DeserializationFailed => errors::ERR_STATE_CORRUPTED, } } } diff --git a/engine/src/errors.rs b/engine/src/errors.rs new file mode 100644 index 000000000..4385cf9f9 --- /dev/null +++ b/engine/src/errors.rs @@ -0,0 +1,88 @@ +pub const ERR_NOT_A_JSON_TYPE: &[u8; 19] = b"ERR_NOT_A_JSON_TYPE"; +pub const ERR_JSON_MISSING_VALUE: &[u8; 22] = b"ERR_JSON_MISSING_VALUE"; +pub const ERR_FAILED_PARSE_U8: &[u8; 19] = b"ERR_FAILED_PARSE_U8"; +pub const ERR_FAILED_PARSE_U64: &[u8; 20] = b"ERR_FAILED_PARSE_U64"; +pub const ERR_FAILED_PARSE_U128: &[u8; 21] = b"ERR_FAILED_PARSE_U128"; +pub const ERR_FAILED_PARSE_BOOL: &[u8; 21] = b"ERR_FAILED_PARSE_BOOL"; +pub const ERR_FAILED_PARSE_STRING: &[u8; 23] = b"ERR_FAILED_PARSE_STRING"; +pub const ERR_FAILED_PARSE_ARRAY: &[u8; 22] = b"ERR_FAILED_PARSE_ARRAY"; +pub const ERR_EXPECTED_STRING_GOT_NUMBER: &[u8; 30] = b"ERR_EXPECTED_STRING_GOT_NUMBER"; +pub const ERR_OUT_OF_RANGE_U8: &[u8; 19] = b"ERR_OUT_OF_RANGE_U8"; +pub const ERR_OUT_OF_RANGE_U128: &[u8; 21] = b"ERR_OUT_OF_RANGE_U128"; + +pub const ERR_PROMISE_COUNT: &[u8; 17] = b"ERR_PROMISE_COUNT"; +pub const ERR_REFUND_FAILURE: &[u8; 18] = b"ERR_REFUND_FAILURE"; +pub const ERR_NOT_ALLOWED_TOO_EARLY: &[u8; 25] = b"ERR_NOT_ALLOWED:TOO_EARLY"; +pub const ERR_PROMISE_FAILED: &[u8; 18] = b"ERR_PROMISE_FAILED"; +pub const ERR_VERIFY_PROOF: &[u8; 16] = b"ERR_VERIFY_PROOF"; +pub const ERR_INVALID_UPGRADE: &[u8; 19] = b"ERR_INVALID_UPGRADE"; +pub const ERR_NO_UPGRADE: &[u8; 14] = b"ERR_NO_UPGRADE"; +pub const ERR_NOT_ALLOWED: &[u8; 15] = b"ERR_NOT_ALLOWED"; + +pub const ERR_SERIALIZE: &str = "ERR_SERIALIZE"; +pub const ERR_PROMISE_ENCODING: &str = "ERR_PROMISE_ENCODING"; +pub const ERR_ARGS: &str = "ERR_ARGS"; + +pub const ERR_BORSH_DESERIALIZE: &str = "ERR_BORSH_DESERIALIZE"; +pub const ERR_META_TX_PARSE: &str = "ERR_META_TX_PARSE"; + +pub const ERR_STACK_UNDERFLOW: &[u8; 19] = b"ERR_STACK_UNDERFLOW"; +pub const ERR_STACK_OVERFLOW: &[u8; 18] = b"ERR_STACK_OVERFLOW"; +pub const ERR_INVALID_JUMP: &[u8; 16] = b"ERR_INVALID_JUMP"; +pub const ERR_INVALID_RANGE: &[u8; 17] = b"ERR_INVALID_RANGE"; +pub const ERR_DESIGNATED_INVALID: &[u8; 22] = b"ERR_DESIGNATED_INVALID"; +pub const ERR_CALL_TOO_DEEP: &[u8; 17] = b"ERR_CALL_TOO_DEEP"; +pub const ERR_CREATE_COLLISION: &[u8; 20] = b"ERR_CREATE_COLLISION"; +pub const ERR_CREATE_CONTRACT_LIMIT: &[u8; 25] = b"ERR_CREATE_CONTRACT_LIMIT"; +pub const ERR_OUT_OF_OFFSET: &[u8; 17] = b"ERR_OUT_OF_OFFSET"; +pub const ERR_OUT_OF_GAS: &[u8; 14] = b"ERR_OUT_OF_GAS"; +pub const ERR_OUT_OF_FUND: &[u8; 15] = b"ERR_OUT_OF_FUND"; +pub const ERR_NOT_SUPPORTED: &[u8; 17] = b"ERR_NOT_SUPPORTED"; +pub const ERR_UNHANDLED_INTERRUPT: &[u8; 23] = b"ERR_UNHANDLED_INTERRUPT"; +pub const ERR_INCORRECT_NONCE: &[u8; 19] = b"ERR_INCORRECT_NONCE"; +pub const ERR_INVALID_CHAIN_ID: &[u8; 20] = b"ERR_INVALID_CHAIN_ID"; +pub const ERR_INVALID_ECDSA_SIGNATURE: &[u8; 27] = b"ERR_INVALID_ECDSA_SIGNATURE"; +pub const ERR_INTRINSIC_GAS: &[u8; 17] = b"ERR_INTRINSIC_GAS"; +pub const ERR_MAX_PRIORITY_FEE_GREATER: &[u8; 28] = b"ERR_MAX_PRIORITY_FEE_GREATER"; +pub const ERR_GAS_OVERFLOW: &[u8; 16] = b"ERR_GAS_OVERFLOW"; +pub const ERR_BALANCE_OVERFLOW: &[u8; 20] = b"ERR_BALANCE_OVERFLOW"; +pub const ERR_GAS_ETH_AMOUNT_OVERFLOW: &[u8; 27] = b"ERR_GAS_ETH_AMOUNT_OVERFLOW"; +pub const ERR_PARSE_ADDRESS: &[u8; 17] = b"ERR_PARSE_ADDRESS"; +pub const ERR_STATE_NOT_FOUND: &[u8; 19] = b"ERR_STATE_NOT_FOUND"; +pub const ERR_STATE_CORRUPTED: &[u8; 19] = b"ERR_STATE_CORRUPTED"; + +pub const ERR_CONNECTOR_STORAGE_KEY_NOT_FOUND: &[u8; 35] = b"ERR_CONNECTOR_STORAGE_KEY_NOT_FOUND"; +pub const ERR_FAILED_DESERIALIZE_CONNECTOR_DATA: &[u8; 37] = + b"ERR_FAILED_DESERIALIZE_CONNECTOR_DATA"; +pub const ERR_PROOF_EXIST: &[u8; 15] = b"ERR_PROOF_EXIST"; +pub const ERR_WRONG_EVENT_ADDRESS: &[u8; 23] = b"ERR_WRONG_EVENT_ADDRESS"; +pub const ERR_CONTRACT_INITIALIZED: &[u8; 24] = b"ERR_CONTRACT_INITIALIZED"; + +pub const ERR_RLP_FAILED: &[u8; 14] = b"ERR_RLP_FAILED"; +pub const ERR_PARSE_DEPOSIT_EVENT: &[u8; 23] = b"ERR_PARSE_DEPOSIT_EVENT"; +pub const ERR_INVALID_EVENT_MESSAGE_FORMAT: &[u8; 32] = b"ERR_INVALID_EVENT_MESSAGE_FORMAT"; +pub const ERR_INVALID_SENDER: &[u8; 18] = b"ERR_INVALID_SENDER"; +pub const ERR_INVALID_AMOUNT: &[u8; 18] = b"ERR_INVALID_AMOUNT"; +pub const ERR_INVALID_FEE: &[u8; 15] = b"ERR_INVALID_FEE"; +pub const ERR_INVALID_ON_TRANSFER_MESSAGE_FORMAT: &[u8; 38] = + b"ERR_INVALID_ON_TRANSFER_MESSAGE_FORMAT"; +pub const ERR_INVALID_ON_TRANSFER_MESSAGE_HEX: &[u8; 35] = b"ERR_INVALID_ON_TRANSFER_MESSAGE_HEX"; +pub const ERR_INVALID_ON_TRANSFER_MESSAGE_DATA: &[u8; 36] = b"ERR_INVALID_ON_TRANSFER_MESSAGE_DATA"; +pub const ERR_INVALID_ACCOUNT_ID: &[u8; 22] = b"ERR_INVALID_ACCOUNT_ID"; +pub const ERR_OVERFLOW_NUMBER: &[u8; 19] = b"ERR_OVERFLOW_NUMBER"; + +pub const ERR_TOTAL_SUPPLY_OVERFLOW: &[u8; 25] = b"ERR_TOTAL_SUPPLY_OVERFLOW"; +pub const ERR_NOT_ENOUGH_BALANCE: &[u8; 22] = b"ERR_NOT_ENOUGH_BALANCE"; +pub const ERR_TOTAL_SUPPLY_UNDERFLOW: &[u8; 26] = b"ERR_TOTAL_SUPPLY_UNDERFLOW"; +pub const ERR_ZERO_AMOUNT: &[u8; 15] = b"ERR_ZERO_AMOUNT"; +pub const ERR_SENDER_EQUALS_RECEIVER: &[u8; 26] = b"ERR_SENDER_EQUALS_RECEIVER"; +pub const ERR_ACCOUNT_NOT_REGISTERED: &[u8; 26] = b"ERR_ACCOUNT_NOT_REGISTERED"; +pub const ERR_NO_AVAILABLE_BALANCE: &[u8; 24] = b"ERR_NO_AVAILABLE_BALANCE"; +pub const ERR_ATTACHED_DEPOSIT_NOT_ENOUGH: &[u8; 31] = b"ERR_ATTACHED_DEPOSIT_NOT_ENOUGH"; +pub const ERR_FAILED_UNREGISTER_ACCOUNT_POSITIVE_BALANCE: &[u8; 46] = + b"ERR_FAILED_UNREGISTER_ACCOUNT_POSITIVE_BALANCE"; + +pub const ERR_ACCOUNTS_COUNTER_OVERFLOW: &str = "ERR_ACCOUNTS_COUNTER_OVERFLOW"; + +pub const ERR_REVERT: &[u8; 10] = b"ERR_REVERT"; +pub const ERR_OUT_OF_FUNDS: &[u8; 16] = b"ERR_OUT_OF_FUNDS"; diff --git a/engine/src/fungible_token.rs b/engine/src/fungible_token.rs index 5f3326b31..fb17601d8 100644 --- a/engine/src/fungible_token.rs +++ b/engine/src/fungible_token.rs @@ -541,7 +541,7 @@ impl FungibleTokenOps { .read_u64(&key) .unwrap_or(0) .checked_add(1) - .expect("ERR_ACCOUNTS_COUNTER_OVERFLOW"); + .expect(crate::errors::ERR_ACCOUNTS_COUNTER_OVERFLOW); self.io.write_storage(&key, &accounts_counter.to_le_bytes()); } self.io @@ -589,14 +589,15 @@ impl FungibleTokenOps { } pub mod error { + use crate::errors; use crate::prelude::types::balance::error::BalanceOverflowError; - const TOTAL_SUPPLY_OVERFLOW: &[u8; 25] = b"ERR_TOTAL_SUPPLY_OVERFLOW"; - const BALANCE_OVERFLOW: &[u8; 20] = b"ERR_BALANCE_OVERFLOW"; - const NOT_ENOUGH_BALANCE: &[u8; 22] = b"ERR_NOT_ENOUGH_BALANCE"; - const TOTAL_SUPPLY_UNDERFLOW: &[u8; 26] = b"ERR_TOTAL_SUPPLY_UNDERFLOW"; - const ZERO_AMOUNT: &[u8; 15] = b"ERR_ZERO_AMOUNT"; - const SELF_TRANSFER: &[u8; 26] = b"ERR_SENDER_EQUALS_RECEIVER"; + const TOTAL_SUPPLY_OVERFLOW: &[u8; 25] = errors::ERR_TOTAL_SUPPLY_OVERFLOW; + const BALANCE_OVERFLOW: &[u8; 20] = errors::ERR_BALANCE_OVERFLOW; + const NOT_ENOUGH_BALANCE: &[u8; 22] = errors::ERR_NOT_ENOUGH_BALANCE; + const TOTAL_SUPPLY_UNDERFLOW: &[u8; 26] = errors::ERR_TOTAL_SUPPLY_UNDERFLOW; + const ZERO_AMOUNT: &[u8; 15] = errors::ERR_ZERO_AMOUNT; + const SELF_TRANSFER: &[u8; 26] = errors::ERR_SENDER_EQUALS_RECEIVER; #[derive(Debug)] pub enum DepositError { @@ -683,11 +684,11 @@ pub mod error { impl AsRef<[u8]> for StorageFundingError { fn as_ref(&self) -> &[u8] { match self { - Self::NotRegistered => b"ERR_ACCOUNT_NOT_REGISTERED", - Self::NoAvailableBalance => b"ERR_NO_AVAILABLE_BALANCE", - Self::InsufficientDeposit => b"ERR_ATTACHED_DEPOSIT_NOT_ENOUGH", + Self::NotRegistered => errors::ERR_ACCOUNT_NOT_REGISTERED, + Self::NoAvailableBalance => errors::ERR_NO_AVAILABLE_BALANCE, + Self::InsufficientDeposit => errors::ERR_ATTACHED_DEPOSIT_NOT_ENOUGH, Self::UnRegisterPositiveBalance => { - b"ERR_FAILED_UNREGISTER_ACCOUNT_POSITIVE_BALANCE" + errors::ERR_FAILED_UNREGISTER_ACCOUNT_POSITIVE_BALANCE } } } diff --git a/engine/src/json.rs b/engine/src/json.rs index 7883e8239..7e5db2573 100644 --- a/engine/src/json.rs +++ b/engine/src/json.rs @@ -1,5 +1,6 @@ use crate::prelude::{BTreeMap, String, Vec}; +use crate::errors; use core::convert::From; use rjson::{Array, Null, Object, Value}; @@ -103,15 +104,15 @@ impl JsonValue { impl AsRef<[u8]> for JsonError { fn as_ref(&self) -> &[u8] { match self { - Self::NotJsonType => b"ERR_NOT_A_JSON_TYPE", - Self::MissingValue => b"ERR_JSON_MISSING_VALUE", - Self::InvalidU8 => b"ERR_FAILED_PARSE_U8", - Self::InvalidU64 => b"ERR_FAILED_PARSE_U64", - Self::InvalidU128 => b"ERR_FAILED_PARSE_U128", - Self::InvalidBool => b"ERR_FAILED_PARSE_BOOL", - Self::InvalidString => b"ERR_FAILED_PARSE_STRING", - Self::InvalidArray => b"ERR_FAILED_PARSE_ARRAY", - Self::ExpectedStringGotNumber => b"ERR_EXPECTED_STRING_GOT_NUMBER", + Self::NotJsonType => errors::ERR_NOT_A_JSON_TYPE, + Self::MissingValue => errors::ERR_JSON_MISSING_VALUE, + Self::InvalidU8 => errors::ERR_FAILED_PARSE_U8, + Self::InvalidU64 => errors::ERR_FAILED_PARSE_U64, + Self::InvalidU128 => errors::ERR_FAILED_PARSE_U128, + Self::InvalidBool => errors::ERR_FAILED_PARSE_BOOL, + Self::InvalidString => errors::ERR_FAILED_PARSE_STRING, + Self::InvalidArray => errors::ERR_FAILED_PARSE_ARRAY, + Self::ExpectedStringGotNumber => errors::ERR_EXPECTED_STRING_GOT_NUMBER, Self::OutOfRange(err) => err.as_ref(), } } @@ -120,8 +121,8 @@ impl AsRef<[u8]> for JsonError { impl AsRef<[u8]> for JsonOutOfRangeError { fn as_ref(&self) -> &[u8] { match self { - Self::OutOfRangeU8 => b"ERR_OUT_OF_RANGE_U8", - Self::OutOfRangeU128 => b"ERR_OUT_OF_RANGE_U128", + Self::OutOfRangeU8 => errors::ERR_OUT_OF_RANGE_U8, + Self::OutOfRangeU128 => errors::ERR_OUT_OF_RANGE_U128, } } } diff --git a/engine/src/lib.rs b/engine/src/lib.rs index 735fd0074..652c118a7 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -24,6 +24,7 @@ pub mod admin_controlled; pub mod connector; pub mod deposit_event; pub mod engine; +pub mod errors; pub mod fungible_token; pub mod json; pub mod log_entry; @@ -70,6 +71,7 @@ mod contract { use crate::connector::{self, EthConnectorContract}; use crate::engine::{self, Engine, EngineState}; + use crate::errors; use crate::fungible_token::FungibleTokenMetadata; use crate::json::parse_json; use crate::parameters::{ @@ -99,7 +101,6 @@ mod contract { const CODE_KEY: &[u8; 4] = b"CODE"; const CODE_STAGE_KEY: &[u8; 10] = b"CODE_STAGE"; - const PROMISE_COUNT_ERR: &str = "ERR_PROMISE_COUNT"; /// /// ADMINISTRATIVE METHODS @@ -182,7 +183,7 @@ mod contract { require_owner_only(&state, &io.predecessor_account_id()); let index = internal_get_upgrade_index(); if io.block_height() <= index + state.upgrade_delay_blocks { - sdk::panic_utf8(b"ERR_NOT_ALLOWED:TOO_EARLY"); + sdk::panic_utf8(errors::ERR_NOT_ALLOWED_TOO_EARLY); } Runtime::self_deploy(&bytes_to_key(KeyPrefix::Config, CODE_KEY)); } @@ -213,7 +214,7 @@ mod contract { ) .sdk_unwrap(); Engine::deploy_code_with_input(&mut engine, input, &mut Runtime) - .map(|res| res.try_to_vec().sdk_expect("ERR_SERIALIZE")) + .map(|res| res.try_to_vec().sdk_expect(errors::ERR_SERIALIZE)) .sdk_process(); // TODO: charge for storage } @@ -223,7 +224,7 @@ mod contract { pub extern "C" fn call() { let io = Runtime; let bytes = io.read_input().to_vec(); - let args = CallArgs::deserialize(&bytes).sdk_expect("ERR_BORSH_DESERIALIZE"); + let args = CallArgs::deserialize(&bytes).sdk_expect(errors::ERR_BORSH_DESERIALIZE); let current_account_id = io.current_account_id(); let mut engine = Engine::new( predecessor_address(&io.predecessor_account_id()), @@ -233,7 +234,7 @@ mod contract { ) .sdk_unwrap(); Engine::call_with_args(&mut engine, args, &mut Runtime) - .map(|res| res.try_to_vec().sdk_expect("ERR_SERIALIZE")) + .map(|res| res.try_to_vec().sdk_expect(errors::ERR_SERIALIZE)) .sdk_process(); // TODO: charge for storage } @@ -258,7 +259,7 @@ mod contract { ); result - .map(|res| res.try_to_vec().sdk_expect("ERR_SERIALIZE")) + .map(|res| res.try_to_vec().sdk_expect(errors::ERR_SERIALIZE)) .sdk_process(); } @@ -275,7 +276,7 @@ mod contract { io.current_account_id().as_bytes(), input, ) - .sdk_expect("ERR_META_TX_PARSE"); + .sdk_expect(errors::ERR_META_TX_PARSE); engine::check_nonce(&io, &meta_call_args.sender, &meta_call_args.nonce).sdk_unwrap(); @@ -292,7 +293,7 @@ mod contract { &mut Runtime, ); result - .map(|res| res.try_to_vec().sdk_expect("ERR_SERIALIZE")) + .map(|res| res.try_to_vec().sdk_expect(errors::ERR_SERIALIZE)) .sdk_process(); } @@ -365,7 +366,12 @@ mod contract { let address = engine::deploy_erc20_token(args, io, &io, &mut Runtime).sdk_unwrap(); - io.return_output(&address.as_bytes().try_to_vec().sdk_expect("ERR_SERIALIZE")); + io.return_output( + &address + .as_bytes() + .try_to_vec() + .sdk_expect(errors::ERR_SERIALIZE), + ); // TODO: charge for storage } @@ -380,7 +386,7 @@ mod contract { // This function should only be called as the callback of // exactly one promise. if io.promise_results_count() != 1 { - sdk::panic_utf8(PROMISE_COUNT_ERR.as_bytes()); + sdk::panic_utf8(errors::ERR_PROMISE_COUNT); } if let Some(PromiseResult::Successful(_)) = io.promise_result(0) { @@ -393,7 +399,7 @@ mod contract { engine::refund_on_error(io, &io, state, args, &mut Runtime).sdk_unwrap(); if !refund_result.status.is_ok() { - sdk::panic_utf8(b"ERR_REFUND_FAILURE"); + sdk::panic_utf8(errors::ERR_REFUND_FAILURE); } } } @@ -409,7 +415,7 @@ mod contract { let current_account_id = io.current_account_id(); let engine = Engine::new(args.sender, current_account_id, io, &env).sdk_unwrap(); let result = Engine::view_with_args(&engine, args).sdk_unwrap(); - io.return_output(&result.try_to_vec().sdk_expect("ERR_SERIALIZE")); + io.return_output(&result.try_to_vec().sdk_expect(errors::ERR_SERIALIZE)); } #[no_mangle] @@ -527,7 +533,7 @@ mod contract { .sdk_unwrap() .withdraw_eth_from_near(¤t_account_id, &predecessor_account_id, args) .sdk_unwrap(); - let result_bytes = result.try_to_vec().sdk_expect("ERR_SERIALIZE"); + let result_bytes = result.try_to_vec().sdk_expect(errors::ERR_SERIALIZE); // We intentionally do not go through the `io` struct here because we must bypass // the check that prevents output that is accepted by the eth_custodian unsafe { @@ -556,16 +562,16 @@ mod contract { // Check result from proof verification call if io.promise_results_count() != 1 { - sdk::panic_utf8(PROMISE_COUNT_ERR.as_bytes()); + sdk::panic_utf8(errors::ERR_PROMISE_COUNT); } let promise_result = match io.promise_result(0) { Some(PromiseResult::Successful(bytes)) => { - bool::try_from_slice(&bytes).sdk_expect("ERR_PROMISE_ENCODING") + bool::try_from_slice(&bytes).sdk_expect(errors::ERR_PROMISE_ENCODING) } - _ => sdk::panic_utf8(b"ERR_PROMISE_FAILED"), + _ => sdk::panic_utf8(errors::ERR_PROMISE_FAILED), }; if !promise_result { - sdk::panic_utf8(b"ERR_VERIFY_PROOF"); + sdk::panic_utf8(errors::ERR_VERIFY_PROOF); } let data = io.read_input_borsh().sdk_unwrap(); @@ -666,7 +672,7 @@ mod contract { io.assert_private_call().sdk_unwrap(); if io.promise_results_count() != 1 { - sdk::panic_utf8(PROMISE_COUNT_ERR.as_bytes()); + sdk::panic_utf8(errors::ERR_PROMISE_COUNT); } let args: ResolveTransferCallArgs = io.read_input().to_value().sdk_unwrap(); @@ -840,7 +846,7 @@ mod contract { const GAS_FOR_FINISH: NearGas = NearGas::new(50_000_000_000_000); let mut io = Runtime; - let args: ([u8; 20], u64, u64) = io.read_input_borsh().sdk_expect("ERR_ARGS"); + let args: ([u8; 20], u64, u64) = io.read_input_borsh().sdk_expect(errors::ERR_ARGS); let address = Address::from_array(args.0); let nonce = U256::from(args.1); let balance = NEP141Wei::new(args.2 as u128); @@ -899,14 +905,16 @@ mod contract { let io = Runtime; match io.read_u64(&bytes_to_key(KeyPrefix::Config, CODE_STAGE_KEY)) { Ok(index) => index, - Err(sdk::error::ReadU64Error::InvalidU64) => sdk::panic_utf8(b"ERR_INVALID_UPGRADE"), - Err(sdk::error::ReadU64Error::MissingValue) => sdk::panic_utf8(b"ERR_NO_UPGRADE"), + Err(sdk::error::ReadU64Error::InvalidU64) => { + sdk::panic_utf8(errors::ERR_INVALID_UPGRADE) + } + Err(sdk::error::ReadU64Error::MissingValue) => sdk::panic_utf8(errors::ERR_NO_UPGRADE), } } fn require_owner_only(state: &EngineState, predecessor_account_id: &AccountId) { if &state.owner_id != predecessor_account_id { - sdk::panic_utf8(b"ERR_NOT_ALLOWED"); + sdk::panic_utf8(errors::ERR_NOT_ALLOWED); } } diff --git a/engine/src/parameters.rs b/engine/src/parameters.rs index 9f3088af3..d26a99ea3 100644 --- a/engine/src/parameters.rs +++ b/engine/src/parameters.rs @@ -1,4 +1,5 @@ use crate::admin_controlled::PausedMask; +use crate::errors; use crate::fungible_token::FungibleTokenMetadata; use crate::json::{JsonError, JsonValue}; use crate::prelude::account_id::AccountId; @@ -98,11 +99,11 @@ impl AsRef<[u8]> for TransactionStatus { fn as_ref(&self) -> &[u8] { match self { Self::Succeed(_) => b"SUCCESS", - Self::Revert(_) => b"ERR_REVERT", - Self::OutOfFund => b"ERR_OUT_OF_FUNDS", - Self::OutOfGas => b"ERR_OUT_OF_GAS", - Self::OutOfOffset => b"ERR_OUT_OF_OFFSET", - Self::CallTooDeep => b"ERR_CALL_TOO_DEEP", + Self::Revert(_) => errors::ERR_REVERT, + Self::OutOfFund => errors::ERR_OUT_OF_FUNDS, + Self::OutOfGas => errors::ERR_OUT_OF_GAS, + Self::OutOfOffset => errors::ERR_OUT_OF_OFFSET, + Self::CallTooDeep => errors::ERR_CALL_TOO_DEEP, } } }