From b5848a72503742084d41691f010a7026c0e21d17 Mon Sep 17 00:00:00 2001 From: Karl Bartel Date: Thu, 29 Feb 2024 15:00:57 +0100 Subject: [PATCH] Improve error messages for credit/debit reverts (#2271) The revert messages now show up in the RPC responses. For most cases, this should provide enough debugging information without looking at the server logs. --- contracts/erc20gas/erc20gas.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/contracts/erc20gas/erc20gas.go b/contracts/erc20gas/erc20gas.go index 5c503ed018..4671ad98ae 100644 --- a/contracts/erc20gas/erc20gas.go +++ b/contracts/erc20gas/erc20gas.go @@ -1,8 +1,10 @@ package erc20gas import ( + "fmt" "math/big" + "github.com/celo-org/celo-blockchain/accounts/abi" "github.com/celo-org/celo-blockchain/common" "github.com/celo-org/celo-blockchain/common/hexutil" "github.com/celo-org/celo-blockchain/contracts/internal/n" @@ -35,9 +37,15 @@ func DebitFees(evm *vm.EVM, address common.Address, amount *big.Int, feeCurrency rootCaller := vm.AccountRef(common.HexToAddress("0x0")) // The caller was already charged for the cost of this operation via IntrinsicGas. - _, leftoverGas, err := evm.Call(rootCaller, *feeCurrency, transactionData, maxGasForDebitGasFeesTransactions, big.NewInt(0)) + ret, leftoverGas, err := evm.Call(rootCaller, *feeCurrency, transactionData, maxGasForDebitGasFeesTransactions, big.NewInt(0)) gasUsed := maxGasForDebitGasFeesTransactions - leftoverGas log.Trace("debitGasFees called", "feeCurrency", *feeCurrency, "gasUsed", gasUsed) + if err != nil { + revertReason, err2 := abi.UnpackRevert(ret) + if err2 == nil { + return fmt.Errorf("DebitFees reverted: %s", revertReason) + } + } return err } @@ -64,8 +72,14 @@ func CreditFees( rootCaller := vm.AccountRef(common.HexToAddress("0x0")) // The caller was already charged for the cost of this operation via IntrinsicGas. - _, leftoverGas, err := evm.Call(rootCaller, *feeCurrency, transactionData, maxGasForCreditGasFeesTransactions, big.NewInt(0)) + ret, leftoverGas, err := evm.Call(rootCaller, *feeCurrency, transactionData, maxGasForCreditGasFeesTransactions, big.NewInt(0)) gasUsed := maxGasForCreditGasFeesTransactions - leftoverGas log.Trace("creditGas called", "feeCurrency", *feeCurrency, "gasUsed", gasUsed) + if err != nil { + revertReason, err2 := abi.UnpackRevert(ret) + if err2 == nil { + return fmt.Errorf("CreditFees reverted: %s", revertReason) + } + } return err }