Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
insumity committed Sep 13, 2024
1 parent 04b6de4 commit 07d4753
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
20 changes: 15 additions & 5 deletions tests/integration/slashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,21 +408,31 @@ 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
// Expect the packet to bounce if the slash meter is negative
providerKeeper.SetSlashMeter(ctx, math.NewInt(-1))
// Only reaches the bouncing code if it fails in the check that chain is launched and the validator is not a consumer validator,
// so we set the chain as stopped.
providerKeeper.SetConsumerPhase(suite.providerCtx(), firstBundle.ConsumerId, providertypes.CONSUMER_PHASE_STOPPED)
ackResult, err = providerKeeper.OnRecvSlashPacket(ctx, packet, *slashPacketData)
suite.Require().NoError(err)
suite.Require().Equal(ccv.SlashPacketBouncedResult, ackResult, "expected bounced result")

// Expect packet not to bounce if the chain is launched
providerKeeper.SetSlashMeter(ctx, math.NewInt(-1))
providerKeeper.SetConsumerPhase(suite.providerCtx(), firstBundle.ConsumerId, providertypes.CONSUMER_PHASE_LAUNCHED)
ackResult, err = providerKeeper.OnRecvSlashPacket(ctx, packet, *slashPacketData)

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning test

This definition of err is never used.
suite.Require().Equal(ccv.SlashPacketHandledResult, ackResult, "expected successful ack")

// Also test what happens if the chain is launched but we have a consumer validator. In this case the check that the
// chain is launched and the validator is not a consumer validator fails, and hence the packet bounces due to the
// negative slash meter.
err = providerKeeper.SetConsumerValidator(ctx, firstBundle.ConsumerId, providertypes.ConsensusValidator{
ProviderConsAddr: validAddress,
})
suite.Require().NoError(err)

// Expect the packet to bounce if the slash meter is negative
providerKeeper.SetSlashMeter(ctx, math.NewInt(-1))
ackResult, err = providerKeeper.OnRecvSlashPacket(ctx, packet, *slashPacketData)
suite.Require().NoError(err)
suite.Require().Equal(ccv.SlashPacketBouncedResult, ackResult, "expected successful ack")
suite.Require().Equal(ccv.SlashPacketBouncedResult, ackResult, "expected bounced result")

// Expect the packet to be handled if the slash meter is positive
providerKeeper.SetSlashMeter(ctx, math.NewInt(0))
Expand Down
5 changes: 5 additions & 0 deletions x/ccv/provider/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,11 @@ func (k Keeper) hasToValidate(
provAddr types.ProviderConsAddress,
consumerId string,
) (bool, error) {
// only ask validators to validate active chains
if !k.IsConsumerActive(ctx, consumerId) {
return false, nil
}

// if the validator was sent as part of the packet in the last epoch, it has to validate
if k.IsConsumerValidator(ctx, consumerId, provAddr) {
return true, nil
Expand Down
4 changes: 2 additions & 2 deletions x/ccv/provider/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ func (k Keeper) OnRecvSlashPacket(
return ccv.V1Result, nil
}

// Check that the validator belongs to the consumer chain valset
if !k.IsConsumerValidator(ctx, consumerId, providerConsAddr) {
// Check that chain is launched and the validator belongs to the consumer chain valset
if k.GetConsumerPhase(ctx, consumerId) == providertypes.CONSUMER_PHASE_LAUNCHED && !k.IsConsumerValidator(ctx, consumerId, providerConsAddr) {
k.Logger(ctx).Error("cannot jail validator %s that does not belong to consumer %s valset",
providerConsAddr.String(), consumerId)
// drop packet but return a slash ack so that the consumer can send another slash packet
Expand Down

0 comments on commit 07d4753

Please sign in to comment.