Skip to content

Commit

Permalink
return the error
Browse files Browse the repository at this point in the history
  • Loading branch information
insumity committed Sep 26, 2023
1 parent 1d3ee78 commit 5c053bb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
8 changes: 6 additions & 2 deletions x/ccv/provider/keeper/double_vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ func (k Keeper) HandleConsumerDoubleVoting(
types.NewConsumerConsAddress(sdk.ConsAddress(evidence.VoteA.ValidatorAddress.Bytes())),
)

k.SlashValidator(ctx, providerAddr)
k.JailAndTombstoneValidator(ctx, providerAddr)
if err := k.SlashValidator(ctx, providerAddr); err != nil {
return err
}
if err := k.JailAndTombstoneValidator(ctx, providerAddr); err != nil {
return err
}

k.Logger(ctx).Info(
"confirmed equivocation",
Expand Down
15 changes: 13 additions & 2 deletions x/ccv/provider/keeper/misbehaviour.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"fmt"

Check failure on line 4 in x/ccv/provider/keeper/misbehaviour.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
"github.com/cosmos/interchain-security/v2/x/ccv/provider/types"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -34,15 +35,22 @@ func (k Keeper) HandleConsumerMisbehaviour(ctx sdk.Context, misbehaviour ibctmty

provAddrs := make([]types.ProviderConsAddress, len(byzantineValidators))

var errors []error
// slash, jail, and tombstone the Byzantine validators
for _, v := range byzantineValidators {
providerAddr := k.GetProviderAddrFromConsumerAddr(
ctx,
misbehaviour.Header1.Header.ChainID,
types.NewConsumerConsAddress(sdk.ConsAddress(v.Address.Bytes())),
)
k.SlashValidator(ctx, providerAddr)
k.JailAndTombstoneValidator(ctx, providerAddr)
err := k.SlashValidator(ctx, providerAddr)
if err != nil {
errors = append(errors, err)
}
err = k.JailAndTombstoneValidator(ctx, providerAddr)
if err != nil {
errors = append(errors, err)
}
provAddrs = append(provAddrs, providerAddr)
}

Expand All @@ -51,6 +59,9 @@ func (k Keeper) HandleConsumerMisbehaviour(ctx sdk.Context, misbehaviour ibctmty
"byzantine validators", provAddrs,
)

if len(errors) > 0 {
return fmt.Errorf("failed to slash, jail, or tombstone validators: %v", errors)
}
return nil
}

Expand Down
42 changes: 20 additions & 22 deletions x/ccv/provider/keeper/punish_validator.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package keeper

import (

Check failure on line 3 in x/ccv/provider/keeper/punish_validator.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
errorsmod "cosmossdk.io/errors"
"fmt"

Check failure on line 5 in x/ccv/provider/keeper/punish_validator.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
sdk "github.com/cosmos/cosmos-sdk/types"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"

slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/interchain-security/v2/x/ccv/provider/types"
)

// JailAndTombstoneValidator jails and tombstones the validator with the given provider consensus address
func (k Keeper) JailAndTombstoneValidator(ctx sdk.Context, providerAddr types.ProviderConsAddress) {
logger := k.Logger(ctx)

// get validator
val, ok := k.stakingKeeper.GetValidatorByConsAddr(ctx, providerAddr.ToSdkConsAddr())
if !ok || val.IsUnbonded() {
logger.Error("validator not found or is unbonded", "provider consensus address", providerAddr.String())
return
func (k Keeper) JailAndTombstoneValidator(ctx sdk.Context, providerAddr types.ProviderConsAddress) error {
validator, found := k.stakingKeeper.GetValidatorByConsAddr(ctx, providerAddr.ToSdkConsAddr())
if !found {
return errorsmod.Wrapf(slashingtypes.ErrNoValidatorForAddress, "provider consensus address: %s", providerAddr.String())
}

if validator.IsUnbonded() {
return fmt.Errorf("validator is unbonded. provider consensus address: %s", providerAddr.String())
}

// check that the validator isn't tombstoned
if k.slashingKeeper.IsTombstoned(ctx, providerAddr.ToSdkConsAddr()) {
logger.Info("validator is already tombstoned", "provider consensus address", providerAddr.String())
return
return fmt.Errorf("validator is tombstoned. provider consensus address: %s", providerAddr.String())
}

// jail validator if not already
if !val.IsJailed() {
if !validator.IsJailed() {
k.stakingKeeper.Jail(ctx, providerAddr.ToSdkConsAddr())
}

Expand All @@ -40,6 +40,8 @@ func (k Keeper) JailAndTombstoneValidator(ctx sdk.Context, providerAddr types.Pr
// because then a validator could i) perform an equivocation, ii) get jailed (e.g., through downtime)
// and in such a case the validator would not get slashed when we call `SlashValidator`.
k.slashingKeeper.Tombstone(ctx, providerAddr.ToSdkConsAddr())

return nil
}

// ComputePowerToSlash computes the power to be slashed based on the tokens in non-matured `undelegations` and
Expand Down Expand Up @@ -72,23 +74,18 @@ func (k Keeper) ComputePowerToSlash(ctx sdk.Context, validator stakingtypes.Vali
}

// SlashValidator slashes validator with `providerAddr`
func (k Keeper) SlashValidator(ctx sdk.Context, providerAddr types.ProviderConsAddress) {
logger := k.Logger(ctx)

func (k Keeper) SlashValidator(ctx sdk.Context, providerAddr types.ProviderConsAddress) error {
validator, found := k.stakingKeeper.GetValidatorByConsAddr(ctx, providerAddr.ToSdkConsAddr())
if !found {
logger.Error("validator not found", "provider consensus address", providerAddr.String())
return
return errorsmod.Wrapf(slashingtypes.ErrNoValidatorForAddress, "provider consensus address: %s", providerAddr.String())
}

if validator.IsUnbonded() {
logger.Info("validator is unbonded", "provider consensus address", providerAddr.String())
return
return fmt.Errorf("validator is unbonded. provider consensus address: %s", providerAddr.String())
}

if k.slashingKeeper.IsTombstoned(ctx, providerAddr.ToSdkConsAddr()) {
logger.Info("validator is already tombstoned", "provider consensus address", providerAddr.String())
return
return fmt.Errorf("validator is tombstoned. provider consensus address: %s", providerAddr.String())
}

undelegations := k.stakingKeeper.GetUnbondingDelegationsFromValidator(ctx, validator.GetOperator())
Expand All @@ -99,4 +96,5 @@ func (k Keeper) SlashValidator(ctx sdk.Context, providerAddr types.ProviderConsA
slashFraction := k.slashingKeeper.SlashFractionDoubleSign(ctx)

k.stakingKeeper.Slash(ctx, providerAddr.ToSdkConsAddr(), 0, totalPower, slashFraction, stakingtypes.DoubleSign)
return nil
}

0 comments on commit 5c053bb

Please sign in to comment.