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(freezeV2): fix some problems #4823

Merged
merged 2 commits into from
Dec 1, 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 @@ -66,14 +66,14 @@ public boolean execute(Object result) throws ContractExeException {
delegateResource(ownerAddress, receiverAddress, true,
delegateBalance);

ownerCapsule.addDelegatedFrozenBalanceForBandwidth(delegateBalance);
ownerCapsule.addDelegatedFrozenV2BalanceForBandwidth(delegateBalance);
ownerCapsule.addFrozenBalanceForBandwidthV2(-delegateBalance);
break;
case ENERGY:
delegateResource(ownerAddress, receiverAddress, false,
delegateBalance);

ownerCapsule.addDelegatedFrozenBalanceForEnergy(delegateBalance);
ownerCapsule.addDelegatedFrozenV2BalanceForEnergy(delegateBalance);
ownerCapsule.addFrozenBalanceForEnergyV2(-delegateBalance);
break;
default:
Expand Down Expand Up @@ -150,14 +150,16 @@ public boolean validate() throws ContractValidateException {
long netUsage = (long) (accountNetUsage * TRX_PRECISION * ((double)
(dynamicStore.getTotalNetWeight()) / dynamicStore.getTotalNetLimit()));

long ownerNetUsage = (long) (netUsage * ((double) (ownerCapsule
.getFrozenV2BalanceForBandwidth()) /
ownerCapsule.getAllFrozenBalanceForBandwidth()));
long remainNetUsage = netUsage
- ownerCapsule.getFrozenBalance()
- ownerCapsule.getAcquiredDelegatedFrozenBalanceForBandwidth()
- ownerCapsule.getAcquiredDelegatedFrozenV2BalanceForBandwidth();

if (ownerCapsule.getFrozenV2BalanceForBandwidth() - ownerNetUsage
< delegateBalance) {
remainNetUsage = Math.max(0, remainNetUsage);

if (ownerCapsule.getFrozenV2BalanceForBandwidth() - remainNetUsage < delegateBalance) {
throw new ContractValidateException(
"delegateBalance must be less than available FreezeBandwidthV2 balance");
"delegateBalance must be less than available FreezeBandwidthV2 balance");
}
}
break;
Expand All @@ -168,12 +170,16 @@ public boolean validate() throws ContractValidateException {
long energyUsage = (long) (ownerCapsule.getEnergyUsage() * TRX_PRECISION * ((double)
(dynamicStore.getTotalEnergyWeight()) / dynamicStore.getTotalEnergyCurrentLimit()));

long ownerEnergyUsage = (long) (energyUsage * ((double) (ownerCapsule
.getFrozenV2BalanceForEnergy()) / ownerCapsule.getAllFrozenBalanceForEnergy()));
long remainEnergyUsage = energyUsage
- ownerCapsule.getEnergyFrozenBalance()
- ownerCapsule.getAcquiredDelegatedFrozenBalanceForEnergy()
- ownerCapsule.getAcquiredDelegatedFrozenV2BalanceForEnergy();

remainEnergyUsage = Math.max(0, remainEnergyUsage);

if (ownerCapsule.getFrozenV2BalanceForEnergy() - ownerEnergyUsage < delegateBalance) {
if (ownerCapsule.getFrozenV2BalanceForEnergy() - remainEnergyUsage < delegateBalance) {
throw new ContractValidateException(
"delegateBalance must be less than available FreezeEnergyV2Balance balance");
"delegateBalance must be less than available FreezeEnergyV2Balance balance");
}
}
break;
Expand Down Expand Up @@ -251,9 +257,9 @@ private void delegateResource(byte[] ownerAddress, byte[] receiverAddress, boole
//modify AccountStore for receiver
AccountCapsule receiverCapsule = accountStore.get(receiverAddress);
if (isBandwidth) {
receiverCapsule.addAcquiredDelegatedFrozenBalanceForBandwidth(balance);
receiverCapsule.addAcquiredDelegatedFrozenV2BalanceForBandwidth(balance);
} else {
receiverCapsule.addAcquiredDelegatedFrozenBalanceForEnergy(balance);
receiverCapsule.addAcquiredDelegatedFrozenV2BalanceForEnergy(balance);
}
accountStore.put(receiverCapsule.createDbKey(), receiverCapsule);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
import org.tron.protos.Protocol.Transaction.Result.code;
import org.tron.protos.contract.BalanceContract.FreezeBalanceV2Contract;

import java.util.Objects;

import static org.tron.core.actuator.ActuatorConstant.NOT_EXIST_STR;
import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION;
import static org.tron.protos.contract.Common.ResourceCode.BANDWIDTH;
import static org.tron.protos.contract.Common.ResourceCode.ENERGY;

@Slf4j(topic = "actuator")
public class FreezeBalanceV2Actuator extends AbstractActuator {
Expand Down Expand Up @@ -56,16 +59,22 @@ public boolean execute(Object result) throws ContractExeException {

switch (freezeBalanceV2Contract.getResource()) {
case BANDWIDTH:
long oldNetWeight = accountCapsule.getFrozenV2BalanceWithDelegated(BANDWIDTH) / TRX_PRECISION;
accountCapsule.addFrozenBalanceForBandwidthV2(frozenBalance);
dynamicStore.addTotalNetWeight(frozenBalance / TRX_PRECISION);
long newNetWeight = accountCapsule.getFrozenV2BalanceWithDelegated(BANDWIDTH) / TRX_PRECISION;
dynamicStore.addTotalNetWeight(newNetWeight - oldNetWeight);
break;
case ENERGY:
long oldEnergyWeight = accountCapsule.getFrozenV2BalanceWithDelegated(ENERGY) / TRX_PRECISION;
accountCapsule.addFrozenBalanceForEnergyV2(frozenBalance);
dynamicStore.addTotalEnergyWeight(frozenBalance / TRX_PRECISION);
long newEnergyWeight = accountCapsule.getFrozenV2BalanceWithDelegated(ENERGY) / TRX_PRECISION;
dynamicStore.addTotalEnergyWeight(newEnergyWeight - oldEnergyWeight);
break;
case TRON_POWER:
long oldTPWeight = accountCapsule.getTronPowerFrozenV2Balance() / TRX_PRECISION;
accountCapsule.addFrozenForTronPowerV2(frozenBalance);
dynamicStore.addTotalTronPowerWeight(frozenBalance / TRX_PRECISION);
long newTPWeight = accountCapsule.getTronPowerFrozenV2Balance() / TRX_PRECISION;
dynamicStore.addTotalTronPowerWeight(newTPWeight - oldTPWeight);
break;
default:
logger.debug("Resource Code Error.");
Expand All @@ -79,7 +88,6 @@ public boolean execute(Object result) throws ContractExeException {
return true;
}


@Override
public boolean validate() throws ContractValidateException {
if (this.any == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public boolean execute(Object result) throws ContractExeException {
BandwidthProcessor bandwidthProcessor = new BandwidthProcessor(chainBaseManager);
bandwidthProcessor.updateUsage(receiverCapsule);

if (receiverCapsule.getAcquiredDelegatedFrozenBalanceForBandwidth()
if (receiverCapsule.getAcquiredDelegatedFrozenV2BalanceForBandwidth()
< unDelegateBalance) {
// A TVM contract suicide, re-create will produce this situation
receiverCapsule.setAcquiredDelegatedFrozenBalanceForBandwidth(0);
receiverCapsule.setAcquiredDelegatedFrozenV2BalanceForBandwidth(0);
} else {
// calculate usage
long unDelegateMaxUsage = (long) ((double) unDelegateBalance / TRX_PRECISION
Expand All @@ -84,7 +84,7 @@ public boolean execute(Object result) throws ContractExeException {
* ((double) (unDelegateBalance) / receiverCapsule.getAllFrozenBalanceForBandwidth()));
transferUsage = Math.min(unDelegateMaxUsage, transferUsage);

receiverCapsule.addAcquiredDelegatedFrozenBalanceForBandwidth(-unDelegateBalance);
receiverCapsule.addAcquiredDelegatedFrozenV2BalanceForBandwidth(-unDelegateBalance);
}

long newNetUsage = receiverCapsule.getNetUsage() - transferUsage;
Expand All @@ -95,10 +95,10 @@ public boolean execute(Object result) throws ContractExeException {
EnergyProcessor energyProcessor = new EnergyProcessor(dynamicStore, accountStore);
energyProcessor.updateUsage(receiverCapsule);

if (receiverCapsule.getAcquiredDelegatedFrozenBalanceForEnergy()
if (receiverCapsule.getAcquiredDelegatedFrozenV2BalanceForEnergy()
< unDelegateBalance) {
// A TVM contract receiver, re-create will produce this situation
receiverCapsule.setAcquiredDelegatedFrozenBalanceForEnergy(0);
receiverCapsule.setAcquiredDelegatedFrozenV2BalanceForEnergy(0);
} else {
// calculate usage
long unDelegateMaxUsage = (long) ((double) unDelegateBalance / TRX_PRECISION
Expand All @@ -107,7 +107,7 @@ public boolean execute(Object result) throws ContractExeException {
* ((double) (unDelegateBalance) / receiverCapsule.getAllFrozenBalanceForEnergy()));
transferUsage = Math.min(unDelegateMaxUsage, transferUsage);

receiverCapsule.addAcquiredDelegatedFrozenBalanceForEnergy(-unDelegateBalance);
receiverCapsule.addAcquiredDelegatedFrozenV2BalanceForEnergy(-unDelegateBalance);
}

long newEnergyUsage = receiverCapsule.getEnergyUsage() - transferUsage;
Expand All @@ -132,7 +132,7 @@ public boolean execute(Object result) throws ContractExeException {
case BANDWIDTH: {
delegatedResourceCapsule.addFrozenBalanceForBandwidth(-unDelegateBalance, 0);

ownerCapsule.addDelegatedFrozenBalanceForBandwidth(-unDelegateBalance);
ownerCapsule.addDelegatedFrozenV2BalanceForBandwidth(-unDelegateBalance);
ownerCapsule.addFrozenBalanceForBandwidthV2(unDelegateBalance);

BandwidthProcessor processor = new BandwidthProcessor(chainBaseManager);
Expand All @@ -148,7 +148,7 @@ public boolean execute(Object result) throws ContractExeException {
case ENERGY: {
delegatedResourceCapsule.addFrozenBalanceForEnergy(-unDelegateBalance, 0);

ownerCapsule.addDelegatedFrozenBalanceForEnergy(-unDelegateBalance);
ownerCapsule.addDelegatedFrozenV2BalanceForEnergy(-unDelegateBalance);
ownerCapsule.addFrozenBalanceForEnergyV2(unDelegateBalance);

EnergyProcessor processor = new EnergyProcessor(dynamicStore, accountStore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,10 @@ public boolean execute(Object result) throws ContractExeException {

ResourceCode freezeType = unfreezeBalanceV2Contract.getResource();

this.updateAccountFrozenInfo(freezeType, accountCapsule, unfreezeBalance);

long expireTime = this.calcUnfreezeExpireTime(now);
accountCapsule.addUnfrozenV2List(freezeType, unfreezeBalance, expireTime);

this.updateTotalResourceWeight(unfreezeBalanceV2Contract, unfreezeBalance);
this.updateTotalResourceWeight(accountCapsule, unfreezeBalanceV2Contract, unfreezeBalance);
this.updateVote(accountCapsule, unfreezeBalanceV2Contract, ownerAddress);

if (dynamicStore.supportAllowNewResourceModel()
Expand Down Expand Up @@ -272,18 +270,28 @@ public long unfreezeExpire(AccountCapsule accountCapsule, long now) {
return unfreezeBalance;
}

public void updateTotalResourceWeight(final UnfreezeBalanceV2Contract unfreezeBalanceV2Contract,
public void updateTotalResourceWeight(AccountCapsule accountCapsule,
final UnfreezeBalanceV2Contract unfreezeBalanceV2Contract,
long unfreezeBalance) {
DynamicPropertiesStore dynamicStore = chainBaseManager.getDynamicPropertiesStore();
switch (unfreezeBalanceV2Contract.getResource()) {
case BANDWIDTH:
dynamicStore.addTotalNetWeight(-unfreezeBalance / TRX_PRECISION);
long oldNetWeight = accountCapsule.getFrozenV2BalanceWithDelegated(BANDWIDTH) / TRX_PRECISION;
accountCapsule.addFrozenBalanceForBandwidthV2(-unfreezeBalance);
long newNetWeight = accountCapsule.getFrozenV2BalanceWithDelegated(BANDWIDTH) / TRX_PRECISION;
dynamicStore.addTotalNetWeight(newNetWeight - oldNetWeight);
break;
case ENERGY:
dynamicStore.addTotalEnergyWeight(-unfreezeBalance / TRX_PRECISION);
long oldEnergyWeight = accountCapsule.getFrozenV2BalanceWithDelegated(ENERGY) / TRX_PRECISION;
accountCapsule.addFrozenBalanceForEnergyV2(-unfreezeBalance);
long newEnergyWeight = accountCapsule.getFrozenV2BalanceWithDelegated(ENERGY) / TRX_PRECISION;
dynamicStore.addTotalEnergyWeight(newEnergyWeight - oldEnergyWeight);
break;
case TRON_POWER:
dynamicStore.addTotalTronPowerWeight(-unfreezeBalance / TRX_PRECISION);
long oldTPWeight = accountCapsule.getTronPowerFrozenV2Balance() / TRX_PRECISION;
accountCapsule.addFrozenForTronPowerV2(-unfreezeBalance);
long newTPWeight = accountCapsule.getTronPowerFrozenV2Balance() / TRX_PRECISION;
dynamicStore.addTotalTronPowerWeight(newTPWeight - oldTPWeight);
break;
default:
//this should never happen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ public void validate(DelegateResourceParam param, Repository repo) throws Contra
long netUsage = (long) (ownerCapsule.getNetUsage() * TRX_PRECISION * ((double)
(repo.getTotalNetWeight()) / dynamicStore.getTotalNetLimit()));

long ownerNetUsage = (long) (netUsage * ((double)(ownerCapsule
.getFrozenV2BalanceForBandwidth()) /
ownerCapsule.getAllFrozenBalanceForBandwidth()));
long remainNetUsage = netUsage
- ownerCapsule.getFrozenBalance()
- ownerCapsule.getAcquiredDelegatedFrozenBalanceForBandwidth()
- ownerCapsule.getAcquiredDelegatedFrozenV2BalanceForBandwidth();

if (ownerCapsule.getFrozenV2BalanceForBandwidth() - ownerNetUsage
< delegateBalance) {
remainNetUsage = Math.max(0, remainNetUsage);

if (ownerCapsule.getFrozenV2BalanceForBandwidth() - remainNetUsage < delegateBalance) {
throw new ContractValidateException(
"delegateBalance must be less than available FreezeBandwidthV2 balance");
"delegateBalance must be less than available FreezeBandwidthV2 balance");
}
}
break;
Expand All @@ -76,12 +78,16 @@ public void validate(DelegateResourceParam param, Repository repo) throws Contra
long energyUsage = (long) (ownerCapsule.getEnergyUsage() * TRX_PRECISION * ((double)
(repo.getTotalEnergyWeight()) / dynamicStore.getTotalEnergyCurrentLimit()));

long ownerEnergyUsage = (long) (energyUsage * ((double)(ownerCapsule
.getFrozenV2BalanceForEnergy()) / ownerCapsule.getAllFrozenBalanceForEnergy()));
long remainEnergyUsage = energyUsage
- ownerCapsule.getEnergyFrozenBalance()
- ownerCapsule.getAcquiredDelegatedFrozenBalanceForEnergy()
- ownerCapsule.getAcquiredDelegatedFrozenV2BalanceForEnergy();

remainEnergyUsage = Math.max(0, remainEnergyUsage);

if (ownerCapsule.getFrozenV2BalanceForEnergy() - ownerEnergyUsage < delegateBalance) {
if (ownerCapsule.getFrozenV2BalanceForEnergy() - remainEnergyUsage < delegateBalance) {
throw new ContractValidateException(
"delegateBalance must be less than available FreezeEnergyV2Balance balance");
"delegateBalance must be less than available FreezeEnergyV2Balance balance");
}
}
break;
Expand Down Expand Up @@ -124,14 +130,14 @@ public void execute(DelegateResourceParam param, Repository repo) {
delegateResource(ownerAddress, receiverAddress, true,
delegateBalance, repo);

ownerCapsule.addDelegatedFrozenBalanceForBandwidth(delegateBalance);
ownerCapsule.addDelegatedFrozenV2BalanceForBandwidth(delegateBalance);
ownerCapsule.addFrozenBalanceForBandwidthV2(-delegateBalance);
break;
case ENERGY:
delegateResource(ownerAddress, receiverAddress, false,
delegateBalance, repo);

ownerCapsule.addDelegatedFrozenBalanceForEnergy(delegateBalance);
ownerCapsule.addDelegatedFrozenV2BalanceForEnergy(delegateBalance);
ownerCapsule.addFrozenBalanceForEnergyV2(-delegateBalance);
break;
default:
Expand Down Expand Up @@ -164,9 +170,9 @@ private void delegateResource(
//update Account for receiver
AccountCapsule receiverCapsule = repo.getAccount(receiverAddress);
if (isBandwidth) {
receiverCapsule.addAcquiredDelegatedFrozenBalanceForBandwidth(delegateBalance);
receiverCapsule.addAcquiredDelegatedFrozenV2BalanceForBandwidth(delegateBalance);
} else {
receiverCapsule.addAcquiredDelegatedFrozenBalanceForEnergy(delegateBalance);
receiverCapsule.addAcquiredDelegatedFrozenV2BalanceForEnergy(delegateBalance);
}
repo.updateDelegatedResource(key, delegatedResourceCapsule);
repo.updateAccount(receiverCapsule.createDbKey(), receiverCapsule);
Expand Down
Loading