Skip to content

Commit

Permalink
chore: added missing changes
Browse files Browse the repository at this point in the history
  • Loading branch information
troykessler committed Dec 20, 2024
1 parent 0afe6e0 commit 1e3c457
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
26 changes: 22 additions & 4 deletions x/stakers/keeper/exported_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func (k Keeper) GetPaginatedStakersByPoolStake(ctx sdk.Context, pagination *quer
addresses = append(addresses, address)
}

// TODO: is this too expensive?
if stakerStatus == querytypes.STAKER_STATUS_UNSPECIFIED || stakerStatus == querytypes.STAKER_STATUS_PROTOCOL_ACTIVE {
sort.Slice(addresses, func(i, j int) bool {
return k.GetValidatorTotalPoolStake(ctx, addresses[i]) > k.GetValidatorTotalPoolStake(ctx, addresses[j])
Expand Down Expand Up @@ -261,7 +260,7 @@ func (k Keeper) GetValidatorPoolStakes(ctx sdk.Context, poolId uint64, mustInclu
totalStakeRemainder := totalStake

// sort descending based on stake
sort.Slice(validators, func(i, j int) bool {
sort.SliceStable(validators, func(i, j int) bool {
return validators[i].Stake > validators[j].Stake
})

Expand All @@ -270,6 +269,8 @@ func (k Keeper) GetValidatorPoolStakes(ctx sdk.Context, poolId uint64, mustInclu
return stakes
}

var lastCutoffIndex int

for i, validator := range validators {
// check if the validator has a higher stake than allowed by the max voting power
if math.LegacyNewDec(int64(stakes[validator.Address])).GT(maxVotingPower.MulInt64(totalStake)) {
Expand All @@ -288,9 +289,20 @@ func (k Keeper) GetValidatorPoolStakes(ctx sdk.Context, poolId uint64, mustInclu
stakes[v.Address] += uint64(math.LegacyNewDec(int64(v.Stake)).QuoInt64(totalStakeRemainder).MulInt64(cutoffAmount).TruncateInt64())
}
}

lastCutoffIndex = i
} else {
// if we reach the first validator who is below the max voting power we know that the remaining
// ones will be also below it
break
}
}

// if no amounts got cut off we can return already
if totalStakeRemainder == totalStake {
return stakes
}

// after we have redistributed all cutoff amounts so that no validator exceeds the maximum voting power
// based on their remaining effective stake we now scale the stakes to get the true effective staking amount.
// This is because while the top validators who got their voting power reduced the lower validators have actually
Expand All @@ -309,8 +321,14 @@ func (k Keeper) GetValidatorPoolStakes(ctx sdk.Context, poolId uint64, mustInclu
}

// scale all effective stakes down to scale factor
for _, validator := range validators {
stakes[validator.Address] = uint64(scaleFactor.MulInt64(int64(stakes[validator.Address])).RoundInt64())
for i, validator := range validators {
// for all validators who got cut off we always round down to ensure that their voting power actually
// stays below the max voting power
if i <= lastCutoffIndex {
stakes[validator.Address] = uint64(scaleFactor.MulInt64(int64(stakes[validator.Address])).TruncateInt64())
} else {
stakes[validator.Address] = uint64(scaleFactor.MulInt64(int64(stakes[validator.Address])).Ceil().TruncateInt64())
}
}

// the result is a map which contains the effective stake for every validator in a pool. The effective stake
Expand Down
18 changes: 9 additions & 9 deletions x/stakers/keeper/keeper_suite_effective_stake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ var _ = Describe("keeper_suite_effective_stake_test.go", Ordered, func() {
Expect(s.App().StakersKeeper.IsVotingPowerTooHigh(s.Ctx(), 0)).To(BeFalse())

Expect(s.App().StakersKeeper.GetValidatorPoolStake(s.Ctx(), i.STAKER_0, 0)).To(Equal(100 * i.KYVE))
Expect(s.App().StakersKeeper.GetValidatorPoolStake(s.Ctx(), i.STAKER_1, 0)).To(Equal(200 * i.KYVE))
Expect(s.App().StakersKeeper.GetValidatorPoolStake(s.Ctx(), i.STAKER_1, 0)).To(Equal(200*i.KYVE - 1))
Expect(s.App().StakersKeeper.GetValidatorPoolStake(s.Ctx(), i.STAKER_2, 0)).To(Equal(100 * i.KYVE))

Expect(s.App().StakersKeeper.GetTotalStakeOfPool(s.Ctx(), 0)).To(Equal(400 * i.KYVE))
Expect(s.App().StakersKeeper.GetTotalStakeOfPool(s.Ctx(), 0)).To(Equal(400*i.KYVE - 1))
})

It("Test effective stake with multiple validators above the max pool voting power", func() {
Expand Down Expand Up @@ -262,11 +262,11 @@ var _ = Describe("keeper_suite_effective_stake_test.go", Ordered, func() {
// ASSERT
Expect(s.App().StakersKeeper.IsVotingPowerTooHigh(s.Ctx(), 0)).To(BeFalse())

Expect(s.App().StakersKeeper.GetValidatorPoolStake(s.Ctx(), i.STAKER_0, 0)).To(Equal(100 * i.KYVE))
Expect(s.App().StakersKeeper.GetValidatorPoolStake(s.Ctx(), i.STAKER_0, 0)).To(Equal(100*i.KYVE - 1))
Expect(s.App().StakersKeeper.GetValidatorPoolStake(s.Ctx(), i.STAKER_1, 0)).To(Equal(0 * i.KYVE))
Expect(s.App().StakersKeeper.GetValidatorPoolStake(s.Ctx(), i.STAKER_2, 0)).To(Equal(100 * i.KYVE))

Expect(s.App().StakersKeeper.GetTotalStakeOfPool(s.Ctx(), 0)).To(Equal(200 * i.KYVE))
Expect(s.App().StakersKeeper.GetTotalStakeOfPool(s.Ctx(), 0)).To(Equal(200*i.KYVE - 1))
})

It("Test effective stake with all validators having zero delegation", func() {
Expand Down Expand Up @@ -449,10 +449,10 @@ var _ = Describe("keeper_suite_effective_stake_test.go", Ordered, func() {
Expect(s.App().StakersKeeper.IsVotingPowerTooHigh(s.Ctx(), 0)).To(BeFalse())

Expect(s.App().StakersKeeper.GetValidatorPoolStake(s.Ctx(), i.STAKER_0, 0)).To(Equal(100 * i.KYVE))
Expect(s.App().StakersKeeper.GetValidatorPoolStake(s.Ctx(), i.STAKER_1, 0)).To(Equal(200 * i.KYVE))
Expect(s.App().StakersKeeper.GetValidatorPoolStake(s.Ctx(), i.STAKER_1, 0)).To(Equal(200*i.KYVE - 1))
Expect(s.App().StakersKeeper.GetValidatorPoolStake(s.Ctx(), i.STAKER_2, 0)).To(Equal(100 * i.KYVE))

Expect(s.App().StakersKeeper.GetTotalStakeOfPool(s.Ctx(), 0)).To(Equal(400 * i.KYVE))
Expect(s.App().StakersKeeper.GetTotalStakeOfPool(s.Ctx(), 0)).To(Equal(400*i.KYVE - 1))
})

It("Test effective stake with multiple validators above the max pool voting power due to stake fractions", func() {
Expand Down Expand Up @@ -492,11 +492,11 @@ var _ = Describe("keeper_suite_effective_stake_test.go", Ordered, func() {
// ASSERT
Expect(s.App().StakersKeeper.IsVotingPowerTooHigh(s.Ctx(), 0)).To(BeFalse())

Expect(s.App().StakersKeeper.GetValidatorPoolStake(s.Ctx(), i.STAKER_0, 0)).To(Equal(uint64(23333333334)))
Expect(s.App().StakersKeeper.GetValidatorPoolStake(s.Ctx(), i.STAKER_1, 0)).To(Equal(uint64(23333333334)))
Expect(s.App().StakersKeeper.GetValidatorPoolStake(s.Ctx(), i.STAKER_0, 0)).To(Equal(uint64(23333333333)))
Expect(s.App().StakersKeeper.GetValidatorPoolStake(s.Ctx(), i.STAKER_1, 0)).To(Equal(uint64(23333333333)))
Expect(s.App().StakersKeeper.GetValidatorPoolStake(s.Ctx(), i.STAKER_2, 0)).To(Equal(20 * i.KYVE))

Expect(s.App().StakersKeeper.GetTotalStakeOfPool(s.Ctx(), 0)).To(Equal(uint64(66666666668)))
Expect(s.App().StakersKeeper.GetTotalStakeOfPool(s.Ctx(), 0)).To(Equal(uint64(66666666666)))
})

It("Test effective stake with some validators having zero delegation due to stake fractions", func() {
Expand Down

0 comments on commit 1e3c457

Please sign in to comment.