Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support simplified revert reason encoding
- Loading branch information
Showing
5 changed files
with
113 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package eth | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/oasisprotocol/oasis-core/go/common/logging" | ||
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/evm" | ||
) | ||
|
||
const ( | ||
revertErrorPrefix = "reverted: " | ||
) | ||
|
||
type RevertErrorV1 struct { | ||
error | ||
Reason string `json:"reason"` | ||
} | ||
|
||
// ErrorData returns the ABI encoded error reason. | ||
func (e *RevertErrorV1) ErrorData() interface{} { | ||
return e.Reason | ||
} | ||
|
||
func (api *publicAPI) newRevertErrorV1(revertErr error) *RevertErrorV1 { | ||
// ABI encoded function. | ||
abiReason := []byte{0x08, 0xc3, 0x79, 0xa0} // Keccak256("Error(string)") | ||
|
||
// ABI encode the revert Reason string. | ||
revertReason := strings.TrimPrefix(revertErr.Error(), revertErrorPrefix) | ||
typ, _ := abi.NewType("string", "", nil) | ||
unpacked, err := (abi.Arguments{{Type: typ}}).Pack(revertReason) | ||
if err != nil { | ||
api.Logger.Error("failed to encode revert error", "revert_reason", revertReason, "err", err) | ||
return &RevertErrorV1{ | ||
error: revertErr, | ||
} | ||
} | ||
abiReason = append(abiReason, unpacked...) | ||
|
||
return &RevertErrorV1{ | ||
error: revertErr, | ||
Reason: hexutil.Encode(abiReason), | ||
} | ||
} | ||
|
||
func (api *publicAPI) handleCallFailure(logger *logging.Logger, err error) error { | ||
// In EVM module version 1 the SDK decoded and returned readable revert reasons. | ||
// Since EVM version 2 the raw data is just base64 encoded and returned. | ||
// Changed in https://github.com/oasisprotocol/oasis-sdk/pull/1479 | ||
rtInfo := api.backend.RuntimeInfo() | ||
switch rtInfo.Modules[evm.ModuleName].Version { | ||
case 1: | ||
if strings.HasPrefix(err.Error(), revertErrorPrefix) { | ||
revertErr := api.newRevertErrorV1(err) | ||
logger.Debug("failed to execute SimulateCall, reverted", "err", err, "reason", revertErr.Reason) | ||
return revertErr | ||
} | ||
logger.Debug("failed to execute SimulateCall", "err", err) | ||
return err | ||
default: | ||
// EVM version 2 onward. | ||
return err | ||
} | ||
} |