github.com/cosmos/cosmos-sdk@v0.50.10/x/crisis/keeper/msg_server.go (about)

     1  package keeper
     2  
     3  import (
     4  	"context"
     5  
     6  	"cosmossdk.io/errors"
     7  
     8  	sdk "github.com/cosmos/cosmos-sdk/types"
     9  	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
    10  	"github.com/cosmos/cosmos-sdk/x/crisis/types"
    11  	govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
    12  )
    13  
    14  var _ types.MsgServer = &Keeper{}
    15  
    16  // VerifyInvariant implements MsgServer.VerifyInvariant method.
    17  // It defines a method to verify a particular invariant.
    18  func (k *Keeper) VerifyInvariant(goCtx context.Context, msg *types.MsgVerifyInvariant) (*types.MsgVerifyInvariantResponse, error) {
    19  	if msg.Sender == "" {
    20  		return nil, sdkerrors.ErrInvalidAddress.Wrap("empty address string is not allowed")
    21  	}
    22  	sender, err := k.addressCodec.StringToBytes(msg.Sender)
    23  	if err != nil {
    24  		return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid sender address: %s", err)
    25  	}
    26  
    27  	ctx := sdk.UnwrapSDKContext(goCtx)
    28  	params, err := k.ConstantFee.Get(goCtx)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	constantFee := sdk.NewCoins(params)
    33  
    34  	if err := k.SendCoinsFromAccountToFeeCollector(ctx, sender, constantFee); err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	// use a cached context to avoid gas costs during invariants
    39  	cacheCtx, _ := ctx.CacheContext()
    40  
    41  	found := false
    42  	msgFullRoute := msg.FullInvariantRoute()
    43  
    44  	var res string
    45  	var stop bool
    46  	for _, invarRoute := range k.Routes() {
    47  		if invarRoute.FullRoute() == msgFullRoute {
    48  			res, stop = invarRoute.Invar(cacheCtx)
    49  			found = true
    50  
    51  			break
    52  		}
    53  	}
    54  
    55  	if !found {
    56  		return nil, types.ErrUnknownInvariant
    57  	}
    58  
    59  	if stop {
    60  		// Currently, because the chain halts here, this transaction will never be included in the
    61  		// blockchain thus the constant fee will have never been deducted. Thus no refund is required.
    62  
    63  		// TODO replace with circuit breaker
    64  		panic(res)
    65  	}
    66  
    67  	ctx.EventManager().EmitEvents(sdk.Events{
    68  		sdk.NewEvent(
    69  			types.EventTypeInvariant,
    70  			sdk.NewAttribute(types.AttributeKeyRoute, msg.InvariantRoute),
    71  		),
    72  	})
    73  
    74  	return &types.MsgVerifyInvariantResponse{}, nil
    75  }
    76  
    77  // UpdateParams implements MsgServer.UpdateParams method.
    78  // It defines a method to update the x/crisis module parameters.
    79  func (k *Keeper) UpdateParams(ctx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
    80  	if k.authority != msg.Authority {
    81  		return nil, errors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, msg.Authority)
    82  	}
    83  
    84  	if !msg.ConstantFee.IsValid() {
    85  		return nil, errors.Wrap(sdkerrors.ErrInvalidCoins, "invalid constant fee")
    86  	}
    87  
    88  	if msg.ConstantFee.IsNegative() {
    89  		return nil, errors.Wrap(sdkerrors.ErrInvalidCoins, "negative constant fee")
    90  	}
    91  
    92  	if err := k.ConstantFee.Set(ctx, msg.ConstantFee); err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	return &types.MsgUpdateParamsResponse{}, nil
    97  }