|
1 | 1 | package org.tron.common.runtime.vm; |
2 | 2 |
|
| 3 | +import static org.junit.Assert.assertArrayEquals; |
3 | 4 | import static org.junit.Assert.assertEquals; |
4 | 5 | import static org.junit.Assert.assertFalse; |
5 | 6 | import static org.junit.Assert.assertNotNull; |
|
15 | 16 | import org.junit.Test; |
16 | 17 | import org.springframework.test.util.ReflectionTestUtils; |
17 | 18 | import org.tron.common.crypto.Hash; |
| 19 | +import org.tron.common.runtime.Runtime; |
18 | 20 | import org.tron.common.runtime.TvmTestUtils; |
19 | 21 | import org.tron.common.utils.ByteUtil; |
20 | 22 | import org.tron.common.utils.ForkController; |
@@ -122,6 +124,69 @@ public void bothProposalsUse102ReadProtection() throws Exception { |
122 | 124 | assertProposalMode(1, 1); |
123 | 125 | } |
124 | 126 |
|
| 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 | + |
125 | 190 | private void assertProposalMode(int proposal99, int proposal102) throws Exception { |
126 | 191 | setMode(proposal99, proposal102); |
127 | 192 | String scenario = "99=" + proposal99 + ", 102=" + proposal102; |
@@ -402,6 +467,18 @@ private void replaceCode(byte[] address, String bytecode) { |
402 | 467 | repository.commit(); |
403 | 468 | } |
404 | 469 |
|
| 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 | + |
405 | 482 | private Transaction transaction(byte[] address) { |
406 | 483 | return TvmTestUtils.generateTriggerSmartContractAndGetTransaction( |
407 | 484 | Hex.decode(OWNER_ADDRESS), address, new byte[0], 0, 1_000_000_000L); |
|
0 commit comments