-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
CancelUnfreezeV2Actuator.java
executable file
·229 lines (206 loc) · 9.6 KB
/
CancelUnfreezeV2Actuator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package org.tron.core.actuator;
import static org.tron.core.actuator.ActuatorConstant.ACCOUNT_EXCEPTION_STR;
import static org.tron.core.actuator.ActuatorConstant.NOT_EXIST_STR;
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 com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.tuple.Triple;
import org.tron.common.utils.DecodeUtil;
import org.tron.common.utils.StringUtil;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.TransactionResultCapsule;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.store.AccountStore;
import org.tron.core.store.DynamicPropertiesStore;
import org.tron.protos.Protocol.Account.UnFreezeV2;
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
import org.tron.protos.Protocol.Transaction.Result.code;
import org.tron.protos.contract.BalanceContract.CancelUnfreezeV2Contract;
@Slf4j(topic = "actuator")
public class CancelUnfreezeV2Actuator extends AbstractActuator {
public CancelUnfreezeV2Actuator() {
super(ContractType.CancelUnfreezeV2Contract, CancelUnfreezeV2Contract.class);
}
@Override
public boolean execute(Object result) throws ContractExeException {
TransactionResultCapsule ret = (TransactionResultCapsule) result;
if (Objects.isNull(ret)) {
throw new RuntimeException(ActuatorConstant.TX_RESULT_NULL);
}
long fee = calcFee();
AccountStore accountStore = chainBaseManager.getAccountStore();
DynamicPropertiesStore dynamicStore = chainBaseManager.getDynamicPropertiesStore();
final CancelUnfreezeV2Contract cancelUnfreezeV2Contract;
byte[] ownerAddress;
try {
cancelUnfreezeV2Contract = getCancelUnfreezeV2Contract();
ownerAddress = getOwnerAddress().toByteArray();
} catch (InvalidProtocolBufferException e) {
logger.debug(e.getMessage(), e);
ret.setStatus(fee, code.FAILED);
throw new ContractExeException(e.getMessage());
}
List<Integer> indexList = cancelUnfreezeV2Contract.getIndexList()
.stream().sorted().collect(Collectors.toList());
AccountCapsule ownerCapsule = accountStore.get(ownerAddress);
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));
List<UnFreezeV2> newUnFreezeV2List = null;
if (indexList.isEmpty()) {
for (UnFreezeV2 unFreezeV2 : unfrozenV2List) {
updateAndCalculate(triple, ownerCapsule, now, atomicWithdrawExpireBalance,
atomicCancelBalance, unFreezeV2);
}
} else {
indexList.forEach(index -> {
UnFreezeV2 unFreezeV2 = unfrozenV2List.get(index);
updateAndCalculate(triple, ownerCapsule, now, atomicWithdrawExpireBalance,
atomicCancelBalance, unFreezeV2);
});
newUnFreezeV2List = unfrozenV2List.stream()
.filter(o -> !indexList.contains(unfrozenV2List.indexOf(o))).collect(Collectors.toList());
}
ownerCapsule.clearUnfrozenV2();
ownerCapsule.addAllUnfrozenV2(newUnFreezeV2List);
addTotalResourceWeight(dynamicStore, triple);
long withdrawExpireBalance = atomicWithdrawExpireBalance.get();
if (withdrawExpireBalance > 0) {
ownerCapsule.setBalance(ownerCapsule.getBalance() + withdrawExpireBalance);
}
accountStore.put(ownerCapsule.createDbKey(), ownerCapsule);
ret.setWithdrawExpireAmount(withdrawExpireBalance);
ret.setCancelUnfreezeV2Amount(atomicCancelBalance.get());
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());
}
private void updateAndCalculate(Triple<AtomicLong, AtomicLong, AtomicLong> triple,
AccountCapsule ownerCapsule, long now, AtomicLong atomicLong, AtomicLong cancelBalance,
UnFreezeV2 unFreezeV2) {
if (unFreezeV2.getUnfreezeExpireTime() > now) {
updateFrozenInfoAndTotalResourceWeight(ownerCapsule, unFreezeV2, triple);
cancelBalance.addAndGet(unFreezeV2.getUnfreezeAmount());
} else {
atomicLong.addAndGet(unFreezeV2.getUnfreezeAmount());
}
}
@Override
public boolean validate() throws ContractValidateException {
if (Objects.isNull(this.any)) {
throw new ContractValidateException(ActuatorConstant.CONTRACT_NOT_EXIST);
}
if (Objects.isNull(chainBaseManager)) {
throw new ContractValidateException(ActuatorConstant.STORE_NOT_EXIST);
}
AccountStore accountStore = chainBaseManager.getAccountStore();
DynamicPropertiesStore dynamicStore = chainBaseManager.getDynamicPropertiesStore();
if (!this.any.is(CancelUnfreezeV2Contract.class)) {
throw new ContractValidateException("contract type error, expected type " +
"[CancelUnfreezeV2Contract], real type[" + any.getClass() + "]");
}
if (!dynamicStore.supportAllowCancelUnfreezeV2()) {
throw new ContractValidateException("Not support CancelUnfreezeV2 transaction,"
+ " need to be opened by the committee");
}
final CancelUnfreezeV2Contract cancelUnfreezeV2Contract;
byte[] ownerAddress;
try {
cancelUnfreezeV2Contract = getCancelUnfreezeV2Contract();
ownerAddress = getOwnerAddress().toByteArray();
} catch (InvalidProtocolBufferException e) {
logger.debug(e.getMessage(), e);
throw new ContractValidateException(e.getMessage());
}
if (!DecodeUtil.addressValid(ownerAddress)) {
throw new ContractValidateException("Invalid address");
}
AccountCapsule accountCapsule = accountStore.get(ownerAddress);
String readableOwnerAddress = StringUtil.createReadableString(ownerAddress);
if (Objects.isNull(accountCapsule)) {
throw new ContractValidateException(ACCOUNT_EXCEPTION_STR
+ readableOwnerAddress + NOT_EXIST_STR);
}
List<UnFreezeV2> unfrozenV2List = accountCapsule.getUnfrozenV2List();
if (unfrozenV2List.isEmpty()) {
throw new ContractValidateException("No unfreezeV2 list to cancel");
}
List<Integer> indexList = cancelUnfreezeV2Contract.getIndexList();
if (indexList.size() > unfrozenV2List.size()) {
throw new ContractValidateException(
"The size[" + indexList.size() + "] of the index cannot exceed the size["
+ unfrozenV2List.size() + "] of unfreezeV2!");
}
for (Integer i : indexList) {
int maxIndex = unfrozenV2List.size() - 1;
if (i < 0 || i > maxIndex) {
throw new ContractValidateException(
"The input index[" + i + "] cannot be less than 0 and cannot be "
+ "greater than the maximum index[" + maxIndex + "] of unfreezeV2!");
}
}
Set<Integer> set = new HashSet<>();
List<Integer> dps = indexList.stream().filter(n -> !set.add(n)).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(dps)) {
throw new ContractValidateException("The element" + dps + " in the index list is duplicated");
}
return true;
}
@Override
public ByteString getOwnerAddress() throws InvalidProtocolBufferException {
return getCancelUnfreezeV2Contract().getOwnerAddress();
}
private CancelUnfreezeV2Contract getCancelUnfreezeV2Contract()
throws InvalidProtocolBufferException {
return any.unpack(CancelUnfreezeV2Contract.class);
}
@Override
public long calcFee() {
return 0;
}
public void updateFrozenInfoAndTotalResourceWeight(
AccountCapsule accountCapsule, UnFreezeV2 unFreezeV2,
Triple<AtomicLong, 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);
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);
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);
break;
default:
break;
}
}
}