From 114fa260f6464a590c198349dcee5a484436b274 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 13 Sep 2024 16:44:29 +0200 Subject: [PATCH] fix!: only validate active chains (backport #2266) (#2268) fix!: only validate active chains (#2266) init commit (cherry picked from commit f4ae1068ec8dde43b0814fd31b4d19b9f2646e80) Co-authored-by: insumity --- tests/integration/slashing.go | 20 +++++++++++++++----- x/ccv/provider/keeper/grpc_query.go | 5 +++++ x/ccv/provider/keeper/relay.go | 4 ++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/tests/integration/slashing.go b/tests/integration/slashing.go index a60765563f..111303cbcf 100644 --- a/tests/integration/slashing.go +++ b/tests/integration/slashing.go @@ -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) 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)) diff --git a/x/ccv/provider/keeper/grpc_query.go b/x/ccv/provider/keeper/grpc_query.go index 0a3edcdfb1..08c9019782 100644 --- a/x/ccv/provider/keeper/grpc_query.go +++ b/x/ccv/provider/keeper/grpc_query.go @@ -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 diff --git a/x/ccv/provider/keeper/relay.go b/x/ccv/provider/keeper/relay.go index 6af8aa23ec..cb251de3ee 100644 --- a/x/ccv/provider/keeper/relay.go +++ b/x/ccv/provider/keeper/relay.go @@ -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