Skip to content

Commit

Permalink
make gas used u64
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger committed Sep 23, 2024
1 parent 3b829e2 commit e5e9fc9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
12 changes: 6 additions & 6 deletions crates/consensus/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct Header {
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 @@ -485,7 +485,7 @@ impl Decodable for Header {
difficulty: Decodable::decode(buf)?,
number: u64::decode(buf)?,
gas_limit: u64::decode(buf)?,
gas_used: u128::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 @@ -674,7 +674,7 @@ pub trait BlockHeader {
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 @@ -749,7 +749,7 @@ impl BlockHeader for Header {
self.gas_limit
}

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

Expand Down
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: u64, 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)
}
}
36 changes: 18 additions & 18 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_used: u64,
gas_limit: u64,
base_fee: u128,
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 as u128 / 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_used[i],
gas_limit[i],
base_fee[i] as u128,
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_used[i],
gas_limit[i],
base_fee[i] as u128,
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_used[i],
gas_limit[i],
base_fee[i] as u128,
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_used[i],
gas_limit[i],
base_fee[i] as u128,
base_fee[i],
BaseFeeParams::base_sepolia(),
) as u64
)
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc-types-eth/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub struct Header {
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

0 comments on commit e5e9fc9

Please sign in to comment.