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

refactor(x/oracle): Refactor GetBaseExchangeRate to return OracleExchangeRate struct #1898

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
56 changes: 28 additions & 28 deletions x/oracle/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestOracleThreshold(t *testing.T) {
oracle.MidBlocker(input.Ctx.WithBlockHeight(1), input.OracleKeeper)
oracle.EndBlocker(input.Ctx.WithBlockHeight(1), input.OracleKeeper)

_, _, _, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx.WithBlockHeight(1), utils.MicroAtomDenom)
_, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx.WithBlockHeight(1), utils.MicroAtomDenom)
require.Error(t, err)

// Case 2.
Expand All @@ -53,10 +53,10 @@ func TestOracleThreshold(t *testing.T) {
oracle.MidBlocker(input.Ctx.WithBlockHeight(1), input.OracleKeeper)
oracle.EndBlocker(input.Ctx.WithBlockHeight(1), input.OracleKeeper)

rate, lastUpdate, _, err := input.OracleKeeper.GetBaseExchangeRate(input.Ctx.WithBlockHeight(1), utils.MicroAtomDenom)
exchangeRate, err := input.OracleKeeper.GetBaseExchangeRate(input.Ctx.WithBlockHeight(1), utils.MicroAtomDenom)
require.NoError(t, err)
require.Equal(t, randomExchangeRate, rate)
require.Equal(t, int64(1), lastUpdate.Int64())
require.Equal(t, randomExchangeRate, exchangeRate.ExchangeRate)
require.Equal(t, int64(1), exchangeRate.LastUpdate.Int64())

// Case 3.
// Increase voting power of absent validator, exchange rate consensus fails
Expand All @@ -74,11 +74,11 @@ func TestOracleThreshold(t *testing.T) {
oracle.MidBlocker(input.Ctx.WithBlockHeight(3), input.OracleKeeper)
oracle.EndBlocker(input.Ctx.WithBlockHeight(3), input.OracleKeeper)

rate, lastUpdate, _, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx.WithBlockHeight(3), utils.MicroAtomDenom)
exchangeRate, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx.WithBlockHeight(3), utils.MicroAtomDenom)
require.NoError(t, err)
require.Equal(t, randomExchangeRate, rate)
require.Equal(t, randomExchangeRate, exchangeRate.ExchangeRate)
// This should still be an older value due to staleness
require.Equal(t, int64(1), lastUpdate.Int64())
require.Equal(t, int64(1), exchangeRate.LastUpdate.Int64())
}

func TestOracleDrop(t *testing.T) {
Expand All @@ -93,11 +93,11 @@ func TestOracleDrop(t *testing.T) {
oracle.MidBlocker(input.Ctx, input.OracleKeeper)
oracle.EndBlocker(input.Ctx, input.OracleKeeper)

rate, lastUpdate, _, err := input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
exchangeRate, err := input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
require.NoError(t, err)
require.Equal(t, randomExchangeRate, rate)
require.Equal(t, randomExchangeRate, exchangeRate.ExchangeRate)
// The value should have a stale height
require.Equal(t, sdk.ZeroInt(), lastUpdate)
require.Equal(t, sdk.ZeroInt(), exchangeRate.LastUpdate)
}

func TestOracleTally(t *testing.T) {
Expand Down Expand Up @@ -194,14 +194,14 @@ func TestOracleTallyTiming(t *testing.T) {

oracle.MidBlocker(input.Ctx, input.OracleKeeper)
oracle.EndBlocker(input.Ctx, input.OracleKeeper)
_, _, _, err := input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
_, err := input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
require.Error(t, err)

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

oracle.MidBlocker(input.Ctx, input.OracleKeeper)
oracle.EndBlocker(input.Ctx, input.OracleKeeper)
_, _, _, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
_, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
require.NoError(t, err)
}

Expand Down Expand Up @@ -398,15 +398,15 @@ func TestInvalidVoteOnAssetUnderThresholdMisses(t *testing.T) {

input.Ctx = input.Ctx.WithBlockHeight(input.Ctx.BlockHeight() + 1)

rate, lastUpdate, _, err := input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
exchangeRate, err := input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
require.NoError(t, err)
require.Equal(t, randomExchangeRate, rate)
require.Equal(t, endBlockerHeight, lastUpdate.Int64())
require.Equal(t, randomExchangeRate, exchangeRate.ExchangeRate)
require.Equal(t, endBlockerHeight, exchangeRate.LastUpdate.Int64())

rate, lastUpdate, _, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroEthDenom)
_, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroEthDenom)
require.NoError(t, err)
require.Equal(t, randomExchangeRate, rate)
require.Equal(t, endBlockerHeight, lastUpdate.Int64())
require.Equal(t, randomExchangeRate, exchangeRate.ExchangeRate)
require.Equal(t, endBlockerHeight, exchangeRate.LastUpdate.Int64())

input.Ctx = input.Ctx.WithBlockHeight(input.Ctx.BlockHeight() + 1)

Expand Down Expand Up @@ -439,17 +439,17 @@ func TestInvalidVoteOnAssetUnderThresholdMisses(t *testing.T) {

input.Ctx = input.Ctx.WithBlockHeight(input.Ctx.BlockHeight() + 1)

rate, lastUpdate, _, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
exchangeRate, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
require.NoError(t, err)
require.Equal(t, anotherRandomExchangeRate, rate)
require.Equal(t, newEndBlockerHeight, lastUpdate.Int64())
require.Equal(t, anotherRandomExchangeRate, exchangeRate.ExchangeRate)
require.Equal(t, newEndBlockerHeight, exchangeRate.LastUpdate.Int64())

// the old value should be persisted because asset didnt meet ballot threshold
rate, lastUpdate, _, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroEthDenom)
exchangeRate, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroEthDenom)
require.NoError(t, err)
require.Equal(t, randomExchangeRate, rate)
require.Equal(t, randomExchangeRate, exchangeRate.ExchangeRate)
// block height should be old
require.Equal(t, endBlockerHeight, lastUpdate.Int64())
require.Equal(t, endBlockerHeight, exchangeRate.LastUpdate.Int64())
}

