Skip to content

feat(freezeV2): modify CancelAllUnfreezeV2 return info #5279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -54,12 +58,13 @@ public boolean execute(Object result) throws ContractExeException {
List<UnFreezeV2> unfrozenV2List = ownerCapsule.getUnfrozenV2List();
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);
updateAndCalculate(triple, ownerCapsule, now, atomicWithdrawExpireBalance, unFreezeV2);
}
ownerCapsule.clearUnfrozenV2();
addTotalResourceWeight(dynamicStore, triple);
Expand All @@ -71,24 +76,29 @@ 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,
AccountCapsule ownerCapsule, long now, AtomicLong atomicLong, AtomicLong cancelBalance,
UnFreezeV2 unFreezeV2) {
private void updateAndCalculate(Triple<Pair<AtomicLong, AtomicLong>, Pair<AtomicLong, AtomicLong>,
Pair<AtomicLong, AtomicLong>> triple,
AccountCapsule ownerCapsule, long now, AtomicLong atomicLong, UnFreezeV2 unFreezeV2) {
if (unFreezeV2.getUnfreezeExpireTime() > now) {
updateFrozenInfoAndTotalResourceWeight(ownerCapsule, unFreezeV2, triple);
cancelBalance.addAndGet(unFreezeV2.getUnfreezeAmount());
} else {
atomicLong.addAndGet(unFreezeV2.getUnfreezeAmount());
}
Expand Down Expand Up @@ -160,25 +170,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 @@ -89,13 +90,13 @@ public void setWithdrawExpireAmount(long amount) {
.setWithdrawExpireAmount(amount).build();
}

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

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

public long getExchangeReceivedAmount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static TransactionInfoCapsule buildTransactionInfoInstance(TransactionCap
builder.setExchangeId(programResult.getRet().getExchangeId());
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 @@ -337,7 +337,8 @@ public static long getAmountFromTransactionInfo(String hash, ContractType contra
amount = transactionInfo.getWithdrawExpireAmount();
break;
case CancelAllUnfreezeV2Contract:
amount = transactionInfo.getCancelAllUnfreezeV2Amount();
amount = transactionInfo.getCancelUnfreezeV2AmountMap().values()
.stream().mapToLong(v -> v).sum();
break;
default:
break;
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
4 changes: 2 additions & 2 deletions protocol/src/main/protos/core/Tron.proto
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ message Transaction {
bytes orderId = 25;
repeated MarketOrderDetail orderDetails = 26;
int64 withdraw_expire_amount = 27;
int64 cancel_all_unfreezeV2_amount = 28;
map<string, int64> cancel_unfreezeV2_amount = 28;
}

message raw {
Expand Down Expand Up @@ -484,7 +484,7 @@ message TransactionInfo {
int64 packingFee = 27;

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

message TransactionRet {
Expand Down