|
| 1 | +package org.tron.core.actuator; |
| 2 | + |
| 3 | +import static org.tron.core.actuator.ActuatorConstant.ACCOUNT_EXCEPTION_STR; |
| 4 | +import static org.tron.core.actuator.ActuatorConstant.NOT_EXIST_STR; |
| 5 | +import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION; |
| 6 | +import static org.tron.protos.contract.Common.ResourceCode.BANDWIDTH; |
| 7 | +import static org.tron.protos.contract.Common.ResourceCode.ENERGY; |
| 8 | +import static org.tron.protos.contract.Common.ResourceCode.TRON_POWER; |
| 9 | + |
| 10 | +import com.google.protobuf.ByteString; |
| 11 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.Objects; |
| 16 | +import java.util.concurrent.atomic.AtomicLong; |
| 17 | +import lombok.extern.slf4j.Slf4j; |
| 18 | +import org.apache.commons.lang3.tuple.Pair; |
| 19 | +import org.apache.commons.lang3.tuple.Triple; |
| 20 | +import org.tron.common.utils.DecodeUtil; |
| 21 | +import org.tron.common.utils.StringUtil; |
| 22 | +import org.tron.core.capsule.AccountCapsule; |
| 23 | +import org.tron.core.capsule.TransactionResultCapsule; |
| 24 | +import org.tron.core.exception.ContractExeException; |
| 25 | +import org.tron.core.exception.ContractValidateException; |
| 26 | +import org.tron.core.store.AccountStore; |
| 27 | +import org.tron.core.store.DynamicPropertiesStore; |
| 28 | +import org.tron.protos.Protocol.Account.UnFreezeV2; |
| 29 | +import org.tron.protos.Protocol.Transaction.Contract.ContractType; |
| 30 | +import org.tron.protos.Protocol.Transaction.Result.code; |
| 31 | +import org.tron.protos.contract.BalanceContract.CancelAllUnfreezeV2Contract; |
| 32 | + |
| 33 | +@Slf4j(topic = "actuator") |
| 34 | +public class CancelAllUnfreezeV2Actuator extends AbstractActuator { |
| 35 | + |
| 36 | + public CancelAllUnfreezeV2Actuator() { |
| 37 | + super(ContractType.CancelAllUnfreezeV2Contract, CancelAllUnfreezeV2Contract.class); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public boolean execute(Object result) throws ContractExeException { |
| 42 | + TransactionResultCapsule ret = (TransactionResultCapsule) result; |
| 43 | + if (Objects.isNull(ret)) { |
| 44 | + throw new RuntimeException(ActuatorConstant.TX_RESULT_NULL); |
| 45 | + } |
| 46 | + long fee = calcFee(); |
| 47 | + AccountStore accountStore = chainBaseManager.getAccountStore(); |
| 48 | + DynamicPropertiesStore dynamicStore = chainBaseManager.getDynamicPropertiesStore(); |
| 49 | + byte[] ownerAddress; |
| 50 | + try { |
| 51 | + ownerAddress = getOwnerAddress().toByteArray(); |
| 52 | + } catch (InvalidProtocolBufferException e) { |
| 53 | + logger.debug(e.getMessage(), e); |
| 54 | + ret.setStatus(fee, code.FAILED); |
| 55 | + throw new ContractExeException(e.getMessage()); |
| 56 | + } |
| 57 | + AccountCapsule ownerCapsule = accountStore.get(ownerAddress); |
| 58 | + List<UnFreezeV2> unfrozenV2List = ownerCapsule.getUnfrozenV2List(); |
| 59 | + long now = dynamicStore.getLatestBlockHeaderTimestamp(); |
| 60 | + AtomicLong atomicWithdrawExpireBalance = new AtomicLong(0L); |
| 61 | + Triple<Pair<AtomicLong, AtomicLong>, Pair<AtomicLong, AtomicLong>, Pair<AtomicLong, AtomicLong>> |
| 62 | + triple = Triple.of( |
| 63 | + Pair.of(new AtomicLong(0L), new AtomicLong(0L)), |
| 64 | + Pair.of(new AtomicLong(0L), new AtomicLong(0L)), |
| 65 | + Pair.of(new AtomicLong(0L), new AtomicLong(0L))); |
| 66 | + for (UnFreezeV2 unFreezeV2 : unfrozenV2List) { |
| 67 | + updateAndCalculate(triple, ownerCapsule, now, atomicWithdrawExpireBalance, unFreezeV2); |
| 68 | + } |
| 69 | + ownerCapsule.clearUnfrozenV2(); |
| 70 | + addTotalResourceWeight(dynamicStore, triple); |
| 71 | + |
| 72 | + long withdrawExpireBalance = atomicWithdrawExpireBalance.get(); |
| 73 | + if (withdrawExpireBalance > 0) { |
| 74 | + ownerCapsule.setBalance(ownerCapsule.getBalance() + withdrawExpireBalance); |
| 75 | + } |
| 76 | + |
| 77 | + accountStore.put(ownerCapsule.createDbKey(), ownerCapsule); |
| 78 | + ret.setWithdrawExpireAmount(withdrawExpireBalance); |
| 79 | + Map<String, Long> cancelUnfreezeV2AmountMap = new HashMap<>(); |
| 80 | + cancelUnfreezeV2AmountMap.put(BANDWIDTH.name(), triple.getLeft().getRight().get()); |
| 81 | + cancelUnfreezeV2AmountMap.put(ENERGY.name(), triple.getMiddle().getRight().get()); |
| 82 | + cancelUnfreezeV2AmountMap.put(TRON_POWER.name(), triple.getRight().getRight().get()); |
| 83 | + ret.putAllCancelUnfreezeV2AmountMap(cancelUnfreezeV2AmountMap); |
| 84 | + ret.setStatus(fee, code.SUCESS); |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + private void addTotalResourceWeight(DynamicPropertiesStore dynamicStore, |
| 89 | + Triple<Pair<AtomicLong, AtomicLong>, |
| 90 | + Pair<AtomicLong, AtomicLong>, |
| 91 | + Pair<AtomicLong, AtomicLong>> triple) { |
| 92 | + dynamicStore.addTotalNetWeight(triple.getLeft().getLeft().get()); |
| 93 | + dynamicStore.addTotalEnergyWeight(triple.getMiddle().getLeft().get()); |
| 94 | + dynamicStore.addTotalTronPowerWeight(triple.getRight().getLeft().get()); |
| 95 | + } |
| 96 | + |
| 97 | + private void updateAndCalculate(Triple<Pair<AtomicLong, AtomicLong>, Pair<AtomicLong, AtomicLong>, |
| 98 | + Pair<AtomicLong, AtomicLong>> triple, |
| 99 | + AccountCapsule ownerCapsule, long now, AtomicLong atomicLong, UnFreezeV2 unFreezeV2) { |
| 100 | + if (unFreezeV2.getUnfreezeExpireTime() > now) { |
| 101 | + updateFrozenInfoAndTotalResourceWeight(ownerCapsule, unFreezeV2, triple); |
| 102 | + } else { |
| 103 | + atomicLong.addAndGet(unFreezeV2.getUnfreezeAmount()); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public boolean validate() throws ContractValidateException { |
| 109 | + if (Objects.isNull(this.any)) { |
| 110 | + throw new ContractValidateException(ActuatorConstant.CONTRACT_NOT_EXIST); |
| 111 | + } |
| 112 | + |
| 113 | + if (Objects.isNull(chainBaseManager)) { |
| 114 | + throw new ContractValidateException(ActuatorConstant.STORE_NOT_EXIST); |
| 115 | + } |
| 116 | + |
| 117 | + AccountStore accountStore = chainBaseManager.getAccountStore(); |
| 118 | + DynamicPropertiesStore dynamicStore = chainBaseManager.getDynamicPropertiesStore(); |
| 119 | + |
| 120 | + if (!this.any.is(CancelAllUnfreezeV2Contract.class)) { |
| 121 | + throw new ContractValidateException("contract type error, expected type " + |
| 122 | + "[CancelAllUnfreezeV2Contract], real type[" + any.getClass() + "]"); |
| 123 | + } |
| 124 | + |
| 125 | + if (!dynamicStore.supportAllowCancelAllUnfreezeV2()) { |
| 126 | + throw new ContractValidateException("Not support CancelAllUnfreezeV2 transaction," |
| 127 | + + " need to be opened by the committee"); |
| 128 | + } |
| 129 | + |
| 130 | + byte[] ownerAddress; |
| 131 | + try { |
| 132 | + ownerAddress = getOwnerAddress().toByteArray(); |
| 133 | + } catch (InvalidProtocolBufferException e) { |
| 134 | + logger.debug(e.getMessage(), e); |
| 135 | + throw new ContractValidateException(e.getMessage()); |
| 136 | + } |
| 137 | + |
| 138 | + if (!DecodeUtil.addressValid(ownerAddress)) { |
| 139 | + throw new ContractValidateException("Invalid address"); |
| 140 | + } |
| 141 | + AccountCapsule accountCapsule = accountStore.get(ownerAddress); |
| 142 | + String readableOwnerAddress = StringUtil.createReadableString(ownerAddress); |
| 143 | + if (Objects.isNull(accountCapsule)) { |
| 144 | + throw new ContractValidateException(ACCOUNT_EXCEPTION_STR |
| 145 | + + readableOwnerAddress + NOT_EXIST_STR); |
| 146 | + } |
| 147 | + |
| 148 | + List<UnFreezeV2> unfrozenV2List = accountCapsule.getUnfrozenV2List(); |
| 149 | + if (unfrozenV2List.isEmpty()) { |
| 150 | + throw new ContractValidateException("No unfreezeV2 list to cancel"); |
| 151 | + } |
| 152 | + |
| 153 | + return true; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public ByteString getOwnerAddress() throws InvalidProtocolBufferException { |
| 158 | + return getCancelAllUnfreezeV2Contract().getOwnerAddress(); |
| 159 | + } |
| 160 | + |
| 161 | + private CancelAllUnfreezeV2Contract getCancelAllUnfreezeV2Contract() |
| 162 | + throws InvalidProtocolBufferException { |
| 163 | + return any.unpack(CancelAllUnfreezeV2Contract.class); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public long calcFee() { |
| 168 | + return 0; |
| 169 | + } |
| 170 | + |
| 171 | + public void updateFrozenInfoAndTotalResourceWeight( |
| 172 | + AccountCapsule accountCapsule, UnFreezeV2 unFreezeV2, |
| 173 | + Triple<Pair<AtomicLong, AtomicLong>, Pair<AtomicLong, AtomicLong>, |
| 174 | + Pair<AtomicLong, AtomicLong>> triple) { |
| 175 | + switch (unFreezeV2.getType()) { |
| 176 | + case BANDWIDTH: |
| 177 | + long oldNetWeight = accountCapsule.getFrozenV2BalanceWithDelegated(BANDWIDTH) / TRX_PRECISION; |
| 178 | + accountCapsule.addFrozenBalanceForBandwidthV2(unFreezeV2.getUnfreezeAmount()); |
| 179 | + long newNetWeight = accountCapsule.getFrozenV2BalanceWithDelegated(BANDWIDTH) / TRX_PRECISION; |
| 180 | + triple.getLeft().getLeft().addAndGet(newNetWeight - oldNetWeight); |
| 181 | + triple.getLeft().getRight().addAndGet(unFreezeV2.getUnfreezeAmount()); |
| 182 | + break; |
| 183 | + case ENERGY: |
| 184 | + long oldEnergyWeight = accountCapsule.getFrozenV2BalanceWithDelegated(ENERGY) / TRX_PRECISION; |
| 185 | + accountCapsule.addFrozenBalanceForEnergyV2(unFreezeV2.getUnfreezeAmount()); |
| 186 | + long newEnergyWeight = accountCapsule.getFrozenV2BalanceWithDelegated(ENERGY) / TRX_PRECISION; |
| 187 | + triple.getMiddle().getLeft().addAndGet(newEnergyWeight - oldEnergyWeight); |
| 188 | + triple.getMiddle().getRight().addAndGet(unFreezeV2.getUnfreezeAmount()); |
| 189 | + break; |
| 190 | + case TRON_POWER: |
| 191 | + long oldTPWeight = accountCapsule.getTronPowerFrozenV2Balance() / TRX_PRECISION; |
| 192 | + accountCapsule.addFrozenForTronPowerV2(unFreezeV2.getUnfreezeAmount()); |
| 193 | + long newTPWeight = accountCapsule.getTronPowerFrozenV2Balance() / TRX_PRECISION; |
| 194 | + triple.getRight().getLeft().addAndGet(newTPWeight - oldTPWeight); |
| 195 | + triple.getRight().getRight().addAndGet(unFreezeV2.getUnfreezeAmount()); |
| 196 | + break; |
| 197 | + default: |
| 198 | + break; |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments