github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/slashing/internal/keeper/keeper.go (about)

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