Skip to content

Commit

Permalink
consensus: make Header gas limit u64 (alloy-rs#1333)
Browse files Browse the repository at this point in the history
* consensus: make Header gas limit u64

* fix clippy

* make gas used u64

* Update Cargo.toml

---------

Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
tcoratger and mattsse authored Sep 26, 2024
1 parent b2ddb1a commit b15d12b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 40 deletions.
32 changes: 22 additions & 10 deletions crates/consensus/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ pub struct Header {
pub number: BlockNumber,
/// A scalar value equal to the current limit of gas expenditure per block; formally Hl.
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
pub gas_limit: u128,
pub gas_limit: u64,
/// A scalar value equal to the total gas used in transactions in this block; formally Hg.
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
pub gas_used: u128,
pub gas_used: u64,
/// A scalar value equal to the reasonable output of Unix’s time() at this block’s inception;
/// formally Hs.
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
Expand Down Expand Up @@ -231,11 +231,11 @@ impl Header {
/// Calculate base fee for next block according to the EIP-1559 spec.
///
/// Returns a `None` if no base fee is set, no EIP-1559 support
pub fn next_block_base_fee(&self, base_fee_params: BaseFeeParams) -> Option<u128> {
pub fn next_block_base_fee(&self, base_fee_params: BaseFeeParams) -> Option<u64> {
Some(calc_next_block_base_fee(
self.gas_used,
self.gas_limit,
self.base_fee_per_gas?,
self.base_fee_per_gas? as u64,
base_fee_params,
))
}
Expand Down Expand Up @@ -493,8 +493,8 @@ impl Decodable for Header {
logs_bloom: Decodable::decode(buf)?,
difficulty: Decodable::decode(buf)?,
number: u64::decode(buf)?,
gas_limit: u128::decode(buf)?,
gas_used: u128::decode(buf)?,
gas_limit: u64::decode(buf)?,
gas_used: u64::decode(buf)?,
timestamp: Decodable::decode(buf)?,
extra_data: Decodable::decode(buf)?,
mix_hash: Decodable::decode(buf)?,
Expand Down Expand Up @@ -680,10 +680,10 @@ pub trait BlockHeader {
fn number(&self) -> BlockNumber;

/// Retrieves the gas limit of the block
fn gas_limit(&self) -> u128;
fn gas_limit(&self) -> u64;

/// Retrieves the gas used by the block
fn gas_used(&self) -> u128;
fn gas_used(&self) -> u64;

/// Retrieves the timestamp of the block
fn timestamp(&self) -> u64;
Expand Down Expand Up @@ -754,11 +754,11 @@ impl BlockHeader for Header {
self.number
}

fn gas_limit(&self) -> u128 {
fn gas_limit(&self) -> u64 {
self.gas_limit
}

fn gas_used(&self) -> u128 {
fn gas_used(&self) -> u64 {
self.gas_used
}

Expand Down Expand Up @@ -817,5 +817,17 @@ mod tests {

let decoded: Header = serde_json::from_str(&json).unwrap();
assert_eq!(decoded, header);

// Create a vector to store the encoded RLP
let mut encoded_rlp = Vec::new();

// Encode the header data
decoded.encode(&mut encoded_rlp);

// Decode the RLP data
let decoded_rlp = Header::decode(&mut encoded_rlp.as_slice()).unwrap();

// Check that the decoded RLP data matches the original header data
assert_eq!(decoded_rlp, decoded);
}
}
2 changes: 1 addition & 1 deletion crates/eips/src/eip1559/basefee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl BaseFeeParams {
///
/// See also [calc_next_block_base_fee]
#[inline]
pub fn next_block_base_fee(self, gas_used: u128, gas_limit: u128, base_fee: u128) -> u128 {
pub fn next_block_base_fee(self, gas_used: u64, gas_limit: u64, base_fee: u64) -> u64 {
calc_next_block_base_fee(gas_used, gas_limit, base_fee, self)
}
}
46 changes: 23 additions & 23 deletions crates/eips/src/eip1559/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ use crate::eip1559::BaseFeeParams;
///
/// For more information, refer to the [EIP-1559 spec](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1559.md).
pub fn calc_next_block_base_fee(
gas_used: u128,
gas_limit: u128,
base_fee: u128,
gas_used: u64,
gas_limit: u64,
base_fee: u64,
base_fee_params: BaseFeeParams,
) -> u128 {
) -> u64 {
// Calculate the target gas by dividing the gas limit by the elasticity multiplier.
let gas_target = gas_limit / base_fee_params.elasticity_multiplier;
let gas_target = gas_limit / base_fee_params.elasticity_multiplier as u64;

match gas_used.cmp(&gas_target) {
// If the gas used in the current block is equal to the gas target, the base fee remains the
Expand All @@ -45,7 +45,7 @@ pub fn calc_next_block_base_fee(
// Ensure a minimum increase of 1.
1,
base_fee * (gas_used - gas_target)
/ (gas_target * base_fee_params.max_change_denominator),
/ (gas_target * base_fee_params.max_change_denominator as u64),
))
}
// If the gas used in the current block is less than the gas target, calculate a new
Expand All @@ -54,7 +54,7 @@ pub fn calc_next_block_base_fee(
// Calculate the decrease in base fee based on the formula defined by EIP-1559.
base_fee.saturating_sub(
base_fee * (gas_target - gas_used)
/ (gas_target * base_fee_params.max_change_denominator),
/ (gas_target * base_fee_params.max_change_denominator as u64),
)
}
}
Expand Down Expand Up @@ -125,11 +125,11 @@ mod tests {
assert_eq!(
next_base_fee[i],
calc_next_block_base_fee(
gas_used[i] as u128,
gas_limit[i] as u128,
base_fee[i] as u128,
gas_used[i],
gas_limit[i],
base_fee[i],
BaseFeeParams::optimism_sepolia(),
) as u64
)
);
}
}
Expand Down Expand Up @@ -157,11 +157,11 @@ mod tests {
assert_eq!(
next_base_fee[i],
calc_next_block_base_fee(
gas_used[i] as u128,
gas_limit[i] as u128,
base_fee[i] as u128,
gas_used[i],
gas_limit[i],
base_fee[i],
BaseFeeParams::optimism(),
) as u64
)
);
}
}
Expand Down Expand Up @@ -189,11 +189,11 @@ mod tests {
assert_eq!(
next_base_fee[i],
calc_next_block_base_fee(
gas_used[i] as u128,
gas_limit[i] as u128,
base_fee[i] as u128,
gas_used[i],
gas_limit[i],
base_fee[i],
BaseFeeParams::optimism_canyon(),
) as u64
)
);
}
}
Expand Down Expand Up @@ -221,11 +221,11 @@ mod tests {
assert_eq!(
next_base_fee[i],
calc_next_block_base_fee(
gas_used[i] as u128,
gas_limit[i] as u128,
base_fee[i] as u128,
gas_used[i],
gas_limit[i],
base_fee[i],
BaseFeeParams::base_sepolia(),
) as u64
)
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/network-primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub trait HeaderResponse {
fn coinbase(&self) -> Address;

/// Gas limit of the block
fn gas_limit(&self) -> u128;
fn gas_limit(&self) -> u64;

/// Mix hash of the block
///
Expand Down Expand Up @@ -392,7 +392,7 @@ impl<T: HeaderResponse> HeaderResponse for WithOtherFields<T> {
self.inner.coinbase()
}

fn gas_limit(&self) -> u128 {
fn gas_limit(&self) -> u64 {
self.inner.gas_limit()
}

Expand Down
2 changes: 1 addition & 1 deletion crates/provider/src/ext/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ mod tests {

let latest_block =
provider.get_block_by_number(BlockNumberOrTag::Latest, false).await.unwrap().unwrap();
assert_eq!(block_gas_limit.to::<u128>(), latest_block.header.gas_limit);
assert_eq!(block_gas_limit.to::<u64>(), latest_block.header.gas_limit);
}

#[tokio::test]
Expand Down
6 changes: 3 additions & 3 deletions crates/rpc-types-eth/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ pub struct Header {
pub number: u64,
/// Gas Limit
#[cfg_attr(feature = "serde", serde(default, with = "alloy_serde::quantity"))]
pub gas_limit: u128,
pub gas_limit: u64,
/// Gas Used
#[cfg_attr(feature = "serde", serde(default, with = "alloy_serde::quantity"))]
pub gas_used: u128,
pub gas_used: u64,
/// Timestamp
#[cfg_attr(feature = "serde", serde(default, with = "alloy_serde::quantity"))]
pub timestamp: u64,
Expand Down Expand Up @@ -259,7 +259,7 @@ impl HeaderResponse for Header {
self.miner
}

fn gas_limit(&self) -> u128 {
fn gas_limit(&self) -> u64 {
self.gas_limit
}

Expand Down

0 comments on commit b15d12b

Please sign in to comment.