diff --git a/CHANGELOG.md b/CHANGELOG.md index 65c6857258..7189643906 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/util/bytes.go b/util/bytes.go index c6380b1cc2..b527f86303 100644 --- a/util/bytes.go +++ b/util/bytes.go @@ -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 +} diff --git a/util/bytes_test.go b/util/bytes_test.go index e93efb73fc..5ea7c66038 100644 --- a/util/bytes_test.go +++ b/util/bytes_test.go @@ -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}) +} diff --git a/util/store/store.go b/util/store/store.go index cce55ae9cd..f480bf819d 100644 --- a/util/store/store.go +++ b/util/store/store.go @@ -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 { @@ -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())