From 5ddb8056c1a221cc3dcd825d7e46f0651baee555 Mon Sep 17 00:00:00 2001 From: Vid Kersic Date: Fri, 12 Apr 2024 09:25:18 +0200 Subject: [PATCH] fix: pvg --- crates/mempool/src/estimate.rs | 1 + crates/mempool/src/uopool.rs | 14 ++++++++++---- crates/mempool/src/utils.rs | 6 +++--- .../src/validate/sanity/verification_gas.rs | 7 ++++++- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/crates/mempool/src/estimate.rs b/crates/mempool/src/estimate.rs index 76753d91..57857448 100644 --- a/crates/mempool/src/estimate.rs +++ b/crates/mempool/src/estimate.rs @@ -45,6 +45,7 @@ fn is_validation_oog(err: T) -> bool { fn is_execution_oog(err: T) -> bool { err.to_string().contains(EXECUTION_OOG) } + fn is_execution_revert(err: T) -> bool { err.to_string().contains(EXECUTION_REVERTED) } diff --git a/crates/mempool/src/uopool.rs b/crates/mempool/src/uopool.rs index 679dbc50..e7c244d0 100644 --- a/crates/mempool/src/uopool.rs +++ b/crates/mempool/src/uopool.rs @@ -2,6 +2,7 @@ use crate::{ estimate::estimate_user_op_gas, mempool::Mempool, mempool_id, + utils::div_ceil, validate::{ UserOperationValidationOutcome, UserOperationValidator, UserOperationValidatorMode, }, @@ -31,7 +32,7 @@ use std::collections::{HashMap, HashSet}; use tracing::{debug, error, info, trace}; const FILTER_MAX_DEPTH: u64 = 10; -const PRE_VERIFICATION_SAFE_RESERVE: u64 = 1_000; +const PRE_VERIFICATION_SAFE_RESERVE_PERC: u64 = 10; // percentage how higher pre verification gas we return /// The alternative mempool pool implementation that provides functionalities to add, remove, /// validate, and serves data requests from the [RPC API](EthApiServer). Architecturally, the @@ -478,10 +479,15 @@ impl UoPool { }, )?; + let pre_verification_gas = div_ceil( + Overhead::default().calculate_pre_verification_gas(uo).saturating_mul( + U256::from(100).saturating_add(PRE_VERIFICATION_SAFE_RESERVE_PERC.into()), + ), + U256::from(100), + ); + Ok(UserOperationGasEstimation { - pre_verification_gas: Overhead::default() - .calculate_pre_verification_gas(uo) - .saturating_add(PRE_VERIFICATION_SAFE_RESERVE.into()), + pre_verification_gas, verification_gas_limit, call_gas_limit, }) diff --git a/crates/mempool/src/utils.rs b/crates/mempool/src/utils.rs index 8b941df0..3fad6e45 100644 --- a/crates/mempool/src/utils.rs +++ b/crates/mempool/src/utils.rs @@ -24,7 +24,7 @@ pub fn equal_code_hashes(hashes: &[CodeHash], hashes_prev: &Vec) -> bo } /// Struct to calculate the pre-verification gas of a [UserOperation](UserOperation) -// https://github.com/eth-infinitism/bundler/blob/main/packages/sdk/src/calcPreVerificationGas.ts#L44-L52 +// https://github.com/eth-infinitism/bundler/blob/main/packages/sdk/src/calcPreVerificationGas.ts#L44-L51 pub struct Overhead { pub fixed: U256, pub per_user_op: U256, @@ -101,8 +101,8 @@ pub fn calculate_valid_gas(gas_price: U256, gas_incr_perc: U256) -> U256 { // -> (100 / 100) * (gas_price * ( 1 + gas_incr_perc / 100 )) // -> (gas_price * ( 100 + gas_incr_perc )) / 100 // -> (gas_price * ( 100 + gas_incr_perc )) / 100 + rounding_const - let numerator = gas_price.saturating_mul(gas_incr_perc.saturating_add(U256::from(100))); let denominator = U256::from(100); + let numerator = gas_price.saturating_mul(gas_incr_perc.saturating_add(denominator)); div_ceil(numerator, denominator) } @@ -136,7 +136,7 @@ pub fn calculate_call_gas_limit(paid: U256, pre_op_gas: U256, fee_per_gas: U256) /// let result = div_ceil(U256::from(10), U256::from(3)); /// assert_eq!(result, U256::from(4)); /// ``` -fn div_ceil(numerator: U256, denominator: U256) -> U256 { +pub fn div_ceil(numerator: U256, denominator: U256) -> U256 { let rounding_const = U256::from(if numerator.checked_rem(denominator).unwrap_or_default() > U256::zero() { 1 diff --git a/crates/mempool/src/validate/sanity/verification_gas.rs b/crates/mempool/src/validate/sanity/verification_gas.rs index d03f1d20..05af79e2 100644 --- a/crates/mempool/src/validate/sanity/verification_gas.rs +++ b/crates/mempool/src/validate/sanity/verification_gas.rs @@ -1,5 +1,6 @@ use crate::{ mempool::Mempool, + utils::div_ceil, validate::{SanityCheck, SanityHelper}, Overhead, Reputation, SanityError, }; @@ -38,7 +39,11 @@ impl SanityCheck for VerificationGas { }); } - let pre_gas = Overhead::default().calculate_pre_verification_gas(uo); + // calculate the pvg and allow 10 % deviation + let pre_gas = div_ceil( + Overhead::default().calculate_pre_verification_gas(uo).saturating_mul(U256::from(90)), + U256::from(100), + ); if uo.pre_verification_gas < pre_gas { return Err(SanityError::PreVerificationGasTooLow { pre_verification_gas: uo.pre_verification_gas,