Skip to content

Commit

Permalink
Adjust calc_gas_limit to be a more direct translation of the geth ver…
Browse files Browse the repository at this point in the history
…sion to check if that fixes differing responses
  • Loading branch information
ckoopmann committed Mar 27, 2024
1 parent 182f515 commit be09550
Showing 1 changed file with 18 additions and 37 deletions.
55 changes: 18 additions & 37 deletions src/rpc/utils.rs
Original file line number Diff line number Diff line change
@@ -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<T: std::cmp::PartialEq + std::fmt::Display>(
name: &str,
Expand All @@ -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
}

0 comments on commit be09550

Please sign in to comment.