Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(oracle)!: count vote omission as abstain for less slashing + more stability #1565

Merged
merged 27 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2b8cd27
refactor(liquidate_test): use named imports
Unique-Divine Aug 25, 2023
6ad181b
refactor(amm_test): use named imports
Unique-Divine Aug 25, 2023
765f5a3
refactor(admin_test, calc_test, grpc_query_test): use named imports
Unique-Divine Aug 25, 2023
c490ffc
refactor(perp/keeper): use named imports
Unique-Divine Aug 25, 2023
02a2e9d
refactor(perp/keeper): use named imports | Complete
Unique-Divine Aug 25, 2023
9cbae1f
changelog
Unique-Divine Aug 25, 2023
a53b360
Merge branch 'master' into realu/refactor-no-unnamed
Unique-Divine Aug 28, 2023
9adc9e4
fix(oracle): miss outside reward band but not on abstain
Unique-Divine Sep 5, 2023
c6a592d
Merge branch 'realu/refactor-no-unnamed' into realu/oracle-miss
Unique-Divine Sep 5, 2023
3ca259a
fix(oracle): #wip checkpoint for omit == abstain
Unique-Divine Sep 5, 2023
b9862f4
checkout x/perp from master
Unique-Divine Sep 5, 2023
d4e9bb8
change log
Unique-Divine Sep 5, 2023
70001e9
test(oracle/types): more unit tests
Unique-Divine Sep 5, 2023
f741d7f
Merge branch 'master' into realu/oracle-miss
Unique-Divine Sep 6, 2023
a0cf656
refactor: use types. instead of New for names args
Unique-Divine Sep 6, 2023
0a9be17
rm error.txt
Unique-Divine Sep 6, 2023
fad86ca
del zombie code
Unique-Divine Sep 6, 2023
f8bba05
variable renaming
k-yang Sep 27, 2023
f749e9a
Merge branch 'master' into realu/oracle-miss
k-yang Oct 10, 2023
1ee6408
rename exchange rate ballot to exchange rate vote
k-yang Oct 10, 2023
bff235a
rename ballot to vote
k-yang Oct 10, 2023
968958d
rename variables
k-yang Oct 10, 2023
d1467ff
rename variables
k-yang Oct 10, 2023
32c34cb
rename variables
k-yang Oct 10, 2023
51b18cc
fix TestWhitelistedPairs
k-yang Oct 10, 2023
1b48b90
rename variables
k-yang Oct 10, 2023
af92024
rename ballots to votes
k-yang Oct 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1459](https://github.com/NibiruChain/nibiru/pull/1459) - fix(spot): wire `x/spot` msgService into app router
* [#1467](https://github.com/NibiruChain/nibiru/pull/1467) - fix(oracle): make `calcTwap` safer
* [#1464](https://github.com/NibiruChain/nibiru/pull/1464) - fix(gov): wire legacy proposal handlers
* [#1565](https://github.com/NibiruChain/nibiru/pull/1565) - fix(oracle)!: Count vote omission as abstain for less slashing + more stability

### State Machine Breaking

Expand Down Expand Up @@ -640,4 +641,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Testing

* [#695](https://github.com/NibiruChain/nibiru/pull/695) Add `OpenPosition` integration tests.
* [#692](https://github.com/NibiruChain/nibiru/pull/692) Add test coverage for Perp MsgServer methods.
* [#692](https://github.com/NibiruChain/nibiru/pull/692) Add test coverage for Perp MsgServer methods.
37 changes: 23 additions & 14 deletions x/oracle/keeper/ballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
// asset pair is associated with its collection of ExchangeRateBallots.
func (k Keeper) groupBallotsByPair(
ctx sdk.Context,
validatorsPerformance types.ValidatorPerformances,
valPerfMap types.ValidatorPerformances,
) (pairBallotsMap map[asset.Pair]types.ExchangeRateBallots) {
pairBallotsMap = map[asset.Pair]types.ExchangeRateBallots{}

for _, value := range k.Votes.Iterate(ctx, collections.Range[sdk.ValAddress]{}).KeyValues() {
voterAddr, aggregateVote := value.Key, value.Value

// organize ballot only for the active validators
if validatorPerformance, exists := validatorsPerformance[aggregateVote.Voter]; exists {
if validatorPerformance, exists := valPerfMap[aggregateVote.Voter]; exists {
for _, exchangeRateTuple := range aggregateVote.ExchangeRateTuples {
power := validatorPerformance.Power
if !exchangeRateTuple.ExchangeRate.IsPositive() {
Expand Down Expand Up @@ -137,11 +137,17 @@
return pairBallotsMap, whitelistedPairs
}

// Tally calculates the median and returns it. Sets the set of voters to be rewarded, i.e. voted within
// a reasonable spread from the weighted median to the store
// Tally calculates the median and returns it. Sets the set of voters to be
// rewarded, i.e. voted within a reasonable spread from the weighted median to
// the store.
//
// ALERT: This function mutates validatorPerformances slice based on the votes made by the validators.
func Tally(ballots types.ExchangeRateBallots, rewardBand sdk.Dec, validatorPerformances types.ValidatorPerformances) sdk.Dec {
// ALERT: This function mutates validatorPerformances slice based on the votes
// made by the validators.
func Tally(
ballots types.ExchangeRateBallots,
rewardBand sdk.Dec,
validatorPerformances types.ValidatorPerformances,
) (sdk.Dec, types.ValidatorPerformances) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bad idea, to have a return only used for tests. The function already mentions that it mutates the validator perfomances, returning it is confusing.

weightedMedian := ballots.WeightedMedianWithAssertion()
standardDeviation := ballots.StandardDeviation(weightedMedian)
rewardSpread := weightedMedian.Mul(rewardBand.QuoInt64(2))
Expand All @@ -155,16 +161,19 @@
voteInsideSpread := ballot.ExchangeRate.GTE(weightedMedian.Sub(rewardSpread)) &&
ballot.ExchangeRate.LTE(weightedMedian.Add(rewardSpread))
isAbstainVote := !ballot.ExchangeRate.IsPositive()

if voteInsideSpread || isAbstainVote {
voterAddr := ballot.Voter.String()

validatorPerformance := validatorPerformances[voterAddr]
isMiss := !(voteInsideSpread || isAbstainVote)
voterAddr := ballot.Voter.String()
validatorPerformance := validatorPerformances[voterAddr]
switch {
case voteInsideSpread:
validatorPerformance.RewardWeight += ballot.Power
validatorPerformance.WinCount++
validatorPerformances[voterAddr] = validatorPerformance
case isMiss:
validatorPerformance.MissCount++
case isAbstainVote:
validatorPerformance.AbstainCount++

Check warning on line 174 in x/oracle/keeper/ballot.go

View check run for this annotation

Codecov / codecov/patch

x/oracle/keeper/ballot.go#L173-L174

Added lines #L173 - L174 were not covered by tests
}
validatorPerformances[voterAddr] = validatorPerformance
}

return weightedMedian
return weightedMedian, validatorPerformances
}
14 changes: 8 additions & 6 deletions x/oracle/keeper/ballot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ func TestGroupBallotsByPair(t *testing.T) {
require.NoError(t, err)
staking.EndBlocker(fixture.Ctx, &fixture.StakingKeeper)

pairBtc := asset.Registry.Pair(denoms.BTC, denoms.NUSD)
pairEth := asset.Registry.Pair(denoms.ETH, denoms.NUSD)
btcBallots := types.ExchangeRateBallots{
types.NewExchangeRateBallot(sdk.NewDec(17), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[0], power),
types.NewExchangeRateBallot(sdk.NewDec(10), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[1], power),
types.NewExchangeRateBallot(sdk.NewDec(6), asset.Registry.Pair(denoms.BTC, denoms.NUSD), ValAddrs[2], power),
{Pair: pairBtc, ExchangeRate: sdk.NewDec(17), Voter: ValAddrs[0], Power: power},
{Pair: pairBtc, ExchangeRate: sdk.NewDec(10), Voter: ValAddrs[1], Power: power},
{Pair: pairBtc, ExchangeRate: sdk.NewDec(6), Voter: ValAddrs[2], Power: power},
}
ethBallots := types.ExchangeRateBallots{
types.NewExchangeRateBallot(sdk.NewDec(1000), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[0], power),
types.NewExchangeRateBallot(sdk.NewDec(1300), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[1], power),
types.NewExchangeRateBallot(sdk.NewDec(2000), asset.Registry.Pair(denoms.ETH, denoms.NUSD), ValAddrs[2], power),
{Pair: pairEth, ExchangeRate: sdk.NewDec(1_000), Voter: ValAddrs[0], Power: power},
{Pair: pairEth, ExchangeRate: sdk.NewDec(1_300), Voter: ValAddrs[1], Power: power},
{Pair: pairEth, ExchangeRate: sdk.NewDec(2_000), Voter: ValAddrs[2], Power: power},
}

for i, ballot := range btcBallots {
Expand Down
2 changes: 1 addition & 1 deletion x/oracle/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestFeederDelegation(t *testing.T) {
exchangeRates := types.ExchangeRateTuples{
{
Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD),
ExchangeRate: randomExchangeRate,
ExchangeRate: TEST_ERATE,
},
}

Expand Down
2 changes: 1 addition & 1 deletion x/oracle/keeper/reward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestKeeperRewardsDistributionMultiVotePeriods(t *testing.T) {
MakeAggregatePrevoteAndVote(t, fixture, msgServer, fixture.Ctx.BlockHeight(), types.ExchangeRateTuples{
{
Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD),
ExchangeRate: randomExchangeRate,
ExchangeRate: TEST_ERATE,
},
}, valIndex)
}
Expand Down
104 changes: 65 additions & 39 deletions x/oracle/keeper/slash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,22 @@ func TestInvalidVotesSlashing(t *testing.T) {

// Account 1, govstable
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{
{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate},
{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE},
}, 0)

// Account 2, govstable, miss vote
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{
{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate.Add(sdk.NewDec(100000000000000))},
{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE.Add(sdk.NewDec(100000000000000))},
}, 1)

// Account 3, govstable
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{
{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate},
{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE},
}, 2)

// Account 4, govstable
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{
{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate},
{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE},
}, 3)

input.OracleKeeper.UpdateExchangeRates(input.Ctx)
Expand All @@ -131,27 +131,27 @@ func TestInvalidVotesSlashing(t *testing.T) {
}

validator := input.StakingKeeper.Validator(input.Ctx, ValAddrs[1])
require.Equal(t, stakingAmt, validator.GetBondedTokens())
require.Equal(t, TEST_STAKING_AMT, validator.GetBondedTokens())

// one more miss vote will inccur ValAddrs[1] slashing
// Account 1, govstable
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{
{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate},
{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE},
}, 0)

// Account 2, govstable, miss vote
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{
{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate.Add(sdk.NewDec(100000000000000))},
{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE.Add(sdk.NewDec(100000000000000))},
}, 1)

// Account 3, govstable
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{
{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate},
{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE},
}, 2)

// Account 4, govstable
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{
{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate},
{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE},
}, 3)

input.Ctx = input.Ctx.WithBlockHeight(votePeriodsPerWindow - 1)
Expand All @@ -160,46 +160,72 @@ func TestInvalidVotesSlashing(t *testing.T) {
// input.OracleKeeper.UpdateExchangeRates(input.Ctx)

validator = input.StakingKeeper.Validator(input.Ctx, ValAddrs[1])
require.Equal(t, sdk.OneDec().Sub(slashFraction).MulInt(stakingAmt).TruncateInt(), validator.GetBondedTokens())
require.Equal(t, sdk.OneDec().Sub(slashFraction).MulInt(TEST_STAKING_AMT).TruncateInt(), validator.GetBondedTokens())
}

// TestWhitelistSlashing: Creates a scenario where one valoper (valIdx 0) does
// not vote throughout an entire vote window, while valopers 1 and 2 do.
func TestWhitelistSlashing(t *testing.T) {
input, h := Setup(t)
input, msgServer := Setup(t)

votePeriodsPerWindow := sdk.NewDec(int64(input.OracleKeeper.SlashWindow(input.Ctx))).QuoInt64(int64(input.OracleKeeper.VotePeriod(input.Ctx))).TruncateInt64()
slashFraction := input.OracleKeeper.SlashFraction(input.Ctx)
// slashFraction := input.OracleKeeper.SlashFraction(input.Ctx)
minValidPerWindow := input.OracleKeeper.MinValidPerWindow(input.Ctx)

for i := uint64(0); i < uint64(sdk.OneDec().Sub(minValidPerWindow).MulInt64(votePeriodsPerWindow).TruncateInt64()); i++ {
input.Ctx = input.Ctx.WithBlockHeight(input.Ctx.BlockHeight() + 1)
pair := asset.Registry.Pair(denoms.NIBI, denoms.NUSD)
priceVoteFromVal := func(valIdx int, block int64, erate sdk.Dec) {
MakeAggregatePrevoteAndVote(t, input, msgServer, block,
types.ExchangeRateTuples{{Pair: pair, ExchangeRate: erate}},
valIdx)
}
input.OracleKeeper.WhitelistedPairs.Insert(input.Ctx, pair)
perfs := input.OracleKeeper.UpdateExchangeRates(input.Ctx)
require.EqualValues(t, 0, perfs.GetTotalRewardWeight())

// Account 2, govstable
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 1)
// Account 3, govstable
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 2)
block := input.Ctx.BlockHeight() + 50
priceVoteFromVal(0, block, TEST_ERATE)
priceVoteFromVal(1, block, TEST_ERATE)
input.Ctx = input.Ctx.WithBlockHeight(block + 100)

input.OracleKeeper.UpdateExchangeRates(input.Ctx)
// input.OracleKeeper.SlashAndResetMissCounters(input.Ctx)
// input.OracleKeeper.UpdateExchangeRates(input.Ctx)
require.Equal(t, i+1, input.OracleKeeper.MissCounters.GetOr(input.Ctx, ValAddrs[0], 0))
}
allowedMissPct := sdk.OneDec().Sub(minValidPerWindow)
allowedMissPeriods := allowedMissPct.MulInt64(votePeriodsPerWindow).
TruncateInt64()

validator := input.StakingKeeper.Validator(input.Ctx, ValAddrs[0])
require.Equal(t, stakingAmt, validator.GetBondedTokens())
// TODO

// one more miss vote will inccur Account 1 slashing
t.Logf("For %v blocks, valoper0 does not vote, while 1 and 2 do.", allowedMissPeriods)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're conflating blocks with vote periods here. One vote period is many blocks (by default 30 on testnet), so if the goal is to vote once every vote period, we have to skip the block number by 30 in each iteration.

However, this test doesn't really test validator slashing, only that the MissCounter doesn't increment. The slashing happens in the abci EndBlocker which is in another test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the name is wrong because it used to be a test about slashing that had abstain votes. Since that doesn't cause slashing, the test is no longer about that either. Will need to repurpose it

for idxMissPeriod := uint64(0); idxMissPeriod < uint64(allowedMissPeriods); idxMissPeriod++ {
block := input.Ctx.BlockHeight() + 1
input.Ctx = input.Ctx.WithBlockHeight(block)

// Account 2, govstable
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 1)
// Account 3, govstable
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 2)
valIdx := 0
priceVoteFromVal(valIdx+1, block, TEST_ERATE)
priceVoteFromVal(valIdx+2, block, TEST_ERATE)

input.Ctx = input.Ctx.WithBlockHeight(votePeriodsPerWindow - 1)
input.OracleKeeper.UpdateExchangeRates(input.Ctx)
input.OracleKeeper.SlashAndResetMissCounters(input.Ctx)
// Valoper 0 votes outside band (miss)

perfs := input.OracleKeeper.UpdateExchangeRates(input.Ctx)
missCount := input.OracleKeeper.MissCounters.GetOr(input.Ctx, ValAddrs[0], 0)
require.EqualValues(t, 0, missCount, perfs.String())
}

t.Log("valoper0 should not be slashed")
validator := input.StakingKeeper.Validator(input.Ctx, ValAddrs[0])
require.Equal(t, TEST_STAKING_AMT, validator.GetBondedTokens())

// // one more miss vote will inccur Account 1 slashing
Unique-Divine marked this conversation as resolved.
Show resolved Hide resolved
//
// // Account 2, govstable
// MakeAggregatePrevoteAndVote(t, input, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 1)
// // Account 3, govstable
// MakeAggregatePrevoteAndVote(t, input, msgServer, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 2)
//
// input.Ctx = input.Ctx.WithBlockHeight(votePeriodsPerWindow - 1)
// input.OracleKeeper.UpdateExchangeRates(input.Ctx)
validator = input.StakingKeeper.Validator(input.Ctx, ValAddrs[0])
require.Equal(t, sdk.OneDec().Sub(slashFraction).MulInt(stakingAmt).TruncateInt(), validator.GetBondedTokens())
// input.OracleKeeper.SlashAndResetMissCounters(input.Ctx)
// // input.OracleKeeper.UpdateExchangeRates(input.Ctx)
// validator = input.StakingKeeper.Validator(input.Ctx, ValAddrs[0])
// require.Equal(t, sdk.OneDec().Sub(slashFraction).MulInt(stakingAmt).TruncateInt(), validator.GetBondedTokens())
}

func TestNotPassedBallotSlashing(t *testing.T) {
Expand All @@ -218,7 +244,7 @@ func TestNotPassedBallotSlashing(t *testing.T) {
input.Ctx = input.Ctx.WithBlockHeight(input.Ctx.BlockHeight() + 1)

// Account 1, govstable
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 0)
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}}, 0)

