1212import org .tron .common .utils .ForkController ;
1313import org .tron .core .capsule .StorageRowCapsule ;
1414import org .tron .core .config .Parameter .ForkBlockVersionEnum ;
15+ import org .tron .core .store .DynamicPropertiesStore ;
1516import org .tron .core .store .StorageRowStore ;
1617
1718public 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 ()) {
0 commit comments