Skip to content

Commit

Permalink
Test other modified functions
Browse files Browse the repository at this point in the history
  • Loading branch information
p-offtermatt committed Jul 18, 2024
1 parent d6bcfe0 commit e77d510
Showing 1 changed file with 63 additions and 28 deletions.
91 changes: 63 additions & 28 deletions x/ccv/provider/keeper/staking_keeper_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sort"
"testing"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand All @@ -12,44 +13,67 @@ import (
"github.com/stretchr/testify/require"
)

// Tests that IterateBondedValidatorsByPower returns the correct validators,
// namely the ones with most power, and stops when the max number of validators is reached.
func TestIterateBondedValidatorsByPower(t *testing.T) {
// TestStakingKeeperInterface tests
// * IterateBondedValidatorsByPower
// * TotalBondedTokens
// * BondedRatio
func TestStakingKeeperInterface(t *testing.T) {
tests := []struct {
name string
maxValidators int64
powers []int64
expectedPowers []int64
name string
maxValidators int64
powers []int64
expectedPowers []int64
mockTotalSupply int64
expectedTotalBonded math.Int
expectedBondedRatio math.LegacyDec
}{
{
name: "no validators",
maxValidators: 5,
powers: []int64{},
expectedPowers: []int64{},
name: "no validators",
maxValidators: 5,
powers: []int64{},
expectedPowers: []int64{},
mockTotalSupply: 100,
expectedTotalBonded: math.ZeroInt(),
expectedBondedRatio: math.LegacyZeroDec(),
},
{
name: "validators less than max",
maxValidators: 5,
powers: []int64{10, 20, 30},
expectedPowers: []int64{10, 20, 30}, // get all validators back
name: "validators less than max",
maxValidators: 5,
powers: []int64{10, 20, 30},
expectedPowers: []int64{10, 20, 30}, // get all validators back
mockTotalSupply: 100,
expectedTotalBonded: math.NewInt(60),
expectedBondedRatio: math.LegacyNewDec(60).Quo(math.LegacyNewDec(100)), // 60% bonded
},
{
name: "validators more than max",
maxValidators: 2,
powers: []int64{10, 20, 30},
expectedPowers: []int64{30, 20}, // get the top 2 validators back
name: "validators more than max",
maxValidators: 2,
powers: []int64{10, 20, 30},
expectedPowers: []int64{30, 20}, // get the top 2 validators back
mockTotalSupply: 100,
// 30 + 20 = 50
expectedTotalBonded: math.NewInt(50),
expectedBondedRatio: math.LegacyNewDec(50).Quo(math.LegacyNewDec(100)), // 50% bonded
},
{
name: "validators equal to max",
maxValidators: 3,
powers: []int64{10, 20, 30},
expectedPowers: []int64{30, 20, 10}, // get all validators back
name: "validators equal to max",
maxValidators: 3,
powers: []int64{10, 20, 30},
expectedPowers: []int64{30, 20, 10}, // get all validators back
mockTotalSupply: 60,
// 30 + 20 + 10 = 60
expectedTotalBonded: math.NewInt(60),
expectedBondedRatio: math.LegacyNewDec(100).Quo(math.LegacyNewDec(100)), // 100% bonded
},
{
name: "validators with equal powers",
maxValidators: 3,
powers: []int64{10, 10, 10, 20, 20, 30, 30},
expectedPowers: []int64{30, 30, 20}, // get the top 3 validators back
name: "validators with equal powers",
maxValidators: 3,
powers: []int64{10, 10, 10, 20, 20, 30, 30},
expectedPowers: []int64{30, 30, 20}, // get the top 3 validators back
mockTotalSupply: 1000,
// 30 + 30 + 20 = 80
expectedTotalBonded: math.NewInt(80),
expectedBondedRatio: math.LegacyNewDec(8).Quo(math.LegacyNewDec(100)), // 8% bonded
},
}

Expand Down Expand Up @@ -81,7 +105,7 @@ func TestIterateBondedValidatorsByPower(t *testing.T) {
}
}
return nil
})
}).AnyTimes()
actualValPowers := []int64{}
err := providerKeeper.IterateBondedValidatorsByPower(ctx, func(index int64, validator types.ValidatorI) (stop bool) {
counter++
Expand All @@ -91,6 +115,17 @@ func TestIterateBondedValidatorsByPower(t *testing.T) {
require.NoError(t, err)
// we don't check that the elements exactly match; just matching the powers is good enough
require.ElementsMatch(t, tt.expectedPowers, actualValPowers)

// check bonded ratio and total bonded
mocks.MockStakingKeeper.EXPECT().StakingTokenSupply(gomock.Any()).Return(math.NewInt(tt.mockTotalSupply), nil).AnyTimes()

totalBonded, err := providerKeeper.TotalBondedTokens(ctx)
require.NoError(t, err)
require.Equal(t, tt.expectedTotalBonded, totalBonded)

bondedRatio, err := providerKeeper.BondedRatio(ctx)
require.NoError(t, err)
require.Equal(t, tt.expectedBondedRatio, bondedRatio)
})
}
}

0 comments on commit e77d510

Please sign in to comment.