Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
insumity committed Sep 7, 2023
1 parent eb6a079 commit e70bf10
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
39 changes: 38 additions & 1 deletion x/ccv/provider/keeper/double_vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"bytes"
"fmt"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"

Expand All @@ -13,6 +14,41 @@ import (
tmtypes "github.com/tendermint/tendermint/types"
)

func (k Keeper) slashValidator(ctx sdk.Context, providerAddr types.ProviderConsAddress) {
val, found := k.stakingKeeper.GetValidatorByConsAddr(ctx, providerAddr.ToSdkConsAddr())

if !found {
//logger.Error("validator not found or is unbonded", providerAddr.String())
return
}
valOperatorAddress := val.GetOperator()

undelegationsInTokens := sdk.NewInt(0)
for _, v := range k.stakingKeeper.GetUnbondingDelegationsFromValidator(ctx, valOperatorAddress) {
for _, entry := range v.Entries {
undelegationsInTokens = undelegationsInTokens.Add(entry.InitialBalance)
}
}

redelegationsInTokens := sdk.NewInt(0)
for _, v := range k.stakingKeeper.GetRedelegationsFromSrcValidator(ctx, valOperatorAddress) {
for _, entry := range v.Entries {
redelegationsInTokens = redelegationsInTokens.Add(entry.InitialBalance)
}
}

powerReduction := k.stakingKeeper.PowerReduction(ctx)
undelegationsAndRedelegationsInPower := sdk.TokensToConsensusPower(
undelegationsInTokens.Add(redelegationsInTokens), powerReduction)

power := k.stakingKeeper.GetLastValidatorPower(ctx, valOperatorAddress)
totalPower := power + undelegationsAndRedelegationsInPower
slashFraction := k.slashingKeeper.SlashFractionDoubleSign(ctx)

// TODO: what if it's another key ????
k.stakingKeeper.Slash(ctx, providerAddr.ToSdkConsAddr(), 0, totalPower, slashFraction, stakingtypes.DoubleSign)
}

// HandleConsumerDoubleVoting verifies a double voting evidence for a given a consumer chain ID
// and a public key and, if successful, executes the jailing of the malicious validator.
func (k Keeper) HandleConsumerDoubleVoting(
Expand All @@ -34,7 +70,8 @@ func (k Keeper) HandleConsumerDoubleVoting(
)

// execute the jailing
k.JailValidator(ctx, providerAddr)
k.slashValidator(ctx, providerAddr)
k.JailAndTombstoneValidator(ctx, providerAddr)

k.Logger(ctx).Info(
"confirmed equivocation",
Expand Down
3 changes: 2 additions & 1 deletion x/ccv/provider/keeper/misbehaviour.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func (k Keeper) HandleConsumerMisbehaviour(ctx sdk.Context, misbehaviour ibctmty
misbehaviour.Header1.Header.ChainID,
types.NewConsumerConsAddress(sdk.ConsAddress(v.Address.Bytes())),
)
k.JailValidator(ctx, providerAddr)
k.slashValidator(ctx, providerAddr)
k.JailAndTombstoneValidator(ctx, providerAddr)
provAddrs = append(provAddrs, providerAddr)
}

Expand Down
7 changes: 4 additions & 3 deletions x/ccv/provider/keeper/punish_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"github.com/cosmos/interchain-security/v2/x/ccv/provider/types"
)

// JailValidator jails the validator with the given provider consensus address
// JailAndTombstoneValidator jails the validator with the given provider consensus address
// Note that the tombstoning is temporarily removed until we slash validator
// for double signing on a consumer chain, see comment
// https://github.com/cosmos/interchain-security/pull/1232#issuecomment-1693127641.
func (k Keeper) JailValidator(ctx sdk.Context, providerAddr types.ProviderConsAddress) {
func (k Keeper) JailAndTombstoneValidator(ctx sdk.Context, providerAddr types.ProviderConsAddress) {
logger := k.Logger(ctx)

// get validator
Expand All @@ -34,5 +34,6 @@ func (k Keeper) JailValidator(ctx sdk.Context, providerAddr types.ProviderConsAd
// update jail time to end after double sign jail duration
k.slashingKeeper.JailUntil(ctx, providerAddr.ToSdkConsAddr(), evidencetypes.DoubleSignJailEndTime)

// TODO: add tombstoning back once we integrate the slashing
// TODO: do we need to jail if we tombstone, that's what cosmos-sdk does
k.slashingKeeper.Tombstone(ctx, providerAddr.ToSdkConsAddr())
}
2 changes: 1 addition & 1 deletion x/ccv/provider/keeper/punish_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestJailValidator(t *testing.T) {
gomock.InOrder(tc.expectedCalls(ctx, mocks, tc.provAddr)...)

// Execute method and assert expected mock calls
providerKeeper.JailValidator(ctx, tc.provAddr)
providerKeeper.JailAndTombstoneValidator(ctx, tc.provAddr)

ctrl.Finish()
}
Expand Down
2 changes: 2 additions & 0 deletions x/ccv/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type StakingKeeper interface {
GetValidatorUpdates(ctx sdk.Context) []abci.ValidatorUpdate
UnbondingCanComplete(ctx sdk.Context, id uint64) error
UnbondingTime(ctx sdk.Context) time.Duration
GetUnbondingDelegationsFromValidator(ctx sdk.Context, valAddr sdk.ValAddress) (ubds []stakingtypes.UnbondingDelegation)
GetRedelegationsFromSrcValidator(ctx sdk.Context, valAddr sdk.ValAddress) (reds []stakingtypes.Redelegation)
GetValidatorByConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) (validator stakingtypes.Validator, found bool)
GetLastValidatorPower(ctx sdk.Context, operator sdk.ValAddress) (power int64)
// slash the validator and delegators of the validator, specifying offence height, offence power, and slash fraction
Expand Down

0 comments on commit e70bf10

Please sign in to comment.