From 9243c6984cc145a25d4dd80f7ec3c1dd4b8d1f5f Mon Sep 17 00:00:00 2001 From: ruthishvitwit <122080147+ruthishvitwit@users.noreply.github.com> Date: Fri, 16 Jun 2023 13:33:31 +0530 Subject: [PATCH] Feat : Removed SDK general error types (#2496) * 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 --- ante/ante.go | 18 +-- ante/gov_ante.go | 8 +- types/errors/errors.go | 34 ++++++ x/globalfee/ante/fee.go | 28 ++--- x/globalfee/ante/fee_utils.go | 212 +++++++++++++++++----------------- x/globalfee/types/params.go | 10 +- 6 files changed, 174 insertions(+), 136 deletions(-) create mode 100644 types/errors/errors.go diff --git a/ante/ante.go b/ante/ante.go index 611ff38835c..5a53c94952d 100644 --- a/ante/ante.go +++ b/ante/ante.go @@ -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 @@ -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 diff --git a/ante/gov_ante.go b/ante/gov_ante.go index 13930843de1..465123795c9 100644 --- a/ante/gov_ante.go +++ b/ante/gov_ante.go @@ -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" @@ -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) } } @@ -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 diff --git a/types/errors/errors.go b/types/errors/errors.go new file mode 100644 index 00000000000..6921a7bef9f --- /dev/null +++ b/types/errors/errors.go @@ -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") +) diff --git a/x/globalfee/ante/fee.go b/x/globalfee/ante/fee.go index cc44edb1cb8..81969e3f857 100644 --- a/x/globalfee/ante/fee.go +++ b/x/globalfee/ante/fee.go @@ -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" @@ -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 @@ -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 @@ -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. // // @@ -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 != [] @@ -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) // } diff --git a/x/globalfee/ante/fee_utils.go b/x/globalfee/ante/fee_utils.go index f776b10f398..27c5fb3899d 100644 --- a/x/globalfee/ante/fee_utils.go +++ b/x/globalfee/ante/fee_utils.go @@ -1,114 +1,114 @@ package ante -// // import ( -// sdk "github.com/cosmos/cosmos-sdk/types" -// sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" -//) -// -//// ContainZeroCoins returns true if the given coins are empty or contain zero coins, -//// Note that the coins denoms must be validated, see sdk.ValidateDenom +// errorsmod "cosmossdk.io/errors" +// sdk "github.com/cosmos/cosmos-sdk/types" +// gaiaerrors "github.com/cosmos/gaia/v11/types/errors" +// ) + +// ContainZeroCoins returns true if the given coins are empty or contain zero coins, +// Note that the coins denoms must be validated, see sdk.ValidateDenom // func ContainZeroCoins(coins sdk.Coins) bool { -// if len(coins) == 0 { -// return true -// } -// for _, coin := range coins { -// if coin.IsZero() { -// return true -// } -// } -// -// return false -//} -// -//// CombinedFeeRequirement returns the global fee and min_gas_price combined and sorted. -//// Both globalFees and minGasPrices must be valid, but CombinedFeeRequirement -//// does not validate them, so it may return 0denom. -//// if globalfee is empty, CombinedFeeRequirement return sdk.Coins{} +// if len(coins) == 0 { +// return true +// } +// for _, coin := range coins { +// if coin.IsZero() { +// return true +// } +// } + +// return false +// } + +// CombinedFeeRequirement returns the global fee and min_gas_price combined and sorted. +// Both globalFees and minGasPrices must be valid, but CombinedFeeRequirement +// does not validate them, so it may return 0denom. +// if globalfee is empty, CombinedFeeRequirement return sdk.Coins{} // func CombinedFeeRequirement(globalFees, minGasPrices sdk.Coins) (sdk.Coins, error) { -// // 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") -// } -// -// // empty min_gas_price -// if len(minGasPrices) == 0 { -// return globalFees, nil -// } -// -// // if min_gas_price denom is in globalfee, and the amount is higher than globalfee, add min_gas_price to allFees -// var allFees sdk.Coins -// for _, fee := range globalFees { -// // min_gas_price denom in global fee -// ok, c := Find(minGasPrices, fee.Denom) -// if ok && c.Amount.GT(fee.Amount) { -// allFees = append(allFees, c) -// } else { -// allFees = append(allFees, fee) -// } -// } -// -// return allFees.Sort(), nil -//} -// -//// Find replaces the functionality of Coins.Find from SDK v0.46.x +// // 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{}, errorsmod.Wrapf(gaiaerrors.ErrNotFound, "global fee cannot be empty") +// } + +// // empty min_gas_price +// if len(minGasPrices) == 0 { +// return globalFees, nil +// } + +// // if min_gas_price denom is in globalfee, and the amount is higher than globalfee, add min_gas_price to allFees +// var allFees sdk.Coins +// for _, fee := range globalFees { +// // min_gas_price denom in global fee +// ok, c := Find(minGasPrices, fee.Denom) +// if ok && c.Amount.GT(fee.Amount) { +// allFees = append(allFees, c) +// } else { +// allFees = append(allFees, fee) +// } +// } + +// return allFees.Sort(), nil +// } + +// // Find replaces the functionality of Coins.Find from SDK v0.46.x // func Find(coins sdk.Coins, denom string) (bool, sdk.Coin) { -// switch len(coins) { -// case 0: -// return false, sdk.Coin{} -// -// case 1: -// coin := coins[0] -// if coin.Denom == denom { -// return true, coin -// } -// return false, sdk.Coin{} -// -// default: -// midIdx := len(coins) / 2 // 2:1, 3:1, 4:2 -// coin := coins[midIdx] -// switch { -// case denom < coin.Denom: -// return Find(coins[:midIdx], denom) -// case denom == coin.Denom: -// return true, coin -// default: -// return Find(coins[midIdx+1:], denom) -// } -// } -//} -// -//// splitCoinsByDenoms returns the given coins split in two whether -//// their demon is or isn't found in the given denom map. +// switch len(coins) { +// case 0: +// return false, sdk.Coin{} + +// case 1: +// coin := coins[0] +// if coin.Denom == denom { +// return true, coin +// } +// return false, sdk.Coin{} + +// default: +// midIdx := len(coins) / 2 // 2:1, 3:1, 4:2 +// coin := coins[midIdx] +// switch { +// case denom < coin.Denom: +// return Find(coins[:midIdx], denom) +// case denom == coin.Denom: +// return true, coin +// default: +// return Find(coins[midIdx+1:], denom) +// } +// } +// } + +// // splitCoinsByDenoms returns the given coins split in two whether +// // their demon is or isn't found in the given denom map. // func splitCoinsByDenoms(feeCoins sdk.Coins, denomMap map[string]struct{}) (sdk.Coins, sdk.Coins) { -// feeCoinsNonZeroDenom, feeCoinsZeroDenom := sdk.Coins{}, sdk.Coins{} -// -// for _, fc := range feeCoins { -// _, found := denomMap[fc.Denom] -// if found { -// feeCoinsZeroDenom = append(feeCoinsZeroDenom, fc) -// } else { -// feeCoinsNonZeroDenom = append(feeCoinsNonZeroDenom, fc) -// } -// } -// -// return feeCoinsNonZeroDenom.Sort(), feeCoinsZeroDenom.Sort() -//} -// -//// getNonZeroFees returns the given fees nonzero coins -//// and a map storing the zero coins's denoms +// feeCoinsNonZeroDenom, feeCoinsZeroDenom := sdk.Coins{}, sdk.Coins{} + +// for _, fc := range feeCoins { +// _, found := denomMap[fc.Denom] +// if found { +// feeCoinsZeroDenom = append(feeCoinsZeroDenom, fc) +// } else { +// feeCoinsNonZeroDenom = append(feeCoinsNonZeroDenom, fc) +// } +// } + +// return feeCoinsNonZeroDenom.Sort(), feeCoinsZeroDenom.Sort() +// } + +// // getNonZeroFees returns the given fees nonzero coins +// // and a map storing the zero coins's denoms // func getNonZeroFees(fees sdk.Coins) (sdk.Coins, map[string]struct{}) { -// requiredFeesNonZero := sdk.Coins{} -// requiredFeesZeroDenom := map[string]struct{}{} -// -// for _, gf := range fees { -// if gf.IsZero() { -// requiredFeesZeroDenom[gf.Denom] = struct{}{} -// } else { -// requiredFeesNonZero = append(requiredFeesNonZero, gf) -// } -// } -// -// return requiredFeesNonZero.Sort(), requiredFeesZeroDenom -//} +// requiredFeesNonZero := sdk.Coins{} +// requiredFeesZeroDenom := map[string]struct{}{} + +// for _, gf := range fees { +// if gf.IsZero() { +// requiredFeesZeroDenom[gf.Denom] = struct{}{} +// } else { +// requiredFeesNonZero = append(requiredFeesNonZero, gf) +// } +// } + +// return requiredFeesNonZero.Sort(), requiredFeesZeroDenom +// } diff --git a/x/globalfee/types/params.go b/x/globalfee/types/params.go index 78298f52192..9e975e59676 100644 --- a/x/globalfee/types/params.go +++ b/x/globalfee/types/params.go @@ -6,8 +6,8 @@ package types // 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" // ) @@ -81,7 +81,7 @@ package types // 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) @@ -90,11 +90,11 @@ package types // type BypassMinFeeMsgTypes []string -// // validateBypassMinFeeMsgTypes checks that bypass msg types aren't empty +// validateBypassMinFeeMsgTypes checks that bypass msg types aren't empty // 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 { @@ -113,7 +113,7 @@ package types // 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