Skip to content

Commit

Permalink
Fix evmtool JSON Error Field to Return Hex String Instead of Long Nul…
Browse files Browse the repository at this point in the history
…l String on REVERT (#7774)

Signed-off-by: 7at <itisfor@outlook.com>
Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com>
  • Loading branch information
itfat and macfarla authored Oct 28, 2024
1 parent 8ed4a90 commit 42b3cd4
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -224,45 +222,14 @@ 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("\"");
}

sb.append(storageString).append("}");
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit 42b3cd4

Please sign in to comment.