Skip to content
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

trace_rawTransaction #3459

Merged
merged 31 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
dc14214
test files from parity
macfarla Feb 18, 2022
178b024
some WIP code for trace_rawTransaction
macfarla Feb 18, 2022
f81868a
Merge branch 'main' of github.com:hyperledger/besu into trace-raw-tra…
macfarla Feb 20, 2022
ae62142
added test dirs to specs; handle empty optionals
macfarla Feb 21, 2022
20f0949
handle null
macfarla Feb 21, 2022
4b66fa1
headers
macfarla Feb 21, 2022
9000863
Merge branch 'main' of github.com:hyperledger/besu into trace-raw-tra…
macfarla Feb 21, 2022
8cebba7
cleanup
macfarla Feb 21, 2022
692aa82
payload
macfarla Feb 21, 2022
9293b36
comment
macfarla Feb 21, 2022
5ab6ba6
error response
macfarla Feb 21, 2022
d326deb
Merge branch 'main' of github.com:hyperledger/besu into trace-raw-tra…
macfarla Feb 22, 2022
159c311
Merge branch 'main' of github.com:hyperledger/besu into trace-raw-tra…
macfarla Feb 24, 2022
4355d5e
PR review
macfarla Feb 24, 2022
7b8dbd5
changelog
macfarla Feb 24, 2022
a0bf437
merge main changes
frankisawesome Mar 2, 2022
1ee657f
merge main
macfarla Mar 4, 2022
9b46f20
Merge branch 'main' of github.com:hyperledger/besu into trace-raw-tra…
macfarla Mar 4, 2022
a3ddbad
Merge branch 'trace-raw-transaction' of github.com:macfarla/besu into…
macfarla Mar 4, 2022
5857359
comment
macfarla Mar 4, 2022
9897f8f
code smells
macfarla Mar 4, 2022
df642e8
merge
macfarla Mar 4, 2022
c56c5d8
code smells
macfarla Mar 4, 2022
cc7a10b
changelog
macfarla Mar 4, 2022
341c18d
Merge branch 'main' into trace-raw-transaction
MadelineMurray Mar 7, 2022
7d227ab
Merge branch 'main' of github.com:hyperledger/besu into trace-raw-tra…
macfarla Mar 7, 2022
4bb3920
cache the block number to make sure we are using the same block
macfarla Mar 7, 2022
f19c781
Merge branch 'trace-raw-transaction' of github.com:macfarla/besu into…
macfarla Mar 7, 2022
eb45609
check optional
macfarla Mar 7, 2022
dd5877f
Merge branch 'main' of github.com:hyperledger/besu into trace-raw-tra…
macfarla Mar 7, 2022
4f58529
Merge branch 'main' into trace-raw-transaction
macfarla Mar 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added test dirs to specs; handle empty optionals
Signed-off-by: Sally MacFarlane <sally.macfarlane@consensys.net>
  • Loading branch information
macfarla committed Feb 21, 2022
commit ae6214244624b90b99102337e754cd778b2530cb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods;

import java.util.Optional;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonCallParameter;
import org.hyperledger.besu.plugin.data.Transaction;

