Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat : Removed SDK general error types #2496

Merged
merged 10 commits into from
Jun 16, 2023
17 changes: 9 additions & 8 deletions ante/ante.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package ante

import (
errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
gaiaerrors "github.com/cosmos/gaia/v9/types/errors"
ibcante "github.com/cosmos/ibc-go/v4/modules/core/ante"
ibckeeper "github.com/cosmos/ibc-go/v4/modules/core/keeper"

Expand All @@ -26,25 +27,25 @@ type HandlerOptions struct {

func NewAnteHandler(opts HandlerOptions) (sdk.AnteHandler, error) {
if opts.AccountKeeper == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "account keeper is required for AnteHandler")
return nil, errorsmod.Wrap(gaiaerrors.ErrLogic, "account keeper is required for AnteHandler")
}
if opts.BankKeeper == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "bank keeper is required for AnteHandler")
return nil, errorsmod.Wrap(gaiaerrors.ErrLogic, "bank keeper is required for AnteHandler")
}
if opts.SignModeHandler == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for AnteHandler")
return nil, errorsmod.Wrap(gaiaerrors.ErrLogic, "sign mode handler is required for AnteHandler")
}
if opts.IBCkeeper == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "IBC keeper is required for AnteHandler")
return nil, errorsmod.Wrap(gaiaerrors.ErrLogic, "IBC keeper is required for AnteHandler")
}
if opts.GlobalFeeSubspace.Name() == "" {
return nil, sdkerrors.Wrap(sdkerrors.ErrNotFound, "globalfee param store is required for AnteHandler")
return nil, errorsmod.Wrap(gaiaerrors.ErrNotFound, "globalfee param store is required for AnteHandler")
}
if opts.StakingSubspace.Name() == "" {
return nil, sdkerrors.Wrap(sdkerrors.ErrNotFound, "staking param store is required for AnteHandler")
return nil, errorsmod.Wrap(gaiaerrors.ErrNotFound, "staking param store is required for AnteHandler")
}
if opts.GovKeeper == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "gov keeper is required for AnteHandler")
return nil, errorsmod.Wrap(gaiaerrors.ErrLogic, "gov keeper is required for AnteHandler")
}

sigGasConsumer := opts.SigGasConsumer
Expand Down
8 changes: 5 additions & 3 deletions ante/gov_ante.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package ante

import (
errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
gaiaerrors "github.com/cosmos/gaia/v9/types/errors"

"github.com/cosmos/cosmos-sdk/x/authz"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
Expand Down Expand Up @@ -49,7 +51,7 @@ func (g GovPreventSpamDecorator) ValidateGovMsgs(ctx sdk.Context, msgs []sdk.Msg
depositParams := g.govKeeper.GetDepositParams(ctx)
minInitialDeposit := g.calcMinInitialDeposit(depositParams.MinDeposit)
if msg.InitialDeposit.IsAllLT(minInitialDeposit) {
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, "insufficient initial deposit amount - required: %v", minInitialDeposit)
return errorsmod.Wrapf(gaiaerrors.ErrInsufficientFunds, "insufficient initial deposit amount - required: %v", minInitialDeposit)
}
}

Expand All @@ -60,7 +62,7 @@ func (g GovPreventSpamDecorator) ValidateGovMsgs(ctx sdk.Context, msgs []sdk.Msg
for _, v := range execMsg.Msgs {
var innerMsg sdk.Msg
if err := g.cdc.UnpackAny(v, &innerMsg); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "cannot unmarshal authz exec msgs")
return errorsmod.Wrap(gaiaerrors.ErrUnauthorized, "cannot unmarshal authz exec msgs")
}
if err := validMsg(innerMsg); err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/cosmos/gaia/v10
go 1.20

