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): historical iterator #1573

Merged
merged 2 commits into from
Nov 11, 2022
Merged
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
17 changes: 17 additions & 0 deletions util/bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package util

// ConcatBytes creates a new slice by merging list of bytes and leaving empty amount of margin
// bytes at the end
func ConcatBytes(margin int, bzs ...[]byte) []byte {
l := 0
for _, bz := range bzs {
l += len(bz)
}
out := make([]byte, l+margin)
offset := 0
for _, bz := range bzs {
copy(out[offset:], bz)
offset += len(bz)
}
return out
}
24 changes: 24 additions & 0 deletions util/bytes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package util

import "testing"
import "github.com/stretchr/testify/require"

func TestMergeBytes(t *testing.T) {
require := require.New(t)
tcs := []struct {
in [][]byte
inMargin int
out []byte
}{
{[][]byte{}, 0, []byte{}},
{[][]byte{}, 2, []byte{0, 0}},
{[][]byte{{1}}, 0, []byte{1}},
{[][]byte{{1}}, 1, []byte{1, 0}},
{[][]byte{{1, 2}, {2}}, 0, []byte{1, 2, 2}},
{[][]byte{{1, 2}, {2}}, 3, []byte{1, 2, 2, 0, 0, 0}},
{[][]byte{{1, 2}, {2}, {3, 3}, {4}}, 1, []byte{1, 2, 2, 3, 3, 4, 0}},
}
for i, tc := range tcs {
require.Equal(tc.out, ConcatBytes(tc.inMargin, tc.in...), i)
}
}
21 changes: 12 additions & 9 deletions x/oracle/keeper/historic_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/umee-network/umee/v3/util"
"github.com/umee-network/umee/v3/x/oracle/types"
)

Expand Down Expand Up @@ -54,7 +55,7 @@ func (k Keeper) GetMedian(
blockNum uint64,
) (sdk.Dec, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetMedianKey(denom, blockNum))
bz := store.Get(types.MedianKey(denom, blockNum))
if bz == nil {
return sdk.ZeroDec(), sdkerrors.Wrap(types.ErrNoMedian, fmt.Sprintf("denom: %s block: %d", denom, blockNum))
}
Expand All @@ -77,7 +78,7 @@ func (k Keeper) SetMedian(
historicPrices := k.getHistoricPrices(ctx, denom)
median := median(historicPrices)
bz := k.cdc.MustMarshal(&sdk.DecProto{Dec: median})
store.Set(types.GetMedianKey(denom, uint64(ctx.BlockHeight())), bz)
store.Set(types.MedianKey(denom, uint64(ctx.BlockHeight())), bz)
k.setMedianDeviation(ctx, denom, median, historicPrices)
}

Expand All @@ -89,7 +90,7 @@ func (k Keeper) GetMedianDeviation(
blockNum uint64,
) (sdk.Dec, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetMedianDeviationKey(denom, blockNum))
bz := store.Get(types.MedianDeviationKey(denom, blockNum))
if bz == nil {
return sdk.ZeroDec(), sdkerrors.Wrap(types.ErrNoMedianDeviation, fmt.Sprintf("denom: %s block: %d", denom, blockNum))
}
Expand All @@ -111,7 +112,7 @@ func (k Keeper) setMedianDeviation(
store := ctx.KVStore(k.storeKey)
medianDeviation := medianDeviation(median, prices)
bz := k.cdc.MustMarshal(&sdk.DecProto{Dec: medianDeviation})
store.Set(types.GetMedianDeviationKey(denom, uint64(ctx.BlockHeight())), bz)
store.Set(types.MedianDeviationKey(denom, uint64(ctx.BlockHeight())), bz)
}

