From 1555a012434faf22056558d8d5e6683f94c0e9a7 Mon Sep 17 00:00:00 2001 From: Asuka Date: Fri, 12 Jul 2024 12:29:20 +0800 Subject: [PATCH 1/2] func(vm): support for CANCELALLUNFREEZEV2 details --- .../main/java/org/tron/core/vm/VMConstant.java | 2 ++ .../CancelAllUnfreezeV2Processor.java | 13 +++++++++++-- .../java/org/tron/core/vm/program/Program.java | 15 ++++++++++++--- .../tron/common/parameter/CommonParameter.java | 6 ++++++ common/src/main/java/org/tron/core/Constant.java | 1 + .../main/java/org/tron/core/config/args/Args.java | 4 ++++ 6 files changed, 36 insertions(+), 5 deletions(-) diff --git a/actuator/src/main/java/org/tron/core/vm/VMConstant.java b/actuator/src/main/java/org/tron/core/vm/VMConstant.java index 4e7f6b29e3f..abbb6ae6d38 100644 --- a/actuator/src/main/java/org/tron/core/vm/VMConstant.java +++ b/actuator/src/main/java/org/tron/core/vm/VMConstant.java @@ -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() { } } diff --git a/actuator/src/main/java/org/tron/core/vm/nativecontract/CancelAllUnfreezeV2Processor.java b/actuator/src/main/java/org/tron/core/vm/nativecontract/CancelAllUnfreezeV2Processor.java index 888071a177b..ec1f4363205 100644 --- a/actuator/src/main/java/org/tron/core/vm/nativecontract/CancelAllUnfreezeV2Processor.java +++ b/actuator/src/main/java/org/tron/core/vm/nativecontract/CancelAllUnfreezeV2Processor.java @@ -7,6 +7,8 @@ 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; @@ -14,6 +16,7 @@ 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 execute(CancelAllUnfreezeV2Param param, Repository repo) throws ContractExeException { + Map 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( diff --git a/actuator/src/main/java/org/tron/core/vm/program/Program.java b/actuator/src/main/java/org/tron/core/vm/program/Program.java index 0b0ef5bc9ba..25eceb09849 100644 --- a/actuator/src/main/java/org/tron/core/vm/program/Program.java +++ b/actuator/src/main/java/org/tron/core/vm/program/Program.java @@ -1996,13 +1996,22 @@ public boolean cancelAllUnfreezeV2Action() { CancelAllUnfreezeV2Processor processor = new CancelAllUnfreezeV2Processor(); processor.validate(param, repository); - long withdrawExpireBalance = processor.execute(param, repository); + Map 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\":%s,\"%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()); diff --git a/common/src/main/java/org/tron/common/parameter/CommonParameter.java b/common/src/main/java/org/tron/common/parameter/CommonParameter.java index 22159063333..1a4110a3908 100644 --- a/common/src/main/java/org/tron/common/parameter/CommonParameter.java +++ b/common/src/main/java/org/tron/common/parameter/CommonParameter.java @@ -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 diff --git a/common/src/main/java/org/tron/core/Constant.java b/common/src/main/java/org/tron/core/Constant.java index 0e634d3ef7d..fc566702ff3 100644 --- a/common/src/main/java/org/tron/core/Constant.java +++ b/common/src/main/java/org/tron/core/Constant.java @@ -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"; diff --git a/framework/src/main/java/org/tron/core/config/args/Args.java b/framework/src/main/java/org/tron/core/config/args/Args.java index 422efefaed8..0d56b06ad7e 100644 --- a/framework/src/main/java/org/tron/core/config/args/Args.java +++ b/framework/src/main/java/org/tron/core/config/args/Args.java @@ -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; From 82d52d9f26b7ccd6bb07aa5b3fb3c19257c45cb9 Mon Sep 17 00:00:00 2001 From: Asuka Date: Thu, 18 Jul 2024 10:39:16 +0800 Subject: [PATCH 2/2] func(vm): change some json format --- actuator/src/main/java/org/tron/core/vm/program/Program.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actuator/src/main/java/org/tron/core/vm/program/Program.java b/actuator/src/main/java/org/tron/core/vm/program/Program.java index 25eceb09849..a7a0719695e 100644 --- a/actuator/src/main/java/org/tron/core/vm/program/Program.java +++ b/actuator/src/main/java/org/tron/core/vm/program/Program.java @@ -2006,7 +2006,7 @@ public boolean cancelAllUnfreezeV2Action() { } if (internalTx != null && CommonParameter.getInstance().saveCancelAllUnfreezeV2Details) { - internalTx.setExtra(String.format("{\"%s\":%s,\"%s\":%d,\"%s\":%d}", + 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)));