From 6b4d5df9246ce869acd6f327450f33e7d6d0b3cf Mon Sep 17 00:00:00 2001 From: Bhanu Pulluri Date: Wed, 25 Sep 2024 04:12:13 -0400 Subject: [PATCH] Expose revert reason field through GraphQL Signed-off-by: Bhanu Pulluri --- .../pojoadapter/TransactionAdapter.java | 18 ++++++++++++++++++ .../api/src/main/resources/schema.graphqls | 3 +++ 2 files changed, 21 insertions(+) diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/graphql/internal/pojoadapter/TransactionAdapter.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/graphql/internal/pojoadapter/TransactionAdapter.java index d5eacf8e357..f8e60c5527c 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/graphql/internal/pojoadapter/TransactionAdapter.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/graphql/internal/pojoadapter/TransactionAdapter.java @@ -26,6 +26,7 @@ import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.ethereum.core.LogWithMetadata; import org.hyperledger.besu.ethereum.core.Transaction; +import org.hyperledger.besu.ethereum.core.TransactionReceipt; import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput; @@ -292,6 +293,23 @@ public Optional getStatus(final DataFetchingEnvironment environment) { : Optional.of((long) receipt.getStatus())); } + /** + * Retrieves the revert reason of the transaction, if any. + * + *

This method uses the getReceipt method to get the receipt of the transaction. It then checks + * the revert reason of the receipt. It would be an empty Optional for successful transactions. + * Otherwise, it returns an Optional containing the revert reason. + * + * @param environment the data fetching environment. + * @return an Optional containing a Bytes object representing the revert reason of the + * transaction, or an empty Optional . + */ + public Optional getRevertReason(final DataFetchingEnvironment environment) { + return getReceipt(environment) + .map(TransactionReceiptWithMetadata::getReceipt) + .flatMap(TransactionReceipt::getRevertReason); + } + /** * Retrieves the gas used by the transaction. * diff --git a/ethereum/api/src/main/resources/schema.graphqls b/ethereum/api/src/main/resources/schema.graphqls index 87c7509b393..19d3ffea460 100644 --- a/ethereum/api/src/main/resources/schema.graphqls +++ b/ethereum/api/src/main/resources/schema.graphqls @@ -579,6 +579,9 @@ type Transaction { BlobVersionedHashes is a set of hash outputs from the blobs in the transaction. """ blobVersionedHashes: [Bytes32!] + + """Reason returned when transaction fails.""" + revertReason: Bytes } """EIP-4895"""