feat(types): Add a ValueCodec for Uint complex type (#19164)

Co-authored-by: Likhita Polavarapu <78951027+likhita-809@users.noreply.github.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
T 2024-01-30 17:01:09 -05:00 committed by GitHub
parent 3e05255956
commit 3f15c4bb78
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 52 additions and 1 deletions

View File

@ -42,6 +42,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
### Features
* (types) [#19164](https://github.com/cosmos/cosmos-sdk/pull/19164) Add a ValueCodec for the math.Uint type that can be used in collections maps.
* (types) [#19281](https://github.com/cosmos/cosmos-sdk/pull/19281) Added a new method, `IsGT`, for `types.Coin`. This method is used to check if a `types.Coin` is greater than another `types.Coin`.
* (client) [#18557](https://github.com/cosmos/cosmos-sdk/pull/18557) Add `--qrcode` flag to `keys show` command to support displaying keys address QR code.
* (client) [#18101](https://github.com/cosmos/cosmos-sdk/pull/18101) Add a `keyring-default-keyname` in `client.toml` for specifying a default key name, and skip the need to use the `--from` flag when signing transactions.

View File

@ -10,3 +10,9 @@ import (
func TestIntValue(t *testing.T) {
colltest.TestValueCodec(t, IntValue, math.NewInt(10005994859))
}
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"))
}

View File

@ -33,6 +33,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{}
// 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
@ -52,6 +55,11 @@ var (
LengthPrefixedBytesKey collcodec.KeyCodec[[]byte] = lengthPrefixedBytesKey{collections.BytesKey}
)
const (
Int string = "math.Int"
Uint string = "math.Uint"
)
type addressUnion interface {
AccAddress | ValAddress | ConsAddress
String() string
@ -198,7 +206,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 timeKeyCodec struct{}