Skip to content

Commit 5ac7f0c

Browse files
committed
feat(delegate): add test for delegate optimization
1 parent 342d242 commit 5ac7f0c

2 files changed

Lines changed: 78 additions & 11 deletions

File tree

chainbase/src/main/java/org/tron/core/store/DelegatedResourceAccountIndexStore.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
public class DelegatedResourceAccountIndexStore extends
1717
TronStoreWithRevoking<DelegatedResourceAccountIndexCapsule> {
1818

19-
private static final byte[] fromPrefix = {0x01};
20-
private static final byte[] toPrefix = {0x02};
19+
private static final byte[] FROM_PREFIX = {0x01};
20+
private static final byte[] TO_PREFIX = {0x02};
2121

2222
@Autowired
2323
public DelegatedResourceAccountIndexStore(@Value("DelegatedResourceAccountIndex") String dbName) {
@@ -61,23 +61,23 @@ public void convert(byte[] address) {
6161
}
6262

6363
public void delegate(byte[] from, byte[] to, long time) {
64-
byte[] fromKey = createKey(fromPrefix, from, to);
64+
byte[] fromKey = createKey(FROM_PREFIX, from, to);
6565
DelegatedResourceAccountIndexCapsule toIndexCapsule =
6666
new DelegatedResourceAccountIndexCapsule(ByteString.copyFrom(to));
6767
toIndexCapsule.setTimestamp(time);
6868
this.put(fromKey, toIndexCapsule);
6969

70-
byte[] toKey = createKey(toPrefix, to, from);
70+
byte[] toKey = createKey(TO_PREFIX, to, from);
7171
DelegatedResourceAccountIndexCapsule fromIndexCapsule =
7272
new DelegatedResourceAccountIndexCapsule(ByteString.copyFrom(from));
7373
fromIndexCapsule.setTimestamp(time);
7474
this.put(toKey, fromIndexCapsule);
7575
}
7676

7777
public void unDelegate(byte[] from, byte[] to) {
78-
byte[] fromKey = createKey(fromPrefix, from, to);
78+
byte[] fromKey = createKey(FROM_PREFIX, from, to);
7979
this.delete(fromKey);
80-
byte[] toKey = createKey(toPrefix, to, from);
80+
byte[] toKey = createKey(TO_PREFIX, to, from);
8181
this.delete(toKey);
8282
}
8383

@@ -89,10 +89,10 @@ public DelegatedResourceAccountIndexCapsule getIndex(byte[] address) {
8989

9090
DelegatedResourceAccountIndexCapsule tmpIndexCapsule =
9191
new DelegatedResourceAccountIndexCapsule(ByteString.copyFrom(address));
92-
byte[] key = new byte[fromPrefix.length + address.length];
92+
byte[] key = new byte[FROM_PREFIX.length + address.length];
9393

94-
System.arraycopy(fromPrefix, 0, key, 0, fromPrefix.length);
95-
System.arraycopy(address, 0, key, fromPrefix.length, address.length);
94+
System.arraycopy(FROM_PREFIX, 0, key, 0, FROM_PREFIX.length);
95+
System.arraycopy(address, 0, key, FROM_PREFIX.length, address.length);
9696
List<DelegatedResourceAccountIndexCapsule> tmpToList =
9797
new ArrayList<>(this.prefixQuery(key).values());
9898

@@ -101,8 +101,8 @@ public DelegatedResourceAccountIndexCapsule getIndex(byte[] address) {
101101
.map(DelegatedResourceAccountIndexCapsule::getAccount).collect(Collectors.toList());
102102
tmpIndexCapsule.setAllToAccounts(list);
103103

104-
System.arraycopy(toPrefix, 0, key, 0, toPrefix.length);
105-
System.arraycopy(address, 0, key, toPrefix.length, address.length);
104+
System.arraycopy(TO_PREFIX, 0, key, 0, TO_PREFIX.length);
105+
System.arraycopy(address, 0, key, TO_PREFIX.length, address.length);
106106
List<DelegatedResourceAccountIndexCapsule> tmpFromList =
107107
new ArrayList<>(this.prefixQuery(key).values());
108108
tmpFromList.sort(Comparator.comparing(DelegatedResourceAccountIndexCapsule::getTimestamp));

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
import com.google.protobuf.Any;
77
import com.google.protobuf.ByteString;
88
import java.io.File;
9+
import java.util.List;
910
import lombok.extern.slf4j.Slf4j;
1011
import org.junit.AfterClass;
1112
import org.junit.Assert;
1213
import org.junit.Before;
1314
import org.junit.BeforeClass;
1415
import org.junit.Test;
1516
import org.tron.common.application.TronApplicationContext;
17+
import org.tron.common.crypto.ECKey;
1618
import org.tron.common.utils.ByteArray;
1719
import org.tron.common.utils.FileUtil;
20+
import org.tron.common.utils.Utils;
1821
import org.tron.core.ChainBaseManager;
1922
import org.tron.core.Constant;
2023
import org.tron.core.Wallet;
@@ -313,6 +316,70 @@ public void testFreezeDelegatedBalanceForBandwidth() {
313316
}
314317
}
315318

319+
@Test
320+
public void testMultiFreezeDelegatedBalanceForBandwidth() {
321+
dbManager.getDynamicPropertiesStore().saveAllowDelegateResource(1);
322+
dbManager.getDynamicPropertiesStore().saveAllowDelegateOptimization(1L);
323+
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(10000L);
324+
long frozenBalance = 1_000_000_000L;
325+
long duration = 3;
326+
final int RECEIVE_COUNT = 100;
327+
String[] RECEIVE_ADDRESSES = new String[RECEIVE_COUNT + 1];
328+
329+
DelegatedResourceAccountIndexCapsule ownerIndexCapsule =
330+
new DelegatedResourceAccountIndexCapsule(
331+
ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)));
332+
for (int i = 0; i < RECEIVE_COUNT + 1; i++) {
333+
ECKey ecKey = new ECKey(Utils.getRandom());
334+
RECEIVE_ADDRESSES[i] = ByteArray.toHexString(ecKey.getAddress());
335+
if (i != RECEIVE_COUNT) {
336+
ownerIndexCapsule.addToAccount(ByteString.copyFrom(ecKey.getAddress()));
337+
}
338+
}
339+
dbManager.getDelegatedResourceAccountIndexStore().put(
340+
ByteArray.fromHexString(OWNER_ADDRESS), ownerIndexCapsule);
341+
AccountCapsule receiverCapsule =
342+
new AccountCapsule(
343+
ByteString.copyFromUtf8("receiver"),
344+
ByteString.copyFrom(ByteArray.fromHexString(RECEIVE_ADDRESSES[RECEIVE_COUNT])),
345+
AccountType.Normal,
346+
initBalance);
347+
dbManager.getAccountStore().put(receiverCapsule.getAddress().toByteArray(), receiverCapsule);
348+
349+
TransactionResultCapsule ret = new TransactionResultCapsule();
350+
FreezeBalanceActuator actuator = new FreezeBalanceActuator();
351+
actuator.setChainBaseManager(dbManager.getChainBaseManager())
352+
.setAny(getDelegatedContractForBandwidth(
353+
OWNER_ADDRESS, RECEIVE_ADDRESSES[RECEIVE_COUNT], frozenBalance, duration));
354+
try {
355+
ownerIndexCapsule = dbManager
356+
.getDelegatedResourceAccountIndexStore().getIndex(ByteArray.fromHexString(OWNER_ADDRESS));
357+
List<ByteString> beforeList = ownerIndexCapsule.getToAccountsList();
358+
actuator.validate();
359+
actuator.execute(ret);
360+
361+
//check DelegatedResourceAccountIndex convert
362+
ownerIndexCapsule = dbManager
363+
.getDelegatedResourceAccountIndexStore().get(ByteArray.fromHexString(OWNER_ADDRESS));
364+
Assert.assertNull(ownerIndexCapsule);
365+
366+
ownerIndexCapsule = dbManager
367+
.getDelegatedResourceAccountIndexStore().getIndex(ByteArray.fromHexString(OWNER_ADDRESS));
368+
Assert.assertEquals(0, ownerIndexCapsule.getFromAccountsList().size());
369+
List<ByteString> tmpList = ownerIndexCapsule.getToAccountsList();
370+
Assert.assertEquals(RECEIVE_COUNT + 1, ownerIndexCapsule.getToAccountsList().size());
371+
for (int i = 0; i < RECEIVE_COUNT; i++) {
372+
Assert.assertEquals(beforeList.get(i), tmpList.get(i));
373+
}
374+
Assert.assertEquals(RECEIVE_ADDRESSES[RECEIVE_COUNT],
375+
ByteArray.toHexString(tmpList.get(RECEIVE_COUNT).toByteArray()));
376+
} catch (ContractValidateException | ContractExeException e) {
377+
Assert.fail("con not reach here");
378+
}
379+
dbManager.getDynamicPropertiesStore().saveAllowDelegateOptimization(0L);
380+
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(10000L);
381+
}
382+
316383
@Test
317384
public void testFreezeDelegatedBalanceForCpuSameNameTokenActive() {
318385
dbManager.getDynamicPropertiesStore().saveAllowDelegateResource(1);

0 commit comments

Comments
 (0)