|
1 | 1 | package org.tron.core.vm.repository; |
2 | 2 |
|
| 3 | +import com.google.protobuf.ByteString; |
3 | 4 | import java.lang.reflect.Method; |
4 | 5 | import lombok.extern.slf4j.Slf4j; |
5 | 6 | import org.junit.After; |
|
8 | 9 | import org.junit.Test; |
9 | 10 | import org.tron.common.BaseTest; |
10 | 11 | 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; |
11 | 15 | import org.tron.core.config.args.Args; |
12 | 16 | import org.tron.core.store.StoreFactory; |
13 | 17 | import org.tron.core.vm.config.VMConfig; |
| 18 | +import org.tron.protos.Protocol.AccountType; |
14 | 19 |
|
15 | 20 | @Slf4j |
16 | 21 | public class RepositoryImplHardenTest extends BaseTest { |
@@ -218,4 +223,58 @@ public void testUsageToBalanceOverflowDetectedWithHardening() { |
218 | 223 | Assert.assertThrows(ArithmeticException.class, |
219 | 224 | () -> invokeUsageToBalance(usage, totalWeight, totalLimit)); |
220 | 225 | } |
| 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 | + } |
221 | 280 | } |
0 commit comments