input.OracleKeeper.UpdateExchangeRates(input.Ctx)
input.OracleKeeper.SlashAndResetMissCounters(input.Ctx)
Expand Down Expand Up @@ -248,13 +274,13 @@ func TestAbstainSlashing(t *testing.T) {
input.Ctx = input.Ctx.WithBlockHeight(input.Ctx.BlockHeight() + 1)

// Account 1, govstable
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 0)
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}}, 0)

// Account 2, govstable, abstain vote
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: sdk.ZeroDec()}}, 1)

// Account 3, govstable
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: randomExchangeRate}}, 2)
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD), ExchangeRate: TEST_ERATE}}, 2)

input.OracleKeeper.UpdateExchangeRates(input.Ctx)
input.OracleKeeper.SlashAndResetMissCounters(input.Ctx)
Expand All @@ -263,5 +289,5 @@ func TestAbstainSlashing(t *testing.T) {
}

validator := input.StakingKeeper.Validator(input.Ctx, ValAddrs[1])
require.Equal(t, stakingAmt, validator.GetBondedTokens())
require.Equal(t, TEST_STAKING_AMT, validator.GetBondedTokens())
}
23 changes: 15 additions & 8 deletions x/oracle/keeper/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ func AllocateRewards(t *testing.T, input TestFixture, rewards sdk.Coins, votePer
}

