Skip to content

Commit

Permalink
refactor: errors module (#2471)
Browse files Browse the repository at this point in the history
* use the errors module to ease 47 transition

* golangci-lint run ./... --fix

---------

Co-authored-by: Marius Poke <marius.poke@posteo.de>
  • Loading branch information
2 people authored and lg committed Jun 30, 2023
1 parent ba957f7 commit 71e75e1
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 222 deletions.
18 changes: 9 additions & 9 deletions ante/ante.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ante

import (
"cosmossdk.io/errors"
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"
Expand All @@ -25,26 +25,26 @@ type HandlerOptions struct {

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

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

import (
"cosmossdk.io/errors"
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"
Expand Down Expand Up @@ -50,7 +50,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 errors.Wrapf(sdkerrors.ErrInsufficientFunds, "insufficient initial deposit amount - required: %v", minInitialDeposit)
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, "insufficient initial deposit amount - required: %v", minInitialDeposit)
}
}

Expand All @@ -61,7 +61,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 errors.Wrapf(sdkerrors.ErrUnauthorized, "cannot unmarshal authz exec msgs")
return errorsmod.Wrapf(sdkerrors.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 @@ -12,6 +12,7 @@ require (
github.com/cosmos/gogoproto v1.4.8
github.com/cosmos/ibc-go/v7 v7.0.0
github.com/cosmos/interchain-security v1.0.1-0.20230522154154-1bb8d39e691a
cosmossdk.io/errors v1.0.0-beta.7
cosmossdk.io/math v1.0.1
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/interchain-security/v2 v2.0.0-rc2
Expand Down
48 changes: 25 additions & 23 deletions x/globalfee/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ package ante
// "errors"
// "fmt"

// 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"
// tmstrings "github.com/tendermint/tendermint/libs/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"
// stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
// tmstrings "github.com/tendermint/tendermint/libs/strings"

// "github.com/cosmos/gaia/v11/x/globalfee"
// "github.com/cosmos/gaia/v11/x/globalfee/types"
Expand Down Expand Up @@ -47,11 +48,11 @@ package ante
// }
// }

// // AnteHandle implements the AnteDecorator interface
// AnteHandle implements the AnteDecorator interface
// 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(sdkerrors.ErrTxDecode, "Tx must implement the sdk.FeeTx interface")
// }

// // Do not check minimum-gas-prices and global fees during simulations
Expand All @@ -65,11 +66,12 @@ package ante
// return ctx, err
// }

// // 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())
// }
// 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, errorsmod.Wrapf(sdkerrors.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
// feeCoins := feeTx.GetFee().Sort()
Expand Down Expand Up @@ -112,18 +114,18 @@ package ante
// return next(ctx, tx, simulate)
// }

// // if the msg does not satisfy bypass condition and the feeCoins denoms are subset of feeRequired,
// // check the feeCoins amount against feeRequired
// //
// // when feeCoins=[]
// // special case: and there is zero coin in fee requirement, pass,
// // otherwise, err
// if len(feeCoins) == 0 {
// 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())
// if the msg does not satisfy bypass condition and the feeCoins denoms are subset of feeRequired,
// check the feeCoins amount against feeRequired
//
// when feeCoins=[]
// special case: and there is zero coin in fee requirement, pass,
// otherwise, err
// if len(feeCoins) == 0 {
// 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())
// }

// // when feeCoins != []
// // special case: if TX has at least one of the zeroCoinFeesDenomReq, then it should pass
Expand Down
24 changes: 12 additions & 12 deletions x/globalfee/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ package globalfee
// "encoding/json"
// "fmt"

// "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"
// "github.com/grpc-ecosystem/grpc-gateway/runtime"
// "github.com/spf13/cobra"
// abci "github.com/tendermint/tendermint/abci/types"
// 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"
// "github.com/cosmos/cosmos-sdk/types/module"
// paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
// "github.com/gorilla/mux"
// "github.com/grpc-ecosystem/grpc-gateway/runtime"
// "github.com/spf13/cobra"
// abci "github.com/tendermint/tendermint/abci/types"

// "github.com/cosmos/gaia/v11/x/globalfee/client/cli"
// "github.com/cosmos/gaia/v11/x/globalfee/keeper"
Expand Down Expand Up @@ -48,7 +48,7 @@ package globalfee
// return err
// }
// if err := data.Params.ValidateBasic(); err != nil {
// return sdkerrors.Wrap(err, "params")
// return errorsmod.Wrap(err, "params")
// }
// return nil
// }
Expand Down
67 changes: 33 additions & 34 deletions x/globalfee/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
package types

//
// import (
// "encoding/json"
//
// "github.com/cosmos/cosmos-sdk/codec"
// sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
//)
//
//// NewGenesisState - Create a new genesis state
// "encoding/json"

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

// // NewGenesisState - Create a new genesis state
// func NewGenesisState(params Params) *GenesisState {
// return &GenesisState{
// Params: params,
// }
//}
//
//// DefaultGenesisState - Return a default genesis state
// return &GenesisState{
// Params: params,
// }
// }

// // DefaultGenesisState - Return a default genesis state
// func DefaultGenesisState() *GenesisState {
// return NewGenesisState(DefaultParams())
//}
//
//// GetGenesisStateFromAppState returns x/auth GenesisState given raw application
//// genesis state.
// return NewGenesisState(DefaultParams())
// }

// // GetGenesisStateFromAppState returns x/auth GenesisState given raw application
// // genesis state.
// func GetGenesisStateFromAppState(cdc codec.Codec, appState map[string]json.RawMessage) *GenesisState {
// var genesisState GenesisState
//
// if appState[ModuleName] != nil {
// cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
// }
//
// return &genesisState
//}
//
// var genesisState GenesisState

// if appState[ModuleName] != nil {
// cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
// }

// return &genesisState
// }

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

// return nil
// }
Loading

0 comments on commit 71e75e1

Please sign in to comment.