Skip to content

Commit

Permalink
init trace_replayBlockTransactions
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelStark committed Aug 26, 2019
1 parent c81600c commit 5454265
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,8 @@ public boolean equals(final Object obj) {
public String toString() {
return Long.toString(value);
}

public String toHexString() {
return String.format("0x%s", Long.toHexString(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.NetServices;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.NetVersion;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.RpcModules;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.TraceReplayBlockTransactions;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.TxPoolPantheonStatistics;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.TxPoolPantheonTransactions;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.Web3ClientVersion;
Expand Down Expand Up @@ -333,6 +334,19 @@ blockchainQueries, new TransactionTracer(blockReplay), parameter),
new AdminChangeLogLevel(parameter));
}

if (rpcApis.contains(RpcApis.TRACE)) {
addMethods(
enabledMethods,
new TraceReplayBlockTransactions(
parameter,
new BlockTracer(
new BlockReplay(
protocolSchedule,
blockchainQueries.getBlockchain(),
blockchainQueries.getWorldStateArchive())),
blockchainQueries));
}

boolean eea = rpcApis.contains(RpcApis.EEA), priv = rpcApis.contains(RpcApis.PRIV);
if (eea || priv) {
final PrivateMarkerTransactionFactory markerTransactionFactory =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class RpcApis {
public static final RpcApi EEA = new RpcApi("EEA");
public static final RpcApi PRIV = new RpcApi("PRIV");
public static final RpcApi TX_POOL = new RpcApi("TXPOOL");
public static final RpcApi TRACE = new RpcApi("TRACE");

public static final List<RpcApi> DEFAULT_JSON_RPC_APIS = Arrays.asList(ETH, NET, WEB3);

Expand All @@ -52,6 +53,8 @@ public static Optional<RpcApi> valueOf(final String name) {
return Optional.of(PRIV);
} else if (name.equals(TX_POOL.getCliValue())) {
return Optional.of(TX_POOL);
} else if (name.equals(TRACE.getCliValue())) {
return Optional.of(TRACE);
} else {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import tech.pegasys.pantheon.ethereum.core.Block;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.Gas;
import tech.pegasys.pantheon.ethereum.debug.TraceOptions;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
Expand All @@ -27,10 +30,12 @@
import tech.pegasys.pantheon.ethereum.vm.DebugOperationTracer;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import tech.pegasys.pantheon.util.bytes.BytesValue;

public class TraceReplayBlockTransactions extends AbstractBlockParameterMethod {

Expand Down Expand Up @@ -82,23 +87,59 @@ private Object traceBlock(final Block block, final TraceTypeParameter traceTypeP
.orElse(null);
}

private List<JsonNode> formatTraces(
private JsonNode formatTraces(
final List<TransactionTrace> traces, final TraceTypeParameter traceTypeParameter) {
return traces.stream()
.map((trace) -> formatTrace(trace, traceTypeParameter))
.collect(Collectors.toList());
final Set<TraceTypeParameter.TraceType> traceTypes = traceTypeParameter.getTraceTypes();
final ObjectMapper mapper = new ObjectMapper();
final ArrayNode resultArrayNode = mapper.createArrayNode();
final ObjectNode resultNode = mapper.createObjectNode();
if (traceTypes.contains(TraceTypeParameter.TraceType.TRACE)) {
final ArrayNode tracesNode = resultNode.putArray("trace");
traces.forEach((trace) -> formatWithTraceOption(trace, mapper, tracesNode));
}

resultArrayNode.add(resultNode);
return resultArrayNode;
}

@SuppressWarnings("unused")
private JsonNode formatTrace(
final TransactionTrace trace, final TraceTypeParameter traceTypeParameter) {
// TODO: generate result
ObjectMapper mapper = new ObjectMapper();
return mapper.createObjectNode();
private void formatWithTraceOption(
final TransactionTrace trace, final ObjectMapper mapper, final ArrayNode tracesNode) {
final ObjectNode traceNode = mapper.createObjectNode();
generateActionNode(traceNode, trace);
generateResultNode(traceNode, trace);
traceNode.put("type", "call");
tracesNode.add(traceNode);
}

private void generateResultNode(final ObjectNode traceNode, final TransactionTrace trace) {
final ObjectNode traceResultNode = traceNode.putObject("result");
traceResultNode.put("output", "0x");
traceResultNode.put("gasUsed", Gas.of(trace.getGas()).toHexString());
}

private void generateActionNode(final ObjectNode traceNode, final TransactionTrace trace) {
final ObjectNode actionResultNode = traceNode.putObject("action");
actionResultNode.put("callType", "call");
actionResultNode.put("from", trace.getTransaction().getSender().toString());
actionResultNode.put(
"gas", trace.getTransaction().getUpfrontGasCost().toStrictShortHexString());
actionResultNode.put(
"input",
trace
.getTransaction()
.getData()
.orElse(trace.getTransaction().getInit().orElse(BytesValue.EMPTY))
.toString());
trace
.getTransaction()
.getTo()
.ifPresent(address -> actionResultNode.put("to", address.toString()));
actionResultNode.put("value", trace.getTransaction().getValue().toStrictShortHexString());
}

private Object emptyResult() {
ObjectMapper mapper = new ObjectMapper();
final ObjectMapper mapper = new ObjectMapper();
return mapper.createArrayNode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

public class TraceTypeParameter {

enum TraceType {
public enum TraceType {
TRACE,
VM_TRACE,
STATE_DIFF;
Expand Down

0 comments on commit 5454265

Please # to comment.