From e05f3e12526919e6e57837b50304986164b4b4f6 Mon Sep 17 00:00:00 2001 From: codchen Date: Thu, 3 Oct 2024 11:19:19 +0800 Subject: [PATCH] use codespace-d errors for wasm->evm calls --- x/evm/client/wasm/query.go | 26 +++++++++++++------------- x/evm/keeper/evm.go | 16 ++++++++-------- x/evm/types/errors.go | 22 ++++++++++++++++++++++ 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/x/evm/client/wasm/query.go b/x/evm/client/wasm/query.go index 6258fb22f7..6f0642a160 100644 --- a/x/evm/client/wasm/query.go +++ b/x/evm/client/wasm/query.go @@ -28,7 +28,7 @@ func NewEVMQueryHandler(k *keeper.Keeper) *EVMQueryHandler { func (h *EVMQueryHandler) HandleStaticCall(ctx sdk.Context, from string, to string, data []byte) ([]byte, error) { fromAddr, err := sdk.AccAddressFromBech32(from) if err != nil { - return nil, err + return nil, types.ErrInvalidBech32 } var toAddr *common.Address if to != "" { @@ -63,7 +63,7 @@ func (h *EVMQueryHandler) HandleERC20TransferPayload(ctx sdk.Context, recipient func (h *EVMQueryHandler) HandleERC20TokenInfo(ctx sdk.Context, contractAddress string, caller string) ([]byte, error) { callerAddr, err := sdk.AccAddressFromBech32(caller) if err != nil { - return nil, err + return nil, types.ErrInvalidBech32 } contract := common.HexToAddress(contractAddress) abi, err := native.NativeMetaData.GetAbi() @@ -135,7 +135,7 @@ func (h *EVMQueryHandler) HandleERC20TokenInfo(ctx sdk.Context, contractAddress func (h *EVMQueryHandler) HandleERC20Balance(ctx sdk.Context, contractAddress string, account string) ([]byte, error) { addr, err := sdk.AccAddressFromBech32(account) if err != nil { - return nil, err + return nil, types.ErrInvalidBech32 } evmAddr, found := h.k.GetEVMAddress(ctx, addr) if !found { @@ -166,7 +166,7 @@ func (h *EVMQueryHandler) HandleERC20Balance(ctx sdk.Context, contractAddress st func (h *EVMQueryHandler) HandleERC721Owner(ctx sdk.Context, caller string, contractAddress string, tokenId string) ([]byte, error) { callerAddr, err := sdk.AccAddressFromBech32(caller) if err != nil { - return nil, err + return nil, types.ErrInvalidBech32 } contract := common.HexToAddress(contractAddress) abi, err := cw721.Cw721MetaData.GetAbi() @@ -309,7 +309,7 @@ func (h *EVMQueryHandler) HandleERC20Allowance(ctx sdk.Context, contractAddress // Get the evm address of the owner ownerAddr, err := sdk.AccAddressFromBech32(owner) if err != nil { - return nil, err + return nil, types.ErrInvalidBech32 } ownerEvmAddr, found := h.k.GetEVMAddress(ctx, ownerAddr) if !found { @@ -353,7 +353,7 @@ func (h *EVMQueryHandler) HandleERC20Allowance(ctx sdk.Context, contractAddress func (h *EVMQueryHandler) HandleERC721Approved(ctx sdk.Context, caller string, contractAddress string, tokenId string) ([]byte, error) { callerAddr, err := sdk.AccAddressFromBech32(caller) if err != nil { - return nil, err + return nil, types.ErrInvalidBech32 } contract := common.HexToAddress(contractAddress) abi, err := cw721.Cw721MetaData.GetAbi() @@ -388,7 +388,7 @@ func (h *EVMQueryHandler) HandleERC721Approved(ctx sdk.Context, caller string, c func (h *EVMQueryHandler) HandleERC721IsApprovedForAll(ctx sdk.Context, caller string, contractAddress string, owner string, operator string) ([]byte, error) { callerAddr, err := sdk.AccAddressFromBech32(caller) if err != nil { - return nil, err + return nil, types.ErrInvalidBech32 } ownerEvmAddr, found := h.k.GetEVMAddress(ctx, sdk.MustAccAddressFromBech32(owner)) if !found { @@ -422,7 +422,7 @@ func (h *EVMQueryHandler) HandleERC721IsApprovedForAll(ctx sdk.Context, caller s func (h *EVMQueryHandler) HandleERC721TotalSupply(ctx sdk.Context, caller string, contractAddress string) ([]byte, error) { callerAddr, err := sdk.AccAddressFromBech32(caller) if err != nil { - return nil, err + return nil, types.ErrInvalidBech32 } contract := common.HexToAddress(contractAddress) abi, err := cw721.Cw721MetaData.GetAbi() @@ -449,7 +449,7 @@ func (h *EVMQueryHandler) HandleERC721TotalSupply(ctx sdk.Context, caller string func (h *EVMQueryHandler) HandleERC721NameSymbol(ctx sdk.Context, caller string, contractAddress string) ([]byte, error) { callerAddr, err := sdk.AccAddressFromBech32(caller) if err != nil { - return nil, err + return nil, types.ErrInvalidBech32 } contract := common.HexToAddress(contractAddress) abi, err := cw721.Cw721MetaData.GetAbi() @@ -489,7 +489,7 @@ func (h *EVMQueryHandler) HandleERC721NameSymbol(ctx sdk.Context, caller string, func (h *EVMQueryHandler) HandleERC721Uri(ctx sdk.Context, caller string, contractAddress string, tokenId string) ([]byte, error) { callerAddr, err := sdk.AccAddressFromBech32(caller) if err != nil { - return nil, err + return nil, types.ErrInvalidBech32 } t, ok := sdk.NewIntFromString(tokenId) if !ok { @@ -519,7 +519,7 @@ func (h *EVMQueryHandler) HandleERC721Uri(ctx sdk.Context, caller string, contra func (h *EVMQueryHandler) HandleGetEvmAddress(ctx sdk.Context, seiAddr string) ([]byte, error) { addr, err := sdk.AccAddressFromBech32(seiAddr) if err != nil { - return nil, err + return nil, types.ErrInvalidBech32 } evmAddr, associated := h.k.GetEVMAddress(ctx, addr) response := bindings.GetEvmAddressResponse{EvmAddress: evmAddr.Hex(), Associated: associated} @@ -536,7 +536,7 @@ func (h *EVMQueryHandler) HandleGetSeiAddress(ctx sdk.Context, evmAddr string) ( func (h *EVMQueryHandler) HandleERC721RoyaltyInfo(ctx sdk.Context, caller string, contractAddress string, tokenId string, salePrice *sdk.Int) ([]byte, error) { callerAddr, err := sdk.AccAddressFromBech32(caller) if err != nil { - return nil, err + return nil, types.ErrInvalidBech32 } t, ok := sdk.NewIntFromString(tokenId) if !ok { @@ -573,7 +573,7 @@ func (h *EVMQueryHandler) HandleERC721RoyaltyInfo(ctx sdk.Context, caller string func (h *EVMQueryHandler) HandleSupportsInterface(ctx sdk.Context, caller string, id string, contractAddress string) ([]byte, error) { callerAddr, err := sdk.AccAddressFromBech32(caller) if err != nil { - return nil, err + return nil, types.ErrInvalidBech32 } contract := common.HexToAddress(contractAddress) abi, err := cw721.Cw721MetaData.GetAbi() diff --git a/x/evm/keeper/evm.go b/x/evm/keeper/evm.go index 1ec7f039d7..be0a5211ca 100644 --- a/x/evm/keeper/evm.go +++ b/x/evm/keeper/evm.go @@ -32,7 +32,7 @@ func (k *Keeper) HandleInternalEVMCall(ctx sdk.Context, req *types.MsgInternalEV } senderAddr, err := sdk.AccAddressFromBech32(req.Sender) if err != nil { - return nil, err + return nil, types.ErrInvalidBech32 } ret, err := k.CallEVM(ctx, k.GetEVMAddressOrDefault(ctx, senderAddr), to, req.Value, req.Data) if err != nil { @@ -75,10 +75,10 @@ func (k *Keeper) HandleInternalEVMDelegateCall(ctx sdk.Context, req *types.MsgIn func (k *Keeper) CallEVM(ctx sdk.Context, from common.Address, to *common.Address, val *sdk.Int, data []byte) (retdata []byte, reterr error) { if ctx.IsEVM() && !ctx.EVMEntryViaWasmdPrecompile() { - return nil, errors.New("sei does not support EVM->CW->EVM call pattern") + return nil, types.ErrMoreThanOneHop } if to == nil && len(data) > params.MaxInitCodeSize { - return nil, fmt.Errorf("%w: code size %v, limit %v", core.ErrMaxInitCodeSizeExceeded, len(data), params.MaxInitCodeSize) + return nil, sdkerrors.Wrap(types.ErrMaxInitCodeSize, fmt.Sprintf("%w: code size %v, limit %v", core.ErrMaxInitCodeSizeExceeded, len(data), params.MaxInitCodeSize)) } value := utils.Big0 if val != nil { @@ -105,15 +105,15 @@ func (k *Keeper) CallEVM(ctx sdk.Context, from common.Address, to *common.Addres } res, err := k.applyEVMMessage(ctx, evmMsg, stateDB, gp) if err != nil { - return nil, err + return nil, sdkerrors.Wrap(types.ErrEVMExecution, err.Error()) } k.consumeEvmGas(ctx, res.UsedGas) if res.Err != nil { - return nil, res.Err + return nil, sdkerrors.Wrap(types.ErrResult, res.Err.Error()) } surplus, err := stateDB.Finalize() if err != nil { - return nil, err + return nil, sdkerrors.Wrap(types.ErrFinalizeState, err.Error()) } vmErr := "" if res.Err != nil { @@ -138,7 +138,7 @@ func (k *Keeper) CallEVM(ctx sdk.Context, from common.Address, to *common.Addres } receipt, err := k.WriteReceipt(ctx, stateDB, evmMsg, ethtypes.LegacyTxType, ctx.TxSum(), res.UsedGas, vmErr) if err != nil { - return nil, err + return nil, sdkerrors.Wrap(types.ErrWriteReceipt, err.Error()) } bloom := ethtypes.Bloom{} bloom.SetBytes(receipt.LogsBloom) @@ -165,7 +165,7 @@ func (k *Keeper) callEVM(ctx sdk.Context, from common.Address, to *common.Addres ret, leftoverGas, err := f(vm.AccountRef(from), to, data, evmGasLimit, value) k.consumeEvmGas(ctx, evmGasLimit-leftoverGas) if err != nil { - return nil, err + return nil, sdkerrors.Wrap(types.ErrEVMExecution, err.Error()) } return ret, nil } diff --git a/x/evm/types/errors.go b/x/evm/types/errors.go index fbe7657987..3014e59866 100644 --- a/x/evm/types/errors.go +++ b/x/evm/types/errors.go @@ -3,6 +3,8 @@ package types import ( "fmt" "strings" + + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) type AssociationMissingErr struct { @@ -23,3 +25,23 @@ func (e AssociationMissingErr) AddressType() string { } return "sei" } + +const EvmCodespace = "evm" + +var ( + errInternal = sdkerrors.Register(EvmCodespace, 1, "internal") + + ErrInvalidBech32 = sdkerrors.Register(EvmCodespace, 2, "invalid bech32 address representation") + + ErrMoreThanOneHop = sdkerrors.Register(EvmCodespace, 3, "sei does not support EVM->CW->EVM call pattern") + + ErrMaxInitCodeSize = sdkerrors.Register(EvmCodespace, 4, "max init code size exceeded") + + ErrEVMExecution = sdkerrors.Register(EvmCodespace, 5, "execution error") + + ErrResult = sdkerrors.Register(EvmCodespace, 6, "error in execution result") + + ErrFinalizeState = sdkerrors.Register(EvmCodespace, 7, "error in stateDB finalization") + + ErrWriteReceipt = sdkerrors.Register(EvmCodespace, 8, "error writing receipt") +)