diff --git a/src/rpc/utils.rs b/src/rpc/utils.rs index 6439f75..c59b89c 100644 --- a/src/rpc/utils.rs +++ b/src/rpc/utils.rs @@ -28,44 +28,27 @@ const MIN_GAS_LIMIT: u64 = 5000; // the target if the baseline gas is lower. // Ported from: https://github.com/flashbots/builder/blob/03ee71cf0a344397204f65ff6d3a917ee8e06724/core/utils/gas_limit.go#L8 // Reference implementation: https://eips.ethereum.org/EIPS/eip-1559#specification -pub fn calc_gas_limit(parent_gas_limit: u64, desired_limit: u64) -> u64 { +pub fn calc_gas_limit(parent_gas_limit: u64, mut desired_limit: u64) -> u64 { // TODO: Understand why 1 is subtracted here let delta = parent_gas_limit / GAS_LIMIT_BOUND_DIVISOR - 1; - let desired_or_min_limit = std::cmp::max(desired_limit, MIN_GAS_LIMIT); - match parent_gas_limit.cmp(&desired_or_min_limit) { - Ordering::Less => { - let max_acceptable_limit = parent_gas_limit + delta; - tracing::debug!( - parent_gas_limit, - delta, - desired_limit, - desired_or_min_limit, - max_acceptable_limit, - "Parent gas limit is less than desired/min limit" - ); - std::cmp::min(max_acceptable_limit, desired_or_min_limit) - } - Ordering::Greater => { - let min_acceptable_limit = parent_gas_limit - delta; - tracing::debug!( - parent_gas_limit, - delta, - desired_limit, - desired_or_min_limit, - min_acceptable_limit, - "Parent gas limit is greater than desired/min limit" - ); - std::cmp::max(min_acceptable_limit, desired_or_min_limit) + let mut limit = parent_gas_limit; + + if desired_limit < MIN_GAS_LIMIT { + desired_limit = MIN_GAS_LIMIT; + } + + // If we're outside our allowed gas range, we try to hone towards them + if limit < desired_limit { + limit = parent_gas_limit + delta; + if limit > desired_limit { + limit = desired_limit; } - Ordering::Equal => { - tracing::debug!( - parent_gas_limit, - delta, - desired_limit, - desired_or_min_limit, - "Parent gas limit is equal to desired/min limit" - ); - parent_gas_limit + } else if limit > desired_limit { + limit = parent_gas_limit - delta; + if limit < desired_limit { + limit = desired_limit; } } + + limit }