From 473d8e42634028214222067cf3cc4446dafc035b Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Mon, 23 Sep 2024 18:35:07 +0200 Subject: [PATCH 1/4] consensus: make Header gas limit u64 --- Cargo.toml | 2 +- crates/consensus/src/header.rs | 20 ++++++++++++++++---- crates/eips/src/eip1559/basefee.rs | 2 +- crates/eips/src/eip1559/helpers.rs | 4 ++-- crates/network-primitives/src/traits.rs | 4 ++-- crates/rpc-types-eth/src/block.rs | 4 ++-- 6 files changed, 24 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b75fe19a851..1d330bad25d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -137,7 +137,7 @@ semver = "1.0" thiserror = "1.0" thiserror-no-std = "2.0.2" url = "2.5" -derive_more = { version = "1.0.0", default-features = false } +derive_more = { version = "1.0.0", default-features = false, features = ["full"] } http = "1.1.0" ## serde diff --git a/crates/consensus/src/header.rs b/crates/consensus/src/header.rs index a234a158036..64a12a4be93 100644 --- a/crates/consensus/src/header.rs +++ b/crates/consensus/src/header.rs @@ -60,7 +60,7 @@ 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, @@ -484,7 +484,7 @@ impl Decodable for Header { logs_bloom: Decodable::decode(buf)?, difficulty: Decodable::decode(buf)?, number: u64::decode(buf)?, - gas_limit: u128::decode(buf)?, + gas_limit: u64::decode(buf)?, gas_used: u128::decode(buf)?, timestamp: Decodable::decode(buf)?, extra_data: Decodable::decode(buf)?, @@ -671,7 +671,7 @@ 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; @@ -745,7 +745,7 @@ impl BlockHeader for Header { self.number } - fn gas_limit(&self) -> u128 { + fn gas_limit(&self) -> u64 { self.gas_limit } @@ -808,5 +808,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..b585cac577e 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: u128, gas_limit: u64, base_fee: u128) -> u128 { 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..ddbfa443739 100644 --- a/crates/eips/src/eip1559/helpers.rs +++ b/crates/eips/src/eip1559/helpers.rs @@ -25,12 +25,12 @@ 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, + gas_limit: u64, base_fee: u128, base_fee_params: BaseFeeParams, ) -> u128 { // 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 as u128 / base_fee_params.elasticity_multiplier; 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 diff --git a/crates/network-primitives/src/traits.rs b/crates/network-primitives/src/traits.rs index 7300aba8d4f..157c5dac452 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/rpc-types-eth/src/block.rs b/crates/rpc-types-eth/src/block.rs index ef47e3f1a42..9c981ad13bb 100644 --- a/crates/rpc-types-eth/src/block.rs +++ b/crates/rpc-types-eth/src/block.rs @@ -80,7 +80,7 @@ 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, @@ -259,7 +259,7 @@ impl HeaderResponse for Header { self.miner } - fn gas_limit(&self) -> u128 { + fn gas_limit(&self) -> u64 { self.gas_limit } From 3b829e262fa95b411928c7a8b1313a3a1debc7d8 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Mon, 23 Sep 2024 18:40:54 +0200 Subject: [PATCH 2/4] fix clippy --- crates/eips/src/eip1559/helpers.rs | 8 ++++---- crates/provider/src/ext/anvil.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/eips/src/eip1559/helpers.rs b/crates/eips/src/eip1559/helpers.rs index ddbfa443739..c4ff8ef2270 100644 --- a/crates/eips/src/eip1559/helpers.rs +++ b/crates/eips/src/eip1559/helpers.rs @@ -126,7 +126,7 @@ mod tests { next_base_fee[i], calc_next_block_base_fee( gas_used[i] as u128, - gas_limit[i] as u128, + gas_limit[i], base_fee[i] as u128, BaseFeeParams::optimism_sepolia(), ) as u64 @@ -158,7 +158,7 @@ mod tests { next_base_fee[i], calc_next_block_base_fee( gas_used[i] as u128, - gas_limit[i] as u128, + gas_limit[i], base_fee[i] as u128, BaseFeeParams::optimism(), ) as u64 @@ -190,7 +190,7 @@ mod tests { next_base_fee[i], calc_next_block_base_fee( gas_used[i] as u128, - gas_limit[i] as u128, + gas_limit[i], base_fee[i] as u128, BaseFeeParams::optimism_canyon(), ) as u64 @@ -222,7 +222,7 @@ mod tests { next_base_fee[i], calc_next_block_base_fee( gas_used[i] as u128, - gas_limit[i] as u128, + gas_limit[i], base_fee[i] as u128, BaseFeeParams::base_sepolia(), ) as u64 diff --git a/crates/provider/src/ext/anvil.rs b/crates/provider/src/ext/anvil.rs index 9349e1eb8cd..6b537f7160f 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] From e5e9fc935e2b76f2389998586c640a442e8f70ca Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Mon, 23 Sep 2024 18:59:09 +0200 Subject: [PATCH 3/4] make gas used u64 --- crates/consensus/src/header.rs | 12 +++++----- crates/eips/src/eip1559/basefee.rs | 2 +- crates/eips/src/eip1559/helpers.rs | 36 +++++++++++++++--------------- crates/rpc-types-eth/src/block.rs | 2 +- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/crates/consensus/src/header.rs b/crates/consensus/src/header.rs index 64a12a4be93..145bad3db93 100644 --- a/crates/consensus/src/header.rs +++ b/crates/consensus/src/header.rs @@ -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"))] @@ -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, )) } @@ -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)?, @@ -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; @@ -749,7 +749,7 @@ impl BlockHeader for Header { self.gas_limit } - fn gas_used(&self) -> u128 { + fn gas_used(&self) -> u64 { self.gas_used } diff --git a/crates/eips/src/eip1559/basefee.rs b/crates/eips/src/eip1559/basefee.rs index b585cac577e..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: 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) } } diff --git a/crates/eips/src/eip1559/helpers.rs b/crates/eips/src/eip1559/helpers.rs index c4ff8ef2270..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_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 @@ -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_used[i], gas_limit[i], - base_fee[i] as u128, + 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_used[i], gas_limit[i], - base_fee[i] as u128, + 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_used[i], gas_limit[i], - base_fee[i] as u128, + 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_used[i], gas_limit[i], - base_fee[i] as u128, + base_fee[i], BaseFeeParams::base_sepolia(), - ) as u64 + ) ); } } diff --git a/crates/rpc-types-eth/src/block.rs b/crates/rpc-types-eth/src/block.rs index 9c981ad13bb..fe477f27e1c 100644 --- a/crates/rpc-types-eth/src/block.rs +++ b/crates/rpc-types-eth/src/block.rs @@ -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, From 00bc7e184b8d028d1fb4e7d0f65622005d80429a Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Mon, 23 Sep 2024 09:59:34 -0700 Subject: [PATCH 4/4] Update Cargo.toml --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 1d330bad25d..b75fe19a851 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -137,7 +137,7 @@ semver = "1.0" thiserror = "1.0" thiserror-no-std = "2.0.2" url = "2.5" -derive_more = { version = "1.0.0", default-features = false, features = ["full"] } +derive_more = { version = "1.0.0", default-features = false } http = "1.1.0" ## serde