From d7aa3d3b07ff2a2b6afcee145defffc7e5a18928 Mon Sep 17 00:00:00 2001 From: John Letey Date: Fri, 13 Sep 2024 15:01:36 -0400 Subject: [PATCH 1/4] feat(types/collections): add `LegacyDec` collection value (#21693) Co-authored-by: Luca Graziotti (cherry picked from commit 3bc707a5a214dde7370102a5c3bebf149ea49fb1) # Conflicts: # types/collections.go --- types/collections.go | 66 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/types/collections.go b/types/collections.go index 41b9e18606c..eb840804097 100644 --- a/types/collections.go +++ b/types/collections.go @@ -32,11 +32,41 @@ var ( // IntValue represents a collections.ValueCodec to work with Int. IntValue collcodec.ValueCodec[math.Int] = intValueCodec{} +<<<<<<< HEAD +======= + // UintValue represents a collections.ValueCodec to work with Uint. + UintValue collcodec.ValueCodec[math.Uint] = uintValueCodec{} + + // LegacyDecValue represents a collections.ValueCodec to work with LegacyDec. + LegacyDecValue collcodec.ValueCodec[math.LegacyDec] = legacyDecValueCodec{} + +>>>>>>> 3bc707a5a (feat(types/collections): add `LegacyDec` collection value (#21693)) // TimeKey represents a collections.KeyCodec to work with time.Time // Deprecated: exists only for state compatibility reasons, should not // be used for new storage keys using time. Please use the time KeyCodec // provided in the collections package. TimeKey collcodec.KeyCodec[time.Time] = timeKeyCodec{} +<<<<<<< HEAD +======= + + // LEUint64Key is a collections KeyCodec that encodes uint64 using little endian. + // NOTE: it MUST NOT be used by other modules, distribution relies on this only for + // state backwards compatibility. + // Deprecated: use collections.Uint64Key instead. + LEUint64Key collcodec.KeyCodec[uint64] = leUint64Key{} + + // LengthPrefixedBytesKey is a collections KeyCodec to work with []byte. + // Deprecated: exists only for state compatibility reasons, should not be + // used for new storage keys using []byte. Please use the BytesKey provided + // in the collections package. + LengthPrefixedBytesKey collcodec.KeyCodec[[]byte] = lengthPrefixedBytesKey{collections.BytesKey} +) + +const ( + Int string = "math.Int" + Uint string = "math.Uint" + LegacyDec string = "math.LegacyDec" +>>>>>>> 3bc707a5a (feat(types/collections): add `LegacyDec` collection value (#21693)) ) type addressUnion interface { @@ -166,6 +196,42 @@ func (i intValueCodec) ValueType() string { return "math.Int" } +type legacyDecValueCodec struct{} + +func (i legacyDecValueCodec) Encode(value math.LegacyDec) ([]byte, error) { + return value.Marshal() +} + +func (i legacyDecValueCodec) Decode(b []byte) (math.LegacyDec, error) { + v := new(math.LegacyDec) + err := v.Unmarshal(b) + if err != nil { + return math.LegacyDec{}, err + } + return *v, nil +} + +func (i legacyDecValueCodec) EncodeJSON(value math.LegacyDec) ([]byte, error) { + return value.MarshalJSON() +} + +func (i legacyDecValueCodec) DecodeJSON(b []byte) (math.LegacyDec, error) { + v := new(math.LegacyDec) + err := v.UnmarshalJSON(b) + if err != nil { + return math.LegacyDec{}, err + } + return *v, nil +} + +func (i legacyDecValueCodec) Stringify(value math.LegacyDec) string { + return value.String() +} + +func (i legacyDecValueCodec) ValueType() string { + return LegacyDec +} + type timeKeyCodec struct{} func (timeKeyCodec) Encode(buffer []byte, key time.Time) (int, error) { From cea77b0bd8cd3c8f55f160fb0062ae8e4f481f6d Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 16 Sep 2024 13:04:12 +0700 Subject: [PATCH 2/4] fix --- types/collections.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/types/collections.go b/types/collections.go index eb840804097..237e25c3e6b 100644 --- a/types/collections.go +++ b/types/collections.go @@ -32,22 +32,17 @@ var ( // IntValue represents a collections.ValueCodec to work with Int. IntValue collcodec.ValueCodec[math.Int] = intValueCodec{} -<<<<<<< HEAD -======= // UintValue represents a collections.ValueCodec to work with Uint. UintValue collcodec.ValueCodec[math.Uint] = uintValueCodec{} // LegacyDecValue represents a collections.ValueCodec to work with LegacyDec. LegacyDecValue collcodec.ValueCodec[math.LegacyDec] = legacyDecValueCodec{} ->>>>>>> 3bc707a5a (feat(types/collections): add `LegacyDec` collection value (#21693)) // TimeKey represents a collections.KeyCodec to work with time.Time // Deprecated: exists only for state compatibility reasons, should not // be used for new storage keys using time. Please use the time KeyCodec // provided in the collections package. TimeKey collcodec.KeyCodec[time.Time] = timeKeyCodec{} -<<<<<<< HEAD -======= // LEUint64Key is a collections KeyCodec that encodes uint64 using little endian. // NOTE: it MUST NOT be used by other modules, distribution relies on this only for @@ -66,7 +61,6 @@ const ( Int string = "math.Int" Uint string = "math.Uint" LegacyDec string = "math.LegacyDec" ->>>>>>> 3bc707a5a (feat(types/collections): add `LegacyDec` collection value (#21693)) ) type addressUnion interface { From 3289bd85e014cf1923c61c625ea84f13cfa29483 Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 16 Sep 2024 13:12:39 +0700 Subject: [PATCH 3/4] more fix --- types/collections.go | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/types/collections.go b/types/collections.go index 237e25c3e6b..4d2c84ca829 100644 --- a/types/collections.go +++ b/types/collections.go @@ -32,9 +32,6 @@ var ( // IntValue represents a collections.ValueCodec to work with Int. IntValue collcodec.ValueCodec[math.Int] = intValueCodec{} - // UintValue represents a collections.ValueCodec to work with Uint. - UintValue collcodec.ValueCodec[math.Uint] = uintValueCodec{} - // LegacyDecValue represents a collections.ValueCodec to work with LegacyDec. LegacyDecValue collcodec.ValueCodec[math.LegacyDec] = legacyDecValueCodec{} @@ -43,23 +40,9 @@ var ( // be used for new storage keys using time. Please use the time KeyCodec // provided in the collections package. TimeKey collcodec.KeyCodec[time.Time] = timeKeyCodec{} - - // LEUint64Key is a collections KeyCodec that encodes uint64 using little endian. - // NOTE: it MUST NOT be used by other modules, distribution relies on this only for - // state backwards compatibility. - // Deprecated: use collections.Uint64Key instead. - LEUint64Key collcodec.KeyCodec[uint64] = leUint64Key{} - - // LengthPrefixedBytesKey is a collections KeyCodec to work with []byte. - // Deprecated: exists only for state compatibility reasons, should not be - // used for new storage keys using []byte. Please use the BytesKey provided - // in the collections package. - LengthPrefixedBytesKey collcodec.KeyCodec[[]byte] = lengthPrefixedBytesKey{collections.BytesKey} ) const ( - Int string = "math.Int" - Uint string = "math.Uint" LegacyDec string = "math.LegacyDec" ) From b87bdfea7ae8306dce02b89cda94342e0af87ade Mon Sep 17 00:00:00 2001 From: sontrinh16 Date: Mon, 16 Sep 2024 13:27:43 +0700 Subject: [PATCH 4/4] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 088c7bfa0c3..c89409b685a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (cli) [#20779](https://github.com/cosmos/cosmos-sdk/pull/20779) Added `module-hash-by-height` command to query and retrieve module hashes at a specified blockchain height, enhancing debugging capabilities. * (cli) [#21372](https://github.com/cosmos/cosmos-sdk/pull/21372) Added a `bulk-add-genesis-account` genesis command to add many genesis accounts at once. +* (types/collections) [#21724](https://github.com/cosmos/cosmos-sdk/pull/21724) Added `LegacyDec` collection value. ### Improvements