Skip to content

Commit 6660adb

Browse files
Merge pull request #6191 from yanghang8612/feature/support_cancel_details
func(vm): support for CANCELALLUNFREEZEV2 details
2 parents 3938504 + a3109e5 commit 6660adb

7 files changed

Lines changed: 38 additions & 5 deletions

File tree

actuator/src/main/java/org/tron/core/vm/VMConstant.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class VMConstant {
1010
public static final int ONE_THOUSAND = 1000;
1111
public static final long SUN_PER_ENERGY = 100;
1212

13+
public static final String WITHDRAW_EXPIRE_BALANCE = "WithdrawExpireBalance";
14+
1315
private VMConstant() {
1416
}
1517
}

actuator/src/main/java/org/tron/core/vm/nativecontract/CancelAllUnfreezeV2Processor.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
import static org.tron.protos.contract.Common.ResourceCode.BANDWIDTH;
88
import static org.tron.protos.contract.Common.ResourceCode.ENERGY;
99

10+
import java.util.HashMap;
11+
import java.util.Map;
1012
import java.util.Objects;
1113
import lombok.extern.slf4j.Slf4j;
1214
import org.tron.common.utils.DecodeUtil;
1315
import org.tron.common.utils.StringUtil;
1416
import org.tron.core.capsule.AccountCapsule;
1517
import org.tron.core.exception.ContractExeException;
1618
import org.tron.core.exception.ContractValidateException;
19+
import org.tron.core.vm.VMConstant;
1720
import org.tron.core.vm.nativecontract.param.CancelAllUnfreezeV2Param;
1821
import org.tron.core.vm.repository.Repository;
1922
import org.tron.protos.Protocol;
@@ -38,13 +41,17 @@ public void validate(CancelAllUnfreezeV2Param param, Repository repo) throws Con
3841
}
3942
}
4043

