Skip to content

Commit 98cca9b

Browse files
feat: implement diff mode tracer (#9442)
* Implement pre state diff mode tracer Signed-off-by: Gabriel-Trintinalia <[email protected]> * add tests Signed-off-by: Gabriel-Trintinalia <[email protected]> * add tests Signed-off-by: Gabriel-Trintinalia <[email protected]> * remove unused methods Signed-off-by: Gabriel-Trintinalia <[email protected]> * Accept PR suggestions Signed-off-by: Gabriel-Trintinalia <[email protected]> --------- Signed-off-by: Gabriel-Trintinalia <[email protected]>
1 parent 7ac38c6 commit 98cca9b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+4031
-159
lines changed

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/AbstractDebugTraceBlock.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ protected Collection<DebugTraceTransactionResult> getTraces(
140140
.thenProcess("executeTransaction", executeTransactionStep)
141141
.thenProcessAsyncOrdered(
142142
"debugTraceTransactionStep",
143-
DebugTraceTransactionStepFactory.createAsync(
144-
traceOptions.tracerType()),
143+
DebugTraceTransactionStepFactory.createAsync(traceOptions),
145144
4)
146145
.andFinishWith("collect_results", tracesList::add);
147146

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/AbstractTraceByBlock.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace;
2525
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
2626
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TraceCallResult;
27-
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.diff.StateDiffGenerator;
28-
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.diff.StateDiffTrace;
27+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.diff.StateTraceGenerator;
2928
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.flat.FlatTrace;
3029
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.flat.FlatTraceGenerator;
3130
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.vm.VmTrace;
@@ -99,9 +98,9 @@ protected JsonNode getTraceCallResult(
9998
() -> builder.output(simulatorResult.getOutput().toString()));
10099

101100
if (traceTypes.contains(TraceType.STATE_DIFF)) {
102-
new StateDiffGenerator()
101+
new StateTraceGenerator()
103102
.generateStateDiff(transactionTrace)
104-
.forEachOrdered(stateDiff -> builder.stateDiff((StateDiffTrace) stateDiff));
103+
.forEachOrdered(builder::stateDiff);
105104
}
106105

107106
if (traceTypes.contains(TraceType.TRACE)) {

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugTraceBlockByNumber.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ protected Object resultByBlockNumber(
147147
.thenProcess("executeTransaction", executeTransactionStep)
148148
.thenProcessAsyncOrdered(
149149
"debugTraceTransactionStep",
150-
DebugTraceTransactionStepFactory.createAsync(
151-
traceOptions.tracerType()),
150+
DebugTraceTransactionStepFactory.createAsync(traceOptions),
152151
4)
153152
.andFinishWith("collect_results", tracesList::add);
154153

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugTraceCall.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ protected PreCloseStateHandler<Object> getSimulatorResultHandler(
106106
final TransactionTrace transactionTrace =
107107
new TransactionTrace(
108108
result.transaction(), result.result(), tracer.getTraceFrames());
109-
return DebugTraceTransactionStepFactory.create(
110-
getTraceOptions(requestContext).tracerType())
109+
return DebugTraceTransactionStepFactory.create(getTraceOptions(requestContext))
111110
.apply(transactionTrace)
112111
.getResult();
113112
});

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugTraceTransaction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private DebugTraceTransactionResult debugTraceTransactionResult(
105105
mutableWorldState ->
106106
transactionTracer
107107
.traceTransaction(mutableWorldState, blockHash, hash, execTracer)
108-
.map(DebugTraceTransactionStepFactory.create(traceOptions.tracerType())))
108+
.map(DebugTraceTransactionStepFactory.create(traceOptions)))
109109
.orElse(null);
110110
}
111111
}

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugTraceTransactionStepFactory.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.CallTracerResultConverter;
1919
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugTraceTransactionResult;
2020
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.OpCodeLoggerTracerResult;
21+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.diff.StateDiffTrace;
22+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.diff.StateTraceGenerator;
23+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.diff.StateTraceResult;
24+
import org.hyperledger.besu.ethereum.debug.TraceOptions;
2125
import org.hyperledger.besu.ethereum.debug.TracerType;
2226

