Skip to content

Feat-11 : Add limit param to stop opcode trace capture after N steps#10173

Merged
macfarla merged 11 commits into
besu-eth:mainfrom
sagarkhandagre998:issue-10115-11
May 5, 2026
Merged

Feat-11 : Add limit param to stop opcode trace capture after N steps#10173
macfarla merged 11 commits into
besu-eth:mainfrom
sagarkhandagre998:issue-10115-11

Conversation

@sagarkhandagre998
Copy link
Copy Markdown
Contributor

@sagarkhandagre998 sagarkhandagre998 commented Apr 3, 2026

PR description

Add limit param to stop opcode trace capture after N steps

Part of aligning Besu's debug RPC endpoints with the execution-apis spec.

What changed:

  • OpCodeTracerConfigBuilder — new limit(int) config option (0 = unlimited, default)
  • TransactionTraceParams — new optional limit JSON param wired into traceOptions()
  • DebugOperationTracer — dedicated stepCount field drives the limit check; backfill of gasRemainingPostExecution on the last captured frame is preserved even after the limit is hit
  • DebugOperationTracerTest — two new unit tests covering frame count cap and backfill correctness

Behaviour:

  • When limit is absent or 0 — no change to existing behaviour
  • When limit=N — exactly N StructLog entries are recorded; EVM execution continues to completion per spec

Two bugs fixed over the naive implementation:

  1. Used traceFrames.size() as step counter — replaced with a dedicated stepCount field that is explicit and reset correctly per transaction
  2. The last captured frame never got its gasRemainingPostExecution backfilled — moved the early-return guard in tracePostExecution to after the backfill block so it always runs

Fixed Issue(s)

Refs #10115

Thanks for sending a pull request! Have you done the following?

  • Checked out our contribution guidelines?
  • Considered documentation and added the doc-change-required label to this PR if updates are required.
  • Considered the changelog and included an update if required.
  • For database changes (e.g. KeyValueSegmentIdentifier) considered compatibility and performed forwards and backwards compatibility tests

Locally, you can run these tests to catch failures early:

  • spotless: ./gradlew spotlessApply
  • unit tests: ./gradlew build
  • acceptance tests: ./gradlew acceptanceTest
  • integration tests: ./gradlew integrationTest
  • reference tests: ./gradlew ethereum:referenceTests:referenceTests
  • hive tests: Engine or other RPCs modified?

@sagarkhandagre998
Copy link
Copy Markdown
Contributor Author

@macfarla Kindly take a look at once.

@parthdagia05
Copy link
Copy Markdown
Contributor

This PR doesn't seem to handle negative limit values if someone passes limit = -1, then stepCount >= limit would be true from step 0 , so no frames would be captured. Better to reject negative values .

Also, limit = 0 is treated as "unlimited" based on the options.limit() > 0 check, but there's no test confirming that behavior. Should add one.

@sagarkhandagre998

@sagarkhandagre998
Copy link
Copy Markdown
Contributor Author

This PR doesn't seem to handle negative limit values if someone passes limit = -1, then stepCount >= limit would be true from step 0 , so no frames would be captured. Better to reject negative values .

Also, limit = 0 is treated as "unlimited" based on the options.limit() > 0 check, but there's no test confirming that behavior. Should add one.

@sagarkhandagre998

@parthdagia05 Good Catch! will fix.

@sagarkhandagre998 sagarkhandagre998 force-pushed the issue-10115-11 branch 2 times, most recently from 83f9f4d to e8a899b Compare April 11, 2026 13:42
@macfarla
Copy link
Copy Markdown
Contributor

@sagarkhandagre998 taking a look, however there is a compile error. please ensure you have built locally and run relevant tests before requesting a review. This will massively speed up the feedback loop.

  • What went wrong:
    Execution failed for task ':ethereum:core:compileJava'.

