Skip to content

Commit d535eb2

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into feature/add_grpc_api
2 parents 0bf4bbc + a02fbb0 commit d535eb2

74 files changed

Lines changed: 2101 additions & 767 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

actuator/src/main/java/org/tron/core/actuator/CancelAllUnfreezeV2Actuator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public boolean execute(Object result) throws ContractExeException {
5858
List<UnFreezeV2> unfrozenV2List = ownerCapsule.getUnfrozenV2List();
5959
long now = dynamicStore.getLatestBlockHeaderTimestamp();
6060
AtomicLong atomicWithdrawExpireBalance = new AtomicLong(0L);
61+
/* The triple object is defined by resource type, with left representing the pair object
62+
corresponding to bandwidth, middle representing the pair object corresponding to energy, and
63+
right representing the pair object corresponding to tron power. The pair object for each
64+
resource type, left represents resource weight, and right represents the number of unfreeze
65+
resources for that resource type. */
6166
Triple<Pair<AtomicLong, AtomicLong>, Pair<AtomicLong, AtomicLong>, Pair<AtomicLong, AtomicLong>>
6267
triple = Triple.of(
6368
Pair.of(new AtomicLong(0L), new AtomicLong(0L)),

actuator/src/main/java/org/tron/core/actuator/DelegateResourceActuator.java

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package org.tron.core.actuator;
22

33
import static org.tron.core.actuator.ActuatorConstant.NOT_EXIST_STR;
4+
import static org.tron.core.config.Parameter.ChainConstant.BLOCK_PRODUCED_INTERVAL;
45
import static org.tron.core.config.Parameter.ChainConstant.DELEGATE_PERIOD;
56
import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION;
67
import static org.tron.protos.contract.Common.ResourceCode;
78
import static org.tron.protos.contract.Common.ResourceCode.BANDWIDTH;
89
import static org.tron.protos.contract.Common.ResourceCode.ENERGY;
10+
import static org.tron.core.vm.utils.FreezeV2Util.getV2EnergyUsage;
11+
import static org.tron.core.vm.utils.FreezeV2Util.getV2NetUsage;
912

1013
import com.google.protobuf.ByteString;
1114
import com.google.protobuf.InvalidProtocolBufferException;
@@ -64,7 +67,8 @@ public boolean execute(Object result) throws ContractExeException {
6467
DynamicPropertiesStore dynamicStore = chainBaseManager.getDynamicPropertiesStore();
6568
long delegateBalance = delegateResourceContract.getBalance();
6669
boolean lock = delegateResourceContract.getLock();
67-
long lockPeriod = getLockPeriod(dynamicStore, delegateResourceContract);
70+
long lockPeriod = getLockPeriod(dynamicStore.supportMaxDelegateLockPeriod(),
71+
delegateResourceContract);
6872
byte[] receiverAddress = delegateResourceContract.getReceiverAddress().toByteArray();
6973

7074
// delegate resource to receiver
@@ -143,7 +147,7 @@ public boolean validate() throws ContractValidateException {
143147

144148
long delegateBalance = delegateResourceContract.getBalance();
145149
if (delegateBalance < TRX_PRECISION) {
146-
throw new ContractValidateException("delegateBalance must be more than 1TRX");
150+
throw new ContractValidateException("delegateBalance must be greater than or equal to 1 TRX");
147151
}
148152

149153
switch (delegateResourceContract.getResource()) {
@@ -153,22 +157,15 @@ public boolean validate() throws ContractValidateException {
153157

154158
long accountNetUsage = ownerCapsule.getNetUsage();
155159
if (null != this.getTx() && this.getTx().isTransactionCreate()) {
156-
accountNetUsage += TransactionUtil.estimateConsumeBandWidthSize(ownerCapsule,
157-
chainBaseManager);
160+
accountNetUsage += TransactionUtil.estimateConsumeBandWidthSize(dynamicStore,
161+
ownerCapsule.getBalance());
158162
}
159163
long netUsage = (long) (accountNetUsage * TRX_PRECISION * ((double)
160164
(dynamicStore.getTotalNetWeight()) / dynamicStore.getTotalNetLimit()));
161-
162-
long remainNetUsage = netUsage
163-
- ownerCapsule.getFrozenBalance()
164-
- ownerCapsule.getAcquiredDelegatedFrozenBalanceForBandwidth()
165-
- ownerCapsule.getAcquiredDelegatedFrozenV2BalanceForBandwidth();
166-
167-
remainNetUsage = Math.max(0, remainNetUsage);
168-
169-
if (ownerCapsule.getFrozenV2BalanceForBandwidth() - remainNetUsage < delegateBalance) {
165+
long v2NetUsage = getV2NetUsage(ownerCapsule, netUsage);
166+
if (ownerCapsule.getFrozenV2BalanceForBandwidth() - v2NetUsage < delegateBalance) {
170167
throw new ContractValidateException(
171-
"delegateBalance must be less than available FreezeBandwidthV2 balance");
168+
"delegateBalance must be less than or equal to available FreezeBandwidthV2 balance");
172169
}
173170
}
174171
break;
@@ -178,17 +175,10 @@ public boolean validate() throws ContractValidateException {
178175

179176
long energyUsage = (long) (ownerCapsule.getEnergyUsage() * TRX_PRECISION * ((double)
180177
(dynamicStore.getTotalEnergyWeight()) / dynamicStore.getTotalEnergyCurrentLimit()));
181-
182-
long remainEnergyUsage = energyUsage
183-
- ownerCapsule.getEnergyFrozenBalance()
184-
- ownerCapsule.getAcquiredDelegatedFrozenBalanceForEnergy()
185-
- ownerCapsule.getAcquiredDelegatedFrozenV2BalanceForEnergy();
186-
187-
remainEnergyUsage = Math.max(0, remainEnergyUsage);
188-
189-
if (ownerCapsule.getFrozenV2BalanceForEnergy() - remainEnergyUsage < delegateBalance) {
178+
long v2EnergyUsage = getV2EnergyUsage(ownerCapsule, energyUsage);
179+
if (ownerCapsule.getFrozenV2BalanceForEnergy() - v2EnergyUsage < delegateBalance) {
190180
throw new ContractValidateException(
191-
"delegateBalance must be less than available FreezeEnergyV2 balance");
181+
"delegateBalance must be less than or equal to available FreezeEnergyV2 balance");
192182
}
193183
}
194184
break;
@@ -219,7 +209,7 @@ public boolean validate() throws ContractValidateException {
219209

220210
boolean lock = delegateResourceContract.getLock();
221211
if (lock && dynamicStore.supportMaxDelegateLockPeriod()) {
222-
long lockPeriod = getLockPeriod(dynamicStore, delegateResourceContract);
212+
long lockPeriod = getLockPeriod(true, delegateResourceContract);
223213
long maxDelegateLockPeriod = dynamicStore.getMaxDelegateLockPeriod();
224214
if (lockPeriod < 0 || lockPeriod > maxDelegateLockPeriod) {
225215
throw new ContractValidateException(
@@ -257,20 +247,20 @@ public boolean validate() throws ContractValidateException {
257247
return true;
258248
}
259249

260-
private long getLockPeriod(DynamicPropertiesStore dynamicStore,
250+
private long getLockPeriod(boolean supportMaxDelegateLockPeriod,
261251
DelegateResourceContract delegateResourceContract) {
262252
long lockPeriod = delegateResourceContract.getLockPeriod();
263-
if (dynamicStore.supportMaxDelegateLockPeriod()) {
264-
return lockPeriod == 0 ? DELEGATE_PERIOD / 3000 : lockPeriod;
253+
if (supportMaxDelegateLockPeriod) {
254+
return lockPeriod == 0 ? DELEGATE_PERIOD / BLOCK_PRODUCED_INTERVAL : lockPeriod;
265255
} else {
266-
return 0;
256+
return DELEGATE_PERIOD / BLOCK_PRODUCED_INTERVAL;
267257
}
268258
}
269259

270260
private void validRemainTime(ResourceCode resourceCode, long lockPeriod, long expireTime,
271261
long now) throws ContractValidateException {
272262
long remainTime = expireTime - now;
273-
if (lockPeriod * 3 * 1000 < remainTime) {
263+
if (lockPeriod * BLOCK_PRODUCED_INTERVAL < remainTime) {
274264
throw new ContractValidateException(
275265
"The lock period for " + resourceCode.name() + " this time cannot be less than the "
276266
+ "remaining time[" + remainTime + "ms] of the last lock period for "
@@ -303,11 +293,7 @@ private void delegateResource(byte[] ownerAddress, byte[] receiverAddress, boole
303293
//modify DelegatedResourceStore
304294
long expireTime = 0;
305295
if (lock) {
306-
if (dynamicPropertiesStore.supportMaxDelegateLockPeriod()) {
307-
expireTime = now + lockPeriod * 3 * 1000;
308-
} else {
309-
expireTime = now + DELEGATE_PERIOD;
310-
}
296+
expireTime = now + lockPeriod * BLOCK_PRODUCED_INTERVAL;
311297
}
312298
byte[] key = DelegatedResourceCapsule.createDbKeyV2(ownerAddress, receiverAddress, lock);
313299
DelegatedResourceCapsule delegatedResourceCapsule = delegatedResourceStore.get(key);

actuator/src/main/java/org/tron/core/actuator/FreezeBalanceActuator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,15 @@ public boolean validate() throws ContractValidateException {
189189
throw new ContractValidateException("frozenBalance must be positive");
190190
}
191191
if (frozenBalance < TRX_PRECISION) {
192-
throw new ContractValidateException("frozenBalance must be more than 1TRX");
192+
throw new ContractValidateException("frozenBalance must be greater than or equal to 1 TRX");
193193
}
194194

195195
int frozenCount = accountCapsule.getFrozenCount();
196196
if (!(frozenCount == 0 || frozenCount == 1)) {
197197
throw new ContractValidateException("frozenCount must be 0 or 1");
198198
}
199199
if (frozenBalance > accountCapsule.getBalance()) {
200-
throw new ContractValidateException("frozenBalance must be less than accountBalance");
200+
throw new ContractValidateException("frozenBalance must be less than or equal to accountBalance");
201201
}
202202

203203
long frozenDuration = freezeBalanceContract.getFrozenDuration();

actuator/src/main/java/org/tron/core/actuator/FreezeBalanceV2Actuator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public boolean execute(Object result) throws ContractExeException {
5454
accountCapsule.initializeOldTronPower();
5555
}
5656

57-
long newBalance = accountCapsule.getBalance() - freezeBalanceV2Contract.getFrozenBalance();
5857
long frozenBalance = freezeBalanceV2Contract.getFrozenBalance();
58+
long newBalance = accountCapsule.getBalance() - frozenBalance;
5959

6060
switch (freezeBalanceV2Contract.getResource()) {
6161
case BANDWIDTH:
@@ -133,11 +133,11 @@ public boolean validate() throws ContractValidateException {
133133
throw new ContractValidateException("frozenBalance must be positive");
134134
}
135135
if (frozenBalance < TRX_PRECISION) {
136-
throw new ContractValidateException("frozenBalance must be more than 1TRX");
136+
throw new ContractValidateException("frozenBalance must be greater than or equal to 1 TRX");
137137
}
138138

139139
if (frozenBalance > accountCapsule.getBalance()) {
140-
throw new ContractValidateException("frozenBalance must be less than accountBalance");
140+
throw new ContractValidateException("frozenBalance must be less than or equal to accountBalance");
141141
}
142142

143143
switch (freezeBalanceV2Contract.getResource()) {

actuator/src/main/java/org/tron/core/actuator/UnDelegateResourceActuator.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.util.Arrays;
1111
import java.util.Objects;
1212
import lombok.extern.slf4j.Slf4j;
13-
import org.apache.commons.lang3.ArrayUtils;
1413
import org.tron.common.utils.DecodeUtil;
1514
import org.tron.common.utils.StringUtil;
1615
import org.tron.core.capsule.AccountCapsule;
@@ -144,9 +143,8 @@ public boolean execute(Object result) throws ContractExeException {
144143

145144
long now = chainBaseManager.getHeadSlot();
146145
if (Objects.nonNull(receiverCapsule) && transferUsage > 0) {
147-
ownerCapsule.setNetUsage(processor.unDelegateIncrease(ownerCapsule, receiverCapsule,
148-
transferUsage, BANDWIDTH, now));
149-
ownerCapsule.setLatestConsumeTime(now);
146+
processor.unDelegateIncrease(ownerCapsule, receiverCapsule,
147+
transferUsage, BANDWIDTH, now);
150148
}
151149
}
152150
break;
@@ -160,9 +158,7 @@ public boolean execute(Object result) throws ContractExeException {
160158

161159
long now = chainBaseManager.getHeadSlot();
162160
if (Objects.nonNull(receiverCapsule) && transferUsage > 0) {
163-
ownerCapsule.setEnergyUsage(processor.unDelegateIncrease(ownerCapsule, receiverCapsule,
164-
transferUsage, ENERGY, now));
165-
ownerCapsule.setLatestConsumeTimeForEnergy(now);
161+
processor.unDelegateIncrease(ownerCapsule, receiverCapsule, transferUsage, ENERGY, now);
166162
}
167163
}
168164
break;
@@ -240,7 +236,7 @@ public boolean validate() throws ContractValidateException {
240236
}
241237

242238
byte[] receiverAddress = unDelegateResourceContract.getReceiverAddress().toByteArray();
243-
if (ArrayUtils.isEmpty(receiverAddress) || !DecodeUtil.addressValid(receiverAddress)) {
239+
if (!DecodeUtil.addressValid(receiverAddress)) {
244240
throw new ContractValidateException("Invalid receiverAddress");
245241
}
246242
if (Arrays.equals(receiverAddress, ownerAddress)) {

actuator/src/main/java/org/tron/core/actuator/UnfreezeBalanceV2Actuator.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import com.google.common.collect.Lists;
1212
import com.google.protobuf.ByteString;
1313
import com.google.protobuf.InvalidProtocolBufferException;
14+
15+
import java.util.ArrayList;
1416
import java.util.Iterator;
1517
import java.util.List;
1618
import java.util.Objects;
@@ -365,19 +367,23 @@ private void updateVote(AccountCapsule accountCapsule,
365367
}
366368

367369
// Update Owner Voting
368-
votesCapsule.clearNewVotes();
370+
List<Vote> addVotes = new ArrayList<>();
369371
for (Vote vote : accountCapsule.getVotesList()) {
370372
long newVoteCount = (long)
371373
((double) vote.getVoteCount() / totalVote * ownedTronPower / TRX_PRECISION);
372374
if (newVoteCount > 0) {
373-
votesCapsule.addNewVotes(vote.getVoteAddress(), newVoteCount);
375+
Vote newVote = Vote.newBuilder()
376+
.setVoteAddress(vote.getVoteAddress())
377+
.setVoteCount(newVoteCount)
378+
.build();
379+
addVotes.add(newVote);
374380
}
375381
}
382+
votesCapsule.clearNewVotes();
383+
votesCapsule.addAllNewVotes(addVotes);
376384
votesStore.put(ownerAddress, votesCapsule);
377385

378386
accountCapsule.clearVotes();
379-
for (Vote vote : votesCapsule.getNewVotes()) {
380-
accountCapsule.addVotes(vote.getVoteAddress(), vote.getVoteCount());
381-
}
387+
accountCapsule.addAllVotes(addVotes);
382388
}
383389
}

actuator/src/main/java/org/tron/core/actuator/WithdrawExpireUnfreezeActuator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ private long getTotalWithdrawUnfreeze(List<UnFreezeV2> unfrozenV2List, long now)
125125
}
126126

127127
private List<UnFreezeV2> getTotalWithdrawList(List<UnFreezeV2> unfrozenV2List, long now) {
128-
return unfrozenV2List.stream().filter(unfrozenV2 -> (unfrozenV2.getUnfreezeAmount() > 0
129-
&& unfrozenV2.getUnfreezeExpireTime() <= now)).collect(Collectors.toList());
128+
return unfrozenV2List.stream().filter(unfrozenV2 -> unfrozenV2.getUnfreezeExpireTime() <= now)
129+
.collect(Collectors.toList());
130130
}
131131

132132
private List<UnFreezeV2> getRemainWithdrawList(List<UnFreezeV2> unfrozenV2List, long now) {

actuator/src/main/java/org/tron/core/utils/ProposalUtil.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.tron.core.Constant.DYNAMIC_ENERGY_INCREASE_FACTOR_RANGE;
44
import static org.tron.core.Constant.DYNAMIC_ENERGY_MAX_FACTOR_RANGE;
5+
import static org.tron.core.config.Parameter.ChainConstant.ONE_YEAR_BLOCK_NUMBERS;
56

67
import org.tron.common.utils.ForkController;
78
import org.tron.core.config.Parameter.ForkBlockVersionConsts;
@@ -714,10 +715,11 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
714715
"Bad chain parameter id [MAX_DELEGATE_LOCK_PERIOD]");
715716
}
716717
long maxDelegateLockPeriod = dynamicPropertiesStore.getMaxDelegateLockPeriod();
717-
if (value <= maxDelegateLockPeriod || value > 10512000L) {
718+
if (value <= maxDelegateLockPeriod || value > ONE_YEAR_BLOCK_NUMBERS) {
718719
throw new ContractValidateException(
719720
"This value[MAX_DELEGATE_LOCK_PERIOD] is only allowed to be greater than "
720-
+ maxDelegateLockPeriod + " and less than or equal to 10512000 !");
721+
+ maxDelegateLockPeriod + " and less than or equal to " + ONE_YEAR_BLOCK_NUMBERS
722+
+ " !");
721723
}
722724
if (dynamicPropertiesStore.getUnfreezeDelayDays() == 0) {
723725
throw new ContractValidateException(

actuator/src/main/java/org/tron/core/utils/TransactionUtil.java

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.tron.core.capsule.TransactionCapsule;
4545
import org.tron.core.exception.PermissionException;
4646
import org.tron.core.exception.SignatureFormatException;
47+
import org.tron.core.store.DynamicPropertiesStore;
4748
import org.tron.protos.Protocol.Permission;
4849
import org.tron.protos.Protocol.Permission.PermissionType;
4950
import org.tron.protos.Protocol.Transaction;
@@ -222,7 +223,7 @@ public TransactionSignWeight getTransactionSignWeight(Transaction trx) {
222223
}
223224
tswBuilder.setPermission(permission);
224225
if (trx.getSignatureCount() > 0) {
225-
List<ByteString> approveList = new ArrayList<ByteString>();
226+
List<ByteString> approveList = new ArrayList<>();
226227
long currentWeight = TransactionCapsule.checkWeight(permission, trx.getSignatureList(),
227228
Sha256Hash.hash(CommonParameter.getInstance()
228229
.isECKeyCryptoEngine(), trx.getRawData().toByteArray()), approveList);
@@ -253,56 +254,24 @@ public TransactionSignWeight getTransactionSignWeight(Transaction trx) {
253254
return tswBuilder.build();
254255
}
255256

256-
public static long consumeBandWidthSize(
257-
final TransactionCapsule transactionCapsule,
258-
ChainBaseManager chainBaseManager) {
259-
long bytesSize;
260-
261-
boolean supportVM = chainBaseManager.getDynamicPropertiesStore().supportVM();
262-
if (supportVM) {
263-
bytesSize = transactionCapsule.getInstance().toBuilder().clearRet().build().getSerializedSize();
264-
} else {
265-
bytesSize = transactionCapsule.getSerializedSize();
266-
}
267-
268-
List<Transaction.Contract> contracts = transactionCapsule.getInstance().getRawData().getContractList();
269-
for (Transaction.Contract contract : contracts) {
270-
if (contract.getType() == Contract.ContractType.ShieldedTransferContract) {
271-
continue;
272-
}
273-
if (supportVM) {
274-
bytesSize += Constant.MAX_RESULT_SIZE_IN_TX;
275-
}
276-
}
277-
278-
return bytesSize;
279-
}
280-
281-
public static long estimateConsumeBandWidthSize(final AccountCapsule ownerCapsule,
282-
ChainBaseManager chainBaseManager) {
257+
public static long estimateConsumeBandWidthSize(DynamicPropertiesStore dps, long balance) {
283258
DelegateResourceContract.Builder builder;
284-
if (chainBaseManager.getDynamicPropertiesStore().supportMaxDelegateLockPeriod()) {
259+
if (dps.supportMaxDelegateLockPeriod()) {
285260
builder = DelegateResourceContract.newBuilder()
286-
.setLock(true)
287-
.setLockPeriod(chainBaseManager.getDynamicPropertiesStore().getMaxDelegateLockPeriod())
288-
.setBalance(ownerCapsule.getFrozenV2BalanceForBandwidth());
261+
.setLock(true)
262+
.setLockPeriod(dps.getMaxDelegateLockPeriod())
263+
.setBalance(balance);
289264
} else {
290265
builder = DelegateResourceContract.newBuilder()
291-
.setLock(true)
292-
.setBalance(ownerCapsule.getFrozenV2BalanceForBandwidth());
266+
.setLock(true)
267+
.setBalance(balance);
293268
}
294-
TransactionCapsule fakeTransactionCapsule = new TransactionCapsule(builder.build()
295-
, ContractType.DelegateResourceContract);
296-
long size1 = consumeBandWidthSize(fakeTransactionCapsule, chainBaseManager);
297-
269+
long builderSize = builder.build().getSerializedSize();
298270
DelegateResourceContract.Builder builder2 = DelegateResourceContract.newBuilder()
299-
.setBalance(TRX_PRECISION);
300-
TransactionCapsule fakeTransactionCapsule2 = new TransactionCapsule(builder2.build()
301-
, ContractType.DelegateResourceContract);
302-
long size2 = consumeBandWidthSize(fakeTransactionCapsule2, chainBaseManager);
303-
long addSize = Math.max(size1 - size2, 0L);
271+
.setBalance(TRX_PRECISION);
272+
long builder2Size = builder2.build().getSerializedSize();
273+
long addSize = Math.max(builderSize - builder2Size, 0L);
304274

305275
return DELEGATE_COST_BASE_SIZE + addSize;
306276
}
307-
308277
}

0 commit comments

Comments
 (0)