fix: use checked arithmetic for cumulative gas accounting in payload builder#42
Open
Himess wants to merge 2 commits intocirclefin:mainfrom
Open
fix: use checked arithmetic for cumulative gas accounting in payload builder#42Himess wants to merge 2 commits intocirclefin:mainfrom
Himess wants to merge 2 commits intocirclefin:mainfrom
Conversation
…builder Use checked_add and saturating_add for cumulative gas tracking to prevent potential u64 overflow, consistent with the defensive arithmetic pattern applied to reward_beneficiary in circlefin#21.
…review Per reviewer feedback, saturating_add silently clamps at u64::MAX which would corrupt cumulative_gas_used rather than fail the block build cleanly. Switch to checked_add with PayloadBuilderError propagation, matching the defensive pattern used at the capacity check on line 579.
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.
Context
Reopen of #24, which was auto-closed after the main branch reset (per @ZhiyuCircle's note on that PR). Rebased onto the new
origin/mainvia cherry-pick since the original base commit no longer exists.Summary
Replaces
saturating_addwith checked arithmetic in two places inpayload.rsso that u64 gas overflows surface as build failures instead of silently clamping:Capacity check (line 578) —
cumulative_gas_used.checked_add(pool_tx.gas_limit()).is_none_or(|total| total > block_gas_limit). Defensive hardening — per @atiwari-circle's review overflow here is not practically reachable (both sides bounded by block_gas_limit ~30M), butchecked_addis the semantically correct primitive.Cumulative update (line 665) —
cumulative_gas_used.checked_add(gas_used).ok_or_else(|| PayloadBuilderError::other(...))?. This was the line called out as incorrect by @atiwari-circle in fix: use checked arithmetic for cumulative gas accounting in payload builder #24: a silent clamp atu64::MAXwould let the payload builder continue past the block gas limit. Overflow is now propagated viaPayloadBuilderError.Links to original discussion: #24