Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion actuator/src/main/java/org/tron/core/utils/ProposalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,21 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
}
break;
}
case ALLOW_OPTIMIZE_TVM_STORAGE_KEY: {
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_8_2_4)) {
throw new ContractValidateException(
"Bad chain parameter id [ALLOW_OPTIMIZE_TVM_STORAGE_KEY]");
}
if (dynamicPropertiesStore.getAllowOptimizeTvmStorageKey() == 1) {
throw new ContractValidateException(
"[ALLOW_OPTIMIZE_TVM_STORAGE_KEY] has been valid, no need to propose again");
}
if (value != 1) {
throw new ContractValidateException(
"This value[ALLOW_OPTIMIZE_TVM_STORAGE_KEY] is only allowed to be 1");
}
break;
}
default:
break;
}
Expand Down Expand Up @@ -1045,7 +1060,8 @@ public enum ProposalType { // current value, value range
ALLOW_TVM_OSAKA(96), // 0, 1
ALLOW_HARDEN_RESOURCE_CALCULATION(97), // 0, 1
ALLOW_HARDEN_EXCHANGE_CALCULATION(98), // 0, 1
ALLOW_OPTIMIZE_TVM_STORAGE(99); // 0, 1
ALLOW_OPTIMIZE_TVM_STORAGE(99), // 0, 1
ALLOW_OPTIMIZE_TVM_STORAGE_KEY(102); // 0, 1
private long code;

ProposalType(long code) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static void load(StoreFactory storeFactory, boolean isolate) {
snapshot.allowTvmSelfdestructRestriction = ds.getAllowTvmSelfdestructRestriction() == 1;
snapshot.allowTvmOsaka = ds.getAllowTvmOsaka() == 1;
snapshot.allowHardenResourceCalculation = ds.getAllowHardenResourceCalculation() == 1;
snapshot.allowOptimizeTvmStorage = ds.getAllowOptimizeTvmStorage() == 1;
snapshot.allowOptimizeTvmStorageKey = ds.allowOptimizeTvmStorageKey();
if (isolate) {
VMConfig.setLocalSnapshot(snapshot);
} else {
Expand Down
4 changes: 4 additions & 0 deletions actuator/src/main/java/org/tron/core/vm/program/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,10 @@ public byte[] getCodeHashAt(DataWord address) {
if (ByteUtil.isNullOrZeroArray(codeHash)) {
byte[] code = getCodeAt(address);
codeHash = Hash.sha3(code);
if (VMConfig.allowOptimizeTvmStorageKey()
&& ByteUtil.isNullOrZeroArray(code)) {
return codeHash;
}
contract.setCodeHash(codeHash);
getContractState().updateContract(tronAddr, contract);
}
Expand Down
17 changes: 15 additions & 2 deletions actuator/src/main/java/org/tron/core/vm/program/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.tron.common.utils.ForkController;
import org.tron.core.capsule.StorageRowCapsule;
import org.tron.core.config.Parameter.ForkBlockVersionEnum;
import org.tron.core.store.DynamicPropertiesStore;
import org.tron.core.store.StorageRowStore;

public class Storage {
Expand All @@ -28,6 +29,7 @@ private enum ReadKind {
private final Map<DataWord, DataWord> oldRowKeyOwners = new HashMap<>();
private final Map<DataWord, ReadKind> readKinds = new HashMap<>();
private final boolean optimizeTvmStorage;
private final boolean legacyReadProtection;
private final boolean aliasCheckEnabled;

@Getter
Expand All @@ -39,11 +41,12 @@ private enum ReadKind {
@Setter
private int contractVersion;

public Storage(byte[] address, StorageRowStore store, boolean optimizeTvmStorage) {
public Storage(byte[] address, StorageRowStore store, DynamicPropertiesStore dynamicStore) {
addrHash = addrHash(address);
this.address = address;
this.store = store;
this.optimizeTvmStorage = optimizeTvmStorage;
this.optimizeTvmStorage = dynamicStore.allowTvmStorageOptimization();
this.legacyReadProtection = dynamicStore.allowOptimizeTvmStorageKey();
this.aliasCheckEnabled = ForkController.instance().pass(ForkBlockVersionEnum.VERSION_4_8_2_3);
}

Expand All @@ -53,6 +56,7 @@ public Storage(Storage storage) {
this.store = storage.store;
this.contractVersion = storage.contractVersion;
this.optimizeTvmStorage = storage.optimizeTvmStorage;
this.legacyReadProtection = storage.legacyReadProtection;
this.aliasCheckEnabled = storage.aliasCheckEnabled;
storage.getRowCache().forEach((DataWord key, StorageRowCapsule row) -> {
StorageRowCapsule newRow = new StorageRowCapsule(row);
Expand Down Expand Up @@ -158,6 +162,12 @@ private DataWord getOptimized(DataWord key) {
}

if (oldKeyTaken(key)) {
if (legacyReadProtection) {
StorageRowCapsule oldRow = store.get(getOldRowKey(key));
if (oldRow != null && oldRow.getValue() != null) {
throw new Program.OutOfTimeException("CPU timeout for storage check");
}
}
return null;
}

Expand Down Expand Up @@ -226,6 +236,9 @@ private void commitOptimized() {
store.delete(getOldRowKey(key));
}
});
if (legacyReadProtection) {
return;
}
readKinds.forEach((DataWord key, ReadKind kind) -> {
StorageRowCapsule row = rowCache.get(key);
if (row != null && row.isDirty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,7 @@ public Storage getStorage(byte[] address) {
storage = parentStorage;
}
} else {
storage = new Storage(address, getStorageRowStore(),
VMConfig.allowOptimizeTvmStorage());
storage = new Storage(address, getStorageRowStore(), getDynamicPropertiesStore());
}
ContractCapsule contract = getContract(address);
if (contract != null) {
Expand Down
Loading
Loading