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

feat(client/v2): stringify bytes cosmos.Dec #16985

Merged
merged 11 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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: 4 additions & 1 deletion client/v2/autocli/flag/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ type Builder struct {

// FileResolver specifies how protobuf file descriptors will be resolved. If it is
// nil protoregistry.GlobalFiles will be used.
FileResolver protodesc.Resolver
FileResolver interface {
protodesc.Resolver
RangeFiles(func(protoreflect.FileDescriptor) bool)
}

messageFlagTypes map[protoreflect.FullName]Type
scalarFlagTypes map[string]Type
Expand Down
48 changes: 39 additions & 9 deletions client/v2/autocli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package autocli

import (
"fmt"
"io"
"time"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
"cosmossdk.io/x/tx/signing/aminojson"
"github.com/cockroachdb/errors"
"github.com/spf13/cobra"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/reflect/protoreflect"

"cosmossdk.io/client/v2/internal/util"
Expand Down Expand Up @@ -101,17 +103,16 @@ func (b *Builder) BuildQueryMethodCommand(descriptor protoreflect.MethodDescript
serviceDescriptor := descriptor.Parent().(protoreflect.ServiceDescriptor)
methodName := fmt.Sprintf("/%s/%s", serviceDescriptor.FullName(), descriptor.Name())
outputType := util.ResolveMessageType(b.TypeResolver, descriptor.Output())
jsonMarshalOptions := protojson.MarshalOptions{
encoderOptions := aminojson.EncoderOptions{
Indent: " ",
UseProtoNames: true,
UseEnumNumbers: false,
EmitUnpopulated: true,
Resolver: b.TypeResolver,
DoNotSortFields: true,
TypeResolver: b.TypeResolver,
FileResolver: b.FileResolver,
}

cmd, err := b.buildMethodCommandCommon(descriptor, options, func(cmd *cobra.Command, input protoreflect.Message) error {
if noIdent, _ := cmd.Flags().GetBool(flagNoIndent); noIdent {
jsonMarshalOptions.Indent = ""
if noIndent, _ := cmd.Flags().GetBool(flagNoIndent); noIndent {
encoderOptions.Indent = ""
}

clientConn, err := getClientConn(cmd)
Expand All @@ -124,7 +125,8 @@ func (b *Builder) BuildQueryMethodCommand(descriptor protoreflect.MethodDescript
return err
}

bz, err := jsonMarshalOptions.Marshal(output.Interface())
enc := encoder(aminojson.NewEncoder(encoderOptions))
bz, err := enc.Marshal(output.Interface())
if err != nil {
return fmt.Errorf("cannot marshal response %v: %w", output.Interface(), err)
}
Expand All @@ -143,3 +145,31 @@ func (b *Builder) BuildQueryMethodCommand(descriptor protoreflect.MethodDescript

return cmd, nil
}

func encoder(encoder aminojson.Encoder) aminojson.Encoder {
return encoder.DefineTypeEncoding("google.protobuf.Duration", func(_ *aminojson.Encoder, msg protoreflect.Message, w io.Writer) error {
var (
secondsName protoreflect.Name = "seconds"
nanosName protoreflect.Name = "nanos"
)

fields := msg.Descriptor().Fields()
secondsField := fields.ByName(secondsName)
if secondsField == nil {
return fmt.Errorf("expected seconds field")
}

// check signs are consistent
seconds := msg.Get(secondsField).Int()

nanosField := fields.ByName(nanosName)
if nanosField == nil {
return fmt.Errorf("expected nanos field")
}

nanos := msg.Get(nanosField).Int()

_, err := fmt.Fprintf(w, `"%s"`, (time.Duration(seconds)*time.Second + (time.Duration(nanos) * time.Nanosecond)).String())
return err
})
}
2 changes: 1 addition & 1 deletion client/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
cosmossdk.io/core v0.10.0
cosmossdk.io/depinject v1.0.0-alpha.4
cosmossdk.io/log v1.2.1
cosmossdk.io/x/tx v0.9.2-0.20230911171206-1f8d1ed9203e
github.com/cockroachdb/errors v1.11.1
github.com/cosmos/cosmos-proto v1.0.0-beta.3
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230718211500-1d74652f6021
Expand All @@ -24,7 +25,6 @@ require (
cosmossdk.io/errors v1.0.0 // indirect
cosmossdk.io/math v1.1.2 // indirect
cosmossdk.io/store v1.0.0-rc.0 // indirect
cosmossdk.io/x/tx v0.9.1 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions client/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ cosmossdk.io/math v1.1.2 h1:ORZetZCTyWkI5GlZ6CZS28fMHi83ZYf+A2vVnHNzZBM=
cosmossdk.io/math v1.1.2/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0=
cosmossdk.io/store v1.0.0-rc.0 h1:9DwOjuUYxDtYxn/REkTxGQAmxlIGfRroB35MQ8TrxF4=
cosmossdk.io/store v1.0.0-rc.0/go.mod h1:FtBDOJmwtOZfmKKF65bKZbTYgS3bDNjjo3nP76dAegk=
cosmossdk.io/x/tx v0.9.1 h1:9pmmXA9Vs4qdouOFnzhsdsff2mif0f0kylMq5xTGhRI=
cosmossdk.io/x/tx v0.9.1/go.mod h1:/YFGTXG6+kyihd8YbfuJiXHV4R/mIMm2uvVzo80CIhA=
cosmossdk.io/x/tx v0.9.2-0.20230911171206-1f8d1ed9203e h1:gWCBprurg5QNDrDtWneIanf1Q286AzNK/9gQUT2Fi1c=
cosmossdk.io/x/tx v0.9.2-0.20230911171206-1f8d1ed9203e/go.mod h1:tTDamDDyuFZKNRVAwfeWp1klTb6R1PIme2oNbrkrskQ=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
Expand Down
2 changes: 1 addition & 1 deletion simapp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
cosmossdk.io/x/evidence v0.0.0-20230613133644-0a778132a60f
cosmossdk.io/x/feegrant v0.0.0-20230613133644-0a778132a60f
cosmossdk.io/x/nft v0.0.0-20230613133644-0a778132a60f
cosmossdk.io/x/tx v0.9.1
cosmossdk.io/x/tx v0.9.2-0.20230911171206-1f8d1ed9203e
cosmossdk.io/x/upgrade v0.0.0-20230613133644-0a778132a60f
github.com/cometbft/cometbft v0.38.0-rc3
github.com/cosmos/cosmos-db v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions simapp/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ cosmossdk.io/math v1.1.2 h1:ORZetZCTyWkI5GlZ6CZS28fMHi83ZYf+A2vVnHNzZBM=
cosmossdk.io/math v1.1.2/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0=
cosmossdk.io/store v1.0.0-rc.0 h1:9DwOjuUYxDtYxn/REkTxGQAmxlIGfRroB35MQ8TrxF4=
cosmossdk.io/store v1.0.0-rc.0/go.mod h1:FtBDOJmwtOZfmKKF65bKZbTYgS3bDNjjo3nP76dAegk=
cosmossdk.io/x/tx v0.9.1 h1:9pmmXA9Vs4qdouOFnzhsdsff2mif0f0kylMq5xTGhRI=
cosmossdk.io/x/tx v0.9.1/go.mod h1:/YFGTXG6+kyihd8YbfuJiXHV4R/mIMm2uvVzo80CIhA=
cosmossdk.io/x/tx v0.9.2-0.20230911171206-1f8d1ed9203e h1:gWCBprurg5QNDrDtWneIanf1Q286AzNK/9gQUT2Fi1c=
cosmossdk.io/x/tx v0.9.2-0.20230911171206-1f8d1ed9203e/go.mod h1:tTDamDDyuFZKNRVAwfeWp1klTb6R1PIme2oNbrkrskQ=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
Expand Down
4 changes: 2 additions & 2 deletions simapp/gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ schema = 3
version = "v1.0.0-rc.0"
hash = "sha256-DgW4ZrDwmgsPtEXajPyAsrQuPjXekw5PfsYFvA5yuiE="
[mod."cosmossdk.io/x/tx"]
version = "v0.9.1"
hash = "sha256-/fopDRDbfIUr8Ub0qqu3k0y6FsLKTAwf51PJaCL18Bk="
version = "v0.9.2-0.20230911171206-1f8d1ed9203e"
hash = "sha256-+lAsf0VrK6pUkeexGLiOPeir0I4QEEadUitgILFAKcU="
[mod."filippo.io/edwards25519"]
version = "v1.0.0"
hash = "sha256-APnPAcmItvtJ5Zsy863lzR2TjEBF9Y66TY1e4M1ap98="
Expand Down
2 changes: 1 addition & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
cosmossdk.io/x/evidence v0.0.0-20230613133644-0a778132a60f
cosmossdk.io/x/feegrant v0.0.0-20230613133644-0a778132a60f
cosmossdk.io/x/nft v0.0.0-20230613133644-0a778132a60f // indirect
cosmossdk.io/x/tx v0.9.1
cosmossdk.io/x/tx v0.9.2-0.20230911171206-1f8d1ed9203e
cosmossdk.io/x/upgrade v0.0.0-20230613133644-0a778132a60f
github.com/cometbft/cometbft v0.38.0-rc3
github.com/cosmos/cosmos-db v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ cosmossdk.io/math v1.1.2 h1:ORZetZCTyWkI5GlZ6CZS28fMHi83ZYf+A2vVnHNzZBM=
cosmossdk.io/math v1.1.2/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0=
cosmossdk.io/store v1.0.0-rc.0 h1:9DwOjuUYxDtYxn/REkTxGQAmxlIGfRroB35MQ8TrxF4=
cosmossdk.io/store v1.0.0-rc.0/go.mod h1:FtBDOJmwtOZfmKKF65bKZbTYgS3bDNjjo3nP76dAegk=
cosmossdk.io/x/tx v0.9.1 h1:9pmmXA9Vs4qdouOFnzhsdsff2mif0f0kylMq5xTGhRI=
cosmossdk.io/x/tx v0.9.1/go.mod h1:/YFGTXG6+kyihd8YbfuJiXHV4R/mIMm2uvVzo80CIhA=
cosmossdk.io/x/tx v0.9.2-0.20230911171206-1f8d1ed9203e h1:gWCBprurg5QNDrDtWneIanf1Q286AzNK/9gQUT2Fi1c=
cosmossdk.io/x/tx v0.9.2-0.20230911171206-1f8d1ed9203e/go.mod h1:tTDamDDyuFZKNRVAwfeWp1klTb6R1PIme2oNbrkrskQ=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
Expand Down
2 changes: 1 addition & 1 deletion tests/starship/tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ require (
cosmossdk.io/x/evidence v0.0.0-20230613133644-0a778132a60f // indirect
cosmossdk.io/x/feegrant v0.0.0-20230613133644-0a778132a60f // indirect
cosmossdk.io/x/nft v0.0.0-20230613133644-0a778132a60f // indirect
cosmossdk.io/x/tx v0.9.1 // indirect
cosmossdk.io/x/tx v0.9.2-0.20230911171206-1f8d1ed9203e // indirect
cosmossdk.io/x/upgrade v0.0.0-20230613133644-0a778132a60f // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
Expand Down