|
| 1 | +package org.tron.common.runtime.vm; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import org.apache.commons.lang3.tuple.Pair; |
| 6 | +import org.junit.AfterClass; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.Before; |
| 9 | +import org.junit.BeforeClass; |
| 10 | +import org.junit.Test; |
| 11 | +import org.tron.common.BaseTest; |
| 12 | +import org.tron.common.TestConstants; |
| 13 | +import org.tron.common.parameter.CommonParameter; |
| 14 | +import org.tron.common.runtime.InternalTransaction; |
| 15 | +import org.tron.common.utils.ForkController; |
| 16 | +import org.tron.core.Constant; |
| 17 | +import org.tron.core.config.Parameter.ForkBlockVersionEnum; |
| 18 | +import org.tron.core.config.args.Args; |
| 19 | +import org.tron.core.exception.ContractValidateException; |
| 20 | +import org.tron.core.store.StoreFactory; |
| 21 | +import org.tron.core.vm.PrecompiledContracts; |
| 22 | +import org.tron.core.vm.PrecompiledContracts.PrecompiledContract; |
| 23 | +import org.tron.core.vm.config.ConfigLoader; |
| 24 | +import org.tron.core.vm.config.VMConfig; |
| 25 | +import org.tron.core.vm.program.Program; |
| 26 | +import org.tron.core.vm.program.Program.OutOfTimeException; |
| 27 | +import org.tron.core.vm.program.invoke.ProgramInvokeMockImpl; |
| 28 | +import org.tron.core.vm.utils.MUtil; |
| 29 | +import org.tron.protos.Protocol; |
| 30 | + |
| 31 | + |
| 32 | +@Slf4j |
| 33 | +public class Create2ModExpForkTest extends BaseTest { |
| 34 | + |
| 35 | + // mirrors the private Program.MAX_DEPTH |
| 36 | + private static final int MAX_CALL_DEPTH = 64; |
| 37 | + |
| 38 | + // mirrors PrecompiledContracts.ModExp.UPPER_BOUND |
| 39 | + private static final int MOD_EXP_UPPER_BOUND = 1024; |
| 40 | + |
| 41 | + // ModExp precompile address (0x05) |
| 42 | + private static final DataWord MOD_EXP_ADDR = new DataWord( |
| 43 | + "0000000000000000000000000000000000000000000000000000000000000005"); |
| 44 | + |
| 45 | + @BeforeClass |
| 46 | + public static void init() { |
| 47 | + Args.setParam(new String[]{"--output-directory", dbPath(), "--debug"}, TestConstants.TEST_CONF); |
| 48 | + CommonParameter.getInstance().setDebug(true); |
| 49 | + } |
| 50 | + |
| 51 | + @AfterClass |
| 52 | + public static void destroy() { |
| 53 | + ConfigLoader.disable = false; |
| 54 | + VMConfig.initVmHardFork(false); |
| 55 | + VMConfig.initAllowTvmCompatibleEvm(0); |
| 56 | + Args.clearParam(); |
| 57 | + } |
| 58 | + |
| 59 | + @Before |
| 60 | + public void setUp() { |
| 61 | + ForkController.instance().init(chainBaseManager); |
| 62 | + deactivateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void checkCPUTimeForCreate2_isGatedByFork() { |
| 67 | + MUtil.checkCPUTimeForCreate2(); |
| 68 | + |
| 69 | + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); |
| 70 | + |
| 71 | + OutOfTimeException ex = |
| 72 | + Assert.assertThrows(OutOfTimeException.class, MUtil::checkCPUTimeForCreate2); |
| 73 | + Assert.assertEquals("CPU timeout for create2 executing", ex.getMessage()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void checkCPUTimeForModExp_isGatedByFork() { |
| 78 | + MUtil.checkCPUTimeForModExp(); |
| 79 | + |
| 80 | + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); |
| 81 | + |
| 82 | + OutOfTimeException ex = |
| 83 | + Assert.assertThrows(OutOfTimeException.class, MUtil::checkCPUTimeForModExp); |
| 84 | + Assert.assertEquals("CPU timeout for modExp executing", ex.getMessage()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void modExp_degenerateInput_throwsOnlyAfterFork() { |
| 89 | + PrecompiledContract modExp = PrecompiledContracts.getContractForAddress(MOD_EXP_ADDR); |
| 90 | + byte[] data = buildModExpInput(MOD_EXP_UPPER_BOUND + 1); |
| 91 | + |
| 92 | + Pair<Boolean, byte[]> out = modExp.execute(data); |
| 93 | + Assert.assertTrue(out.getLeft()); |
| 94 | + |
| 95 | + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); |
| 96 | + |
| 97 | + OutOfTimeException ex = |
| 98 | + Assert.assertThrows(OutOfTimeException.class, () -> modExp.execute(data)); |
| 99 | + Assert.assertEquals("CPU timeout for modExp executing", ex.getMessage()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void modExp_atUpperBound_doesNotThrowAfterFork() { |
| 104 | + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); |
| 105 | + |
| 106 | + PrecompiledContract modExp = PrecompiledContracts.getContractForAddress(MOD_EXP_ADDR); |
| 107 | + Pair<Boolean, byte[]> out = modExp.execute(buildModExpInput(MOD_EXP_UPPER_BOUND)); |
| 108 | + Assert.assertTrue(out.getLeft()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void createContract2_atMaxDepth_legacyPath_throwsAfterFork() |
| 113 | + throws ContractValidateException { |
| 114 | + VMConfig.initAllowTvmCompatibleEvm(0); |
| 115 | + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); |
| 116 | + |
| 117 | + Program program = buildProgramAtMaxDepth(); |
| 118 | + OutOfTimeException ex = Assert.assertThrows(OutOfTimeException.class, |
| 119 | + () -> program.createContract2( |
| 120 | + DataWord.ZERO(), DataWord.ZERO(), DataWord.ZERO(), DataWord.ZERO())); |
| 121 | + Assert.assertEquals("CPU timeout for create2 executing", ex.getMessage()); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void createContract2_atMaxDepth_compatibleEvmOn_doesNotThrow() |
| 126 | + throws ContractValidateException { |
| 127 | + VMConfig.initAllowTvmCompatibleEvm(1); |
| 128 | + activateFork(ForkBlockVersionEnum.VERSION_4_8_1_1); |
| 129 | + |
| 130 | + Program program = buildProgramAtMaxDepth(); |
| 131 | + program.createContract2(DataWord.ZERO(), DataWord.ZERO(), DataWord.ZERO(), DataWord.ZERO()); |
| 132 | + Assert.assertEquals(DataWord.ZERO(), program.getStack().pop()); |
| 133 | + } |
| 134 | + |
| 135 | + // ---- helpers --------------------------------------------------------------------------------- |
| 136 | + |
| 137 | + private Program buildProgramAtMaxDepth() throws ContractValidateException { |
| 138 | + StoreFactory.init(); |
| 139 | + StoreFactory storeFactory = StoreFactory.getInstance(); |
| 140 | + storeFactory.setChainBaseManager(chainBaseManager); |
| 141 | + byte[] ops = new byte[] {0}; |
| 142 | + ProgramInvokeMockImpl invoke = new ProgramInvokeMockImpl(storeFactory, ops, ops) { |
| 143 | + @Override |
| 144 | + public int getCallDeep() { |
| 145 | + return MAX_CALL_DEPTH; |
| 146 | + } |
| 147 | + }; |
| 148 | + Program program = new Program(ops, ops, invoke, |
| 149 | + new InternalTransaction(Protocol.Transaction.getDefaultInstance(), |
| 150 | + InternalTransaction.TrxType.TRX_UNKNOWN_TYPE)); |
| 151 | + program.setRootTransactionId(new byte[32]); |
| 152 | + return program; |
| 153 | + } |
| 154 | + |
| 155 | + private byte[] buildModExpInput(int expLen) { |
| 156 | + byte[] data = new byte[96]; |
| 157 | + byte[] expLenWord = new DataWord(expLen).getData(); |
| 158 | + System.arraycopy(expLenWord, 0, data, 32, 32); |
| 159 | + return data; |
| 160 | + } |
| 161 | + |
| 162 | + private void activateFork(ForkBlockVersionEnum forkVersion) { |
| 163 | + byte[] stats = new byte[27]; |
| 164 | + Arrays.fill(stats, (byte) 1); |
| 165 | + chainBaseManager.getDynamicPropertiesStore().statsByVersion(forkVersion.getValue(), stats); |
| 166 | + long maintenanceTimeInterval = |
| 167 | + chainBaseManager.getDynamicPropertiesStore().getMaintenanceTimeInterval(); |
| 168 | + long hardForkTime = ((forkVersion.getHardForkTime() - 1) / maintenanceTimeInterval + 1) |
| 169 | + * maintenanceTimeInterval; |
| 170 | + chainBaseManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(hardForkTime + 1); |
| 171 | + } |
| 172 | + |
| 173 | + private void deactivateFork(ForkBlockVersionEnum forkVersion) { |
| 174 | + chainBaseManager.getDynamicPropertiesStore() |
| 175 | + .statsByVersion(forkVersion.getValue(), new byte[27]); |
| 176 | + chainBaseManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(0L); |
| 177 | + } |
| 178 | +} |
0 commit comments