<!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Adds the amount of coins slashed from the validator to the `Slash` event. Additionally, before this PR, the jail/slash events were emitted **BEFORE** the slash/jail functions were even ran, which could result in a false positive event if an error occurred in these functions. ~~These events were moved to the end of the functions that implement the logic for them instead.~~ This PR moves the events to be emitted after the logic is executed. Closes: #9138 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> - +Add `amount_slashed` to slash event - +Slash now returns the amount of tokens burned - +Events moved to be emitted after the logic is executed - +Add test to test the slash return amount ~~- +Add EventType `Jail` to separate it from the `Slash` event~~ ~~- +Move slash/jail events into the functions that execute the logic for it~~ ~~- -Remove `Reason` attribute from slash event (didn't appear to be consistent with what was happening in code)~~ ### 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... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] 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... - [x] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - @ryanchristo - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [x] reviewed state machine logic - not applicable - [x] reviewed API design and naming - @ryanchristo - [x] reviewed documentation is accurate - @ryanchristo - [x] reviewed tests and test coverage - @ryanchristo - [ ] manually tested (if applicable)
301 lines
10 KiB
Go
301 lines
10 KiB
Go
package keeper
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
types "github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
)
|
|
|
|
// Slash a validator for an infraction committed at a known height
|
|
// Find the contributing stake at that height and burn the specified slashFactor
|
|
// of it, updating unbonding delegations & redelegations appropriately
|
|
//
|
|
// CONTRACT:
|
|
// slashFactor is non-negative
|
|
// CONTRACT:
|
|
// Infraction was committed equal to or less than an unbonding period in the past,
|
|
// so all unbonding delegations and redelegations from that height are stored
|
|
// CONTRACT:
|
|
// Slash will not slash unbonded validators (for the above reason)
|
|
// CONTRACT:
|
|
// Infraction was committed at the current height or at a past height,
|
|
// not at a height in the future
|
|
func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeight int64, power int64, slashFactor sdk.Dec) sdk.Int {
|
|
logger := k.Logger(ctx)
|
|
|
|
if slashFactor.IsNegative() {
|
|
panic(fmt.Errorf("attempted to slash with a negative slash factor: %v", slashFactor))
|
|
}
|
|
|
|
// Amount of slashing = slash slashFactor * power at time of infraction
|
|
amount := k.TokensFromConsensusPower(ctx, power)
|
|
slashAmountDec := amount.ToDec().Mul(slashFactor)
|
|
slashAmount := slashAmountDec.TruncateInt()
|
|
|
|
// ref https://github.com/cosmos/cosmos-sdk/issues/1348
|
|
|
|
validator, found := k.GetValidatorByConsAddr(ctx, consAddr)
|
|
if !found {
|
|
// If not found, the validator must have been overslashed and removed - so we don't need to do anything
|
|
// NOTE: Correctness dependent on invariant that unbonding delegations / redelegations must also have been completely
|
|
// slashed in this case - which we don't explicitly check, but should be true.
|
|
// Log the slash attempt for future reference (maybe we should tag it too)
|
|
logger.Error(
|
|
"WARNING: ignored attempt to slash a nonexistent validator; we recommend you investigate immediately",
|
|
"validator", consAddr.String(),
|
|
)
|
|
return sdk.NewInt(0)
|
|
}
|
|
|
|
// should not be slashing an unbonded validator
|
|
if validator.IsUnbonded() {
|
|
panic(fmt.Sprintf("should not be slashing unbonded validator: %s", validator.GetOperator()))
|
|
}
|
|
|
|
operatorAddress := validator.GetOperator()
|
|
|
|
// call the before-modification hook
|
|
k.BeforeValidatorModified(ctx, operatorAddress)
|
|
|
|
// Track remaining slash amount for the validator
|
|
// This will decrease when we slash unbondings and
|
|
// redelegations, as that stake has since unbonded
|
|
remainingSlashAmount := slashAmount
|
|
|
|
switch {
|
|
case infractionHeight > ctx.BlockHeight():
|
|
// Can't slash infractions in the future
|
|
panic(fmt.Sprintf(
|
|
"impossible attempt to slash future infraction at height %d but we are at height %d",
|
|
infractionHeight, ctx.BlockHeight()))
|
|
|
|
case infractionHeight == ctx.BlockHeight():
|
|
// Special-case slash at current height for efficiency - we don't need to
|
|
// look through unbonding delegations or redelegations.
|
|
logger.Info(
|
|
"slashing at current height; not scanning unbonding delegations & redelegations",
|
|
"height", infractionHeight,
|
|
)
|
|
|
|
case infractionHeight < ctx.BlockHeight():
|
|
// Iterate through unbonding delegations from slashed validator
|
|
unbondingDelegations := k.GetUnbondingDelegationsFromValidator(ctx, operatorAddress)
|
|
for _, unbondingDelegation := range unbondingDelegations {
|
|
amountSlashed := k.SlashUnbondingDelegation(ctx, unbondingDelegation, infractionHeight, slashFactor)
|
|
if amountSlashed.IsZero() {
|
|
continue
|
|
}
|
|
|
|
remainingSlashAmount = remainingSlashAmount.Sub(amountSlashed)
|
|
}
|
|
|
|
// Iterate through redelegations from slashed source validator
|
|
redelegations := k.GetRedelegationsFromSrcValidator(ctx, operatorAddress)
|
|
for _, redelegation := range redelegations {
|
|
amountSlashed := k.SlashRedelegation(ctx, validator, redelegation, infractionHeight, slashFactor)
|
|
if amountSlashed.IsZero() {
|
|
continue
|
|
}
|
|
|
|
remainingSlashAmount = remainingSlashAmount.Sub(amountSlashed)
|
|
}
|
|
}
|
|
|
|
// cannot decrease balance below zero
|
|
tokensToBurn := sdk.MinInt(remainingSlashAmount, validator.Tokens)
|
|
tokensToBurn = sdk.MaxInt(tokensToBurn, sdk.ZeroInt()) // defensive.
|
|
|
|
// we need to calculate the *effective* slash fraction for distribution
|
|
if validator.Tokens.IsPositive() {
|
|
effectiveFraction := tokensToBurn.ToDec().QuoRoundUp(validator.Tokens.ToDec())
|
|
// possible if power has changed
|
|
if effectiveFraction.GT(sdk.OneDec()) {
|
|
effectiveFraction = sdk.OneDec()
|
|
}
|
|
// call the before-slashed hook
|
|
k.BeforeValidatorSlashed(ctx, operatorAddress, effectiveFraction)
|
|
}
|
|
|
|
// Deduct from validator's bonded tokens and update the validator.
|
|
// Burn the slashed tokens from the pool account and decrease the total supply.
|
|
validator = k.RemoveValidatorTokens(ctx, validator, tokensToBurn)
|
|
|
|
switch validator.GetStatus() {
|
|
case types.Bonded:
|
|
if err := k.burnBondedTokens(ctx, tokensToBurn); err != nil {
|
|
panic(err)
|
|
}
|
|
case types.Unbonding, types.Unbonded:
|
|
if err := k.burnNotBondedTokens(ctx, tokensToBurn); err != nil {
|
|
panic(err)
|
|
}
|
|
default:
|
|
panic("invalid validator status")
|
|
}
|
|
|
|
logger.Info(
|
|
"validator slashed by slash factor",
|
|
"validator", validator.GetOperator().String(),
|
|
"slash_factor", slashFactor.String(),
|
|
"burned", tokensToBurn,
|
|
)
|
|
return tokensToBurn
|
|
}
|
|
|
|
// jail a validator
|
|
func (k Keeper) Jail(ctx sdk.Context, consAddr sdk.ConsAddress) {
|
|
validator := k.mustGetValidatorByConsAddr(ctx, consAddr)
|
|
k.jailValidator(ctx, validator)
|
|
logger := k.Logger(ctx)
|
|
logger.Info("validator jailed", "validator", consAddr)
|
|
}
|
|
|
|
// unjail a validator
|
|
func (k Keeper) Unjail(ctx sdk.Context, consAddr sdk.ConsAddress) {
|
|
validator := k.mustGetValidatorByConsAddr(ctx, consAddr)
|
|
k.unjailValidator(ctx, validator)
|
|
logger := k.Logger(ctx)
|
|
logger.Info("validator un-jailed", "validator", consAddr)
|
|
}
|
|
|
|
// slash an unbonding delegation and update the pool
|
|
// return the amount that would have been slashed assuming
|
|
// the unbonding delegation had enough stake to slash
|
|
// (the amount actually slashed may be less if there's
|
|
// insufficient stake remaining)
|
|
func (k Keeper) SlashUnbondingDelegation(ctx sdk.Context, unbondingDelegation types.UnbondingDelegation,
|
|
infractionHeight int64, slashFactor sdk.Dec) (totalSlashAmount sdk.Int) {
|
|
now := ctx.BlockHeader().Time
|
|
totalSlashAmount = sdk.ZeroInt()
|
|
burnedAmount := sdk.ZeroInt()
|
|
|
|
// perform slashing on all entries within the unbonding delegation
|
|
for i, entry := range unbondingDelegation.Entries {
|
|
// If unbonding started before this height, stake didn't contribute to infraction
|
|
if entry.CreationHeight < infractionHeight {
|
|
continue
|
|
}
|
|
|
|
if entry.IsMature(now) {
|
|
// Unbonding delegation no longer eligible for slashing, skip it
|
|
continue
|
|
}
|
|
|
|
// Calculate slash amount proportional to stake contributing to infraction
|
|
slashAmountDec := slashFactor.MulInt(entry.InitialBalance)
|
|
slashAmount := slashAmountDec.TruncateInt()
|
|
totalSlashAmount = totalSlashAmount.Add(slashAmount)
|
|
|
|
// Don't slash more tokens than held
|
|
// Possible since the unbonding delegation may already
|
|
// have been slashed, and slash amounts are calculated
|
|
// according to stake held at time of infraction
|
|
unbondingSlashAmount := sdk.MinInt(slashAmount, entry.Balance)
|
|
|
|
// Update unbonding delegation if necessary
|
|
if unbondingSlashAmount.IsZero() {
|
|
continue
|
|
}
|
|
|
|
burnedAmount = burnedAmount.Add(unbondingSlashAmount)
|
|
entry.Balance = entry.Balance.Sub(unbondingSlashAmount)
|
|
unbondingDelegation.Entries[i] = entry
|
|
k.SetUnbondingDelegation(ctx, unbondingDelegation)
|
|
}
|
|
|
|
if err := k.burnNotBondedTokens(ctx, burnedAmount); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return totalSlashAmount
|
|
}
|
|
|
|
// slash a redelegation and update the pool
|
|
// return the amount that would have been slashed assuming
|
|
// the unbonding delegation had enough stake to slash
|
|
// (the amount actually slashed may be less if there's
|
|
// insufficient stake remaining)
|
|
// NOTE this is only slashing for prior infractions from the source validator
|
|
func (k Keeper) SlashRedelegation(ctx sdk.Context, srcValidator types.Validator, redelegation types.Redelegation,
|
|
infractionHeight int64, slashFactor sdk.Dec) (totalSlashAmount sdk.Int) {
|
|
now := ctx.BlockHeader().Time
|
|
totalSlashAmount = sdk.ZeroInt()
|
|
bondedBurnedAmount, notBondedBurnedAmount := sdk.ZeroInt(), sdk.ZeroInt()
|
|
|
|
// perform slashing on all entries within the redelegation
|
|
for _, entry := range redelegation.Entries {
|
|
// If redelegation started before this height, stake didn't contribute to infraction
|
|
if entry.CreationHeight < infractionHeight {
|
|
continue
|
|
}
|
|
|
|
if entry.IsMature(now) {
|
|
// Redelegation no longer eligible for slashing, skip it
|
|
continue
|
|
}
|
|
|
|
// Calculate slash amount proportional to stake contributing to infraction
|
|
slashAmountDec := slashFactor.MulInt(entry.InitialBalance)
|
|
slashAmount := slashAmountDec.TruncateInt()
|
|
totalSlashAmount = totalSlashAmount.Add(slashAmount)
|
|
|
|
// Unbond from target validator
|
|
sharesToUnbond := slashFactor.Mul(entry.SharesDst)
|
|
if sharesToUnbond.IsZero() {
|
|
continue
|
|
}
|
|
|
|
valDstAddr, err := sdk.ValAddressFromBech32(redelegation.ValidatorDstAddress)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
delegatorAddress, err := sdk.AccAddressFromBech32(redelegation.DelegatorAddress)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
delegation, found := k.GetDelegation(ctx, delegatorAddress, valDstAddr)
|
|
if !found {
|
|
// If deleted, delegation has zero shares, and we can't unbond any more
|
|
continue
|
|
}
|
|
|
|
if sharesToUnbond.GT(delegation.Shares) {
|
|
sharesToUnbond = delegation.Shares
|
|
}
|
|
|
|
tokensToBurn, err := k.Unbond(ctx, delegatorAddress, valDstAddr, sharesToUnbond)
|
|
if err != nil {
|
|
panic(fmt.Errorf("error unbonding delegator: %v", err))
|
|
}
|
|
|
|
dstValidator, found := k.GetValidator(ctx, valDstAddr)
|
|
if !found {
|
|
panic("destination validator not found")
|
|
}
|
|
|
|
// tokens of a redelegation currently live in the destination validator
|
|
// therefor we must burn tokens from the destination-validator's bonding status
|
|
switch {
|
|
case dstValidator.IsBonded():
|
|
bondedBurnedAmount = bondedBurnedAmount.Add(tokensToBurn)
|
|
case dstValidator.IsUnbonded() || dstValidator.IsUnbonding():
|
|
notBondedBurnedAmount = notBondedBurnedAmount.Add(tokensToBurn)
|
|
default:
|
|
panic("unknown validator status")
|
|
}
|
|
}
|
|
|
|
if err := k.burnBondedTokens(ctx, bondedBurnedAmount); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if err := k.burnNotBondedTokens(ctx, notBondedBurnedAmount); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return totalSlashAmount
|
|
}
|