Skip to content

Commit

Permalink
relay test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
stana-miric committed Nov 22, 2024
1 parent 973dbee commit 99d4eea
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 22 deletions.
21 changes: 19 additions & 2 deletions testutil/keeper/expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func GetMocksForDeleteConsumerChain(ctx sdk.Context, mocks *MockedKeepers) []*go

func GetMocksForHandleSlashPacket(ctx sdk.Context, mocks MockedKeepers,
expectedProviderValConsAddr providertypes.ProviderConsAddress,
valToReturn stakingtypes.Validator, expectJailing bool,
valToReturn stakingtypes.Validator, expectJailing bool, valAddr []byte,
) []*gomock.Call {
// These first two calls are always made.
calls := []*gomock.Call{
Expand All @@ -102,16 +102,33 @@ func GetMocksForHandleSlashPacket(ctx sdk.Context, mocks MockedKeepers,

mocks.MockSlashingKeeper.EXPECT().IsTombstoned(ctx,
expectedProviderValConsAddr.ToSdkConsAddr()).Return(false).Times(1),

// called in slash fn
mocks.MockStakingKeeper.EXPECT().GetValidatorByConsAddr(
ctx, expectedProviderValConsAddr.ToSdkConsAddr()).Return(
valToReturn, nil,
).Times(1),
mocks.MockSlashingKeeper.EXPECT().IsTombstoned(ctx,
expectedProviderValConsAddr.ToSdkConsAddr()).Return(false).Times(1),
mocks.MockStakingKeeper.EXPECT().GetUnbondingDelegationsFromValidator(ctx,
valAddr).Return([]stakingtypes.UnbondingDelegation{}, nil).Times(1),
mocks.MockStakingKeeper.EXPECT().GetRedelegationsFromSrcValidator(ctx,
valAddr).Return([]stakingtypes.Redelegation{}, nil).Times(1),
mocks.MockStakingKeeper.EXPECT().GetLastValidatorPower(ctx,
valAddr).Return(int64(100), nil).Times(1),
mocks.MockStakingKeeper.EXPECT().PowerReduction(ctx).Return(sdk.DefaultPowerReduction).Times(1),
mocks.MockStakingKeeper.EXPECT().SlashWithInfractionReason(ctx, expectedProviderValConsAddr.ToSdkConsAddr(), gomock.Any(),
gomock.Any(), gomock.Any(), gomock.Any()).Return(math.NewInt(0), nil).Times(1),
}

if expectJailing {
// jail
calls = append(calls, mocks.MockStakingKeeper.EXPECT().Jail(
gomock.Eq(ctx),
gomock.Eq(expectedProviderValConsAddr.ToSdkConsAddr()),
).Return(nil))

// JailUntil is set in this code path.
calls = append(calls, mocks.MockSlashingKeeper.EXPECT().DowntimeJailDuration(ctx).Return(time.Hour, nil).Times(1))
calls = append(calls, mocks.MockSlashingKeeper.EXPECT().JailUntil(ctx,
expectedProviderValConsAddr.ToSdkConsAddr(), gomock.Any()).Return(nil).Times(1))
}
Expand Down
18 changes: 9 additions & 9 deletions x/ccv/provider/keeper/consumer_equivocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (k Keeper) HandleConsumerDoubleVoting(
return err
}

if err = k.SlashValidator(ctx, providerAddr, infractionParams.DoubleSign, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN); err != nil {
if err = k.SlashValidator(ctx, providerAddr, infractionParams.DoubleSign, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN, evidence.VoteA.Height); err != nil {
return err
}
if err = k.JailAndTombstoneValidator(ctx, providerAddr, infractionParams.DoubleSign); err != nil {
Expand Down Expand Up @@ -203,7 +203,7 @@ func (k Keeper) HandleConsumerMisbehaviour(ctx sdk.Context, consumerId string, m
consumerId,
types.NewConsumerConsAddress(sdk.ConsAddress(v.Address.Bytes())),
)
err := k.SlashValidator(ctx, providerAddr, infractionParams.DoubleSign, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN)
err := k.SlashValidator(ctx, providerAddr, infractionParams.DoubleSign, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN, 0)
if err != nil {
logger.Error("failed to slash validator: %s", err)
continue
Expand Down Expand Up @@ -492,7 +492,12 @@ func (k Keeper) ComputePowerToSlash(ctx sdk.Context, validator stakingtypes.Vali
}

// SlashValidator slashes validator with given provider Address
func (k Keeper) SlashValidator(ctx sdk.Context, providerAddr types.ProviderConsAddress, slashingParams *types.SlashJailParameters, slashingReason stakingtypes.Infraction) error {
func (k Keeper) SlashValidator(
ctx sdk.Context,
providerAddr types.ProviderConsAddress,
slashingParams *types.SlashJailParameters,
slashingReason stakingtypes.Infraction,
infractionHeight int64) error {
validator, err := k.stakingKeeper.GetValidatorByConsAddr(ctx, providerAddr.ToSdkConsAddr())
if err != nil && errors.Is(err, stakingtypes.ErrNoValidatorFound) {
return errorsmod.Wrapf(slashingtypes.ErrNoValidatorForAddress, "provider consensus address: %s", providerAddr.String())
Expand Down Expand Up @@ -529,12 +534,7 @@ func (k Keeper) SlashValidator(ctx sdk.Context, providerAddr types.ProviderConsA
powerReduction := k.stakingKeeper.PowerReduction(ctx)
totalPower := k.ComputePowerToSlash(ctx, validator, undelegations, redelegations, lastPower, powerReduction)

consAdrr, err := validator.GetConsAddr()
if err != nil {
return err
}

_, err = k.stakingKeeper.SlashWithInfractionReason(ctx, consAdrr, 0, totalPower, slashingParams.SlashFraction, slashingReason)
_, err = k.stakingKeeper.SlashWithInfractionReason(ctx, providerAddr.ToSdkConsAddr(), infractionHeight, totalPower, slashingParams.SlashFraction, slashingReason)
return err
}

Expand Down
4 changes: 2 additions & 2 deletions x/ccv/provider/keeper/consumer_equivocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ func TestSlashValidator(t *testing.T) {
}

gomock.InOrder(expectedCalls...)
keeper.SlashValidator(ctx, providerAddr, getTestInfractionParameters().DoubleSign, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN)
keeper.SlashValidator(ctx, providerAddr, getTestInfractionParameters().DoubleSign, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN, 0)
}

// TestSlashValidatorDoesNotSlashIfValidatorIsUnbonded asserts that `SlashValidator` does not call
Expand All @@ -766,7 +766,7 @@ func TestSlashValidatorDoesNotSlashIfValidatorIsUnbonded(t *testing.T) {
}

gomock.InOrder(expectedCalls...)
keeper.SlashValidator(ctx, providerAddr, getTestInfractionParameters().DoubleSign, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN)
keeper.SlashValidator(ctx, providerAddr, getTestInfractionParameters().DoubleSign, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN, 0)
}

func TestEquivocationEvidenceMinHeightCRUD(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion x/ccv/provider/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func (k Keeper) HandleSlashPacket(ctx sdk.Context, consumerId string, data ccv.S
}

// slash validator
if err = k.SlashValidator(ctx, providerConsAddr, infractionParams.Downtime, stakingtypes.Infraction_INFRACTION_DOWNTIME); err != nil {
if err = k.SlashValidator(ctx, providerConsAddr, infractionParams.Downtime, stakingtypes.Infraction_INFRACTION_DOWNTIME, int64(infractionHeight)); err != nil {
k.Logger(ctx).Error("failed to slash vaidator", providerConsAddr.ToSdkConsAddr().String(), "err", err.Error())
return
}
Expand Down
37 changes: 29 additions & 8 deletions x/ccv/provider/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
Expand Down Expand Up @@ -186,6 +187,12 @@ func TestOnRecvDowntimeSlashPacket(t *testing.T) {
})
require.NoError(t, err)

// set consumer infraction parameters
err = providerKeeper.SetInfractionParameters(ctx, consumerId0, *getTestInfractionParameters())
require.NoError(t, err)
err = providerKeeper.SetInfractionParameters(ctx, consumerId1, *getTestInfractionParameters())
require.NoError(t, err)

// Also bounced for chain-2
ackResult, err = executeOnRecvSlashPacket(t, &providerKeeper, ctx, channelId1, 2, packetData)
require.Equal(t, ccv.SlashPacketBouncedResult, ackResult)
Expand All @@ -200,12 +207,15 @@ func TestOnRecvDowntimeSlashPacket(t *testing.T) {

// Mock call to GetEffectiveValPower, so that it returns 2.
providerAddr := providertypes.NewProviderConsAddress(packetData.Validator.Address)
valAddr := sdk.ValAddress(packetData.Validator.Address).String()
valOper, err := providerKeeper.ValidatorAddressCodec().StringToBytes(valAddr)
require.NoError(t, err)
calls := []*gomock.Call{
mocks.MockStakingKeeper.EXPECT().GetValidatorByConsAddr(ctx, providerAddr.ToSdkConsAddr()).
Return(stakingtypes.Validator{
// provided address must be valid so it can be processed correctly
// by k.ValidatorAddressCodec().StringToBytes(val.GetOperator()) call in GetEffectiveValPower()
OperatorAddress: sdk.ValAddress(packetData.Validator.Address).String(),
OperatorAddress: valAddr,
}, nil).Times(1),
mocks.MockStakingKeeper.EXPECT().GetLastValidatorPower(ctx, gomock.Any()).
Return(int64(2), nil).Times(1),
Expand All @@ -214,7 +224,7 @@ func TestOnRecvDowntimeSlashPacket(t *testing.T) {
// Add mocks for slash packet handling
calls = append(calls,
testkeeper.GetMocksForHandleSlashPacket(
ctx, mocks, providerAddr, stakingtypes.Validator{Jailed: false}, true)...,
ctx, mocks, providerAddr, stakingtypes.Validator{Jailed: false, OperatorAddress: valAddr}, true, valOper)...,
)
gomock.InOrder(calls...)

Expand Down Expand Up @@ -343,6 +353,11 @@ func TestHandleSlashPacket(t *testing.T) {
providerConsAddr := cryptotestutil.NewCryptoIdentityFromIntSeed(7842334).ProviderConsAddress()
consumerConsAddr := cryptotestutil.NewCryptoIdentityFromIntSeed(784987634).ConsumerConsAddress()

valOperAddr := cryptotestutil.NewCryptoIdentityFromIntSeed(7842334).SDKValOpAddressString()
valAddrCodec := authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix())
valOperAddrBytes, err := valAddrCodec.StringToBytes(valOperAddr)
require.NoError(t, err)

testCases := []struct {
name string
packetData ccv.SlashPacketData
Expand Down Expand Up @@ -431,9 +446,10 @@ func TestHandleSlashPacket(t *testing.T) {
) []*gomock.Call {
return testkeeper.GetMocksForHandleSlashPacket(
ctx, mocks,
providerConsAddr, // expected provider cons addr returned from GetProviderAddrFromConsumerAddr
stakingtypes.Validator{Jailed: false}, // staking keeper val to return
true) // expectJailing = true
providerConsAddr, // expected provider cons addr returned from GetProviderAddrFromConsumerAddr
stakingtypes.Validator{Jailed: false, OperatorAddress: valOperAddr}, // staking keeper val to return
true, // expectJailing = true
valOperAddrBytes)
},
1,
consumerConsAddr,
Expand All @@ -449,9 +465,10 @@ func TestHandleSlashPacket(t *testing.T) {
) []*gomock.Call {
return testkeeper.GetMocksForHandleSlashPacket(
ctx, mocks,
providerConsAddr, // expected provider cons addr returned from GetProviderAddrFromConsumerAddr
stakingtypes.Validator{Jailed: true}, // staking keeper val to return
false) // expectJailing = false, validator is already jailed.
providerConsAddr, // expected provider cons addr returned from GetProviderAddrFromConsumerAddr
stakingtypes.Validator{Jailed: true, OperatorAddress: valOperAddr}, // staking keeper val to return
false, // expectJailing = false, validator is already jailed.
valOperAddrBytes)
},
1,
consumerConsAddr,
Expand All @@ -477,6 +494,10 @@ func TestHandleSlashPacket(t *testing.T) {
err := providerKeeper.SetConsumerValidator(ctx, chainId, providertypes.ConsensusValidator{ProviderConsAddr: providerConsAddr.Address.Bytes()})
require.NoError(t, err)

// set infraction params
err = providerKeeper.SetInfractionParameters(ctx, chainId, *getTestInfractionParameters())
require.NoError(t, err)

// Execute method and assert expected mock calls.
providerKeeper.HandleSlashPacket(ctx, chainId, tc.packetData)

Expand Down

0 comments on commit 99d4eea

Please sign in to comment.