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
2 people authored and sainoe committed Jun 23, 2023
1 parent 599fc36 commit 4e93489
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 136 deletions.
18 changes: 10 additions & 8 deletions ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ 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"
ibcante "github.com/cosmos/ibc-go/v7/modules/core/ante"
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"

gaiaerrors "github.com/cosmos/gaia/v11/types/errors"
// gaiafeeante "github.com/cosmos/gaia/v11/x/globalfee/ante"
)

// HandlerOptions extend the SDK's AnteHandler options by requiring the IBC
Expand All @@ -25,26 +27,26 @@ 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")
}
// TODO: Enable with Globalfee
// 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
8 changes: 5 additions & 3 deletions ante/gov_ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ 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"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
Expand Down Expand Up @@ -50,7 +52,7 @@ func (g GovPreventSpamDecorator) ValidateGovMsgs(ctx sdk.Context, msgs []sdk.Msg
params := g.govKeeper.GetParams(ctx)
minInitialDeposit := g.calcMinInitialDeposit(params.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 +63,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")
)
28 changes: 14 additions & 14 deletions x/globalfee/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ package ante

// 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 @@ package ante
// 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 @@ package ante

// 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 @@ -88,14 +88,14 @@ package ante
// // when feeCoins does not contain zero coins' denoms in feeRequired
// feeCoinsNonZeroDenom, feeCoinsZeroDenom := splitCoinsByDenoms(feeCoins, zeroCoinFeesDenomReq)

// // Check that the fees are in expected denominations.
// // according to splitCoinsByDenoms(), feeCoinsZeroDenom must be in denom subset of zeroCoinFeesDenomReq.
// // check if feeCoinsNonZeroDenom is a subset of nonZeroCoinFeesReq.
// // 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())
// }
// Check that the fees are in expected denominations.
// according to splitCoinsByDenoms(), feeCoinsZeroDenom must be in denom subset of zeroCoinFeesDenomReq.
// check if feeCoinsNonZeroDenom is a subset of nonZeroCoinFeesReq.
// 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, 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 @@ package ante
// 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,8 +146,8 @@ package ante
// 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
Loading

0 comments on commit 4e93489

Please sign in to comment.