Skip to content

Commit

Permalink
Merge pull request #5134 from lxcmyf/feature/optimize_stake2_code
Browse files Browse the repository at this point in the history
fix(freezeV2): optimize Stake2.0 code
  • Loading branch information
halibobo1205 authored May 10, 2023
2 parents 631c5df + a8fc651 commit 7414c11
Show file tree
Hide file tree
Showing 246 changed files with 5,303 additions and 7,190 deletions.
21 changes: 0 additions & 21 deletions DownloadLinks.sh

This file was deleted.

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
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 @@ -144,9 +143,8 @@ public boolean execute(Object result) throws ContractExeException {

long now = chainBaseManager.getHeadSlot();
if (Objects.nonNull(receiverCapsule) && transferUsage > 0) {
ownerCapsule.setNetUsage(processor.unDelegateIncrease(ownerCapsule, receiverCapsule,
transferUsage, BANDWIDTH, now));
ownerCapsule.setLatestConsumeTime(now);
processor.unDelegateIncrease(ownerCapsule, receiverCapsule,
transferUsage, BANDWIDTH, now);
}
}
break;
Expand All @@ -160,9 +158,7 @@ public boolean execute(Object result) throws ContractExeException {

long now = chainBaseManager.getHeadSlot();
if (Objects.nonNull(receiverCapsule) && transferUsage > 0) {
ownerCapsule.setEnergyUsage(processor.unDelegateIncrease(ownerCapsule, receiverCapsule,
transferUsage, ENERGY, now));
ownerCapsule.setLatestConsumeTimeForEnergy(now);
processor.unDelegateIncrease(ownerCapsule, receiverCapsule, transferUsage, ENERGY, now);
}
}
break;
Expand Down Expand Up @@ -240,7 +236,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
16 changes: 0 additions & 16 deletions actuator/src/main/java/org/tron/core/utils/TransactionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import org.tron.protos.Protocol.Transaction;
import org.tron.protos.Protocol.Transaction.Contract;
import org.tron.protos.Protocol.Transaction.Result.contractResult;
import org.tron.protos.Protocol.TransactionSign;
import org.tron.protos.contract.SmartContractOuterClass.CreateSmartContract;
import org.tron.protos.contract.SmartContractOuterClass.TriggerSmartContract;
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
Expand Down Expand Up @@ -184,21 +183,6 @@ public static String makeUpperCamelMethod(String originName) {
.replace("_", "");
}

public static TransactionCapsule getTransactionSign(TransactionSign transactionSign) {
byte[] privateKey = transactionSign.getPrivateKey().toByteArray();
TransactionCapsule trx = new TransactionCapsule(transactionSign.getTransaction());
trx.sign(privateKey);
return trx;
}

public TransactionCapsule addSign(TransactionSign transactionSign)
throws PermissionException, SignatureException, SignatureFormatException {
byte[] privateKey = transactionSign.getPrivateKey().toByteArray();
TransactionCapsule trx = new TransactionCapsule(transactionSign.getTransaction());
trx.addSign(privateKey, chainBaseManager.getAccountStore());
return trx;
}

public TransactionSignWeight getTransactionSignWeight(Transaction trx) {
TransactionSignWeight.Builder tswBuilder = TransactionSignWeight.newBuilder();
TransactionExtention.Builder trxExBuilder = TransactionExtention.newBuilder();
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,22 +78,17 @@ 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;
default:
throw new ContractValidateException(
"ResourceCode error, valid ResourceCode[BANDWIDTH、ENERGY]");
"Unknown ResourceCode, valid ResourceCode[BANDWIDTH、ENERGY]");
}

byte[] receiverAddress = param.getReceiverAddress();
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 All @@ -48,7 +48,7 @@ public void validate(FreezeBalanceParam param, Repository repo) throws ContractV
break;
default:
throw new ContractValidateException(
"ResourceCode error,valid ResourceCode[BANDWIDTH、ENERGY]");
"Unknown ResourceCode, valid ResourceCode[BANDWIDTH、ENERGY]");
}

// validate for delegating resource
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package org.tron.core.vm.nativecontract;

import static org.tron.core.actuator.ActuatorConstant.ACCOUNT_EXCEPTION_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.protos.contract.Common.ResourceCode.BANDWIDTH;
import static org.tron.protos.contract.Common.ResourceCode.ENERGY;

import lombok.extern.slf4j.Slf4j;
import org.tron.common.utils.DecodeUtil;
import org.tron.common.utils.StringUtil;
Expand All @@ -9,12 +15,6 @@
import org.tron.core.vm.nativecontract.param.FreezeBalanceV2Param;
import org.tron.core.vm.repository.Repository;

import static org.tron.core.actuator.ActuatorConstant.ACCOUNT_EXCEPTION_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.protos.contract.Common.ResourceCode.BANDWIDTH;
import static org.tron.protos.contract.Common.ResourceCode.ENERGY;

@Slf4j(topic = "VMProcessor")
public class FreezeBalanceV2Processor {

Expand All @@ -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 All @@ -50,16 +51,16 @@ public void validate(FreezeBalanceV2Param param, Repository repo) throws Contrac
case TRON_POWER:
if (!repo.getDynamicPropertiesStore().supportAllowNewResourceModel()) {
throw new ContractValidateException(
"ResourceCode error, valid ResourceCode[BANDWIDTH、ENERGY]");
"Unknown ResourceCode, valid ResourceCode[BANDWIDTH、ENERGY]");
}
break;
default:
if (repo.getDynamicPropertiesStore().supportAllowNewResourceModel()) {
throw new ContractValidateException(
"ResourceCode error, valid ResourceCode[BANDWIDTH、ENERGY、TRON_POWER]");
"Unknown ResourceCode, valid ResourceCode[BANDWIDTH、ENERGY、TRON_POWER]");
} else {
throw new ContractValidateException(
"ResourceCode error, valid ResourceCode[BANDWIDTH、ENERGY]");
"Unknown ResourceCode, valid ResourceCode[BANDWIDTH、ENERGY]");
}
}
}
Expand Down
Loading

0 comments on commit 7414c11

Please sign in to comment.