Skip to content

Commit 1555a01

Browse files
committed
func(vm): support for CANCELALLUNFREEZEV2 details
1 parent a8ad2a1 commit 1555a01

File tree

6 files changed

+36
-5
lines changed

6 files changed

+36
-5
lines changed

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
@@ -1996,13 +1996,22 @@ public boolean cancelAllUnfreezeV2Action() {
19961996

19971997
CancelAllUnfreezeV2Processor processor = new CancelAllUnfreezeV2Processor();
19981998
processor.validate(param, repository);
1999-
long withdrawExpireBalance = processor.execute(param, repository);
1999+
Map<String, Long> result = processor.execute(param, repository);
20002000
repository.commit();
2001-
if (withdrawExpireBalance > 0) {
2001+
2002+
if (result.get(VMConstant.WITHDRAW_EXPIRE_BALANCE) > 0) {
20022003
increaseNonce();
2003-
addInternalTx(null, owner, owner, withdrawExpireBalance, null,
2004+
addInternalTx(null, owner, owner, result.get(VMConstant.WITHDRAW_EXPIRE_BALANCE), null,
20042005
"withdrawExpireUnfreezeWhileCanceling", nonce, null);
20052006
}
2007+
2008+
if (internalTx != null && CommonParameter.getInstance().saveCancelAllUnfreezeV2Details) {
2009+
internalTx.setExtra(String.format("{\"%s\":%s,\"%s\":%d,\"%s\":%d}",
2010+
BANDWIDTH.name(), result.getOrDefault(BANDWIDTH.name(), 0L),
2011+
ENERGY.name(), result.getOrDefault(ENERGY.name(), 0L),
2012+
TRON_POWER.name(), result.getOrDefault(TRON_POWER.name(), 0L)));
2013+
}
2014+
20062015
return true;
20072016
} catch (ContractValidateException e) {
20082017
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
@@ -221,6 +221,7 @@ public class Constant {
221221
public static final String VM_SAVE_INTERNAL_TX = "vm.saveInternalTx";
222222

223223
public static final String VM_SAVE_FEATURED_INTERNAL_TX = "vm.saveFeaturedInternalTx";
224+
public static final String VM_SAVE_CANCEL_ALL_UNFREEZE_V2_DETAILS = "vm.saveCancelAllUnfreezeV2Details";
224225

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

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
@@ -883,6 +883,10 @@ public static void setParam(final String[] args, final String confFileName) {
883883
config.hasPath(Constant.VM_SAVE_FEATURED_INTERNAL_TX)
884884
&& config.getBoolean(Constant.VM_SAVE_FEATURED_INTERNAL_TX);
885885

886+
PARAMETER.saveCancelAllUnfreezeV2Details =
887+
config.hasPath(Constant.VM_SAVE_CANCEL_ALL_UNFREEZE_V2_DETAILS)
888+
&& config.getBoolean(Constant.VM_SAVE_CANCEL_ALL_UNFREEZE_V2_DETAILS);
889+
886890
// PARAMETER.allowShieldedTransaction =
887891
// config.hasPath(Constant.COMMITTEE_ALLOW_SHIELDED_TRANSACTION) ? config
888892
// .getInt(Constant.COMMITTEE_ALLOW_SHIELDED_TRANSACTION) : 0;

0 commit comments

Comments
 (0)