|
| 1 | +package org.tron.core.actuator; |
| 2 | + |
| 3 | +import static org.tron.core.actuator.ActuatorConstant.NOT_EXIST_STR; |
| 4 | +import static org.tron.core.config.Parameter.ChainConstant.DELEGATE_PERIOD; |
| 5 | +import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION; |
| 6 | + |
| 7 | +import com.google.protobuf.ByteString; |
| 8 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.Objects; |
| 11 | +import lombok.extern.slf4j.Slf4j; |
| 12 | +import org.apache.commons.lang3.ArrayUtils; |
| 13 | +import org.tron.common.utils.DecodeUtil; |
| 14 | +import org.tron.common.utils.StringUtil; |
| 15 | +import org.tron.core.capsule.AccountCapsule; |
| 16 | +import org.tron.core.capsule.DelegatedResourceCapsule; |
| 17 | +import org.tron.core.capsule.TransactionResultCapsule; |
| 18 | +import org.tron.core.db.BandwidthProcessor; |
| 19 | +import org.tron.core.db.EnergyProcessor; |
| 20 | +import org.tron.core.exception.ContractExeException; |
| 21 | +import org.tron.core.exception.ContractValidateException; |
| 22 | +import org.tron.core.store.AccountStore; |
| 23 | +import org.tron.core.store.DelegatedResourceAccountIndexStore; |
| 24 | +import org.tron.core.store.DelegatedResourceStore; |
| 25 | +import org.tron.core.store.DynamicPropertiesStore; |
| 26 | +import org.tron.core.utils.TransactionUtil; |
| 27 | +import org.tron.protos.Protocol.AccountType; |
| 28 | +import org.tron.protos.Protocol.Transaction.Contract.ContractType; |
| 29 | +import org.tron.protos.Protocol.Transaction.Result.code; |
| 30 | +import org.tron.protos.contract.BalanceContract.DelegateResourceContract; |
| 31 | + |
| 32 | +@Slf4j(topic = "actuator") |
| 33 | +public class DelegateResourceActuator extends AbstractActuator { |
| 34 | + |
| 35 | + public DelegateResourceActuator() { |
| 36 | + super(ContractType.DelegateResourceContract, DelegateResourceContract.class); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public boolean execute(Object result) throws ContractExeException { |
| 41 | + TransactionResultCapsule ret = (TransactionResultCapsule) result; |
| 42 | + if (Objects.isNull(ret)) { |
| 43 | + throw new RuntimeException(ActuatorConstant.TX_RESULT_NULL); |
| 44 | + } |
| 45 | + |
| 46 | + long fee = calcFee(); |
| 47 | + final DelegateResourceContract delegateResourceContract; |
| 48 | + AccountStore accountStore = chainBaseManager.getAccountStore(); |
| 49 | + try { |
| 50 | + delegateResourceContract = any.unpack(DelegateResourceContract.class); |
| 51 | + } catch (InvalidProtocolBufferException e) { |
| 52 | + logger.debug(e.getMessage(), e); |
| 53 | + ret.setStatus(fee, code.FAILED); |
| 54 | + throw new ContractExeException(e.getMessage()); |
| 55 | + } |
| 56 | + |
| 57 | + AccountCapsule ownerCapsule = accountStore |
| 58 | + .get(delegateResourceContract.getOwnerAddress().toByteArray()); |
| 59 | + |
| 60 | + long delegateBalance = delegateResourceContract.getBalance(); |
| 61 | + boolean lock = delegateResourceContract.getLock(); |
| 62 | + byte[] ownerAddress = delegateResourceContract.getOwnerAddress().toByteArray(); |
| 63 | + byte[] receiverAddress = delegateResourceContract.getReceiverAddress().toByteArray(); |
| 64 | + |
| 65 | + // delegate resource to receiver |
| 66 | + switch (delegateResourceContract.getResource()) { |
| 67 | + case BANDWIDTH: |
| 68 | + delegateResource(ownerAddress, receiverAddress, true, |
| 69 | + delegateBalance, lock); |
| 70 | + |
| 71 | + ownerCapsule.addDelegatedFrozenV2BalanceForBandwidth(delegateBalance); |
| 72 | + ownerCapsule.addFrozenBalanceForBandwidthV2(-delegateBalance); |
| 73 | + break; |
| 74 | + case ENERGY: |
| 75 | + delegateResource(ownerAddress, receiverAddress, false, |
| 76 | + delegateBalance, lock); |
| 77 | + |
| 78 | + ownerCapsule.addDelegatedFrozenV2BalanceForEnergy(delegateBalance); |
| 79 | + ownerCapsule.addFrozenBalanceForEnergyV2(-delegateBalance); |
| 80 | + break; |
| 81 | + default: |
| 82 | + logger.debug("Resource Code Error."); |
| 83 | + } |
| 84 | + |
| 85 | + accountStore.put(ownerCapsule.createDbKey(), ownerCapsule); |
| 86 | + |
| 87 | + ret.setStatus(fee, code.SUCESS); |
| 88 | + |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + @Override |
| 94 | + public boolean validate() throws ContractValidateException { |
| 95 | + if (this.any == null) { |
| 96 | + throw new ContractValidateException(ActuatorConstant.CONTRACT_NOT_EXIST); |
| 97 | + } |
| 98 | + if (chainBaseManager == null) { |
| 99 | + throw new ContractValidateException(ActuatorConstant.STORE_NOT_EXIST); |
| 100 | + } |
| 101 | + AccountStore accountStore = chainBaseManager.getAccountStore(); |
| 102 | + DynamicPropertiesStore dynamicStore = chainBaseManager.getDynamicPropertiesStore(); |
| 103 | + if (!any.is(DelegateResourceContract.class)) { |
| 104 | + throw new ContractValidateException( |
| 105 | + "contract type error,expected type [DelegateResourceContract],real type[" |
| 106 | + + any.getClass() + "]"); |
| 107 | + } |
| 108 | + |
| 109 | + if (!dynamicStore.supportDR()) { |
| 110 | + throw new ContractValidateException("No support for resource delegate"); |
| 111 | + } |
| 112 | + |
| 113 | + if (!dynamicStore.supportUnfreezeDelay()) { |
| 114 | + throw new ContractValidateException("Not support Delegate resource transaction," |
| 115 | + + " need to be opened by the committee"); |
| 116 | + } |
| 117 | + |
| 118 | + final DelegateResourceContract delegateResourceContract; |
| 119 | + try { |
| 120 | + delegateResourceContract = this.any.unpack(DelegateResourceContract.class); |
| 121 | + } catch (InvalidProtocolBufferException e) { |
| 122 | + logger.debug(e.getMessage(), e); |
| 123 | + throw new ContractValidateException(e.getMessage()); |
| 124 | + } |
| 125 | + byte[] ownerAddress = delegateResourceContract.getOwnerAddress().toByteArray(); |
| 126 | + if (!DecodeUtil.addressValid(ownerAddress)) { |
| 127 | + throw new ContractValidateException("Invalid address"); |
| 128 | + } |
| 129 | + |
| 130 | + AccountCapsule ownerCapsule = accountStore.get(ownerAddress); |
| 131 | + if (ownerCapsule == null) { |
| 132 | + String readableOwnerAddress = StringUtil.createReadableString(ownerAddress); |
| 133 | + throw new ContractValidateException( |
| 134 | + ActuatorConstant.ACCOUNT_EXCEPTION_STR + readableOwnerAddress + NOT_EXIST_STR); |
| 135 | + } |
| 136 | + |
| 137 | + long delegateBalance = delegateResourceContract.getBalance(); |
| 138 | + if (delegateBalance < TRX_PRECISION) { |
| 139 | + throw new ContractValidateException("delegateBalance must be more than 1TRX"); |
| 140 | + } |
| 141 | + |
| 142 | + switch (delegateResourceContract.getResource()) { |
| 143 | + case BANDWIDTH: { |
| 144 | + BandwidthProcessor processor = new BandwidthProcessor(chainBaseManager); |
| 145 | + processor.updateUsageForDelegated(ownerCapsule); |
| 146 | + |
| 147 | + long accountNetUsage = ownerCapsule.getNetUsage(); |
| 148 | + if (null != this.getTx() && this.getTx().isTransactionCreate()) { |
| 149 | + accountNetUsage += TransactionUtil.estimateConsumeBandWidthSize(ownerCapsule, |
| 150 | + chainBaseManager); |
| 151 | + } |
| 152 | + long netUsage = (long) (accountNetUsage * TRX_PRECISION * ((double) |
| 153 | + (dynamicStore.getTotalNetWeight()) / dynamicStore.getTotalNetLimit())); |
| 154 | + |
| 155 | + long remainNetUsage = netUsage |
| 156 | + - ownerCapsule.getFrozenBalance() |
| 157 | + - ownerCapsule.getAcquiredDelegatedFrozenBalanceForBandwidth() |
| 158 | + - ownerCapsule.getAcquiredDelegatedFrozenV2BalanceForBandwidth(); |
| 159 | + |
| 160 | + remainNetUsage = Math.max(0, remainNetUsage); |
| 161 | + |
| 162 | + if (ownerCapsule.getFrozenV2BalanceForBandwidth() - remainNetUsage < delegateBalance) { |
| 163 | + throw new ContractValidateException( |
| 164 | + "delegateBalance must be less than available FreezeBandwidthV2 balance"); |
| 165 | + } |
| 166 | + } |
| 167 | + break; |
| 168 | + case ENERGY: { |
| 169 | + EnergyProcessor processor = new EnergyProcessor(dynamicStore, accountStore); |
| 170 | + processor.updateUsage(ownerCapsule); |
| 171 | + |
| 172 | + long energyUsage = (long) (ownerCapsule.getEnergyUsage() * TRX_PRECISION * ((double) |
| 173 | + (dynamicStore.getTotalEnergyWeight()) / dynamicStore.getTotalEnergyCurrentLimit())); |
| 174 | + |
| 175 | + long remainEnergyUsage = energyUsage |
| 176 | + - ownerCapsule.getEnergyFrozenBalance() |
| 177 | + - ownerCapsule.getAcquiredDelegatedFrozenBalanceForEnergy() |
| 178 | + - ownerCapsule.getAcquiredDelegatedFrozenV2BalanceForEnergy(); |
| 179 | + |
| 180 | + remainEnergyUsage = Math.max(0, remainEnergyUsage); |
| 181 | + |
| 182 | + if (ownerCapsule.getFrozenV2BalanceForEnergy() - remainEnergyUsage < delegateBalance) { |
| 183 | + throw new ContractValidateException( |
| 184 | + "delegateBalance must be less than available FreezeEnergyV2 balance"); |
| 185 | + } |
| 186 | + } |
| 187 | + break; |
| 188 | + default: |
| 189 | + throw new ContractValidateException( |
| 190 | + "ResourceCode error, valid ResourceCode[BANDWIDTH、ENERGY]"); |
| 191 | + } |
| 192 | + |
| 193 | + byte[] receiverAddress = delegateResourceContract.getReceiverAddress().toByteArray(); |
| 194 | + |
| 195 | + if (ArrayUtils.isEmpty(receiverAddress) || !DecodeUtil.addressValid(receiverAddress)) { |
| 196 | + throw new ContractValidateException("Invalid receiverAddress"); |
| 197 | + } |
| 198 | + |
| 199 | + |
| 200 | + if (Arrays.equals(receiverAddress, ownerAddress)) { |
| 201 | + throw new ContractValidateException( |
| 202 | + "receiverAddress must not be the same as ownerAddress"); |
| 203 | + } |
| 204 | + |
| 205 | + AccountCapsule receiverCapsule = accountStore.get(receiverAddress); |
| 206 | + if (receiverCapsule == null) { |
| 207 | + String readableOwnerAddress = StringUtil.createReadableString(receiverAddress); |
| 208 | + throw new ContractValidateException( |
| 209 | + ActuatorConstant.ACCOUNT_EXCEPTION_STR |
| 210 | + + readableOwnerAddress + NOT_EXIST_STR); |
| 211 | + } |
| 212 | + |
| 213 | + if (receiverCapsule.getType() == AccountType.Contract) { |
| 214 | + throw new ContractValidateException( |
| 215 | + "Do not allow delegate resources to contract addresses"); |
| 216 | + } |
| 217 | + |
| 218 | + return true; |
| 219 | + } |
| 220 | + |
| 221 | + @Override |
| 222 | + public ByteString getOwnerAddress() throws InvalidProtocolBufferException { |
| 223 | + return any.unpack(DelegateResourceContract.class).getOwnerAddress(); |
| 224 | + } |
| 225 | + |
| 226 | + @Override |
| 227 | + public long calcFee() { |
| 228 | + return 0; |
| 229 | + } |
| 230 | + |
| 231 | + private void delegateResource(byte[] ownerAddress, byte[] receiverAddress, boolean isBandwidth, |
| 232 | + long balance, boolean lock) { |
| 233 | + AccountStore accountStore = chainBaseManager.getAccountStore(); |
| 234 | + DynamicPropertiesStore dynamicPropertiesStore = chainBaseManager.getDynamicPropertiesStore(); |
| 235 | + DelegatedResourceStore delegatedResourceStore = chainBaseManager.getDelegatedResourceStore(); |
| 236 | + DelegatedResourceAccountIndexStore delegatedResourceAccountIndexStore = chainBaseManager |
| 237 | + .getDelegatedResourceAccountIndexStore(); |
| 238 | + |
| 239 | + // 1. unlock the expired delegate resource |
| 240 | + long now = chainBaseManager.getDynamicPropertiesStore().getLatestBlockHeaderTimestamp(); |
| 241 | + delegatedResourceStore.unLockExpireResource(ownerAddress, receiverAddress, now); |
| 242 | + |
| 243 | + //modify DelegatedResourceStore |
| 244 | + byte[] key; |
| 245 | + long expireTime = 0; |
| 246 | + if (lock) { |
| 247 | + expireTime = now + DELEGATE_PERIOD; |
| 248 | + } |
| 249 | + key = DelegatedResourceCapsule.createDbKeyV2(ownerAddress, receiverAddress, lock); |
| 250 | + DelegatedResourceCapsule delegatedResourceCapsule = delegatedResourceStore.get(key); |
| 251 | + if (delegatedResourceCapsule == null) { |
| 252 | + delegatedResourceCapsule = new DelegatedResourceCapsule(ByteString.copyFrom(ownerAddress), |
| 253 | + ByteString.copyFrom(receiverAddress)); |
| 254 | + } |
| 255 | + |
| 256 | + if (isBandwidth) { |
| 257 | + delegatedResourceCapsule.addFrozenBalanceForBandwidth(balance, expireTime); |
| 258 | + } else { |
| 259 | + delegatedResourceCapsule.addFrozenBalanceForEnergy(balance, expireTime); |
| 260 | + } |
| 261 | + delegatedResourceStore.put(key, delegatedResourceCapsule); |
| 262 | + |
| 263 | + //modify DelegatedResourceAccountIndexStore |
| 264 | + delegatedResourceAccountIndexStore.delegateV2(ownerAddress, receiverAddress, |
| 265 | + dynamicPropertiesStore.getLatestBlockHeaderTimestamp()); |
| 266 | + |
| 267 | + //modify AccountStore for receiver |
| 268 | + AccountCapsule receiverCapsule = accountStore.get(receiverAddress); |
| 269 | + if (isBandwidth) { |
| 270 | + receiverCapsule.addAcquiredDelegatedFrozenV2BalanceForBandwidth(balance); |
| 271 | + } else { |
| 272 | + receiverCapsule.addAcquiredDelegatedFrozenV2BalanceForEnergy(balance); |
| 273 | + } |
| 274 | + accountStore.put(receiverCapsule.createDbKey(), receiverCapsule); |
| 275 | + } |
| 276 | + |
| 277 | +} |
0 commit comments