Skip to content

Commit

Permalink
fix to apply reward spread by multiplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Yun committed Nov 20, 2019
1 parent 64cf204 commit 0ec5a21
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
44 changes: 43 additions & 1 deletion x/oracle/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func TestOracleTally(t *testing.T) {
rewardees := []sdk.AccAddress{}
weightedMedian := ballot.WeightedMedian()
standardDeviation := ballot.StandardDeviation()
maxSpread := input.OracleKeeper.RewardBand(input.Ctx).QuoInt64(2)
maxSpread := weightedMedian.Mul(input.OracleKeeper.RewardBand(input.Ctx).QuoInt64(2))

if standardDeviation.GT(maxSpread) {
maxSpread = standardDeviation
Expand Down Expand Up @@ -301,6 +301,48 @@ func TestOracleRewardDistribution(t *testing.T) {
require.Equal(t, expectedRewardAmt, rewards.AmountOf(core.MicroLunaDenom).TruncateInt())
}

func TestOracleRewardBand(t *testing.T) {
input, h := setup(t)
params := input.OracleKeeper.GetParams(input.Ctx)
params.Whitelist = types.DenomList{core.MicroKRWDenom}
input.OracleKeeper.SetParams(input.Ctx, params)

rewardSpread := randomExchangeRate.Mul(input.OracleKeeper.RewardBand(input.Ctx).QuoInt64(2))

// no one will miss the vote
// Account 1, KRW
makePrevoteAndVote(t, input, h, 0, core.MicroKRWDenom, randomExchangeRate.Sub(rewardSpread), 0)

// Account 2, KRW
makePrevoteAndVote(t, input, h, 0, core.MicroKRWDenom, randomExchangeRate, 1)

// Account 3, KRW
makePrevoteAndVote(t, input, h, 0, core.MicroKRWDenom, randomExchangeRate.Add(rewardSpread), 2)

EndBlocker(input.Ctx, input.OracleKeeper)

require.Equal(t, int64(0), input.OracleKeeper.GetMissCounter(input.Ctx, keeper.ValAddrs[0]))
require.Equal(t, int64(0), input.OracleKeeper.GetMissCounter(input.Ctx, keeper.ValAddrs[1]))
require.Equal(t, int64(0), input.OracleKeeper.GetMissCounter(input.Ctx, keeper.ValAddrs[2]))

// Account 1 will miss the vote due to raward band condition
// Account 1, KRW
makePrevoteAndVote(t, input, h, 0, core.MicroKRWDenom, randomExchangeRate.Sub(rewardSpread.Add(sdk.OneDec())), 0)

// Account 2, KRW
makePrevoteAndVote(t, input, h, 0, core.MicroKRWDenom, randomExchangeRate, 1)

// Account 3, KRW
makePrevoteAndVote(t, input, h, 0, core.MicroKRWDenom, randomExchangeRate.Add(rewardSpread), 2)

EndBlocker(input.Ctx, input.OracleKeeper)

require.Equal(t, int64(1), input.OracleKeeper.GetMissCounter(input.Ctx, keeper.ValAddrs[0]))
require.Equal(t, int64(0), input.OracleKeeper.GetMissCounter(input.Ctx, keeper.ValAddrs[1]))
require.Equal(t, int64(0), input.OracleKeeper.GetMissCounter(input.Ctx, keeper.ValAddrs[2]))

}

func TestOracleMultiRewardDistribution(t *testing.T) {
input, h := setup(t)

Expand Down
5 changes: 4 additions & 1 deletion x/oracle/tally.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package oracle

import (
"fmt"
"sort"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -16,12 +17,14 @@ func tally(ctx sdk.Context, pb types.ExchangeRateBallot, rewardBand sdk.Dec) (we

weightedMedian = pb.WeightedMedian()
standardDeviation := pb.StandardDeviation()
rewardSpread := rewardBand.QuoInt64(2)
rewardSpread := weightedMedian.Mul(rewardBand.QuoInt64(2))

if standardDeviation.GT(rewardSpread) {
rewardSpread = standardDeviation
}

fmt.Println(standardDeviation)

for _, vote := range pb {
// Filter ballot winners & abstain voters
if (vote.ExchangeRate.GTE(weightedMedian.Sub(rewardSpread)) &&
Expand Down

0 comments on commit 0ec5a21

Please sign in to comment.