Docsity

Get revert reason - Besu, Resúmenes de Música

Toggle sidebar
bg1
I do this document because I’ve seen several options on the
internet of how to obtain the Revert Reason of a transaction and in
my case I found a simpler way and also that avoids the problem of
special characters that appear in the conversion:
public static String getRevertReason(String revertReason)
{String errorMethodId = "0x08c379a0"; //
Numeric.toHexString(Hash.sha3("Error(string)".getBytes())).subs
tring(0, 10)if (revertReason.startsWith(errorMethodId)) {return
hexToAscii(revertReason.substring(274)).trim();}return
"Transaction failed";}public static String hexToAscii(String
hexStr) {StringBuilder output = new StringBuilder("");for (int
i = 0; i < hexStr.length(); i += 2) {String str =
hexStr.substring(i, i + 2);output.append((char)
Integer.parseInt(str, 16));}return output.toString();}
According to solidity documentation, I’m removing the initial part
corresponding to:
• Function selector for Error
• Data offset
• String length
and I would only be keeping the part corresponding to the
hexadecimal of the message and for this part I only do a conversion
from hexadecimal string to ascii string.
Example test:
@Testpublic void reason() throws Exception {String revertReason
=
"0x08c379a00000000000000000000000000000000000000000000000000000
pf2

Vista previa parcial del texto

¡Descarga Get revert reason - Besu y más Resúmenes en PDF de Música solo en Docsity!

I do this document because I’ve seen several options on the

internet of how to obtain the Revert Reason of a transaction and in

my case I found a simpler way and also that avoids the problem of

special characters that appear in the conversion:

public static String getRevertReason(String revertReason) {String errorMethodId = "0x08c379a0"; // Numeric.toHexString(Hash.sha3("Error(string)".getBytes())).subs tring(0, 10)if (revertReason.startsWith(errorMethodId)) {return hexToAscii(revertReason.substring(274)).trim();}return "Transaction failed";}public static String hexToAscii(String hexStr) {StringBuilder output = new StringBuilder("");for (int i = 0; i < hexStr.length(); i += 2) {String str = hexStr.substring(i, i + 2);output.append((char) Integer.parseInt(str, 16));}return output.toString();}

According to solidity documentation, I’m removing the initial part

corresponding to:

• Function selector for Error

• Data offset

• String length

and I would only be keeping the part corresponding to the

hexadecimal of the message and for this part I only do a conversion

from hexadecimal string to ascii string.

Example test:

@Testpublic void reason() throws Exception {String revertReason

"0x08c379a

000000000006408c379a00000000000000000000000000 00000000000000000 000000000000000000002000000000000000000000000000000000000000000 0000000000000000000001c726f6c65206e6f742070726573656e7420696e 746865207374657000000000000000000000000000000000000000000000000 00000000000000000";String reason = getRevertReason(revertReason);assertEquals(reason, "role not present in the step");}