## Description Routine gofumpting. No changelog entry because no changes to how the code functions. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
102 lines
3.1 KiB
Go
102 lines
3.1 KiB
Go
package keeper
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/x/slashing/types"
|
|
)
|
|
|
|
// Keeper of the slashing store
|
|
type Keeper struct {
|
|
storeKey storetypes.StoreKey
|
|
cdc codec.BinaryCodec
|
|
legacyAmino *codec.LegacyAmino
|
|
sk types.StakingKeeper
|
|
|
|
// the address capable of executing a MsgUpdateParams message. Typically, this
|
|
// should be the x/gov module account.
|
|
authority string
|
|
}
|
|
|
|
// NewKeeper creates a slashing keeper
|
|
func NewKeeper(cdc codec.BinaryCodec, legacyAmino *codec.LegacyAmino, key storetypes.StoreKey, sk types.StakingKeeper, authority string) Keeper {
|
|
return Keeper{
|
|
storeKey: key,
|
|
cdc: cdc,
|
|
legacyAmino: legacyAmino,
|
|
sk: sk,
|
|
authority: authority,
|
|
}
|
|
}
|
|
|
|
// GetAuthority returns the x/slashing module's authority.
|
|
func (k Keeper) GetAuthority() string {
|
|
return k.authority
|
|
}
|
|
|
|
// Logger returns a module-specific logger.
|
|
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
|
|
return ctx.Logger().With("module", "x/"+types.ModuleName)
|
|
}
|
|
|
|
// AddPubkey sets a address-pubkey relation
|
|
func (k Keeper) AddPubkey(ctx sdk.Context, pubkey cryptotypes.PubKey) error {
|
|
bz, err := k.cdc.MarshalInterface(pubkey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
store := ctx.KVStore(k.storeKey)
|
|
key := types.AddrPubkeyRelationKey(pubkey.Address())
|
|
store.Set(key, bz)
|
|
return nil
|
|
}
|
|
|
|
// GetPubkey returns the pubkey from the adddress-pubkey relation
|
|
func (k Keeper) GetPubkey(ctx sdk.Context, a cryptotypes.Address) (cryptotypes.PubKey, error) {
|
|
store := ctx.KVStore(k.storeKey)
|
|
bz := store.Get(types.AddrPubkeyRelationKey(a))
|
|
if bz == nil {
|
|
return nil, fmt.Errorf("address %s not found", sdk.ConsAddress(a))
|
|
}
|
|
var pk cryptotypes.PubKey
|
|
return pk, k.cdc.UnmarshalInterface(bz, &pk)
|
|
}
|
|
|
|
// Slash attempts to slash a validator. The slash is delegated to the staking
|
|
// module to make the necessary validator changes.
|
|
func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, fraction sdk.Dec, power, distributionHeight int64) {
|
|
coinsBurned := k.sk.Slash(ctx, consAddr, distributionHeight, power, fraction)
|
|
ctx.EventManager().EmitEvent(
|
|
sdk.NewEvent(
|
|
types.EventTypeSlash,
|
|
sdk.NewAttribute(types.AttributeKeyAddress, consAddr.String()),
|
|
sdk.NewAttribute(types.AttributeKeyPower, fmt.Sprintf("%d", power)),
|
|
sdk.NewAttribute(types.AttributeKeyReason, types.AttributeValueDoubleSign),
|
|
sdk.NewAttribute(types.AttributeKeyBurnedCoins, coinsBurned.String()),
|
|
),
|
|
)
|
|
}
|
|
|
|
// Jail attempts to jail a validator. The slash is delegated to the staking module
|
|
// to make the necessary validator changes.
|
|
func (k Keeper) Jail(ctx sdk.Context, consAddr sdk.ConsAddress) {
|
|
k.sk.Jail(ctx, consAddr)
|
|
ctx.EventManager().EmitEvent(
|
|
sdk.NewEvent(
|
|
types.EventTypeSlash,
|
|
sdk.NewAttribute(types.AttributeKeyJailed, consAddr.String()),
|
|
),
|
|
)
|
|
}
|
|
|
|
func (k Keeper) deleteAddrPubkeyRelation(ctx sdk.Context, addr cryptotypes.Address) {
|
|
store := ctx.KVStore(k.storeKey)
|
|
store.Delete(types.AddrPubkeyRelationKey(addr))
|
|
}
|