|
| 1 | +package org.tron.common.logsfilter; |
| 2 | + |
| 3 | +import static org.mockito.ArgumentMatchers.any; |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | +import static org.mockito.Mockito.never; |
| 6 | +import static org.mockito.Mockito.verify; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | +import static org.tron.common.logsfilter.ContractEventParserAbi.MAX_ABI_ENTRIES; |
| 9 | +import static org.tron.common.logsfilter.ContractEventParserAbi.MAX_ABI_EVENTS; |
| 10 | +import static org.tron.common.logsfilter.ContractEventParserAbi.MAX_ABI_IDENTIFIER_LENGTH; |
| 11 | + |
| 12 | +import com.google.common.collect.Lists; |
| 13 | +import java.io.IOException; |
| 14 | +import java.lang.reflect.Field; |
| 15 | +import java.lang.reflect.Method; |
| 16 | +import java.util.List; |
| 17 | +import org.junit.AfterClass; |
| 18 | +import org.junit.Assert; |
| 19 | +import org.junit.BeforeClass; |
| 20 | +import org.junit.ClassRule; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.rules.TemporaryFolder; |
| 23 | +import org.mockito.ArgumentCaptor; |
| 24 | +import org.tron.common.TestConstants; |
| 25 | +import org.tron.common.crypto.Hash; |
| 26 | +import org.tron.common.logsfilter.capsule.ContractTriggerCapsule; |
| 27 | +import org.tron.common.logsfilter.capsule.RawData; |
| 28 | +import org.tron.common.logsfilter.trigger.ContractEventTrigger; |
| 29 | +import org.tron.common.logsfilter.trigger.ContractLogTrigger; |
| 30 | +import org.tron.common.logsfilter.trigger.ContractTrigger; |
| 31 | +import org.tron.common.runtime.vm.DataWord; |
| 32 | +import org.tron.common.runtime.vm.LogInfo; |
| 33 | +import org.tron.core.config.args.Args; |
| 34 | +import org.tron.core.services.event.BlockEventGet; |
| 35 | +import org.tron.protos.contract.SmartContractOuterClass.SmartContract.ABI; |
| 36 | + |
| 37 | +/** |
| 38 | + * Behavior tests for the ABI-size guard on both event-subscription decode paths — version 0 |
| 39 | + * ({@link ContractTriggerCapsule#processTrigger()}) and version 1 |
| 40 | + * ({@link BlockEventGet} {@code processTrigger}). Each over-limit case carries a topic0 that |
| 41 | + * matches an event present in the ABI, so without the guard it would decode; the test therefore |
| 42 | + * fails if the call-site guard is removed, not merely if the predicate changes. |
| 43 | + */ |
| 44 | +public class ContractAbiSizeLimitTest { |
| 45 | + |
| 46 | + @ClassRule |
| 47 | + public static final TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 48 | + |
| 49 | + @BeforeClass |
| 50 | + public static void init() throws IOException { |
| 51 | + Args.setParam(new String[] {"--output-directory", temporaryFolder.newFolder().toString()}, |
| 52 | + TestConstants.TEST_CONF); |
| 53 | + Args.getInstance().getStorage().setContractParseSwitch(true); |
| 54 | + } |
| 55 | + |
| 56 | + @AfterClass |
| 57 | + public static void destroy() { |
| 58 | + Args.clearParam(); |
| 59 | + } |
| 60 | + |
| 61 | + private ABI.Entry event(String name, String... paramTypes) { |
| 62 | + ABI.Entry.Builder e = ABI.Entry.newBuilder() |
| 63 | + .setType(ABI.Entry.EntryType.Event).setAnonymous(false).setName(name); |
| 64 | + for (String t : paramTypes) { |
| 65 | + e.addInputs(ABI.Entry.Param.newBuilder().setType(t)); |
| 66 | + } |
| 67 | + return e.build(); |
| 68 | + } |
| 69 | + |
| 70 | + private ABI.Entry function(String name) { |
| 71 | + return ABI.Entry.newBuilder().setType(ABI.Entry.EntryType.Function).setName(name).build(); |
| 72 | + } |
| 73 | + |
| 74 | + /** topic0 = sha3("name(type1,type2)"), i.e. what a log matching this event would carry. */ |
| 75 | + private DataWord topic0(String name, String... paramTypes) { |
| 76 | + StringBuilder sig = new StringBuilder(name).append("("); |
| 77 | + for (int i = 0; i < paramTypes.length; i++) { |
| 78 | + if (i > 0) { |
| 79 | + sig.append(","); |
| 80 | + } |
| 81 | + sig.append(paramTypes[i]); |
| 82 | + } |
| 83 | + sig.append(")"); |
| 84 | + return new DataWord(Hash.sha3(sig.toString().getBytes())); |
| 85 | + } |
| 86 | + |
| 87 | + private ContractTrigger trigger(ABI abi, DataWord topic0) { |
| 88 | + byte[] address = new byte[20]; |
| 89 | + List<DataWord> topics = Lists.newArrayList(topic0); |
| 90 | + byte[] data = new byte[64]; |
| 91 | + data[31] = 0x2a; |
| 92 | + data[63] = 0x7b; |
| 93 | + ContractTrigger t = new ContractTrigger(); |
| 94 | + t.setBlockNumber(1L); |
| 95 | + t.setRemoved(false); |
| 96 | + t.setAbi(abi); |
| 97 | + t.setLogInfo(new LogInfo(address, topics, data)); |
| 98 | + t.setRawData(new RawData(address, topics, data)); |
| 99 | + return t; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Runs the version-1 path: {@link BlockEventGet} {@code processTrigger} returns the decoded |
| 104 | + * event trigger, or the raw log trigger when the ABI is rejected. |
| 105 | + */ |
| 106 | + private ContractTrigger v1Process(ContractTrigger input) throws Exception { |
| 107 | + Method m = BlockEventGet.class.getDeclaredMethod("processTrigger", ContractTrigger.class); |
| 108 | + m.setAccessible(true); |
| 109 | + return (ContractTrigger) m.invoke(new BlockEventGet(), input); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void v1_inLimitAbiIsDecodedToEvent() throws Exception { |
| 114 | + ABI abi = ABI.newBuilder().addEntrys(event("Transfer", "address", "uint256")).build(); |
| 115 | + ContractTrigger out = v1Process(trigger(abi, topic0("Transfer", "address", "uint256"))); |
| 116 | + Assert.assertTrue(out instanceof ContractEventTrigger); |
| 117 | + Assert.assertEquals("Transfer", ((ContractEventTrigger) out).getEventName()); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void v1_overEventLimitFallsBackToRawLog() throws Exception { |
| 122 | + ABI.Builder b = ABI.newBuilder(); |
| 123 | + for (int i = 0; i < MAX_ABI_EVENTS; i++) { |
| 124 | + b.addEntrys(event("Filler" + i)); |
| 125 | + } |
| 126 | + b.addEntrys(event("Transfer", "address", "uint256")); // 513th event, matches topic0 |
| 127 | + ContractTrigger in = trigger(b.build(), topic0("Transfer", "address", "uint256")); |
| 128 | + ContractTrigger out = v1Process(in); |
| 129 | + Assert.assertTrue(out instanceof ContractLogTrigger); |
| 130 | + Assert.assertFalse(out instanceof ContractEventTrigger); |
| 131 | + // raw data is preserved |
| 132 | + Assert.assertEquals(in.getLogInfo().getHexTopics(), ((ContractLogTrigger) out).getTopicList()); |
| 133 | + Assert.assertEquals(in.getLogInfo().getHexData(), ((ContractLogTrigger) out).getData()); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void v1_overEntryLimitFallsBackToRawLog() throws Exception { |
| 138 | + ABI.Builder b = ABI.newBuilder(); |
| 139 | + for (int i = 0; i < MAX_ABI_ENTRIES; i++) { |
| 140 | + b.addEntrys(function("f" + i)); |
| 141 | + } |
| 142 | + b.addEntrys(event("Transfer", "address", "uint256")); // 1025th entry, matches topic0 |
| 143 | + ContractTrigger out = v1Process(trigger(b.build(), topic0("Transfer", "address", "uint256"))); |
| 144 | + Assert.assertTrue(out instanceof ContractLogTrigger); |
| 145 | + Assert.assertFalse(out instanceof ContractEventTrigger); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void v1_oversizedEntrySkippedButValidEntryMatches() throws Exception { |
| 150 | + ABI abi = ABI.newBuilder() |
| 151 | + .addEntrys(event(repeatA(MAX_ABI_IDENTIFIER_LENGTH + 1))) // oversized name, skipped |
| 152 | + .addEntrys(event("Transfer", "address", "uint256")) // valid, matches |
| 153 | + .build(); |
| 154 | + ContractTrigger out = v1Process(trigger(abi, topic0("Transfer", "address", "uint256"))); |
| 155 | + Assert.assertTrue(out instanceof ContractEventTrigger); |
| 156 | + Assert.assertEquals("Transfer", ((ContractEventTrigger) out).getEventName()); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void v1_oversizedMatchingEntryIsSkippedAndFallsBackToRawLog() throws Exception { |
| 161 | + // The over-long entry is itself the one the log matches: the per-entry skip is the only |
| 162 | + // thing preventing it from being decoded, so dropping that guard would turn this into an |
| 163 | + // event instead of a raw log. |
| 164 | + String bigName = repeatA(MAX_ABI_IDENTIFIER_LENGTH + 1); |
| 165 | + ABI abi = ABI.newBuilder().addEntrys(event(bigName)).build(); |
| 166 | + ContractTrigger out = v1Process(trigger(abi, topic0(bigName))); |
| 167 | + Assert.assertTrue(out instanceof ContractLogTrigger); |
| 168 | + Assert.assertFalse(out instanceof ContractEventTrigger); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + public void v0_inLimitAbiIsDecodedToEvent() throws Exception { |
| 173 | + EventPluginLoader mock = contractTriggersEnabled(); |
| 174 | + EventPluginLoader original = swapLoader(mock); |
| 175 | + try { |
| 176 | + ABI abi = ABI.newBuilder().addEntrys(event("Transfer", "address", "uint256")).build(); |
| 177 | + new ContractTriggerCapsule(trigger(abi, topic0("Transfer", "address", "uint256"))) |
| 178 | + .processTrigger(); |
| 179 | + ArgumentCaptor<ContractEventTrigger> captor = |
| 180 | + ArgumentCaptor.forClass(ContractEventTrigger.class); |
| 181 | + verify(mock).postContractEventTrigger(captor.capture()); |
| 182 | + verify(mock, never()).postContractLogTrigger(any()); |
| 183 | + Assert.assertEquals("Transfer", captor.getValue().getEventName()); |
| 184 | + } finally { |
| 185 | + swapLoader(original); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + public void v0_overEventLimitFallsBackToRawLog() throws Exception { |
| 191 | + ABI.Builder b = ABI.newBuilder(); |
| 192 | + for (int i = 0; i < MAX_ABI_EVENTS; i++) { |
| 193 | + b.addEntrys(event("Filler" + i)); |
| 194 | + } |
| 195 | + b.addEntrys(event("Transfer", "address", "uint256")); // would decode without the guard |
| 196 | + v0AssertFallsBackToRawLog(b.build(), topic0("Transfer", "address", "uint256")); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + public void v0_overEntryLimitFallsBackToRawLog() throws Exception { |
| 201 | + ABI.Builder b = ABI.newBuilder(); |
| 202 | + for (int i = 0; i < MAX_ABI_ENTRIES; i++) { |
| 203 | + b.addEntrys(function("f" + i)); |
| 204 | + } |
| 205 | + b.addEntrys(event("Transfer", "address", "uint256")); // would decode without the guard |
| 206 | + v0AssertFallsBackToRawLog(b.build(), topic0("Transfer", "address", "uint256")); |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + public void v0_oversizedMatchingEntryFallsBackToRawLog() throws Exception { |
| 211 | + String bigName = repeatA(MAX_ABI_IDENTIFIER_LENGTH + 1); |
| 212 | + ABI abi = ABI.newBuilder().addEntrys(event(bigName)).build(); |
| 213 | + v0AssertFallsBackToRawLog(abi, topic0(bigName)); |
| 214 | + } |
| 215 | + |
| 216 | + @Test |
| 217 | + public void v0_oversizedEntrySkippedButValidEntryMatches() throws Exception { |
| 218 | + EventPluginLoader mock = contractTriggersEnabled(); |
| 219 | + EventPluginLoader original = swapLoader(mock); |
| 220 | + try { |
| 221 | + ABI abi = ABI.newBuilder() |
| 222 | + .addEntrys(event(repeatA(MAX_ABI_IDENTIFIER_LENGTH + 1))) // oversized name, skipped |
| 223 | + .addEntrys(event("Transfer", "address", "uint256")) // valid, matches |
| 224 | + .build(); |
| 225 | + new ContractTriggerCapsule(trigger(abi, topic0("Transfer", "address", "uint256"))) |
| 226 | + .processTrigger(); |
| 227 | + ArgumentCaptor<ContractEventTrigger> captor = |
| 228 | + ArgumentCaptor.forClass(ContractEventTrigger.class); |
| 229 | + verify(mock).postContractEventTrigger(captor.capture()); |
| 230 | + Assert.assertEquals("Transfer", captor.getValue().getEventName()); |
| 231 | + } finally { |
| 232 | + swapLoader(original); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + private void v0AssertFallsBackToRawLog(ABI abi, DataWord topic0) throws Exception { |
| 237 | + EventPluginLoader mock = contractTriggersEnabled(); |
| 238 | + EventPluginLoader original = swapLoader(mock); |
| 239 | + try { |
| 240 | + ContractTrigger in = trigger(abi, topic0); |
| 241 | + new ContractTriggerCapsule(in).processTrigger(); |
| 242 | + ArgumentCaptor<ContractLogTrigger> captor = |
| 243 | + ArgumentCaptor.forClass(ContractLogTrigger.class); |
| 244 | + verify(mock, never()).postContractEventTrigger(any()); |
| 245 | + verify(mock).postContractLogTrigger(captor.capture()); |
| 246 | + // raw data is preserved |
| 247 | + Assert.assertEquals(in.getLogInfo().getHexTopics(), captor.getValue().getTopicList()); |
| 248 | + Assert.assertEquals(in.getLogInfo().getHexData(), captor.getValue().getData()); |
| 249 | + } finally { |
| 250 | + swapLoader(original); |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + private EventPluginLoader contractTriggersEnabled() { |
| 255 | + EventPluginLoader mock = mock(EventPluginLoader.class); |
| 256 | + when(mock.isContractEventTriggerEnable()).thenReturn(true); |
| 257 | + when(mock.isContractLogTriggerEnable()).thenReturn(true); |
| 258 | + return mock; |
| 259 | + } |
| 260 | + |
| 261 | + private EventPluginLoader swapLoader(EventPluginLoader loader) throws Exception { |
| 262 | + Field f = EventPluginLoader.class.getDeclaredField("instance"); |
| 263 | + f.setAccessible(true); |
| 264 | + EventPluginLoader previous = (EventPluginLoader) f.get(null); |
| 265 | + f.set(null, loader); |
| 266 | + return previous; |
| 267 | + } |
| 268 | + |
| 269 | + private String repeatA(int n) { |
| 270 | + StringBuilder sb = new StringBuilder(n); |
| 271 | + for (int i = 0; i < n; i++) { |
| 272 | + sb.append('A'); |
| 273 | + } |
| 274 | + return sb.toString(); |
| 275 | + } |
| 276 | +} |
0 commit comments