Skip to content

Commit 25513b1

Browse files
committed
feat: optimize delegate and undelegate instruction handling
1 parent f8b05d4 commit 25513b1

14 files changed

Lines changed: 355 additions & 3 deletions

File tree

‎actuator/src/main/java/org/tron/core/vm/nativecontract/CancelAllUnfreezeV2Processor.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.tron.core.vm.VMConstant;
2020
import org.tron.core.vm.nativecontract.param.CancelAllUnfreezeV2Param;
2121
import org.tron.core.vm.repository.Repository;
22+
import org.tron.core.vm.utils.MUtil;
2223
import org.tron.protos.Protocol;
2324

2425
@Slf4j(topic = "VMProcessor")
@@ -39,6 +40,10 @@ public void validate(CancelAllUnfreezeV2Param param, Repository repo) throws Con
3940
throw new ContractValidateException(
4041
ACCOUNT_EXCEPTION_STR + readableOwnerAddress + NOT_EXIST_STR);
4142
}
43+
44+
if (accountCapsule.hasInvalidDelegatedV2()) {
45+
MUtil.checkCPUTimeForInvalidDelegatedV2Balance();
46+
}
4247
}
4348

4449
public Map<String, Long> execute(CancelAllUnfreezeV2Param param, Repository repo) throws ContractExeException {

‎actuator/src/main/java/org/tron/core/vm/nativecontract/FreezeBalanceV2Processor.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.tron.core.store.DynamicPropertiesStore;
1515
import org.tron.core.vm.nativecontract.param.FreezeBalanceV2Param;
1616
import org.tron.core.vm.repository.Repository;
17+
import org.tron.core.vm.utils.MUtil;
1718

1819
@Slf4j(topic = "VMProcessor")
1920
public class FreezeBalanceV2Processor {
@@ -63,6 +64,10 @@ public void validate(FreezeBalanceV2Param param, Repository repo) throws Contrac
6364
"Unknown ResourceCode, valid ResourceCode[BANDWIDTH、ENERGY]");
6465
}
6566
}
67+
68+
if (repo.isSelfDestructed(ownerAddress)) {
69+
MUtil.checkCPUTimeForFreezeV2AfterSelfDestruct();
70+
}
6671
}
6772

6873
public void execute(FreezeBalanceV2Param param, Repository repo) {

‎actuator/src/main/java/org/tron/core/vm/nativecontract/UnfreezeBalanceV2Processor.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.tron.core.vm.config.VMConfig;
2424
import org.tron.core.vm.nativecontract.param.UnfreezeBalanceV2Param;
2525
import org.tron.core.vm.repository.Repository;
26+
import org.tron.core.vm.utils.MUtil;
2627
import org.tron.core.vm.utils.VoteRewardUtil;
2728
import org.tron.protos.Protocol;
2829
import org.tron.protos.contract.Common;
@@ -86,6 +87,10 @@ public void validate(UnfreezeBalanceV2Param param, Repository repo)
8687
throw new ContractValidateException(
8788
"Invalid unfreeze_balance, [" + param.getUnfreezeBalance() + "] is invalid");
8889
}
90+
91+
if (accountCapsule.hasInvalidDelegatedV2()) {
92+
MUtil.checkCPUTimeForInvalidDelegatedV2Balance();
93+
}
8994
}
9095

9196
private boolean checkUnfreezeBalance(

‎actuator/src/main/java/org/tron/core/vm/nativecontract/WithdrawExpireUnfreezeProcessor.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.tron.core.store.DynamicPropertiesStore;
1818
import org.tron.core.vm.nativecontract.param.WithdrawExpireUnfreezeParam;
1919
import org.tron.core.vm.repository.Repository;
20+
import org.tron.core.vm.utils.MUtil;
2021
import org.tron.protos.Protocol;
2122

2223
@Slf4j(topic = "VMProcessor")
@@ -52,6 +53,10 @@ public void validate(WithdrawExpireUnfreezeParam param, Repository repo) throws
5253
logger.debug(e.getMessage(), e);
5354
throw new ContractValidateException(e.getMessage());
5455
}
56+
57+
if (accountCapsule.hasInvalidDelegatedV2()) {
58+
MUtil.checkCPUTimeForInvalidDelegatedV2Balance();
59+
}
5560
}
5661

5762
private long getTotalWithdrawUnfreeze(List<Protocol.Account.UnFreezeV2> unfrozenV2List, long now) {

‎actuator/src/main/java/org/tron/core/vm/program/ContractState.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ public boolean isNewContract(byte[] address) {
131131
return repository.isNewContract(address);
132132
}
133133

134+
@Override
135+
public void markSelfDestruct(byte[] address) {
136+
repository.markSelfDestruct(address);
137+
}
138+
139+
@Override
140+
public boolean isSelfDestructed(byte[] address) {
141+
return repository.isSelfDestructed(address);
142+
}
143+
134144
@Override
135145
public void updateAccount(byte[] address, AccountCapsule accountCapsule) {
136146
repository.updateAccount(address, accountCapsule);

‎actuator/src/main/java/org/tron/core/vm/program/Program.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,18 @@ public void suicide(DataWord obtainerAddress) {
512512
internalTx.setValue(internalTx.getValue() + expireUnfrozenBalance);
513513
}
514514
}
515+
516+
getContractState().markSelfDestruct(owner);
515517
getResult().addDeleteAccount(this.getContractAddress());
516518
}
517519

