Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: error #192

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 12 additions & 16 deletions extension/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ targets = ["x86_64-unknown-linux-gnu"]
codec.workspace = true
log.workspace = true

# Local
pop-primitives.workspace = true

# Substrate
frame-support.workspace = true
frame-system.workspace = true
Expand All @@ -33,19 +30,18 @@ rand = "0.8.5"
[features]
default = ["std"]
std = [
"log/std",
"codec/std",
"frame-support/std",
"frame-system/std",
"pallet-contracts/std",
"pop-primitives/std",
"sp-runtime/std",
"sp-core/std",
"sp-std/std",
"log/std",
"codec/std",
"frame-support/std",
"frame-system/std",
"pallet-contracts/std",
"sp-runtime/std",
"sp-core/std",
"sp-std/std",
]
runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-contracts/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-contracts/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
4 changes: 1 addition & 3 deletions extension/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,7 @@ impl TryFrom<u8> for FuncId {
}

/// Converts a `DispatchError` to a `u32` status code based on the version of the API the contract uses.
/// The contract calling the chain extension can optionally convert the status code to the descriptive `Error`.
///
/// For `Error` see `pop_primitives::<version>::error::Error`.
/// The contract calling the chain extension can optionally convert the status code to the descriptive `pop_api::Error`.
///
/// The error encoding can vary per version, allowing for flexible and backward-compatible error handling.
/// As a result, contracts maintain compatibility across different versions of the runtime.
Expand Down
2 changes: 2 additions & 0 deletions pop-api/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ frame-system = { version = "29.0.0", default-features = false }
pallet-balances = { version = "29.0.2", default-features = false }
pallet-assets = { version = "30.0.0", default-features = false }
pallet-contracts = { version = "28.0.0", default-features = false }
pop-api = { path = "../../pop-api", default-features = false }
pop-primitives = { path = "../../primitives", default-features = false }
pop-runtime-devnet = { path = "../../runtime/devnet", default-features = false }
sp-io = { version = "31.0.0", default-features = false }
Expand All @@ -25,6 +26,7 @@ std = [
"pallet-balances/std",
"pallet-assets/std",
"pallet-contracts/std",
"pop-api/std",
"pop-primitives/std",
"pop-runtime-devnet/std",
"scale/std",
Expand Down
2 changes: 1 addition & 1 deletion pop-api/integration-tests/contracts/fungibles/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ mod fungibles {

#[ink::test]
fn default_works() {
PopApiFungiblesExample::new();
Fungibles::new();
}
}
}
2 changes: 1 addition & 1 deletion pop-api/integration-tests/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use pop_primitives::error::{
use pop_api::{
ArithmeticError::*,
Error::{self, *},
TokenError::*,
Expand Down
1 change: 1 addition & 0 deletions pop-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use constants::DECODING_FAILED;
use ink::env::chain_extension::{ChainExtensionMethod, FromStatusCode};
#[cfg(feature = "assets")]
pub use v0::assets;
pub use v0::{ArithmeticError, Error, TokenError, TransactionalError};

/// Module providing primitives types.
pub mod primitives;
Expand Down
12 changes: 5 additions & 7 deletions pop-api/src/v0/assets/fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use crate::{
primitives::{AccountId, AssetId, Balance},
Result, StatusCode,
};
pub use asset_management::*;
use constants::*;
use ink::{env::chain_extension::ChainExtensionMethod, prelude::vec::Vec};
pub use management::*;
pub use metadata::*;

// Helper method to build a dispatch call.
Expand Down Expand Up @@ -485,13 +485,11 @@ mod tests {
use super::FungiblesError;
use crate::{
constants::{ASSETS, BALANCES},
primitives::error::{
ArithmeticError::*,
Error::{self, *},
TokenError::*,
TransactionalError::*,
},
ArithmeticError::*,
Error::{self, *},
StatusCode,
TokenError::*,
TransactionalError::*,
};

fn error_into_status_code(error: Error) -> StatusCode {
Expand Down
122 changes: 120 additions & 2 deletions pop-api/src/v0/mod.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,141 @@
use crate::{
build_extension_method,
constants::{DISPATCH, READ_STATE},
primitives::error::Error,
StatusCode,
};
use ink::env::chain_extension::ChainExtensionMethod;
use ink::{env::chain_extension::ChainExtensionMethod, scale::Decode};

/// APIs for asset-related use cases.
#[cfg(feature = "assets")]
pub mod assets;

pub(crate) const V0: u8 = 0;

/// Reason why a Pop API call failed.
#[derive(Debug, Eq, PartialEq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[repr(u8)]
#[allow(clippy::unnecessary_cast)]
pub enum Error {
/// An unknown error occurred. This variant captures any unexpected errors that the
/// contract cannot specifically handle. It is useful for cases where there are breaking
/// changes in the runtime or when an error falls outside the predefined categories. The
/// variant includes:
Other {
/// The index within the `DispatchError`.
dispatch_error_index: u8,
/// The index within the `DispatchError` variant (e.g. a `TokenError`).
error_index: u8,
/// The specific error code or sub-index, providing additional context (e.g.
/// error` in `ModuleError`).
error: u8,
} = 0,
/// Failed to lookup some data.
CannotLookup = 1,
/// A bad origin.
BadOrigin = 2,
/// A custom error in a module.
///
Module {
/// The pallet index.
index: u8,
/// The error within the pallet.
error: u8,
} = 3,
/// At least one consumer is remaining so the account cannot be destroyed.
ConsumerRemaining = 4,
/// There are no providers so the account cannot be created.
NoProviders = 5,
/// There are too many consumers so the account cannot be created.
TooManyConsumers = 6,
/// An error to do with tokens.
Token(TokenError) = 7,
/// An arithmetic error.
Arithmetic(ArithmeticError) = 8,
/// The number of transactional layers has been reached, or we are not in a transactional
/// layer.
Transactional(TransactionalError) = 9,
/// Resources exhausted, e.g. attempt to read/write data which is too large to manipulate.
Exhausted = 10,
/// The state is corrupt; this is generally not going to fix itself.
Corruption = 11,
/// Some resource (e.g. a preimage) is unavailable right now. This might fix itself later.
Unavailable = 12,
/// Root origin is not allowed.
RootNotAllowed = 13,
/// Unknown call.
UnknownCall = 254,
/// Decoding failed.
DecodingFailed = 255,
}

impl From<StatusCode> for Error {
fn from(value: StatusCode) -> Self {
value.0.into()
}
}

impl From<u32> for Error {
/// Converts a `u32` status code into an `Error`.
///
/// This conversion maps a raw status code returned by the runtime into the more
/// descriptive `Error` enum variant, providing better context and understanding of the
/// error.
fn from(value: u32) -> Self {
let encoded = value.to_le_bytes();
Error::decode(&mut &encoded[..]).unwrap_or(Error::DecodingFailed)
}
}

/// Description of what went wrong when trying to complete an operation on a token.
#[derive(Debug, Eq, PartialEq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
pub enum TokenError {
/// Funds are unavailable.
FundsUnavailable,
/// Some part of the balance gives the only provider reference to the account and thus cannot
/// be (re)moved.
OnlyProvider,
/// Account cannot exist with the funds that would be given.
BelowMinimum,
/// Account cannot be created.
CannotCreate,
/// The asset in question is unknown.
UnknownAsset,
/// Funds exist but are frozen.
Frozen,
/// Operation is not supported by the asset.
Unsupported,
/// Account cannot be created for a held balance.
CannotCreateHold,
/// Withdrawal would cause unwanted loss of account.
NotExpendable,
/// Account cannot receive the assets.
Blocked,
}

/// Arithmetic errors.
#[derive(Debug, Eq, PartialEq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
pub enum ArithmeticError {
/// Underflow.
Underflow,
/// Overflow.
Overflow,
/// Division by zero.
DivisionByZero,
}

/// Errors related to transactional storage layers.
#[derive(Debug, Eq, PartialEq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
pub enum TransactionalError {
/// Too many transactional layers have been spawned.
LimitReached,
/// A transactional layer was expected, but does not exist.
NoLayer,
}

// Helper method to build a dispatch call.
//
// Parameters:
Expand Down
9 changes: 1 addition & 8 deletions primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ license = "GPL-3.0-only"
version = "0.0.0"
edition = "2021"

[dependencies]
codec.workspace = true
scale-info.workspace = true

[features]
default = ["std"]
std = [
"codec/std",
"scale-info/std",
]
std = []
Loading
Loading