require (
cosmossdk.io/errors v1.0.0-beta.7
cosmossdk.io/math v1.0.0
github.com/cosmos/cosmos-sdk v0.45.16-ics
github.com/cosmos/go-bip39 v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
mpoke marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ cosmossdk.io/core v0.5.1 h1:vQVtFrIYOQJDV3f7rw4pjjVqc1id4+mE0L9hHP66pyI=
cosmossdk.io/core v0.5.1/go.mod h1:KZtwHCLjcFuo0nmDc24Xy6CRNEL9Vl/MeimQ2aC7NLE=
cosmossdk.io/depinject v1.0.0-alpha.3 h1:6evFIgj//Y3w09bqOUOzEpFj5tsxBqdc5CfkO7z+zfw=
cosmossdk.io/depinject v1.0.0-alpha.3/go.mod h1:eRbcdQ7MRpIPEM5YUJh8k97nxHpYbc3sMUnEtt8HPWU=
cosmossdk.io/errors v1.0.0-beta.7 h1:gypHW76pTQGVnHKo6QBkb4yFOJjC+sUGRc5Al3Odj1w=
cosmossdk.io/errors v1.0.0-beta.7/go.mod h1:mz6FQMJRku4bY7aqS/Gwfcmr/ue91roMEKAmDUDpBfE=
cosmossdk.io/math v1.0.0 h1:ro9w7eKx23om2tZz/VM2Pf+z2WAbGX1yDQQOJ6iGeJw=
cosmossdk.io/math v1.0.0/go.mod h1:Ygz4wBHrgc7g0N+8+MrnTfS9LLn9aaTGa9hKopuym5k=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
Expand Down
34 changes: 34 additions & 0 deletions types/errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package errors

import (
errorsmod "cosmossdk.io/errors"
)

const codespace = "gaia"

var (
// ErrTxDecode is returned if we cannot parse a transaction
ErrTxDecode = errorsmod.Register(codespace, 1, "tx parse error")
// ErrUnauthorized is used whenever a request without sufficient
// authorization is handled.
ErrUnauthorized = errorsmod.Register(codespace, 2, "unauthorized")

// ErrInsufficientFunds is used when the account cannot pay requested amount.
ErrInsufficientFunds = errorsmod.Register(codespace, 3, "insufficient funds")

// ErrInsufficientFunds is used when the account cannot pay requested amount.
ErrInsufficientFee = errorsmod.Register(codespace, 4, "insufficient fee")

// ErrInvalidCoins is used when sdk.Coins are invalid.
ErrInvalidCoins = errorsmod.Register(codespace, 5, "invalid coins")

// ErrInvalidType defines an error an invalid type.
ErrInvalidType = errorsmod.Register(codespace, 6, "invalid type")

// ErrLogic defines an internal logic error, e.g. an invariant or assertion
// that is violated. It is a programmer error, not a user-facing error.
ErrLogic = errorsmod.Register(codespace, 7, "internal logic error")

// ErrNotFound defines an error when requested entity doesn't exist in the state.
ErrNotFound = errorsmod.Register(codespace, 8, "not found")
)
13 changes: 7 additions & 6 deletions x/globalfee/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"errors"
"fmt"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
gaiaerrors "github.com/cosmos/gaia/v9/types/errors"
tmstrings "github.com/tendermint/tendermint/libs/strings"

"github.com/cosmos/gaia/v10/x/globalfee"
Expand Down Expand Up @@ -51,7 +52,7 @@ func NewFeeDecorator(globalfeeSubspace, stakingSubspace paramtypes.Subspace) Fee
func (mfd FeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must implement the sdk.FeeTx interface")
return ctx, errorsmod.Wrap(gaiaerrors.ErrTxDecode, "Tx must implement the sdk.FeeTx interface")
}

// Do not check minimum-gas-prices and global fees during simulations
Expand All @@ -68,7 +69,7 @@ func (mfd FeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, ne
// reject the transaction early if the feeCoins have more denoms than the fee requirement
// feeRequired cannot be empty
if feeTx.GetFee().Len() > feeRequired.Len() {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "fee is not a subset of required fees; got %s, required: %s", feeTx.GetFee().String(), feeRequired.String())
return ctx, errorsmod.Wrapf(gaiaerrors.ErrInvalidCoins, "fee is not a subset of required fees; got %s, required: %s", feeTx.GetFee().String(), feeRequired.String())
}

// Sort fee tx's coins, zero coins in feeCoins are already removed
Expand All @@ -92,7 +93,7 @@ func (mfd FeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, ne
// special case: if feeCoinsNonZeroDenom=[], DenomsSubsetOf returns true
// special case: if feeCoinsNonZeroDenom is not empty, but nonZeroCoinFeesReq empty, return false
if !feeCoinsNonZeroDenom.DenomsSubsetOf(nonZeroCoinFeesReq) {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInsufficientFee, "fee is not a subset of required fees; got %s, required: %s", feeCoins.String(), feeRequired.String())
return ctx, errorsmod.Wrapf(gaiaerrors.ErrInsufficientFee, "fee is not a subset of required fees; got %s, required: %s", feeCoins.String(), feeRequired.String())
}

// If the feeCoins pass the denoms check, check they are bypass-msg types.
Expand Down Expand Up @@ -122,7 +123,7 @@ func (mfd FeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, ne
if len(zeroCoinFeesDenomReq) != 0 {
return next(ctx, tx, simulate)
}
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInsufficientFee, "insufficient fees; got: %s required: %s", feeCoins.String(), feeRequired.String())
return ctx, errorsmod.Wrapf(gaiaerrors.ErrInsufficientFee, "insufficient fees; got: %s required: %s", feeCoins.String(), feeRequired.String())
}

