Skip to content

Commit

Permalink
Replace 'UnknownError' with concrete errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunxw committed Jul 26, 2023
1 parent e576f74 commit 401cf8c
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 38 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions chain-extensions/types/xvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ homepage.workspace = true
repository.workspace = true

[dependencies]
astar-primitives = { workspace = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
sp-runtime = { workspace = true }
Expand All @@ -21,4 +22,5 @@ std = [
"scale-info/std",
"sp-runtime/std",
"sp-std/std",
"astar-primitives/std",
]
39 changes: 23 additions & 16 deletions chain-extensions/types/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,41 @@

#![cfg_attr(not(feature = "std"), no_std)]

use astar_primitives::xvm::CallError;
use parity_scale_codec::{Decode, Encode};
use sp_runtime::{DispatchError, ModuleError};
use sp_std::vec::Vec;

#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, Debug)]
pub enum XvmExecutionResult {
/// Success
Success = 0,
// TODO: expand this with concrete XVM errors
/// Error not (yet) covered by a dedidacted code
UnknownError = 255,
Ok,
/// Failure
Err(u32),
}

impl TryFrom<DispatchError> for XvmExecutionResult {
type Error = DispatchError;
impl From<CallError> for XvmExecutionResult {
fn from(input: CallError) -> Self {
use CallError::*;

fn try_from(input: DispatchError) -> Result<Self, Self::Error> {
let _error_text = match input {
DispatchError::Module(ModuleError { message, .. }) => message,
_ => Some("No module error Info"),
// `0` is reserved for `Ok`
let error_code = match input {
InvalidVmId => 1,
SameVmCallNotAllowed => 2,
InvalidTarget => 3,
InputTooLarge => 4,
ExecutionFailed(_) => 5,
};
Self::Err(error_code)
}
}

// TODO: expand this with concrete XVM errors (see dapps-staking types for example)
Ok(XvmExecutionResult::UnknownError)
impl From<XvmExecutionResult> for u32 {
fn from(input: XvmExecutionResult) -> Self {
match input {
XvmExecutionResult::Ok => 0,
XvmExecutionResult::Err(code) => code,
}
}
}

Expand All @@ -55,6 +65,3 @@ pub struct XvmCallArgs {
/// Encoded call params
pub input: Vec<u8>,
}

pub const FRONTIER_VM_ID: u8 = 0x0F;
pub const PARITY_WASM_VM_ID: u8 = 0x1F;
23 changes: 12 additions & 11 deletions chain-extensions/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ where
};

let vm_id = {
let result = vm_id.try_into();
match result {
match TryInto::<VmId>::try_into(vm_id) {
Ok(id) => id,
Err(_) => {
Err(err) => {
// TODO: Propagate error
return Ok(RetVal::Converging(XvmExecutionResult::UnknownError as u32));
let result = Into::<XvmExecutionResult>::into(err);
return Ok(RetVal::Converging(result.into()));
}
}
};
Expand All @@ -103,25 +103,26 @@ where
env.adjust_weight(charged_weight, actual_weight);

match call_result {
Ok(success) => {
Ok(info) => {
log::trace!(
target: "xvm-extension::xvm_call",
"success: {:?}", success
"info: {:?}", info
);

let buffer: sp_std::vec::Vec<_> = success.output.encode();
let buffer: sp_std::vec::Vec<_> = info.output.encode();
env.write(&buffer, false, None)?;
Ok(RetVal::Converging(XvmExecutionResult::Success as u32))
Ok(RetVal::Converging(XvmExecutionResult::Ok.into()))
}

Err(failure) => {
Err(err) => {
log::trace!(
target: "xvm-extension::xvm_call",
"failure: {:?}", failure
"err: {:?}", err
);

// TODO Propagate error
Ok(RetVal::Converging(XvmExecutionResult::UnknownError as u32))
let result = Into::<XvmExecutionResult>::into(err.error);
Ok(RetVal::Converging(result.into()))
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions pallets/xvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ std = [
"pallet-contracts/std",
"pallet-evm/std",
"scale-info/std",
"serde/std",
"serde",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
"astar-primitives/std",
"pallet-ethereum-checked/std",
"frame-benchmarking?/std",
]

runtime-benchmarks = [
Expand Down
6 changes: 3 additions & 3 deletions precompiles/xvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ precompile-utils = { workspace = true, features = ["testing"] }

pallet-balances = { workspace = true }
pallet-contracts = { workspace = true }
pallet-timestamp = { workspace = true }
pallet-ethereum = { workspace = true }
pallet-ethereum-checked = { workspace = true }
pallet-insecure-randomness-collective-flip = { workspace = true }
pallet-timestamp = { workspace = true }
sp-runtime = { workspace = true }

[features]
Expand All @@ -60,6 +60,6 @@ std = [
"sp-runtime/std",
"pallet-xvm/std",
"astar-primitives/std",
"pallet-balances/std",
"pallet-ethereum/std",
"pallet-balances/std",
"pallet-ethereum/std",
]
12 changes: 6 additions & 6 deletions primitives/src/xvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ pub struct CallInfo {
pub enum CallError {
/// Invalid VM id.
InvalidVmId,
/// The call failed on EVM or WASM execution.
ExecutionFailed(Vec<u8>),
/// Input is too large.
InputTooLarge,
/// Target contract address is invalid.
InvalidTarget,
/// Calling the contracts in the same VM is not allowed.
SameVmCallNotAllowed,
/// Target contract address is invalid.
InvalidTarget,
/// Input is too large.
InputTooLarge,
/// The call failed on EVM or WASM execution.
ExecutionFailed(Vec<u8>),
}

/// XVM call error with used weight info.
Expand Down

0 comments on commit 401cf8c

Please sign in to comment.