Skip to content

Commit

Permalink
feat(freezeV2): distinguish resource types for cancelUnfreezeV2Amount
Browse files Browse the repository at this point in the history
  • Loading branch information
lxcmyf committed Jun 15, 2023
1 parent 6c20014 commit fa0310a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION;
import static org.tron.protos.contract.Common.ResourceCode.BANDWIDTH;
import static org.tron.protos.contract.Common.ResourceCode.ENERGY;
import static org.tron.protos.contract.Common.ResourceCode.TRON_POWER;

import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import org.tron.common.utils.DecodeUtil;
import org.tron.common.utils.StringUtil;
Expand Down Expand Up @@ -55,8 +59,11 @@ public boolean execute(Object result) throws ContractExeException {
long now = dynamicStore.getLatestBlockHeaderTimestamp();
AtomicLong atomicWithdrawExpireBalance = new AtomicLong(0L);
AtomicLong atomicCancelBalance = new AtomicLong(0L);
Triple<AtomicLong, AtomicLong, AtomicLong> triple =
Triple.of(new AtomicLong(0L), new AtomicLong(0L), new AtomicLong(0L));
Triple<Pair<AtomicLong, AtomicLong>, Pair<AtomicLong, AtomicLong>, Pair<AtomicLong, AtomicLong>>
triple = Triple.of(
Pair.of(new AtomicLong(0L), new AtomicLong(0L)),
Pair.of(new AtomicLong(0L), new AtomicLong(0L)),
Pair.of(new AtomicLong(0L), new AtomicLong(0L)));
for (UnFreezeV2 unFreezeV2 : unfrozenV2List) {
updateAndCalculate(triple, ownerCapsule, now, atomicWithdrawExpireBalance,
atomicCancelBalance, unFreezeV2);
Expand All @@ -72,18 +79,26 @@ public boolean execute(Object result) throws ContractExeException {
accountStore.put(ownerCapsule.createDbKey(), ownerCapsule);
ret.setWithdrawExpireAmount(withdrawExpireBalance);
ret.setCancelAllUnfreezeV2Amount(atomicCancelBalance.get());
Map<String, Long> cancelUnfreezeV2AmountMap = new HashMap<>();
cancelUnfreezeV2AmountMap.put(BANDWIDTH.name(), triple.getLeft().getRight().get());
cancelUnfreezeV2AmountMap.put(ENERGY.name(), triple.getMiddle().getRight().get());
cancelUnfreezeV2AmountMap.put(TRON_POWER.name(), triple.getRight().getRight().get());
ret.putAllCancelUnfreezeV2AmountMap(cancelUnfreezeV2AmountMap);
ret.setStatus(fee, code.SUCESS);
return true;
}

private void addTotalResourceWeight(DynamicPropertiesStore dynamicStore,
Triple<AtomicLong, AtomicLong, AtomicLong> triple) {
dynamicStore.addTotalNetWeight(triple.getLeft().get());
dynamicStore.addTotalEnergyWeight(triple.getMiddle().get());
dynamicStore.addTotalTronPowerWeight(triple.getRight().get());
Triple<Pair<AtomicLong, AtomicLong>,
Pair<AtomicLong, AtomicLong>,
Pair<AtomicLong, AtomicLong>> triple) {
dynamicStore.addTotalNetWeight(triple.getLeft().getLeft().get());
dynamicStore.addTotalEnergyWeight(triple.getMiddle().getLeft().get());
dynamicStore.addTotalTronPowerWeight(triple.getRight().getLeft().get());
}

private void updateAndCalculate(Triple<AtomicLong, AtomicLong, AtomicLong> triple,
private void updateAndCalculate(Triple<Pair<AtomicLong, AtomicLong>, Pair<AtomicLong, AtomicLong>,
Pair<AtomicLong, AtomicLong>> triple,
AccountCapsule ownerCapsule, long now, AtomicLong atomicLong, AtomicLong cancelBalance,
UnFreezeV2 unFreezeV2) {
if (unFreezeV2.getUnfreezeExpireTime() > now) {
Expand Down Expand Up @@ -160,25 +175,29 @@ public long calcFee() {

public void updateFrozenInfoAndTotalResourceWeight(
AccountCapsule accountCapsule, UnFreezeV2 unFreezeV2,
Triple<AtomicLong, AtomicLong, AtomicLong> triple) {
Triple<Pair<AtomicLong, AtomicLong>, Pair<AtomicLong, AtomicLong>,
Pair<AtomicLong, AtomicLong>> triple) {
switch (unFreezeV2.getType()) {
case BANDWIDTH:
long oldNetWeight = accountCapsule.getFrozenV2BalanceWithDelegated(BANDWIDTH) / TRX_PRECISION;
accountCapsule.addFrozenBalanceForBandwidthV2(unFreezeV2.getUnfreezeAmount());
long newNetWeight = accountCapsule.getFrozenV2BalanceWithDelegated(BANDWIDTH) / TRX_PRECISION;
triple.getLeft().addAndGet(newNetWeight - oldNetWeight);
triple.getLeft().getLeft().addAndGet(newNetWeight - oldNetWeight);
triple.getLeft().getRight().addAndGet(unFreezeV2.getUnfreezeAmount());
break;
case ENERGY:
long oldEnergyWeight = accountCapsule.getFrozenV2BalanceWithDelegated(ENERGY) / TRX_PRECISION;
accountCapsule.addFrozenBalanceForEnergyV2(unFreezeV2.getUnfreezeAmount());
long newEnergyWeight = accountCapsule.getFrozenV2BalanceWithDelegated(ENERGY) / TRX_PRECISION;
triple.getMiddle().addAndGet(newEnergyWeight - oldEnergyWeight);
triple.getMiddle().getLeft().addAndGet(newEnergyWeight - oldEnergyWeight);
triple.getMiddle().getRight().addAndGet(unFreezeV2.getUnfreezeAmount());
break;
case TRON_POWER:
long oldTPWeight = accountCapsule.getTronPowerFrozenV2Balance() / TRX_PRECISION;
accountCapsule.addFrozenForTronPowerV2(unFreezeV2.getUnfreezeAmount());
long newTPWeight = accountCapsule.getTronPowerFrozenV2Balance() / TRX_PRECISION;
triple.getRight().addAndGet(newTPWeight - oldTPWeight);
triple.getRight().getLeft().addAndGet(newTPWeight - oldTPWeight);
triple.getRight().getRight().addAndGet(unFreezeV2.getUnfreezeAmount());
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.tron.core.exception.BadItemException;
import org.tron.protos.Protocol.MarketOrderDetail;
Expand Down Expand Up @@ -98,6 +99,15 @@ public void setCancelAllUnfreezeV2Amount(long amount) {
.setCancelAllUnfreezeV2Amount(amount).build();
}

public Map<String, Long> getCancelUnfreezeV2AmountMap() {
return transactionResult.getCancelUnfreezeV2AmountMap();
}

public void putAllCancelUnfreezeV2AmountMap(Map<String, Long> map) {
this.transactionResult = this.transactionResult.toBuilder()
.putAllCancelUnfreezeV2Amount(map).build();
}

public long getExchangeReceivedAmount() {
return transactionResult.getExchangeReceivedAmount();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public static TransactionInfoCapsule buildTransactionInfoInstance(TransactionCap
builder.setWithdrawAmount(programResult.getRet().getWithdrawAmount());
builder.setWithdrawExpireAmount(programResult.getRet().getWithdrawExpireAmount());
builder.setCancelAllUnfreezeV2Amount(programResult.getRet().getCancelAllUnfreezeV2Amount());
builder.putAllCancelUnfreezeV2Amount(programResult.getRet().getCancelUnfreezeV2AmountMap());
builder.setExchangeReceivedAmount(programResult.getRet().getExchangeReceivedAmount());
builder.setExchangeInjectAnotherAmount(programResult.getRet().getExchangeInjectAnotherAmount());
builder.setExchangeWithdrawAnotherAmount(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -81,8 +82,13 @@ public void testCancelAllUnfreezeV2() {
assertEquals(SUCESS, ret.getInstance().getRet());
AccountCapsule owner = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
Map<String, Long> cancelUnfreezeV2AmountMap = ret.getInstance()
.getCancelUnfreezeV2AmountMap();
assertEquals(2000000L, ret.getInstance().getWithdrawExpireAmount());
assertEquals(0, owner.getUnfrozenV2List().size());
assertEquals(8000000L, cancelUnfreezeV2AmountMap.get("BANDWIDTH").longValue());
assertEquals(8000000L, cancelUnfreezeV2AmountMap.get("ENERGY").longValue());
assertEquals(0, cancelUnfreezeV2AmountMap.get("TRON_POWER").longValue());
} catch (ContractValidateException | ContractExeException e) {
fail();
}
Expand Down
2 changes: 2 additions & 0 deletions protocol/src/main/protos/core/Tron.proto
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ message Transaction {
repeated MarketOrderDetail orderDetails = 26;
int64 withdraw_expire_amount = 27;
int64 cancel_all_unfreezeV2_amount = 28;
map<string, int64> cancel_unfreezeV2_amount = 29;
}

message raw {
Expand Down Expand Up @@ -485,6 +486,7 @@ message TransactionInfo {

int64 withdraw_expire_amount = 28;
int64 cancel_all_unfreezeV2_amount = 29;
map<string, int64> cancel_unfreezeV2_amount = 30;
}

message TransactionRet {
Expand Down

0 comments on commit fa0310a

Please sign in to comment.