Skip to content

Commit 705382e

Browse files
Merge pull request #7053 from yanghang8612/opt/storage-read-migration
feat: improve execution consistency and storage handling
2 parents 5c449a1 + 4786eb0 commit 705382e

20 files changed

Lines changed: 948 additions & 62 deletions

File tree

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,21 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
956956
}
957957
break;
958958
}
959+
case ALLOW_OPTIMIZE_TVM_STORAGE_KEY: {
960+
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_8_2_4)) {
961+
throw new ContractValidateException(
962+
"Bad chain parameter id [ALLOW_OPTIMIZE_TVM_STORAGE_KEY]");
963+
}
964+
if (dynamicPropertiesStore.getAllowOptimizeTvmStorageKey() == 1) {
965+
throw new ContractValidateException(
966+
"[ALLOW_OPTIMIZE_TVM_STORAGE_KEY] has been valid, no need to propose again");
967+
}
968+
if (value != 1) {
969+
throw new ContractValidateException(
970+
"This value[ALLOW_OPTIMIZE_TVM_STORAGE_KEY] is only allowed to be 1");
971+
}
972+
break;
973+
}
959974
default:
960975
break;
961976
}
@@ -1045,7 +1060,8 @@ public enum ProposalType { // current value, value range
10451060
ALLOW_TVM_OSAKA(96), // 0, 1
10461061
ALLOW_HARDEN_RESOURCE_CALCULATION(97), // 0, 1
10471062
ALLOW_HARDEN_EXCHANGE_CALCULATION(98), // 0, 1
1048-
ALLOW_OPTIMIZE_TVM_STORAGE(99); // 0, 1
1063+
ALLOW_OPTIMIZE_TVM_STORAGE(99), // 0, 1
1064+
ALLOW_OPTIMIZE_TVM_STORAGE_KEY(102); // 0, 1
10491065
private long code;
10501066

10511067
ProposalType(long code) {

‎actuator/src/main/java/org/tron/core/vm/config/ConfigLoader.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static void load(StoreFactory storeFactory, boolean isolate) {
5050
snapshot.allowTvmSelfdestructRestriction = ds.getAllowTvmSelfdestructRestriction() == 1;
5151
snapshot.allowTvmOsaka = ds.getAllowTvmOsaka() == 1;
5252
snapshot.allowHardenResourceCalculation = ds.getAllowHardenResourceCalculation() == 1;
53-
snapshot.allowOptimizeTvmStorage = ds.getAllowOptimizeTvmStorage() == 1;
53+
snapshot.allowOptimizeTvmStorageKey = ds.allowOptimizeTvmStorageKey();
5454
if (isolate) {
5555
VMConfig.setLocalSnapshot(snapshot);
5656
} else {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,10 @@ public byte[] getCodeHashAt(DataWord address) {
13151315
if (ByteUtil.isNullOrZeroArray(codeHash)) {
13161316
byte[] code = getCodeAt(address);
13171317
codeHash = Hash.sha3(code);
1318+
if (VMConfig.allowOptimizeTvmStorageKey()
1319+
&& ByteUtil.isNullOrZeroArray(code)) {
1320+
return codeHash;
1321+
}
13181322
contract.setCodeHash(codeHash);
13191323
getContractState().updateContract(tronAddr, contract);
13201324
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.tron.common.utils.ForkController;
1313
import org.tron.core.capsule.StorageRowCapsule;
1414
import org.tron.core.config.Parameter.ForkBlockVersionEnum;
15+
import org.tron.core.store.DynamicPropertiesStore;
1516
import org.tron.core.store.StorageRowStore;
1617

1718
public class Storage {
@@ -28,6 +29,7 @@ private enum ReadKind {
2829
private final Map<DataWord, DataWord> oldRowKeyOwners = new HashMap<>();
2930
private final Map<DataWord, ReadKind> readKinds = new HashMap<>();
3031
private final boolean optimizeTvmStorage;
32+
private final boolean legacyReadProtection;
3133
private final boolean aliasCheckEnabled;
3234

3335
@Getter
@@ -39,11 +41,12 @@ private enum ReadKind {
3941
@Setter
4042
private int contractVersion;
4143

42-
public Storage(byte[] address, StorageRowStore store, boolean optimizeTvmStorage) {
44+
public Storage(byte[] address, StorageRowStore store, DynamicPropertiesStore dynamicStore) {
4345
addrHash = addrHash(address);
4446
this.address = address;
4547
this.store = store;
46-
this.optimizeTvmStorage = optimizeTvmStorage;
48+
this.optimizeTvmStorage = dynamicStore.allowTvmStorageOptimization();
49+
this.legacyReadProtection = dynamicStore.allowOptimizeTvmStorageKey();
4750
this.aliasCheckEnabled = ForkController.instance().pass(ForkBlockVersionEnum.VERSION_4_8_2_3);
4851
}
4952

@@ -53,6 +56,7 @@ public Storage(Storage storage) {
5356
this.store = storage.store;
5457
this.contractVersion = storage.contractVersion;
5558
this.optimizeTvmStorage = storage.optimizeTvmStorage;
59+
this.legacyReadProtection = storage.legacyReadProtection;
5660
this.aliasCheckEnabled = storage.aliasCheckEnabled;
5761
storage.getRowCache().forEach((DataWord key, StorageRowCapsule row) -> {
5862
StorageRowCapsule newRow = new StorageRowCapsule(row);
@@ -158,6 +162,12 @@ private DataWord getOptimized(DataWord key) {
158162
}
159163

160164
if (oldKeyTaken(key)) {
165+
if (legacyReadProtection) {
166+
StorageRowCapsule oldRow = store.get(getOldRowKey(key));
167+
if (oldRow != null && oldRow.getValue() != null) {
168+
throw new Program.OutOfTimeException("CPU timeout for storage check");
169+
}
170+
}
161171
return null;
162172
}
163173

@@ -226,6 +236,9 @@ private void commitOptimized() {
226236
store.delete(getOldRowKey(key));
227237
}
228238
});
239+
if (legacyReadProtection) {
240+
return;
241+
}
229242
readKinds.forEach((DataWord key, ReadKind kind) -> {
230243
StorageRowCapsule row = rowCache.get(key);
231244
if (row != null && row.isDirty()) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,7 @@ public Storage getStorage(byte[] address) {
739739
storage = parentStorage;
740740
}
741741
} else {
742-
storage = new Storage(address, getStorageRowStore(),
743-
VMConfig.allowOptimizeTvmStorage());
742+
storage = new Storage(address, getStorageRowStore(), getDynamicPropertiesStore());
744743
}
745744
ContractCapsule contract = getContract(address);
746745
if (contract != null) {

0 commit comments

Comments
 (0)