Repository navigation
fix(consensus-any): saturate baseFeePerGas above u64::MAX on deser (#3741) - #3976
Merged
mattsse merged 2 commits intoMay 12, 2026
Merged
Conversation
…lloy-rs#3741) The Ethereum execution spec defines base_fee_per_gas as arbitrary-precision (Uint), but alloy types AnyHeader::base_fee_per_gas as Option<u64> for compatibility with Ethereum mainnet, which has never observed a value above u64::MAX. Some EVM-compatible chains do — Stable (chain id 988) returns baseFeePerGas = 0x3635c9adc5dea00000 (10^21) for a range of historical blocks, causing RPC deserialization to fail with: invalid type: integer `1000000000000000000000`, expected u64 Per the maintainer-blessed direction in the issue: keep Option<u64> typing and saturate above u64::MAX on deserialize instead of erroring. Custom serde adapter saturating_base_fee_per_gas reuses alloy_serde::quantity::opt for serialize (no behavioral change on output) and parses through Option<U256> on deserialize with try_into falling back to u64::MAX. Tests cover: - in-range hex (1 Gwei) - saturating above u64::MAX (Stable chain 10^21 case) - exactly u64::MAX (no truncation) - absent (preserves None for pre-London headers) Consumers needing full precision should construct a custom header type with a wider field; this is documented in the field-level doc. Closes alloy-rs#3741
satyakwok
requested review from
DaniPopes,
grandizzy,
klkvr,
mattsse,
onbjerg and
zerosnacks
as code owners
May 11, 2026 18:52
mattsse
approved these changes
May 12, 2026
mattsse
left a comment
Member
There was a problem hiding this comment.
Looks good now. I pushed a follow-up commit to keep the saturating path limited to human-readable serde, preserve the existing binary serde shape, and cover that with a bincode regression test.
Contributor
Author
|
Sharp split — JSON sees untrusted RPC where saturate is the sane fallback, but binary overflow inside our own storage = corruption bug, fail-loud beats silent-clamp. Good call on the bincode regression test pinning that boundary; would've drifted quietly without it. |
2 of 3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #3741.
Bug
The Ethereum execution spec defines
base_fee_per_gasas arbitrary-precision (Uint), but alloy typesAnyHeader::base_fee_per_gasasOption<u64>for compatibility with Ethereum mainnet, which has never observed a value aboveu64::MAX. Some EVM-compatible chains do — Stable (chain id 988) returnsbaseFeePerGas = 0x3635c9adc5dea00000(10²¹) for a range of historical blocks, causing RPC deserialization to fail with:Fix
Per maintainer-blessed direction (@mattsse in the issue thread):
Keep
Option<u64>typing and saturate aboveu64::MAXon deserialize instead of erroring. Newsaturating_base_fee_per_gasserde adapter:alloy_serde::quantity::opt::serialize— no behavioral change on outputOption<U256>thentry_into::<u64>()withunwrap_or(u64::MAX)— saturates oversize, preserves NoneField-level doc records the rationale + points consumers needing full precision toward a custom header type.
Tests
Four new sub-tests under
block::header::tests:deserializes_base_fee_within_u640x3b9aca00(1 gwei)Some(1_000_000_000)deserializes_base_fee_saturates_above_u640x3635c9adc5dea00000(Stable chain 10²¹)Some(u64::MAX)deserializes_base_fee_exact_u64_max0xffffffffffffffffSome(u64::MAX)(no truncation)deserializes_base_fee_absentNone(pre-London headers)(Pre-existing
deserializes_tron_state_root_in_headercontinues to pass.)Compatibility
None.u64::MAX— consumers that need the full-precision value should construct a custom header type with a wider field (now documented at the field level).Open questions
blob_gas_used/excess_blob_gas? The execution spec lists those asU64(notUint), so the spec-aligned answer is no — the strict u64 typing matches intent. Happy to follow up if the team wants those relaxed too.