From 6cf56a6ac01b159b98ccf873d0546c55cc20d9e1 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Fri, 4 Oct 2024 19:59:12 +0700 Subject: [PATCH] refactor: pop_primitive dispatch error conversion --- Cargo.lock | 2 ++ Cargo.toml | 15 ++++++++------- crates/pop-drink/Cargo.toml | 1 + crates/pop-drink/src/lib.rs | 38 +++++++++++++++++++++++++++---------- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 74e6953..4fc49b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4670,6 +4670,7 @@ dependencies = [ "pallet-contracts", "parity-scale-codec", "pop-api", + "pop-primitives", "pop-runtime-devnet", "sp-io", ] @@ -4680,6 +4681,7 @@ version = "0.0.0" dependencies = [ "parity-scale-codec", "scale-info", + "sp-runtime", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index af99044..600b4d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,11 @@ [workspace] resolver = "2" members = [ - "crates/ink-sandbox", - "crates/drink/drink", - "crates/drink/drink-cli", - "crates/drink/drink/test-macro", - "crates/pop-drink", + "crates/ink-sandbox", + "crates/drink/drink", + "crates/drink/drink-cli", + "crates/drink/drink/test-macro", + "crates/pop-drink", ] exclude = ["crates/drink/examples"] @@ -30,7 +30,7 @@ proc-macro2 = { version = "1" } quote = { version = "1" } ratatui = { version = "0.21.0" } scale = { package = "parity-scale-codec", version = "3.6.9", features = [ - "derive", + "derive", ] } scale-info = { version = "2.10.0" } serde_json = { version = "1.0" } @@ -55,6 +55,7 @@ sp-runtime-interface = { version = "28.0.0", features = ["std"] } drink = { path = "crates/drink/drink" } ink_sandbox = { path = "crates/ink-sandbox" } pallet-api = { path = "../pop-node/pallets/api" } +pop-api = { path = "../pop-node/pop-api" } pop-drink = { path = "crates/pop-drink" } +pop-primitives = { path = "../pop-node/primitives" } pop-runtime-devnet = { path = "../pop-node/runtime/devnet" } -pop-api = { path = "../pop-node/pop-api" } diff --git a/crates/pop-drink/Cargo.toml b/crates/pop-drink/Cargo.toml index b826b85..87b728f 100644 --- a/crates/pop-drink/Cargo.toml +++ b/crates/pop-drink/Cargo.toml @@ -14,3 +14,4 @@ frame-support.workspace = true sp-io.workspace = true scale.workspace = true pop-api.workspace = true +pop-primitives.workspace = true diff --git a/crates/pop-drink/src/lib.rs b/crates/pop-drink/src/lib.rs index ae7b0f5..a0feccb 100644 --- a/crates/pop-drink/src/lib.rs +++ b/crates/pop-drink/src/lib.rs @@ -1,20 +1,19 @@ pub use drink::*; -pub use frame_support::{self, assert_ok, sp_runtime::DispatchError, traits::PalletInfoAccess}; +pub use frame_support::{self, assert_ok}; pub use ink_sandbox::api::assets_api::AssetsAPI; use ink_sandbox::{AccountIdFor, BalanceFor}; -use scale::Decode; +use scale::{Decode, Encode}; pub use session::{error::SessionError, ContractBundle, Session, NO_SALT}; pub use sp_io::TestExternalities; +pub const DECODING_FAILED_ERROR: [u8; 4] = [11, 0, 0, 0]; + pub mod devnet { use super::*; pub use frame_support::sp_runtime::{ ArithmeticError, DispatchError, ModuleError, TokenError, TransactionalError, }; - pub use pop_runtime_devnet::{ - config::api::versioning::V0Error, Assets, Balances, BuildStorage, Contracts, Runtime, - }; - use scale::Encode; + pub use pop_runtime_devnet::{Assets, Balances, BuildStorage, Contracts, Runtime}; // Types used in the pop runtime. pub type Balance = BalanceFor; @@ -36,15 +35,34 @@ pub mod devnet { Contracts(ContractsError), } - impl Into for RuntimeError { - fn into(self) -> u32 { - let dispatch_error = match self { + impl From for u32 { + fn from(value: RuntimeError) -> Self { + use pop_primitives::Error; + let dispatch_error = match value { RuntimeError::Raw(dispatch_error) => dispatch_error, RuntimeError::Assets(error) => error.into(), RuntimeError::Balances(error) => error.into(), RuntimeError::Contracts(error) => error.into(), }; - V0Error::from(dispatch_error).into() + let primitive_error = match dispatch_error { + DispatchError::Module(error) => { + // Note: message not used + let ModuleError { index, error, message: _message } = error; + // Map `pallet-contracts::Error::DecodingFailed` to `Error::DecodingFailed` + if index as usize + == ::index() + && error == DECODING_FAILED_ERROR + { + Error::DecodingFailed + } else { + // Note: lossy conversion of error value due to returned contract status code + // size limitation + Error::Module { index, error: [error[0], error[1]] } + } + }, + _ => dispatch_error.into(), + }; + Error::from(primitive_error).into() } }