From 1c1e67fa51103c5da7f3258550867b0c1eaa92c0 Mon Sep 17 00:00:00 2001 From: psaradev Date: Tue, 2 Feb 2021 13:12:05 +1000 Subject: [PATCH] Less function to not use default power reduction --- x/staking/keeper/historical_info_test.go | 22 ++++++++++++++++++---- x/staking/types/historical_info.go | 2 +- x/staking/types/validator.go | 6 +----- x/staking/types/validator_test.go | 4 +++- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/x/staking/keeper/historical_info_test.go b/x/staking/keeper/historical_info_test.go index 727fe58959..db6c6a6b47 100644 --- a/x/staking/keeper/historical_info_test.go +++ b/x/staking/keeper/historical_info_test.go @@ -1,7 +1,6 @@ package keeper_test import ( - "sort" "testing" "github.com/stretchr/testify/require" @@ -13,6 +12,17 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/types" ) +// IsValSetSorted reports whether valset is sorted. +func IsValSetSorted(data []types.Validator, powerReduction sdk.Int) bool { + n := len(data) + for i := n - 1; i > 0; i-- { + if types.ValidatorsByVotingPower(data).Less(i, i-1, powerReduction) { + return false + } + } + return true +} + func TestHistoricalInfo(t *testing.T) { _, app, ctx := createTestInput() @@ -31,7 +41,7 @@ func TestHistoricalInfo(t *testing.T) { recv, found := app.StakingKeeper.GetHistoricalInfo(ctx, 2) require.True(t, found, "HistoricalInfo not found after set") require.Equal(t, hi, recv, "HistoricalInfo not equal") - require.True(t, sort.IsSorted(types.ValidatorsByVotingPower(recv.Valset)), "HistoricalInfo validators is not sorted") + require.True(t, IsValSetSorted(recv.Valset, app.StakingKeeper.PowerReduction(ctx)), "HistoricalInfo validators is not sorted") app.StakingKeeper.DeleteHistoricalInfo(ctx, 2) @@ -78,14 +88,18 @@ func TestTrackHistoricalInfo(t *testing.T) { // Set bonded validators in keeper val1 := teststaking.NewValidator(t, addrVals[2], PKs[2]) + val1.Status = types.Bonded // when not bonded, consensus power is Zero + val1.Tokens = app.StakingKeeper.TokensFromConsensusPower(ctx, 10) app.StakingKeeper.SetValidator(ctx, val1) app.StakingKeeper.SetLastValidatorPower(ctx, val1.GetOperator(), 10) val2 := teststaking.NewValidator(t, addrVals[3], PKs[3]) + val1.Status = types.Bonded + val2.Tokens = app.StakingKeeper.TokensFromConsensusPower(ctx, 80) app.StakingKeeper.SetValidator(ctx, val2) app.StakingKeeper.SetLastValidatorPower(ctx, val2.GetOperator(), 80) vals := []types.Validator{val1, val2} - sort.Sort(types.ValidatorsByVotingPower(vals)) + IsValSetSorted(vals, app.StakingKeeper.PowerReduction(ctx)) // Set Header for BeginBlock context header := tmproto.Header{ @@ -103,7 +117,7 @@ func TestTrackHistoricalInfo(t *testing.T) { } recv, found = app.StakingKeeper.GetHistoricalInfo(ctx, 10) require.True(t, found, "GetHistoricalInfo failed after BeginBlock") - require.Equal(t, expected, recv, "GetHistoricalInfo returned eunexpected result") + require.Equal(t, expected, recv, "GetHistoricalInfo returned unexpected result") // Check HistoricalInfo at height 5, 4 is pruned recv, found = app.StakingKeeper.GetHistoricalInfo(ctx, 4) diff --git a/x/staking/types/historical_info.go b/x/staking/types/historical_info.go index 5a278a7e54..6e474bb0c6 100644 --- a/x/staking/types/historical_info.go +++ b/x/staking/types/historical_info.go @@ -17,7 +17,7 @@ import ( func NewHistoricalInfo(header tmproto.Header, valSet Validators, powerReduction sdk.Int) HistoricalInfo { // Must sort in the same way that tendermint does sort.SliceStable(valSet, func(i, j int) bool { - return ValidatorsByVotingPower(valSet).LessAfterPowerReductionApply(i, j, powerReduction) + return ValidatorsByVotingPower(valSet).Less(i, j, powerReduction) }) return HistoricalInfo{ diff --git a/x/staking/types/validator.go b/x/staking/types/validator.go index 5cf55db5d6..d8b113bbc5 100644 --- a/x/staking/types/validator.go +++ b/x/staking/types/validator.go @@ -116,11 +116,7 @@ type ValidatorsByVotingPower []Validator func (valz ValidatorsByVotingPower) Len() int { return len(valz) } -func (valz ValidatorsByVotingPower) Less(i, j int) bool { - return valz.LessAfterPowerReductionApply(i, j, sdk.DefaultPowerReduction) -} - -func (valz ValidatorsByVotingPower) LessAfterPowerReductionApply(i, j int, powerReduction sdk.Int) bool { +func (valz ValidatorsByVotingPower) Less(i, j int, powerReduction sdk.Int) bool { if valz[i].ConsensusPower(powerReduction) == valz[j].ConsensusPower(powerReduction) { addrI, errI := valz[i].GetConsAddr() addrJ, errJ := valz[j].GetConsAddr() diff --git a/x/staking/types/validator_test.go b/x/staking/types/validator_test.go index f7888b7e42..8601fbeec7 100644 --- a/x/staking/types/validator_test.go +++ b/x/staking/types/validator_test.go @@ -295,7 +295,9 @@ func TestValidatorsSortTendermint(t *testing.T) { sort.Sort(tmtypes.ValidatorsByVotingPower(expectedVals)) // sort in SDK and then convert to tendermint - sort.Sort(types.ValidatorsByVotingPower(valz)) + sort.SliceStable(valz, func(i, j int) bool { + return types.ValidatorsByVotingPower(valz).Less(i, j, sdk.DefaultPowerReduction) + }) actualVals, err := teststaking.ToTmValidators(valz, sdk.DefaultPowerReduction) require.NoError(t, err)