Skip to content

Commit

Permalink
debug_traceBlock use existing block header instead of hash (#8184)
Browse files Browse the repository at this point in the history
This means you don't need to have the block already in the chain when you call debug_traceBlock with a block's RLP (you just need the parent).

Signed-off-by: Simon Dudley <[email protected]>
  • Loading branch information
siladu authored Jan 29, 2025
1 parent b5d507d commit 4b8d935
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected Collection<DebugTraceTransactionResult> getTraces(
block ->
Tracer.processTracing(
getBlockchainQueries(),
block.getHash(),
Optional.of(block.getHeader()),
traceableState -> {
List<DebugTraceTransactionResult> tracesList =
Collections.synchronizedList(new ArrayList<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters;
Expand All @@ -33,6 +32,8 @@
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockDataGenerator;
import org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.metrics.ObservableMetricsSystem;
import org.hyperledger.besu.testutil.DeterministicEthScheduler;
Expand All @@ -59,10 +60,7 @@ public class DebugTraceBlockByHashTest {
@Mock private BlockchainQueries blockchainQueries;
@Mock private ObservableMetricsSystem metricsSystem;
@Mock private Blockchain blockchain;
@Mock private Block block;
private DebugTraceBlockByHash debugTraceBlockByHash;
private final Hash blockHash =
Hash.fromHexString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");

@BeforeEach
public void setUp() {
Expand All @@ -79,13 +77,18 @@ public void nameShouldBeDebugTraceBlockByHash() {
@SuppressWarnings("unchecked")
@Test
public void shouldReturnCorrectResponse() {
final Object[] params = new Object[] {blockHash};
final Block block =
new BlockDataGenerator()
.block(
BlockDataGenerator.BlockOptions.create()
.setBlockHeaderFunctions(new MainnetBlockHeaderFunctions()));

final Object[] params = new Object[] {block.getHash()};
final JsonRpcRequestContext request =
new JsonRpcRequestContext(new JsonRpcRequest("2.0", "debug_traceBlockByHash", params));

when(blockchainQueries.getBlockchain()).thenReturn(blockchain);
when(blockchain.getBlockByHash(blockHash)).thenReturn(Optional.of(block));
when(block.getHash()).thenReturn(blockHash);
when(blockchain.getBlockByHash(block.getHash())).thenReturn(Optional.of(block));

DebugTraceTransactionResult result1 = mock(DebugTraceTransactionResult.class);
DebugTraceTransactionResult result2 = mock(DebugTraceTransactionResult.class);
Expand All @@ -96,7 +99,10 @@ public void shouldReturnCorrectResponse() {
mockedTracer
.when(
() ->
Tracer.processTracing(eq(blockchainQueries), eq(blockHash), any(Function.class)))
Tracer.processTracing(
eq(blockchainQueries),
eq(Optional.of(block.getHeader())),
any(Function.class)))
.thenReturn(Optional.of(resultList));

final JsonRpcResponse jsonRpcResponse = debugTraceBlockByHash.response(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ public void shouldReturnCorrectResponse() {
.when(
() ->
Tracer.processTracing(
eq(blockchainQueries), eq(block.getHash()), any(Function.class)))
eq(blockchainQueries),
eq(Optional.of(block.getHeader())),
any(Function.class)))
.thenReturn(Optional.of(resultList));

final JsonRpcResponse jsonRpcResponse = debugTraceBlock.response(request);
Expand Down

0 comments on commit 4b8d935

Please sign in to comment.