Skip to content
Open
Show file tree
Hide file tree
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
19 changes: 18 additions & 1 deletion actuator/src/main/java/org/tron/core/utils/ProposalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,22 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
}
break;
}
case ALLOW_HARDEN_RESOURCE_CALCULATION: {
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_8_2)) {
throw new ContractValidateException(
"Bad chain parameter id [ALLOW_HARDEN_RESOURCE_CALCULATION]");
}
if (dynamicPropertiesStore.getAllowHardenResourceCalculation() == 1) {
throw new ContractValidateException(
"[ALLOW_HARDEN_RESOURCE_CALCULATION] has been valid, "
+ "no need to propose again");
}
if (value != 1) {
throw new ContractValidateException(
"This value[ALLOW_HARDEN_RESOURCE_CALCULATION] is only allowed to be 1");
}
break;
}
default:
break;
}
Expand Down Expand Up @@ -971,7 +987,8 @@ public enum ProposalType { // current value, value range
ALLOW_TVM_BLOB(89), // 0, 1
PROPOSAL_EXPIRE_TIME(92), // (0, 31536003000)
ALLOW_TVM_SELFDESTRUCT_RESTRICTION(94), // 0, 1
ALLOW_TVM_OSAKA(96); // 0, 1
ALLOW_TVM_OSAKA(96), // 0, 1
ALLOW_HARDEN_RESOURCE_CALCULATION(97); // 0, 1

private long code;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static void load(StoreFactory storeFactory) {
VMConfig.initAllowTvmBlob(ds.getAllowTvmBlob());
VMConfig.initAllowTvmSelfdestructRestriction(ds.getAllowTvmSelfdestructRestriction());
VMConfig.initAllowTvmOsaka(ds.getAllowTvmOsaka());
VMConfig.initAllowHardenResourceCalculation(ds.getAllowHardenResourceCalculation());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.google.common.collect.HashBasedTable;
import com.google.protobuf.ByteString;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Optional;
Expand All @@ -17,6 +18,7 @@
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.encoders.Hex;
import org.tron.common.crypto.Hash;
import org.tron.common.math.StrictMathWrapper;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.runtime.vm.DataWord;
import org.tron.common.utils.ByteArray;
Expand Down Expand Up @@ -223,7 +225,7 @@ public Pair<Long, Long> getAccountEnergyUsageBalanceAndRestoreSeconds(AccountCap
long totalEnergyLimit = getDynamicPropertiesStore().getTotalEnergyCurrentLimit();
long totalEnergyWeight = getTotalEnergyWeight();

long balance = (long) ((double) newEnergyUsage * totalEnergyWeight / totalEnergyLimit * TRX_PRECISION);
long balance = usageToBalance(newEnergyUsage, totalEnergyWeight, totalEnergyLimit);

return Pair.of(balance, restoreSlots * BLOCK_PRODUCED_INTERVAL / 1_000);
}
Expand All @@ -246,11 +248,22 @@ public Pair<Long, Long> getAccountNetUsageBalanceAndRestoreSeconds(AccountCapsul
long totalNetLimit = getDynamicPropertiesStore().getTotalNetLimit();
long totalNetWeight = getTotalNetWeight();

long balance = (long) ((double) newNetUsage * totalNetWeight / totalNetLimit * TRX_PRECISION);
long balance = usageToBalance(newNetUsage, totalNetWeight, totalNetLimit);

return Pair.of(balance, restoreSlots * BLOCK_PRODUCED_INTERVAL / 1_000);
}

private long usageToBalance(long usage, long totalWeight, long totalLimit) {
if (hardenResourceCalculation()) {
return BigInteger.valueOf(usage)
.multiply(BigInteger.valueOf(totalWeight))
.multiply(BigInteger.valueOf(TRX_PRECISION))
.divide(BigInteger.valueOf(totalLimit))
.longValueExact();
}
return (long) ((double) usage * totalWeight / totalLimit * TRX_PRECISION);
}

@Override
public AssetIssueCapsule getAssetIssue(byte[] tokenId) {
byte[] tokenIdWithoutLeadingZero = ByteUtil.stripLeadingZeroes(tokenId);
Expand Down Expand Up @@ -896,8 +909,19 @@ private long recover(long lastUsage, long lastTime, long now, long personalWindo
}

private long increase(long lastUsage, long usage, long lastTime, long now, long windowSize) {
long averageLastUsage = divideCeil(lastUsage * precision, windowSize);
long averageUsage = divideCeil(usage * precision, windowSize);
long averageLastUsage;
long averageUsage;
if (hardenResourceCalculation()) {
BigInteger biPrecision = BigInteger.valueOf(precision);
BigInteger biWindowSize = BigInteger.valueOf(windowSize);
averageLastUsage = divideCeilExact(
BigInteger.valueOf(lastUsage).multiply(biPrecision), biWindowSize);
averageUsage = divideCeilExact(
BigInteger.valueOf(usage).multiply(biPrecision), biWindowSize);
} else {
averageLastUsage = divideCeil(lastUsage * precision, windowSize);
averageUsage = divideCeil(usage * precision, windowSize);
}

if (lastTime != now) {
assert now > lastTime;
Expand All @@ -917,21 +941,46 @@ private long divideCeil(long numerator, long denominator) {
return (numerator / denominator) + ((numerator % denominator) > 0 ? 1 : 0);
}

private long divideCeilExact(BigInteger numerator, BigInteger denominator) {
BigInteger[] divRem = numerator.divideAndRemainder(denominator);
long result = divRem[0].longValueExact();
if (divRem[1].signum() > 0) {
result = StrictMathWrapper.addExact(result, 1);
}
return result;
}

private long getUsage(long usage, long windowSize) {
if (hardenResourceCalculation()) {
return BigInteger.valueOf(usage)
.multiply(BigInteger.valueOf(windowSize))
.divide(BigInteger.valueOf(precision))
.longValueExact();
}
return usage * windowSize / precision;
}

private boolean hardenResourceCalculation() {
return VMConfig.allowHardenResourceCalculation();
}

public long calculateGlobalEnergyLimit(AccountCapsule accountCapsule) {
long frozeBalance = accountCapsule.getAllFrozenBalanceForEnergy();
if (frozeBalance < 1_000_000L) {
if (frozeBalance < precision) {
return 0;
}
long energyWeight = frozeBalance / 1_000_000L;
long energyWeight = frozeBalance / precision;
long totalEnergyLimit = getDynamicPropertiesStore().getTotalEnergyCurrentLimit();
long totalEnergyWeight = getDynamicPropertiesStore().getTotalEnergyWeight();

assert totalEnergyWeight > 0;

if (hardenResourceCalculation()) {
return BigInteger.valueOf(energyWeight)
.multiply(BigInteger.valueOf(totalEnergyLimit))
.divide(BigInteger.valueOf(totalEnergyWeight))
.longValueExact();
}
return (long) (energyWeight * ((double) totalEnergyLimit / totalEnergyWeight));
}

Expand Down
10 changes: 8 additions & 2 deletions chainbase/src/main/java/org/tron/core/db/BandwidthProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,6 @@ public long calculateGlobalNetLimit(AccountCapsule accountCapsule) {
if (frozeBalance < TRX_PRECISION) {
return 0;
}
long netWeight = frozeBalance / TRX_PRECISION;
long totalNetLimit = chainBaseManager.getDynamicPropertiesStore().getTotalNetLimit();
long totalNetWeight = chainBaseManager.getDynamicPropertiesStore().getTotalNetWeight();
if (dynamicPropertiesStore.allowNewReward() && totalNetWeight <= 0) {
Expand All @@ -446,16 +445,23 @@ public long calculateGlobalNetLimit(AccountCapsule accountCapsule) {
if (totalNetWeight == 0) {
return 0;
}
if (hardenCalculation()) {
return calculateGlobalLimitV1(frozeBalance, totalNetLimit, totalNetWeight);
}
long netWeight = frozeBalance / TRX_PRECISION;
return (long) (netWeight * ((double) totalNetLimit / totalNetWeight));
}

public long calculateGlobalNetLimitV2(long frozeBalance) {
double netWeight = (double) frozeBalance / TRX_PRECISION;
long totalNetLimit = dynamicPropertiesStore.getTotalNetLimit();
long totalNetWeight = dynamicPropertiesStore.getTotalNetWeight();
if (totalNetWeight == 0) {
return 0;
}
if (hardenCalculation()) {
return calculateGlobalLimitV2(frozeBalance, totalNetLimit, totalNetWeight);
}
double netWeight = (double) frozeBalance / TRX_PRECISION;
return (long) (netWeight * ((double) totalNetLimit / totalNetWeight));
}

Expand Down
40 changes: 29 additions & 11 deletions chainbase/src/main/java/org/tron/core/db/EnergyProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.tron.core.config.Parameter.ChainConstant.BLOCK_PRODUCED_INTERVAL;
import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION;

import java.math.BigInteger;
import lombok.extern.slf4j.Slf4j;
import org.tron.common.parameter.CommonParameter;
import org.tron.core.capsule.AccountCapsule;
Expand Down Expand Up @@ -71,17 +72,20 @@ public void updateAdaptiveTotalEnergyLimit() {

long result;
if (totalEnergyAverageUsage > targetTotalEnergyLimit) {
result = totalEnergyCurrentLimit * AdaptiveResourceLimitConstants.CONTRACT_RATE_NUMERATOR
/ AdaptiveResourceLimitConstants.CONTRACT_RATE_DENOMINATOR;
// logger.info(totalEnergyAverageUsage + ">" + targetTotalEnergyLimit + "\n" + result);
result = scaleByRate(totalEnergyCurrentLimit,
AdaptiveResourceLimitConstants.CONTRACT_RATE_NUMERATOR,
AdaptiveResourceLimitConstants.CONTRACT_RATE_DENOMINATOR);
} else {
result = totalEnergyCurrentLimit * AdaptiveResourceLimitConstants.EXPAND_RATE_NUMERATOR
/ AdaptiveResourceLimitConstants.EXPAND_RATE_DENOMINATOR;
// logger.info(totalEnergyAverageUsage + "<" + targetTotalEnergyLimit + "\n" + result);
result = scaleByRate(totalEnergyCurrentLimit,
AdaptiveResourceLimitConstants.EXPAND_RATE_NUMERATOR,
AdaptiveResourceLimitConstants.EXPAND_RATE_DENOMINATOR);
}
long upperBound = hardenCalculation()
? BigInteger.valueOf(totalEnergyLimit).multiply(BigInteger.valueOf(
dynamicPropertiesStore.getAdaptiveResourceLimitMultiplier())).longValueExact()
: totalEnergyLimit * dynamicPropertiesStore.getAdaptiveResourceLimitMultiplier();
result = min(max(result, totalEnergyLimit, this.disableJavaLangMath()),
totalEnergyLimit * dynamicPropertiesStore.getAdaptiveResourceLimitMultiplier(),
this.disableJavaLangMath());
upperBound, this.disableJavaLangMath());

dynamicPropertiesStore.saveTotalEnergyCurrentLimit(result);
logger.debug("Adjust totalEnergyCurrentLimit, old: {}, new: {}.",
Expand Down Expand Up @@ -147,24 +151,30 @@ public long calculateGlobalEnergyLimit(AccountCapsule accountCapsule) {
return 0;
}

long energyWeight = frozeBalance / TRX_PRECISION;
long totalEnergyLimit = dynamicPropertiesStore.getTotalEnergyCurrentLimit();
long totalEnergyWeight = dynamicPropertiesStore.getTotalEnergyWeight();
if (dynamicPropertiesStore.allowNewReward() && totalEnergyWeight <= 0) {
return 0;
} else {
assert totalEnergyWeight > 0;
}
if (hardenCalculation()) {
return calculateGlobalLimitV1(frozeBalance, totalEnergyLimit, totalEnergyWeight);
}
long energyWeight = frozeBalance / TRX_PRECISION;
return (long) (energyWeight * ((double) totalEnergyLimit / totalEnergyWeight));
}

public long calculateGlobalEnergyLimitV2(long frozeBalance) {
double energyWeight = (double) frozeBalance / TRX_PRECISION;
long totalEnergyLimit = dynamicPropertiesStore.getTotalEnergyCurrentLimit();
long totalEnergyWeight = dynamicPropertiesStore.getTotalEnergyWeight();
if (totalEnergyWeight == 0) {
return 0;
}
if (hardenCalculation()) {
return calculateGlobalLimitV2(frozeBalance, totalEnergyLimit, totalEnergyWeight);
}
double energyWeight = (double) frozeBalance / TRX_PRECISION;
return (long) (energyWeight * ((double) totalEnergyLimit / totalEnergyWeight));
}

Expand All @@ -184,7 +194,15 @@ private long getHeadSlot() {
return getHeadSlot(dynamicPropertiesStore);
}


private long scaleByRate(long value, long numerator, long denominator) {
if (hardenCalculation()) {
return BigInteger.valueOf(value)
.multiply(BigInteger.valueOf(numerator))
.divide(BigInteger.valueOf(denominator))
.longValueExact();
}
return value * numerator / denominator;
}
}


Loading
Loading