// when feeCoins != []
Expand All @@ -144,7 +145,7 @@ func (mfd FeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, ne
errMsg = fmt.Sprintf("Insufficient fees; bypass-min-fee-msg-types with gas consumption %v exceeds the maximum allowed gas value of %v.", gas, maxTotalBypassMinFeeMsgGasUsage)
}

return ctx, sdkerrors.Wrap(sdkerrors.ErrInsufficientFee, errMsg)
return ctx, errorsmod.Wrap(gaiaerrors.ErrInsufficientFee, errMsg)
}

return next(ctx, tx, simulate)
Expand Down
5 changes: 3 additions & 2 deletions x/globalfee/ante/fee_utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package ante

import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
gaiaerrors "github.com/cosmos/gaia/v9/types/errors"
)

// ContainZeroCoins returns true if the given coins are empty or contain zero coins,
Expand All @@ -28,7 +29,7 @@ func CombinedFeeRequirement(globalFees, minGasPrices sdk.Coins) (sdk.Coins, erro
// global fees should never be empty
// since it has a default value using the staking module's bond denom
if len(globalFees) == 0 {
return sdk.Coins{}, sdkerrors.Wrapf(sdkerrors.ErrNotFound, "global fee cannot be empty")
return sdk.Coins{}, errorsmod.Wrapf(gaiaerrors.ErrNotFound, "global fee cannot be empty")
}

// empty min_gas_price
Expand Down
4 changes: 2 additions & 2 deletions x/globalfee/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"encoding/json"
"fmt"

errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/module"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -48,7 +48,7 @@ func (a AppModuleBasic) ValidateGenesis(marshaler codec.JSONCodec, _ client.TxEn
return err
}
if err := data.Params.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "params")
return errorsmod.Wrap(err, "params")
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions x/globalfee/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package types
import (
"encoding/json"

errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// NewGenesisState - Create a new genesis state
Expand Down Expand Up @@ -33,7 +33,7 @@ func GetGenesisStateFromAppState(cdc codec.Codec, appState map[string]json.RawMe

func ValidateGenesis(data GenesisState) error {
if err := data.Params.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "globalfee params")
return errorsmod.Wrap(err, "globalfee params")
}

return nil
Expand Down
9 changes: 5 additions & 4 deletions x/globalfee/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"fmt"
"strings"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
gaiaerrors "github.com/cosmos/gaia/v9/types/errors"
ibcclienttypes "github.com/cosmos/ibc-go/v4/modules/core/02-client/types"
ibcchanneltypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"
)
Expand Down Expand Up @@ -80,7 +81,7 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
func validateMinimumGasPrices(i interface{}) error {
v, ok := i.(sdk.DecCoins)
if !ok {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "type: %T, expected sdk.DecCoins", i)
return errorsmod.Wrapf(gaiaerrors.ErrInvalidType, "type: %T, expected sdk.DecCoins", i)
}

dec := DecCoins(v)
Expand All @@ -93,7 +94,7 @@ type BypassMinFeeMsgTypes []string
func validateBypassMinFeeMsgTypes(i interface{}) error {
bypassMinFeeMsgTypes, ok := i.([]string)
if !ok {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "type: %T, expected []sdk.Msg", i)
return errorsmod.Wrapf(gaiaerrors.ErrInvalidType, "type: %T, expected []sdk.Msg", i)
}

for _, msgType := range bypassMinFeeMsgTypes {
Expand All @@ -112,7 +113,7 @@ func validateBypassMinFeeMsgTypes(i interface{}) error {
func validateMaxTotalBypassMinFeeMsgGasUsage(i interface{}) error {
_, ok := i.(uint64)
if !ok {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "type: %T, expected uint64", i)
return errorsmod.Wrapf(gaiaerrors.ErrInvalidType, "type: %T, expected uint64", i)
}

return nil
Expand Down