Skip to content

Commit

Permalink
fix!: fix slashing in PSS (#1838)
Browse files Browse the repository at this point in the history
* drop slash packet for opted-out validators before updating slash meter

* fix integration test

* fix ut

* update UT
  • Loading branch information
sainoe authored May 1, 2024
1 parent aef34a1 commit 31a7aa3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
9 changes: 9 additions & 0 deletions tests/integration/slashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,15 @@ func (suite *CCVTestSuite) TestOnRecvSlashPacketErrors() {
// Check expected behavior for handling SlashPackets for downtime infractions
slashPacketData.Infraction = stakingtypes.Infraction_INFRACTION_DOWNTIME

// Expect packet to be handled if the validator didn't opt in
ackResult, err = providerKeeper.OnRecvSlashPacket(ctx, packet, *slashPacketData)
suite.Require().NoError(err, "no error expected")
suite.Require().Equal(ccv.SlashPacketHandledResult, ackResult, "expected successful ack")

providerKeeper.SetConsumerValidator(ctx, firstBundle.Chain.ChainID, providertypes.ConsumerValidator{
ProviderConsAddr: validAddress,
})

// Expect the packet to bounce if the slash meter is negative
providerKeeper.SetSlashMeter(ctx, sdk.NewInt(-1))
ackResult, err = providerKeeper.OnRecvSlashPacket(ctx, packet, *slashPacketData)
Expand Down
19 changes: 10 additions & 9 deletions x/ccv/provider/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,16 @@ func (k Keeper) OnRecvSlashPacket(
return ccv.V1Result, nil
}

// 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 but return a slash ack so that the consumer can send another slash packet
k.AppendSlashAck(ctx, chainID, consumerConsAddr.String())

return ccv.SlashPacketHandledResult, nil
}

meter := k.GetSlashMeter(ctx)
// Return bounce ack if meter is negative in value
if meter.IsNegative() {
Expand Down Expand Up @@ -401,15 +411,6 @@ 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 but return a slash ack so that the consumer can send another slash packet
k.AppendSlashAck(ctx, chainID, consumerConsAddr.String())
return
}

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

Expand Down
27 changes: 10 additions & 17 deletions x/ccv/provider/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,22 @@ func TestOnRecvDowntimeSlashPacket(t *testing.T) {
// Set a block height for the valset update id in the generated packet data
providerKeeper.SetValsetUpdateBlockHeight(ctx, packetData.ValsetUpdateId, uint64(15))

// Set consumer validator
providerKeeper.SetConsumerValidator(ctx, "chain-1", providertypes.ConsumerValidator{
ProviderConsAddr: packetData.Validator.Address,
})

// Set slash meter to negative value and assert a bounce ack is returned
providerKeeper.SetSlashMeter(ctx, math.NewInt(-5))
ackResult, err := executeOnRecvSlashPacket(t, &providerKeeper, ctx, "channel-1", 1, packetData)
require.Equal(t, ccv.SlashPacketBouncedResult, ackResult)
require.NoError(t, err)

// Set consumer validator
providerKeeper.SetConsumerValidator(ctx, "chain-2", providertypes.ConsumerValidator{
ProviderConsAddr: packetData.Validator.Address,
})

// Also bounced for chain-2
ackResult, err = executeOnRecvSlashPacket(t, &providerKeeper, ctx, "channel-2", 2, packetData)
require.Equal(t, ccv.SlashPacketBouncedResult, ackResult)
Expand Down Expand Up @@ -298,8 +308,6 @@ func TestHandleSlashPacket(t *testing.T) {

providerConsAddr := cryptotestutil.NewCryptoIdentityFromIntSeed(7842334).ProviderConsAddress()
consumerConsAddr := cryptotestutil.NewCryptoIdentityFromIntSeed(784987634).ConsumerConsAddress()
// this "dummy" consensus address won't be stored on the provider states
dummyConsAddr := cryptotestutil.NewCryptoIdentityFromIntSeed(784987639).ConsumerConsAddress()

testCases := []struct {
name string
Expand All @@ -309,21 +317,6 @@ func TestHandleSlashPacket(t *testing.T) {
expectedSlashAcksLen int
expectedSlashAckConsumerConsAddress types.ConsumerConsAddress
}{
{
"validator isn't a consumer validator",
ccv.SlashPacketData{
Validator: abci.Validator{Address: dummyConsAddr.ToSdkConsAddr()},
ValsetUpdateId: validVscID,
Infraction: stakingtypes.Infraction_INFRACTION_DOWNTIME,
},
func(ctx sdk.Context, mocks testkeeper.MockedKeepers,
expectedPacketData ccv.SlashPacketData,
) []*gomock.Call {
return []*gomock.Call{}
},
1,
dummyConsAddr,
},
{
"unfound validator",
ccv.SlashPacketData{
Expand Down

0 comments on commit 31a7aa3

Please sign in to comment.