2327
import java.util.concurrent.CompletableFuture;
@@ -42,12 +46,13 @@ public class DebugTraceTransactionStepFactory {
4246
* DebugTraceTransactionResult} with the appropriate tracer result based on the specified tracer
4347
* type.
4448
*
45-
* @param tracerType the type of tracer to use for processing the transaction trace
49+
* @param traceOptions the trace options containing the tracer type and configuration
4650
* @return a function that processes a {@link TransactionTrace} and returns a {@link
4751
* DebugTraceTransactionResult} with the appropriate tracer result
4852
*/
4953
public static Function<TransactionTrace, DebugTraceTransactionResult> create(
50-
final TracerType tracerType) {
54+
final TraceOptions traceOptions) {
55+
TracerType tracerType = traceOptions.tracerType();
5156
return switch (tracerType) {
5257
case OPCODE_TRACER ->
5358
transactionTrace -> {
@@ -72,9 +77,17 @@ public static Function<TransactionTrace, DebugTraceTransactionResult> create(
7277
};
7378
case PRESTATE_TRACER ->
7479
transactionTrace -> {
75-
// TODO: Implement prestateTracer logic and wire it here
76-
var result = new UnimplementedTracerResult();
77-
return new DebugTraceTransactionResult(transactionTrace, result);
80+
final var generator = new StateTraceGenerator();
81+
final boolean diffMode =
82+
Boolean.TRUE.equals(traceOptions.tracerConfig().getOrDefault("diffMode", false));
83+
final StateDiffTrace trace =
84+
(diffMode
85+
? generator.generateStateDiff(transactionTrace)
86+
: generator.generatePreState(transactionTrace))
87+
.findFirst()
88+
.orElseGet(StateDiffTrace::new);
89+
return new DebugTraceTransactionResult(
90+
transactionTrace, new StateTraceResult(trace, diffMode));
7891
};
7992
};
8093
}
@@ -84,14 +97,14 @@ public static Function<TransactionTrace, DebugTraceTransactionResult> create(
8497
* DebugTraceTransactionResult} with the appropriate tracer result based on the specified tracer
8598
* type.
8699
*
87-
* @param tracerType the type of tracer to use for processing the transaction trace
100+
* @param traceOptions the options of tracer to use for processing the transaction trace
88101
* @return an asynchronous function that processes a {@link TransactionTrace} and returns a {@link
89102
* DebugTraceTransactionResult} with the appropriate tracer result
90103
*/
91104
public static Function<TransactionTrace, CompletableFuture<DebugTraceTransactionResult>>
92-
createAsync(final TracerType tracerType) {
105+
createAsync(final TraceOptions traceOptions) {
93106
return transactionTrace ->
94-
CompletableFuture.supplyAsync(() -> create(tracerType).apply(transactionTrace));
107+
CompletableFuture.supplyAsync(() -> create(traceOptions).apply(transactionTrace));
95108
}
96109

97110
public static class UnimplementedTracerResult {

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/TraceReplayTransactionStep.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TraceTypeParameter.TraceType;
1818
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace;
1919
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TraceReplayResult;
20-
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.diff.StateDiffGenerator;
21-
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.diff.StateDiffTrace;
20+
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.diff.StateTraceGenerator;
2221
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.flat.FlatTrace;
2322
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.flat.FlatTraceGenerator;
2423
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.vm.VmTrace;
@@ -57,9 +56,9 @@ public CompletableFuture<TraceReplayResult> apply(final TransactionTrace transac
5756
builder.transactionHash(transactionTrace.getTransaction().getHash().toHexString());
5857

5958
if (traceTypes.contains(TraceType.STATE_DIFF)) {
60-
new StateDiffGenerator()
59+
new StateTraceGenerator()
6160
.generateStateDiff(transactionTrace)
62-
.forEachOrdered(stateDiff -> builder.stateDiff((StateDiffTrace) stateDiff));
61+
.forEachOrdered(builder::stateDiff);
6362
}
6463

6564
if (traceTypes.contains(TraceType.TRACE)) {

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/tracing/diff/AccountDiff.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,25 @@
1616

1717
import java.util.Map;
1818

19+
import com.fasterxml.jackson.annotation.JsonIgnore;
20+
1921
public final class AccountDiff {
2022

2123
private final DiffNode balance;
2224
private final DiffNode code;
2325
private final DiffNode nonce;
26+
@JsonIgnore private final DiffNode codeHash;
2427
private final Map<String, DiffNode> storage;
2528

2629
AccountDiff(
2730
final DiffNode balance,
2831
final DiffNode code,
32+
final DiffNode codeHash,
2933
final DiffNode nonce,
3034
final Map<String, DiffNode> storage) {
3135
this.balance = balance;
3236
this.code = code;
37+
this.codeHash = codeHash;
3338
this.nonce = nonce;
3439
this.storage = storage;
3540
}
@@ -42,6 +47,11 @@ public DiffNode getCode() {
4247
return code;
4348
}
4449

50+
@JsonIgnore
51+
public DiffNode getCodeHash() {
52+
return codeHash;
53+
}
54+
4555
public DiffNode getNonce() {
4656
return nonce;
4757
}
@@ -53,6 +63,7 @@ public Map<String, DiffNode> getStorage() {
5363
boolean hasDifference() {
5464
return balance.hasDifference()
5565
|| code.hasDifference()
66+
|| codeHash.hasDifference()
5667
|| nonce.hasDifference()
5768
|| !storage.isEmpty();
5869
}

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/tracing/diff/DiffNode.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ public final class DiffNode {
3838
this.to = to;
3939
}
4040

41-
boolean hasDifference() {
41+
public Optional<String> getFrom() {
42+
return from;
43+
}
44+
45+
public Optional<String> getTo() {
46+
return to;
47+
}
48+
49+
public boolean hasDifference() {
4250
return from.map(it -> !it.equals(to.get())).orElse(to.isPresent());
4351
}
4452

ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/tracing/diff/StateDiffGenerator.java

Lines changed: 0 additions & 125 deletions
This file was deleted.

0 commit comments

Comments
 (0)