fix(chainbase): fix divide-by-zero and volatile head#6687
Open
Little-Peony wants to merge 5 commits intotronprotocol:developfrom
Open
fix(chainbase): fix divide-by-zero and volatile head#6687Little-Peony wants to merge 5 commits intotronprotocol:developfrom
Little-Peony wants to merge 5 commits intotronprotocol:developfrom
Conversation
… loading Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
alan-eth
reviewed
Apr 16, 2026
| // Calc new usage by fixed x-axes | ||
| long newUsage = max(0, newArea / newSize, dynamicPropertiesStore.disableJavaLangMath()); | ||
| // A zero window size means no valid time window exists and thus zero usage | ||
| long newUsage = newSize == 0 ? 0 |
There was a problem hiding this comment.
The zero-division guard is well-reasoned — newSize can indeed be 0 when mergedSize == currentSize and size from the receipt is 0, even though getWindowSize() itself never returns 0. Great job tracing the actual root cause rather than just adding a blanket check! 👏
Since this is a subtle edge case that's easy to regress on, would it be worth adding a unit test in TransactionTraceTest that exercises the newSize == 0 path? This would also serve as documentation for future readers about when this condition can occur.
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.
What does this PR do?
Fix audit warnings in energy-related code.
Changes
TransactionTrace: guardnewArea / newSizewith anewSize == 0check inresetAccountUsage. A zero window size means no valid time window exists, so usage should be 0 rather than dividing by zero. Also fixes a minor comment typo (pre consumed→preconsumed).Chainbase: mark theheadfield asvolatileto ensure correct cross-thread visibility when the head snapshot is updated.Test plan
newSize) path