Skip to content

Commit 4786eb0

Browse files
committed
opt: skip EXTCODEHASH metadata writes only in special cases
1 parent 0a6eb93 commit 4786eb0

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

‎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
}

‎framework/src/test/java/org/tron/common/runtime/vm/StorageMigrationTest.java‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.tron.common.runtime.vm;
22

3+
import static org.junit.Assert.assertArrayEquals;
34
import static org.junit.Assert.assertEquals;
45
import static org.junit.Assert.assertFalse;
56
import static org.junit.Assert.assertNotNull;
@@ -15,6 +16,7 @@
1516
import org.junit.Test;
1617
import org.springframework.test.util.ReflectionTestUtils;
1718
import org.tron.common.crypto.Hash;
19+
import org.tron.common.runtime.Runtime;
1820
import org.tron.common.runtime.TvmTestUtils;
1921
import org.tron.common.utils.ByteUtil;
2022
import org.tron.common.utils.ForkController;
@@ -122,6 +124,69 @@ public void bothProposalsUse102ReadProtection() throws Exception {
122124
assertProposalMode(1, 1);
123125
}
124126

127+
@Test
128+
public void extCodeHashSkipsEmptyCodeWriteWhenProposal102() throws Exception {
129+
// Non-empty code is written back even when 102 is on. Only empty code skips the write.
130+
for (boolean emptyCode : new boolean[]{false, true}) {
131+
byte[] targetCode = emptyCode ? new byte[0] : Hex.decode("600160005500");
132+
for (int proposal99 : new int[]{0, 1}) {
133+
for (int proposal102 : new int[]{0, 1}) {
134+
setMode(proposal99, proposal102);
135+
boolean skipWrite = proposal102 == 1 && emptyCode;
136+
for (byte[] initialHash : new byte[][]{new byte[0], new byte[32],
137+
new DataWord(123).getData()}) {
138+
String scenario = "empty=" + emptyCode + ", 99=" + proposal99 + ", 102=" + proposal102
139+
+ ", initialHash=" + Hex.toHexString(initialHash);
140+
byte[] target = install(emptyCode ? "00" : Hex.toHexString(targetCode));
141+
if (emptyCode) {
142+
dbManager.getCodeStore().delete(target);
143+
}
144+
setCodeHash(target, initialHash);
145+
byte[] reader = install(readCodeHash(target) + "60005260206000f3");
146+
boolean missing = ByteUtil.isNullOrZeroArray(initialHash);
147+
byte[] returned = missing ? Hash.sha3(targetCode) : initialHash;
148+
byte[] stored = missing && !skipWrite ? Hash.sha3(targetCode) : initialHash;
149+
150+
for (int read = 0; read < 2; read++) {
151+
Runtime result = TvmTestUtils.processTransactionAndReturnRuntime(
152+
transaction(reader), dbManager, null);
153+
assertEquals(scenario, SUCCESS, result.getResult().getResultCode());
154+
assertArrayEquals(scenario, returned, result.getResult().getHReturn());
155+
assertArrayEquals(scenario, stored,
156+
repository().getContract(target).getCodeHash());
157+
}
158+
}
159+
}
160+
}
161+
}
162+
}
163+
164+
@Test
165+
public void extCodeHashMetadataRespectsTransactionAndSessionRollback() throws Exception {
166+
byte[] targetCode = Hex.decode("600160005500");
167+
for (int proposal99 : new int[]{0, 1}) {
168+
for (int proposal102 : new int[]{0, 1}) {
169+
setMode(proposal99, proposal102);
170+
String scenario = "99=" + proposal99 + ", 102=" + proposal102;
171+
byte[] target = install(Hex.toHexString(targetCode));
172+
setCodeHash(target, new byte[0]);
173+
for (String end : new String[]{REVERT_CODE, INVALID}) {
174+
byte[] reader = install(readCodeHash(target) + "50" + end);
175+
assertResult(scenario + ", end=" + end, resultFor(end), reader);
176+
assertArrayEquals(scenario, new byte[0], repository().getContract(target).getCodeHash());
177+
}
178+
179+
byte[] reader = install(readCodeHash(target) + "50" + STOP);
180+
try (ISession ignored = dbManager.getRevokingStore().buildSession()) {
181+
assertResult(scenario + " before session rollback", SUCCESS, reader);
182+
assertArrayEquals(scenario, Hash.sha3(targetCode),
183+
repository().getContract(target).getCodeHash());
184+
}
185+
assertArrayEquals(scenario, new byte[0], repository().getContract(target).getCodeHash());
186+
}
187+
}
188+
}
189+
125190
private void assertProposalMode(int proposal99, int proposal102) throws Exception {
126191
setMode(proposal99, proposal102);
127192
String scenario = "99=" + proposal99 + ", 102=" + proposal102;
@@ -402,6 +467,18 @@ private void replaceCode(byte[] address, String bytecode) {
402467
repository.commit();
403468
}
404469

470+
private void setCodeHash(byte[] address, byte[] codeHash) {
471+
Repository repository = repository();
472+
ContractCapsule contract = repository.getContract(address);
473+
contract.setCodeHash(codeHash);
474+
repository.updateContract(address, contract);
475+
repository.commit();
476+
}
477+
478+
private String readCodeHash(byte[] address) {
479+
return "73" + Hex.toHexString(Arrays.copyOfRange(address, 1, 21)) + "3f";
480+
}
481+
405482
private Transaction transaction(byte[] address) {
406483
return TvmTestUtils.generateTriggerSmartContractAndGetTransaction(
407484
Hex.decode(OWNER_ADDRESS), address, new byte[0], 0, 1_000_000_000L);

0 commit comments

Comments
 (0)