revm_precompile/
interface.rs1use core::fmt;
2use primitives::Bytes;
3use std::string::String;
4
5pub type PrecompileResult = Result<PrecompileOutput, PrecompileError>;
9
10#[derive(Clone, Debug, PartialEq, Eq, Hash)]
12pub struct PrecompileOutput {
13 pub gas_used: u64,
15 pub bytes: Bytes,
17}
18
19impl PrecompileOutput {
20 pub fn new(gas_used: u64, bytes: Bytes) -> Self {
22 Self { gas_used, bytes }
23 }
24}
25
26pub type PrecompileFn = fn(&Bytes, u64) -> PrecompileResult;
27
28#[derive(Clone, Debug, PartialEq, Eq, Hash)]
29pub enum PrecompileError {
30 OutOfGas,
32 Blake2WrongLength,
34 Blake2WrongFinalIndicatorFlag,
35 ModexpExpOverflow,
37 ModexpBaseOverflow,
38 ModexpModOverflow,
39 Bn128FieldPointNotAMember,
41 Bn128AffineGFailedToCreate,
42 Bn128PairLength,
43 BlobInvalidInputLength,
46 BlobMismatchedVersion,
48 BlobVerifyKzgProofFailed,
50 Fatal(String),
52 Other(String),
54}
55
56impl PrecompileError {
57 pub fn other(err: impl Into<String>) -> Self {
59 Self::Other(err.into())
60 }
61
62 pub fn is_oog(&self) -> bool {
64 matches!(self, Self::OutOfGas)
65 }
66}
67
68impl core::error::Error for PrecompileError {}
69
70impl fmt::Display for PrecompileError {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 let s = match self {
73 Self::OutOfGas => "out of gas",
74 Self::Blake2WrongLength => "wrong input length for blake2",
75 Self::Blake2WrongFinalIndicatorFlag => "wrong final indicator flag for blake2",
76 Self::ModexpExpOverflow => "modexp exp overflow",
77 Self::ModexpBaseOverflow => "modexp base overflow",
78 Self::ModexpModOverflow => "modexp mod overflow",
79 Self::Bn128FieldPointNotAMember => "field point not a member of bn128 curve",
80 Self::Bn128AffineGFailedToCreate => "failed to create affine g point for bn128 curve",
81 Self::Bn128PairLength => "bn128 invalid pair length",
82 Self::BlobInvalidInputLength => "invalid blob input length",
83 Self::BlobMismatchedVersion => "mismatched blob version",
84 Self::BlobVerifyKzgProofFailed => "verifying blob kzg proof failed",
85 Self::Fatal(s) => s,
86 Self::Other(s) => s,
87 };
88 f.write_str(s)
89 }
90}