41-
public long execute(CancelAllUnfreezeV2Param param, Repository repo) throws ContractExeException {
44+
public Map<String, Long> execute(CancelAllUnfreezeV2Param param, Repository repo) throws ContractExeException {
45+
Map<String, Long> result = new HashMap<>();
4246
byte[] ownerAddress = param.getOwnerAddress();
4347
AccountCapsule ownerCapsule = repo.getAccount(ownerAddress);
4448
long now = repo.getDynamicPropertiesStore().getLatestBlockHeaderTimestamp();
4549
long withdrawExpireBalance = 0L;
4650
for (Protocol.Account.UnFreezeV2 unFreezeV2: ownerCapsule.getUnfrozenV2List()) {
4751
if (unFreezeV2.getUnfreezeExpireTime() > now) {
52+
String resourceName = unFreezeV2.getType().name();
53+
result.put(resourceName, result.getOrDefault(resourceName, 0L) + unFreezeV2.getUnfreezeAmount());
54+
4855
updateFrozenInfoAndTotalResourceWeight(ownerCapsule, unFreezeV2, repo);
4956
} else {
5057
// withdraw
@@ -57,7 +64,9 @@ public long execute(CancelAllUnfreezeV2Param param, Repository repo) throws Cont
5764
ownerCapsule.clearUnfrozenV2();
5865

5966
repo.updateAccount(ownerCapsule.createDbKey(), ownerCapsule);
60-
return withdrawExpireBalance;
67+
68+
result.put(VMConstant.WITHDRAW_EXPIRE_BALANCE, withdrawExpireBalance);
69+
return result;
6170
}
6271

6372
public void updateFrozenInfoAndTotalResourceWeight(

actuator/src/main/java/org/tron/core/vm/program/Program.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,13 +2006,22 @@ public boolean cancelAllUnfreezeV2Action() {
20062006

20072007
CancelAllUnfreezeV2Processor processor = new CancelAllUnfreezeV2Processor();
20082008
processor.validate(param, repository);
2009-
long withdrawExpireBalance = processor.execute(param, repository);
2009+
Map<String, Long> result = processor.execute(param, repository);
20102010
repository.commit();
2011-
if (withdrawExpireBalance > 0) {
2011+
2012+
if (result.get(VMConstant.WITHDRAW_EXPIRE_BALANCE) > 0) {
20122013
increaseNonce();
2013-
addInternalTx(null, owner, owner, withdrawExpireBalance, null,
2014+
addInternalTx(null, owner, owner, result.get(VMConstant.WITHDRAW_EXPIRE_BALANCE), null,
20142015
"withdrawExpireUnfreezeWhileCanceling", nonce, null);
20152016
}
2017+
2018+
if (internalTx != null && CommonParameter.getInstance().saveCancelAllUnfreezeV2Details) {
2019+
internalTx.setExtra(String.format("{\"%s\":%d,\"%s\":%d,\"%s\":%d}",
2020+
BANDWIDTH.name(), result.getOrDefault(BANDWIDTH.name(), 0L),
2021+
ENERGY.name(), result.getOrDefault(ENERGY.name(), 0L),
2022+
TRON_POWER.name(), result.getOrDefault(TRON_POWER.name(), 0L)));
2023+
}
2024+
20162025
return true;
20172026
} catch (ContractValidateException e) {
20182027
logger.warn("TVM CancelAllUnfreezeV2: validate failure. Reason: {}", e.getMessage());

common/src/main/java/org/tron/common/parameter/CommonParameter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ public class CommonParameter {
8888
public boolean saveFeaturedInternalTx;
8989
@Getter
9090
@Setter
91+
@Parameter(names = {"--save-cancel-all-unfreeze-v2-details"}, description = "Record the details of the internal "
92+
+ "transactions generated by the CANCELALLUNFREEZEV2 opcode, such as bandwidth/energy/tronpower cancel amount. "
93+
+ "(default: false)")
94+
public boolean saveCancelAllUnfreezeV2Details;
95+
@Getter
96+
@Setter
9197
@Parameter(names = {"--long-running-time"})
9298
public int longRunningTime = 10;
9399
@Getter

common/src/main/java/org/tron/core/Constant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ public class Constant {
229229
public static final String VM_SAVE_INTERNAL_TX = "vm.saveInternalTx";
230230

231231
public static final String VM_SAVE_FEATURED_INTERNAL_TX = "vm.saveFeaturedInternalTx";
232+
public static final String VM_SAVE_CANCEL_ALL_UNFREEZE_V2_DETAILS = "vm.saveCancelAllUnfreezeV2Details";
232233

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

framework/src/main/java/org/tron/core/config/args/Args.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,10 @@ public static void setParam(final Config config) {
923923
config.hasPath(Constant.VM_SAVE_FEATURED_INTERNAL_TX)
924924
&& config.getBoolean(Constant.VM_SAVE_FEATURED_INTERNAL_TX);
925925

926+
PARAMETER.saveCancelAllUnfreezeV2Details =
927+
config.hasPath(Constant.VM_SAVE_CANCEL_ALL_UNFREEZE_V2_DETAILS)
928+
&& config.getBoolean(Constant.VM_SAVE_CANCEL_ALL_UNFREEZE_V2_DETAILS);
929+
926930
// PARAMETER.allowShieldedTransaction =
927931
// config.hasPath(Constant.COMMITTEE_ALLOW_SHIELDED_TRANSACTION) ? config
928932
// .getInt(Constant.COMMITTEE_ALLOW_SHIELDED_TRANSACTION) : 0;

framework/src/test/java/org/tron/common/runtime/vm/FreezeV2Test.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.junit.Test;
2424
import org.junit.rules.TemporaryFolder;
2525
import org.tron.common.application.TronApplicationContext;
26+
import org.tron.common.parameter.CommonParameter;
2627
import org.tron.common.runtime.Runtime;
2728
import org.tron.common.runtime.RuntimeImpl;
2829
import org.tron.common.runtime.TVMTestResult;
@@ -275,6 +276,7 @@ private TVMTestResult triggerWithdrawExpireUnfreeze(
275276
private TVMTestResult triggerCancelAllUnfreezeV2(
276277
byte[] callerAddr, byte[] contractAddr, contractResult expectedResult, Consumer<byte[]> check)
277278
throws Exception {
279+
CommonParameter.getInstance().saveCancelAllUnfreezeV2Details = true;
278280
return triggerContract(
279281
callerAddr, contractAddr, fee, expectedResult, check, "cancelAllUnfreezeBalanceV2()");
280282
}

0 commit comments

Comments
 (0)