diff --git a/CHANGELOG.md b/CHANGELOG.md index 32aa6e30b8..b713c4d08e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features +* (types) [#23780](https://github.com/cosmos/cosmos-sdk/pull/23780) Add a ValueCodec for the math.Uint type that can be used in collections maps. * (perf)[#24045](https://github.com/cosmos/cosmos-sdk/pull/24045) Sims: Replace runsim command with Go stdlib testing. CLI: `Commit` default true, `Lean`, `SimulateEveryOperation`, `PrintAllInvariants`, `DBBackend` params removed * (crypto/keyring) [#24040](https://github.com/cosmos/cosmos-sdk/pull/24040) Expose the db keyring used in the keystore. * (types) [#23919](https://github.com/cosmos/cosmos-sdk/pull/23919) Add MustValAddressFromBech32 function. diff --git a/types/codec_test.go b/types/codec_test.go index b4b01c23ec..5314904970 100644 --- a/types/codec_test.go +++ b/types/codec_test.go @@ -3,10 +3,36 @@ package types import ( "testing" + "github.com/stretchr/testify/require" + "cosmossdk.io/collections/colltest" "cosmossdk.io/math" ) func TestIntValue(t *testing.T) { colltest.TestValueCodec(t, IntValue, math.NewInt(10005994859)) + + original := math.NewInt(132005994859) + + // Stringify Test + str := IntValue.Stringify(original) + require.Equal(t, original.String(), str) + + // ValueType Test + require.Equal(t, IntValue.ValueType(), "math.Int") +} + +func TestUintValue(t *testing.T) { + colltest.TestValueCodec(t, UintValue, math.NewUint(1337)) + colltest.TestValueCodec(t, UintValue, math.ZeroUint()) + colltest.TestValueCodec(t, UintValue, math.NewUintFromString("1000000000000000000")) + + original := math.NewUint(1234567890) + + // Stringify Test + str := UintValue.Stringify(original) + require.Equal(t, original.String(), str) + + // ValueType Test + require.Equal(t, UintValue.ValueType(), "math.Uint") } diff --git a/types/collections.go b/types/collections.go index 4d2c84ca82..b79d748866 100644 --- a/types/collections.go +++ b/types/collections.go @@ -32,6 +32,9 @@ 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{} @@ -46,6 +49,11 @@ const ( LegacyDec string = "math.LegacyDec" ) +const ( + Int string = "math.Int" + Uint string = "math.Uint" +) + type addressUnion interface { AccAddress | ValAddress | ConsAddress String() string @@ -170,7 +178,43 @@ func (i intValueCodec) Stringify(value math.Int) string { } func (i intValueCodec) ValueType() string { - return "math.Int" + return Int +} + +type uintValueCodec struct{} + +func (i uintValueCodec) Encode(value math.Uint) ([]byte, error) { + return value.Marshal() +} + +func (i uintValueCodec) Decode(b []byte) (math.Uint, error) { + v := new(math.Uint) + err := v.Unmarshal(b) + if err != nil { + return math.Uint{}, err + } + return *v, nil +} + +func (i uintValueCodec) EncodeJSON(value math.Uint) ([]byte, error) { + return value.MarshalJSON() +} + +func (i uintValueCodec) DecodeJSON(b []byte) (math.Uint, error) { + v := new(math.Uint) + err := v.UnmarshalJSON(b) + if err != nil { + return math.Uint{}, err + } + return *v, nil +} + +func (i uintValueCodec) Stringify(value math.Uint) string { + return value.String() +} + +func (i uintValueCodec) ValueType() string { + return Uint } type legacyDecValueCodec struct{}