From 1aa3a284d7b9f15340d770438433a1a2e100b0eb Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 8 Oct 2024 21:24:01 +0200 Subject: [PATCH] fix(client/v2): *big.Int unmarshal (backport #21853) (#22174) Co-authored-by: Julien Robert --- client/v2/CHANGELOG.md | 12 ++++++- client/v2/autocli/flag/builder.go | 2 ++ client/v2/autocli/flag/legacy_dec.go | 48 ++++++++++++++++++++++++++++ x/auth/tx/builder.go | 10 +++--- 4 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 client/v2/autocli/flag/legacy_dec.go diff --git a/client/v2/CHANGELOG.md b/client/v2/CHANGELOG.md index 831ee40ce922..5dce1ff0ecdf 100644 --- a/client/v2/CHANGELOG.md +++ b/client/v2/CHANGELOG.md @@ -45,7 +45,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements -* [#21712](https://github.com/cosmos/cosmos-sdk/pull/21712) Marshal `type` field as proto message url in queries instead of amino name. +* [#21936](https://github.com/cosmos/cosmos-sdk/pull/21936) Print possible enum values in error message after an invalid input was provided. ### API Breaking Changes @@ -53,6 +53,16 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* [#21853](https://github.com/cosmos/cosmos-sdk/pull/21853) Fix `*big.Int` unmarshalling in txs. + +## [v2.0.0-beta.5] - 2024-09-18 + +### Improvements + +* [#21712](https://github.com/cosmos/cosmos-sdk/pull/21712) Marshal `type` field as proto message url in queries instead of amino name. + +### Bug Fixes + * [#20964](https://github.com/cosmos/cosmos-sdk/pull/20964) Fix `GetNodeHomeDirectory` helper in `client/v2/helpers` to respect the `(PREFIX)_HOME` environment variable. ## [v2.0.0-beta.3] - 2024-07-15 diff --git a/client/v2/autocli/flag/builder.go b/client/v2/autocli/flag/builder.go index 6ff325c53bdb..bdd42634dd35 100644 --- a/client/v2/autocli/flag/builder.go +++ b/client/v2/autocli/flag/builder.go @@ -26,6 +26,7 @@ const ( ValidatorAddressStringScalarType = "cosmos.ValidatorAddressString" ConsensusAddressStringScalarType = "cosmos.ConsensusAddressString" PubkeyScalarType = "cosmos.Pubkey" + DecScalarType = "cosmos.Dec" ) // Builder manages options for building pflag flags for protobuf messages. @@ -67,6 +68,7 @@ func (b *Builder) init() { b.scalarFlagTypes[ValidatorAddressStringScalarType] = validatorAddressStringType{} b.scalarFlagTypes[ConsensusAddressStringScalarType] = consensusAddressStringType{} b.scalarFlagTypes[PubkeyScalarType] = pubkeyType{} + b.scalarFlagTypes[DecScalarType] = decType{} } } diff --git a/client/v2/autocli/flag/legacy_dec.go b/client/v2/autocli/flag/legacy_dec.go new file mode 100644 index 000000000000..073afa94f1f5 --- /dev/null +++ b/client/v2/autocli/flag/legacy_dec.go @@ -0,0 +1,48 @@ +package flag + +import ( + "context" + + "google.golang.org/protobuf/reflect/protoreflect" + + "cosmossdk.io/math" +) + +type decType struct{} + +func (a decType) NewValue(_ *context.Context, _ *Builder) Value { + return &decValue{} +} + +func (a decType) DefaultValue() string { + return "0" +} + +type decValue struct { + value string +} + +func (a decValue) Get(protoreflect.Value) (protoreflect.Value, error) { + return protoreflect.ValueOf(a.value), nil +} + +func (a decValue) String() string { + return a.value +} + +func (a *decValue) Set(s string) error { + dec, err := math.LegacyNewDecFromStr(s) + if err != nil { + return err + } + + // we need to convert from float representation to non-float representation using default precision + // 0.5 -> 500000000000000000 + a.value = dec.BigInt().String() + + return nil +} + +func (a decValue) Type() string { + return "cosmos.Dec" +} diff --git a/x/auth/tx/builder.go b/x/auth/tx/builder.go index 423bca3a0870..2b1c866e8e0a 100644 --- a/x/auth/tx/builder.go +++ b/x/auth/tx/builder.go @@ -112,7 +112,7 @@ var marshalOption = proto.MarshalOptions{ func (w *builder) getTx() (*gogoTxWrapper, error) { anyMsgs, err := msgsV1toAnyV2(w.msgs) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to convert messages: %w", err) } body := &txv1beta1.TxBody{ Messages: anyMsgs, @@ -136,12 +136,12 @@ func (w *builder) getTx() (*gogoTxWrapper, error) { bodyBytes, err := marshalOption.Marshal(body) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to marshal body: %w", err) } authInfoBytes, err := marshalOption.Marshal(authInfo) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to marshal auth info: %w", err) } txRawBytes, err := marshalOption.Marshal(&txv1beta1.TxRaw{ @@ -150,12 +150,12 @@ func (w *builder) getTx() (*gogoTxWrapper, error) { Signatures: w.signatures, }) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to marshal tx raw: %w", err) } decodedTx, err := w.decoder.Decode(txRawBytes) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to decode tx: %w", err) } return newWrapperFromDecodedTx(w.addressCodec, w.codec, decodedTx)