-
Notifications
You must be signed in to change notification settings - Fork 645
/
Copy pathinterface.rs
97 lines (87 loc) · 3.14 KB
/
interface.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use context_interface::result::EVMError;
use core::fmt;
use primitives::Bytes;
use std::string::{String, ToString};
/// A precompile operation result type
///
/// Returns either `Ok((gas_used, return_bytes))` or `Err(error)`.
pub type PrecompileResult = Result<PrecompileOutput, PrecompileError>;
/// Precompile execution output
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct PrecompileOutput {
/// Gas used by the precompile
pub gas_used: u64,
/// Output bytes
pub bytes: Bytes,
}
impl PrecompileOutput {
/// Returns new precompile output with the given gas used and output bytes.
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 PrecompileError {
/// out of gas is the main error. Others are here just for completeness
OutOfGas,
// Blake2 errors
Blake2WrongLength,
Blake2WrongFinalIndicatorFlag,
// Modexp errors
ModexpExpOverflow,
ModexpBaseOverflow,
ModexpModOverflow,
// Bn128 errors
Bn128FieldPointNotAMember,
Bn128AffineGFailedToCreate,
Bn128PairLength,
// Blob errors
/// The input length is not exactly 192 bytes
BlobInvalidInputLength,
/// The commitment does not match the versioned hash
BlobMismatchedVersion,
/// The proof verification failed
BlobVerifyKzgProofFailed,
/// Fatal error with a custom error message
Fatal(String),
/// Catch-all variant for other errors
Other(String),
}
impl PrecompileError {
/// Returns another error with the given message.
pub fn other(err: impl Into<String>) -> Self {
Self::Other(err.into())
}
/// Returns `true` if the error is out of gas.
pub fn is_oog(&self) -> bool {
matches!(self, Self::OutOfGas)
}
}
impl<DB, TXERROR> From<PrecompileError> for EVMError<DB, TXERROR> {
fn from(value: PrecompileError) -> Self {
Self::Precompile(value.to_string())
}
}
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::Fatal(s) => s,
Self::Other(s) => s,
};
f.write_str(s)
}
}