Skip to content

Commit

Permalink
fix: oracle.RewardBallotWinner detect duplicated uumee (#1432)
Browse files Browse the repository at this point in the history
## Description



---

### Author Checklist

_All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues._

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] added appropriate labels to the PR
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/umee-network/umee/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

_All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items._

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
robert-zaremba committed Sep 20, 2022
1 parent 09bbf02 commit abcdb11
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
11 changes: 11 additions & 0 deletions util/genmap/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package genmap

// Contains returns true if x is in ls
func Contains[T comparable](x T, ls []T) bool {
for i := range ls {
if ls[i] == x {
return true
}
}
return false
}
23 changes: 13 additions & 10 deletions x/oracle/keeper/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@ import (

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

"github.com/umee-network/umee/v3/util/genmap"
"github.com/umee-network/umee/v3/x/oracle/types"
)

// prependUmeeIfUnique pushs `uumee` denom to the front of the list, if it is not yet included.
func prependUmeeIfUnique(voteTargets []string) []string {
if genmap.Contains(types.UmeeDenom, voteTargets) {
return voteTargets
}
rewardDenoms := make([]string, len(voteTargets)+1)
rewardDenoms[0] = types.UmeeDenom
copy(rewardDenoms[1:], voteTargets)
return rewardDenoms
}

// RewardBallotWinners is executed at the end of every voting period, where we
// give out a portion of seigniorage reward(reward-weight) to the oracle voters
// that voted correctly.
Expand All @@ -18,15 +30,6 @@ func (k Keeper) RewardBallotWinners(
voteTargets []string,
ballotWinners []types.Claim,
) {
rewardDenoms := make([]string, len(voteTargets)+1)
rewardDenoms[0] = types.UmeeDenom

i := 1
for _, denom := range voteTargets {
rewardDenoms[i] = denom
i++
}

// sum weight of the claims
var ballotPowerSum int64
for _, winner := range ballotWinners {
Expand All @@ -39,8 +42,8 @@ func (k Keeper) RewardBallotWinners(
}

distributionRatio := sdk.NewDec(votePeriod).QuoInt64(rewardDistributionWindow)

var periodRewards sdk.DecCoins
rewardDenoms := prependUmeeIfUnique(voteTargets)
for _, denom := range rewardDenoms {
rewardPool := k.GetRewardPool(ctx, denom)

Expand Down
25 changes: 25 additions & 0 deletions x/oracle/keeper/reward_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package keeper

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestPrependUmeeIfUnique(t *testing.T) {
require := require.New(t)
tcs := []struct {
in []string
out []string
}{
// Should prepend "uumee" to a slice of denoms, unless it is already present.
{[]string{}, []string{"uumee"}},
{[]string{"a"}, []string{"uumee", "a"}},
{[]string{"x", "a", "heeeyyy"}, []string{"uumee", "x", "a", "heeeyyy"}},
{[]string{"x", "a", "uumee"}, []string{"x", "a", "uumee"}},
}
for i, tc := range tcs {
require.Equal(tc.out, prependUmeeIfUnique(tc.in), i)
}

}

0 comments on commit abcdb11

Please sign in to comment.