Skip to content

feat(freezeV2): optimize Stake2.0 code #5426

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

Merged
Next Next commit
fix(freezeV2): optimize Stake2.0 code
  • Loading branch information
lxcmyf committed Apr 14, 2023
commit ebd21421438a8c02ca00d51368858249451d588c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.tron.core.actuator.ActuatorConstant.NOT_EXIST_STR;
import static org.tron.core.config.Parameter.ChainConstant.DELEGATE_PERIOD;
import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION;
import static org.tron.core.vm.utils.FreezeV2Util.getV2EnergyUsage;
import static org.tron.core.vm.utils.FreezeV2Util.getV2NetUsage;

import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
Expand Down Expand Up @@ -136,7 +138,7 @@ public boolean validate() throws ContractValidateException {

long delegateBalance = delegateResourceContract.getBalance();
if (delegateBalance < TRX_PRECISION) {
throw new ContractValidateException("delegateBalance must be more than 1TRX");
throw new ContractValidateException("delegateBalance must be greater than or equal to 1 TRX");
}

switch (delegateResourceContract.getResource()) {
Expand All @@ -152,16 +154,11 @@ public boolean validate() throws ContractValidateException {
long netUsage = (long) (accountNetUsage * TRX_PRECISION * ((double)
(dynamicStore.getTotalNetWeight()) / dynamicStore.getTotalNetLimit()));

long remainNetUsage = netUsage
- ownerCapsule.getFrozenBalance()
- ownerCapsule.getAcquiredDelegatedFrozenBalanceForBandwidth()
- ownerCapsule.getAcquiredDelegatedFrozenV2BalanceForBandwidth();
long v2NetUsage = getV2NetUsage(ownerCapsule, netUsage);

remainNetUsage = Math.max(0, remainNetUsage);

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

long remainEnergyUsage = energyUsage
- ownerCapsule.getEnergyFrozenBalance()
- ownerCapsule.getAcquiredDelegatedFrozenBalanceForEnergy()
- ownerCapsule.getAcquiredDelegatedFrozenV2BalanceForEnergy();

remainEnergyUsage = Math.max(0, remainEnergyUsage);
long v2EnergyUsage = getV2EnergyUsage(ownerCapsule, energyUsage);

if (ownerCapsule.getFrozenV2BalanceForEnergy() - remainEnergyUsage < delegateBalance) {
if (ownerCapsule.getFrozenV2BalanceForEnergy() - v2EnergyUsage < delegateBalance) {
throw new ContractValidateException(
"delegateBalance must be less than available FreezeEnergyV2 balance");
"delegateBalance must be less than or equal to available FreezeEnergyV2 balance");
}
}
break;
Expand Down Expand Up @@ -218,6 +210,8 @@ public boolean validate() throws ContractValidateException {
return true;
}



@Override
public ByteString getOwnerAddress() throws InvalidProtocolBufferException {
return any.unpack(DelegateResourceContract.class).getOwnerAddress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ public boolean validate() throws ContractValidateException {
throw new ContractValidateException("frozenBalance must be positive");
}
if (frozenBalance < TRX_PRECISION) {
throw new ContractValidateException("frozenBalance must be more than 1TRX");
throw new ContractValidateException("frozenBalance must be greater than or equal to 1 TRX");
}

int frozenCount = accountCapsule.getFrozenCount();
if (!(frozenCount == 0 || frozenCount == 1)) {
throw new ContractValidateException("frozenCount must be 0 or 1");
}
if (frozenBalance > accountCapsule.getBalance()) {
throw new ContractValidateException("frozenBalance must be less than accountBalance");
throw new ContractValidateException("frozenBalance must be less than or equal to accountBalance");
}

long frozenDuration = freezeBalanceContract.getFrozenDuration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ public boolean validate() throws ContractValidateException {
throw new ContractValidateException("frozenBalance must be positive");
}
if (frozenBalance < TRX_PRECISION) {
throw new ContractValidateException("frozenBalance must be more than 1TRX");
throw new ContractValidateException("frozenBalance must be greater than or equal to 1 TRX");
}

if (frozenBalance > accountCapsule.getBalance()) {
throw new ContractValidateException("frozenBalance must be less than accountBalance");
throw new ContractValidateException("frozenBalance must be less than or equal to accountBalance");
}

switch (freezeBalanceV2Contract.getResource()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Arrays;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.tron.common.utils.DecodeUtil;
import org.tron.common.utils.StringUtil;
import org.tron.core.capsule.AccountCapsule;
Expand Down Expand Up @@ -240,7 +239,7 @@ public boolean validate() throws ContractValidateException {
}

byte[] receiverAddress = unDelegateResourceContract.getReceiverAddress().toByteArray();
if (ArrayUtils.isEmpty(receiverAddress) || !DecodeUtil.addressValid(receiverAddress)) {
if (!DecodeUtil.addressValid(receiverAddress)) {
throw new ContractValidateException("Invalid receiverAddress");
}
if (Arrays.equals(receiverAddress, ownerAddress)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.tron.core.actuator.ActuatorConstant.NOT_EXIST_STR;
import static org.tron.core.actuator.ActuatorConstant.STORE_NOT_EXIST;
import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION;
import static org.tron.core.vm.utils.FreezeV2Util.getV2EnergyUsage;
import static org.tron.core.vm.utils.FreezeV2Util.getV2NetUsage;

import com.google.common.primitives.Bytes;
import com.google.protobuf.ByteString;
Expand Down Expand Up @@ -49,7 +51,7 @@ public void validate(DelegateResourceParam param, Repository repo) throws Contra
}
long delegateBalance = param.getDelegateBalance();
if (delegateBalance < TRX_PRECISION) {
throw new ContractValidateException("delegateBalance must be more than 1TRX");
throw new ContractValidateException("delegateBalance must be greater than or equal to 1 TRX");
}

switch (param.getResourceType()) {
Expand All @@ -60,16 +62,11 @@ public void validate(DelegateResourceParam param, Repository repo) throws Contra
long netUsage = (long) (ownerCapsule.getNetUsage() * TRX_PRECISION * ((double)
(repo.getTotalNetWeight()) / dynamicStore.getTotalNetLimit()));

long remainNetUsage = netUsage
- ownerCapsule.getFrozenBalance()
- ownerCapsule.getAcquiredDelegatedFrozenBalanceForBandwidth()
- ownerCapsule.getAcquiredDelegatedFrozenV2BalanceForBandwidth();
long v2NetUsage = getV2NetUsage(ownerCapsule, netUsage);

remainNetUsage = Math.max(0, remainNetUsage);

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

long remainEnergyUsage = energyUsage
- ownerCapsule.getEnergyFrozenBalance()
- ownerCapsule.getAcquiredDelegatedFrozenBalanceForEnergy()
- ownerCapsule.getAcquiredDelegatedFrozenV2BalanceForEnergy();

remainEnergyUsage = Math.max(0, remainEnergyUsage);
long v2EnergyUsage = getV2EnergyUsage(ownerCapsule, energyUsage);

if (ownerCapsule.getFrozenV2BalanceForEnergy() - remainEnergyUsage < delegateBalance) {
if (ownerCapsule.getFrozenV2BalanceForEnergy() - v2EnergyUsage < delegateBalance) {
throw new ContractValidateException(
"delegateBalance must be less than available FreezeEnergyV2 balance");
"delegateBalance must be less than or equal to available FreezeEnergyV2 balance");
}
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public void validate(FreezeBalanceParam param, Repository repo) throws ContractV
if (frozenBalance <= 0) {
throw new ContractValidateException("FrozenBalance must be positive");
} else if (frozenBalance < TRX_PRECISION) {
throw new ContractValidateException("FrozenBalance must be more than 1TRX");
throw new ContractValidateException("FrozenBalance must be greater than or equal to 1 TRX");
} else if (frozenBalance > ownerCapsule.getBalance()) {
throw new ContractValidateException("FrozenBalance must be less than accountBalance");
throw new ContractValidateException("FrozenBalance must be less than or equal to accountBalance");
}

// validate frozen count of owner account
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ public void validate(FreezeBalanceV2Param param, Repository repo) throws Contrac
if (frozenBalance <= 0) {
throw new ContractValidateException("FrozenBalance must be positive");
} else if (frozenBalance < TRX_PRECISION) {
throw new ContractValidateException("FrozenBalance must be more than 1TRX");
throw new ContractValidateException("FrozenBalance must be greater than or equal to 1 TRX");
} else if (frozenBalance > ownerCapsule.getBalance()) {
throw new ContractValidateException("FrozenBalance must be less than accountBalance");
throw new ContractValidateException(
"FrozenBalance must be less than or equal to accountBalance");
}

// validate arg @resourceType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void validate(UnDelegateResourceParam param, Repository repo) throws Cont
}

byte[] receiverAddress = param.getReceiverAddress();
if (ArrayUtils.isEmpty(receiverAddress) || !DecodeUtil.addressValid(receiverAddress)) {
if (!DecodeUtil.addressValid(receiverAddress)) {
throw new ContractValidateException("Invalid receiverAddress");
}
if (Arrays.equals(receiverAddress, ownerAddress)) {
Expand Down Expand Up @@ -104,7 +104,9 @@ public void execute(UnDelegateResourceParam param, Repository repo) {
case BANDWIDTH:
BandwidthProcessor bandwidthProcessor = new BandwidthProcessor(ChainBaseManager.getInstance());
bandwidthProcessor.updateUsageForDelegated(receiverCapsule);

/* For example, in a scenario where a regular account can be upgraded to a contract
account through an interface, the account information will be cleared after the
contract suicide, and this account will be converted to a regular account in the future */
if (receiverCapsule.getAcquiredDelegatedFrozenV2BalanceForBandwidth()
< unDelegateBalance) {
// A TVM contract suicide, re-create will produce this situation
Expand Down
40 changes: 25 additions & 15 deletions actuator/src/main/java/org/tron/core/vm/utils/FreezeV2Util.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.tron.core.vm.utils;

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 java.util.List;
import java.util.stream.Collectors;

Expand All @@ -11,8 +15,8 @@
import org.tron.core.vm.config.VMConfig;
import org.tron.core.vm.repository.Repository;
import org.tron.protos.Protocol;
import org.tron.protos.contract.Common;

import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION;

public class FreezeV2Util {

Expand Down Expand Up @@ -163,13 +167,8 @@ public static long queryDelegatableResource(byte[] address, long type, Repositor
return frozenV2Resource;
}

long remainNetUsage = usage
- accountCapsule.getFrozenBalance()
- accountCapsule.getAcquiredDelegatedFrozenBalanceForBandwidth()
- accountCapsule.getAcquiredDelegatedFrozenV2BalanceForBandwidth();

remainNetUsage = Math.max(0, remainNetUsage);
return Math.max(0L, frozenV2Resource - remainNetUsage);
long v2NetUsage = getV2NetUsage(accountCapsule, usage);
return Math.max(0L, frozenV2Resource - v2NetUsage);
}

if (type == 1) {
Expand All @@ -188,13 +187,8 @@ public static long queryDelegatableResource(byte[] address, long type, Repositor
return frozenV2Resource;
}

long remainEnergyUsage = usage
- accountCapsule.getEnergyFrozenBalance()
- accountCapsule.getAcquiredDelegatedFrozenBalanceForEnergy()
- accountCapsule.getAcquiredDelegatedFrozenV2BalanceForEnergy();

remainEnergyUsage = Math.max(0, remainEnergyUsage);
return Math.max(0L, frozenV2Resource - remainEnergyUsage);
long v2EnergyUsage = getV2EnergyUsage(accountCapsule, usage);
return Math.max(0L, frozenV2Resource - v2EnergyUsage);
}

return 0L;
Expand Down Expand Up @@ -250,4 +244,20 @@ private static List<Protocol.Account.UnFreezeV2> getTotalWithdrawList(List<Proto
&& unfrozenV2.getUnfreezeExpireTime() <= now)).collect(Collectors.toList());
}

public static long getV2NetUsage(AccountCapsule ownerCapsule, long netUsage) {
long v2NetUsage= netUsage
- ownerCapsule.getFrozenBalance()
- ownerCapsule.getAcquiredDelegatedFrozenBalanceForBandwidth()
- ownerCapsule.getAcquiredDelegatedFrozenV2BalanceForBandwidth();
return Math.max(0, v2NetUsage);
}

public static long getV2EnergyUsage(AccountCapsule ownerCapsule, long energyUsage) {
long v2EnergyUsage= energyUsage
- ownerCapsule.getEnergyFrozenBalance()
- ownerCapsule.getAcquiredDelegatedFrozenBalanceForEnergy()
- ownerCapsule.getAcquiredDelegatedFrozenV2BalanceForEnergy();
return Math.max(0, v2EnergyUsage);
}

}
20 changes: 6 additions & 14 deletions framework/src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import static org.tron.core.config.Parameter.DatabaseConstants.PROPOSAL_COUNT_LIMIT_MAX;
import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.parseEnergyFee;
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.EARLIEST_STR;
import static org.tron.core.vm.utils.FreezeV2Util.getV2EnergyUsage;
import static org.tron.core.vm.utils.FreezeV2Util.getV2NetUsage;
import static org.tron.protos.contract.Common.ResourceCode;

import com.google.common.collect.ContiguousSet;
Expand Down Expand Up @@ -876,14 +878,9 @@ public long calcCanDelegatedBandWidthMaxSize(
long netUsage = (long) (accountNetUsage * TRX_PRECISION * ((double)
(dynamicStore.getTotalNetWeight()) / dynamicStore.getTotalNetLimit()));

long remainNetUsage = netUsage
- ownerCapsule.getFrozenBalance()
- ownerCapsule.getAcquiredDelegatedFrozenBalanceForBandwidth()
- ownerCapsule.getAcquiredDelegatedFrozenV2BalanceForBandwidth();
long v2NetUsage = getV2NetUsage(ownerCapsule, netUsage);

remainNetUsage = Math.max(0, remainNetUsage);

long maxSize = ownerCapsule.getFrozenV2BalanceForBandwidth() - remainNetUsage;
long maxSize = ownerCapsule.getFrozenV2BalanceForBandwidth() - v2NetUsage;
return Math.max(0, maxSize);
}

Expand All @@ -901,14 +898,9 @@ public long calcCanDelegatedEnergyMaxSize(ByteString ownerAddress) {
long energyUsage = (long) (ownerCapsule.getEnergyUsage() * TRX_PRECISION * ((double)
(dynamicStore.getTotalEnergyWeight()) / dynamicStore.getTotalEnergyCurrentLimit()));

long remainEnergyUsage = energyUsage
- ownerCapsule.getEnergyFrozenBalance()
- ownerCapsule.getAcquiredDelegatedFrozenBalanceForEnergy()
- ownerCapsule.getAcquiredDelegatedFrozenV2BalanceForEnergy();

remainEnergyUsage = Math.max(0, remainEnergyUsage);
long v2EnergyUsage = getV2EnergyUsage(ownerCapsule, energyUsage);

long maxSize = ownerCapsule.getFrozenV2BalanceForEnergy() - remainEnergyUsage;
long maxSize = ownerCapsule.getFrozenV2BalanceForEnergy() - v2EnergyUsage;
return Math.max(0, maxSize);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ public void testDelegateResourceWithNoFreeze() {
actuator.execute(ret);
Assert.fail("cannot run here.");
} catch (ContractValidateException e) {
Assert.assertEquals("delegateBalance must be less than available FreezeBandwidthV2 balance",
Assert.assertEquals(
"delegateBalance must be less than or equal to available FreezeBandwidthV2 balance",
e.getMessage());
} catch (ContractExeException e) {
Assert.fail();
Expand All @@ -193,7 +194,7 @@ public void testDelegateResourceWithNoFreeze() {
Assert.fail("cannot run here.");
} catch (ContractValidateException e) {
Assert.assertEquals(
"delegateBalance must be less than available FreezeEnergyV2 balance",
"delegateBalance must be less than or equal to available FreezeEnergyV2 balance",
e.getMessage());
} catch (ContractExeException e) {
Assert.fail(e.getMessage());
Expand Down Expand Up @@ -222,7 +223,8 @@ public void testDelegateBandwidthWithUsage() {
actuator.execute(ret);
Assert.fail("cannot run here.");
} catch (ContractValidateException e) {
Assert.assertEquals("delegateBalance must be less than available FreezeBandwidthV2 balance",
Assert.assertEquals(
"delegateBalance must be less than or equal to available FreezeBandwidthV2 balance",
e.getMessage());
} catch (ContractExeException e) {
Assert.fail(e.getMessage());
Expand Down Expand Up @@ -253,7 +255,7 @@ public void testDelegateCpuWithUsage() {
Assert.fail("cannot run here.");
} catch (ContractValidateException e) {
Assert.assertEquals(
"delegateBalance must be less than available FreezeEnergyV2 balance",
"delegateBalance must be less than or equal to available FreezeEnergyV2 balance",
e.getMessage());
} catch (ContractExeException e) {
Assert.fail(e.getMessage());
Expand Down Expand Up @@ -512,7 +514,8 @@ public void delegateLessThanZero() {
actuator.execute(ret);
Assert.fail("cannot run here.");
} catch (ContractValidateException e) {
Assert.assertEquals("delegateBalance must be more than 1TRX", e.getMessage());
Assert.assertEquals("delegateBalance must be greater than or equal to 1 TRX",
e.getMessage());
} catch (ContractExeException e) {
Assert.fail(e.getMessage());
}
Expand Down Expand Up @@ -550,7 +553,8 @@ public void delegateMoreThanBalance() {
actuator.execute(ret);
Assert.fail("cannot run here.");
} catch (ContractValidateException e) {
Assert.assertEquals("delegateBalance must be less than available FreezeBandwidthV2 balance",
Assert.assertEquals(
"delegateBalance must be less than or equal to available FreezeBandwidthV2 balance",
e.getMessage());
} catch (ContractExeException e) {
Assert.fail(e.getMessage());
Expand Down
Loading