Less function to not use default power reduction
This commit is contained in:
parent
7b5cf61eef
commit
1c1e67fa51
@ -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)
|
||||
|
||||
@ -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{
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user