var (
stakingAmt = sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction)
randomExchangeRate = sdk.NewDec(1700)
TEST_STAKING_AMT = sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction)
Unique-Divine marked this conversation as resolved.
Show resolved Hide resolved
TEST_ERATE = sdk.NewDec(1700)
)

func Setup(t *testing.T) (TestFixture, types.MsgServer) {
Expand All @@ -304,22 +304,29 @@ func Setup(t *testing.T) (TestFixture, types.MsgServer) {
sh := stakingkeeper.NewMsgServerImpl(&fixture.StakingKeeper)

// Validator created
_, err := sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[0], ValPubKeys[0], stakingAmt))
_, err := sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[0], ValPubKeys[0], TEST_STAKING_AMT))
require.NoError(t, err)
_, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[1], ValPubKeys[1], stakingAmt))
_, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[1], ValPubKeys[1], TEST_STAKING_AMT))
require.NoError(t, err)
_, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[2], ValPubKeys[2], stakingAmt))
_, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[2], ValPubKeys[2], TEST_STAKING_AMT))
require.NoError(t, err)
_, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[3], ValPubKeys[3], stakingAmt))
_, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[3], ValPubKeys[3], TEST_STAKING_AMT))
require.NoError(t, err)
_, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[4], ValPubKeys[4], stakingAmt))
_, err = sh.CreateValidator(fixture.Ctx, NewTestMsgCreateValidator(ValAddrs[4], ValPubKeys[4], TEST_STAKING_AMT))
require.NoError(t, err)
staking.EndBlocker(fixture.Ctx, &fixture.StakingKeeper)

return fixture, h
}

func MakeAggregatePrevoteAndVote(t *testing.T, input TestFixture, msgServer types.MsgServer, height int64, rates types.ExchangeRateTuples, valIdx int) {
func MakeAggregatePrevoteAndVote(
t *testing.T,
input TestFixture,
msgServer types.MsgServer,
height int64,
rates types.ExchangeRateTuples,
valIdx int,
) {
salt := "1"
ratesStr, err := rates.ToString()
require.NoError(t, err)
Expand Down
Loading
Loading