Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Add slashing logic for PSS #1710

Merged
merged 6 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions x/ccv/provider/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,14 @@ func (k Keeper) HandleSlashPacket(ctx sdk.Context, chainID string, data ccv.Slas
"infractionType", data.Infraction,
)

// Check that the validator belongs to the consumer chain valset
if !k.IsConsumerValidator(ctx, chainID, providerConsAddr) {
k.Logger(ctx).Error("cannot jail validator %s that does not belong to consumer %s valset",
providerConsAddr.String(), chainID)
// drop packet
return
}

// Obtain validator from staking keeper
validator, found := k.stakingKeeper.GetValidatorByConsAddr(ctx, providerConsAddr.ToSdkConsAddr())

Expand Down
66 changes: 43 additions & 23 deletions x/ccv/provider/keeper/relay_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package keeper_test

import (
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
ibctesting "github.com/cosmos/ibc-go/v7/testing"
"strings"
"testing"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
ibctesting "github.com/cosmos/ibc-go/v7/testing"

clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
"github.com/golang/mock/gomock"
Expand All @@ -22,6 +23,7 @@ import (
cryptotestutil "github.com/cosmos/interchain-security/v4/testutil/crypto"
testkeeper "github.com/cosmos/interchain-security/v4/testutil/keeper"
"github.com/cosmos/interchain-security/v4/x/ccv/provider/keeper"
"github.com/cosmos/interchain-security/v4/x/ccv/provider/types"
providertypes "github.com/cosmos/interchain-security/v4/x/ccv/provider/types"
ccv "github.com/cosmos/interchain-security/v4/x/ccv/types"
)
Expand Down Expand Up @@ -289,6 +291,8 @@ func TestValidateSlashPacket(t *testing.T) {
func TestHandleSlashPacket(t *testing.T) {
chainId := "consumer-id"
validVscID := uint64(234)

dumConsAddr := cryptotestutil.NewCryptoIdentityFromIntSeed(784987639).ConsumerConsAddress()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add a comment here that this is not a consumer validator? What does dum stand for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dum-my

providerConsAddr := cryptotestutil.NewCryptoIdentityFromIntSeed(7842334).ProviderConsAddress()
consumerConsAddr := cryptotestutil.NewCryptoIdentityFromIntSeed(784987634).ConsumerConsAddress()

Expand All @@ -299,6 +303,20 @@ func TestHandleSlashPacket(t *testing.T) {
expectedCalls func(sdk.Context, testkeeper.MockedKeepers, ccv.SlashPacketData) []*gomock.Call
expectedSlashAcksLen int
}{
{
"validator isn't a consumer validator",
ccv.SlashPacketData{
Validator: abci.Validator{Address: dumConsAddr.ToSdkConsAddr()},
ValsetUpdateId: validVscID,
Infraction: stakingtypes.Infraction_INFRACTION_DOWNTIME,
},
func(ctx sdk.Context, mocks testkeeper.MockedKeepers,
expectedPacketData ccv.SlashPacketData,
) []*gomock.Call {
return []*gomock.Call{}
},
0,
},
{
"unfound validator",
ccv.SlashPacketData{
Expand Down Expand Up @@ -403,34 +421,36 @@ func TestHandleSlashPacket(t *testing.T) {
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
providerKeeper, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(
t, testkeeper.NewInMemKeeperParams(t))

providerKeeper, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(
t, testkeeper.NewInMemKeeperParams(t))
// Setup expected mock calls
gomock.InOrder(tc.expectedCalls(ctx, mocks, tc.packetData)...)

// Setup expected mock calls
gomock.InOrder(tc.expectedCalls(ctx, mocks, tc.packetData)...)
// Setup init chain height and a single valid valset update ID to block height mapping.
providerKeeper.SetInitChainHeight(ctx, chainId, 5)
providerKeeper.SetValsetUpdateBlockHeight(ctx, validVscID, 99)

// Setup init chain height and a single valid valset update ID to block height mapping.
providerKeeper.SetInitChainHeight(ctx, chainId, 5)
providerKeeper.SetValsetUpdateBlockHeight(ctx, validVscID, 99)
// Setup consumer address to provider address mapping.
require.NotEmpty(t, tc.packetData.Validator.Address)
providerKeeper.SetValidatorByConsumerAddr(ctx, chainId, consumerConsAddr, providerConsAddr)
providerKeeper.SetConsumerValidator(ctx, chainId, types.ConsumerValidator{ProviderConsAddr: providerConsAddr.Address.Bytes()})

// Setup consumer address to provider address mapping.
require.NotEmpty(t, tc.packetData.Validator.Address)
providerKeeper.SetValidatorByConsumerAddr(ctx, chainId, consumerConsAddr, providerConsAddr)
// Execute method and assert expected mock calls.
providerKeeper.HandleSlashPacket(ctx, chainId, tc.packetData)

// Execute method and assert expected mock calls.
providerKeeper.HandleSlashPacket(ctx, chainId, tc.packetData)
require.Equal(t, tc.expectedSlashAcksLen, len(providerKeeper.GetSlashAcks(ctx, chainId)))

require.Equal(t, tc.expectedSlashAcksLen, len(providerKeeper.GetSlashAcks(ctx, chainId)))

if tc.expectedSlashAcksLen == 1 {
// must match the consumer address
require.Equal(t, consumerConsAddr.String(), providerKeeper.GetSlashAcks(ctx, chainId)[0])
require.NotEqual(t, providerConsAddr.String(), providerKeeper.GetSlashAcks(ctx, chainId)[0])
require.NotEqual(t, providerConsAddr.String(), consumerConsAddr.String())
}
if tc.expectedSlashAcksLen == 1 {
// must match the consumer address
require.Equal(t, consumerConsAddr.String(), providerKeeper.GetSlashAcks(ctx, chainId)[0])
require.NotEqual(t, providerConsAddr.String(), providerKeeper.GetSlashAcks(ctx, chainId)[0])
require.NotEqual(t, providerConsAddr.String(), consumerConsAddr.String())
}

ctrl.Finish()
ctrl.Finish()
})
}
}

Expand Down
Loading