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

     1  package keeper
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	storetypes "cosmossdk.io/core/store"
     8  	"cosmossdk.io/log"
     9  	sdkmath "cosmossdk.io/math"
    10  
    11  	"github.com/cosmos/cosmos-sdk/codec"
    12  	cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
    13  	sdk "github.com/cosmos/cosmos-sdk/types"
    14  	"github.com/cosmos/cosmos-sdk/x/slashing/types"
    15  	stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
    16  )
    17  
    18  // Keeper of the slashing store
    19  type Keeper struct {
    20  	storeService storetypes.KVStoreService
    21  	cdc          codec.BinaryCodec
    22  	legacyAmino  *codec.LegacyAmino
    23  	sk           types.StakingKeeper
    24  
    25  	// the address capable of executing a MsgUpdateParams message. Typically, this
    26  	// should be the x/gov module account.
    27  	authority string
    28  }
    29  
    30  // NewKeeper creates a slashing keeper
    31  func NewKeeper(cdc codec.BinaryCodec, legacyAmino *codec.LegacyAmino, storeService storetypes.KVStoreService, sk types.StakingKeeper, authority string) Keeper {
    32  	return Keeper{
    33  		storeService: storeService,
    34  		cdc:          cdc,
    35  		legacyAmino:  legacyAmino,
    36  		sk:           sk,
    37  		authority:    authority,
    38  	}
    39  }
    40  
    41  // GetAuthority returns the x/slashing module's authority.
    42  func (k Keeper) GetAuthority() string {
    43  	return k.authority
    44  }
    45  
    46  // Logger returns a module-specific logger.
    47  func (k Keeper) Logger(ctx context.Context) log.Logger {
    48  	sdkCtx := sdk.UnwrapSDKContext(ctx)
    49  	return sdkCtx.Logger().With("module", "x/"+types.ModuleName)
    50  }
    51  
    52  // AddPubkey sets a address-pubkey relation
    53  func (k Keeper) AddPubkey(ctx context.Context, pubkey cryptotypes.PubKey) error {
    54  	bz, err := k.cdc.MarshalInterface(pubkey)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	store := k.storeService.OpenKVStore(ctx)
    59  	key := types.AddrPubkeyRelationKey(pubkey.Address())
    60  	return store.Set(key, bz)
    61  }
    62  
    63  // GetPubkey returns the pubkey from the adddress-pubkey relation
    64  func (k Keeper) GetPubkey(ctx context.Context, a cryptotypes.Address) (cryptotypes.PubKey, error) {
    65  	store := k.storeService.OpenKVStore(ctx)
    66  	bz, err := store.Get(types.AddrPubkeyRelationKey(a))
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	if bz == nil {
    71  		return nil, fmt.Errorf("address %s not found", sdk.ConsAddress(a))
    72  	}
    73  	var pk cryptotypes.PubKey
    74  	return pk, k.cdc.UnmarshalInterface(bz, &pk)
    75  }
    76  
    77  // Slash attempts to slash a validator. The slash is delegated to the staking
    78  // module to make the necessary validator changes. It specifies no intraction reason.
    79  func (k Keeper) Slash(ctx context.Context, consAddr sdk.ConsAddress, fraction sdkmath.LegacyDec, power, distributionHeight int64) error {
    80  	return k.SlashWithInfractionReason(ctx, consAddr, fraction, power, distributionHeight, stakingtypes.Infraction_INFRACTION_UNSPECIFIED)
    81  }
    82  
    83  // SlashWithInfractionReason attempts to slash a validator. The slash is delegated to the staking
    84  // module to make the necessary validator changes. It specifies an intraction reason.
    85  func (k Keeper) SlashWithInfractionReason(ctx context.Context, consAddr sdk.ConsAddress, fraction sdkmath.LegacyDec, power, distributionHeight int64, infraction stakingtypes.Infraction) error {
    86  	coinsBurned, err := k.sk.SlashWithInfractionReason(ctx, consAddr, distributionHeight, power, fraction, infraction)
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	reasonAttr := sdk.NewAttribute(types.AttributeKeyReason, types.AttributeValueUnspecified)
    92  	switch infraction {
    93  	case stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN:
    94  		reasonAttr = sdk.NewAttribute(types.AttributeKeyReason, types.AttributeValueDoubleSign)
    95  	case stakingtypes.Infraction_INFRACTION_DOWNTIME:
    96  		reasonAttr = sdk.NewAttribute(types.AttributeKeyReason, types.AttributeValueMissingSignature)
    97  	}
    98  
    99  	sdkCtx := sdk.UnwrapSDKContext(ctx)
   100  	sdkCtx.EventManager().EmitEvent(
   101  		sdk.NewEvent(
   102  			types.EventTypeSlash,
   103  			sdk.NewAttribute(types.AttributeKeyAddress, consAddr.String()),
   104  			sdk.NewAttribute(types.AttributeKeyPower, fmt.Sprintf("%d", power)),
   105  			reasonAttr,
   106  			sdk.NewAttribute(types.AttributeKeyBurnedCoins, coinsBurned.String()),
   107  		),
   108  	)
   109  	return nil
   110  }
   111  
   112  // Jail attempts to jail a validator. The slash is delegated to the staking module
   113  // to make the necessary validator changes.
   114  func (k Keeper) Jail(ctx context.Context, consAddr sdk.ConsAddress) error {
   115  	sdkCtx := sdk.UnwrapSDKContext(ctx)
   116  	k.sk.Jail(sdkCtx, consAddr)
   117  	sdkCtx.EventManager().EmitEvent(
   118  		sdk.NewEvent(
   119  			types.EventTypeSlash,
   120  			sdk.NewAttribute(types.AttributeKeyJailed, consAddr.String()),
   121  		),
   122  	)
   123  	return nil
   124  }
   125  
   126  func (k Keeper) deleteAddrPubkeyRelation(ctx context.Context, addr cryptotypes.Address) error {
   127  	store := k.storeService.OpenKVStore(ctx)
   128  	return store.Delete(types.AddrPubkeyRelationKey(addr))
   129  }