public class JsonCallParameterUtil {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods;

import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TraceTypeParameter.TraceType.VM_TRACE;
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.BLOCK_NOT_FOUND;
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.INTERNAL_ERROR;

Expand All @@ -24,15 +23,8 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TraceTypeParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TraceCallResult;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.diff.StateDiffGenerator;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.diff.StateDiffTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.flat.FlatTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.flat.FlatTraceGenerator;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.flat.MixInIgnoreRevertReason;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.vm.VmTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.vm.VmTraceGenerator;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.api.util.TraceUtils;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.debug.TraceOptions;
Expand All @@ -45,16 +37,11 @@

import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import com.fasterxml.jackson.databind.ObjectMapper;

public class TraceCall extends AbstractBlockParameterMethod implements JsonRpcMethod {
private final ProtocolSchedule protocolSchedule;
private final TransactionSimulator transactionSimulator;

private static final ObjectMapper MAPPER_IGNORE_REVERT_REASON = new ObjectMapper();

public TraceCall(
final BlockchainQueries blockchainQueries,
final ProtocolSchedule protocolSchedule,
Expand All @@ -63,9 +50,6 @@ public TraceCall(

this.protocolSchedule = protocolSchedule;
this.transactionSimulator = transactionSimulator;

// The trace_call specification does not output the revert reason, so we have to remove it
MAPPER_IGNORE_REVERT_REASON.addMixIn(FlatTrace.class, MixInIgnoreRevertReason.class);
}

@Override
Expand Down Expand Up @@ -117,36 +101,11 @@ protected Object resultByBlockNumber(
maybeSimulatorResult.get().getResult(),
tracer.getTraceFrames());

// TODO why do we get the head of the chain here rather than use the block param
final Block block = blockchainQueries.get().getBlockchain().getChainHeadBlock();

final TraceCallResult.Builder builder = TraceCallResult.builder();

transactionTrace
.getResult()
.getRevertReason()
.ifPresentOrElse(
revertReason -> builder.output(revertReason.toHexString()),
() -> builder.output(maybeSimulatorResult.get().getOutput().toString()));

if (traceTypes.contains(TraceTypeParameter.TraceType.STATE_DIFF)) {
new StateDiffGenerator()
.generateStateDiff(transactionTrace)
.forEachOrdered(stateDiff -> builder.stateDiff((StateDiffTrace) stateDiff));
}

if (traceTypes.contains(TraceTypeParameter.TraceType.TRACE)) {
FlatTraceGenerator.generateFromTransactionTrace(
protocolSchedule, transactionTrace, block, new AtomicInteger(), false)
.forEachOrdered(trace -> builder.addTrace((FlatTrace) trace));
}

if (traceTypes.contains(VM_TRACE)) {
new VmTraceGenerator(transactionTrace)
.generateTraceStream()
.forEachOrdered(vmTrace -> builder.vmTrace((VmTrace) vmTrace));
}

return MAPPER_IGNORE_REVERT_REASON.valueToTree(builder.build());
return TraceUtils.getTraceCallResult(
protocolSchedule, traceTypes, maybeSimulatorResult, transactionTrace, block);
}

private TransactionValidationParams buildTransactionValidationParams() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TraceTypeParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.api.util.DomainObjectDecodeUtils;
import org.hyperledger.besu.ethereum.api.util.TraceUtils;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.debug.TraceOptions;
import org.hyperledger.besu.ethereum.mainnet.ImmutableTransactionValidationParams;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams;
import org.hyperledger.besu.ethereum.rlp.RLPException;
import org.hyperledger.besu.ethereum.transaction.CallParameter;
Expand All @@ -19,17 +25,24 @@

import java.util.Optional;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TraceRawTransaction implements JsonRpcMethod
{
public class TraceRawTransaction implements JsonRpcMethod {
private static final Logger LOG = LoggerFactory.getLogger(TraceRawTransaction.class);
private final TransactionSimulator transactionSimulator;
private final BlockchainQueries blockchainQueries;
private final ProtocolSchedule protocolSchedule;

// TODO write some tests
public TraceRawTransaction(
final ProtocolSchedule protocolSchedule,
final BlockchainQueries blockchainQueries,
final TransactionSimulator transactionSimulator) {
this.transactionSimulator = transactionSimulator;
this.protocolSchedule = protocolSchedule;
this.blockchainQueries = blockchainQueries;
}

@Override
Expand All @@ -38,15 +51,12 @@ public String getName() {
}

@Override
public JsonRpcResponse response(JsonRpcRequestContext requestContext) {
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
if (requestContext.getRequest().getParamLength() != 2) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);
}

// TODO get blockNumber from blockchainqueries
final long blockNumber = 0L;

final String rawTransaction = requestContext.getRequiredParameter(0, String.class);

final Transaction transaction;
Expand All @@ -59,36 +69,48 @@ public JsonRpcResponse response(JsonRpcRequestContext requestContext) {

LOG.trace("Received local transaction {}", transaction);
frankisawesome marked this conversation as resolved.
Show resolved Hide resolved

// TODO turn tx into callParams

// second param is the trace type/s
final TraceTypeParameter traceTypeParameter =
requestContext.getRequiredParameter(1, TraceTypeParameter.class);
Copy link
Contributor

Choose a reason for hiding this comment

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

If we do get an invalid parameter here, do we return a meaningful error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You'll see this message at DEBUG level

	at org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcHttpService.lambda$handleJsonSingleRequest$14(JsonRpcHttpService.java:610)
	at io.vertx.core.impl.ContextImpl.lambda$null$0(ContextImpl.java:159)
	at io.vertx.core.impl.AbstractContext.dispatch(AbstractContext.java:100)
	at io.vertx.core.impl.ContextImpl.lambda$executeBlocking$1(ContextImpl.java:157)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of `org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TraceTypeParameter`, problem: Invalid trace types supplied: boo, foo
 at [Source: (String)"["boo","foo"]"; line: 1, column: 13]


final Set<TraceTypeParameter.TraceType> traceTypes = traceTypeParameter.getTraceTypes();
final DebugOperationTracer tracer = new DebugOperationTracer(buildTraceOptions(traceTypes));
final Optional<TransactionSimulatorResult> maybeSimulatorResult =
transactionSimulator.process(CallParameter.fromTransaction(transaction),
transactionSimulator.process(
CallParameter.fromTransaction(transaction),
buildTransactionValidationParams(),
tracer,
blockNumber);
blockchainQueries.headBlockNumber());

if (maybeSimulatorResult.isEmpty()) {
throw new IllegalStateException("Invalid transaction simulator result.");
macfarla marked this conversation as resolved.
Show resolved Hide resolved
}
// return MAPPER_IGNORE_REVERT_REASON.valueToTree(builder.build());
return null;

final TransactionTrace transactionTrace =
new TransactionTrace(
maybeSimulatorResult.get().getTransaction(),
maybeSimulatorResult.get().getResult(),
tracer.getTraceFrames());
// TODO why do we get the head of the chain here rather than use the block param
final Block block = blockchainQueries.getBlockchain().getChainHeadBlock();

return new JsonRpcSuccessResponse(
requestContext.getRequest().getId(),
TraceUtils.getTraceCallResult(
protocolSchedule, traceTypes, maybeSimulatorResult, transactionTrace, block));
}

private TransactionValidationParams buildTransactionValidationParams() {
macfarla marked this conversation as resolved.
Show resolved Hide resolved
return ImmutableTransactionValidationParams.builder()
.from(TransactionValidationParams.transactionSimulator())
.build();
}

private TraceOptions buildTraceOptions(final Set<TraceTypeParameter.TraceType> traceTypes) {
return new TraceOptions(
traceTypes.contains(TraceTypeParameter.TraceType.STATE_DIFF),
false,
traceTypes.contains(TraceTypeParameter.TraceType.TRACE)
|| traceTypes.contains(TraceTypeParameter.TraceType.VM_TRACE));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.TraceBlock;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.TraceCall;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.TraceFilter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.TraceRawTransaction;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.TraceReplayBlockTransactions;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.TraceTransaction;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockReplay;
Expand Down Expand Up @@ -68,6 +69,14 @@ protected Map<String, JsonRpcMethod> create() {
new TraceCall(
blockchainQueries,
protocolSchedule,
new TransactionSimulator(
blockchainQueries.getBlockchain(),
blockchainQueries.getWorldStateArchive(),
protocolSchedule,
privacyParameters)),
new TraceRawTransaction(
protocolSchedule,
blockchainQueries,
new TransactionSimulator(
blockchainQueries.getBlockchain(),
blockchainQueries.getWorldStateArchive(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.hyperledger.besu.ethereum.api.util;

import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TraceTypeParameter.TraceType.VM_TRACE;

import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TraceTypeParameter.TraceType;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TraceCallResult;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.diff.StateDiffGenerator;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.diff.StateDiffTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.flat.FlatTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.flat.FlatTraceGenerator;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.flat.MixInIgnoreRevertReason;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.vm.VmTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.vm.VmTraceGenerator;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.transaction.TransactionSimulatorResult;

import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class TraceUtils {
public static JsonNode getTraceCallResult(
final ProtocolSchedule protocolSchedule,
final Set<TraceType> traceTypes,
final Optional<TransactionSimulatorResult> maybeSimulatorResult,
final TransactionTrace transactionTrace,
final Block block) {
final TraceCallResult.Builder builder = TraceCallResult.builder();
final ObjectMapper MAPPER_IGNORE_REVERT_REASON = new ObjectMapper();
// The trace_call specification does not output the revert reason, so we have to remove it
MAPPER_IGNORE_REVERT_REASON.addMixIn(FlatTrace.class, MixInIgnoreRevertReason.class);

transactionTrace
.getResult()
.getRevertReason()
.ifPresentOrElse(
revertReason -> builder.output(revertReason.toHexString()),
() -> builder.output(maybeSimulatorResult.get().getOutput().toString()));

if (traceTypes.contains(TraceType.STATE_DIFF)) {
new StateDiffGenerator()
.generateStateDiff(transactionTrace)
.forEachOrdered(stateDiff -> builder.stateDiff((StateDiffTrace) stateDiff));
}

if (traceTypes.contains(TraceType.TRACE)) {
FlatTraceGenerator.generateFromTransactionTrace(
protocolSchedule, transactionTrace, block, new AtomicInteger(), false)
.forEachOrdered(trace -> builder.addTrace((FlatTrace) trace));
}

if (traceTypes.contains(VM_TRACE)) {
new VmTraceGenerator(transactionTrace)
.generateTraceStream()
.forEachOrdered(vmTrace -> builder.vmTrace((VmTrace) vmTrace));
}

return MAPPER_IGNORE_REVERT_REASON.valueToTree(builder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public static Object[][] specs() {
"trace/specs/replay-trace-transaction/all",
"trace/specs/replay-trace-transaction/halt-cases",
"trace/specs/trace-filter",
"trace/specs/trace-call"
"trace/specs/trace-call",
"trace/specs/trace-raw-transaction"
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public static Object[][] specs() {
"trace/specs/replay-trace-transaction/all",
"trace/specs/replay-trace-transaction/halt-cases",
"trace/specs/trace-filter",
"trace/specs/trace-call"
"trace/specs/trace-call",
"trace/specs/trace-raw-transaction"
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.Transaction;

import java.util.Objects;
import java.util.Optional;

import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.ethereum.core.Transaction;

// Represents parameters for a eth_call or eth_estimateGas JSON-RPC methods.
public class CallParameter {
Expand Down Expand Up @@ -136,8 +136,14 @@ public int hashCode() {
}

public static CallParameter fromTransaction(final Transaction tx) {
final CallParameter callParams = new CallParameter(tx.getSender(), Address.fromPlugin(tx.getTo().get()), tx.getGasLimit(),
Wei.fromQuantity(tx.getGasPrice().get()), Optional.of(Wei.fromQuantity(tx.getMaxPriorityFeePerGas().get())),tx.getMaxFeePerGas(), Wei.fromQuantity(tx.getValue()), tx.getData().get());
return callParams;
return new CallParameter(
tx.getSender(),
Address.fromPlugin(tx.getTo().orElseGet(() -> Address.ZERO)),
tx.getGasLimit(),
Wei.fromQuantity(tx.getGasPrice().orElseGet(() -> Wei.ZERO)),
Optional.of(Wei.fromQuantity(tx.getMaxPriorityFeePerGas().orElseGet(() -> Wei.ZERO))),
tx.getMaxFeePerGas(),
Wei.fromQuantity(tx.getValue()),
tx.getData().get());
}
}