Skip to content

Commit 81cfba2

Browse files
committed
fix(vm): correct parens in RepositoryImpl.calculateGlobalEnergyLimit
1 parent df53cd9 commit 81cfba2

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

actuator/src/main/java/org/tron/core/vm/repository/RepositoryImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,8 +977,8 @@ public long calculateGlobalEnergyLimit(AccountCapsule accountCapsule) {
977977

978978
if (hardenResourceCalculation()) {
979979
return BigInteger.valueOf(energyWeight)
980-
.multiply(BigInteger.valueOf(totalEnergyLimit)
981-
.divide(BigInteger.valueOf(totalEnergyWeight)))
980+
.multiply(BigInteger.valueOf(totalEnergyLimit))
981+
.divide(BigInteger.valueOf(totalEnergyWeight))
982982
.longValueExact();
983983
}
984984
return (long) (energyWeight * ((double) totalEnergyLimit / totalEnergyWeight));

framework/src/test/java/org/tron/core/vm/repository/RepositoryImplHardenTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.tron.core.vm.repository;
22

3+
import com.google.protobuf.ByteString;
34
import java.lang.reflect.Method;
45
import lombok.extern.slf4j.Slf4j;
56
import org.junit.After;
@@ -8,9 +9,13 @@
89
import org.junit.Test;
910
import org.tron.common.BaseTest;
1011
import org.tron.common.TestConstants;
12+
import org.tron.common.utils.ByteArray;
13+
import org.tron.core.Wallet;
14+
import org.tron.core.capsule.AccountCapsule;
1115
import org.tron.core.config.args.Args;
1216
import org.tron.core.store.StoreFactory;
1317
import org.tron.core.vm.config.VMConfig;
18+
import org.tron.protos.Protocol.AccountType;
1419

1520
@Slf4j
1621
public class RepositoryImplHardenTest extends BaseTest {
@@ -218,4 +223,58 @@ public void testUsageToBalanceOverflowDetectedWithHardening() {
218223
Assert.assertThrows(ArithmeticException.class,
219224
() -> invokeUsageToBalance(usage, totalWeight, totalLimit));
220225
}
226+
227+
@Test
228+
public void testCalculateGlobalEnergyLimitHardenedParityWithNonIntegerRatio() {
229+
long totalEnergyLimit = 50_000_000_000L;
230+
long totalEnergyWeight = 1_234_567L;
231+
long frozeBalance = 10_000_000_000L;
232+
233+
dbManager.getDynamicPropertiesStore().saveTotalEnergyCurrentLimit(totalEnergyLimit);
234+
dbManager.getDynamicPropertiesStore().saveTotalEnergyWeight(totalEnergyWeight);
235+
236+
AccountCapsule account = new AccountCapsule(
237+
ByteString.copyFromUtf8("owner"),
238+
ByteString.copyFrom(ByteArray.fromHexString(
239+
Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc")),
240+
AccountType.Normal, 0L);
241+
account.setFrozenForEnergy(frozeBalance, 0L);
242+
243+
VMConfig.initAllowHardenResourceCalculation(0);
244+
long resultOld = repository.calculateGlobalEnergyLimit(account);
245+
246+
VMConfig.initAllowHardenResourceCalculation(1);
247+
long resultNew = repository.calculateGlobalEnergyLimit(account);
248+
249+
long expected = java.math.BigInteger.valueOf(10000L)
250+
.multiply(java.math.BigInteger.valueOf(totalEnergyLimit))
251+
.divide(java.math.BigInteger.valueOf(totalEnergyWeight))
252+
.longValueExact();
253+
Assert.assertEquals(expected, resultNew);
254+
Assert.assertEquals(resultOld, resultNew);
255+
256+
long buggy = 10000L * (totalEnergyLimit / totalEnergyWeight);
257+
Assert.assertNotEquals(buggy, resultNew);
258+
}
259+
260+
@Test
261+
public void testCalculateGlobalEnergyLimitHardenedOverflowDetected() {
262+
long totalEnergyLimit = Long.MAX_VALUE / 2;
263+
long totalEnergyWeight = 1L;
264+
long frozeBalance = Long.MAX_VALUE / 4;
265+
266+
dbManager.getDynamicPropertiesStore().saveTotalEnergyCurrentLimit(totalEnergyLimit);
267+
dbManager.getDynamicPropertiesStore().saveTotalEnergyWeight(totalEnergyWeight);
268+
269+
AccountCapsule account = new AccountCapsule(
270+
ByteString.copyFromUtf8("owner"),
271+
ByteString.copyFrom(ByteArray.fromHexString(
272+
Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc")),
273+
AccountType.Normal, 0L);
274+
account.setFrozenForEnergy(frozeBalance, 0L);
275+
276+
VMConfig.initAllowHardenResourceCalculation(1);
277+
Assert.assertThrows(ArithmeticException.class,
278+
() -> repository.calculateGlobalEnergyLimit(account));
279+
}
221280
}

0 commit comments

Comments
 (0)