Skip to content

Commit

Permalink
fix(oracle): fix pairVotes error and return type
Browse files Browse the repository at this point in the history
  • Loading branch information
SpekalsG3 committed Nov 7, 2024
1 parent 9f757b0 commit cb2210a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 27 deletions.
38 changes: 14 additions & 24 deletions x/oracle/keeper/ballot.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

Check failure on line 1 in x/oracle/keeper/ballot.go

View workflow job for this annotation

GitHub Actions / lint

: # github.com/archway-network/archway/x/oracle/keeper

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

"cosmossdk.io/math"
Expand All @@ -20,8 +22,8 @@ import (
func (k Keeper) GroupVotesByPair(
ctx sdk.Context,
validatorPerformances types.ValidatorPerformances,
) (pairVotes map[asset.Pair]types.ExchangeRateVotes) {
pairVotes = map[asset.Pair]types.ExchangeRateVotes{}
) map[asset.Pair]types.ExchangeRateVotes {
pairVotes := map[asset.Pair]types.ExchangeRateVotes{}

err := k.Votes.Walk(ctx, nil, func(voterAddrBytes []byte, aggregateVote types.AggregateExchangeRateVote) (bool, error) {
voterAddr := sdk.ValAddress(voterAddrBytes)
Expand Down Expand Up @@ -55,7 +57,7 @@ func (k Keeper) GroupVotesByPair(
panic(err)
}

return
return pairVotes
}

// ClearVotesAndPrevotes clears all tallied prevotes and votes from the store
Expand All @@ -73,42 +75,30 @@ func (k Keeper) ClearVotesAndPrevotes(ctx sdk.Context, votePeriod uint64) {
})

// Clear all aggregate votes
iter, err := k.Votes.Iterate(ctx, nil)
if err != nil {
k.Logger(ctx).Error("failed to get votes iterator", "error", err)
return
}
keys, err := iter.Keys()
err := k.Votes.Clear(ctx, nil)
if err != nil {
k.Logger(ctx).Error("failed to get keys for votes iterator", "error", err)
return
}
for _, valAddr := range keys {
err := k.Votes.Remove(ctx, valAddr)
if err != nil {
k.Logger(ctx).Error("failed to delete vote", "error", err)
}
k.Logger(ctx).Error("failed to clear votes", "error", err)
}
}

// IsPassingVoteThreshold votes is passing the threshold amount of voting power
func IsPassingVoteThreshold(
votes types.ExchangeRateVotes, thresholdVotingPower math.Int, minVoters uint64,
) bool {
) error {
totalPower := math.NewInt(votes.Power())
if totalPower.IsZero() {
return false
return fmt.Errorf("total voting power is 0")
}

if totalPower.LT(thresholdVotingPower) {
return false
return fmt.Errorf("total voting power is less then thresholdVotingPower (%g)", thresholdVotingPower)
}

if votes.NumValidVoters() < minVoters {
return false
return fmt.Errorf("number of validators (%d) is less then minVoters (%d)", votes.NumValidVoters(), minVoters)
}

return true
return nil
}

// RemoveInvalidVotes removes the votes which have not reached the vote
Expand Down Expand Up @@ -141,11 +131,11 @@ func (k Keeper) RemoveInvalidVotes(

// If the votes is not passed, remove it from the whitelistedPairs set
// to prevent slashing validators who did valid vote.
if !IsPassingVoteThreshold(
if err := IsPassingVoteThreshold(
votes,
k.VoteThreshold(ctx).MulInt64(totalBondedPower).RoundInt(),
k.MinVoters(ctx),
) {
); err != nil {
whitelistedPairs.Remove(pair)
delete(pairVotes, pair)
continue
Expand Down
2 changes: 1 addition & 1 deletion x/oracle/keeper/ballot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,5 +375,5 @@ func TestZeroBallotPower(t *testing.T) {
types.NewExchangeRateVote(math.LegacyNewDec(6), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[2], 0),
}

assert.False(t, keeper.IsPassingVoteThreshold(btcVotess, math.ZeroInt(), 0))
assert.NoError(t, keeper.IsPassingVoteThreshold(btcVotess, math.ZeroInt(), 0))
}
4 changes: 2 additions & 2 deletions x/oracle/keeper/update_exchange_rates.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func (k Keeper) getPairVotes(
ctx sdk.Context,
validatorPerformances types.ValidatorPerformances,
whitelistedPairs set.Set[asset.Pair],
) (pairVotes map[asset.Pair]types.ExchangeRateVotes) {
pairVotes = k.GroupVotesByPair(ctx, validatorPerformances)
) map[asset.Pair]types.ExchangeRateVotes {
pairVotes := k.GroupVotesByPair(ctx, validatorPerformances)

k.RemoveInvalidVotes(ctx, pairVotes, whitelistedPairs)

Expand Down

0 comments on commit cb2210a

Please sign in to comment.