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

chore(reth-ethereum-consensus): use const GAS_LIMIT_BOUND_DIVISOR #10948

Merged
merged 3 commits into from
Sep 17, 2024
Merged
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
26 changes: 17 additions & 9 deletions crates/ethereum/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ use reth_primitives::{
};
use std::{sync::Arc, time::SystemTime};

/// The bound divisor of the gas limit, used in update calculations.
const GAS_LIMIT_BOUND_DIVISOR: u64 = 1024;

mod validation;
pub use validation::validate_block_post_execution;

Expand All @@ -43,7 +46,7 @@ impl EthBeaconConsensus {
/// Checks the gas limit for consistency between parent and self headers.
///
/// The maximum allowable difference between self and parent gas limits is determined by the
/// parent's gas limit divided by the elasticity multiplier (1024).
/// parent's gas limit divided by the [`GAS_LIMIT_BOUND_DIVISOR`].
fn validate_against_parent_gas_limit(
&self,
header: &SealedHeader,
Expand All @@ -62,15 +65,16 @@ impl EthBeaconConsensus {

// Check for an increase in gas limit beyond the allowed threshold.
if header.gas_limit > parent_gas_limit {
if header.gas_limit - parent_gas_limit >= parent_gas_limit / 1024 {
if header.gas_limit - parent_gas_limit >= parent_gas_limit / GAS_LIMIT_BOUND_DIVISOR {
return Err(ConsensusError::GasLimitInvalidIncrease {
parent_gas_limit,
child_gas_limit: header.gas_limit,
})
}
}
// Check for a decrease in gas limit beyond the allowed threshold.
else if parent_gas_limit - header.gas_limit >= parent_gas_limit / 1024 {
else if parent_gas_limit - header.gas_limit >= parent_gas_limit / GAS_LIMIT_BOUND_DIVISOR
{
return Err(ConsensusError::GasLimitInvalidDecrease {
parent_gas_limit,
child_gas_limit: header.gas_limit,
Expand Down Expand Up @@ -230,7 +234,7 @@ mod tests {

#[test]
fn test_valid_gas_limit_increase() {
let parent = header_with_gas_limit(1024 * 10);
let parent = header_with_gas_limit(GAS_LIMIT_BOUND_DIVISOR * 10);
let child = header_with_gas_limit(parent.gas_limit + 5);

assert_eq!(
Expand All @@ -254,8 +258,10 @@ mod tests {

#[test]
fn test_invalid_gas_limit_increase_exceeding_limit() {
let parent = header_with_gas_limit(1024 * 10);
let child = header_with_gas_limit(parent.gas_limit + parent.gas_limit / 1024 + 1);
let parent = header_with_gas_limit(GAS_LIMIT_BOUND_DIVISOR * 10);
let child = header_with_gas_limit(
parent.gas_limit + parent.gas_limit / GAS_LIMIT_BOUND_DIVISOR + 1,
);

assert_eq!(
EthBeaconConsensus::new(Arc::new(ChainSpec::default()))
Expand All @@ -269,7 +275,7 @@ mod tests {

#[test]
fn test_valid_gas_limit_decrease_within_limit() {
let parent = header_with_gas_limit(1024 * 10);
let parent = header_with_gas_limit(GAS_LIMIT_BOUND_DIVISOR * 10);
let child = header_with_gas_limit(parent.gas_limit - 5);

assert_eq!(
Expand All @@ -281,8 +287,10 @@ mod tests {

#[test]
fn test_invalid_gas_limit_decrease_exceeding_limit() {
let parent = header_with_gas_limit(1024 * 10);
let child = header_with_gas_limit(parent.gas_limit - parent.gas_limit / 1024 - 1);
let parent = header_with_gas_limit(GAS_LIMIT_BOUND_DIVISOR * 10);
let child = header_with_gas_limit(
parent.gas_limit - parent.gas_limit / GAS_LIMIT_BOUND_DIVISOR - 1,
);

assert_eq!(
EthBeaconConsensus::new(Arc::new(ChainSpec::default()))
Expand Down
Loading