Skip to main content

phos_data_network_precompiles/
error.rs

1//! Error handling for DATA Network precompiles.
2
3use alloy_primitives::Bytes;
4use revm::precompile::{PrecompileError, PrecompileOutput, PrecompileResult};
5
6/// Errors produced while executing a DATA Network precompile.
7#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
8pub enum DataNetworkPrecompileError {
9    /// Gas limit exceeded during precompile execution.
10    #[error("gas limit exceeded")]
11    OutOfGas,
12
13    /// The caller is not authorized to perform the requested operation.
14    #[error("{0}")]
15    Unauthorized(&'static str),
16
17    /// The call is validly formed but cannot be executed with the supplied values.
18    #[error("{0}")]
19    Revert(&'static str),
20
21    /// An unrecoverable internal error occurred.
22    #[error("fatal precompile error: {0}")]
23    Fatal(String),
24}
25
26/// Result type used by DATA Network precompile operations.
27pub type Result<T> = std::result::Result<T, DataNetworkPrecompileError>;
28
29impl From<DataNetworkPrecompileError> for PrecompileError {
30    fn from(value: DataNetworkPrecompileError) -> Self {
31        match value {
32            DataNetworkPrecompileError::OutOfGas => Self::OutOfGas,
33            DataNetworkPrecompileError::Fatal(message) => Self::Fatal(message),
34            error @ (DataNetworkPrecompileError::Unauthorized(_)
35            | DataNetworkPrecompileError::Revert(_)) => Self::Other(error.to_string().into()),
36        }
37    }
38}
39
40/// Converts a DATA Network precompile result into REVM's precompile result.
41pub(crate) trait IntoPrecompileResult<T> {
42    fn into_precompile_result(self, encode_ok: impl FnOnce(T) -> Bytes) -> PrecompileResult;
43}
44
45impl<T> IntoPrecompileResult<T> for Result<T> {
46    fn into_precompile_result(self, encode_ok: impl FnOnce(T) -> Bytes) -> PrecompileResult {
47        match self {
48            Ok(value) => Ok(PrecompileOutput::new(0, encode_ok(value))),
49            Err(error) => Err(error.into()),
50        }
51    }
52}