Skip to content

Commit d63d9e1

Browse files
committed
fix(mechanism): optimize total weight when unfreeze owner
1 parent 3033ed3 commit d63d9e1

2 files changed

Lines changed: 102 additions & 3 deletions

File tree

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public boolean execute(Object result) throws ContractExeException {
108108
}
109109

110110
AccountCapsule receiverCapsule = accountStore.get(receiverAddress);
111-
111+
112112
if (dynamicStore.getAllowTvmConstantinople() == 0 ||
113113
(receiverCapsule != null && receiverCapsule.getType() != AccountType.Contract)) {
114114
switch (unfreezeBalanceContract.getResource()) {
@@ -146,6 +146,8 @@ public boolean execute(Object result) throws ContractExeException {
146146
break;
147147
}
148148
accountStore.put(receiverCapsule.createDbKey(), receiverCapsule);
149+
} else {
150+
decrease = -unfreezeBalance / TRX_PRECISION;
149151
}
150152

151153
accountCapsule.setBalance(oldBalance + unfreezeBalance);
@@ -187,7 +189,7 @@ public boolean execute(Object result) throws ContractExeException {
187189
} else {
188190
switch (unfreezeBalanceContract.getResource()) {
189191
case BANDWIDTH:
190-
192+
long oldNetWeight = accountCapsule.getFrozenBalance() / TRX_PRECISION;
191193
List<Frozen> frozenList = Lists.newArrayList();
192194
frozenList.addAll(accountCapsule.getFrozenList());
193195
Iterator<Frozen> iterator = frozenList.iterator();
@@ -203,9 +205,11 @@ public boolean execute(Object result) throws ContractExeException {
203205
accountCapsule.setInstance(accountCapsule.getInstance().toBuilder()
204206
.setBalance(oldBalance + unfreezeBalance)
205207
.clearFrozen().addAllFrozen(frozenList).build());
206-
208+
long newNetWeight = accountCapsule.getFrozenBalance() / TRX_PRECISION;
209+
decrease = newNetWeight - oldNetWeight;
207210
break;
208211
case ENERGY:
212+
long oldEnergyWeight = accountCapsule.getEnergyFrozenBalance() / TRX_PRECISION;
209213
unfreezeBalance = accountCapsule.getAccountResource().getFrozenBalanceForEnergy()
210214
.getFrozenBalance();
211215

@@ -214,12 +218,17 @@ public boolean execute(Object result) throws ContractExeException {
214218
accountCapsule.setInstance(accountCapsule.getInstance().toBuilder()
215219
.setBalance(oldBalance + unfreezeBalance)
216220
.setAccountResource(newAccountResource).build());
221+
long newEnergyWeight = accountCapsule.getEnergyFrozenBalance() / TRX_PRECISION;
222+
decrease = newEnergyWeight - oldEnergyWeight;
217223
break;
218224
case TRON_POWER:
225+
long oldTPWeight = accountCapsule.getTronPowerFrozenBalance() / TRX_PRECISION;
219226
unfreezeBalance = accountCapsule.getTronPowerFrozenBalance();
220227
accountCapsule.setInstance(accountCapsule.getInstance().toBuilder()
221228
.setBalance(oldBalance + unfreezeBalance)
222229
.clearTronPower().build());
230+
long newTPWeight = accountCapsule.getTronPowerFrozenBalance() / TRX_PRECISION;
231+
decrease = newTPWeight - oldTPWeight;
223232
break;
224233
default:
225234
//this should never happen

framework/src/test/java/org/tron/core/actuator/UnfreezeBalanceActuatorTest.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ private Any getContract(String ownerAddress, ResourceCode resourceCode) {
140140
@Test
141141
public void testUnfreezeBalanceForBandwidth() {
142142
long now = System.currentTimeMillis();
143+
dbManager.getDynamicPropertiesStore().saveAllowNewRewardEnable(0);
143144
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(now);
144145

145146
AccountCapsule accountCapsule = dbManager.getAccountStore()
@@ -178,10 +179,98 @@ public void testUnfreezeBalanceForBandwidth() {
178179
}
179180
}
180181

182+
@Test
183+
public void testUnfreezeSelfAndOthersForBandwidth() {
184+
dbManager.getDynamicPropertiesStore().saveAllowDelegateResource(1);
185+
dbManager.getDynamicPropertiesStore().saveAllowNewRewardEnable(1);
186+
long now = System.currentTimeMillis();
187+
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(now);
188+
189+
AccountCapsule owner = dbManager.getAccountStore().get(ByteArray.fromHexString(OWNER_ADDRESS));
190+
owner.setDelegatedFrozenBalanceForBandwidth(150_0000L);
191+
owner.setFrozen(150_0000L, now);
192+
dbManager.getDynamicPropertiesStore().saveTotalNetWeight(2L);
193+
long beforeWeight = dbManager.getDynamicPropertiesStore().getTotalNetWeight();
194+
Assert.assertEquals(2, beforeWeight);
195+
196+
AccountCapsule receiver = dbManager.getAccountStore()
197+
.get(ByteArray.fromHexString(RECEIVER_ADDRESS));
198+
receiver.setAcquiredDelegatedFrozenBalanceForBandwidth(150_0000L);
199+
200+
dbManager.getAccountStore().put(owner.createDbKey(), owner);
201+
dbManager.getAccountStore().put(receiver.createDbKey(), receiver);
202+
203+
//init DelegatedResourceCapsule
204+
DelegatedResourceCapsule delegatedResourceCapsule = new DelegatedResourceCapsule(
205+
owner.getAddress(), receiver.getAddress());
206+
delegatedResourceCapsule.setFrozenBalanceForBandwidth(150_0000L, now - 100L);
207+
dbManager.getDelegatedResourceStore().put(DelegatedResourceCapsule
208+
.createDbKey(ByteArray.fromHexString(OWNER_ADDRESS),
209+
ByteArray.fromHexString(RECEIVER_ADDRESS)), delegatedResourceCapsule);
210+
211+
//init DelegatedResourceAccountIndex
212+
{
213+
DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndex =
214+
new DelegatedResourceAccountIndexCapsule(
215+
owner.getAddress());
216+
delegatedResourceAccountIndex
217+
.addToAccount(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS)));
218+
dbManager.getDelegatedResourceAccountIndexStore()
219+
.put(ByteArray.fromHexString(OWNER_ADDRESS), delegatedResourceAccountIndex);
220+
}
221+
222+
{
223+
DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndex =
224+
new DelegatedResourceAccountIndexCapsule(
225+
receiver.getAddress());
226+
delegatedResourceAccountIndex
227+
.addFromAccount(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)));
228+
dbManager.getDelegatedResourceAccountIndexStore()
229+
.put(ByteArray.fromHexString(RECEIVER_ADDRESS), delegatedResourceAccountIndex);
230+
}
231+
232+
233+
234+
UnfreezeBalanceActuator actuator1 = new UnfreezeBalanceActuator();
235+
actuator1.setChainBaseManager(dbManager.getChainBaseManager())
236+
.setAny(getContractForBandwidth(OWNER_ADDRESS));
237+
TransactionResultCapsule ret1 = new TransactionResultCapsule();
238+
try {
239+
actuator1.validate();
240+
actuator1.execute(ret1);
241+
long afterWeight1 = dbManager.getDynamicPropertiesStore().getTotalNetWeight();
242+
Assert.assertEquals(1, afterWeight1);
243+
Assert.assertEquals(ret1.getInstance().getRet(), code.SUCESS);
244+
} catch (ContractValidateException e) {
245+
logger.error("ContractValidateException", e);
246+
Assert.assertFalse(e instanceof ContractValidateException);
247+
} catch (ContractExeException e) {
248+
Assert.assertFalse(e instanceof ContractExeException);
249+
}
250+
251+
UnfreezeBalanceActuator actuator = new UnfreezeBalanceActuator();
252+
actuator.setChainBaseManager(dbManager.getChainBaseManager())
253+
.setAny(getDelegatedContractForBandwidth(OWNER_ADDRESS, RECEIVER_ADDRESS));
254+
TransactionResultCapsule ret = new TransactionResultCapsule();
255+
256+
try {
257+
actuator.validate();
258+
actuator.execute(ret);
259+
long afterWeight = dbManager.getDynamicPropertiesStore().getTotalNetWeight();
260+
Assert.assertEquals(0, afterWeight);
261+
Assert.assertEquals(ret.getInstance().getRet(), code.SUCESS);
262+
} catch (ContractValidateException e) {
263+
Assert.assertFalse(e instanceof ContractValidateException);
264+
} catch (ContractExeException e) {
265+
Assert.assertFalse(e instanceof ContractExeException);
266+
}
267+
dbManager.getDynamicPropertiesStore().saveAllowNewRewardEnable(0);
268+
}
181269

182270
@Test
183271
public void testUnfreezeBalanceForEnergy() {
184272
long now = System.currentTimeMillis();
273+
dbManager.getDynamicPropertiesStore().saveAllowNewRewardEnable(0);
185274
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(now);
186275

187276
AccountCapsule accountCapsule = dbManager.getAccountStore()
@@ -304,6 +393,7 @@ public void testUnfreezeDelegatedBalanceForBandwidth() {
304393
} catch (ContractExeException e) {
305394
Assert.assertFalse(e instanceof ContractExeException);
306395
}
396+
307397
}
308398

309399
@Test

0 commit comments

Comments
 (0)