func TestAbstainSlashing(t *testing.T) {
Expand Down Expand Up @@ -571,7 +571,7 @@ func TestAbstainWithSmallStakingPower(t *testing.T) {

oracle.MidBlocker(input.Ctx, input.OracleKeeper)
oracle.EndBlocker(input.Ctx, input.OracleKeeper)
_, _, _, err := input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
_, err := input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
require.Error(t, err)
}

Expand All @@ -591,11 +591,11 @@ func TestOraclePriceSnapshot(t *testing.T) {
oracle.MidBlocker(input.Ctx, input.OracleKeeper)
oracle.EndBlocker(input.Ctx, input.OracleKeeper)

rate, lastUpdate, _, err := input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
exchangeRate, err := input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
require.NoError(t, err)
require.Equal(t, randomExchangeRate, rate)
require.Equal(t, randomExchangeRate, exchangeRate.ExchangeRate)
// The value should have a stale height
require.Equal(t, sdk.ZeroInt(), lastUpdate)
require.Equal(t, sdk.ZeroInt(), exchangeRate.LastUpdate)
ts := input.Ctx.BlockTime().UnixMilli()

snapshot := input.OracleKeeper.GetPriceSnapshot(input.Ctx, 100)
Expand Down
8 changes: 4 additions & 4 deletions x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
//-----------------------------------
// ExchangeRate logic

func (k Keeper) GetBaseExchangeRate(ctx sdk.Context, denom string) (sdk.Dec, sdk.Int, int64, error) {
func (k Keeper) GetBaseExchangeRate(ctx sdk.Context, denom string) (types.OracleExchangeRate, error) {
exchangeRate := types.OracleExchangeRate{}
store := ctx.KVStore(k.storeKey)
b := store.Get(types.GetExchangeRateKey(denom))
if b == nil {
return sdk.ZeroDec(), sdk.ZeroInt(), 0, sdkerrors.Wrap(types.ErrUnknownDenom, denom)
return exchangeRate, sdkerrors.Wrap(types.ErrUnknownDenom, denom)
}

exchangeRate := types.OracleExchangeRate{}
k.cdc.MustUnmarshal(b, &exchangeRate)
return exchangeRate.ExchangeRate, exchangeRate.LastUpdate, exchangeRate.LastUpdateTimestamp, nil
return exchangeRate, nil
}

func (k Keeper) SetBaseExchangeRate(ctx sdk.Context, denom string, exchangeRate sdk.Dec) {
Expand Down
24 changes: 12 additions & 12 deletions x/oracle/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,33 @@ func TestExchangeRate(t *testing.T) {

// Set & get rates
input.OracleKeeper.SetBaseExchangeRate(input.Ctx, utils.MicroSeiDenom, cnyExchangeRate)
rate, lastUpdate, _, err := input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroSeiDenom)
exchangeRate, err := input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroSeiDenom)
require.NoError(t, err)
require.Equal(t, cnyExchangeRate, rate)
require.Equal(t, sdk.ZeroInt(), lastUpdate)
require.Equal(t, cnyExchangeRate, exchangeRate.ExchangeRate)
require.Equal(t, sdk.ZeroInt(), exchangeRate.LastUpdate)

input.Ctx = input.Ctx.WithBlockHeight(3)
ts := time.Now()
input.Ctx = input.Ctx.WithBlockTime(ts)

input.OracleKeeper.SetBaseExchangeRate(input.Ctx, utils.MicroEthDenom, gbpExchangeRate)
rate, lastUpdate, lastUpdateTimestamp, err := input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroEthDenom)
exchangeRate, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroEthDenom)
require.NoError(t, err)
require.Equal(t, gbpExchangeRate, rate)
require.Equal(t, sdk.NewInt(3), lastUpdate)
require.Equal(t, ts.UnixMilli(), lastUpdateTimestamp)
require.Equal(t, gbpExchangeRate, exchangeRate.ExchangeRate)
require.Equal(t, sdk.NewInt(3), exchangeRate.LastUpdate)
require.Equal(t, ts.UnixMilli(), exchangeRate.LastUpdateTimestamp)

input.Ctx = input.Ctx.WithBlockHeight(15)
laterTS := ts.Add(time.Hour)
input.Ctx = input.Ctx.WithBlockTime(laterTS)

// verify behavior works with event too
input.OracleKeeper.SetBaseExchangeRateWithEvent(input.Ctx, utils.MicroAtomDenom, krwExchangeRate)
rate, lastUpdate, lastUpdateTimestamp, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
exchangeRate, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
require.NoError(t, err)
require.Equal(t, krwExchangeRate, rate)
require.Equal(t, sdk.NewInt(15), lastUpdate)
require.Equal(t, laterTS.UnixMilli(), lastUpdateTimestamp)
require.Equal(t, krwExchangeRate, exchangeRate.ExchangeRate)
require.Equal(t, sdk.NewInt(15), exchangeRate.LastUpdate)
require.Equal(t, laterTS.UnixMilli(), exchangeRate.LastUpdateTimestamp)
require.True(t, func() bool {
expectedEvent := sdk.NewEvent(types.EventTypeExchangeRateUpdate,
sdk.NewAttribute(types.AttributeKeyDenom, utils.MicroAtomDenom),
Expand All @@ -73,7 +73,7 @@ func TestExchangeRate(t *testing.T) {
}())

input.OracleKeeper.DeleteBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
_, _, _, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
_, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
require.Error(t, err)

numExchangeRates := 0
Expand Down
10 changes: 5 additions & 5 deletions x/oracle/keeper/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ func TestMigrate2to3(t *testing.T) {
m.Migrate2to3(input.Ctx)

// Get rate
rate, lastUpdate, lastUpdateTimestamp, err := input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroSeiDenom)
baseExchangeRate, err := input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroSeiDenom)
require.NoError(t, err)
require.Equal(t, exchangeRate, rate)
require.Equal(t, sdk.ZeroInt(), lastUpdate)
require.Equal(t, int64(0), lastUpdateTimestamp)
require.Equal(t, exchangeRate, baseExchangeRate.ExchangeRate)
require.Equal(t, sdk.ZeroInt(), baseExchangeRate.LastUpdate)
require.Equal(t, int64(0), baseExchangeRate.LastUpdateTimestamp)

input.OracleKeeper.DeleteBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
_, _, _, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
_, err = input.OracleKeeper.GetBaseExchangeRate(input.Ctx, utils.MicroAtomDenom)
require.Error(t, err)

numExchangeRates := 0
Expand Down
6 changes: 2 additions & 4 deletions x/oracle/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ func (q querier) ExchangeRate(c context.Context, req *types.QueryExchangeRateReq
}

ctx := sdk.UnwrapSDKContext(c)
exchangeRate, lastUpdate, lastUpdateTimestamp, err := q.GetBaseExchangeRate(ctx, req.Denom)
exchangeRate, err := q.GetBaseExchangeRate(ctx, req.Denom)
if err != nil {
return nil, err
}

return &types.QueryExchangeRateResponse{OracleExchangeRate: types.OracleExchangeRate{
ExchangeRate: exchangeRate, LastUpdate: lastUpdate, LastUpdateTimestamp: lastUpdateTimestamp,
}}, nil
return &types.QueryExchangeRateResponse{OracleExchangeRate: exchangeRate}, nil
}

// ExchangeRates queries exchange rates of all denoms
Expand Down