Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pvg #302

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/mempool/src/estimate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ fn is_validation_oog<T: ToString>(err: T) -> bool {
fn is_execution_oog<T: ToString>(err: T) -> bool {
err.to_string().contains(EXECUTION_OOG)
}

fn is_execution_revert<T: ToString>(err: T) -> bool {
err.to_string().contains(EXECUTION_REVERTED)
}
Expand Down
14 changes: 10 additions & 4 deletions crates/mempool/src/uopool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
estimate::estimate_user_op_gas,
mempool::Mempool,
mempool_id,
utils::div_ceil,
validate::{
UserOperationValidationOutcome, UserOperationValidator, UserOperationValidatorMode,
},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -478,10 +479,15 @@ impl<M: Middleware + 'static, V: UserOperationValidator> UoPool<M, V> {
},
)?;

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,
})
Expand Down
6 changes: 3 additions & 3 deletions crates/mempool/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn equal_code_hashes(hashes: &[CodeHash], hashes_prev: &Vec<CodeHash>) -> 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,
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion crates/mempool/src/validate/sanity/verification_gas.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
mempool::Mempool,
utils::div_ceil,
validate::{SanityCheck, SanityHelper},
Overhead, Reputation, SanityError,
};
Expand Down Expand Up @@ -38,7 +39,11 @@ impl<M: Middleware> SanityCheck<M> 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,
Expand Down
Loading