Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions actuator/src/main/java/org/tron/core/vm/EnergyCost.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,20 +367,18 @@ public static long getVoteWitnessCost(Program program) {
public static long getVoteWitnessCost2(Program program) {
Stack stack = program.getStack();
long oldMemSize = program.getMemSize();
DataWord amountArrayLength = stack.get(stack.size() - 1).clone();
DataWord amountArrayOffset = stack.get(stack.size() - 2);
DataWord witnessArrayLength = stack.get(stack.size() - 3).clone();
DataWord witnessArrayOffset = stack.get(stack.size() - 4);
BigInteger amountArrayLength = stack.get(stack.size() - 1).value();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why it was changed from DataWord to BigInteger;
And the PR title doesn't match the code format modifications, so relevant details should be supplemented.

Copy link

Choose a reason for hiding this comment

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

Inlining memNeeded() is fine for readability, but can you confirm it didn't contain any boundary checks we're now missing. Also, verify that BigInteger produces identical results to DataWord for edge cases (very large value), since their overflow behavior differs.

I also think this PR need more relavant details for review.

BigInteger amountArrayOffset = stack.get(stack.size() - 2).value();
BigInteger witnessArrayLength = stack.get(stack.size() - 3).value();
BigInteger witnessArrayOffset = stack.get(stack.size() - 4).value();

DataWord wordSize = new DataWord(DataWord.WORD_SIZE);
BigInteger wordSize = BigInteger.valueOf(DataWord.WORD_SIZE);

amountArrayLength.mul(wordSize);
amountArrayLength.add(wordSize); // dynamic array length is at least 32 bytes
BigInteger amountArrayMemoryNeeded = memNeeded(amountArrayOffset, amountArrayLength);
BigInteger amountArrayMemoryNeeded =
amountArrayLength.multiply(wordSize).add(wordSize).add(amountArrayOffset);

witnessArrayLength.mul(wordSize);
witnessArrayLength.add(wordSize); // dynamic array length is at least 32 bytes
BigInteger witnessArrayMemoryNeeded = memNeeded(witnessArrayOffset, witnessArrayLength);
BigInteger witnessArrayMemoryNeeded =
witnessArrayLength.multiply(wordSize).add(wordSize).add(witnessArrayOffset);

return VOTE_WITNESS + calcMemEnergy(oldMemSize,
(amountArrayMemoryNeeded.compareTo(witnessArrayMemoryNeeded) > 0
Expand Down
Loading