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
133 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,86 @@ | ||
package eth | ||
|
||
import ( | ||
"context" | ||
"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/core" | ||
"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(ctx context.Context, logger *logging.Logger, err error) error { | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
// 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 | ||
|
||
// Query the indexer for runtime info, to obtain EVM module version. | ||
rtInfo := api.backend.RuntimeInfo() | ||
if rtInfo == nil { | ||
// In case the node has indexer disabled, query the runtime info on the fly. | ||
// XXX: This could be cached per epoch. | ||
var cerr error | ||
rtInfo, cerr = core.NewV1(api.client).RuntimeInfo(ctx) | ||
if cerr != nil { | ||
logger.Debug("failed querying runtime info", "err", err) | ||
} | ||
} | ||
|
||
switch { | ||
case rtInfo == nil || rtInfo.Modules[evm.ModuleName].Version > 1: | ||
// EVM version 2 onward (if no runtime info assume >1). | ||
logger.Debug("failed to execute SimulateCall", "err", err) | ||
return err | ||
default: | ||
// EVM version 1 (deprecated). | ||
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 | ||
} | ||
} |