diff --git a/x/oracle/keeper/ballot.go b/x/oracle/keeper/ballot.go index aa66cab1..97befd87 100644 --- a/x/oracle/keeper/ballot.go +++ b/x/oracle/keeper/ballot.go @@ -1,6 +1,8 @@ package keeper import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" "cosmossdk.io/math" @@ -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) @@ -55,7 +57,7 @@ func (k Keeper) GroupVotesByPair( panic(err) } - return + return pairVotes } // ClearVotesAndPrevotes clears all tallied prevotes and votes from the store @@ -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 @@ -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 diff --git a/x/oracle/keeper/ballot_test.go b/x/oracle/keeper/ballot_test.go index 339ce193..08c1d8be 100644 --- a/x/oracle/keeper/ballot_test.go +++ b/x/oracle/keeper/ballot_test.go @@ -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)) } diff --git a/x/oracle/keeper/update_exchange_rates.go b/x/oracle/keeper/update_exchange_rates.go index fc65f4a0..5647321d 100644 --- a/x/oracle/keeper/update_exchange_rates.go +++ b/x/oracle/keeper/update_exchange_rates.go @@ -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)