Skip to content

refactor(vm): optimize opcode energy cost calculation with BigInteger#6613

Merged
CodeNinjaEvan merged 2 commits intotronprotocol:developfrom
yanghang8612:optimize/energy-calculation
Apr 9, 2026
Merged

refactor(vm): optimize opcode energy cost calculation with BigInteger#6613
CodeNinjaEvan merged 2 commits intotronprotocol:developfrom
yanghang8612:optimize/energy-calculation

Conversation

@yanghang8612
Copy link
Copy Markdown
Collaborator

Summary

  • Use BigInteger instead of DataWord for memory size computation in energy cost calculation to improve precision
  • Add allowTvmOsaka proposal config in VMConfig for future activation control
  • Add unit tests for the optimized calculation

Use BigInteger for memory size computation to improve precision and add unit tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
// witness array zero, amount array non-zero
Program program = mockProgram(0, 0, 64, 1, 0);
long cost = EnergyCost.getVoteWitnessCost3(program);
// witnessMemNeeded = 0 (size is zero)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The unit tests in VoteWitnessCost3Test are well-structured — especially appreciate the overflow boundary coverage in testLargeArrayLengthOverflow and testLargeOffsetOverflow, and the consistency check against cost2 in testConsistentWithCost2ForSmallValues. One minor note: the comment in testZeroLengthOneArray states witnessMemNeeded = 0, but cost3 computes size = 0 * 32 + 32 = 32, which is non-zero — worth clarifying if this is an intentional behavior change from cost2.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good catch! The comment was indeed inaccurate — in cost3, even when arrayLength is 0, the size is computed as 0 * 32 + 32 = 32 (accounting for the dynamic array length prefix), so witnessMemNeeded is 32, not 0. This is an intentional behavior difference from cost2, where a zero-length DataWord after mul stays zero and memNeeded returns 0. Fixed the comment in the latest push. Thanks for the review!

@kuny0707 kuny0707 requested review from CodeNinjaEvan and Federico2014 and removed request for CodeNinjaEvan April 6, 2026 08:47
@halibobo1205 halibobo1205 added this to the GreatVoyage-v4.8.2 milestone Apr 8, 2026
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@tronprotocol tronprotocol deleted a comment from lily309 Apr 9, 2026
Copy link
Copy Markdown
Collaborator

@CodeNinjaEvan CodeNinjaEvan left a comment

Choose a reason for hiding this comment

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

LGTM

@lvs0075 lvs0075 requested a review from CodeNinjaEvan April 9, 2026 09:52
@halibobo1205 halibobo1205 added the topic:vm VM, smart contract label Apr 9, 2026
@CodeNinjaEvan CodeNinjaEvan merged commit 1ae9bb3 into tronprotocol:develop Apr 9, 2026
12 checks passed
@github-project-automation github-project-automation bot moved this to Done in java-tron Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:vm VM, smart contract

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants