From be09550a510ffd0171fb2219bf433b035a06afc7 Mon Sep 17 00:00:00 2001 From: christn Date: Wed, 27 Mar 2024 14:32:54 +0800 Subject: [PATCH] Adjust calc_gas_limit to be a more direct translation of the geth version to check if that fixes differing responses --- src/rpc/utils.rs | 55 ++++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/src/rpc/utils.rs b/src/rpc/utils.rs index 6439f75..646291c 100644 --- a/src/rpc/utils.rs +++ b/src/rpc/utils.rs @@ -1,7 +1,5 @@ use crate::rpc::result::internal_rpc_err; use jsonrpsee::core::RpcResult; -use reth_tracing::tracing; -use std::cmp::Ordering; pub fn compare_values( name: &str, @@ -28,44 +26,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 }