diff --git a/crates/consensus/src/header.rs b/crates/consensus/src/header.rs index 8ab3a9f99c7..73dc7ec0db3 100644 --- a/crates/consensus/src/header.rs +++ b/crates/consensus/src/header.rs @@ -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"))] @@ -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 { + pub fn next_block_base_fee(&self, base_fee_params: BaseFeeParams) -> Option { 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, )) } @@ -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)?, @@ -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; @@ -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 } @@ -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); } } diff --git a/crates/eips/src/eip1559/basefee.rs b/crates/eips/src/eip1559/basefee.rs index ebb673f9b8e..895135b7eb8 100644 --- a/crates/eips/src/eip1559/basefee.rs +++ b/crates/eips/src/eip1559/basefee.rs @@ -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) } } diff --git a/crates/eips/src/eip1559/helpers.rs b/crates/eips/src/eip1559/helpers.rs index 5bea1fa2314..a37bac89097 100644 --- a/crates/eips/src/eip1559/helpers.rs +++ b/crates/eips/src/eip1559/helpers.rs @@ -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 @@ -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 @@ -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), ) } } @@ -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 + ) ); } } @@ -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 + ) ); } } @@ -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 + ) ); } } @@ -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 + ) ); } } diff --git a/crates/network-primitives/src/traits.rs b/crates/network-primitives/src/traits.rs index fe8dd988711..244b1c4f7b7 100644 --- a/crates/network-primitives/src/traits.rs +++ b/crates/network-primitives/src/traits.rs @@ -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 /// @@ -392,7 +392,7 @@ impl HeaderResponse for WithOtherFields { self.inner.coinbase() } - fn gas_limit(&self) -> u128 { + fn gas_limit(&self) -> u64 { self.inner.gas_limit() } diff --git a/crates/provider/src/ext/anvil.rs b/crates/provider/src/ext/anvil.rs index a32d3439258..93733250557 100644 --- a/crates/provider/src/ext/anvil.rs +++ b/crates/provider/src/ext/anvil.rs @@ -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::(), latest_block.header.gas_limit); + assert_eq!(block_gas_limit.to::(), latest_block.header.gas_limit); } #[tokio::test] diff --git a/crates/rpc-types-eth/src/block.rs b/crates/rpc-types-eth/src/block.rs index 17fc57def77..1a9afb31f85 100644 --- a/crates/rpc-types-eth/src/block.rs +++ b/crates/rpc-types-eth/src/block.rs @@ -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, @@ -259,7 +259,7 @@ impl HeaderResponse for Header { self.miner } - fn gas_limit(&self) -> u128 { + fn gas_limit(&self) -> u64 { self.gas_limit }