Skip to content

Commit 3fcf8b0

Browse files
authored
Merge pull request #4776 from lxcmyf/release_4.6.0
fix(mechanism): optimize freezing precision query
2 parents 87032e2 + 3033ed3 commit 3fcf8b0

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,16 +292,13 @@ private long delegateResource(byte[] ownerAddress, byte[] receiverAddress, boole
292292
//modify DelegatedResourceStore
293293
DelegatedResourceCapsule delegatedResourceCapsule = delegatedResourceStore
294294
.get(key);
295-
long oldWeight;
296295
if (delegatedResourceCapsule != null) {
297-
oldWeight = delegatedResourceCapsule.getFrozenBalance(isBandwidth) / TRX_PRECISION;
298296
if (isBandwidth) {
299297
delegatedResourceCapsule.addFrozenBalanceForBandwidth(balance, expireTime);
300298
} else {
301299
delegatedResourceCapsule.addFrozenBalanceForEnergy(balance, expireTime);
302300
}
303301
} else {
304-
oldWeight = 0;
305302
delegatedResourceCapsule = new DelegatedResourceCapsule(
306303
ByteString.copyFrom(ownerAddress),
307304
ByteString.copyFrom(receiverAddress));
@@ -312,7 +309,6 @@ private long delegateResource(byte[] ownerAddress, byte[] receiverAddress, boole
312309
}
313310

314311
}
315-
long newWeight = delegatedResourceCapsule.getFrozenBalance(isBandwidth) / TRX_PRECISION;
316312
delegatedResourceStore.put(key, delegatedResourceCapsule);
317313

318314
//modify DelegatedResourceAccountIndexStore
@@ -349,12 +345,17 @@ private long delegateResource(byte[] ownerAddress, byte[] receiverAddress, boole
349345

350346
//modify AccountStore
351347
AccountCapsule receiverCapsule = accountStore.get(receiverAddress);
348+
long oldWeight;
349+
long newWeight;
352350
if (isBandwidth) {
351+
oldWeight = receiverCapsule.getAcquiredDelegatedFrozenBalanceForBandwidth() / TRX_PRECISION;
353352
receiverCapsule.addAcquiredDelegatedFrozenBalanceForBandwidth(balance);
353+
newWeight = receiverCapsule.getAcquiredDelegatedFrozenBalanceForBandwidth() / TRX_PRECISION;
354354
} else {
355+
oldWeight = receiverCapsule.getAcquiredDelegatedFrozenBalanceForEnergy() / TRX_PRECISION;
355356
receiverCapsule.addAcquiredDelegatedFrozenBalanceForEnergy(balance);
357+
newWeight = receiverCapsule.getAcquiredDelegatedFrozenBalanceForEnergy() / TRX_PRECISION;
356358
}
357-
358359
accountStore.put(receiverCapsule.createDbKey(), receiverCapsule);
359360
return newWeight - oldWeight;
360361
}

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public boolean execute(Object result) throws ContractExeException {
8383
byte[] receiverAddress = unfreezeBalanceContract.getReceiverAddress().toByteArray();
8484
//If the receiver is not included in the contract, unfreeze frozen balance for this account.
8585
//otherwise,unfreeze delegated frozen balance provided this account.
86+
long decrease = 0;
8687
if (!ArrayUtils.isEmpty(receiverAddress) && dynamicStore.supportDR()) {
8788
byte[] key = DelegatedResourceCapsule
8889
.createDbKey(unfreezeBalanceContract.getOwnerAddress().toByteArray(),
@@ -107,25 +108,38 @@ public boolean execute(Object result) throws ContractExeException {
107108
}
108109

109110
AccountCapsule receiverCapsule = accountStore.get(receiverAddress);
111+
110112
if (dynamicStore.getAllowTvmConstantinople() == 0 ||
111113
(receiverCapsule != null && receiverCapsule.getType() != AccountType.Contract)) {
112114
switch (unfreezeBalanceContract.getResource()) {
113115
case BANDWIDTH:
116+
long oldNetWeight = receiverCapsule.getAcquiredDelegatedFrozenBalanceForBandwidth() /
117+
TRX_PRECISION;
114118
if (dynamicStore.getAllowTvmSolidity059() == 1
115119
&& receiverCapsule.getAcquiredDelegatedFrozenBalanceForBandwidth()
116120
< unfreezeBalance) {
121+
oldNetWeight = unfreezeBalance / TRX_PRECISION;
117122
receiverCapsule.setAcquiredDelegatedFrozenBalanceForBandwidth(0);
118123
} else {
119124
receiverCapsule.addAcquiredDelegatedFrozenBalanceForBandwidth(-unfreezeBalance);
120125
}
126+
long newNetWeight = receiverCapsule.getAcquiredDelegatedFrozenBalanceForBandwidth() /
127+
TRX_PRECISION;
128+
decrease = newNetWeight - oldNetWeight;
121129
break;
122130
case ENERGY:
131+
long oldEnergyWeight = receiverCapsule.getAcquiredDelegatedFrozenBalanceForEnergy() /
132+
TRX_PRECISION;
123133
if (dynamicStore.getAllowTvmSolidity059() == 1
124134
&& receiverCapsule.getAcquiredDelegatedFrozenBalanceForEnergy() < unfreezeBalance) {
135+
oldEnergyWeight = unfreezeBalance / TRX_PRECISION;
125136
receiverCapsule.setAcquiredDelegatedFrozenBalanceForEnergy(0);
126137
} else {
127138
receiverCapsule.addAcquiredDelegatedFrozenBalanceForEnergy(-unfreezeBalance);
128139
}
140+
long newEnergyWeight = receiverCapsule.getAcquiredDelegatedFrozenBalanceForEnergy() /
141+
TRX_PRECISION;
142+
decrease = newEnergyWeight - oldEnergyWeight;
129143
break;
130144
default:
131145
//this should never happen
@@ -213,19 +227,20 @@ public boolean execute(Object result) throws ContractExeException {
213227
}
214228

215229
}
216-
230+
231+
long weight = dynamicStore.allowNewRewardEnable() ? decrease : -unfreezeBalance / TRX_PRECISION;
217232
switch (unfreezeBalanceContract.getResource()) {
218233
case BANDWIDTH:
219234
dynamicStore
220-
.addTotalNetWeight(-unfreezeBalance / TRX_PRECISION);
235+
.addTotalNetWeight(weight);
221236
break;
222237
case ENERGY:
223238
dynamicStore
224-
.addTotalEnergyWeight(-unfreezeBalance / TRX_PRECISION);
239+
.addTotalEnergyWeight(weight);
225240
break;
226241
case TRON_POWER:
227242
dynamicStore
228-
.addTotalTronPowerWeight(-unfreezeBalance / TRX_PRECISION);
243+
.addTotalTronPowerWeight(weight);
229244
break;
230245
default:
231246
//this should never happen

0 commit comments

Comments
 (0)