518520
public void suicide2(DataWord obtainerAddress) {
519-
520521
byte[] owner = getContextAddress();
522+
523+
if (getContractState().isSelfDestructed(obtainerAddress.toTronAddress())) {
524+
MUtil.checkCPUTimeForSelfDestructedBeneficiary();
525+
}
526+
521527
boolean isNewContract = getContractState().isNewContract(owner);
522528
if (isNewContract) {
523529
suicide(obtainerAddress);
@@ -540,6 +546,7 @@ public void suicide2(DataWord obtainerAddress) {
540546
"suicide", nonce, getContractState().getAccount(owner).getAssetMapV2());
541547

542548
if (FastByteComparisons.isEqual(owner, obtainer)) {
549+
getContractState().markSelfDestruct(owner);
543550
return;
544551
}
545552

@@ -579,6 +586,8 @@ public void suicide2(DataWord obtainerAddress) {
579586
internalTx.setValue(internalTx.getValue() + expireUnfrozenBalance);
580587
}
581588
}
589+
590+
getContractState().markSelfDestruct(owner);
582591
}
583592

584593
public Repository getContractState() {

‎actuator/src/main/java/org/tron/core/vm/repository/Repository.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public interface Repository {
5959

6060
boolean isNewContract(byte[] address);
6161

62+
void markSelfDestruct(byte[] address);
63+
64+
boolean isSelfDestructed(byte[] address);
65+
6266
void updateAccount(byte[] address, AccountCapsule accountCapsule);
6367

6468
void updateDynamicProperty(byte[] word, BytesCapsule bytesCapsule);

‎actuator/src/main/java/org/tron/core/vm/repository/RepositoryImpl.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public class RepositoryImpl implements Repository {
139139
private final HashMap<Key, Value<DelegatedResourceAccountIndex>> delegatedResourceAccountIndexCache = new HashMap<>();
140140
private final HashBasedTable<Key, Key, Value<byte[]>> transientStorage = HashBasedTable.create();
141141
private final HashSet<Key> newContractCache = new HashSet<>();
142+
private final HashSet<Key> selfDestructCache = new HashSet<>();
142143

143144
public static void removeLruCache(byte[] address) {
144145
}
@@ -572,6 +573,29 @@ public boolean isNewContract(byte[] address) {
572573
}
573574
}
574575

576+
@Override
577+
public void markSelfDestruct(byte[] address) {
578+
selfDestructCache.add(Key.create(address));
579+
}
580+
581+
@Override
582+
public boolean isSelfDestructed(byte[] address) {
583+
Key key = Key.create(address);
584+
if (selfDestructCache.contains(key)) {
585+
return true;
586+
}
587+
588+
if (parent != null) {
589+
boolean isSelfDestructed = parent.isSelfDestructed(address);
590+
if (isSelfDestructed) {
591+
selfDestructCache.add(key);
592+
}
593+
return isSelfDestructed;
594+
} else {
595+
return false;
596+
}
597+
}
598+
575599
@Override
576600
public void updateAccount(byte[] address, AccountCapsule accountCapsule) {
577601
accountCache.put(Key.create(address),
@@ -780,6 +804,7 @@ public void commit() {
780804
commitDelegatedResourceAccountIndexCache(repository);
781805
commitTransientStorage(repository);
782806
commitNewContractCache(repository);
807+
commitSelfDestructCache(repository);
783808
}
784809

785810
@Override
@@ -1142,6 +1167,12 @@ public void commitNewContractCache(Repository deposit) {
11421167
}
11431168
}
11441169

1170+
private void commitSelfDestructCache(Repository deposit) {
1171+
if (deposit != null) {
1172+
selfDestructCache.forEach(key -> deposit.markSelfDestruct(key.getData()));
1173+
}
1174+
}
1175+
11451176
/**
11461177
* Get the block id from the number.
11471178
*/

‎actuator/src/main/java/org/tron/core/vm/utils/MUtil.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,23 @@ public static void checkCPUTimeForModExp() {
7676
throw new OutOfTimeException("CPU timeout for modExp executing");
7777
}
7878
}
79+
80+
public static void checkCPUTimeForFreezeV2AfterSelfDestruct() {
81+
if (ForkController.instance().pass(Parameter.ForkBlockVersionEnum.VERSION_4_8_2_2)) {
82+
throw new OutOfTimeException("CPU timeout for FreezeBalanceV2 after SELFDESTRUCT");
83+
}
84+
}
85+
86+
public static void checkCPUTimeForSelfDestructedBeneficiary() {
87+
if (ForkController.instance().pass(Parameter.ForkBlockVersionEnum.VERSION_4_8_2_2)) {
88+
throw new OutOfTimeException(
89+
"CPU timeout for SELFDESTRUCT with selfdestructed beneficiary");
90+
}
91+
}
92+
93+
public static void checkCPUTimeForInvalidDelegatedV2Balance() {
94+
if (ForkController.instance().pass(Parameter.ForkBlockVersionEnum.VERSION_4_8_2_2)) {
95+
throw new OutOfTimeException("CPU timeout for invalid delegated V2 balance");
96+
}
97+
}
7998
}

‎chainbase/src/main/java/org/tron/core/capsule/AccountCapsule.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,10 @@ public void clearDelegatedResource() {
13361336
this.account = builder.build();
13371337
}
13381338

1339+
public boolean hasInvalidDelegatedV2() {
1340+
return getDelegatedFrozenV2BalanceForBandwidth() < 0 || getDelegatedFrozenV2BalanceForEnergy() < 0;
1341+
}
1342+
13391343
public void importAsset(byte[] key) {
13401344
this.account = AssetUtil.importAsset(this.account, key);
13411345
}

0 commit comments

Comments
 (0)