address review comments

This commit is contained in:
Kevin Davis 2020-04-13 14:08:14 -04:00
parent f425efc56c
commit 8f9aece875
4 changed files with 43 additions and 18 deletions

View File

@ -25,7 +25,10 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, gs GenesisState) {
// Set the current price (if any) based on what's now in the store // Set the current price (if any) based on what's now in the store
for _, market := range params.Markets { for _, market := range params.Markets {
if market.Active { if market.Active {
rps := keeper.GetRawPrices(ctx, market.MarketID) rps, err := keeper.GetRawPrices(ctx, market.MarketID)
if err != nil {
panic(err)
}
if len(rps) > 0 { if len(rps) > 0 {
err := keeper.SetCurrentPrices(ctx, market.MarketID) err := keeper.SetCurrentPrices(ctx, market.MarketID)
if err != nil { if err != nil {
@ -44,7 +47,10 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState {
var postedPrices []PostedPrice var postedPrices []PostedPrice
for _, market := range keeper.GetMarkets(ctx) { for _, market := range keeper.GetMarkets(ctx) {
pp := keeper.GetRawPrices(ctx, market.MarketID) pp, err := keeper.GetRawPrices(ctx, market.MarketID)
if err != nil {
panic(err)
}
postedPrices = append(postedPrices, pp...) postedPrices = append(postedPrices, pp...)
} }

View File

@ -46,7 +46,10 @@ func (k Keeper) SetPrice(
// If the expiry is less than or equal to the current blockheight, we consider the price valid // If the expiry is less than or equal to the current blockheight, we consider the price valid
if expiry.After(ctx.BlockTime()) { if expiry.After(ctx.BlockTime()) {
store := ctx.KVStore(k.key) store := ctx.KVStore(k.key)
prices := k.GetRawPrices(ctx, marketID) prices, err := k.GetRawPrices(ctx, marketID)
if err != nil {
return types.PostedPrice{}, err
}
var index int var index int
found := false found := false
for i := range prices { for i := range prices {
@ -96,7 +99,10 @@ func (k Keeper) SetCurrentPrices(ctx sdk.Context, marketID string) sdk.Error {
validPrevPrice = false validPrevPrice = false
} }
prices := k.GetRawPrices(ctx, marketID) prices, err := k.GetRawPrices(ctx, marketID)
if err != nil {
return err
}
var notExpiredPrices types.CurrentPrices var notExpiredPrices types.CurrentPrices
// filter out expired prices // filter out expired prices
for _, v := range prices { for _, v := range prices {
@ -174,8 +180,10 @@ func (k Keeper) GetCurrentPrice(ctx sdk.Context, marketID string) (types.Current
return types.CurrentPrice{}, types.ErrNoValidPrice(k.codespace) return types.CurrentPrice{}, types.ErrNoValidPrice(k.codespace)
} }
var price types.CurrentPrice var price types.CurrentPrice
k.cdc.MustUnmarshalBinaryBare(bz, &price) err := k.cdc.UnmarshalBinaryBare(bz, &price)
if err != nil {
return types.CurrentPrice{}, sdk.ErrInternal(sdk.AppendMsgToErr("failed to unmarshal result", err.Error()))
}
if price.Price.Equal(sdk.ZeroDec()) { if price.Price.Equal(sdk.ZeroDec()) {
return types.CurrentPrice{}, types.ErrNoValidPrice(k.codespace) return types.CurrentPrice{}, types.ErrNoValidPrice(k.codespace)
} }
@ -183,12 +191,18 @@ func (k Keeper) GetCurrentPrice(ctx sdk.Context, marketID string) (types.Current
} }
// GetRawPrices fetches the set of all prices posted by oracles for an asset // GetRawPrices fetches the set of all prices posted by oracles for an asset
func (k Keeper) GetRawPrices(ctx sdk.Context, marketID string) types.PostedPrices { func (k Keeper) GetRawPrices(ctx sdk.Context, marketID string) (types.PostedPrices, sdk.Error) {
store := ctx.KVStore(k.key) store := ctx.KVStore(k.key)
bz := store.Get(types.RawPriceKey(marketID)) bz := store.Get(types.RawPriceKey(marketID))
if bz == nil {
return types.PostedPrices{}, nil
}
var prices types.PostedPrices var prices types.PostedPrices
k.cdc.MustUnmarshalBinaryBare(bz, &prices) err := k.cdc.UnmarshalBinaryBare(bz, &prices)
return prices if err != nil {
return types.PostedPrices{}, sdk.ErrInternal(sdk.AppendMsgToErr("failed to unmarshal result", err.Error()))
}
return prices, nil
} }
// Codespace return the codespace for the keeper // Codespace return the codespace for the keeper

View File

@ -67,7 +67,8 @@ func TestKeeper_GetSetPrice(t *testing.T) {
time.Now().Add(1*time.Hour)) time.Now().Add(1*time.Hour))
require.NoError(t, err) require.NoError(t, err)
// Get raw prices // Get raw prices
rawPrices := keeper.GetRawPrices(ctx, "tstusd") rawPrices, err := keeper.GetRawPrices(ctx, "tstusd")
require.NoError(t, err)
require.Equal(t, len(rawPrices), 1) require.Equal(t, len(rawPrices), 1)
require.Equal(t, rawPrices[0].Price.Equal(sdk.MustNewDecFromStr("0.33")), true) require.Equal(t, rawPrices[0].Price.Equal(sdk.MustNewDecFromStr("0.33")), true)
// Set price by oracle 2 // Set price by oracle 2
@ -77,7 +78,8 @@ func TestKeeper_GetSetPrice(t *testing.T) {
time.Now().Add(time.Hour*1)) time.Now().Add(time.Hour*1))
require.NoError(t, err) require.NoError(t, err)
rawPrices = keeper.GetRawPrices(ctx, "tstusd") rawPrices, err = keeper.GetRawPrices(ctx, "tstusd")
require.NoError(t, err)
require.Equal(t, len(rawPrices), 2) require.Equal(t, len(rawPrices), 2)
require.Equal(t, rawPrices[1].Price.Equal(sdk.MustNewDecFromStr("0.35")), true) require.Equal(t, rawPrices[1].Price.Equal(sdk.MustNewDecFromStr("0.35")), true)
@ -87,7 +89,8 @@ func TestKeeper_GetSetPrice(t *testing.T) {
sdk.MustNewDecFromStr("0.37"), sdk.MustNewDecFromStr("0.37"),
time.Now().Add(time.Hour*1)) time.Now().Add(time.Hour*1))
require.NoError(t, err) require.NoError(t, err)
rawPrices = keeper.GetRawPrices(ctx, "tstusd") rawPrices, err = keeper.GetRawPrices(ctx, "tstusd")
require.NoError(t, err)
require.Equal(t, rawPrices[0].Price.Equal(sdk.MustNewDecFromStr("0.37")), true) require.Equal(t, rawPrices[0].Price.Equal(sdk.MustNewDecFromStr("0.37")), true)
} }

View File

@ -56,18 +56,20 @@ func queryPrice(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (res []by
func queryRawPrices(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (res []byte, sdkErr sdk.Error) { func queryRawPrices(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (res []byte, sdkErr sdk.Error) {
var requestParams types.QueryWithMarketIDParams var requestParams types.QueryWithMarketIDParams
err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams) err2 := keeper.cdc.UnmarshalJSON(req.Data, &requestParams)
if err != nil { if err2 != nil {
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err)) return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err2))
} }
_, found := keeper.GetMarket(ctx, requestParams.MarketID) _, found := keeper.GetMarket(ctx, requestParams.MarketID)
if !found { if !found {
return []byte{}, sdk.ErrUnknownRequest("asset not found") return []byte{}, sdk.ErrUnknownRequest("asset not found")
} }
rawPrices := keeper.GetRawPrices(ctx, requestParams.MarketID) rawPrices, err := keeper.GetRawPrices(ctx, requestParams.MarketID)
bz, err := codec.MarshalJSONIndent(keeper.cdc, rawPrices)
if err != nil { if err != nil {
return nil, err
}
bz, err2 := codec.MarshalJSONIndent(keeper.cdc, rawPrices)
if err2 != nil {
panic("could not marshal result to JSON") panic("could not marshal result to JSON")
} }