Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mechanism): optimize deflation problems #4772

Merged
merged 1 commit into from
Nov 8, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import static org.tron.core.actuator.ActuatorConstant.NOT_EXIST_STR;
import static org.tron.core.config.Parameter.ChainConstant.FROZEN_PERIOD;
import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION;
import static org.tron.protos.contract.Common.ResourceCode;
import static org.tron.protos.contract.Common.ResourceCode.BANDWIDTH;
import static org.tron.protos.contract.Common.ResourceCode.ENERGY;
import static org.tron.protos.contract.Common.ResourceCode.TRON_POWER;

import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
Expand Down Expand Up @@ -72,44 +76,48 @@ public boolean execute(Object result) throws ContractExeException {
byte[] ownerAddress = freezeBalanceContract.getOwnerAddress().toByteArray();
byte[] receiverAddress = freezeBalanceContract.getReceiverAddress().toByteArray();

long increment;
switch (freezeBalanceContract.getResource()) {
case BANDWIDTH:
if (!ArrayUtils.isEmpty(receiverAddress)
&& dynamicStore.supportDR()) {
delegateResource(ownerAddress, receiverAddress, true,
frozenBalance, expireTime);
increment = delegateResource(ownerAddress, receiverAddress, true,
frozenBalance, expireTime);
accountCapsule.addDelegatedFrozenBalanceForBandwidth(frozenBalance);
} else {
long oldNetWeight = accountCapsule.getFrozenBalance() / TRX_PRECISION;
long newFrozenBalanceForBandwidth =
frozenBalance + accountCapsule.getFrozenBalance();
accountCapsule.setFrozenForBandwidth(newFrozenBalanceForBandwidth, expireTime);
long newNetWeight = accountCapsule.getFrozenBalance() / TRX_PRECISION;
increment = newNetWeight - oldNetWeight;
}
dynamicStore
.addTotalNetWeight(frozenBalance / TRX_PRECISION);
addTotalWeight(BANDWIDTH, dynamicStore, frozenBalance, increment);
break;
case ENERGY:
if (!ArrayUtils.isEmpty(receiverAddress)
&& dynamicStore.supportDR()) {
delegateResource(ownerAddress, receiverAddress, false,
frozenBalance, expireTime);
increment = delegateResource(ownerAddress, receiverAddress, false,
frozenBalance, expireTime);
accountCapsule.addDelegatedFrozenBalanceForEnergy(frozenBalance);
} else {
long oldEnergyWeight = accountCapsule.getEnergyFrozenBalance() / TRX_PRECISION;
long newFrozenBalanceForEnergy =
frozenBalance + accountCapsule.getAccountResource()
.getFrozenBalanceForEnergy()
.getFrozenBalance();
frozenBalance + accountCapsule.getEnergyFrozenBalance();
accountCapsule.setFrozenForEnergy(newFrozenBalanceForEnergy, expireTime);
long newEnergyWeight = accountCapsule.getEnergyFrozenBalance() / TRX_PRECISION;
increment = newEnergyWeight - oldEnergyWeight;
}
dynamicStore
.addTotalEnergyWeight(frozenBalance / TRX_PRECISION);
addTotalWeight(ENERGY, dynamicStore, frozenBalance, increment);
break;
case TRON_POWER:
long oldTPWeight = accountCapsule.getTronPowerFrozenBalance() / TRX_PRECISION;
long newFrozenBalanceForTronPower =
frozenBalance + accountCapsule.getTronPowerFrozenBalance();
accountCapsule.setFrozenForTronPower(newFrozenBalanceForTronPower, expireTime);

dynamicStore
.addTotalTronPowerWeight(frozenBalance / TRX_PRECISION);
long newTPWeight = accountCapsule.getTronPowerFrozenBalance() / TRX_PRECISION;
increment = newTPWeight - oldTPWeight;
addTotalWeight(TRON_POWER, dynamicStore, frozenBalance, increment);
break;
default:
logger.debug("Resource Code Error.");
Expand All @@ -123,6 +131,23 @@ public boolean execute(Object result) throws ContractExeException {
return true;
}

private void addTotalWeight(ResourceCode resourceCode, DynamicPropertiesStore dynamicStore,
long frozenBalance, long increment) {
long weight = dynamicStore.allowNewRewardEnable() ? increment : frozenBalance / TRX_PRECISION;
switch (resourceCode) {
case BANDWIDTH:
dynamicStore.addTotalNetWeight(weight);
break;
case ENERGY:
dynamicStore.addTotalEnergyWeight(weight);
break;
case TRON_POWER:
dynamicStore.addTotalTronPowerWeight(weight);
break;
default:
logger.debug("Resource Code Error.");
}
}

@Override
public boolean validate() throws ContractValidateException {
Expand Down Expand Up @@ -175,11 +200,6 @@ public boolean validate() throws ContractValidateException {
throw new ContractValidateException("frozenBalance must be less than accountBalance");
}

// long maxFrozenNumber = dbManager.getDynamicPropertiesStore().getMaxFrozenNumber();
// if (accountCapsule.getFrozenCount() >= maxFrozenNumber) {
// throw new ContractValidateException("max frozen number is: " + maxFrozenNumber);
// }

long frozenDuration = freezeBalanceContract.getFrozenDuration();
long minFrozenTime = dynamicStore.getMinFrozenTime();
long maxFrozenTime = dynamicStore.getMaxFrozenTime();
Expand Down Expand Up @@ -262,7 +282,7 @@ public long calcFee() {
return 0;
}

private void delegateResource(byte[] ownerAddress, byte[] receiverAddress, boolean isBandwidth,
private long delegateResource(byte[] ownerAddress, byte[] receiverAddress, boolean isBandwidth,
long balance, long expireTime) {
AccountStore accountStore = chainBaseManager.getAccountStore();
DelegatedResourceStore delegatedResourceStore = chainBaseManager.getDelegatedResourceStore();
Expand All @@ -272,13 +292,16 @@ private void delegateResource(byte[] ownerAddress, byte[] receiverAddress, boole
//modify DelegatedResourceStore
DelegatedResourceCapsule delegatedResourceCapsule = delegatedResourceStore
.get(key);
long oldWeight;
if (delegatedResourceCapsule != null) {
oldWeight = delegatedResourceCapsule.getFrozenBalance(isBandwidth) / TRX_PRECISION;
if (isBandwidth) {
delegatedResourceCapsule.addFrozenBalanceForBandwidth(balance, expireTime);
} else {
delegatedResourceCapsule.addFrozenBalanceForEnergy(balance, expireTime);
}
} else {
oldWeight = 0;
delegatedResourceCapsule = new DelegatedResourceCapsule(
ByteString.copyFrom(ownerAddress),
ByteString.copyFrom(receiverAddress));
Expand All @@ -289,6 +312,7 @@ private void delegateResource(byte[] ownerAddress, byte[] receiverAddress, boole
}

}
long newWeight = delegatedResourceCapsule.getFrozenBalance(isBandwidth) / TRX_PRECISION;
delegatedResourceStore.put(key, delegatedResourceCapsule);

//modify DelegatedResourceAccountIndexStore
Expand Down Expand Up @@ -332,6 +356,7 @@ private void delegateResource(byte[] ownerAddress, byte[] receiverAddress, boole
}

accountStore.put(receiverCapsule.createDbKey(), receiverCapsule);
return newWeight - oldWeight;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ public enum ProposalType { // current value, value range
ALLOW_HIGHER_LIMIT_FOR_MAX_CPU_TIME_OF_ONE_TX(65), // 0, 1
ALLOW_ASSET_OPTIMIZATION(66), // 0, 1
ALLOW_NEW_REWARD_ALGO(67), // 0, 1
MEMO_FEE(68); // 0, [1, 1000_000_000]
MEMO_FEE(68); // 0, [0, 1000_000_000]

private long code;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ public long getFrozenBalanceForBandwidth() {
return this.delegatedResource.getFrozenBalanceForBandwidth();
}

public long getFrozenBalance(boolean isBandwidth) {
if (isBandwidth) {
return getFrozenBalanceForBandwidth();
} else {
return getFrozenBalanceForEnergy();
}

}

public void setFrozenBalanceForBandwidth(long Bandwidth, long expireTime) {
this.delegatedResource = this.delegatedResource.toBuilder()
.setFrozenBalanceForBandwidth(Bandwidth)
Expand Down