2019-07-19 06:43:38 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/crypto"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing/internal/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Keeper of the slashing store
|
|
|
|
type Keeper struct {
|
|
|
|
storeKey sdk.StoreKey
|
|
|
|
cdc *codec.Codec
|
|
|
|
sk types.StakingKeeper
|
|
|
|
paramspace types.ParamSubspace
|
|
|
|
codespace sdk.CodespaceType
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewKeeper creates a slashing keeper
|
|
|
|
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, sk types.StakingKeeper, paramspace types.ParamSubspace, codespace sdk.CodespaceType) Keeper {
|
|
|
|
keeper := Keeper{
|
|
|
|
storeKey: key,
|
|
|
|
cdc: cdc,
|
|
|
|
sk: sk,
|
|
|
|
paramspace: paramspace.WithKeyTable(types.ParamKeyTable()),
|
|
|
|
codespace: codespace,
|
|
|
|
}
|
|
|
|
return keeper
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logger returns a module-specific logger.
|
|
|
|
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
|
|
|
|
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddPubkey sets a address-pubkey relation
|
|
|
|
func (k Keeper) AddPubkey(ctx sdk.Context, pubkey crypto.PubKey) {
|
|
|
|
addr := pubkey.Address()
|
|
|
|
k.setAddrPubkeyRelation(ctx, addr, pubkey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPubkey returns the pubkey from the adddress-pubkey relation
|
|
|
|
func (k Keeper) GetPubkey(ctx sdk.Context, address crypto.Address) (crypto.PubKey, error) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
var pubkey crypto.PubKey
|
|
|
|
err := k.cdc.UnmarshalBinaryLengthPrefixed(store.Get(types.GetAddrPubkeyRelationKey(address)), &pubkey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("address %s not found", sdk.ConsAddress(address))
|
|
|
|
}
|
|
|
|
return pubkey, nil
|
|
|
|
}
|
|
|
|
|
2019-12-02 17:58:14 -08:00
|
|
|
// 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) {
|
|
|
|
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),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
k.sk.Slash(ctx, consAddr, distributionHeight, power, fraction)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeSlash,
|
|
|
|
sdk.NewAttribute(types.AttributeKeyJailed, consAddr.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
k.sk.Jail(ctx, consAddr)
|
|
|
|
}
|
|
|
|
|
2019-07-19 06:43:38 -07:00
|
|
|
func (k Keeper) setAddrPubkeyRelation(ctx sdk.Context, addr crypto.Address, pubkey crypto.PubKey) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
bz := k.cdc.MustMarshalBinaryLengthPrefixed(pubkey)
|
|
|
|
store.Set(types.GetAddrPubkeyRelationKey(addr), bz)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k Keeper) deleteAddrPubkeyRelation(ctx sdk.Context, addr crypto.Address) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
store.Delete(types.GetAddrPubkeyRelationKey(addr))
|
|
|
|
}
|