// getHistoricPrices returns all the historic prices of a given denom.
Expand Down Expand Up @@ -143,7 +144,9 @@ func (k Keeper) IterateHistoricPrices(
) {
store := ctx.KVStore(k.storeKey)

iter := sdk.KVStorePrefixIterator(store, append(types.KeyPrefixHistoricPrice, []byte(denom)...))
// make sure we have one zero byte to correctly separate denoms
prefix := util.ConcatBytes(1, types.KeyPrefixHistoricPrice, []byte(denom))
iter := sdk.KVStorePrefixIterator(store, prefix)
defer iter.Close()

for ; iter.Valid(); iter.Next() {
Expand All @@ -168,7 +171,7 @@ func (k Keeper) AddHistoricPrice(
ExchangeRate: exchangeRate,
BlockNum: block,
})
store.Set(types.GetHistoricPriceKey(denom, block), bz)
store.Set(types.HistoricPriceKey(denom, block), bz)
}

// DeleteHistoricPrice deletes the historic price of a denom at a
Expand All @@ -179,7 +182,7 @@ func (k Keeper) DeleteHistoricPrice(
blockNum uint64,
) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetHistoricPriceKey(denom, blockNum))
store.Delete(types.HistoricPriceKey(denom, blockNum))
}

// DeleteMedian deletes a given denom's median price in the last prune
Expand All @@ -190,7 +193,7 @@ func (k Keeper) DeleteMedian(
blockNum uint64,
) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetMedianKey(denom, blockNum))
store.Delete(types.MedianKey(denom, blockNum))
}

// DeleteMedianDeviation deletes a given denom's standard deviation around
Expand All @@ -201,5 +204,5 @@ func (k Keeper) DeleteMedianDeviation(
blockNum uint64,
) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetMedianDeviationKey(denom, blockNum))
store.Delete(types.MedianDeviationKey(denom, blockNum))
}
27 changes: 12 additions & 15 deletions x/oracle/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,33 +61,30 @@ func GetAggregateExchangeRateVoteKey(v sdk.ValAddress) (key []byte) {
return append(key, address.MustLengthPrefix(v)...)
}

// GetMedianKey - stored by *denom* and *block*
func GetMedianKey(denom string, blockNum uint64) (key []byte) {
// MedianKey - stored by *denom* and *block*
func MedianKey(denom string, blockNum uint64) (key []byte) {
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
key = append(key, KeyPrefixMedian...)
key = appendDenomAndBlockToKey(key, denom, blockNum)
return append(key, 0) // append 0 for null-termination
return appendDenomAndBlock(key, denom, blockNum)
}

// GetMedianDeviationKey - stored by *denom* and *block*
func GetMedianDeviationKey(denom string, blockNum uint64) (key []byte) {
// MedianDeviationKey - stored by *denom* and *block*
func MedianDeviationKey(denom string, blockNum uint64) (key []byte) {
key = append(key, KeyPrefixMedianDeviation...)
key = appendDenomAndBlockToKey(key, denom, blockNum)
return append(key, 0) // append 0 for null-termination
return appendDenomAndBlock(key, denom, blockNum)
}

// GetHistoricPriceKey - stored by *denom* and *block*
func GetHistoricPriceKey(denom string, blockNum uint64) (key []byte) {
// HistoricPriceKey - stored by *denom* and *block*
func HistoricPriceKey(denom string, blockNum uint64) (key []byte) {
key = append(key, KeyPrefixHistoricPrice...)
key = appendDenomAndBlockToKey(key, denom, blockNum)
return append(key, 0) // append 0 for null-termination
return appendDenomAndBlock(key, denom, blockNum)
}

func appendDenomAndBlockToKey(key []byte, denom string, blockNum uint64) []byte {
func appendDenomAndBlock(key []byte, denom string, blockNum uint64) []byte {
key = append(key, []byte(denom)...)
key = append(key, 0) // null delimeter to avoid collision between different denoms
block := make([]byte, 8)
binary.LittleEndian.PutUint64(block, blockNum)
key = append(key, block...)
return key
return append(key, block...)
}

func ParseDemonFromHistoricPriceKey(key []byte) string {
Expand Down