revm_precompile/
interface.rsuse context_interface::result::EVMError;
use core::fmt;
use primitives::Bytes;
use std::string::{String, ToString};
pub type PrecompileResult = Result<PrecompileOutput, PrecompileErrors>;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct PrecompileOutput {
pub gas_used: u64,
pub bytes: Bytes,
}
impl PrecompileOutput {
pub fn new(gas_used: u64, bytes: Bytes) -> Self {
Self { gas_used, bytes }
}
}
pub type PrecompileFn = fn(&Bytes, u64) -> PrecompileResult;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum PrecompileErrors {
Error(PrecompileError),
Fatal { msg: String },
}
impl<DB, TXERROR> From<PrecompileErrors> for EVMError<DB, TXERROR> {
fn from(value: PrecompileErrors) -> Self {
Self::Precompile(value.to_string())
}
}
impl core::error::Error for PrecompileErrors {}
impl fmt::Display for PrecompileErrors {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Error(e) => e.fmt(f),
Self::Fatal { msg } => f.write_str(msg),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum PrecompileError {
OutOfGas,
Blake2WrongLength,
Blake2WrongFinalIndicatorFlag,
ModexpExpOverflow,
ModexpBaseOverflow,
ModexpModOverflow,
Bn128FieldPointNotAMember,
Bn128AffineGFailedToCreate,
Bn128PairLength,
BlobInvalidInputLength,
BlobMismatchedVersion,
BlobVerifyKzgProofFailed,
Other(String),
}
impl PrecompileError {
pub fn other(err: impl Into<String>) -> Self {
Self::Other(err.into())
}
pub fn is_oog(&self) -> bool {
matches!(self, Self::OutOfGas)
}
}
impl From<PrecompileError> for PrecompileErrors {
fn from(err: PrecompileError) -> Self {
PrecompileErrors::Error(err)
}
}
impl core::error::Error for PrecompileError {}
impl fmt::Display for PrecompileError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Self::OutOfGas => "out of gas",
Self::Blake2WrongLength => "wrong input length for blake2",
Self::Blake2WrongFinalIndicatorFlag => "wrong final indicator flag for blake2",
Self::ModexpExpOverflow => "modexp exp overflow",
Self::ModexpBaseOverflow => "modexp base overflow",
Self::ModexpModOverflow => "modexp mod overflow",
Self::Bn128FieldPointNotAMember => "field point not a member of bn128 curve",
Self::Bn128AffineGFailedToCreate => "failed to create affine g point for bn128 curve",
Self::Bn128PairLength => "bn128 invalid pair length",
Self::BlobInvalidInputLength => "invalid blob input length",
Self::BlobMismatchedVersion => "mismatched blob version",
Self::BlobVerifyKzgProofFailed => "verifying blob kzg proof failed",
Self::Other(s) => s,
};
f.write_str(s)
}
}