Skip to content

Commit

Permalink
chore(util)\!: move UintWithNullPrefix to oracle module
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed May 6, 2024
1 parent 2f0b9aa commit 9a0a5a9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
10 changes: 0 additions & 10 deletions util/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@ func ConcatBytes(margin int, bzs ...[]byte) []byte {
return out
}

// UintWithNullPrefix efficiently serializes uint using LittleEndian and
// prepends zero byte (null prefix).
// TODO: ideally we use BigEndian here (for prefix ordering), but it will require
// data migration.
func UintWithNullPrefix(n uint64) []byte {
bz := make([]byte, 9)
binary.LittleEndian.PutUint64(bz[1:], n)
return bz
}

// KeyWithUint32 concatenates prefix big endian serialized n value.
// No zero byte is appended at the end.
func KeyWithUint32(prefix []byte, n uint32) []byte {
Expand Down
12 changes: 0 additions & 12 deletions util/bytes_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package util

import (
"encoding/binary"
"math"
"testing"

"gotest.tools/v3/assert"
Expand All @@ -27,16 +25,6 @@ func TestMergeBytes(t *testing.T) {
}
}

func TestUintWithNullPrefix(t *testing.T) {
expected := []byte{0}
num := make([]byte, 8)
binary.LittleEndian.PutUint64(num, math.MaxUint64)
expected = append(expected, num...)

out := UintWithNullPrefix(math.MaxUint64)
assert.DeepEqual(t, expected, out)
}

func TestKeyWithUint(t *testing.T) {
prefix := []byte{1, 10}

Expand Down
16 changes: 13 additions & 3 deletions x/oracle/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ func KeyAggregateExchangeRateVote(v sdk.ValAddress) []byte {

// KeyMedian - stored by *denom*
func KeyMedian(denom string, blockNum uint64) (key []byte) {
return util.ConcatBytes(0, KeyPrefixMedian, []byte(denom), util.UintWithNullPrefix(blockNum))
return util.ConcatBytes(0, KeyPrefixMedian, []byte(denom), uintWithNullPrefix(blockNum))
}

// KeyMedianDeviation - stored by *denom*
func KeyMedianDeviation(denom string, blockNum uint64) (key []byte) {
return util.ConcatBytes(0, KeyPrefixMedianDeviation, []byte(denom), util.UintWithNullPrefix(blockNum))
return util.ConcatBytes(0, KeyPrefixMedianDeviation, []byte(denom), uintWithNullPrefix(blockNum))
}

// KeyHistoricPrice - stored by *denom* and *block*
func KeyHistoricPrice(denom string, blockNum uint64) (key []byte) {
return util.ConcatBytes(0, KeyPrefixHistoricPrice, []byte(denom), util.UintWithNullPrefix(blockNum))
return util.ConcatBytes(0, KeyPrefixHistoricPrice, []byte(denom), uintWithNullPrefix(blockNum))
}

// KeyHistoricPrice - stored by *denom* and *block*
Expand All @@ -84,3 +84,13 @@ func KeyAvgCounter(denom string, counterID byte) (key []byte) {
func ParseDenomAndBlockFromKey(key []byte, prefix []byte) (string, uint64) {
return string(key[len(prefix) : len(key)-9]), binary.LittleEndian.Uint64(key[len(key)-8:])
}

// uintWithNullPrefix efficiently serializes uint using LittleEndian and
// prepends zero byte (null prefix).
// TODO: ideally we use BigEndian here (for prefix ordering), but it will require
// data migration.
func uintWithNullPrefix(n uint64) []byte {
bz := make([]byte, 9)
binary.LittleEndian.PutUint64(bz[1:], n)
return bz
}
12 changes: 12 additions & 0 deletions x/oracle/types/keys_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"encoding/binary"
"math"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -138,3 +140,13 @@ func TestParseDenomAndBlockFromMedianDeviationKey(t *testing.T) {
assert.Equal(t, denom, parsedDenom)
assert.Equal(t, blockNum, parsedBlockNum)
}

func TestUintWithNullPrefix(t *testing.T) {
expected := []byte{0}
num := make([]byte, 8)
binary.LittleEndian.PutUint64(num, math.MaxUint64)
expected = append(expected, num...)

out := uintWithNullPrefix(math.MaxUint64)
assert.Equal(t, expected, out)
}

0 comments on commit 9a0a5a9

Please sign in to comment.