Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoke committed Sep 3, 2024
1 parent 4674e77 commit c226518
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 52 deletions.
14 changes: 10 additions & 4 deletions tests/integration/partial_set_security_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package integration

import (
"github.com/cosmos/interchain-security/v5/x/ccv/provider/types"
"slices"
"sort"
"testing"

"github.com/cosmos/interchain-security/v5/x/ccv/provider/types"

"cosmossdk.io/math"
ccv "github.com/cosmos/interchain-security/v5/x/ccv/types"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -152,9 +153,14 @@ func TestMinStake(t *testing.T) {
// adjust parameters

// set the minStake according to the test case
providerKeeper.SetConsumerPowerShapingParameters(s.providerChain.GetContext(), s.getFirstBundle().ConsumerId, types.PowerShapingParameters{
MinStake: tc.minStake,
})
err = providerKeeper.SetConsumerPowerShapingParameters(
s.providerChain.GetContext(),
s.getFirstBundle().ConsumerId,
types.PowerShapingParameters{
MinStake: tc.minStake,
},
)
s.Require().NoError(err)

// delegate and undelegate to trigger a vscupdate

Expand Down
3 changes: 2 additions & 1 deletion tests/mbt/driver/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,10 @@ func (s *Driver) ConfigureNewPath(consumerChain, providerChain *ibctesting.TestC
require.NoError(s.t, err, "Error setting consumer genesis on provider for chain %v", consumerChain.ChainID)

// set the top N percentage to 100 to simulate a full consumer
s.providerKeeper().SetConsumerPowerShapingParameters(providerChain.GetContext(), consumerChain.ChainID, types.PowerShapingParameters{
err = s.providerKeeper().SetConsumerPowerShapingParameters(providerChain.GetContext(), consumerChain.ChainID, types.PowerShapingParameters{
Top_N: 100,
})
require.NoError(s.t, err, "Error setting consumer top N for chain %v", consumerChain.ChainID)

// Client ID is set in InitGenesis and we treat it as a black box. So
// must query it to use it with the endpoint.
Expand Down
11 changes: 7 additions & 4 deletions testutil/keeper/unit_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,15 @@ func SetupForStoppingConsumerChain(t *testing.T, ctx sdk.Context,
gomock.InOrder(expectations...)

providerKeeper.SetConsumerChainId(ctx, consumerId, "chainID")
providerKeeper.SetConsumerMetadata(ctx, consumerId, GetTestConsumerMetadata())
providerKeeper.SetConsumerInitializationParameters(ctx, consumerId, GetTestInitializationParameters())
providerKeeper.SetConsumerPowerShapingParameters(ctx, consumerId, GetTestPowerShapingParameters())
err := providerKeeper.SetConsumerMetadata(ctx, consumerId, GetTestConsumerMetadata())
require.NoError(t, err)
err = providerKeeper.SetConsumerInitializationParameters(ctx, consumerId, GetTestInitializationParameters())
require.NoError(t, err)
err = providerKeeper.SetConsumerPowerShapingParameters(ctx, consumerId, GetTestPowerShapingParameters())
require.NoError(t, err)
providerKeeper.SetConsumerPhase(ctx, consumerId, providertypes.ConsumerPhase_CONSUMER_PHASE_INITIALIZED)

err := providerKeeper.CreateConsumerClient(ctx, consumerId)
err = providerKeeper.CreateConsumerClient(ctx, consumerId)
require.NoError(t, err)
// set the mapping consumer ID <> client ID for the consumer chain
providerKeeper.SetConsumerClientId(ctx, consumerId, "clientID")
Expand Down
20 changes: 13 additions & 7 deletions x/ccv/provider/keeper/consumer_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,14 @@ func TestCreateConsumerClient(t *testing.T) {

// Call method with same arbitrary values as defined above in mock expectations.
providerKeeper.SetConsumerChainId(ctx, "0", "chainID")
providerKeeper.SetConsumerMetadata(ctx, "0", testkeeper.GetTestConsumerMetadata())
providerKeeper.SetConsumerInitializationParameters(ctx, "0", testkeeper.GetTestInitializationParameters())
providerKeeper.SetConsumerPowerShapingParameters(ctx, "0", testkeeper.GetTestPowerShapingParameters())
err := providerKeeper.CreateConsumerClient(ctx, "0")
err := providerKeeper.SetConsumerMetadata(ctx, "0", testkeeper.GetTestConsumerMetadata())
require.NoError(t, err)
err = providerKeeper.SetConsumerInitializationParameters(ctx, "0", testkeeper.GetTestInitializationParameters())
require.NoError(t, err)
err = providerKeeper.SetConsumerPowerShapingParameters(ctx, "0", testkeeper.GetTestPowerShapingParameters())
require.NoError(t, err)

err = providerKeeper.CreateConsumerClient(ctx, "0")
if tc.expClientCreated {
require.NoError(t, err)
testCreatedConsumerClient(t, ctx, providerKeeper, "0", "clientID")
Expand Down Expand Up @@ -526,9 +529,12 @@ func TestMakeConsumerGenesis(t *testing.T) {
UnbondingPeriod: unbondingPeriod,
}
providerKeeper.SetConsumerChainId(ctx, "0", "testchain1")
providerKeeper.SetConsumerMetadata(ctx, "0", consumerMetadata)
providerKeeper.SetConsumerInitializationParameters(ctx, "0", initializationParameters)
providerKeeper.SetConsumerPowerShapingParameters(ctx, "0", providertypes.PowerShapingParameters{})
err := providerKeeper.SetConsumerMetadata(ctx, "0", consumerMetadata)
require.NoError(t, err)
err = providerKeeper.SetConsumerInitializationParameters(ctx, "0", initializationParameters)
require.NoError(t, err)
err = providerKeeper.SetConsumerPowerShapingParameters(ctx, "0", providertypes.PowerShapingParameters{})
require.NoError(t, err)

actualGenesis, _, err := providerKeeper.MakeConsumerGenesis(ctx, "0")
require.NoError(t, err)
Expand Down
15 changes: 10 additions & 5 deletions x/ccv/provider/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ func TestQueryConsumerValidators(t *testing.T) {
require.Equal(t, res.Validators[0].ProviderAddress, providerAddr1.String())

// update consumer TopN param
pk.SetConsumerPowerShapingParameters(ctx, consumerId, types.PowerShapingParameters{Top_N: 50})
err = pk.SetConsumerPowerShapingParameters(ctx, consumerId, types.PowerShapingParameters{Top_N: 50})
require.NoError(t, err)

// expect both opted-in and topN validator
expRes := types.QueryConsumerValidatorsResponse{
Expand Down Expand Up @@ -445,13 +446,14 @@ func TestGetConsumerChain(t *testing.T) {
clientID := fmt.Sprintf("client-%d", len(consumerID)-i)
topN := topNs[i]
pk.SetConsumerClientId(ctx, consumerID, clientID)
pk.SetConsumerPowerShapingParameters(ctx, consumerID, types.PowerShapingParameters{
err := pk.SetConsumerPowerShapingParameters(ctx, consumerID, types.PowerShapingParameters{
Top_N: topN,
ValidatorSetCap: validatorSetCaps[i],
ValidatorsPowerCap: validatorPowerCaps[i],
MinStake: minStakes[i].Uint64(),
AllowInactiveVals: allowInactiveVals[i],
})
require.NoError(t, err)
pk.SetMinimumPowerInTopN(ctx, consumerID, expectedMinPowerInTopNs[i])
for _, addr := range allowlists[i] {
pk.SetAllowlist(ctx, consumerID, addr)
Expand Down Expand Up @@ -532,7 +534,8 @@ func TestQueryConsumerChain(t *testing.T) {
_, err = providerKeeper.QueryConsumerChain(ctx, &req)
require.Error(t, err)

providerKeeper.SetConsumerMetadata(ctx, consumerId, types.ConsumerMetadata{Name: chainId})
err = providerKeeper.SetConsumerMetadata(ctx, consumerId, types.ConsumerMetadata{Name: chainId})
require.NoError(t, err)

expRes := types.QueryConsumerChainResponse{
ChainId: chainId,
Expand All @@ -548,17 +551,19 @@ func TestQueryConsumerChain(t *testing.T) {
require.NoError(t, err)
require.Equal(t, &expRes, res)

providerKeeper.SetConsumerInitializationParameters(
err = providerKeeper.SetConsumerInitializationParameters(
ctx,
consumerId,
types.ConsumerInitializationParameters{SpawnTime: ctx.BlockTime()},
)
require.NoError(t, err)

providerKeeper.SetConsumerPowerShapingParameters(
err = providerKeeper.SetConsumerPowerShapingParameters(
ctx,
consumerId,
types.PowerShapingParameters{Top_N: uint32(50)},
)
require.NoError(t, err)

expRes.InitParams = &types.ConsumerInitializationParameters{SpawnTime: ctx.BlockTime()}
expRes.PowerShapingParams = &types.PowerShapingParameters{Top_N: uint32(50)}
Expand Down
14 changes: 9 additions & 5 deletions x/ccv/provider/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,12 @@ func TestUpdateMinimumPowerInTopN(t *testing.T) {
consumerId := "0"

// test case where Top N is 0 in which case there's no minimum power in top N
providerKeeper.SetConsumerPowerShapingParameters(ctx, consumerId, providertypes.PowerShapingParameters{
err := providerKeeper.SetConsumerPowerShapingParameters(ctx, consumerId, providertypes.PowerShapingParameters{
Top_N: 0,
})
require.NoError(t, err)

err := providerKeeper.UpdateMinimumPowerInTopN(ctx, consumerId, 0, 0)
err = providerKeeper.UpdateMinimumPowerInTopN(ctx, consumerId, 0, 0)
require.NoError(t, err)
_, found := providerKeeper.GetMinimumPowerInTopN(ctx, consumerId)
require.False(t, found)
Expand All @@ -607,29 +608,32 @@ func TestUpdateMinimumPowerInTopN(t *testing.T) {
providerKeeper.SetParams(ctx, params)

// when top N is 50, the minimum power is 30 (because top validator has to validate)
providerKeeper.SetConsumerPowerShapingParameters(ctx, consumerId, providertypes.PowerShapingParameters{
err = providerKeeper.SetConsumerPowerShapingParameters(ctx, consumerId, providertypes.PowerShapingParameters{
Top_N: 50,
})
require.NoError(t, err)
err = providerKeeper.UpdateMinimumPowerInTopN(ctx, consumerId, 0, 50)
require.NoError(t, err)
minimumPowerInTopN, found := providerKeeper.GetMinimumPowerInTopN(ctx, consumerId)
require.True(t, found)
require.Equal(t, int64(30), minimumPowerInTopN)

// when top N is 51, the minimum power is 20 (because top 2 validators have to validate)
providerKeeper.SetConsumerPowerShapingParameters(ctx, consumerId, providertypes.PowerShapingParameters{
err = providerKeeper.SetConsumerPowerShapingParameters(ctx, consumerId, providertypes.PowerShapingParameters{
Top_N: 51,
})
require.NoError(t, err)
err = providerKeeper.UpdateMinimumPowerInTopN(ctx, consumerId, 50, 51)
require.NoError(t, err)
minimumPowerInTopN, found = providerKeeper.GetMinimumPowerInTopN(ctx, consumerId)
require.True(t, found)
require.Equal(t, int64(20), minimumPowerInTopN)

// when top N is 100, the minimum power is 10 (that of the validator with the lowest power)
providerKeeper.SetConsumerPowerShapingParameters(ctx, consumerId, providertypes.PowerShapingParameters{
err = providerKeeper.SetConsumerPowerShapingParameters(ctx, consumerId, providertypes.PowerShapingParameters{
Top_N: 100,
})
require.NoError(t, err)
err = providerKeeper.UpdateMinimumPowerInTopN(ctx, consumerId, 51, 100)
require.NoError(t, err)
minimumPowerInTopN, found = providerKeeper.GetMinimumPowerInTopN(ctx, consumerId)
Expand Down
27 changes: 18 additions & 9 deletions x/ccv/provider/keeper/partial_set_security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ func TestCanValidateChain(t *testing.T) {

// with TopN chains, the validator can be considered,
mocks.MockStakingKeeper.EXPECT().GetValidatorByConsAddr(gomock.Any(), providerAddr.Address).Return(validator, nil).Times(2)
providerKeeper.SetConsumerPowerShapingParameters(ctx, consumerID, types.PowerShapingParameters{Top_N: 50})
err = providerKeeper.SetConsumerPowerShapingParameters(ctx, consumerID, types.PowerShapingParameters{Top_N: 50})
require.NoError(t, err)
powerShapingParameters, err = providerKeeper.GetConsumerPowerShapingParameters(ctx, consumerID)
require.NoError(t, err)
// validator's power is LT the min power
Expand All @@ -422,7 +423,8 @@ func TestCanValidateChain(t *testing.T) {
require.True(t, providerKeeper.CanValidateChain(ctx, consumerID, providerAddr, powerShapingParameters.Top_N, 2))

// With OptIn chains, validator can validate only if it has already opted-in
providerKeeper.SetConsumerPowerShapingParameters(ctx, consumerID, types.PowerShapingParameters{Top_N: 0})
err = providerKeeper.SetConsumerPowerShapingParameters(ctx, consumerID, types.PowerShapingParameters{Top_N: 0})
require.NoError(t, err)
powerShapingParameters, err = providerKeeper.GetConsumerPowerShapingParameters(ctx, consumerID)
require.NoError(t, err)
require.True(t, providerKeeper.CanValidateChain(ctx, consumerID, providerAddr, powerShapingParameters.Top_N, 2))
Expand Down Expand Up @@ -471,41 +473,46 @@ func TestCapValidatorSet(t *testing.T) {
consumerValidators := providerKeeper.CapValidatorSet(ctx, powerShapingParameters, validators)
require.Equal(t, validators, consumerValidators)

providerKeeper.SetConsumerPowerShapingParameters(ctx, "consumerId", types.PowerShapingParameters{
err = providerKeeper.SetConsumerPowerShapingParameters(ctx, "consumerId", types.PowerShapingParameters{
ValidatorSetCap: 0,
})
require.NoError(t, err)
powerShapingParameters, err = providerKeeper.GetConsumerPowerShapingParameters(ctx, "consumerId")
require.NoError(t, err)
consumerValidators = providerKeeper.CapValidatorSet(ctx, powerShapingParameters, validators)
require.Equal(t, validators, consumerValidators)

providerKeeper.SetConsumerPowerShapingParameters(ctx, "consumerId", types.PowerShapingParameters{
err = providerKeeper.SetConsumerPowerShapingParameters(ctx, "consumerId", types.PowerShapingParameters{
ValidatorSetCap: 100,
})
require.NoError(t, err)
powerShapingParameters, err = providerKeeper.GetConsumerPowerShapingParameters(ctx, "consumerId")
require.NoError(t, err)
consumerValidators = providerKeeper.CapValidatorSet(ctx, powerShapingParameters, validators)
require.Equal(t, validators, consumerValidators)

providerKeeper.SetConsumerPowerShapingParameters(ctx, "consumerId", types.PowerShapingParameters{
err = providerKeeper.SetConsumerPowerShapingParameters(ctx, "consumerId", types.PowerShapingParameters{
ValidatorSetCap: 1,
})
require.NoError(t, err)
powerShapingParameters, err = providerKeeper.GetConsumerPowerShapingParameters(ctx, "consumerId")
require.NoError(t, err)
consumerValidators = providerKeeper.CapValidatorSet(ctx, powerShapingParameters, validators)
require.Equal(t, []types.ConsensusValidator{validatorC}, consumerValidators)

providerKeeper.SetConsumerPowerShapingParameters(ctx, "consumerId", types.PowerShapingParameters{
err = providerKeeper.SetConsumerPowerShapingParameters(ctx, "consumerId", types.PowerShapingParameters{
ValidatorSetCap: 2,
})
require.NoError(t, err)
powerShapingParameters, err = providerKeeper.GetConsumerPowerShapingParameters(ctx, "consumerId")
require.NoError(t, err)
consumerValidators = providerKeeper.CapValidatorSet(ctx, powerShapingParameters, validators)
require.Equal(t, []types.ConsensusValidator{validatorC, validatorB}, consumerValidators)

providerKeeper.SetConsumerPowerShapingParameters(ctx, "consumerId", types.PowerShapingParameters{
err = providerKeeper.SetConsumerPowerShapingParameters(ctx, "consumerId", types.PowerShapingParameters{
ValidatorSetCap: 3,
})
require.NoError(t, err)
powerShapingParameters, err = providerKeeper.GetConsumerPowerShapingParameters(ctx, "consumerId")
require.NoError(t, err)
consumerValidators = providerKeeper.CapValidatorSet(ctx, powerShapingParameters, validators)
Expand Down Expand Up @@ -563,9 +570,10 @@ func TestCapValidatorsPower(t *testing.T) {
sortValidators(cappedValidators)
require.Equal(t, validators, cappedValidators)

providerKeeper.SetConsumerPowerShapingParameters(ctx, "consumerId", types.PowerShapingParameters{
err = providerKeeper.SetConsumerPowerShapingParameters(ctx, "consumerId", types.PowerShapingParameters{
ValidatorsPowerCap: 33,
})
require.NoError(t, err)
powerShapingParameters, err = providerKeeper.GetConsumerPowerShapingParameters(ctx, "consumerId")
require.NoError(t, err)
cappedValidators = providerKeeper.CapValidatorsPower(ctx, powerShapingParameters.ValidatorsPowerCap, validators)
Expand Down Expand Up @@ -858,10 +866,11 @@ func TestIfInactiveValsDisallowedProperty(t *testing.T) {
// Set up the parameters in the provider keeper

// do not allow inactive validators
providerKeeper.SetConsumerPowerShapingParameters(ctx, "consumerId", types.PowerShapingParameters{
err := providerKeeper.SetConsumerPowerShapingParameters(ctx, "consumerId", types.PowerShapingParameters{
MinStake: minStake,
AllowInactiveVals: false,
})
require.NoError(t, err)
params := providerKeeper.GetParams(ctx)
params.MaxProviderConsensusValidators = int64(maxProviderConsensusVals)
providerKeeper.SetParams(ctx, params)
Expand Down
Loading

0 comments on commit c226518

Please sign in to comment.