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

func(vm): support for CANCELALLUNFREEZEV2 details #5911

Closed
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
2 changes: 2 additions & 0 deletions actuator/src/main/java/org/tron/core/vm/VMConstant.java
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@ public class VMConstant {
public static final int ONE_THOUSAND = 1000;
public static final long SUN_PER_ENERGY = 100;

public static final String WITHDRAW_EXPIRE_BALANCE = "WithdrawExpireBalance";

private VMConstant() {
}
}
Original file line number Diff line number Diff line change
@@ -7,13 +7,16 @@
import static org.tron.protos.contract.Common.ResourceCode.BANDWIDTH;
import static org.tron.protos.contract.Common.ResourceCode.ENERGY;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.tron.common.utils.DecodeUtil;
import org.tron.common.utils.StringUtil;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.vm.VMConstant;
import org.tron.core.vm.nativecontract.param.CancelAllUnfreezeV2Param;
import org.tron.core.vm.repository.Repository;
import org.tron.protos.Protocol;
@@ -38,13 +41,17 @@ public void validate(CancelAllUnfreezeV2Param param, Repository repo) throws Con
}
}

public long execute(CancelAllUnfreezeV2Param param, Repository repo) throws ContractExeException {
public Map<String, Long> execute(CancelAllUnfreezeV2Param param, Repository repo) throws ContractExeException {
Map<String, Long> result = new HashMap<>();
byte[] ownerAddress = param.getOwnerAddress();
AccountCapsule ownerCapsule = repo.getAccount(ownerAddress);
long now = repo.getDynamicPropertiesStore().getLatestBlockHeaderTimestamp();
long withdrawExpireBalance = 0L;
for (Protocol.Account.UnFreezeV2 unFreezeV2: ownerCapsule.getUnfrozenV2List()) {
if (unFreezeV2.getUnfreezeExpireTime() > now) {
String resourceName = unFreezeV2.getType().name();
result.put(resourceName, result.getOrDefault(resourceName, 0L) + unFreezeV2.getUnfreezeAmount());

updateFrozenInfoAndTotalResourceWeight(ownerCapsule, unFreezeV2, repo);
} else {
// withdraw
@@ -57,7 +64,9 @@ public long execute(CancelAllUnfreezeV2Param param, Repository repo) throws Cont
ownerCapsule.clearUnfrozenV2();

repo.updateAccount(ownerCapsule.createDbKey(), ownerCapsule);
return withdrawExpireBalance;

result.put(VMConstant.WITHDRAW_EXPIRE_BALANCE, withdrawExpireBalance);
return result;
}

public void updateFrozenInfoAndTotalResourceWeight(
15 changes: 12 additions & 3 deletions actuator/src/main/java/org/tron/core/vm/program/Program.java
Original file line number Diff line number Diff line change
@@ -1996,13 +1996,22 @@ public boolean cancelAllUnfreezeV2Action() {

CancelAllUnfreezeV2Processor processor = new CancelAllUnfreezeV2Processor();
processor.validate(param, repository);
long withdrawExpireBalance = processor.execute(param, repository);
Map<String, Long> result = processor.execute(param, repository);
repository.commit();
if (withdrawExpireBalance > 0) {

if (result.get(VMConstant.WITHDRAW_EXPIRE_BALANCE) > 0) {
increaseNonce();
addInternalTx(null, owner, owner, withdrawExpireBalance, null,
addInternalTx(null, owner, owner, result.get(VMConstant.WITHDRAW_EXPIRE_BALANCE), null,
"withdrawExpireUnfreezeWhileCanceling", nonce, null);
}

if (internalTx != null && CommonParameter.getInstance().saveCancelAllUnfreezeV2Details) {
internalTx.setExtra(String.format("{\"%s\":%d,\"%s\":%d,\"%s\":%d}",
BANDWIDTH.name(), result.getOrDefault(BANDWIDTH.name(), 0L),
ENERGY.name(), result.getOrDefault(ENERGY.name(), 0L),
TRON_POWER.name(), result.getOrDefault(TRON_POWER.name(), 0L)));
}

return true;
} catch (ContractValidateException e) {
logger.warn("TVM CancelAllUnfreezeV2: validate failure. Reason: {}", e.getMessage());
Original file line number Diff line number Diff line change
@@ -88,6 +88,12 @@ public class CommonParameter {
public boolean saveFeaturedInternalTx;
@Getter
@Setter
@Parameter(names = {"--save-cancel-all-unfreeze-v2-details"}, description = "Record the details of the internal "
+ "transactions generated by the CANCELALLUNFREEZEV2 opcode, such as bandwidth/energy/tronpower cancel amount. "
+ "(default: false)")
public boolean saveCancelAllUnfreezeV2Details;
@Getter
@Setter
@Parameter(names = {"--long-running-time"})
public int longRunningTime = 10;
@Getter
1 change: 1 addition & 0 deletions common/src/main/java/org/tron/core/Constant.java
Original file line number Diff line number Diff line change
@@ -221,6 +221,7 @@ public class Constant {
public static final String VM_SAVE_INTERNAL_TX = "vm.saveInternalTx";

public static final String VM_SAVE_FEATURED_INTERNAL_TX = "vm.saveFeaturedInternalTx";
public static final String VM_SAVE_CANCEL_ALL_UNFREEZE_V2_DETAILS = "vm.saveCancelAllUnfreezeV2Details";

// public static final String COMMITTEE_ALLOW_SHIELDED_TRANSACTION = "committee.allowShieldedTransaction";

4 changes: 4 additions & 0 deletions framework/src/main/java/org/tron/core/config/args/Args.java
Original file line number Diff line number Diff line change
@@ -883,6 +883,10 @@ public static void setParam(final String[] args, final String confFileName) {
config.hasPath(Constant.VM_SAVE_FEATURED_INTERNAL_TX)
&& config.getBoolean(Constant.VM_SAVE_FEATURED_INTERNAL_TX);

PARAMETER.saveCancelAllUnfreezeV2Details =
config.hasPath(Constant.VM_SAVE_CANCEL_ALL_UNFREEZE_V2_DETAILS)
&& config.getBoolean(Constant.VM_SAVE_CANCEL_ALL_UNFREEZE_V2_DETAILS);

// PARAMETER.allowShieldedTransaction =
// config.hasPath(Constant.COMMITTEE_ALLOW_SHIELDED_TRANSACTION) ? config
// .getInt(Constant.COMMITTEE_ALLOW_SHIELDED_TRANSACTION) : 0;
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.tron.common.application.TronApplicationContext;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.runtime.Runtime;
import org.tron.common.runtime.RuntimeImpl;
import org.tron.common.runtime.TVMTestResult;
@@ -274,6 +275,7 @@ private TVMTestResult triggerWithdrawExpireUnfreeze(
private TVMTestResult triggerCancelAllUnfreezeV2(
byte[] callerAddr, byte[] contractAddr, contractResult expectedResult, Consumer<byte[]> check)
throws Exception {
CommonParameter.getInstance().saveCancelAllUnfreezeV2Details = true;
return triggerContract(
callerAddr, contractAddr, fee, expectedResult, check, "cancelAllUnfreezeBalanceV2()");
}