From f62261e4b6b41371acbdcc1a602fa45c90a5b704 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 5 May 2022 13:54:56 -0700 Subject: [PATCH] refactor: x/oracle: make GetExchangeRateBase more efficient (#866) (#872) (cherry picked from commit d91976514c17d22198a7c1680e6d939ae7e427da) Co-authored-by: Adam Wozniak <29418299+adamewozniak@users.noreply.github.com> --- CHANGELOG.md | 4 +++ x/oracle/keeper/ballot_test.go | 2 +- x/oracle/keeper/grpc_query_test.go | 12 ++++---- x/oracle/keeper/keeper.go | 13 +++----- x/oracle/keeper/keeper_test.go | 48 ++++++++++++++++++++++-------- 5 files changed, 51 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d12530835..166e3b28cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +### Improvements + +- [866](https://github.com/umee-network/umee/pull/866) Make the x/oracle keeper's GetExchangeRateBase method more efficient. + ## [v2.0.1](https://github.com/umee-network/umee/releases/tag/v2.0.1) - 2022-04-25 ### Features diff --git a/x/oracle/keeper/ballot_test.go b/x/oracle/keeper/ballot_test.go index aa0183c2f8..5a1ed112f3 100644 --- a/x/oracle/keeper/ballot_test.go +++ b/x/oracle/keeper/ballot_test.go @@ -7,7 +7,7 @@ import ( ) func (s *IntegrationTestSuite) TestBallot_OrganizeBallotByDenom() { - s.app.OracleKeeper.SetExchangeRate(s.ctx, exchangeRate, sdk.OneDec()) + s.app.OracleKeeper.SetExchangeRate(s.ctx, displayDenom, sdk.OneDec()) claimMap := make(map[string]types.Claim) // Empty Map diff --git a/x/oracle/keeper/grpc_query_test.go b/x/oracle/keeper/grpc_query_test.go index 39f242f4b5..36fde1eb33 100644 --- a/x/oracle/keeper/grpc_query_test.go +++ b/x/oracle/keeper/grpc_query_test.go @@ -12,26 +12,26 @@ import ( ) func (s *IntegrationTestSuite) TestQuerier_ActiveExchangeRates() { - s.app.OracleKeeper.SetExchangeRate(s.ctx, exchangeRate, sdk.OneDec()) + s.app.OracleKeeper.SetExchangeRate(s.ctx, displayDenom, sdk.OneDec()) res, err := s.queryClient.ActiveExchangeRates(s.ctx.Context(), &types.QueryActiveExchangeRatesRequest{}) s.Require().NoError(err) - s.Require().Equal([]string{exchangeRate}, res.ActiveRates) + s.Require().Equal([]string{displayDenom}, res.ActiveRates) } func (s *IntegrationTestSuite) TestQuerier_ExchangeRates() { - s.app.OracleKeeper.SetExchangeRate(s.ctx, exchangeRate, sdk.OneDec()) + s.app.OracleKeeper.SetExchangeRate(s.ctx, displayDenom, sdk.OneDec()) res, err := s.queryClient.ExchangeRates(s.ctx.Context(), &types.QueryExchangeRatesRequest{}) s.Require().NoError(err) s.Require().Equal(sdk.DecCoins{ - sdk.NewDecCoinFromDec(exchangeRate, sdk.OneDec()), + sdk.NewDecCoinFromDec(displayDenom, sdk.OneDec()), }, res.ExchangeRates) res, err = s.queryClient.ExchangeRates(s.ctx.Context(), &types.QueryExchangeRatesRequest{ - Denom: exchangeRate, + Denom: displayDenom, }) s.Require().NoError(err) s.Require().Equal(sdk.DecCoins{ - sdk.NewDecCoinFromDec(exchangeRate, sdk.OneDec()), + sdk.NewDecCoinFromDec(displayDenom, sdk.OneDec()), }, res.ExchangeRates) } diff --git a/x/oracle/keeper/keeper.go b/x/oracle/keeper/keeper.go index c7436616b6..3b3e658d9a 100644 --- a/x/oracle/keeper/keeper.go +++ b/x/oracle/keeper/keeper.go @@ -89,12 +89,13 @@ func (k Keeper) GetExchangeRate(ctx sdk.Context, symbol string) (sdk.Dec, error) // in the base denom (e.g. ATOM -> uatom) func (k Keeper) GetExchangeRateBase(ctx sdk.Context, denom string) (sdk.Dec, error) { var symbol string - + var exponent uint64 // Translate the base denom -> symbol params := k.GetParams(ctx) for _, listDenom := range params.AcceptList { if listDenom.BaseDenom == denom { symbol = listDenom.SymbolDenom + exponent = uint64(listDenom.Exponent) break } } @@ -107,14 +108,8 @@ func (k Keeper) GetExchangeRateBase(ctx sdk.Context, denom string) (sdk.Dec, err return sdk.ZeroDec(), err } - for _, acceptedDenom := range params.AcceptList { - if denom == acceptedDenom.BaseDenom { - powerReduction := ten.Power(uint64(acceptedDenom.Exponent)) - return exchangeRate.Quo(powerReduction), nil - } - } - - return sdk.ZeroDec(), sdkerrors.Wrap(types.ErrUnknownDenom, denom) + powerReduction := ten.Power(exponent) + return exchangeRate.Quo(powerReduction), nil } // SetExchangeRate sets the consensus exchange rate of USD denominated in the diff --git a/x/oracle/keeper/keeper_test.go b/x/oracle/keeper/keeper_test.go index d9addf2537..2494e87c4c 100644 --- a/x/oracle/keeper/keeper_test.go +++ b/x/oracle/keeper/keeper_test.go @@ -24,7 +24,8 @@ import ( ) const ( - exchangeRate string = umeeapp.DisplayDenom + displayDenom string = umeeapp.DisplayDenom + bondDenom string = umeeapp.BondDenom ) type IntegrationTestSuite struct { @@ -177,7 +178,7 @@ func (s *IntegrationTestSuite) TestAggregateExchangeRateVote() { var tuples types.ExchangeRateTuples tuples = append(tuples, types.ExchangeRateTuple{ - Denom: exchangeRate, + Denom: displayDenom, ExchangeRate: sdk.ZeroDec(), }) @@ -205,8 +206,8 @@ func (s *IntegrationTestSuite) TestAggregateExchangeRateVoteError() { func (s *IntegrationTestSuite) TestSetExchangeRateWithEvent() { app, ctx := s.app, s.ctx - app.OracleKeeper.SetExchangeRateWithEvent(ctx, exchangeRate, sdk.OneDec()) - rate, err := app.OracleKeeper.GetExchangeRate(ctx, exchangeRate) + app.OracleKeeper.SetExchangeRateWithEvent(ctx, displayDenom, sdk.OneDec()) + rate, err := app.OracleKeeper.GetExchangeRate(ctx, displayDenom) s.Require().NoError(err) s.Require().Equal(rate, sdk.OneDec()) } @@ -221,30 +222,53 @@ func (s *IntegrationTestSuite) TestGetExchangeRate_InvalidDenom() { func (s *IntegrationTestSuite) TestGetExchangeRate_NotSet() { app, ctx := s.app, s.ctx - _, err := app.OracleKeeper.GetExchangeRate(ctx, exchangeRate) + _, err := app.OracleKeeper.GetExchangeRate(ctx, displayDenom) s.Require().Error(err) } func (s *IntegrationTestSuite) TestGetExchangeRate_Valid() { app, ctx := s.app, s.ctx - app.OracleKeeper.SetExchangeRate(ctx, exchangeRate, sdk.OneDec()) - rate, err := app.OracleKeeper.GetExchangeRate(ctx, exchangeRate) + app.OracleKeeper.SetExchangeRate(ctx, displayDenom, sdk.OneDec()) + rate, err := app.OracleKeeper.GetExchangeRate(ctx, displayDenom) s.Require().NoError(err) s.Require().Equal(rate, sdk.OneDec()) - app.OracleKeeper.SetExchangeRate(ctx, strings.ToLower(exchangeRate), sdk.OneDec()) - rate, err = app.OracleKeeper.GetExchangeRate(ctx, exchangeRate) + app.OracleKeeper.SetExchangeRate(ctx, strings.ToLower(displayDenom), sdk.OneDec()) + rate, err = app.OracleKeeper.GetExchangeRate(ctx, displayDenom) s.Require().NoError(err) s.Require().Equal(rate, sdk.OneDec()) } +func (s *IntegrationTestSuite) TestGetExchangeRateBase() { + oracleParams := s.app.OracleKeeper.GetParams(s.ctx) + + var exponent uint64 + for _, denom := range oracleParams.AcceptList { + if denom.BaseDenom == bondDenom { + exponent = uint64(denom.Exponent) + } + } + + power := sdk.MustNewDecFromStr("10").Power(exponent) + + s.app.OracleKeeper.SetExchangeRate(s.ctx, displayDenom, sdk.OneDec()) + rate, err := s.app.OracleKeeper.GetExchangeRateBase(s.ctx, bondDenom) + s.Require().NoError(err) + s.Require().Equal(rate.Mul(power), sdk.OneDec()) + + s.app.OracleKeeper.SetExchangeRate(s.ctx, strings.ToLower(displayDenom), sdk.OneDec()) + rate, err = s.app.OracleKeeper.GetExchangeRateBase(s.ctx, bondDenom) + s.Require().NoError(err) + s.Require().Equal(rate.Mul(power), sdk.OneDec()) +} + func (s *IntegrationTestSuite) TestDeleteExchangeRate() { app, ctx := s.app, s.ctx - app.OracleKeeper.SetExchangeRate(ctx, exchangeRate, sdk.OneDec()) - app.OracleKeeper.DeleteExchangeRate(ctx, exchangeRate) - _, err := app.OracleKeeper.GetExchangeRate(ctx, exchangeRate) + app.OracleKeeper.SetExchangeRate(ctx, displayDenom, sdk.OneDec()) + app.OracleKeeper.DeleteExchangeRate(ctx, displayDenom) + _, err := app.OracleKeeper.GetExchangeRate(ctx, displayDenom) s.Require().Error(err) }