Skip to content

Commit fa0310a

Browse files
committed
feat(freezeV2): distinguish resource types for cancelUnfreezeV2Amount
1 parent 6c20014 commit fa0310a

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

actuator/src/main/java/org/tron/core/actuator/CancelAllUnfreezeV2Actuator.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION;
66
import static org.tron.protos.contract.Common.ResourceCode.BANDWIDTH;
77
import static org.tron.protos.contract.Common.ResourceCode.ENERGY;
8+
import static org.tron.protos.contract.Common.ResourceCode.TRON_POWER;
89

910
import com.google.protobuf.ByteString;
1011
import com.google.protobuf.InvalidProtocolBufferException;
12+
import java.util.HashMap;
1113
import java.util.List;
14+
import java.util.Map;
1215
import java.util.Objects;
1316
import java.util.concurrent.atomic.AtomicLong;
1417
import lombok.extern.slf4j.Slf4j;
18+
import org.apache.commons.lang3.tuple.Pair;
1519
import org.apache.commons.lang3.tuple.Triple;
1620
import org.tron.common.utils.DecodeUtil;
1721
import org.tron.common.utils.StringUtil;
@@ -55,8 +59,11 @@ public boolean execute(Object result) throws ContractExeException {
5559
long now = dynamicStore.getLatestBlockHeaderTimestamp();
5660
AtomicLong atomicWithdrawExpireBalance = new AtomicLong(0L);
5761
AtomicLong atomicCancelBalance = new AtomicLong(0L);
58-
Triple<AtomicLong, AtomicLong, AtomicLong> triple =
59-
Triple.of(new AtomicLong(0L), new AtomicLong(0L), new AtomicLong(0L));
62+
Triple<Pair<AtomicLong, AtomicLong>, Pair<AtomicLong, AtomicLong>, Pair<AtomicLong, AtomicLong>>
63+
triple = Triple.of(
64+
Pair.of(new AtomicLong(0L), new AtomicLong(0L)),
65+
Pair.of(new AtomicLong(0L), new AtomicLong(0L)),
66+
Pair.of(new AtomicLong(0L), new AtomicLong(0L)));
6067
for (UnFreezeV2 unFreezeV2 : unfrozenV2List) {
6168
updateAndCalculate(triple, ownerCapsule, now, atomicWithdrawExpireBalance,
6269
atomicCancelBalance, unFreezeV2);
@@ -72,18 +79,26 @@ public boolean execute(Object result) throws ContractExeException {
7279
accountStore.put(ownerCapsule.createDbKey(), ownerCapsule);
7380
ret.setWithdrawExpireAmount(withdrawExpireBalance);
7481
ret.setCancelAllUnfreezeV2Amount(atomicCancelBalance.get());
82+
Map<String, Long> cancelUnfreezeV2AmountMap = new HashMap<>();
83+
cancelUnfreezeV2AmountMap.put(BANDWIDTH.name(), triple.getLeft().getRight().get());
84+
cancelUnfreezeV2AmountMap.put(ENERGY.name(), triple.getMiddle().getRight().get());
85+
cancelUnfreezeV2AmountMap.put(TRON_POWER.name(), triple.getRight().getRight().get());
86+
ret.putAllCancelUnfreezeV2AmountMap(cancelUnfreezeV2AmountMap);
7587
ret.setStatus(fee, code.SUCESS);
7688
return true;
7789
}
7890

7991
private void addTotalResourceWeight(DynamicPropertiesStore dynamicStore,
80-
Triple<AtomicLong, AtomicLong, AtomicLong> triple) {
81-
dynamicStore.addTotalNetWeight(triple.getLeft().get());
82-
dynamicStore.addTotalEnergyWeight(triple.getMiddle().get());
83-
dynamicStore.addTotalTronPowerWeight(triple.getRight().get());
92+
Triple<Pair<AtomicLong, AtomicLong>,
93+
Pair<AtomicLong, AtomicLong>,
94+
Pair<AtomicLong, AtomicLong>> triple) {
95+
dynamicStore.addTotalNetWeight(triple.getLeft().getLeft().get());
96+
dynamicStore.addTotalEnergyWeight(triple.getMiddle().getLeft().get());
97+
dynamicStore.addTotalTronPowerWeight(triple.getRight().getLeft().get());
8498
}
8599

86-
private void updateAndCalculate(Triple<AtomicLong, AtomicLong, AtomicLong> triple,
100+
private void updateAndCalculate(Triple<Pair<AtomicLong, AtomicLong>, Pair<AtomicLong, AtomicLong>,
101+
Pair<AtomicLong, AtomicLong>> triple,
87102
AccountCapsule ownerCapsule, long now, AtomicLong atomicLong, AtomicLong cancelBalance,
88103
UnFreezeV2 unFreezeV2) {
89104
if (unFreezeV2.getUnfreezeExpireTime() > now) {
@@ -160,25 +175,29 @@ public long calcFee() {
160175

161176
public void updateFrozenInfoAndTotalResourceWeight(
162177
AccountCapsule accountCapsule, UnFreezeV2 unFreezeV2,
163-
Triple<AtomicLong, AtomicLong, AtomicLong> triple) {
178+
Triple<Pair<AtomicLong, AtomicLong>, Pair<AtomicLong, AtomicLong>,
179+
Pair<AtomicLong, AtomicLong>> triple) {
164180
switch (unFreezeV2.getType()) {
165181
case BANDWIDTH:
166182
long oldNetWeight = accountCapsule.getFrozenV2BalanceWithDelegated(BANDWIDTH) / TRX_PRECISION;
167183
accountCapsule.addFrozenBalanceForBandwidthV2(unFreezeV2.getUnfreezeAmount());
168184
long newNetWeight = accountCapsule.getFrozenV2BalanceWithDelegated(BANDWIDTH) / TRX_PRECISION;
169-
triple.getLeft().addAndGet(newNetWeight - oldNetWeight);
185+
triple.getLeft().getLeft().addAndGet(newNetWeight - oldNetWeight);
186+
triple.getLeft().getRight().addAndGet(unFreezeV2.getUnfreezeAmount());
170187
break;
171188
case ENERGY:
172189
long oldEnergyWeight = accountCapsule.getFrozenV2BalanceWithDelegated(ENERGY) / TRX_PRECISION;
173190
accountCapsule.addFrozenBalanceForEnergyV2(unFreezeV2.getUnfreezeAmount());
174191
long newEnergyWeight = accountCapsule.getFrozenV2BalanceWithDelegated(ENERGY) / TRX_PRECISION;
175-
triple.getMiddle().addAndGet(newEnergyWeight - oldEnergyWeight);
192+
triple.getMiddle().getLeft().addAndGet(newEnergyWeight - oldEnergyWeight);
193+
triple.getMiddle().getRight().addAndGet(unFreezeV2.getUnfreezeAmount());
176194
break;
177195
case TRON_POWER:
178196
long oldTPWeight = accountCapsule.getTronPowerFrozenV2Balance() / TRX_PRECISION;
179197
accountCapsule.addFrozenForTronPowerV2(unFreezeV2.getUnfreezeAmount());
180198
long newTPWeight = accountCapsule.getTronPowerFrozenV2Balance() / TRX_PRECISION;
181-
triple.getRight().addAndGet(newTPWeight - oldTPWeight);
199+
triple.getRight().getLeft().addAndGet(newTPWeight - oldTPWeight);
200+
triple.getRight().getRight().addAndGet(unFreezeV2.getUnfreezeAmount());
182201
break;
183202
default:
184203
break;

chainbase/src/main/java/org/tron/core/capsule/TransactionResultCapsule.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.protobuf.ByteString;
44
import com.google.protobuf.InvalidProtocolBufferException;
55
import java.util.List;
6+
import java.util.Map;
67
import lombok.extern.slf4j.Slf4j;
78
import org.tron.core.exception.BadItemException;
89
import org.tron.protos.Protocol.MarketOrderDetail;
@@ -98,6 +99,15 @@ public void setCancelAllUnfreezeV2Amount(long amount) {
9899
.setCancelAllUnfreezeV2Amount(amount).build();
99100
}
100101

102+
public Map<String, Long> getCancelUnfreezeV2AmountMap() {
103+
return transactionResult.getCancelUnfreezeV2AmountMap();
104+
}
105+
106+
public void putAllCancelUnfreezeV2AmountMap(Map<String, Long> map) {
107+
this.transactionResult = this.transactionResult.toBuilder()
108+
.putAllCancelUnfreezeV2Amount(map).build();
109+
}
110+
101111
public long getExchangeReceivedAmount() {
102112
return transactionResult.getExchangeReceivedAmount();
103113
}

chainbase/src/main/java/org/tron/core/capsule/utils/TransactionUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public static TransactionInfoCapsule buildTransactionInfoInstance(TransactionCap
101101
builder.setWithdrawAmount(programResult.getRet().getWithdrawAmount());
102102
builder.setWithdrawExpireAmount(programResult.getRet().getWithdrawExpireAmount());
103103
builder.setCancelAllUnfreezeV2Amount(programResult.getRet().getCancelAllUnfreezeV2Amount());
104+
builder.putAllCancelUnfreezeV2Amount(programResult.getRet().getCancelUnfreezeV2AmountMap());
104105
builder.setExchangeReceivedAmount(programResult.getRet().getExchangeReceivedAmount());
105106
builder.setExchangeInjectAnotherAmount(programResult.getRet().getExchangeInjectAnotherAmount());
106107
builder.setExchangeWithdrawAnotherAmount(

framework/src/test/java/org/tron/core/actuator/CancelAllUnfreezeV2ActuatorTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import com.google.protobuf.Any;
1111
import com.google.protobuf.ByteString;
12+
import java.util.Map;
1213
import lombok.extern.slf4j.Slf4j;
1314
import org.junit.Before;
1415
import org.junit.Test;
@@ -81,8 +82,13 @@ public void testCancelAllUnfreezeV2() {
8182
assertEquals(SUCESS, ret.getInstance().getRet());
8283
AccountCapsule owner = dbManager.getAccountStore()
8384
.get(ByteArray.fromHexString(OWNER_ADDRESS));
85+
Map<String, Long> cancelUnfreezeV2AmountMap = ret.getInstance()
86+
.getCancelUnfreezeV2AmountMap();
8487
assertEquals(2000000L, ret.getInstance().getWithdrawExpireAmount());
8588
assertEquals(0, owner.getUnfrozenV2List().size());
89+
assertEquals(8000000L, cancelUnfreezeV2AmountMap.get("BANDWIDTH").longValue());
90+
assertEquals(8000000L, cancelUnfreezeV2AmountMap.get("ENERGY").longValue());
91+
assertEquals(0, cancelUnfreezeV2AmountMap.get("TRON_POWER").longValue());
8692
} catch (ContractValidateException | ContractExeException e) {
8793
fail();
8894
}

protocol/src/main/protos/core/Tron.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ message Transaction {
424424
repeated MarketOrderDetail orderDetails = 26;
425425
int64 withdraw_expire_amount = 27;
426426
int64 cancel_all_unfreezeV2_amount = 28;
427+
map<string, int64> cancel_unfreezeV2_amount = 29;
427428
}
428429

429430
message raw {
@@ -485,6 +486,7 @@ message TransactionInfo {
485486

486487
int64 withdraw_expire_amount = 28;
487488
int64 cancel_all_unfreezeV2_amount = 29;
489+
map<string, int64> cancel_unfreezeV2_amount = 30;
488490
}
489491

490492
message TransactionRet {

0 commit comments

Comments
 (0)