-
Notifications
You must be signed in to change notification settings - Fork 689
feat(rpc): add debug_intermediateRoots #11524
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
base: master
Are you sure you want to change the base?
Changes from all commits
206f0ed
fc08566
a160e9f
5d39af7
4b194fb
598cf9b
8258b4f
b3290a8
74b08eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // SPDX-FileCopyrightText: 2026 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| using Nethermind.Core; | ||
| using Nethermind.Core.Crypto; | ||
| using Nethermind.Core.Specs; | ||
| using Nethermind.Evm.State; | ||
| using Nethermind.Evm.Tracing; | ||
| using Nethermind.Evm.TransactionProcessing; | ||
|
|
||
| namespace Nethermind.Blockchain.Tracing; | ||
|
|
||
| /// <summary> | ||
| /// Captures the world state root after each transaction in a block. | ||
| /// Mirrors geth's <c>debug_intermediateRoots</c> behaviour. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Roots are produced per transaction in execution order; a failed transaction still | ||
| /// produces its post-execution root (matching geth's partial-result semantics). | ||
| /// System-level calls (EIP-4788, EIP-2935) and withdrawals do not produce entries | ||
| /// because <see cref="BlockTracerBase{TTrace,TTracer}"/> only dispatches on user transactions. | ||
| /// </remarks> | ||
| public class IntermediateRootsBlockTracer(IWorldState worldState, IReleaseSpec spec) | ||
| : BlockTracerBase<Hash256, IntermediateRootsBlockTracer.IntermediateRootsTxTracer> | ||
| { | ||
| protected override IntermediateRootsTxTracer OnStart(Transaction? tx) => new(worldState, spec); | ||
|
|
||
| protected override Hash256 OnEnd(IntermediateRootsTxTracer txTracer) => txTracer.StateRoot; | ||
|
|
||
| public class IntermediateRootsTxTracer(IWorldState worldState, IReleaseSpec spec) : TxTracer | ||
| { | ||
| public override bool IsTracingReceipt => true; | ||
|
|
||
| public Hash256 StateRoot { get; private set; } | ||
|
|
||
| public override void MarkAsSuccess(Address recipient, in GasConsumed gasSpent, byte[] output, LogEntry[] logs, Hash256? stateRoot = null) => | ||
| Capture(stateRoot); | ||
|
|
||
| public override void MarkAsFailed(Address recipient, in GasConsumed gasSpent, byte[] output, string? error, Hash256? stateRoot = null) => | ||
| Capture(stateRoot); | ||
|
Comment on lines
+36
to
+40
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Medium — Partial-result semantics unspecified and untested If a transaction fails mid-block (e.g. an OOG reverted tx),
Geth returns
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Documented the behaviour in the tracer's XML |
||
|
|
||
| private void Capture(Hash256? reportedStateRoot) | ||
| { | ||
| if (reportedStateRoot is not null) | ||
| { | ||
| StateRoot = reportedStateRoot; | ||
| return; | ||
| } | ||
|
|
||
| // Post-Byzantium (EIP-658): TransactionProcessor commits with commitRoots=false, | ||
| // which buffers changes into _blockChanges without flushing them to the trie. | ||
| // Force a flush so the recomputed root reflects this transaction's effect — the | ||
| // BlockProcessor's later end-of-block commit is idempotent for already-flushed entries. | ||
| worldState.Commit(spec, commitRoots: true); | ||
| worldState.RecalculateStateRoot(); | ||
| StateRoot = worldState.StateRoot; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |||||||||
| using Nethermind.Evm.State; | ||||||||||
| using Nethermind.State.OverridableEnv; | ||||||||||
| using Nethermind.Evm.Tracing; | ||||||||||
| using Nethermind.Blockchain.Tracing; | ||||||||||
| using Nethermind.Blockchain.Tracing.GethStyle; | ||||||||||
| using Nethermind.Blockchain.Tracing.GethStyle.Custom.JavaScript; | ||||||||||
| using Nethermind.Blockchain.Tracing.GethStyle.Custom.Native; | ||||||||||
|
|
@@ -123,6 +124,34 @@ public IReadOnlyCollection<GethLikeTxTrace> TraceBlock(BlockParameter blockParam | |||||||||
|
|
||||||||||
| public IReadOnlyCollection<GethLikeTxTrace> TraceBlock(Block block, GethTraceOptions options, CancellationToken cancellationToken) => TraceBlockImpl(block, options, cancellationToken); | ||||||||||
|
|
||||||||||
| public IReadOnlyCollection<Hash256> TraceBlockIntermediateRoots(Hash256 blockHash, GethTraceOptions options, CancellationToken cancellationToken) | ||||||||||
| { | ||||||||||
| ArgumentNullException.ThrowIfNull(blockHash); | ||||||||||
| ArgumentNullException.ThrowIfNull(options); | ||||||||||
|
|
||||||||||
| // Mirror geth: canonical blocks first, fall back to the bad-block store so the diagnostic | ||||||||||
| // use case (replaying a rejected block to find divergence) works. | ||||||||||
| Block block = blockTree.FindBlock(blockHash) | ||||||||||
| ?? badBlockStore.GetAll().FirstOrDefault(b => b.Hash == blockHash) | ||||||||||
| ?? throw new InvalidOperationException($"Cannot find block {blockHash}"); | ||||||||||
| if (block.IsGenesis) throw new InvalidOperationException("genesis is not traceable"); | ||||||||||
|
|
||||||||||
| BlockHeader? parent = FindParent(block); | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. High — Genesis NRE / wrong behaviour
Add a guard before the
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch. Added an explicit |
||||||||||
|
|
||||||||||
| using Scope<BlockProcessingComponents> scope = blockProcessingEnv.BuildAndOverride(parent, options.StateOverrides); | ||||||||||
| IntermediateRootsBlockTracer tracer = new(scope.Component.WorldState, specProvider.GetSpec(block.Header)); | ||||||||||
| try | ||||||||||
| { | ||||||||||
| scope.Component.BlockchainProcessor.Process(block, ProcessingOptions.Trace, tracer.WithCancellation(cancellationToken), cancellationToken); | ||||||||||
| return tracer.BuildResult(); | ||||||||||
| } | ||||||||||
| catch | ||||||||||
| { | ||||||||||
| tracer.TryDispose(); | ||||||||||
| throw; | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public IEnumerable<string> TraceBlockToFile(Hash256 blockHash, GethTraceOptions options, CancellationToken cancellationToken) | ||||||||||
| { | ||||||||||
| ArgumentNullException.ThrowIfNull(blockHash); | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,15 @@ public interface IGethStyleTracer | |
| IReadOnlyCollection<GethLikeTxTrace> TraceBlock(BlockParameter blockParameter, GethTraceOptions options, CancellationToken cancellationToken); | ||
| IReadOnlyCollection<GethLikeTxTrace> TraceBlock(Rlp blockRlp, GethTraceOptions options, CancellationToken cancellationToken); | ||
| IReadOnlyCollection<GethLikeTxTrace> TraceBlock(Block block, GethTraceOptions options, CancellationToken cancellationToken); | ||
| /// <summary> | ||
| /// Replays <paramref name="blockHash"/> and returns the world-state root after each transaction | ||
| /// in execution order. EIP-4788/EIP-2935 system calls and withdrawals do not produce an entry. | ||
| /// </summary> | ||
| /// <param name="blockHash">Hash of a canonical or bad-stored block to replay.</param> | ||
| /// <param name="options">Trace options (state overrides are applied to the parent base state).</param> | ||
| /// <param name="cancellationToken">Cooperative cancellation.</param> | ||
| /// <returns>Post-tx state roots in execution order; failed transactions still produce a root.</returns> | ||
| IReadOnlyCollection<Hash256> TraceBlockIntermediateRoots(Hash256 blockHash, GethTraceOptions options, CancellationToken cancellationToken); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Low — missing XML doc Per the project's coding style, all public API members should have /// <summary>Replays <paramref name="blockHash"/> and returns the world-state root after each transaction, in execution order.</summary>
IReadOnlyCollection<Hash256> TraceBlockIntermediateRoots(Hash256 blockHash, GethTraceOptions options, CancellationToken cancellationToken);
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added on the interface in |
||
| IEnumerable<string> TraceBlockToFile(Hash256 blockHash, GethTraceOptions options, CancellationToken cancellationToken); | ||
| IEnumerable<string> TraceBadBlockToFile(Hash256 blockHash, GethTraceOptions options, CancellationToken cancellationToken); | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -419,4 +419,53 @@ public void StandardTraceBlockToFile_returns_error_when_state_unavailable(bool i | |||||||||||||||||||
| actual.ErrorCode.Should().Be(ErrorCodes.ResourceUnavailable); | ||||||||||||||||||||
| actual.Result.Error.Should().Contain("No state available"); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| [Test] | ||||||||||||||||||||
| public void Debug_intermediateRoots_returns_post_tx_roots_from_bridge() | ||||||||||||||||||||
| { | ||||||||||||||||||||
| Hash256 blockHash = TestItem.KeccakA; | ||||||||||||||||||||
| BlockHeader header = Build.A.BlockHeader.WithNumber(1).TestObject; | ||||||||||||||||||||
| _blockFinder.FindHeader(blockHash).Returns(header); | ||||||||||||||||||||
| _blockchainBridge.HasStateForBlock(Arg.Is(header)).Returns(true); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Hash256[] expected = [TestItem.KeccakB, TestItem.KeccakC]; | ||||||||||||||||||||
| _debugBridge | ||||||||||||||||||||
| .GetBlockIntermediateRoots(Arg.Is(blockHash), Arg.Any<CancellationToken>(), Arg.Any<GethTraceOptions?>()) | ||||||||||||||||||||
| .Returns(expected); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| DebugRpcModule rpcModule = CreateDebugRpcModule(_debugBridge); | ||||||||||||||||||||
| ResultWrapper<IReadOnlyCollection<Hash256>> actual = rpcModule.debug_intermediateRoots(blockHash); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| actual.Result.ResultType.Should().Be(ResultType.Success); | ||||||||||||||||||||
| actual.Data.Should().BeEquivalentTo(expected); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| [Test] | ||||||||||||||||||||
| public void Debug_intermediateRoots_fails_when_state_unavailable() | ||||||||||||||||||||
| { | ||||||||||||||||||||
| Hash256 blockHash = TestItem.KeccakA; | ||||||||||||||||||||
| BlockHeader header = Build.A.BlockHeader.WithNumber(1).TestObject; | ||||||||||||||||||||
| _blockFinder.FindHeader(blockHash).Returns(header); | ||||||||||||||||||||
| _blockchainBridge.HasStateForBlock(Arg.Is(header)).Returns(false); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| DebugRpcModule rpcModule = CreateDebugRpcModule(_debugBridge); | ||||||||||||||||||||
| ResultWrapper<IReadOnlyCollection<Hash256>> actual = rpcModule.debug_intermediateRoots(blockHash); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| actual.Result.ResultType.Should().Be(ResultType.Failure); | ||||||||||||||||||||
| actual.ErrorCode.Should().Be(ErrorCodes.ResourceUnavailable); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Medium — missing integration test + Low — missing "block not found" RPC test Two gaps in coverage:
[Test]
public void Debug_intermediateRoots_fails_when_block_not_found()
{
Hash256 blockHash = TestItem.KeccakA;
_blockFinder.FindHeader(blockHash).ReturnsNull();
DebugRpcModule rpcModule = CreateDebugRpcModule(_debugBridge);
ResultWrapper<IReadOnlyCollection<Hash256>> actual = rpcModule.debug_intermediateRoots(blockHash);
actual.Result.ResultType.Should().Be(ResultType.Failure);
actual.ErrorCode.Should().Be(ErrorCodes.ResourceNotFound);
}
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Block-not-found unit test landed in
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Medium — Critical test scenarios missing Both current tests mock
At minimum, add a
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Integration test in |
||||||||||||||||||||
|
|
||||||||||||||||||||
| [Test] | ||||||||||||||||||||
| public void Debug_intermediateRoots_fails_when_block_not_found() | ||||||||||||||||||||
| { | ||||||||||||||||||||
| Hash256 blockHash = TestItem.KeccakA; | ||||||||||||||||||||
| _blockFinder.FindHeader(blockHash).ReturnsNull(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| DebugRpcModule rpcModule = CreateDebugRpcModule(_debugBridge); | ||||||||||||||||||||
| ResultWrapper<IReadOnlyCollection<Hash256>> actual = rpcModule.debug_intermediateRoots(blockHash); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| actual.Result.ResultType.Should().Be(ResultType.Failure); | ||||||||||||||||||||
| actual.ErrorCode.Should().Be(ErrorCodes.ResourceNotFound); | ||||||||||||||||||||
| actual.Result.Error.Should().Contain("Cannot find header"); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // SPDX-FileCopyrightText: 2026 Demerzel Solutions Limited | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
| using Nethermind.Core; | ||
| using Nethermind.Core.Crypto; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace Nethermind.JsonRpc.Test.Modules; | ||
|
|
||
| public partial class DebugRpcModuleTests | ||
| { | ||
| [Test] | ||
| public async Task Debug_intermediateRoots_returns_one_root_per_transaction() | ||
| { | ||
| using Context context = await Context.Create(); | ||
| await context.Blockchain.AddBlock(CreateTraceBlockTransactions(context.Blockchain)); | ||
|
|
||
| Hash256 blockHash = context.Blockchain.BlockTree.Head!.Hash!; | ||
| ResultWrapper<IReadOnlyCollection<Hash256>> result = context.DebugRpcModule.debug_intermediateRoots(blockHash); | ||
|
|
||
| result.Result.ResultType.Should().Be(ResultType.Success); | ||
| result.Data.Should().HaveCount(2, "the block has exactly two user transactions"); | ||
| result.Data.Should().OnlyHaveUniqueItems("each tx mutates state, producing a distinct post-tx root"); | ||
| result.Data.Should().NotContain(Keccak.Zero); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Debug_intermediateRoots_rejects_genesis() | ||
| { | ||
| using Context context = await Context.Create(); | ||
| Hash256 genesisHash = context.Blockchain.BlockTree.Genesis!.Hash!; | ||
|
|
||
| Func<ResultWrapper<IReadOnlyCollection<Hash256>>> act = | ||
| () => context.DebugRpcModule.debug_intermediateRoots(genesisHash); | ||
|
|
||
| act.Should().Throw<InvalidOperationException>().WithMessage("*genesis*"); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Low — Unused
usingNethermind.Evm.TransactionProcessingis not referenced anywhere in this file. Remove it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left this one in —
GasConsumedlives inNethermind.Evm.TransactionProcessingand it's the type in theMarkAsSuccess/MarkAsFailedsignatures. Tried removing it and the build went red.