fix(eip1559): prevent divide-by-zero in next base fee calculation#3884
fix(eip1559): prevent divide-by-zero in next base fee calculation#3884InoMurko wants to merge 4 commits intoalloy-rs:mainfrom
Conversation
|
@DaniPopes any interest in reviewing this? |
|
base fee params shouldnt be 0 to begin with, but sure i guess. please remove "op reth" mentions from pr, there's no optimism code in here. |
Fixed! |
b5cad38 to
7cf190a
Compare
|
rebased and ready to be reviewed @DaniPopes! |
7cf190a to
82b5807
Compare
|
@DaniPopes can this get reviewed? |
|
@DaniPopes any interest in reviewing this? |
|
I couldn’t push the formatting fix directly here because maintainer edits are disabled on the source branch. I opened an upstream replacement with the same changes plus the rustfmt fix: #3951. |
|
Closed as superseded by #3951, which has been merged. |
Sounds good. Thanks everyone! |
Summary
Hardens EIP-1559 next-base-fee calculation against divide-by-zero when elasticity, max-change denominator, or effective gas target is zero, so callers do not panic on degenerate parameters.
Problem
calc_next_block_base_feedivides bygas_target * max_change_denominator(and computesgas_targetasgas_limit / elasticity). Any of the following can make that denominator zero and panic in debug/release:elasticity_multiplier == 0ormax_change_denominator == 0gas_target == 0withgas_used > 0(e.g.gas_limit < elasticityon chains that do not enforce the same gas-limit rules as L1)Callers that build
BaseFeeParamsfrom external or misconfigured values could pass a struct with a zero field even when the other is non-zero; that still flows intocalc_next_block_base_feeand can trigger the same failure.Separately, node logs showing
base_fee=0.00Gweifor very small base fees (e.g. 50 wei) are often a display rounding artifact ({:.2}Gwei), not necessarily a zero base fee in the header.Solution
At the start of
calc_next_block_base_fee, return the parentbase_feeunchanged whenelasticity == 0,max_change_denominator == 0, orgas_target == 0(usingu128division forgas_limit / elasticityto avoid overflow/truncation surprises). This matches the defensive behavior of Nethermind’sDefaultBaseFeeCalculatorforparentGasTarget == 0/ zero denominator.Testing
gas_limit < elasticitywithgas_used > 0Notes
This is defense in depth: correct chain config and valid inputs remain the source of truth; these guards prevent crashes and align with other clients’ safe fallbacks for degenerate inputs.
Related
DefaultBaseFeeCalculator(parentGasTarget == 0/ zero denominator → unchanged parent base fee)