Skip to content
Merged
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
46 changes: 34 additions & 12 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ scale-info = { version = "2.11.3", default-features = false, features = [
tracing = { version = "0.1.40", default-features = false }
serde = { version = "1.0.215", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.110", default-features = false }
thiserror = { version = "2", default-features = false }

# std
clap = { version = "4.5.4", features = ["derive"] }
Expand Down
4 changes: 3 additions & 1 deletion pvq-executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ polkavm = { workspace = true }

pvq-primitives = { workspace = true }

thiserror = { workspace = true }

[features]
default = ["std"]
std = ["tracing/std", "polkavm/std", "pvq-primitives/std"]
std = ["tracing/std", "polkavm/std", "pvq-primitives/std", "thiserror/std"]
47 changes: 34 additions & 13 deletions pvq-executor/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
use pvq_primitives::PvqError;
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum PvqExecutorError<UserError> {
#[error("Invalid PVQ program format")]
InvalidProgramFormat,
#[error("Memory access error: {0}")]
MemoryAccessError(polkavm::MemoryAccessError),
// Extract from the PVM CallError
#[error("Trap")]
Trap,
// Extract from the PVM CallError
#[error("Not enough gas")]
NotEnoughGas,
// Usually a custom error type from the extension system definition
#[error("User error: {0}")]
User(UserError),
// Other errors directly from the PVM
#[error("Other PVM error: {0}")]
OtherPvmError(polkavm::Error),
}

impl<UserError> From<polkavm::MemoryAccessError> for PvqExecutorError<UserError> {
fn from(err: polkavm::MemoryAccessError) -> Self {
Self::MemoryAccessError(err)
}
}

impl<UserError> From<polkavm::Error> for PvqExecutorError<UserError> {
fn from(err: polkavm::Error) -> Self {
Self::OtherPvmError(err)
}
}

impl<UserError> From<polkavm::CallError<UserError>> for PvqExecutorError<UserError> {
fn from(err: polkavm::CallError<UserError>) -> Self {
match err {
Expand All @@ -36,6 +30,33 @@ impl<UserError> From<polkavm::CallError<UserError>> for PvqExecutorError<UserErr
}
}

impl<UserError> From<polkavm::Error> for PvqExecutorError<UserError> {
fn from(e: polkavm::Error) -> Self {
Self::OtherPvmError(e)
}
}

impl<UserError> From<polkavm::MemoryAccessError> for PvqExecutorError<UserError> {
fn from(e: polkavm::MemoryAccessError) -> Self {
Self::MemoryAccessError(e)
}
}

#[cfg(feature = "std")]
impl<UserError: core::fmt::Debug> From<PvqExecutorError<UserError>> for PvqError {
fn from(e: PvqExecutorError<UserError>) -> PvqError {
match e {
PvqExecutorError::InvalidProgramFormat => "Invalid PVQ program format".to_string(),
PvqExecutorError::MemoryAccessError(_) => "Memory access error".to_string(),
PvqExecutorError::Trap => "Trap".to_string(),
PvqExecutorError::NotEnoughGas => "Not enough gas".to_string(),
PvqExecutorError::User(user_error) => format!("Host call error: {user_error:?}"),
PvqExecutorError::OtherPvmError(pvm_error) => format!("Other error: {pvm_error:?}"),
}
}
}

#[cfg(not(feature = "std"))]
impl<UserError> From<PvqExecutorError<UserError>> for PvqError {
fn from(e: PvqExecutorError<UserError>) -> PvqError {
match e {
Expand Down
2 changes: 2 additions & 0 deletions pvq-extension/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
pvq-primitives = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
tracing-subscriber = { workspace = true }
Expand All @@ -31,4 +32,5 @@ std = [
"scale-info/std",
"tracing/std",
"serde/std",
"thiserror/std",
]
3 changes: 2 additions & 1 deletion pvq-extension/src/calldata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ pub trait Dispatchable {
}

/// Error type for dispatch operations
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum DispatchError {
#[error("PhantomData")]
PhantomData,
}

Expand Down
34 changes: 8 additions & 26 deletions pvq-extension/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,36 @@
// TODO: contain source error
use crate::DispatchError;
use parity_scale_codec::Error as CodecError;
use scale_info::prelude::fmt;
use scale_info::prelude::fmt::{Display, Formatter};

/// Errors that can occur when working with extensions
// Typically will be used as a UserError
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum ExtensionError {
/// Permission denied for the requested operation
#[error("Permission denied")]
PermissionError,

/// Failed to allocate memory
#[error("Failed to allocate memory")]
MemoryAllocationError,

/// Error accessing memory
#[error("Memory access error: {0}")]
MemoryAccessError(polkavm::MemoryAccessError),

/// Error decoding data
#[error("Decode error: {0}")]
DecodeError(CodecError),

/// Error dispatching a call
DispatchError(DispatchError),
#[error("Dispatch error: {0:?}")]
DispatchError(#[from] DispatchError),

/// The requested extension is not supported
#[error("Unsupported extension")]
UnsupportedExtension,
}

impl Display for ExtensionError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::PermissionError => write!(f, "Permission denied"),
Self::MemoryAllocationError => write!(f, "Failed to allocate memory"),
Self::MemoryAccessError(e) => write!(f, "Memory access error: {:?}", e),
Self::DecodeError(e) => write!(f, "Decode error: {:?}", e),
Self::DispatchError(e) => write!(f, "Dispatch error: {:?}", e),
Self::UnsupportedExtension => write!(f, "Unsupported extension"),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for ExtensionError {}

impl From<polkavm::MemoryAccessError> for ExtensionError {
fn from(e: polkavm::MemoryAccessError) -> Self {
Self::MemoryAccessError(e)
Expand All @@ -54,9 +42,3 @@ impl From<CodecError> for ExtensionError {
Self::DecodeError(e)
}
}

impl From<DispatchError> for ExtensionError {
fn from(e: DispatchError) -> Self {
Self::DispatchError(e)
}
}
6 changes: 6 additions & 0 deletions pvq-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
extern crate alloc;

use alloc::vec::Vec;
#[cfg(not(feature = "std"))]
use parity_scale_codec::{Decode, Encode};
#[cfg(not(feature = "std"))]
use scale_info::TypeInfo;

pub type PvqResult = Result<PvqResponse, PvqError>;

pub type PvqResponse = Vec<u8>;

#[cfg(feature = "std")]
pub type PvqError = String;

#[cfg(not(feature = "std"))]
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, TypeInfo)]
pub enum PvqError {
FailedToDecode,
Expand Down
Loading