-
Notifications
You must be signed in to change notification settings - Fork 1k
Add support for 4byteTracer in debug_trace* methods #9642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
0df540b
Implement Geth's 4ByteTracer in Besu
usmansaleem 8d8b569
Merge remote-tracking branch 'upstream/main' into 4byte_tracer
usmansaleem 1b4a54e
Add CHANGELOG entry for 4byteTracer
usmansaleem 8deee68
handle initial transaction call in 4bytetracer converter
usmansaleem a0b1a18
4ByteTracer - Use LinkedHashMap to keep the CALL insertion order
usmansaleem c461ea7
4ByteTracer - Use TreeMap to sort keys to match Geth output
usmansaleem 8104688
Merge remote-tracking branch 'upstream/main' into 4byte_tracer
usmansaleem 479785a
Merge remote-tracking branch 'upstream/main' into 4byte_tracer
usmansaleem c262c79
Merge remote-tracking branch 'upstream/main' into 4byte_tracer
usmansaleem c7aaa9a
changelog
usmansaleem b40071a
Merge remote-tracking branch 'upstream/main' into 4byte_tracer
usmansaleem 0e486f8
Restore changelog entry
usmansaleem a7f2d59
Merge remote-tracking branch 'upstream/main' into 4byte_tracer
usmansaleem 46a322b
Merge remote-tracking branch 'upstream/main' into 4byte_tracer
usmansaleem 14ce61d
Merge remote-tracking branch 'upstream/main' into 4byte_tracer
usmansaleem 9fead65
Fix merge conflicts
usmansaleem f0f73f0
Merge remote-tracking branch 'upstream/main' into 4byte_tracer
usmansaleem b22d4c4
Merge remote-tracking branch 'upstream/main' into 4byte_tracer
usmansaleem cc2cdd1
Merge remote-tracking branch 'upstream/main' into 4byte_tracer
usmansaleem b8d81da
Use ProtocolSpec precompile registry in FourByteTracer
usmansaleem 8f69bf9
Merge remote-tracking branch 'upstream/main' into 4byte_tracer
usmansaleem 992c3ea
simplify skip scenario
usmansaleem 590ef1a
Merge remote-tracking branch 'upstream/main' into 4byte_tracer
usmansaleem 7c15651
Merge remote-tracking branch 'upstream/main' into 4byte_tracer
usmansaleem 7bbb749
spec tests for 4ByteTracer
usmansaleem 38963d6
Merge remote-tracking branch 'upstream/main' into 4byte_tracer
usmansaleem fa5929a
mark file final as it is a utility pattern class
usmansaleem 02791dc
refactor test as per review suggestions
usmansaleem 8407f4b
Merge remote-tracking branch 'upstream/main' into 4byte_tracer
usmansaleem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/FourByteTracerResult.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Copyright contributors to Besu. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.hyperledger.besu.ethereum.api.jsonrpc.internal.results; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Map; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonValue; | ||
|
|
||
| /** | ||
| * Represents the result format for Ethereum's 4byteTracer as specified in the Geth documentation. | ||
| * | ||
| * <p>The 4byteTracer collects function selectors (the first 4 bytes of call data) from all calls | ||
| * made during transaction execution, along with the size of the supplied call data. This is useful | ||
| * for analyzing which contract functions were invoked during a transaction. | ||
| * | ||
| * <p>The JSON output is a simple map where keys are in the format | ||
| * "0x[4-byte-selector]-[calldata-size]" and values are the number of times that combination was | ||
| * called. For example: | ||
| * | ||
| * <pre>{@code | ||
| * { | ||
| * "0x27dc297e-128": 1, | ||
| * "0x38cc4831-0": 2, | ||
| * "0x524f3889-96": 1 | ||
| * } | ||
| * }</pre> | ||
| * | ||
| * @see <a | ||
| * href="https://geth.ethereum.org/docs/developers/evm-tracing/built-in-tracers#4byte-tracer"> | ||
| * Geth 4byteTracer Documentation</a> | ||
| */ | ||
| public class FourByteTracerResult { | ||
|
|
||
| /** | ||
| * Map of function selector + call data size to occurrence count. Key format: | ||
| * "0x[4-byte-selector]-[calldata-size]" | ||
| */ | ||
| private final Map<String, Integer> selectorCounts; | ||
|
|
||
| /** | ||
| * Constructs a FourByteTracerResult with the given selector counts. | ||
| * | ||
| * @param selectorCounts map of selector-size keys to occurrence counts | ||
| */ | ||
| public FourByteTracerResult(final Map<String, Integer> selectorCounts) { | ||
| this.selectorCounts = selectorCounts != null ? selectorCounts : Collections.emptyMap(); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the map of function selectors and their occurrence counts. | ||
| * | ||
| * <p>The {@link JsonValue} annotation causes Jackson to serialize this object directly as the | ||
| * map, without wrapping it in an object. This matches Geth's output format. | ||
| * | ||
| * @return the map of selector-size keys to occurrence counts | ||
| */ | ||
| @JsonValue | ||
| public Map<String, Integer> getSelectorCounts() { | ||
| return selectorCounts; | ||
| } | ||
| } |
221 changes: 221 additions & 0 deletions
221
...hyperledger/besu/ethereum/api/jsonrpc/internal/results/FourByteTracerResultConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| /* | ||
| * Copyright contributors to Besu. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.hyperledger.besu.ethereum.api.jsonrpc.internal.results; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
|
|
||
| import org.hyperledger.besu.datatypes.Address; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.calltrace.OpcodeCategory; | ||
| import org.hyperledger.besu.ethereum.core.Transaction; | ||
| import org.hyperledger.besu.evm.tracing.TraceFrame; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.TreeMap; | ||
|
|
||
| import org.apache.tuweni.bytes.Bytes; | ||
|
|
||
| /** | ||
| * Converts Ethereum transaction traces into 4byte tracer format. | ||
| * | ||
| * <p>This class transforms transaction traces to collect function selectors (the first 4 bytes of | ||
| * call data) from all internal calls made during transaction execution, along with the size of the | ||
| * remaining call data (excluding the 4-byte selector). This matches Geth's 4byteTracer output | ||
| * format. | ||
| * | ||
| * <p>The converter processes only CALL-type operations that successfully enter: | ||
| * | ||
| * <ul> | ||
| * <li>CALL | ||
| * <li>CALLCODE | ||
| * <li>DELEGATECALL | ||
| * <li>STATICCALL | ||
| * </ul> | ||
| * | ||
| * <p>CREATE and CREATE2 operations are excluded, as are calls to precompiled contracts and calls | ||
| * that fail to enter. | ||
| * | ||
| * <p>For each qualifying call operation, it extracts the first 4 bytes of the input data (the | ||
| * function selector) and combines it with the size of the remaining call data (size - 4) to create | ||
| * a key in the format "0x[4-byte-selector]-[remaining-size]". The result is a map of these keys to | ||
| * their occurrence counts. | ||
| * | ||
| * @see <a href="https://github.com/ethereum/go-ethereum/blob/master/eth/tracers/native/4byte.go"> | ||
| * Geth 4byteTracer Implementation</a> | ||
| */ | ||
| public class FourByteTracerResultConverter { | ||
|
|
||
| private static final int FUNCTION_SELECTOR_LENGTH = 4; | ||
|
|
||
| private FourByteTracerResultConverter() { | ||
| // Utility class - prevent instantiation | ||
| } | ||
|
|
||
| /** | ||
| * Converts a transaction trace to a 4byte tracer result. | ||
| * | ||
| * @param transactionTrace The transaction trace to convert | ||
| * @return A 4byte tracer result containing function selector counts | ||
| * @throws NullPointerException if transactionTrace is null | ||
| */ | ||
| public static FourByteTracerResult convert(final TransactionTrace transactionTrace) { | ||
| checkNotNull( | ||
| transactionTrace, "FourByteTracerResultConverter requires a non-null TransactionTrace"); | ||
|
|
||
| // Sort keys alphabetically to match Geth's JSON encoding behavior | ||
| final Map<String, Integer> selectorCounts = new TreeMap<>(); | ||
|
|
||
| processInitialTransaction(transactionTrace, selectorCounts); | ||
|
|
||
| // Process all trace frames for internal calls only (not the initial transaction) | ||
| if (transactionTrace.getTraceFrames() != null) { | ||
| processTraceFrames(transactionTrace.getTraceFrames(), selectorCounts); | ||
| } | ||
|
|
||
| return new FourByteTracerResult(selectorCounts); | ||
| } | ||
|
|
||
| /** | ||
| * Processes the initial transaction to extract its function selector. | ||
| * | ||
| * <p>This matches Geth's behavior where OnEnter fires for the transaction's entry into the | ||
| * contract, not just for internal calls. | ||
| * | ||
| * @param transactionTrace the transaction trace | ||
| * @param selectorCounts the map to update with selector counts | ||
| */ | ||
| private static void processInitialTransaction( | ||
| final TransactionTrace transactionTrace, final Map<String, Integer> selectorCounts) { | ||
|
|
||
| final Transaction tx = transactionTrace.getTransaction(); | ||
|
|
||
| // Skip if this is a contract creation (no function selector) | ||
| if (tx.isContractCreation()) { | ||
| return; | ||
| } | ||
|
|
||
| // Skip if target is not present | ||
| final Optional<Address> to = tx.getTo(); | ||
| if (to.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| // Skip precompiled contracts (addresses 0x01-0x0a typically) | ||
| // Note: This logic should match how precompiles are detected elsewhere in Besu | ||
| // For now, simple check for addresses <= 0x0a | ||
| final Bytes toAddressBytes = to.get().getBytes(); | ||
| if (toAddressBytes.numberOfLeadingZeroBytes() >= 19 | ||
| && toAddressBytes.trimLeadingZeros().size() == 1 | ||
| && toAddressBytes.trimLeadingZeros().get(0) <= 0x0a) { | ||
usmansaleem marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| final Bytes inputData = tx.getPayload(); | ||
|
|
||
| // Only process if we have at least 4 bytes for the function selector | ||
| if (inputData != null && inputData.size() >= FUNCTION_SELECTOR_LENGTH) { | ||
| final String key = createKey(inputData); | ||
| selectorCounts.merge(key, 1, Integer::sum); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Processes all trace frames to extract function selectors from CALL operations. | ||
| * | ||
| * <p>This method only processes CALL, CALLCODE, DELEGATECALL, and STATICCALL operations that | ||
| * successfully enter a new execution scope. CREATE and CREATE2 operations are ignored to match | ||
| * Geth's behavior. | ||
| * | ||
| * @param frames the list of trace frames to process | ||
| * @param selectorCounts the map to update with selector counts | ||
| */ | ||
| private static void processTraceFrames( | ||
| final List<TraceFrame> frames, final Map<String, Integer> selectorCounts) { | ||
|
|
||
| for (int i = 0; i < frames.size(); i++) { | ||
| final TraceFrame frame = frames.get(i); | ||
| final String opcode = frame.getOpcode(); | ||
|
|
||
| // Only process CALL-type operations (not CREATE/CREATE2) | ||
| if (OpcodeCategory.isCallOp(opcode)) { | ||
| final TraceFrame nextTrace = (i < frames.size() - 1) ? frames.get(i + 1) : null; | ||
| processCall(frame, nextTrace, selectorCounts); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Processes a single CALL operation to extract its function selector. | ||
| * | ||
| * <p>This method: | ||
| * | ||
| * <ul> | ||
| * <li>Checks if the call actually entered (depth increased in next frame) | ||
| * <li>Skips calls to precompiled contracts | ||
| * <li>Extracts the input data from the entered call | ||
| * <li>Skips calls with less than 4 bytes of input (no function selector) | ||
| * <li>Creates a key in the format "0x[selector]-[size-4]" and increments its count | ||
| * </ul> | ||
| * | ||
| * <p>This matches Geth's OnEnter hook behavior which only fires when a call successfully enters a | ||
| * new execution scope. | ||
| * | ||
| * @param frame the current trace frame (the CALL instruction) | ||
| * @param nextTrace the next trace frame (first frame inside the call, may be null) | ||
| * @param selectorCounts the map to update with selector counts | ||
| */ | ||
| private static void processCall( | ||
| final TraceFrame frame, | ||
| final TraceFrame nextTrace, | ||
| final Map<String, Integer> selectorCounts) { | ||
|
|
||
| // Skip precompiled contracts | ||
| if (frame.isPrecompile()) { | ||
| return; | ||
| } | ||
|
|
||
| // Check if call entered (depth increased) - matches Geth's OnEnter behavior | ||
| final boolean calleeEntered = nextTrace != null && nextTrace.getDepth() > frame.getDepth(); | ||
| if (!calleeEntered) { | ||
| return; | ||
| } | ||
|
|
||
| // Get the input data from the entered call (nextTrace has the actual input data) | ||
| final Bytes inputData = nextTrace.getInputData(); | ||
|
|
||
| // Only process if we have at least 4 bytes for the function selector | ||
| if (inputData != null && inputData.size() >= FUNCTION_SELECTOR_LENGTH) { | ||
| final String key = createKey(inputData); | ||
| selectorCounts.merge(key, 1, Integer::sum); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates a key in the format "0x[4-byte-selector]-[size-4]" from the input data. | ||
| * | ||
| * <p>The size portion represents the length of the input data minus the 4-byte selector, matching | ||
| * Geth's implementation where the size is calculated as `len(input) - 4`. | ||
| * | ||
| * @param inputData the input data containing the function selector | ||
| * @return the formatted key (e.g., "0x27dc297e-128") | ||
| */ | ||
| private static String createKey(final Bytes inputData) { | ||
| final Bytes selector = inputData.slice(0, FUNCTION_SELECTOR_LENGTH); | ||
| final int remainingSize = inputData.size() - FUNCTION_SELECTOR_LENGTH; | ||
| return selector.toHexString() + "-" + remainingSize; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.