Skip to content

Commit

Permalink
refactor(crisis): move ValidateBasiclogic to msgServer (#15751)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Apr 8, 2023
1 parent 7a92fd0 commit 22eb756
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
22 changes: 18 additions & 4 deletions x/crisis/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/crisis/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)
Expand All @@ -15,13 +16,14 @@ var _ types.MsgServer = &Keeper{}
// VerifyInvariant implements MsgServer.VerifyInvariant method.
// It defines a method to verify a particular invariant.
func (k *Keeper) VerifyInvariant(goCtx context.Context, msg *types.MsgVerifyInvariant) (*types.MsgVerifyInvariantResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
constantFee := sdk.NewCoins(k.GetConstantFee(ctx))

sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return nil, err
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid sender address: %s", err)
}

ctx := sdk.UnwrapSDKContext(goCtx)
constantFee := sdk.NewCoins(k.GetConstantFee(ctx))

if err := k.SendCoinsFromAccountToFeeCollector(ctx, sender, constantFee); err != nil {
return nil, err
}
Expand Down Expand Up @@ -68,10 +70,22 @@ func (k *Keeper) VerifyInvariant(goCtx context.Context, msg *types.MsgVerifyInva
// UpdateParams implements MsgServer.UpdateParams method.
// It defines a method to update the x/crisis module parameters.
func (k *Keeper) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if _, err := sdk.AccAddressFromBech32(req.Authority); err != nil {
return nil, errors.Wrap(err, "invalid authority address")
}

if k.authority != req.Authority {
return nil, errors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, req.Authority)
}

if !req.ConstantFee.IsValid() {
return nil, errors.Wrap(sdkerrors.ErrInvalidCoins, "invalid constant fee")
}

if req.ConstantFee.IsNegative() {
return nil, errors.Wrap(sdkerrors.ErrInvalidCoins, "negative constant fee")
}

ctx := sdk.UnwrapSDKContext(goCtx)
if err := k.SetConstantFee(ctx, req.ConstantFee); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion x/crisis/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s *KeeperTestSuite) SetupTest() {
key := storetypes.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test"))
encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{})
keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", "")
keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", sdk.AccAddress([]byte("addr1_______________")).String())

s.ctx = testCtx.Ctx
s.keeper = keeper
Expand Down
28 changes: 0 additions & 28 deletions x/crisis/types/msgs.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package types

import (
errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
)

Expand Down Expand Up @@ -35,14 +32,6 @@ func (msg MsgVerifyInvariant) GetSignBytes() []byte {
return sdk.MustSortJSON(bz)
}

// quick validity check
func (msg MsgVerifyInvariant) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid sender address: %s", err)
}
return nil
}

// FullInvariantRoute - get the messages full invariant route
func (msg MsgVerifyInvariant) FullInvariantRoute() string {
return msg.InvariantModuleName + "/" + msg.InvariantRoute
Expand All @@ -61,20 +50,3 @@ func (msg MsgUpdateParams) GetSignBytes() []byte {
bz := aminoCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}

// ValidateBasic performs basic MsgUpdateParams message validation.
func (msg MsgUpdateParams) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Authority); err != nil {
return errorsmod.Wrap(err, "invalid authority address")
}

if !msg.ConstantFee.IsValid() {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, "invalid costant fee")
}

if msg.ConstantFee.IsNegative() {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, "negative costant fee")
}

return nil
}

0 comments on commit 22eb756

Please sign in to comment.