Skip to content

fix(chainbase): fix divide-by-zero and volatile head#6687

Open
Little-Peony wants to merge 5 commits intotronprotocol:developfrom
Little-Peony:cherry_pick_6620
Open

fix(chainbase): fix divide-by-zero and volatile head#6687
Little-Peony wants to merge 5 commits intotronprotocol:developfrom
Little-Peony:cherry_pick_6620

Conversation

@Little-Peony
Copy link
Copy Markdown

@Little-Peony Little-Peony commented Apr 16, 2026

What does this PR do?

Fix audit warnings in energy-related code.

Changes

  • TransactionTrace: guard newArea / newSize with a newSize == 0 check in resetAccountUsage. 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 consumedpreconsumed).
  • Chainbase: mark the head field as volatile to ensure correct cross-thread visibility when the head snapshot is updated.

Test plan

  • Existing tests pass — no behaviour change for the normal (non-zero newSize) path

… loading

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Little-Peony Little-Peony changed the title fix(api): add whitelist rate limiter class to prevent arbitrary class loading fix(http): replace Class.forName with whitelist in RateLimiterServlet Apr 16, 2026
@halibobo1205 halibobo1205 added topic:DB Database topic:api rpc/http related issue labels Apr 16, 2026
@halibobo1205 halibobo1205 added this to the GreatVoyage-v4.8.2 milestone Apr 16, 2026
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Little-Peony Little-Peony changed the title fix(http): replace Class.forName with whitelist in RateLimiterServlet fix(chainbase): fix divide-by-zero and volatile head in energy calculation Apr 16, 2026
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Sunny6889 Sunny6889 removed the topic:api rpc/http related issue label Apr 16, 2026
@Sunny6889 Sunny6889 changed the title fix(chainbase): fix divide-by-zero and volatile head in energy calculation fix(chainbase): fix divide-by-zero and volatile head 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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:DB Database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants