github.com/Finschia/finschia-sdk@v0.48.1/x/slashing/keeper/keeper.go (about)

     1  package keeper
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Finschia/ostracon/libs/log"
     7  
     8  	"github.com/Finschia/finschia-sdk/codec"
     9  	cryptotypes "github.com/Finschia/finschia-sdk/crypto/types"
    10  	sdk "github.com/Finschia/finschia-sdk/types"
    11  	"github.com/Finschia/finschia-sdk/x/slashing/types"
    12  )
    13  
    14  // Keeper of the slashing store
    15  type Keeper struct {
    16  	storeKey   sdk.StoreKey
    17  	cdc        codec.BinaryCodec
    18  	sk         types.StakingKeeper
    19  	paramspace types.ParamSubspace
    20  }
    21  
    22  // NewKeeper creates a slashing keeper
    23  func NewKeeper(cdc codec.BinaryCodec, key sdk.StoreKey, sk types.StakingKeeper, paramspace types.ParamSubspace) Keeper {
    24  	// set KeyTable if it has not already been set
    25  	if !paramspace.HasKeyTable() {
    26  		paramspace = paramspace.WithKeyTable(types.ParamKeyTable())
    27  	}
    28  
    29  	return Keeper{
    30  		storeKey:   key,
    31  		cdc:        cdc,
    32  		sk:         sk,
    33  		paramspace: paramspace,
    34  	}
    35  }
    36  
    37  // Logger returns a module-specific logger.
    38  func (k Keeper) Logger(ctx sdk.Context) log.Logger {
    39  	return ctx.Logger().With("module", "x/"+types.ModuleName)
    40  }
    41  
    42  // AddPubkey sets a address-pubkey relation
    43  func (k Keeper) AddPubkey(ctx sdk.Context, pubkey cryptotypes.PubKey) error {
    44  	bz, err := k.cdc.MarshalInterface(pubkey)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	store := ctx.KVStore(k.storeKey)
    49  	key := types.AddrPubkeyRelationKey(pubkey.Address())
    50  	store.Set(key, bz)
    51  	return nil
    52  }
    53  
    54  // GetPubkey returns the pubkey from the adddress-pubkey relation
    55  func (k Keeper) GetPubkey(ctx sdk.Context, a cryptotypes.Address) (cryptotypes.PubKey, error) {
    56  	store := ctx.KVStore(k.storeKey)
    57  	bz := store.Get(types.AddrPubkeyRelationKey(a))
    58  	if bz == nil {
    59  		return nil, fmt.Errorf("address %s not found", sdk.ConsAddress(a))
    60  	}
    61  	var pk cryptotypes.PubKey
    62  	return pk, k.cdc.UnmarshalInterface(bz, &pk)
    63  }
    64  
    65  // Slash attempts to slash a validator. The slash is delegated to the staking
    66  // module to make the necessary validator changes.
    67  func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, fraction sdk.Dec, power, distributionHeight int64) {
    68  	ctx.EventManager().EmitEvent(
    69  		sdk.NewEvent(
    70  			types.EventTypeSlash,
    71  			sdk.NewAttribute(types.AttributeKeyAddress, consAddr.String()),
    72  			sdk.NewAttribute(types.AttributeKeyPower, fmt.Sprintf("%d", power)),
    73  			sdk.NewAttribute(types.AttributeKeyReason, types.AttributeValueDoubleSign),
    74  		),
    75  	)
    76  
    77  	k.sk.Slash(ctx, consAddr, distributionHeight, power, fraction)
    78  }
    79  
    80  // Jail attempts to jail a validator. The slash is delegated to the staking module
    81  // to make the necessary validator changes.
    82  func (k Keeper) Jail(ctx sdk.Context, consAddr sdk.ConsAddress) {
    83  	ctx.EventManager().EmitEvent(
    84  		sdk.NewEvent(
    85  			types.EventTypeSlash,
    86  			sdk.NewAttribute(types.AttributeKeyJailed, consAddr.String()),
    87  		),
    88  	)
    89  
    90  	k.sk.Jail(ctx, consAddr)
    91  }
    92  
    93  func (k Keeper) deleteAddrPubkeyRelation(ctx sdk.Context, addr cryptotypes.Address) {
    94  	store := ctx.KVStore(k.storeKey)
    95  	store.Delete(types.AddrPubkeyRelationKey(addr))
    96  }