|
| 1 | +/* |
| 2 | + * Copyright contributors to Besu. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: Apache-2.0 |
| 14 | + */ |
| 15 | +package org.hyperledger.besu.ethereum.api.jsonrpc.internal.results; |
| 16 | + |
| 17 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 18 | + |
| 19 | +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace; |
| 20 | +import org.hyperledger.besu.ethereum.core.Transaction; |
| 21 | +import org.hyperledger.besu.evm.tracing.TraceFrame; |
| 22 | + |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +import org.apache.tuweni.bytes.Bytes; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | +/** |
| 32 | + * Converts Ethereum transaction traces into 4byte tracer format. |
| 33 | + * |
| 34 | + * <p>The 4byteTracer collects the function selectors of every function executed in the lifetime of |
| 35 | + * a transaction, along with the size of the supplied call data. The result is a map where the keys |
| 36 | + * are SELECTOR-PARAMETERDATASIZE and the values are number of occurrences of this key. |
| 37 | + * |
| 38 | + * <p>Function selectors are the first 4 bytes of the Keccak-256 hash of function signatures, which |
| 39 | + * are used to identify which function to call in Solidity contracts. |
| 40 | + */ |
| 41 | +public class FourByteTracerResultConverter { |
| 42 | + private static final Logger LOG = LoggerFactory.getLogger(FourByteTracerResultConverter.class); |
| 43 | + |
| 44 | + |
| 45 | + /** |
| 46 | + * Converts a transaction trace to a 4byte tracer result. |
| 47 | + * |
| 48 | + * @param transactionTrace The transaction trace to convert |
| 49 | + * @return A 4byte tracer result containing function selectors and their occurrence counts |
| 50 | + * @throws NullPointerException if transactionTrace or its components are null |
| 51 | + */ |
| 52 | + public static FourByteTracerResult convert(final TransactionTrace transactionTrace) { |
| 53 | + checkNotNull( |
| 54 | + transactionTrace, "FourByteTracerResultConverter requires a non-null TransactionTrace"); |
| 55 | + checkNotNull( |
| 56 | + transactionTrace.getTransaction(), |
| 57 | + "FourByteTracerResultConverter requires non-null Transaction"); |
| 58 | + checkNotNull( |
| 59 | + transactionTrace.getResult(), |
| 60 | + "FourByteTracerResultConverter requires non-null Result"); |
| 61 | + |
| 62 | + final Map<String, Integer> selectorCounts = new HashMap<>(); |
| 63 | + final Transaction transaction = transactionTrace.getTransaction(); |
| 64 | + final List<TraceFrame> traceFrames = transactionTrace.getTraceFrames(); |
| 65 | + |
| 66 | + // Process the initial transaction call data only for message-call transactions |
| 67 | + if (transaction.getTo().isPresent()) { |
| 68 | + processCallData(transaction.getPayload(), selectorCounts); |
| 69 | + } |
| 70 | + |
| 71 | + // Process all trace frames for additional function calls |
| 72 | + if (traceFrames != null) { |
| 73 | + LOG.trace("Processing {} trace frames for 4byte tracer", traceFrames.size()); |
| 74 | + for (final TraceFrame frame : traceFrames) { |
| 75 | + if (shouldProcessFrame(frame)) { |
| 76 | + final Bytes inputData = frame.getInputData(); |
| 77 | + if (inputData != null) { |
| 78 | + processCallData(inputData, selectorCounts); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + LOG.trace("4byte tracer found {} unique function selectors", selectorCounts.size()); |
| 85 | + return new FourByteTracerResult(selectorCounts); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Processes call data to extract function selector and update counts. |
| 90 | + * |
| 91 | + * Stores the 4-byte function selector along with the size |
| 92 | + * of the call data minus the 4-byte selector (i.e., the actual parameter data size). |
| 93 | + * |
| 94 | + * @param callData The call data to process |
| 95 | + * @param selectorCounts The map to update with selector counts |
| 96 | + */ |
| 97 | + private static void processCallData(final Bytes callData, final Map<String, Integer> selectorCounts) { |
| 98 | + if (callData == null || callData.size() < 4) { |
| 99 | + // Not enough data for a function selector |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + // Extract the first 4 bytes as the function selector |
| 104 | + final Bytes selector = callData.slice(0, 4); |
| 105 | + final String selectorHex = selector.toHexString(); |
| 106 | + |
| 107 | + // Use len(input)-4 (parameter data size, excluding selector) |
| 108 | + final int parameterDataSize = callData.size() - 4; |
| 109 | + |
| 110 | + // Create the key in the format "selector-parameterdatasize" |
| 111 | + final String key = selectorHex + "-" + parameterDataSize; |
| 112 | + |
| 113 | + // Update the count |
| 114 | + selectorCounts.put(key, selectorCounts.getOrDefault(key, 0) + 1); |
| 115 | + |
| 116 | + LOG.trace("4byte tracer: selector {} with parameter data size {} (key: {})", |
| 117 | + selectorHex, parameterDataSize, key); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Determines if a trace frame should be processed for 4byte analysis. |
| 122 | + * |
| 123 | + * Only process CALL, CALLCODE, DELEGATECALL, and STATICCALL operations, |
| 124 | + * excluding CREATE/CREATE2 and precompiled contracts. |
| 125 | + * |
| 126 | + * @param frame The trace frame to check |
| 127 | + * @return true if the frame should be processed, false otherwise |
| 128 | + */ |
| 129 | + private static boolean shouldProcessFrame(final TraceFrame frame) { |
| 130 | + final String opcode = frame.getOpcode(); |
| 131 | + |
| 132 | + // Only process specific call operations |
| 133 | + if (!isRelevantCallOperation(opcode)) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + // Skip precompiled contracts (portable across forks/chains) |
| 138 | + if (frame.isPrecompile()) { |
| 139 | + LOG.trace("Skipping precompiled contract call"); |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + // Ensure we have enough input data for a function selector |
| 144 | + final Bytes inputData = frame.getInputData(); |
| 145 | + return inputData != null && inputData.size() >= 4; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Checks if the given opcode represents a relevant call operation for 4byte tracing. |
| 150 | + * |
| 151 | + * Only CALL, CALLCODE, DELEGATECALL, and STATICCALL |
| 152 | + * are relevant. CREATE and CREATE2 are explicitly excluded. |
| 153 | + * |
| 154 | + * @param opcode The opcode to check |
| 155 | + * @return true if the opcode is a relevant call operation, false otherwise |
| 156 | + */ |
| 157 | + private static boolean isRelevantCallOperation(final String opcode) { |
| 158 | + return "CALL".equals(opcode) || |
| 159 | + "CALLCODE".equals(opcode) || |
| 160 | + "DELEGATECALL".equals(opcode) || |
| 161 | + "STATICCALL".equals(opcode); |
| 162 | + // Note: CREATE and CREATE2 are intentionally excluded |
| 163 | + } |
| 164 | +} |
| 165 | + |
0 commit comments