diff --git a/crates/database/src/states/bundle_account.rs b/crates/database/src/states/bundle_account.rs index d1379dec99..f3a5010b88 100644 --- a/crates/database/src/states/bundle_account.rs +++ b/crates/database/src/states/bundle_account.rs @@ -67,7 +67,7 @@ impl BundleAccount { } } - /// Fetch account info if it exist. + /// Fetch account info if it exists. pub fn account_info(&self) -> Option { self.info.clone() } diff --git a/crates/database/src/states/state.rs b/crates/database/src/states/state.rs index b7f521d960..ca0daab32f 100644 --- a/crates/database/src/states/state.rs +++ b/crates/database/src/states/state.rs @@ -32,7 +32,7 @@ pub struct State { /// /// This allows us to have only one layer of cache where we can fetch data. /// - /// Additionally we can introduce some preloading of data from database. + /// Additionally, we can introduce some preloading of data from database. pub cache: CacheState, /// Optional database that we use to fetch data from /// @@ -290,7 +290,7 @@ impl Database for State { btree_map::Entry::Vacant(entry) => { let ret = *entry.insert(self.database.block_hash(number)?); - // Prune all hashes that are older then BLOCK_HASH_HISTORY + // Prune all hashes that are older than BLOCK_HASH_HISTORY let last_block = number.saturating_sub(BLOCK_HASH_HISTORY); while let Some(entry) = self.block_hashes.first_entry() { if *entry.key() < last_block { diff --git a/crates/database/src/states/state_builder.rs b/crates/database/src/states/state_builder.rs index fc4cd0c00c..222e1de831 100644 --- a/crates/database/src/states/state_builder.rs +++ b/crates/database/src/states/state_builder.rs @@ -23,7 +23,7 @@ pub struct StateBuilder { with_bundle_update: bool, /// Do we want to merge transitions in background? /// - /// This will allows evm to continue executing. + /// This will allow evm to continue executing. /// /// Default is false. with_background_transition_merge: bool, @@ -64,7 +64,7 @@ impl StateBuilder { /// Set the database. pub fn with_database(self, database: ODB) -> StateBuilder { // Cast to the different database. - // Note that we return different type depending of the database NewDBError. + // Note that we return different type depending on the database NewDBError. StateBuilder { with_state_clear: self.with_state_clear, database, diff --git a/crates/handler/src/frame.rs b/crates/handler/src/frame.rs index 083acda797..8861c01c6e 100644 --- a/crates/handler/src/frame.rs +++ b/crates/handler/src/frame.rs @@ -18,7 +18,7 @@ use interpreter::{ CreateScheme, EOFCreateInputs, EOFCreateKind, FrameInput, Gas, InputsImpl, InstructionResult, Interpreter, InterpreterAction, InterpreterResult, InterpreterTypes, SharedMemory, }; -use precompile::PrecompileErrors; +use precompile::PrecompileError; use primitives::{keccak256, Address, Bytes, B256, U256}; use specification::{ constants::CALL_STACK_LIMIT, @@ -81,7 +81,7 @@ where Output = InterpreterAction, >, >, - ERROR: From> + From, + ERROR: From> + From, { type Evm = EVM; type FrameInit = FrameInput; @@ -150,7 +150,7 @@ where Precompiles: PrecompileProvider, Instructions: InstructionProvider, >, - ERROR: From> + From, + ERROR: From> + From, { /// Make call frame #[inline] @@ -522,7 +522,7 @@ where Output = InterpreterAction, >, >, - ERROR: From> + From, + ERROR: From> + From, { pub fn init_first( evm: &mut EVM, diff --git a/crates/handler/src/handler.rs b/crates/handler/src/handler.rs index d17a562da9..5e371f1b03 100644 --- a/crates/handler/src/handler.rs +++ b/crates/handler/src/handler.rs @@ -11,7 +11,7 @@ use context_interface::{ }; use core::mem; use interpreter::{FrameInput, Host, InitialAndFloorGas, Interpreter, InterpreterAction}; -use precompile::PrecompileErrors; +use precompile::PrecompileError; use primitives::Log; use state::EvmState; use std::{vec, vec::Vec}; @@ -20,7 +20,7 @@ pub trait EvmTrError: From + From + From<<::Db as Database>::Error> - + From + + From { } @@ -29,7 +29,7 @@ impl< T: From + From + From<<::Db as Database>::Error> - + From, + + From, > EvmTrError for T { } diff --git a/crates/handler/src/handler/types.rs b/crates/handler/src/handler/types.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/crates/handler/src/precompile_provider.rs b/crates/handler/src/precompile_provider.rs index cc7f5e2f92..f20e887402 100644 --- a/crates/handler/src/precompile_provider.rs +++ b/crates/handler/src/precompile_provider.rs @@ -2,7 +2,7 @@ use auto_impl::auto_impl; use context::Cfg; use context_interface::ContextTr; use interpreter::{Gas, InstructionResult, InterpreterResult}; -use precompile::PrecompileErrors; +use precompile::PrecompileError; use precompile::{PrecompileSpecId, Precompiles}; use primitives::{Address, Bytes}; use specification::hardfork::SpecId; @@ -22,7 +22,7 @@ pub trait PrecompileProvider { address: &Address, bytes: &Bytes, gas_limit: u64, - ) -> Result, PrecompileErrors>; + ) -> Result, PrecompileError>; /// Get the warm addresses. fn warm_addresses(&self) -> Box + '_>; @@ -70,7 +70,7 @@ where address: &Address, bytes: &Bytes, gas_limit: u64, - ) -> Result, PrecompileErrors> { + ) -> Result, PrecompileError> { let Some(precompile) = self.precompiles.get(address) else { return Ok(None); }; @@ -88,14 +88,16 @@ where result.result = InstructionResult::Return; result.output = output.bytes; } - Err(PrecompileErrors::Error(e)) => { + Err(e) => { + if let PrecompileError::Fatal(_) = e { + return Err(e); + } result.result = if e.is_oog() { InstructionResult::PrecompileOOG } else { InstructionResult::PrecompileError }; } - Err(err @ PrecompileErrors::Fatal { .. }) => return Err(err), } Ok(Some(result)) } diff --git a/crates/inspector/src/traits.rs b/crates/inspector/src/traits.rs index fdee6c3df2..78d7997c9b 100644 --- a/crates/inspector/src/traits.rs +++ b/crates/inspector/src/traits.rs @@ -8,7 +8,7 @@ use interpreter::{ interpreter::EthInterpreter, FrameInput, Interpreter, InterpreterAction, InterpreterResult, InterpreterTypes, }; -use precompile::PrecompileErrors; +use precompile::PrecompileError; /// Inspector EVM trait. pub trait InspectorEvmTr: EvmTr { @@ -83,7 +83,7 @@ where Output = InterpreterAction, >, > + InspectorEvmTr, - ERROR: From> + From, + ERROR: From> + From, { type IT = EthInterpreter; diff --git a/crates/optimism/src/bn128.rs b/crates/optimism/src/bn128.rs index 02dbf0c6c1..4e807a0e69 100644 --- a/crates/optimism/src/bn128.rs +++ b/crates/optimism/src/bn128.rs @@ -13,7 +13,7 @@ pub mod pair { pub fn run_pair(input: &[u8], gas_limit: u64) -> PrecompileResult { if input.len() > GRANITE_MAX_INPUT_SIZE { - return Err(PrecompileError::Bn128PairLength.into()); + return Err(PrecompileError::Bn128PairLength); } bn128::run_pair( input, @@ -27,7 +27,7 @@ pub mod pair { #[cfg(test)] mod tests { use super::*; - use revm::{precompile::PrecompileErrors, primitives::hex}; + use revm::{precompile::PrecompileError, primitives::hex}; use std::vec; #[test] @@ -65,25 +65,16 @@ mod tests { .unwrap(); let res = pair::run_pair(&input, 260_000); - assert!(matches!( - res, - Err(PrecompileErrors::Error(PrecompileError::Bn128PairLength)) - )); + assert!(matches!(res, Err(PrecompileError::Bn128PairLength))); // Valid input length shorter than 112687 let input = vec![1u8; 586 * bn128::PAIR_ELEMENT_LEN]; let res = pair::run_pair(&input, 260_000); - assert!(matches!( - res, - Err(PrecompileErrors::Error(PrecompileError::OutOfGas)) - )); + assert!(matches!(res, Err(PrecompileError::OutOfGas))); // Input length longer than 112687 let input = vec![1u8; 587 * bn128::PAIR_ELEMENT_LEN]; let res = pair::run_pair(&input, 260_000); - assert!(matches!( - res, - Err(PrecompileErrors::Error(PrecompileError::Bn128PairLength)) - )); + assert!(matches!(res, Err(PrecompileError::Bn128PairLength))); } } diff --git a/crates/optimism/src/handler/precompiles.rs b/crates/optimism/src/handler/precompiles.rs index 005eef272c..a06e913170 100644 --- a/crates/optimism/src/handler/precompiles.rs +++ b/crates/optimism/src/handler/precompiles.rs @@ -1,6 +1,6 @@ use crate::OpSpecId; use once_cell::race::OnceBox; -use precompile::{secp256r1, PrecompileErrors, Precompiles}; +use precompile::{secp256r1, PrecompileError, Precompiles}; use revm::{ context::Cfg, context_interface::ContextTr, @@ -103,7 +103,7 @@ where address: &precompile::Address, bytes: &precompile::Bytes, gas_limit: u64, - ) -> Result, PrecompileErrors> { + ) -> Result, PrecompileError> { self.precompile_provider .run(context, address, bytes, gas_limit) } diff --git a/crates/precompile/src/blake2.rs b/crates/precompile/src/blake2.rs index 2dac62fa21..7a0369681b 100644 --- a/crates/precompile/src/blake2.rs +++ b/crates/precompile/src/blake2.rs @@ -13,20 +13,20 @@ pub fn run(input: &Bytes, gas_limit: u64) -> PrecompileResult { let input = &input[..]; if input.len() != INPUT_LENGTH { - return Err(PrecompileError::Blake2WrongLength.into()); + return Err(PrecompileError::Blake2WrongLength); } // Rounds 4 bytes let rounds = u32::from_be_bytes(input[..4].try_into().unwrap()) as usize; let gas_used = rounds as u64 * F_ROUND; if gas_used > gas_limit { - return Err(PrecompileError::OutOfGas.into()); + return Err(PrecompileError::OutOfGas); } let f = match input[212] { 1 => true, 0 => false, - _ => return Err(PrecompileError::Blake2WrongFinalIndicatorFlag.into()), + _ => return Err(PrecompileError::Blake2WrongFinalIndicatorFlag), }; let mut h = [0u64; 8]; diff --git a/crates/precompile/src/bls12_381/g1_add.rs b/crates/precompile/src/bls12_381/g1_add.rs index 8607fa66eb..f67024f0b8 100644 --- a/crates/precompile/src/bls12_381/g1_add.rs +++ b/crates/precompile/src/bls12_381/g1_add.rs @@ -20,15 +20,14 @@ pub const PRECOMPILE: PrecompileWithAddress = /// See also: pub(super) fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { if G1_ADD_BASE_GAS_FEE > gas_limit { - return Err(PrecompileError::OutOfGas.into()); + return Err(PrecompileError::OutOfGas); } if input.len() != G1_ADD_INPUT_LENGTH { return Err(PrecompileError::Other(format!( "G1ADD input should be {G1_ADD_INPUT_LENGTH} bytes, was {}", input.len() - )) - .into()); + ))); } // NB: There is no subgroup check for the G1 addition precompile. diff --git a/crates/precompile/src/bls12_381/g1_msm.rs b/crates/precompile/src/bls12_381/g1_msm.rs index ed390d7f69..de8dc8c07b 100644 --- a/crates/precompile/src/bls12_381/g1_msm.rs +++ b/crates/precompile/src/bls12_381/g1_msm.rs @@ -30,14 +30,13 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { return Err(PrecompileError::Other(format!( "G1MSM input length should be multiple of {}, was {}", G1_MSM_INPUT_LENGTH, input_len - )) - .into()); + ))); } let k = input_len / G1_MSM_INPUT_LENGTH; let required_gas = msm_required_gas(k, &DISCOUNT_TABLE_G1_MSM, G1_MSM_BASE_GAS_FEE); if required_gas > gas_limit { - return Err(PrecompileError::OutOfGas.into()); + return Err(PrecompileError::OutOfGas); } let mut g1_points: Vec = Vec::with_capacity(k); diff --git a/crates/precompile/src/bls12_381/g2_add.rs b/crates/precompile/src/bls12_381/g2_add.rs index 70355579be..52528ccf39 100644 --- a/crates/precompile/src/bls12_381/g2_add.rs +++ b/crates/precompile/src/bls12_381/g2_add.rs @@ -21,15 +21,14 @@ pub const PRECOMPILE: PrecompileWithAddress = /// See also pub(super) fn g2_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { if G2_ADD_BASE_GAS_FEE > gas_limit { - return Err(PrecompileError::OutOfGas.into()); + return Err(PrecompileError::OutOfGas); } if input.len() != G2_ADD_INPUT_LENGTH { return Err(PrecompileError::Other(format!( "G2ADD input should be {G2_ADD_INPUT_LENGTH} bytes, was {}", input.len() - )) - .into()); + ))); } // NB: There is no subgroup check for the G2 addition precompile. diff --git a/crates/precompile/src/bls12_381/g2_msm.rs b/crates/precompile/src/bls12_381/g2_msm.rs index ef5f66d34d..87dfeebe26 100644 --- a/crates/precompile/src/bls12_381/g2_msm.rs +++ b/crates/precompile/src/bls12_381/g2_msm.rs @@ -30,14 +30,13 @@ pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { return Err(PrecompileError::Other(format!( "G2MSM input length should be multiple of {}, was {}", G2_MSM_INPUT_LENGTH, input_len - )) - .into()); + ))); } let k = input_len / G2_MSM_INPUT_LENGTH; let required_gas = msm_required_gas(k, &DISCOUNT_TABLE_G2_MSM, G2_MSM_BASE_GAS_FEE); if required_gas > gas_limit { - return Err(PrecompileError::OutOfGas.into()); + return Err(PrecompileError::OutOfGas); } let mut g2_points: Vec = Vec::with_capacity(k); diff --git a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs index 926400329e..6d62ed47cb 100644 --- a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs +++ b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs @@ -17,15 +17,14 @@ pub const PRECOMPILE: PrecompileWithAddress = /// See also: pub(super) fn map_fp2_to_g2(input: &Bytes, gas_limit: u64) -> PrecompileResult { if MAP_FP2_TO_G2_BASE_GAS_FEE > gas_limit { - return Err(PrecompileError::OutOfGas.into()); + return Err(PrecompileError::OutOfGas); } if input.len() != PADDED_FP2_LENGTH { return Err(PrecompileError::Other(format!( "MAP_FP2_TO_G2 input should be {PADDED_FP2_LENGTH} bytes, was {}", input.len() - )) - .into()); + ))); } let input_p0_x = remove_padding(&input[..PADDED_FP_LENGTH])?; diff --git a/crates/precompile/src/bls12_381/map_fp_to_g1.rs b/crates/precompile/src/bls12_381/map_fp_to_g1.rs index 90f6f12ca0..7b15a53197 100644 --- a/crates/precompile/src/bls12_381/map_fp_to_g1.rs +++ b/crates/precompile/src/bls12_381/map_fp_to_g1.rs @@ -17,15 +17,14 @@ pub const PRECOMPILE: PrecompileWithAddress = /// See also: pub(super) fn map_fp_to_g1(input: &Bytes, gas_limit: u64) -> PrecompileResult { if MAP_FP_TO_G1_BASE_GAS_FEE > gas_limit { - return Err(PrecompileError::OutOfGas.into()); + return Err(PrecompileError::OutOfGas); } if input.len() != PADDED_FP_LENGTH { return Err(PrecompileError::Other(format!( "MAP_FP_TO_G1 input should be {PADDED_FP_LENGTH} bytes, was {}", input.len() - )) - .into()); + ))); } let input_p0 = remove_padding(input)?; @@ -55,7 +54,7 @@ mod test { let fail = map_fp_to_g1(&input, MAP_FP_TO_G1_BASE_GAS_FEE); assert_eq!( fail, - Err(PrecompileError::Other("non-canonical fp value".to_string()).into()) + Err(PrecompileError::Other("non-canonical fp value".to_string())) ); } } diff --git a/crates/precompile/src/bls12_381/pairing.rs b/crates/precompile/src/bls12_381/pairing.rs index 6b97d14bbf..9f26cd3c30 100644 --- a/crates/precompile/src/bls12_381/pairing.rs +++ b/crates/precompile/src/bls12_381/pairing.rs @@ -30,14 +30,14 @@ pub(super) fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { if input_len == 0 || input_len % PAIRING_INPUT_LENGTH != 0 { return Err(PrecompileError::Other(format!( "Pairing input length should be multiple of {PAIRING_INPUT_LENGTH}, was {input_len}" - )) - .into()); + ))); } let k = input_len / PAIRING_INPUT_LENGTH; - let required_gas: u64 = PAIRING_PAIRING_MULTIPLIER_BASE * k as u64 + PAIRING_PAIRING_OFFSET_BASE; + let required_gas: u64 = + PAIRING_PAIRING_MULTIPLIER_BASE * k as u64 + PAIRING_PAIRING_OFFSET_BASE; if required_gas > gas_limit { - return Err(PrecompileError::OutOfGas.into()); + return Err(PrecompileError::OutOfGas); } // Accumulator for the fp12 multiplications of the miller loops. diff --git a/crates/precompile/src/bn128.rs b/crates/precompile/src/bn128.rs index f54f9b7fff..b0ec876ca1 100644 --- a/crates/precompile/src/bn128.rs +++ b/crates/precompile/src/bn128.rs @@ -119,7 +119,7 @@ pub fn new_g1_point(px: Fq, py: Fq) -> Result { pub fn run_add(input: &[u8], gas_cost: u64, gas_limit: u64) -> PrecompileResult { if gas_cost > gas_limit { - return Err(PrecompileError::OutOfGas.into()); + return Err(PrecompileError::OutOfGas); } let input = right_pad::(input); @@ -137,7 +137,7 @@ pub fn run_add(input: &[u8], gas_cost: u64, gas_limit: u64) -> PrecompileResult pub fn run_mul(input: &[u8], gas_cost: u64, gas_limit: u64) -> PrecompileResult { if gas_cost > gas_limit { - return Err(PrecompileError::OutOfGas.into()); + return Err(PrecompileError::OutOfGas); } let input = right_pad::(input); @@ -163,11 +163,11 @@ pub fn run_pair( ) -> PrecompileResult { let gas_used = (input.len() / PAIR_ELEMENT_LEN) as u64 * pair_per_point_cost + pair_base_cost; if gas_used > gas_limit { - return Err(PrecompileError::OutOfGas.into()); + return Err(PrecompileError::OutOfGas); } if input.len() % PAIR_ELEMENT_LEN != 0 { - return Err(PrecompileError::Bn128PairLength.into()); + return Err(PrecompileError::Bn128PairLength); } let success = if input.is_empty() { @@ -227,7 +227,7 @@ mod tests { mul::BYZANTIUM_MUL_GAS_COST, pair::{BYZANTIUM_PAIR_BASE, BYZANTIUM_PAIR_PER_POINT}, }, - PrecompileError, PrecompileErrors, + PrecompileError, }; use primitives::hex; @@ -284,10 +284,7 @@ mod tests { let res = run_add(&input, BYZANTIUM_ADD_GAS_COST, 499); - assert!(matches!( - res, - Err(PrecompileErrors::Error(PrecompileError::OutOfGas)) - )); + assert!(matches!(res, Err(PrecompileError::OutOfGas))); // No input test let input = [0u8; 0]; @@ -314,9 +311,7 @@ mod tests { let res = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500); assert!(matches!( res, - Err(PrecompileErrors::Error( - PrecompileError::Bn128AffineGFailedToCreate - )) + Err(PrecompileError::Bn128AffineGFailedToCreate) )); } @@ -349,10 +344,7 @@ mod tests { .unwrap(); let res = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 39_999); - assert!(matches!( - res, - Err(PrecompileErrors::Error(PrecompileError::OutOfGas)) - )); + assert!(matches!(res, Err(PrecompileError::OutOfGas))); // Zero multiplication test let input = hex::decode( @@ -396,9 +388,7 @@ mod tests { let res = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000); assert!(matches!( res, - Err(PrecompileErrors::Error( - PrecompileError::Bn128AffineGFailedToCreate - )) + Err(PrecompileError::Bn128AffineGFailedToCreate) )); } @@ -457,10 +447,7 @@ mod tests { BYZANTIUM_PAIR_BASE, 259_999, ); - assert!(matches!( - res, - Err(PrecompileErrors::Error(PrecompileError::OutOfGas)) - )); + assert!(matches!(res, Err(PrecompileError::OutOfGas))); // No input test let input = [0u8; 0]; @@ -497,9 +484,7 @@ mod tests { ); assert!(matches!( res, - Err(PrecompileErrors::Error( - PrecompileError::Bn128AffineGFailedToCreate - )) + Err(PrecompileError::Bn128AffineGFailedToCreate) )); // Invalid input length @@ -518,9 +503,6 @@ mod tests { BYZANTIUM_PAIR_BASE, 260_000, ); - assert!(matches!( - res, - Err(PrecompileErrors::Error(PrecompileError::Bn128PairLength)) - )); + assert!(matches!(res, Err(PrecompileError::Bn128PairLength))); } } diff --git a/crates/precompile/src/hash.rs b/crates/precompile/src/hash.rs index e33608d0f5..43256e5a58 100644 --- a/crates/precompile/src/hash.rs +++ b/crates/precompile/src/hash.rs @@ -18,7 +18,7 @@ pub const RIPEMD160: PrecompileWithAddress = pub fn sha256_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { let cost = calc_linear_cost_u32(input.len(), 60, 12); if cost > gas_limit { - Err(PrecompileError::OutOfGas.into()) + Err(PrecompileError::OutOfGas) } else { let output = sha2::Sha256::digest(input); Ok(PrecompileOutput::new(cost, output.to_vec().into())) @@ -34,7 +34,7 @@ pub fn sha256_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { pub fn ripemd160_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { let gas_used = calc_linear_cost_u32(input.len(), 600, 120); if gas_used > gas_limit { - Err(PrecompileError::OutOfGas.into()) + Err(PrecompileError::OutOfGas) } else { let mut hasher = ripemd::Ripemd160::new(); hasher.update(input); diff --git a/crates/precompile/src/identity.rs b/crates/precompile/src/identity.rs index 339d68a9b0..d03571225c 100644 --- a/crates/precompile/src/identity.rs +++ b/crates/precompile/src/identity.rs @@ -18,7 +18,7 @@ pub const IDENTITY_PER_WORD: u64 = 3; pub fn identity_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { let gas_used = calc_linear_cost_u32(input.len(), IDENTITY_BASE, IDENTITY_PER_WORD); if gas_used > gas_limit { - return Err(PrecompileError::OutOfGas.into()); + return Err(PrecompileError::OutOfGas); } Ok(PrecompileOutput::new(gas_used, input.clone())) } diff --git a/crates/precompile/src/interface.rs b/crates/precompile/src/interface.rs index 16e268b5d3..a8709f013b 100644 --- a/crates/precompile/src/interface.rs +++ b/crates/precompile/src/interface.rs @@ -6,7 +6,7 @@ use std::string::{String, ToString}; /// A precompile operation result type /// /// Returns either `Ok((gas_used, return_bytes))` or `Err(error)`. -pub type PrecompileResult = Result; +pub type PrecompileResult = Result; /// Precompile execution output #[derive(Clone, Debug, PartialEq, Eq, Hash)] @@ -26,30 +26,6 @@ impl PrecompileOutput { pub type PrecompileFn = fn(&Bytes, u64) -> PrecompileResult; -/// Precompile errors. -#[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub enum PrecompileErrors { - Error(PrecompileError), - Fatal { msg: String }, -} - -impl From for EVMError { - 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 { /// out of gas is the main error. Others are here just for completeness @@ -72,12 +48,14 @@ pub enum PrecompileError { 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 an other error with the given message. + /// Returns another error with the given message. pub fn other(err: impl Into) -> Self { Self::Other(err.into()) } @@ -88,9 +66,9 @@ impl PrecompileError { } } -impl From for PrecompileErrors { - fn from(err: PrecompileError) -> Self { - PrecompileErrors::Error(err) +impl From for EVMError { + fn from(value: PrecompileError) -> Self { + Self::Precompile(value.to_string()) } } @@ -111,6 +89,7 @@ impl fmt::Display for PrecompileError { 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) diff --git a/crates/precompile/src/kzg_point_evaluation.rs b/crates/precompile/src/kzg_point_evaluation.rs index d96b7e0910..9214694a18 100644 --- a/crates/precompile/src/kzg_point_evaluation.rs +++ b/crates/precompile/src/kzg_point_evaluation.rs @@ -31,19 +31,19 @@ pub const RETURN_VALUE: &[u8; 64] = &hex!( /// with z and y being padded 32 byte big endian values pub fn run(input: &Bytes, gas_limit: u64) -> PrecompileResult { if gas_limit < GAS_COST { - return Err(PrecompileError::OutOfGas.into()); + return Err(PrecompileError::OutOfGas); } // Verify input length. if input.len() != 192 { - return Err(PrecompileError::BlobInvalidInputLength.into()); + return Err(PrecompileError::BlobInvalidInputLength); } // Verify commitment matches versioned_hash let versioned_hash = &input[..32]; let commitment = &input[96..144]; if kzg_to_versioned_hash(commitment) != versioned_hash { - return Err(PrecompileError::BlobMismatchedVersion.into()); + return Err(PrecompileError::BlobMismatchedVersion); } // Verify KZG proof with z and y in big endian format @@ -52,7 +52,7 @@ pub fn run(input: &Bytes, gas_limit: u64) -> PrecompileResult { let y = as_bytes32(&input[64..96]); let proof = as_bytes48(&input[144..192]); if !verify_kzg_proof(commitment, z, y, proof) { - return Err(PrecompileError::BlobVerifyKzgProofFailed.into()); + return Err(PrecompileError::BlobVerifyKzgProofFailed); } // Return FIELD_ELEMENTS_PER_BLOB and BLS_MODULUS as padded 32 byte big endian values diff --git a/crates/precompile/src/lib.rs b/crates/precompile/src/lib.rs index 66faee445c..a05ed7365c 100644 --- a/crates/precompile/src/lib.rs +++ b/crates/precompile/src/lib.rs @@ -146,7 +146,7 @@ impl Precompiles { if #[cfg(any(feature = "c-kzg", feature = "kzg-rs"))] { let precompile = kzg_point_evaluation::POINT_EVALUATION.clone(); } else { - let precompile = PrecompileWithAddress(u64_to_address(0x0A), |_,_| Err(PrecompileErrors::Fatal { msg: "c-kzg feature is not enabled".into()})); + let precompile = PrecompileWithAddress(u64_to_address(0x0A), |_,_| Err(PrecompileError::Fatal("c-kzg feature is not enabled".into()))); } } diff --git a/crates/precompile/src/modexp.rs b/crates/precompile/src/modexp.rs index c563ddd0a9..7dd20d957b 100644 --- a/crates/precompile/src/modexp.rs +++ b/crates/precompile/src/modexp.rs @@ -48,7 +48,7 @@ where { // If there is no minimum gas, return error. if min_gas > gas_limit { - return Err(PrecompileError::OutOfGas.into()); + return Err(PrecompileError::OutOfGas); } // The format of input is: @@ -64,10 +64,10 @@ where // Cast base and modulus to usize, it does not make sense to handle larger values let Ok(base_len) = usize::try_from(base_len) else { - return Err(PrecompileError::ModexpBaseOverflow.into()); + return Err(PrecompileError::ModexpBaseOverflow); }; let Ok(mod_len) = usize::try_from(mod_len) else { - return Err(PrecompileError::ModexpModOverflow.into()); + return Err(PrecompileError::ModexpModOverflow); }; // Handle a special case when both the base and mod length are zero. @@ -77,7 +77,7 @@ where // Cast exponent length to usize, since it does not make sense to handle larger values. let Ok(exp_len) = usize::try_from(exp_len) else { - return Err(PrecompileError::ModexpExpOverflow.into()); + return Err(PrecompileError::ModexpExpOverflow); }; // Used to extract ADJUSTED_EXPONENT_LENGTH. @@ -97,7 +97,7 @@ where // Check if we have enough gas. let gas_cost = calc_gas(base_len as u64, exp_len as u64, mod_len as u64, &exp_highp); if gas_cost > gas_limit { - return Err(PrecompileError::OutOfGas.into()); + return Err(PrecompileError::OutOfGas); } // Padding is needed if the input does not contain all 3 values. diff --git a/crates/precompile/src/secp256k1.rs b/crates/precompile/src/secp256k1.rs index 86cfa8f9fe..8c8b6a68a2 100644 --- a/crates/precompile/src/secp256k1.rs +++ b/crates/precompile/src/secp256k1.rs @@ -17,7 +17,7 @@ pub fn ec_recover_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { const ECRECOVER_BASE: u64 = 3_000; if ECRECOVER_BASE > gas_limit { - return Err(PrecompileError::OutOfGas.into()); + return Err(PrecompileError::OutOfGas); } let input = right_pad::<128>(input); diff --git a/crates/precompile/src/secp256r1.rs b/crates/precompile/src/secp256r1.rs index ed3a38e78b..c3912179c9 100644 --- a/crates/precompile/src/secp256r1.rs +++ b/crates/precompile/src/secp256r1.rs @@ -35,7 +35,7 @@ pub const P256VERIFY: PrecompileWithAddress = /// | 32 | 32 | 32 | 32 | 32 | pub fn p256_verify(input: &Bytes, gas_limit: u64) -> PrecompileResult { if P256VERIFY_BASE > gas_limit { - return Err(PrecompileError::OutOfGas.into()); + return Err(PrecompileError::OutOfGas); } let result = if verify_impl(input).is_some() { B256::with_last_byte(1).into() @@ -75,7 +75,7 @@ pub fn verify_impl(input: &[u8]) -> Option<()> { #[cfg(test)] mod test { use super::*; - use crate::PrecompileErrors; + use crate::PrecompileError; use primitives::hex::FromHex; use rstest::rstest; @@ -116,10 +116,7 @@ mod test { let result = p256_verify(&input, target_gas); assert!(result.is_err()); - assert_eq!( - result.err(), - Some(PrecompileErrors::Error(PrecompileError::OutOfGas)) - ); + assert_eq!(result.err(), Some(PrecompileError::OutOfGas)); } #[rstest] diff --git a/examples/erc20_gas/src/main.rs b/examples/erc20_gas/src/main.rs index 4c0c76ce4b..55f9386025 100644 --- a/examples/erc20_gas/src/main.rs +++ b/examples/erc20_gas/src/main.rs @@ -15,7 +15,7 @@ use revm::{ ContextTr, Journal, }, database_interface::WrapDatabaseAsync, - precompile::PrecompileErrors, + precompile::PrecompileError, primitives::{address, keccak256, Address, Bytes, TxKind, U256}, specification::hardfork::SpecId, state::AccountInfo, @@ -88,7 +88,7 @@ where ERROR: From + From + From<::Error> - + From, + + From, { let sender_balance_slot = erc_address_storage(sender); let sender_balance = context.journal().sload(TOKEN, sender_balance_slot)?.data;