diff --git a/crates/consensus/src/header.rs b/crates/consensus/src/header.rs index 0d5bb832769..252f05168a9 100644 --- a/crates/consensus/src/header.rs +++ b/crates/consensus/src/header.rs @@ -54,9 +54,9 @@ pub struct Header { /// zero; formally Hi. pub number: BlockNumber, /// A scalar value equal to the current limit of gas expenditure per block; formally Hl. - pub gas_limit: u64, + pub gas_limit: u128, /// A scalar value equal to the total gas used in transactions in this block; formally Hg. - pub gas_used: u64, + pub gas_used: u128, /// A scalar value equal to the reasonable output of Unix’s time() at this block’s inception; /// formally Hs. pub timestamp: u64, @@ -73,14 +73,14 @@ pub struct Header { /// The algorithm results in the base fee per gas increasing when blocks are /// above the gas target, and decreasing when blocks are below the gas target. The base fee per /// gas is burned. - pub base_fee_per_gas: Option, + pub base_fee_per_gas: Option, /// The total amount of blob gas consumed by the transactions within the block, added in /// EIP-4844. - pub blob_gas_used: Option, + pub blob_gas_used: Option, /// A running total of blob gas consumed in excess of the target, prior to the block. Blocks /// with above-target blob gas consumption increase this value, blocks with below-target blob /// gas consumption decrease it (bounded at 0). This was added in EIP-4844. - pub excess_blob_gas: Option, + pub excess_blob_gas: Option, /// The hash of the parent beacon block's root is included in execution blocks, as proposed by /// EIP-4788. /// @@ -194,7 +194,7 @@ 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, @@ -207,7 +207,7 @@ impl Header { /// spec. /// /// Returns a `None` if no excess blob gas is set, no EIP-4844 support - pub fn next_block_excess_blob_gas(&self) -> Option { + pub fn next_block_excess_blob_gas(&self) -> Option { Some(calc_excess_blob_gas(self.excess_blob_gas?, self.blob_gas_used?)) } @@ -224,14 +224,14 @@ impl Header { mem::size_of::() + // logs bloom mem::size_of::() + // difficulty mem::size_of::() + // number - mem::size_of::() + // gas limit - mem::size_of::() + // gas used + mem::size_of::() + // gas limit + mem::size_of::() + // gas used mem::size_of::() + // timestamp mem::size_of::() + // mix hash mem::size_of::() + // nonce - mem::size_of::>() + // base fee per gas - mem::size_of::>() + // blob gas used - mem::size_of::>() + // excess blob gas + mem::size_of::>() + // base fee per gas + mem::size_of::>() + // blob gas used + mem::size_of::>() + // excess blob gas mem::size_of::>() + // parent beacon block root self.extra_data.len() // extra data } @@ -396,9 +396,9 @@ impl Decodable for Header { receipts_root: Decodable::decode(buf)?, logs_bloom: Decodable::decode(buf)?, difficulty: Decodable::decode(buf)?, - number: U256::decode(buf)?.to::(), - gas_limit: U256::decode(buf)?.to::(), - gas_used: U256::decode(buf)?.to::(), + number: u64::decode(buf)?, + gas_limit: u128::decode(buf)?, + gas_used: u128::decode(buf)?, timestamp: Decodable::decode(buf)?, extra_data: Decodable::decode(buf)?, mix_hash: Decodable::decode(buf)?, @@ -414,7 +414,7 @@ impl Decodable for Header { if buf.first().map(|b| *b == EMPTY_LIST_CODE).unwrap_or_default() { buf.advance(1) } else { - this.base_fee_per_gas = Some(U256::decode(buf)?.to::()); + this.base_fee_per_gas = Some(U256::decode(buf)?.to::()); } } @@ -432,7 +432,7 @@ impl Decodable for Header { if buf.first().map(|b| *b == EMPTY_LIST_CODE).unwrap_or_default() { buf.advance(1) } else { - this.blob_gas_used = Some(U256::decode(buf)?.to::()); + this.blob_gas_used = Some(U256::decode(buf)?.to::()); } } @@ -440,7 +440,7 @@ impl Decodable for Header { if buf.first().map(|b| *b == EMPTY_LIST_CODE).unwrap_or_default() { buf.advance(1) } else { - this.excess_blob_gas = Some(U256::decode(buf)?.to::()); + this.excess_blob_gas = Some(U256::decode(buf)?.to::()); } } diff --git a/crates/consensus/src/receipt/any.rs b/crates/consensus/src/receipt/any.rs index 2fae44a51e1..35482caab59 100644 --- a/crates/consensus/src/receipt/any.rs +++ b/crates/consensus/src/receipt/any.rs @@ -51,7 +51,7 @@ impl AnyReceiptEnvelope { } /// Returns the cumulative gas used at this receipt. - pub const fn cumulative_gas_used(&self) -> u64 { + pub const fn cumulative_gas_used(&self) -> u128 { self.inner.receipt.cumulative_gas_used } diff --git a/crates/consensus/src/receipt/envelope.rs b/crates/consensus/src/receipt/envelope.rs index 0738a0308e4..6890f5a9433 100644 --- a/crates/consensus/src/receipt/envelope.rs +++ b/crates/consensus/src/receipt/envelope.rs @@ -60,7 +60,7 @@ impl ReceiptEnvelope { } /// Returns the cumulative gas used at this receipt. - pub fn cumulative_gas_used(&self) -> u64 { + pub fn cumulative_gas_used(&self) -> u128 { self.as_receipt().unwrap().cumulative_gas_used } diff --git a/crates/consensus/src/receipt/mod.rs b/crates/consensus/src/receipt/mod.rs index 08acf747524..3eda915b193 100644 --- a/crates/consensus/src/receipt/mod.rs +++ b/crates/consensus/src/receipt/mod.rs @@ -25,7 +25,7 @@ pub trait TxReceipt { } /// Returns the cumulative gas used in the block after this transaction was executed. - fn cumulative_gas_used(&self) -> u64; + fn cumulative_gas_used(&self) -> u128; /// Returns the logs emitted by this transaction. fn logs(&self) -> &[Log]; @@ -47,7 +47,7 @@ mod tests { let receipt = ReceiptEnvelope::Legacy(ReceiptWithBloom { receipt: Receipt { - cumulative_gas_used: 0x1u64, + cumulative_gas_used: 0x1u128, logs: vec![Log { address: address!("0000000000000000000000000000000000000011"), data: LogData::new_unchecked( @@ -79,7 +79,7 @@ mod tests { let expected = ReceiptWithBloom { receipt: Receipt { - cumulative_gas_used: 0x1u64, + cumulative_gas_used: 0x1u128, logs: vec![Log { address: address!("0000000000000000000000000000000000000011"), data: LogData::new_unchecked( diff --git a/crates/consensus/src/receipt/receipts.rs b/crates/consensus/src/receipt/receipts.rs index db9eb3afc22..3acd06e5a5c 100644 --- a/crates/consensus/src/receipt/receipts.rs +++ b/crates/consensus/src/receipt/receipts.rs @@ -17,8 +17,8 @@ pub struct Receipt { #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity_bool"))] pub status: bool, /// Gas used - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex"))] - pub cumulative_gas_used: u64, + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + pub cumulative_gas_used: u128, /// Log send from contracts. pub logs: Vec, } @@ -46,7 +46,7 @@ impl TxReceipt for Receipt { self.bloom_slow() } - fn cumulative_gas_used(&self) -> u64 { + fn cumulative_gas_used(&self) -> u128 { self.cumulative_gas_used } @@ -85,7 +85,7 @@ impl TxReceipt for ReceiptWithBloom { Some(self.logs_bloom) } - fn cumulative_gas_used(&self) -> u64 { + fn cumulative_gas_used(&self) -> u128 { self.receipt.cumulative_gas_used } diff --git a/crates/eips/src/eip1559/basefee.rs b/crates/eips/src/eip1559/basefee.rs index 02696d1295b..1794344507f 100644 --- a/crates/eips/src/eip1559/basefee.rs +++ b/crates/eips/src/eip1559/basefee.rs @@ -7,17 +7,19 @@ use crate::eip1559::constants::{ #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct BaseFeeParams { /// The base_fee_max_change_denominator from EIP-1559 - pub max_change_denominator: u64, + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::num::u128_hex_or_decimal"))] + pub max_change_denominator: u128, /// The elasticity multiplier from EIP-1559 - pub elasticity_multiplier: u64, + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::num::u128_hex_or_decimal"))] + pub elasticity_multiplier: u128, } impl BaseFeeParams { /// Get the base fee parameters for Ethereum mainnet pub const fn ethereum() -> BaseFeeParams { BaseFeeParams { - max_change_denominator: DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR, - elasticity_multiplier: DEFAULT_ELASTICITY_MULTIPLIER, + max_change_denominator: DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR as u128, + elasticity_multiplier: DEFAULT_ELASTICITY_MULTIPLIER as u128, } } } diff --git a/crates/eips/src/eip1559/helpers.rs b/crates/eips/src/eip1559/helpers.rs index 98980e2371a..ef7ac11b09e 100644 --- a/crates/eips/src/eip1559/helpers.rs +++ b/crates/eips/src/eip1559/helpers.rs @@ -24,11 +24,11 @@ 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: u64, - gas_limit: u64, - base_fee: u64, + gas_used: u128, + gas_limit: u128, + base_fee: u128, base_fee_params: BaseFeeParams, -) -> u64 { +) -> u128 { // Calculate the target gas by dividing the gas limit by the elasticity multiplier. let gas_target = gas_limit / base_fee_params.elasticity_multiplier; @@ -44,18 +44,17 @@ pub fn calc_next_block_base_fee( + (core::cmp::max( // Ensure a minimum increase of 1. 1, - base_fee as u128 * (gas_used - gas_target) as u128 - / (gas_target as u128 * base_fee_params.max_change_denominator as u128), - ) as u64) + base_fee * (gas_used - gas_target) + / (gas_target * base_fee_params.max_change_denominator), + )) } // If the gas used in the current block is less than the gas target, calculate a new // decreased base fee. core::cmp::Ordering::Less => { // Calculate the decrease in base fee based on the formula defined by EIP-1559. base_fee.saturating_sub( - (base_fee as u128 * (gas_target - gas_used) as u128 - / (gas_target as u128 * base_fee_params.max_change_denominator as u128)) - as u64, + base_fee * (gas_target - gas_used) + / (gas_target * base_fee_params.max_change_denominator), ) } } diff --git a/crates/eips/src/eip4844/mod.rs b/crates/eips/src/eip4844/mod.rs index 3d2fc2322da..0bd1097ab67 100644 --- a/crates/eips/src/eip4844/mod.rs +++ b/crates/eips/src/eip4844/mod.rs @@ -55,7 +55,7 @@ pub const MAX_BLOBS_PER_BLOCK: usize = (MAX_DATA_GAS_PER_BLOCK / DATA_GAS_PER_BL pub const TARGET_BLOBS_PER_BLOCK: u64 = TARGET_DATA_GAS_PER_BLOCK / DATA_GAS_PER_BLOB; // 393216 / 131072 = 3 /// Determines the maximum rate of change for blob fee -pub const BLOB_GASPRICE_UPDATE_FRACTION: u64 = 3_338_477u64; // 3338477 +pub const BLOB_GASPRICE_UPDATE_FRACTION: u128 = 3_338_477u128; // 3338477 /// Minimum gas price for a data blob pub const BLOB_TX_MIN_BLOB_GASPRICE: u128 = 1u128; @@ -80,8 +80,12 @@ pub type Bytes48 = FixedBytes<48>; /// See also [the EIP-4844 helpers](https://eips.ethereum.org/EIPS/eip-4844#helpers) /// (`calc_excess_blob_gas`). #[inline] -pub const fn calc_excess_blob_gas(parent_excess_blob_gas: u64, parent_blob_gas_used: u64) -> u64 { - (parent_excess_blob_gas + parent_blob_gas_used).saturating_sub(TARGET_DATA_GAS_PER_BLOCK) +pub const fn calc_excess_blob_gas( + parent_excess_blob_gas: u128, + parent_blob_gas_used: u128, +) -> u128 { + (parent_excess_blob_gas + parent_blob_gas_used) + .saturating_sub(TARGET_DATA_GAS_PER_BLOCK as u128) } /// Calculates the blob gas price from the header's excess blob gas field. @@ -89,12 +93,8 @@ pub const fn calc_excess_blob_gas(parent_excess_blob_gas: u64, parent_blob_gas_u /// See also [the EIP-4844 helpers](https://eips.ethereum.org/EIPS/eip-4844#helpers) /// (`get_blob_gasprice`). #[inline] -pub fn calc_blob_gasprice(excess_blob_gas: u64) -> u128 { - fake_exponential( - BLOB_TX_MIN_BLOB_GASPRICE as u64, - excess_blob_gas, - BLOB_GASPRICE_UPDATE_FRACTION, - ) +pub fn calc_blob_gasprice(excess_blob_gas: u128) -> u128 { + fake_exponential(BLOB_TX_MIN_BLOB_GASPRICE, excess_blob_gas, BLOB_GASPRICE_UPDATE_FRACTION) } /// Approximates `factor * e ** (numerator / denominator)` using Taylor expansion. @@ -108,11 +108,8 @@ pub fn calc_blob_gasprice(excess_blob_gas: u64) -> u128 { /// /// This function panics if `denominator` is zero. #[inline] -fn fake_exponential(factor: u64, numerator: u64, denominator: u64) -> u128 { +fn fake_exponential(factor: u128, numerator: u128, denominator: u128) -> u128 { assert_ne!(denominator, 0, "attempt to divide by zero"); - let factor = factor as u128; - let numerator = numerator as u128; - let denominator = denominator as u128; let mut i = 1; let mut output = 0; @@ -164,8 +161,8 @@ mod tests { ), (DATA_GAS_PER_BLOB - 1, (TARGET_DATA_GAS_PER_BLOCK / DATA_GAS_PER_BLOB) - 1, 0), ] { - let actual = calc_excess_blob_gas(excess, blobs * DATA_GAS_PER_BLOB); - assert_eq!(actual, expected, "test: {t:?}"); + let actual = calc_excess_blob_gas(excess as u128, (blobs * DATA_GAS_PER_BLOB) as u128); + assert_eq!(actual, expected as u128, "test: {t:?}"); } } @@ -213,9 +210,9 @@ mod tests { (1, 5, 2, 11), // approximate 12.18 (2, 5, 2, 23), // approximate 24.36 (1, 50000000, 2225652, 5709098764), - (1, 380928, BLOB_GASPRICE_UPDATE_FRACTION, 1), + (1, 380928, BLOB_GASPRICE_UPDATE_FRACTION.try_into().unwrap(), 1), ] { - let actual = fake_exponential(factor, numerator, denominator); + let actual = fake_exponential(factor as u128, numerator as u128, denominator as u128); assert_eq!(actual, expected, "test: {t:?}"); } } diff --git a/crates/genesis/src/lib.rs b/crates/genesis/src/lib.rs index 5c448bc2fbd..745f6b8f873 100644 --- a/crates/genesis/src/lib.rs +++ b/crates/genesis/src/lib.rs @@ -24,7 +24,7 @@ use alloc::collections::BTreeMap; use alloy_primitives::{Address, Bytes, B256, U256}; use alloy_serde::{ - num::{u64_hex_or_decimal, u64_hex_or_decimal_opt}, + num::{u128_hex_or_decimal, u128_hex_or_decimal_opt, u64_hex, u64_hex_or_decimal_opt}, storage::deserialize_storage_map, ttd::deserialize_json_ttd_opt, }; @@ -38,16 +38,16 @@ pub struct Genesis { #[serde(default)] pub config: ChainConfig, /// The genesis header nonce. - #[serde(with = "u64_hex_or_decimal")] + #[serde(with = "u64_hex")] pub nonce: u64, /// The genesis header timestamp. - #[serde(with = "u64_hex_or_decimal")] + #[serde(with = "u64_hex")] pub timestamp: u64, /// The genesis header extra data. pub extra_data: Bytes, /// The genesis header gas limit. - #[serde(with = "u64_hex_or_decimal")] - pub gas_limit: u64, + #[serde(with = "u128_hex_or_decimal")] + pub gas_limit: u128, /// The genesis header difficulty. pub difficulty: U256, /// The genesis header mix hash. @@ -64,16 +64,16 @@ pub struct Genesis { // should NOT be set in a real genesis file, but are included here for compatibility with // consensus tests, which have genesis files with these fields populated. /// The genesis header base fee - #[serde(skip_serializing_if = "Option::is_none", with = "u64_hex_or_decimal_opt")] - pub base_fee_per_gas: Option, + #[serde(default, skip_serializing_if = "Option::is_none", with = "u128_hex_or_decimal_opt")] + pub base_fee_per_gas: Option, /// The genesis header excess blob gas - #[serde(skip_serializing_if = "Option::is_none", with = "u64_hex_or_decimal_opt")] - pub excess_blob_gas: Option, + #[serde(default, skip_serializing_if = "Option::is_none", with = "u128_hex_or_decimal_opt")] + pub excess_blob_gas: Option, /// The genesis header blob gas used - #[serde(skip_serializing_if = "Option::is_none", with = "u64_hex_or_decimal_opt")] - pub blob_gas_used: Option, + #[serde(default, skip_serializing_if = "Option::is_none", with = "u128_hex_or_decimal_opt")] + pub blob_gas_used: Option, /// The genesis block number - #[serde(skip_serializing_if = "Option::is_none", with = "u64_hex_or_decimal_opt")] + #[serde(default, skip_serializing_if = "Option::is_none", with = "u64_hex_or_decimal_opt")] pub number: Option, } @@ -158,7 +158,7 @@ impl Genesis { } /// Set the gas limit. - pub const fn with_gas_limit(mut self, gas_limit: u64) -> Self { + pub const fn with_gas_limit(mut self, gas_limit: u128) -> Self { self.gas_limit = gas_limit; self } @@ -182,19 +182,19 @@ impl Genesis { } /// Set the base fee. - pub const fn with_base_fee(mut self, base_fee: Option) -> Self { + pub const fn with_base_fee(mut self, base_fee: Option) -> Self { self.base_fee_per_gas = base_fee; self } /// Set the excess blob gas. - pub const fn with_excess_blob_gas(mut self, excess_blob_gas: Option) -> Self { + pub const fn with_excess_blob_gas(mut self, excess_blob_gas: Option) -> Self { self.excess_blob_gas = excess_blob_gas; self } /// Set the blob gas used. - pub const fn with_blob_gas_used(mut self, blob_gas_used: Option) -> Self { + pub const fn with_blob_gas_used(mut self, blob_gas_used: Option) -> Self { self.blob_gas_used = blob_gas_used; self } diff --git a/crates/provider/src/fillers/gas.rs b/crates/provider/src/fillers/gas.rs index 95bba241776..62f717f3e5a 100644 --- a/crates/provider/src/fillers/gas.rs +++ b/crates/provider/src/fillers/gas.rs @@ -276,7 +276,7 @@ mod tests { let tx = tx.get_receipt().await.unwrap(); assert_eq!(tx.effective_gas_price, 0x3b9aca00); - assert_eq!(tx.gas_used, Some(0x5208)); + assert_eq!(tx.gas_used, 0x5208); } #[tokio::test] @@ -297,7 +297,7 @@ mod tests { let receipt = tx.get_receipt().await.unwrap(); - assert_eq!(receipt.gas_used, Some(0x5208)); + assert_eq!(receipt.gas_used, 0x5208); } #[tokio::test] diff --git a/crates/provider/src/heart.rs b/crates/provider/src/heart.rs index f49744b72b1..0a4591973a4 100644 --- a/crates/provider/src/heart.rs +++ b/crates/provider/src/heart.rs @@ -3,7 +3,7 @@ use crate::{Provider, RootProvider}; use alloy_json_rpc::RpcError; use alloy_network::Network; -use alloy_primitives::{B256, U256}; +use alloy_primitives::B256; use alloy_rpc_types::Block; use alloy_transport::{utils::Spawnable, Transport, TransportErrorKind, TransportResult}; use futures::{stream::StreamExt, FutureExt, Stream}; @@ -363,7 +363,7 @@ pub(crate) struct Heartbeat { unconfirmed: HashMap, /// Ordered map of transactions waiting for confirmations. - waiting_confs: BTreeMap>, + waiting_confs: BTreeMap>, /// Ordered map of transactions to reap at a certain time. reap_at: BTreeMap, @@ -383,8 +383,8 @@ impl + Unpin + 'static> Heartbeat { impl Heartbeat { /// Check if any transactions have enough confirmations to notify. - fn check_confirmations(&mut self, current_height: &U256) { - let to_keep = self.waiting_confs.split_off(&(current_height + U256::from(1))); + fn check_confirmations(&mut self, current_height: u64) { + let to_keep = self.waiting_confs.split_off(&(current_height + 1)); let to_notify = std::mem::replace(&mut self.waiting_confs, to_keep); for watcher in to_notify.into_values().flatten() { watcher.notify(); @@ -444,13 +444,10 @@ impl Heartbeat { } // Otherwise add it to the waiting list. debug!(tx=%watcher.config.tx_hash, %block_height, confirmations, "adding to waiting list"); - self.waiting_confs - .entry(*block_height + U256::from(confirmations) - U256::from(1)) - .or_default() - .push(watcher); + self.waiting_confs.entry(*block_height + confirmations - 1).or_default().push(watcher); } - self.check_confirmations(block_height); + self.check_confirmations(*block_height); // Update the latest block. We use `send_replace` here to ensure the // latest block is always up to date, even if no receivers exist. diff --git a/crates/provider/src/provider.rs b/crates/provider/src/provider.rs index d9e9b065c81..6c4dc15819e 100644 --- a/crates/provider/src/provider.rs +++ b/crates/provider/src/provider.rs @@ -919,7 +919,6 @@ pub trait Provider: .ok_or(RpcError::NullResp)? .header .base_fee_per_gas - .map(|fee| fee.to::()) .ok_or(RpcError::UnsupportedFeature("eip1559"))? } }; @@ -1150,7 +1149,7 @@ mod tests { let mut stream = sub.into_stream().take(2); let mut n = 1; while let Some(block) = stream.next().await { - assert_eq!(block.header.number.unwrap(), U256::from(n)); + assert_eq!(block.header.number.unwrap(), n); assert_eq!(block.transactions.hashes().len(), 0); n += 1; } @@ -1172,7 +1171,7 @@ mod tests { let mut stream = sub.into_stream().take(2); let mut n = 1; while let Some(block) = stream.next().await { - assert_eq!(block.header.number.unwrap(), U256::from(n)); + assert_eq!(block.header.number.unwrap(), n); assert_eq!(block.transactions.hashes().len(), 0); n += 1; } @@ -1192,7 +1191,7 @@ mod tests { let mut stream = sub.into_stream().take(1); while let Some(block) = stream.next().await { println!("New block {:?}", block); - assert!(block.header.number.unwrap() > U256::ZERO); + assert!(block.header.number.unwrap() > 0); } } @@ -1291,7 +1290,7 @@ mod tests { let num = 0; let tag: BlockNumberOrTag = num.into(); let block = provider.get_block_by_number(tag, true).await.unwrap().unwrap(); - assert_eq!(block.header.number.unwrap(), U256::from(num)); + assert_eq!(block.header.number.unwrap(), num); } #[tokio::test] @@ -1302,7 +1301,7 @@ mod tests { let num = 0; let tag: BlockNumberOrTag = num.into(); let block = provider.get_block_by_number(tag, true).await.unwrap().unwrap(); - assert_eq!(block.header.number.unwrap(), U256::from(num)); + assert_eq!(block.header.number.unwrap(), num); } #[tokio::test] diff --git a/crates/rpc-types/src/eth/block.rs b/crates/rpc-types/src/eth/block.rs index 084dcb7ab45..ec02f8e5eb3 100644 --- a/crates/rpc-types/src/eth/block.rs +++ b/crates/rpc-types/src/eth/block.rs @@ -5,7 +5,7 @@ use crate::{other::OtherFields, Transaction, Withdrawal}; use alloy_eips::{calc_blob_gasprice, calc_excess_blob_gas}; use alloy_primitives::{ - ruint::ParseError, Address, BlockHash, BlockNumber, Bloom, Bytes, B256, B64, U256, U64, + ruint::ParseError, Address, BlockHash, BlockNumber, Bloom, Bytes, B256, U256, U64, }; use alloy_rlp::{bytes, Decodable, Encodable, Error as RlpError}; use serde::{ @@ -75,13 +75,17 @@ pub struct Header { /// Difficulty pub difficulty: U256, /// Block number - pub number: Option, + #[serde(default, with = "alloy_serde::num::u64_hex_opt")] + pub number: Option, /// Gas Limit - pub gas_limit: U256, + #[serde(default, with = "alloy_serde::num::u128_hex_or_decimal")] + pub gas_limit: u128, /// Gas Used - pub gas_used: U256, + #[serde(default, with = "alloy_serde::num::u128_hex_or_decimal")] + pub gas_used: u128, /// Timestamp - pub timestamp: U256, + #[serde(default, with = "alloy_serde::num::u64_hex")] + pub timestamp: u64, /// Total difficulty #[serde(skip_serializing_if = "Option::is_none")] pub total_difficulty: Option, @@ -101,19 +105,36 @@ pub struct Header { #[serde(skip_serializing_if = "Option::is_none")] pub mix_hash: Option, /// Nonce - pub nonce: Option, + #[serde( + default, + skip_serializing_if = "Option::is_none", + with = "alloy_serde::num::u64_hex_opt" + )] + pub nonce: Option, /// Base fee per unit of gas (if past London) - #[serde(skip_serializing_if = "Option::is_none")] - pub base_fee_per_gas: Option, + #[serde( + default, + skip_serializing_if = "Option::is_none", + with = "alloy_serde::num::u128_hex_or_decimal_opt" + )] + pub base_fee_per_gas: Option, /// Withdrawals root hash added by EIP-4895 and is ignored in legacy headers. #[serde(skip_serializing_if = "Option::is_none")] pub withdrawals_root: Option, /// Blob gas used - #[serde(skip_serializing_if = "Option::is_none")] - pub blob_gas_used: Option, + #[serde( + default, + skip_serializing_if = "Option::is_none", + with = "alloy_serde::num::u128_hex_or_decimal_opt" + )] + pub blob_gas_used: Option, /// Excess blob gas - #[serde(skip_serializing_if = "Option::is_none")] - pub excess_blob_gas: Option, + #[serde( + default, + skip_serializing_if = "Option::is_none", + with = "alloy_serde::num::u128_hex_or_decimal_opt" + )] + pub excess_blob_gas: Option, /// Parent beacon block root #[serde(skip_serializing_if = "Option::is_none")] pub parent_beacon_block_root: Option, @@ -124,7 +145,7 @@ impl Header { /// /// Returns `None` if `excess_blob_gas` is None pub fn blob_fee(&self) -> Option { - self.excess_blob_gas.map(|gas| calc_blob_gasprice(gas.to())) + self.excess_blob_gas.map(calc_blob_gasprice) } /// Returns the blob fee for the next block according to the EIP-4844 spec. @@ -140,8 +161,8 @@ impl Header { /// spec. /// /// Returns a `None` if no excess blob gas is set, no EIP-4844 support - pub fn next_block_excess_blob_gas(&self) -> Option { - Some(calc_excess_blob_gas(self.excess_blob_gas?.to(), self.blob_gas_used?.to())) + pub fn next_block_excess_blob_gas(&self) -> Option { + Some(calc_excess_blob_gas(self.excess_blob_gas?, self.blob_gas_used?)) } } @@ -1172,17 +1193,17 @@ mod tests { transactions_root: B256::with_last_byte(6), receipts_root: B256::with_last_byte(7), withdrawals_root: Some(B256::with_last_byte(8)), - number: Some(U256::from(9)), - gas_used: U256::from(10), - gas_limit: U256::from(11), + number: Some(9), + gas_used: 10, + gas_limit: 11, extra_data: Bytes::from(vec![1, 2, 3]), logs_bloom: Bloom::default(), - timestamp: U256::from(12), + timestamp: 12, difficulty: U256::from(13), total_difficulty: Some(U256::from(100000)), mix_hash: Some(B256::with_last_byte(14)), - nonce: Some(B64::with_last_byte(15)), - base_fee_per_gas: Some(U256::from(20)), + nonce: Some(15), + base_fee_per_gas: Some(20), blob_gas_used: None, excess_blob_gas: None, parent_beacon_block_root: None, @@ -1196,7 +1217,7 @@ mod tests { let serialized = serde_json::to_string(&block).unwrap(); assert_eq!( serialized, - r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000002","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000003","miner":"0x0000000000000000000000000000000000000004","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000005","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000006","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000007","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0xd","number":"0x9","gasLimit":"0xb","gasUsed":"0xa","timestamp":"0xc","totalDifficulty":"0x186a0","extraData":"0x010203","mixHash":"0x000000000000000000000000000000000000000000000000000000000000000e","nonce":"0x000000000000000f","baseFeePerGas":"0x14","withdrawalsRoot":"0x0000000000000000000000000000000000000000000000000000000000000008","uncles":["0x0000000000000000000000000000000000000000000000000000000000000011"],"transactions":["0x0000000000000000000000000000000000000000000000000000000000000012"],"size":"0x13","withdrawals":[]}"# + r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000002","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000003","miner":"0x0000000000000000000000000000000000000004","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000005","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000006","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000007","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0xd","number":"0x9","gasLimit":"0xb","gasUsed":"0xa","timestamp":"0xc","totalDifficulty":"0x186a0","extraData":"0x010203","mixHash":"0x000000000000000000000000000000000000000000000000000000000000000e","nonce":"0xf","baseFeePerGas":"0x14","withdrawalsRoot":"0x0000000000000000000000000000000000000000000000000000000000000008","uncles":["0x0000000000000000000000000000000000000000000000000000000000000011"],"transactions":["0x0000000000000000000000000000000000000000000000000000000000000012"],"size":"0x13","withdrawals":[]}"# ); let deserialized: Block = serde_json::from_str(&serialized).unwrap(); assert_eq!(block, deserialized); @@ -1214,17 +1235,17 @@ mod tests { transactions_root: B256::with_last_byte(6), receipts_root: B256::with_last_byte(7), withdrawals_root: Some(B256::with_last_byte(8)), - number: Some(U256::from(9)), - gas_used: U256::from(10), - gas_limit: U256::from(11), + number: Some(9), + gas_used: 10, + gas_limit: 11, extra_data: Bytes::from(vec![1, 2, 3]), logs_bloom: Bloom::default(), - timestamp: U256::from(12), + timestamp: 12, difficulty: U256::from(13), total_difficulty: Some(U256::from(100000)), mix_hash: Some(B256::with_last_byte(14)), - nonce: Some(B64::with_last_byte(15)), - base_fee_per_gas: Some(U256::from(20)), + nonce: Some(15), + base_fee_per_gas: Some(20), blob_gas_used: None, excess_blob_gas: None, parent_beacon_block_root: None, @@ -1238,7 +1259,7 @@ mod tests { let serialized = serde_json::to_string(&block).unwrap(); assert_eq!( serialized, - r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000002","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000003","miner":"0x0000000000000000000000000000000000000004","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000005","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000006","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000007","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0xd","number":"0x9","gasLimit":"0xb","gasUsed":"0xa","timestamp":"0xc","totalDifficulty":"0x186a0","extraData":"0x010203","mixHash":"0x000000000000000000000000000000000000000000000000000000000000000e","nonce":"0x000000000000000f","baseFeePerGas":"0x14","withdrawalsRoot":"0x0000000000000000000000000000000000000000000000000000000000000008","uncles":[],"size":"0x13"}"# + r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000002","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000003","miner":"0x0000000000000000000000000000000000000004","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000005","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000006","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000007","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0xd","number":"0x9","gasLimit":"0xb","gasUsed":"0xa","timestamp":"0xc","totalDifficulty":"0x186a0","extraData":"0x010203","mixHash":"0x000000000000000000000000000000000000000000000000000000000000000e","nonce":"0xf","baseFeePerGas":"0x14","withdrawalsRoot":"0x0000000000000000000000000000000000000000000000000000000000000008","uncles":[],"size":"0x13"}"# ); let deserialized: Block = serde_json::from_str(&serialized).unwrap(); assert_eq!(block, deserialized); @@ -1256,17 +1277,17 @@ mod tests { transactions_root: B256::with_last_byte(6), receipts_root: B256::with_last_byte(7), withdrawals_root: None, - number: Some(U256::from(9)), - gas_used: U256::from(10), - gas_limit: U256::from(11), + number: Some(9), + gas_used: 10, + gas_limit: 11, extra_data: Bytes::from(vec![1, 2, 3]), logs_bloom: Bloom::default(), - timestamp: U256::from(12), + timestamp: 12, difficulty: U256::from(13), total_difficulty: Some(U256::from(100000)), mix_hash: Some(B256::with_last_byte(14)), - nonce: Some(B64::with_last_byte(15)), - base_fee_per_gas: Some(U256::from(20)), + nonce: Some(15), + base_fee_per_gas: Some(20), blob_gas_used: None, excess_blob_gas: None, parent_beacon_block_root: None, @@ -1280,7 +1301,7 @@ mod tests { let serialized = serde_json::to_string(&block).unwrap(); assert_eq!( serialized, - r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000002","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000003","miner":"0x0000000000000000000000000000000000000004","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000005","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000006","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000007","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0xd","number":"0x9","gasLimit":"0xb","gasUsed":"0xa","timestamp":"0xc","totalDifficulty":"0x186a0","extraData":"0x010203","mixHash":"0x000000000000000000000000000000000000000000000000000000000000000e","nonce":"0x000000000000000f","baseFeePerGas":"0x14","uncles":["0x0000000000000000000000000000000000000000000000000000000000000011"],"transactions":["0x0000000000000000000000000000000000000000000000000000000000000012"],"size":"0x13"}"# + r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000002","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000003","miner":"0x0000000000000000000000000000000000000004","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000005","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000006","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000007","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0xd","number":"0x9","gasLimit":"0xb","gasUsed":"0xa","timestamp":"0xc","totalDifficulty":"0x186a0","extraData":"0x010203","mixHash":"0x000000000000000000000000000000000000000000000000000000000000000e","nonce":"0xf","baseFeePerGas":"0x14","uncles":["0x0000000000000000000000000000000000000000000000000000000000000011"],"transactions":["0x0000000000000000000000000000000000000000000000000000000000000012"],"size":"0x13"}"# ); let deserialized: Block = serde_json::from_str(&serialized).unwrap(); assert_eq!(block, deserialized); diff --git a/crates/rpc-types/src/eth/transaction/common.rs b/crates/rpc-types/src/eth/transaction/common.rs index b6217bdb82f..dd00fee8d74 100644 --- a/crates/rpc-types/src/eth/transaction/common.rs +++ b/crates/rpc-types/src/eth/transaction/common.rs @@ -15,5 +15,5 @@ pub struct TransactionInfo { /// Number of the block. pub block_number: Option, /// Base fee of the block. - pub base_fee: Option, + pub base_fee: Option, } diff --git a/crates/rpc-types/src/eth/transaction/optimism.rs b/crates/rpc-types/src/eth/transaction/optimism.rs index cbc8b6594ad..2d44ae4e3e7 100644 --- a/crates/rpc-types/src/eth/transaction/optimism.rs +++ b/crates/rpc-types/src/eth/transaction/optimism.rs @@ -1,7 +1,7 @@ //! Misc Optimism-specific types. use crate::other::OtherFields; -use alloy_primitives::{B256, U128, U256, U64}; +use alloy_primitives::{B256, U128, U64}; use serde::{Deserialize, Serialize}; /// Optimism specific transaction fields @@ -30,17 +30,29 @@ pub struct OptimismTransactionReceiptFields { #[serde(skip_serializing_if = "Option::is_none")] pub deposit_receipt_version: Option, /// L1 fee for the transaction - #[serde(skip_serializing_if = "Option::is_none")] - pub l1_fee: Option, + #[serde( + default, + skip_serializing_if = "Option::is_none", + with = "alloy_serde::num::u128_hex_or_decimal_opt" + )] + pub l1_fee: Option, /// L1 fee scalar for the transaction #[serde(default, skip_serializing_if = "Option::is_none", with = "l1_fee_scalar_serde")] pub l1_fee_scalar: Option, /// L1 gas price for the transaction - #[serde(skip_serializing_if = "Option::is_none")] - pub l1_gas_price: Option, + #[serde( + default, + skip_serializing_if = "Option::is_none", + with = "alloy_serde::num::u128_hex_or_decimal_opt" + )] + pub l1_gas_price: Option, /// L1 gas used for the transaction - #[serde(skip_serializing_if = "Option::is_none")] - pub l1_gas_used: Option, + #[serde( + default, + skip_serializing_if = "Option::is_none", + with = "alloy_serde::num::u128_hex_or_decimal_opt" + )] + pub l1_gas_used: Option, } impl From for OtherFields { diff --git a/crates/rpc-types/src/eth/transaction/receipt.rs b/crates/rpc-types/src/eth/transaction/receipt.rs index b055ff56e8f..b51cd8c2e0e 100644 --- a/crates/rpc-types/src/eth/transaction/receipt.rs +++ b/crates/rpc-types/src/eth/transaction/receipt.rs @@ -28,21 +28,29 @@ pub struct TransactionReceipt> { #[serde(with = "alloy_serde::u64_hex_opt")] pub block_number: Option, /// Gas used by this transaction alone. - #[serde(with = "alloy_serde::u64_hex_opt")] - pub gas_used: Option, + #[serde(with = "alloy_serde::u128_hex_or_decimal")] + pub gas_used: u128, /// The price paid post-execution by the transaction (i.e. base fee + priority fee). Both /// fields in 1559-style transactions are maximums (max fee + max priority fee), the amount /// that's actually paid by users can only be determined post-execution - #[serde(with = "alloy_serde::u64_hex")] - pub effective_gas_price: u64, + #[serde(with = "alloy_serde::u128_hex_or_decimal")] + pub effective_gas_price: u128, /// Blob gas used by the eip-4844 transaction /// /// This is None for non eip-4844 transactions - #[serde(skip_serializing_if = "Option::is_none", with = "alloy_serde::u64_hex_opt", default)] - pub blob_gas_used: Option, + #[serde( + skip_serializing_if = "Option::is_none", + with = "alloy_serde::u128_hex_or_decimal_opt", + default + )] + pub blob_gas_used: Option, /// The price paid by the eip-4844 transaction per blob gas. - #[serde(skip_serializing_if = "Option::is_none", with = "alloy_serde::u64_hex_opt", default)] - pub blob_gas_price: Option, + #[serde( + skip_serializing_if = "Option::is_none", + with = "alloy_serde::u128_hex_or_decimal_opt", + default + )] + pub blob_gas_price: Option, /// Address of the sender pub from: Address, /// Address of the receiver. None when its a contract creation transaction. @@ -146,7 +154,7 @@ mod test { ); const EXPECTED_BLOOM: Bloom = bloom!("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000200000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000800000000000000000000000000000000004000000000000000000800000000100000020000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000010000000000000000000000000000"); - const EXPECTED_CGU: u64 = 0xa42aec; + const EXPECTED_CGU: u128 = 0xa42aec; assert!(matches!( receipt.inner, diff --git a/crates/rpc-types/src/eth/transaction/request.rs b/crates/rpc-types/src/eth/transaction/request.rs index f47178ba5c7..513675fce30 100644 --- a/crates/rpc-types/src/eth/transaction/request.rs +++ b/crates/rpc-types/src/eth/transaction/request.rs @@ -18,18 +18,35 @@ pub struct TransactionRequest { /// The destination address of the transaction. pub to: Option
, /// The legacy gas price. - #[serde(default)] + #[serde( + default, + skip_serializing_if = "Option::is_none", + with = "alloy_serde::num::u128_hex_or_decimal_opt" + )] pub gas_price: Option, /// The max base fee per gas the sender is willing to pay. - #[serde(default)] + #[serde( + default, + skip_serializing_if = "Option::is_none", + with = "alloy_serde::num::u128_hex_or_decimal_opt" + )] pub max_fee_per_gas: Option, /// The max priority fee per gas the sender is willing to pay, also called the miner tip. - #[serde(default)] + #[serde( + default, + skip_serializing_if = "Option::is_none", + with = "alloy_serde::num::u128_hex_or_decimal_opt" + )] pub max_priority_fee_per_gas: Option, /// The max fee per blob gas for EIP-4844 blob transactions. - #[serde(skip_serializing_if = "Option::is_none")] + #[serde( + default, + skip_serializing_if = "Option::is_none", + with = "alloy_serde::num::u128_hex_or_decimal_opt" + )] pub max_fee_per_blob_gas: Option, /// The gas limit for the transaction. + #[serde(default, with = "alloy_serde::num::u128_hex_or_decimal_opt")] pub gas: Option, /// The value transferred in the transaction, in wei. pub value: Option,