Skip to content

Commit

Permalink
Feat : Removed SDK general error types (#2496)
Browse files Browse the repository at this point in the history
* Feat : Removed SDK general error types

* Changed  unnecessary wrapf to wrap

* made some required changes

* changed version 10 to 11

* ran go mod tidy

---------

Co-authored-by: Marius Poke <marius.poke@posteo.de>
  • Loading branch information
ruthishvitwit and mpoke authored Jun 16, 2023
1 parent 9672648 commit 1c8b069
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 23 deletions.
16 changes: 8 additions & 8 deletions ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ 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/v11/types/errors"
ibcante "github.com/cosmos/ibc-go/v4/modules/core/ante"
ibckeeper "github.com/cosmos/ibc-go/v4/modules/core/keeper"

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

func NewAnteHandler(opts HandlerOptions) (sdk.AnteHandler, error) {
if opts.AccountKeeper == nil {
return nil, errorsmod.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, errorsmod.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, errorsmod.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, errorsmod.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, errorsmod.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, errorsmod.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, errorsmod.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
7 changes: 4 additions & 3 deletions ante/gov_ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ 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/v11/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 @@ -50,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 errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, "insufficient initial deposit amount - required: %v", minInitialDeposit)
return errorsmod.Wrapf(gaiaerrors.ErrInsufficientFunds, "insufficient initial deposit amount - required: %v", minInitialDeposit)
}
}

Expand All @@ -61,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 errorsmod.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
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")
)
12 changes: 6 additions & 6 deletions x/globalfee/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (

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/v11/types/errors"
tmstrings "github.com/tendermint/tendermint/libs/strings"

"github.com/cosmos/gaia/v11/x/globalfee"
Expand Down Expand Up @@ -52,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, errorsmod.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 @@ -70,7 +70,7 @@ func (mfd FeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, ne

// feeRequired cannot be empty
if feeTx.GetFee().Len() > feeRequired.Len() {
return ctx, errorsmod.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 @@ -94,7 +94,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 @@ -124,7 +124,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, errorsmod.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 @@ -146,7 +146,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/v11/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
8 changes: 4 additions & 4 deletions x/globalfee/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

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/v11/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 @@ -81,7 +81,7 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
func validateMinimumGasPrices(i interface{}) error {
v, ok := i.(sdk.DecCoins)
if !ok {
return errorsmod.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 @@ -94,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 @@ -113,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

0 comments on commit 1c8b069

Please sign in to comment.