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