diff --git a/x/ccv/provider/keeper/staking_keeper_interface_test.go b/x/ccv/provider/keeper/staking_keeper_interface_test.go index 99d5529d87..510756bf12 100644 --- a/x/ccv/provider/keeper/staking_keeper_interface_test.go +++ b/x/ccv/provider/keeper/staking_keeper_interface_test.go @@ -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" @@ -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 }, } @@ -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++ @@ -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) }) } }