Skip to content

Commit

Permalink
feat(util): KeyWithUint64 and KeyWithUint32 helper functions for db k…
Browse files Browse the repository at this point in the history
…eys (#2495)

* feat(util): KeyWithUint64 and KeyWithUint32 helper functions for db keys

* changelog

* lint
  • Loading branch information
robert-zaremba authored Apr 16, 2024
1 parent 571d7c6 commit 04288c1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

- [2474](https://github.com/umee-network/umee/pull/2474) (proto) add `gogo.messagename_all` option to all messages.
- [2494](https://github.com/umee-network/umee/pull/2494) Use go 1.22
- [2495](https://github.com/umee-network/umee/pull/2495) (util) `KeyWithUint64` and `KeyWithUint32` helper functions for db keys.

### Bug Fixes

Expand Down
18 changes: 18 additions & 0 deletions util/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,21 @@ func UintWithNullPrefix(n uint64) []byte {
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 {
out := make([]byte, len(prefix)+4)
copy(out, prefix)
binary.BigEndian.PutUint32(out[len(prefix):], n)
return out
}

// KeyWithUint64 concatenates prefix big endian serialized n value.
// No zero byte is appended at the end.
func KeyWithUint64(prefix []byte, n uint64) []byte {
out := make([]byte, len(prefix)+8)
copy(out, prefix)
binary.BigEndian.PutUint64(out[len(prefix):], n)
return out
}
24 changes: 24 additions & 0 deletions util/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,27 @@ func TestUintWithNullPrefix(t *testing.T) {
out := UintWithNullPrefix(math.MaxUint64)
assert.DeepEqual(t, expected, out)
}

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

out := KeyWithUint32(nil, 200)
assert.DeepEqual(t, out, []byte{0, 0, 0, 200})

out = KeyWithUint32(prefix, 200)
assert.DeepEqual(t, out, []byte{1, 10, 0, 0, 0, 200})

out = KeyWithUint32(prefix, 256)
assert.DeepEqual(t, out, []byte{1, 10, 0, 0, 1, 0})

// uint64 version

out = KeyWithUint64(nil, 200)
assert.DeepEqual(t, out, []byte{0, 0, 0, 0, 0, 0, 0, 200})

out = KeyWithUint64(prefix, 200)
assert.DeepEqual(t, out, []byte{1, 10, 0, 0, 0, 0, 0, 0, 0, 200})

out = KeyWithUint64(prefix, 256)
assert.DeepEqual(t, out, []byte{1, 10, 0, 0, 0, 0, 0, 0, 1, 0})
}
8 changes: 6 additions & 2 deletions util/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func SetBinValue[T BinMarshalable](store sdk.KVStore, key []byte, value T, errFi
// instead of GetValue.
// Returns a boolean indicating whether any data was found. If the return is false, the object
// is not changed by this function.
func GetValueCdc(store sdk.KVStore, cdc codec.BinaryCodec, key []byte, object codec.ProtoMarshaler, errField string) bool {
func GetValueCdc(store sdk.KVStore, cdc codec.BinaryCodec, key []byte, object codec.ProtoMarshaler,
errField string) bool {

if bz := store.Get(key); len(bz) > 0 {
err := cdc.Unmarshal(bz, object)
if err != nil {
Expand All @@ -88,7 +90,9 @@ func GetValueCdc(store sdk.KVStore, cdc codec.BinaryCodec, key []byte, object co
// SetValueCdc is similar to the SetValue, but uses codec for marshaling. For Protobuf objects the
// result is the same, unless codec.Any is used. In the latter case this function MUST be used,
// instead of SetValue.
func SetValueCdc(store sdk.KVStore, cdc codec.BinaryCodec, key []byte, object codec.ProtoMarshaler, errField string) error {
func SetValueCdc(store sdk.KVStore, cdc codec.BinaryCodec, key []byte, object codec.ProtoMarshaler,
errField string) error {

bz, err := cdc.Marshal(object)
if err != nil {
return fmt.Errorf("failed to encode %s, %s", errField, err.Error())
Expand Down

0 comments on commit 04288c1

Please sign in to comment.