Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client/v2): *big.Int unmarshal #21853

Merged
merged 18 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions client/v2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ Ref: https://keepachangelog.com/en/1.0.0/

* [#17709](https://github.com/cosmos/cosmos-sdk/pull/17709) Address codecs have been removed from `autocli.AppOptions` and `flag.Builder`. Instead client/v2 uses the address codecs present in the context (introduced in [#17503](https://github.com/cosmos/cosmos-sdk/pull/17503)).

### Bug Fixes

* [#21853](https://github.com/cosmos/cosmos-sdk/pull/21853) Fix `*big.Int` unmarshalling in txs.
* [#21853](https://github.com/cosmos/cosmos-sdk/pull/21853) Fix `*big.Int` marshalling in queries.

## [v2.0.0-beta.5] - 2024-09-18

### Improvements
Expand Down
2 changes: 2 additions & 0 deletions client/v2/autocli/flag/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -67,6 +68,7 @@ func (b *Builder) init() {
b.scalarFlagTypes[ValidatorAddressStringScalarType] = validatorAddressStringType{}
b.scalarFlagTypes[ConsensusAddressStringScalarType] = consensusAddressStringType{}
b.scalarFlagTypes[PubkeyScalarType] = pubkeyType{}
b.scalarFlagTypes[DecScalarType] = decType{}
}
}

Expand Down
48 changes: 48 additions & 0 deletions client/v2/autocli/flag/legacy_dec.go
Original file line number Diff line number Diff line change
@@ -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
}
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved

func (a decValue) Type() string {
return "cosmos.Dec"
}
30 changes: 29 additions & 1 deletion client/v2/autocli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"errors"
"fmt"
"io"
"math/big"
"strings"
"time"

Expand Down Expand Up @@ -171,7 +172,7 @@
}

func encoder(encoder aminojson.Encoder) aminojson.Encoder {
return encoder.DefineTypeEncoding("google.protobuf.Duration", func(_ *aminojson.Encoder, msg protoreflect.Message, w io.Writer) error {
customEncoder := encoder.DefineTypeEncoding("google.protobuf.Duration", func(_ *aminojson.Encoder, msg protoreflect.Message, w io.Writer) error {
var (
secondsName protoreflect.Name = "seconds"
nanosName protoreflect.Name = "nanos"
Expand Down Expand Up @@ -231,4 +232,31 @@
_, err = fmt.Fprintf(w, `"%s"`, sdk.NewDecCoinFromDec(denom, amountDec)) // TODO(@julienrbrt): Eventually remove this SDK dependency
return err
})

customEncoder.DefineScalarEncoding("cosmos.Dec", func(_ *aminojson.Encoder, value protoreflect.Value, w io.Writer) error {
decStr := value.String()

// If the decimal doesn't contain a point, we assume it's a value formatted using the legacy
// `math.Dec`. So we try to parse it as an integer and then convert it to a
// decimal.
if !strings.Contains(decStr, ".") {
parsedInt, ok := new(big.Int).SetString(decStr, 10)
if !ok {
return fmt.Errorf("invalid decimal: %s", decStr)
}

// We assume the decimal has 18 digits of precision.
decStr = math.LegacyNewDecFromBigIntWithPrec(parsedInt, math.LegacyPrecision).String()
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
}

formatted, err := math.FormatDec(decStr)
if err != nil {
return fmt.Errorf("cannot format decimal %s: %w", decStr, err)
}

_, err = fmt.Fprintf(w, `"%s"`, formatted)
Fixed Show fixed Hide fixed
return nil
})

return customEncoder
}
10 changes: 5 additions & 5 deletions x/auth/tx/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions x/tx/decode/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (d *Decoder) Decode(txBytes []byte) (*DecodedTx, error) {

err = proto.Unmarshal(txBytes, &raw)
if err != nil {
return nil, err
return nil, errorsmod.Wrap(ErrTxDecode, err.Error())
}

var body v1beta1.TxBody
Expand Down Expand Up @@ -136,7 +136,7 @@ func (d *Decoder) Decode(txBytes []byte) (*DecodedTx, error) {
dynamicMsg := dynamicpb.NewMessageType(msgDesc.(protoreflect.MessageDescriptor)).New().Interface()
err = anyMsg.UnmarshalTo(dynamicMsg)
if err != nil {
return nil, err
return nil, errorsmod.Wrap(ErrTxDecode, fmt.Sprintf("cannot unmarshal Any message: %v", err))
}
dynamicMsgs = append(dynamicMsgs, dynamicMsg)

Expand All @@ -148,7 +148,7 @@ func (d *Decoder) Decode(txBytes []byte) (*DecodedTx, error) {
msg := reflect.New(gogoType.Elem()).Interface().(gogoproto.Message)
err = d.codec.Unmarshal(anyMsg.Value, msg)
if err != nil {
return nil, err
return nil, errorsmod.Wrap(ErrTxDecode, err.Error())
}
msgs = append(msgs, msg)

Expand Down
Loading