Skip to content

Commit

Permalink
chore(oracle): curate default oracle whitelist (#1659)
Browse files Browse the repository at this point in the history
* chore(oracle): curate default oracle whitelist

* update changelog
  • Loading branch information
k-yang committed Nov 2, 2023
1 parent cf6acee commit f1e6859
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 121 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1630](https://github.com/NibiruChain/nibiru/pull/1630) - refactor(wasm): clean up wasmbinding/ folder structure
* [#1631](https://github.com/NibiruChain/nibiru/pull/1631) - fix(.goreleaser.yml): Load version for wasmvm dynamically.
* [#1638](https://github.com/NibiruChain/nibiru/pull/1638) - test(tokenfactory): integration test core logic with a real smart contract using `nibiru-std`
* [#1659](https://github.com/NibiruChain/nibiru/pull/1659) - refactor(oracle): curate oracle default whitelist

### Dependencies
- Bump `github.com/prometheus/client_golang` from 1.16.0 to 1.17.0 ([#1605](https://github.com/NibiruChain/nibiru/pull/1605))
Expand Down
10 changes: 5 additions & 5 deletions x/oracle/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestOracleTallyTiming(t *testing.T) {
// all the Addrs vote for the block ... not last period block yet, so tally fails
for i := range keeper.Addrs[:4] {
keeper.MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{
{Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD), ExchangeRate: sdk.OneDec()},
{Pair: asset.Registry.Pair(denoms.BTC, denoms.USD), ExchangeRate: sdk.OneDec()},
}, i)
}

Expand All @@ -31,22 +31,22 @@ func TestOracleTallyTiming(t *testing.T) {
require.Equal(t, 1, int(input.Ctx.BlockHeight()))

EndBlocker(input.Ctx, input.OracleKeeper)
_, err = input.OracleKeeper.ExchangeRates.Get(input.Ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD))
_, err = input.OracleKeeper.ExchangeRates.Get(input.Ctx, asset.Registry.Pair(denoms.BTC, denoms.USD))
require.Error(t, err)

input.Ctx = input.Ctx.WithBlockHeight(int64(params.VotePeriod - 1))

EndBlocker(input.Ctx, input.OracleKeeper)
_, err = input.OracleKeeper.ExchangeRates.Get(input.Ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD))
_, err = input.OracleKeeper.ExchangeRates.Get(input.Ctx, asset.Registry.Pair(denoms.BTC, denoms.USD))
require.NoError(t, err)
}

// Set prices for 2 pairs, one that is updated and the other which is updated only once.
// Ensure that the updated pair is not deleted and the other pair is deleted after a certain time.
func TestOraclePriceExpiration(t *testing.T) {
input, h := keeper.Setup(t)
pair1 := asset.Registry.Pair(denoms.BTC, denoms.NUSD)
pair2 := asset.Registry.Pair(denoms.ETH, denoms.NUSD)
pair1 := asset.Registry.Pair(denoms.BTC, denoms.USD)
pair2 := asset.Registry.Pair(denoms.ETH, denoms.USD)

// Set prices for both pairs
for i := range keeper.Addrs[:4] {
Expand Down
18 changes: 9 additions & 9 deletions x/oracle/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestFeederDelegation(t *testing.T) {

exchangeRates := types.ExchangeRateTuples{
{
Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD),
Pair: asset.Registry.Pair(denoms.BTC, denoms.USD),
ExchangeRate: testExchangeRate,
},
}
Expand Down Expand Up @@ -83,43 +83,43 @@ func TestAggregatePrevoteVote(t *testing.T) {
salt := "1"
exchangeRates := types.ExchangeRateTuples{
{
Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD),
Pair: asset.Registry.Pair(denoms.ATOM, denoms.USD),
ExchangeRate: sdk.MustNewDecFromStr("1000.23"),
},
{
Pair: asset.Registry.Pair(denoms.ETH, denoms.NUSD),
Pair: asset.Registry.Pair(denoms.ETH, denoms.USD),
ExchangeRate: sdk.MustNewDecFromStr("0.29"),
},

{
Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD),
Pair: asset.Registry.Pair(denoms.BTC, denoms.USD),
ExchangeRate: sdk.MustNewDecFromStr("0.27"),
},
}

otherExchangeRate := types.ExchangeRateTuples{
{
Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD),
Pair: asset.Registry.Pair(denoms.ATOM, denoms.USD),
ExchangeRate: sdk.MustNewDecFromStr("1000.23"),
},
{
Pair: asset.Registry.Pair(denoms.ETH, denoms.NUSD),
Pair: asset.Registry.Pair(denoms.ETH, denoms.USD),
ExchangeRate: sdk.MustNewDecFromStr("0.29"),
},

{
Pair: asset.Registry.Pair(denoms.ETH, denoms.NUSD),
Pair: asset.Registry.Pair(denoms.ETH, denoms.USD),
ExchangeRate: sdk.MustNewDecFromStr("0.27"),
},
}

unintendedExchangeRateStr := types.ExchangeRateTuples{
{
Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD),
Pair: asset.Registry.Pair(denoms.ATOM, denoms.USD),
ExchangeRate: sdk.MustNewDecFromStr("1000.23"),
},
{
Pair: asset.Registry.Pair(denoms.ETH, denoms.NUSD),
Pair: asset.Registry.Pair(denoms.ETH, denoms.USD),
ExchangeRate: sdk.MustNewDecFromStr("0.29"),
},
{
Expand Down
4 changes: 2 additions & 2 deletions x/oracle/keeper/reward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func TestKeeperRewardsDistributionMultiVotePeriods(t *testing.T) {
// this simulates allocating rewards for the pair nibi:nusd
// this simulates allocating rewards for the pair atom:usd
// over 5 voting periods. It simulates rewards are correctly
// distributed over 5 voting periods to 5 validators.
// then we simulate that after the 5 voting periods are
Expand All @@ -39,7 +39,7 @@ func TestKeeperRewardsDistributionMultiVotePeriods(t *testing.T) {
// then changes the height to current height + vote period for the vote
MakeAggregatePrevoteAndVote(t, fixture, msgServer, fixture.Ctx.BlockHeight(), types.ExchangeRateTuples{
{
Pair: asset.Registry.Pair(denoms.NIBI, denoms.NUSD),
Pair: asset.Registry.Pair(denoms.ATOM, denoms.USD),
ExchangeRate: testExchangeRate,
},
}, valIndex)
Expand Down
44 changes: 22 additions & 22 deletions x/oracle/keeper/slash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ func TestInvalidVotesSlashing(t *testing.T) {
input, h := Setup(t)
params, err := input.OracleKeeper.Params.Get(input.Ctx)
require.NoError(t, err)
params.Whitelist = []asset.Pair{asset.Registry.Pair(denoms.NIBI, denoms.NUSD)}
params.Whitelist = []asset.Pair{asset.Registry.Pair(denoms.ATOM, denoms.USD)}
input.OracleKeeper.Params.Set(input.Ctx, params)
input.OracleKeeper.WhitelistedPairs.Insert(input.Ctx, asset.Registry.Pair(denoms.NIBI, denoms.NUSD))
input.OracleKeeper.WhitelistedPairs.Insert(input.Ctx, asset.Registry.Pair(denoms.ATOM, denoms.USD))

votePeriodsPerWindow := sdk.NewDec(int64(input.OracleKeeper.SlashWindow(input.Ctx))).QuoInt64(int64(input.OracleKeeper.VotePeriod(input.Ctx))).TruncateInt64()
slashFraction := input.OracleKeeper.SlashFraction(input.Ctx)
Expand All @@ -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: testExchangeRate},
{Pair: asset.Registry.Pair(denoms.ATOM, denoms.USD), ExchangeRate: testExchangeRate},
}, 0)

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

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

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

input.OracleKeeper.UpdateExchangeRates(input.Ctx)
Expand All @@ -136,22 +136,22 @@ func TestInvalidVotesSlashing(t *testing.T) {
// 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: testExchangeRate},
{Pair: asset.Registry.Pair(denoms.ATOM, denoms.USD), ExchangeRate: testExchangeRate},
}, 0)

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

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

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

input.Ctx = input.Ctx.WithBlockHeight(votePeriodsPerWindow - 1)
Expand All @@ -171,7 +171,7 @@ func TestWhitelistSlashing(t *testing.T) {
votePeriodsPerSlashWindow := sdk.NewDec(int64(input.OracleKeeper.SlashWindow(input.Ctx))).QuoInt64(int64(input.OracleKeeper.VotePeriod(input.Ctx))).TruncateInt64()
minValidVotePeriodsPerWindow := input.OracleKeeper.MinValidPerWindow(input.Ctx)

pair := asset.Registry.Pair(denoms.NIBI, denoms.NUSD)
pair := asset.Registry.Pair(denoms.ATOM, denoms.USD)
priceVoteFromVal := func(valIdx int, block int64, erate sdk.Dec) {
MakeAggregatePrevoteAndVote(t, input, msgServer, block,
types.ExchangeRateTuples{{Pair: pair, ExchangeRate: erate}},
Expand Down Expand Up @@ -207,19 +207,19 @@ func TestNotPassedBallotSlashing(t *testing.T) {
input, h := Setup(t)
params, err := input.OracleKeeper.Params.Get(input.Ctx)
require.NoError(t, err)
params.Whitelist = []asset.Pair{asset.Registry.Pair(denoms.NIBI, denoms.NUSD)}
params.Whitelist = []asset.Pair{asset.Registry.Pair(denoms.ATOM, denoms.USD)}
input.OracleKeeper.Params.Set(input.Ctx, params)

// clear tobin tax to reset vote targets
for _, p := range input.OracleKeeper.WhitelistedPairs.Iterate(input.Ctx, collections.Range[asset.Pair]{}).Keys() {
input.OracleKeeper.WhitelistedPairs.Delete(input.Ctx, p)
}
input.OracleKeeper.WhitelistedPairs.Insert(input.Ctx, asset.Registry.Pair(denoms.NIBI, denoms.NUSD))
input.OracleKeeper.WhitelistedPairs.Insert(input.Ctx, asset.Registry.Pair(denoms.ATOM, denoms.USD))

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: testExchangeRate}}, 0)
MakeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: asset.Registry.Pair(denoms.ATOM, denoms.USD), ExchangeRate: testExchangeRate}}, 0)

input.OracleKeeper.UpdateExchangeRates(input.Ctx)
input.OracleKeeper.SlashAndResetMissCounters(input.Ctx)
Expand All @@ -235,27 +235,27 @@ func TestAbstainSlashing(t *testing.T) {
// reset whitelisted pairs
params, err := input.OracleKeeper.Params.Get(input.Ctx)
require.NoError(t, err)
params.Whitelist = []asset.Pair{asset.Registry.Pair(denoms.NIBI, denoms.NUSD)}
params.Whitelist = []asset.Pair{asset.Registry.Pair(denoms.ATOM, denoms.USD)}
input.OracleKeeper.Params.Set(input.Ctx, params)
for _, p := range input.OracleKeeper.WhitelistedPairs.Iterate(input.Ctx, collections.Range[asset.Pair]{}).Keys() {
input.OracleKeeper.WhitelistedPairs.Delete(input.Ctx, p)
}
input.OracleKeeper.WhitelistedPairs.Insert(input.Ctx, asset.Registry.Pair(denoms.NIBI, denoms.NUSD))
input.OracleKeeper.WhitelistedPairs.Insert(input.Ctx, asset.Registry.Pair(denoms.ATOM, denoms.USD))

votePeriodsPerWindow := sdk.NewDec(int64(input.OracleKeeper.SlashWindow(input.Ctx))).QuoInt64(int64(input.OracleKeeper.VotePeriod(input.Ctx))).TruncateInt64()
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)

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

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

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

input.OracleKeeper.UpdateExchangeRates(input.Ctx)
input.OracleKeeper.SlashAndResetMissCounters(input.Ctx)
Expand Down
Loading

0 comments on commit f1e6859

Please sign in to comment.