From 37e86141a23a4d33d2853edba7e23849c6c13b40 Mon Sep 17 00:00:00 2001 From: 7at Date: Wed, 16 Oct 2024 00:46:19 +0500 Subject: [PATCH] Fix evmtool JSON Error Field to Return Hex String Instead of Long Null String on REVERT Signed-off-by: 7at --- .../besu/evmtool/EvmToolCommand.java | 4 +-- .../besu/evmtool/trace/revert.json | 2 +- .../besu/evm/tracing/StandardJsonTracer.java | 35 +------------------ .../besu/evm/toy/EvmToyCommand.java | 5 +-- 4 files changed, 4 insertions(+), 42 deletions(-) diff --git a/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/EvmToolCommand.java b/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/EvmToolCommand.java index f9d7c0db476..2574d4b8e39 100644 --- a/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/EvmToolCommand.java +++ b/ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/EvmToolCommand.java @@ -529,9 +529,7 @@ public void run() { messageFrame .getExceptionalHaltReason() .ifPresent(haltReason -> out.println(haltReason)); - messageFrame - .getRevertReason() - .ifPresent(bytes -> out.println(new String(bytes.toArrayUnsafe(), UTF_8))); + messageFrame.getRevertReason().ifPresent(bytes -> out.println(bytes.toHexString())); } } } diff --git a/ethereum/evmtool/src/test/resources/org/hyperledger/besu/evmtool/trace/revert.json b/ethereum/evmtool/src/test/resources/org/hyperledger/besu/evmtool/trace/revert.json index 4798d5da706..32ee526fdd9 100644 --- a/ethereum/evmtool/src/test/resources/org/hyperledger/besu/evmtool/trace/revert.json +++ b/ethereum/evmtool/src/test/resources/org/hyperledger/besu/evmtool/trace/revert.json @@ -12,7 +12,7 @@ {"pc":7,"op":82,"gas":"0x2540be3fa","gasCost":"0x6","memSize":0,"stack":["0x4e6f7065","0x0"],"depth":1,"refund":0,"opName":"MSTORE"}, {"pc":8,"op":96,"gas":"0x2540be3f4","gasCost":"0x3","memSize":32,"stack":[],"depth":1,"refund":0,"opName":"PUSH1"}, {"pc":10,"op":96,"gas":"0x2540be3f1","gasCost":"0x3","memSize":32,"stack":["0x4"],"depth":1,"refund":0,"opName":"PUSH1"}, - {"pc":12,"op":253,"gas":"0x2540be3ee","gasCost":"0x0","memSize":32,"stack":["0x4","0x1c"],"depth":1,"refund":0,"opName":"REVERT","error":"Nope"}, + {"pc":12,"op":253,"gas":"0x2540be3ee","gasCost":"0x0","memSize":32,"stack":["0x4","0x1c"],"depth":1,"refund":0,"opName":"REVERT","error":"0x4e6f7065"}, {"stateRoot":"0x405bbd98da2aca6dff77f79e0b270270c48d6a3e07b76db675b20e454b50bbcb","output":"0x4e6f7065","gasUsed":"0x12","pass":true,"fork":"Cancun"} ] } \ No newline at end of file diff --git a/evm/src/main/java/org/hyperledger/besu/evm/tracing/StandardJsonTracer.java b/evm/src/main/java/org/hyperledger/besu/evm/tracing/StandardJsonTracer.java index baf052ba4eb..8071fa942f1 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/tracing/StandardJsonTracer.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/tracing/StandardJsonTracer.java @@ -14,8 +14,6 @@ */ package org.hyperledger.besu.evm.tracing; -import static com.google.common.base.Strings.padStart; - import org.hyperledger.besu.evm.code.OpcodeInfo; import org.hyperledger.besu.evm.frame.ExceptionalHaltReason; import org.hyperledger.besu.evm.frame.MessageFrame; @@ -224,7 +222,7 @@ public void tracePostExecution( .append("\""); } else if (messageFrame.getRevertReason().isPresent()) { sb.append(",\"error\":\"") - .append(quoteEscape(messageFrame.getRevertReason().orElse(Bytes.EMPTY))) + .append(messageFrame.getRevertReason().get().toHexString()) .append("\""); } @@ -232,37 +230,6 @@ public void tracePostExecution( out.println(sb); } - private static String quoteEscape(final Bytes bytes) { - final StringBuilder result = new StringBuilder(bytes.size()); - for (final byte b : bytes.toArrayUnsafe()) { - final int c = Byte.toUnsignedInt(b); - // list from RFC-4627 section 2 - if (c == '"') { - result.append("\\\""); - } else if (c == '\\') { - result.append("\\\\"); - } else if (c == '/') { - result.append("\\/"); - } else if (c == '\b') { - result.append("\\b"); - } else if (c == '\f') { - result.append("\\f"); - } else if (c == '\n') { - result.append("\\n"); - } else if (c == '\r') { - result.append("\\r"); - } else if (c == '\t') { - result.append("\\t"); - } else if (c <= 0x1F) { - result.append("\\u"); - result.append(padStart(Integer.toHexString(c), 4, '0')); - } else { - result.append((char) b); - } - } - return result.toString(); - } - @Override public void tracePrecompileCall( final MessageFrame frame, final long gasRequirement, final Bytes output) { diff --git a/evm/src/test/java/org/hyperledger/besu/evm/toy/EvmToyCommand.java b/evm/src/test/java/org/hyperledger/besu/evm/toy/EvmToyCommand.java index fcf26c3c569..9a5c3cd1eaf 100644 --- a/evm/src/test/java/org/hyperledger/besu/evm/toy/EvmToyCommand.java +++ b/evm/src/test/java/org/hyperledger/besu/evm/toy/EvmToyCommand.java @@ -32,7 +32,6 @@ import java.io.PrintStream; import java.math.BigInteger; -import java.nio.charset.StandardCharsets; import java.util.Deque; import java.util.List; @@ -208,9 +207,7 @@ public void run() { out.println(messageFrame.getExceptionalHaltReason().get()); } if (messageFrame.getRevertReason().isPresent()) { - out.println( - new String( - messageFrame.getRevertReason().get().toArrayUnsafe(), StandardCharsets.UTF_8)); + out.println(messageFrame.getRevertReason().get().toHexString()); } } if (messageFrameStack.isEmpty()) {