Compilation failed; see the compiler output below.
/Users/sallymacfarlane/workspace/b2/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/vm/DebugOperationTracer.java:68: error: cannot find symbol
if (!(traceOpcode = traceOpcode(currentOperation))) {
^
symbol: method traceOpcode(Operation)
location: class DebugOperationTracer
1 error

@macfarla macfarla marked this pull request as draft April 21, 2026 08:55
@sagarkhandagre998
Copy link
Copy Markdown
Contributor Author

@sagarkhandagre998 taking a look, however there is a compile error. please ensure you have built locally and run relevant tests before requesting a review. This will massively speed up the feedback loop.

* What went wrong:
  Execution failed for task ':ethereum:core:compileJava'.

Compilation failed; see the compiler output below.
/Users/sallymacfarlane/workspace/b2/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/vm/DebugOperationTracer.java:68: error: cannot find symbol
if (!(traceOpcode = traceOpcode(currentOperation))) {
^
symbol: method traceOpcode(Operation)
location: class DebugOperationTracer
1 error

Really sorry for that @macfarla ,I had built that locally and tested but had resolved the conflict from github web and something wrong happend . I will fix it to the correct changes .

@sagarkhandagre998
Copy link
Copy Markdown
Contributor Author

@macfarla

traceOpcode() was a private method in DebugOperationTracer itself before upstream commit b2cbdeae01 (Stream debug_traceBlock responses directly to avoid OOM on large blocks). That commit introduced AbstractDebugOperationTracer and moved the logic there — renaming it to shouldTraceOpcode() (private to the base class) and having the base class own tracePreExecution.

thats why you got the compilation error , I will fix that .

Copy link
Copy Markdown
Contributor

@macfarla macfarla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couple of comments. I'm not sure about modifying the traceOpcode boolean, there may be a cleaner way

Signed-off-by: Sagar Khandagre <sagar.khandagre998@gmail.com>
…emainingPostExecution

Signed-off-by: Sagar Khandagre <sagar.khandagre998@gmail.com>
…ostExecution backfill

Signed-off-by: Sagar Khandagre <sagar.khandagre998@gmail.com>
Signed-off-by: Sagar Khandagre <sagar.khandagre998@gmail.com>
Verifies that limit(0) captures all EVM steps without capping,
per the spec contract that 0 means unlimited.

Without this test a regression flipping the guard from
 to  would pass all other limit
tests undetected.

Signed-off-by: Sagar Khandagre <sagar.khandagre998@gmail.com>
Signed-off-by: Sagar Khandagre <sagar.khandagre998@gmail.com>
Signed-off-by: Sagar Khandagre <sagar.khandagre998@gmail.com>
…s positive.

Signed-off-by: Sagar Khandagre <sagar.khandagre998@gmail.com>
Introduce a dedicated limitReached flag in DebugOperationTracer for step-limit handling.
Stop mutating traceOpcode for limit enforcement; keep it only for opcode-filter semantics.
Preserve existing behavior: no new frames after limit, while backfill path still runs correctly.
Improve readability and reduce surprise/maintenance risk called out in review.

Signed-off-by: Sagar Khandagre <sagar.khandagre998@gmail.com>
@sagarkhandagre998
Copy link
Copy Markdown
Contributor Author

@macfarla please take a look.

Copy link
Copy Markdown
Contributor

@macfarla macfarla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking pretty good, can you add a changelog entry and we can run final checks

@Override
protected void capturePreExecutionState(final MessageFrame frame) {
if (options.limit() > 0 && stepCount >= options.limit()) {
limitReached = true;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep this is better

Signed-off-by: Sagar Khandagre <sagar.khandagre998@gmail.com>
@sagarkhandagre998
Copy link
Copy Markdown
Contributor Author

@macfarla We can run the final checks .

@macfarla macfarla merged commit a95327a into besu-eth:main May 5, 2026
34 checks passed
abhay-dev2901 pushed a commit to abhay-dev2901/besu that referenced this pull request May 14, 2026
besu-eth#10173)

* feat(debug): add limit param to stop trace capture after N steps

Signed-off-by: Sagar Khandagre <sagar.khandagre998@gmail.com>

---------

Signed-off-by: Sagar Khandagre <sagar.khandagre998@gmail.com>
Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com>
Signed-off-by: abhay-dev2901 <abhaytp1998@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants