Skip to content

Commit

Permalink
feat(client/v2): Add clientCtx to commands in autocli (#17709)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
atheeshp and julienrbrt authored Sep 15, 2023
1 parent c7e0bd7 commit a0bd4e9
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 97 deletions.
18 changes: 6 additions & 12 deletions client/v2/autocli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import (
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
"cosmossdk.io/client/v2/autocli/flag"
"cosmossdk.io/client/v2/autocli/keyring"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"
"cosmossdk.io/log"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/runtime"
)

// AppOptions are autocli options for an app. These options can be built via depinject based on an app config. Ex:
Expand All @@ -42,10 +40,8 @@ type AppOptions struct {
// module or need to be improved.
ModuleOptions map[string]*autocliv1.ModuleOptions `optional:"true"`

// AddressCodec is the address codec to use for the app.
AddressCodec address.Codec
ValidatorAddressCodec runtime.ValidatorAddressCodec
ConsensusAddressCodec runtime.ConsensusAddressCodec
// ClientCtx contains the necessary information needed to execute the commands.
ClientCtx *client.Context

// Keyring is the keyring to use for client/v2.
Keyring keyring.Keyring `optional:"true"`
Expand All @@ -70,12 +66,10 @@ func (appOptions AppOptions) EnhanceRootCommand(rootCmd *cobra.Command) error {
builder := &Builder{
Logger: appOptions.Logger,
Builder: flag.Builder{
TypeResolver: protoregistry.GlobalTypes,
FileResolver: proto.HybridResolver,
AddressCodec: appOptions.AddressCodec,
ValidatorAddressCodec: appOptions.ValidatorAddressCodec,
ConsensusAddressCodec: appOptions.ConsensusAddressCodec,
Keyring: appOptions.Keyring,
TypeResolver: protoregistry.GlobalTypes,
FileResolver: proto.HybridResolver,
ClientCtx: appOptions.ClientCtx,
Keyring: appOptions.Keyring,
},
GetClientConn: func(cmd *cobra.Command) (grpc.ClientConnInterface, error) {
return client.GetClientQueryContext(cmd)
Expand Down
22 changes: 15 additions & 7 deletions client/v2/autocli/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,30 @@ func (b *Builder) Validate() error {
b.Logger = log.NewNopLogger()
}

if b.AddressCodec == nil {
if b.ClientCtx == nil {
return errors.New("client context is required in builder")
}

if b.ClientCtx.AddressCodec == nil {
return errors.New("address codec is required in builder")
}

if b.ValidatorAddressCodec == nil {
if b.ClientCtx.ValidatorAddressCodec == nil {
return errors.New("validator address codec is required in builder")
}

if b.ConsensusAddressCodec == nil {
if b.ClientCtx.ConsensusAddressCodec == nil {
return errors.New("consensus address codec is required in builder")
}

if b.Keyring == nil {
if b.ClientCtx.Keyring != nil {
b.Keyring = b.ClientCtx.Keyring
} else {
b.Keyring = keyring.NoKeyring{}
}
}

if b.TypeResolver == nil {
return errors.New("type resolver is required in builder")
}
Expand All @@ -53,9 +65,5 @@ func (b *Builder) Validate() error {
return errors.New("file resolver is required in builder")
}

if b.Keyring == nil {
b.Keyring = keyring.NoKeyring{}
}

return nil
}
28 changes: 23 additions & 5 deletions client/v2/autocli/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ import (
"cosmossdk.io/client/v2/autocli/flag"
"cosmossdk.io/client/v2/internal/testpb"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
)

type fixture struct {
Expand All @@ -27,6 +31,7 @@ type fixture struct {

func initFixture(t *testing.T) *fixture {
t.Helper()
home := t.TempDir()
server := grpc.NewServer()
testpb.RegisterQueryServer(server, &testEchoServer{})
reflectionv2alpha1.RegisterReflectionServiceServer(server, &testReflectionServer{})
Expand All @@ -42,14 +47,27 @@ func initFixture(t *testing.T) *fixture {
clientConn, err := grpc.Dial(listener.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.NilError(t, err)

appCodec := moduletestutil.MakeTestEncodingConfig().Codec
kr, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, home, nil, appCodec)
assert.NilError(t, err)

var initClientCtx client.Context
initClientCtx = initClientCtx.
WithAddressCodec(addresscodec.NewBech32Codec("cosmos")).
WithValidatorAddressCodec(addresscodec.NewBech32Codec("cosmosvaloper")).
WithConsensusAddressCodec(addresscodec.NewBech32Codec("cosmosvalcons")).
WithKeyring(kr).
WithKeyringDir(home).
WithHomeDir(home).
WithViper("")

conn := &testClientConn{ClientConn: clientConn}
b := &Builder{
Builder: flag.Builder{
TypeResolver: protoregistry.GlobalTypes,
FileResolver: protoregistry.GlobalFiles,
AddressCodec: addresscodec.NewBech32Codec("cosmos"),
ValidatorAddressCodec: addresscodec.NewBech32Codec("cosmosvaloper"),
ConsensusAddressCodec: addresscodec.NewBech32Codec("cosmosvalcons"),
TypeResolver: protoregistry.GlobalTypes,
FileResolver: protoregistry.GlobalFiles,
ClientCtx: &initClientCtx,
Keyring: kr,
},
GetClientConn: func(*cobra.Command) (grpc.ClientConnInterface, error) {
return conn, nil
Expand Down
6 changes: 3 additions & 3 deletions client/v2/autocli/flag/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
type addressStringType struct{}

func (a addressStringType) NewValue(_ context.Context, b *Builder) Value {
return &addressValue{addressCodec: b.AddressCodec, keyring: b.Keyring}
return &addressValue{addressCodec: b.ClientCtx.AddressCodec, keyring: b.Keyring}
}

func (a addressStringType) DefaultValue() string {
Expand All @@ -29,7 +29,7 @@ func (a addressStringType) DefaultValue() string {
type validatorAddressStringType struct{}

func (a validatorAddressStringType) NewValue(_ context.Context, b *Builder) Value {
return &addressValue{addressCodec: b.ValidatorAddressCodec, keyring: b.Keyring}
return &addressValue{addressCodec: b.ClientCtx.ValidatorAddressCodec, keyring: b.Keyring}
}

func (a validatorAddressStringType) DefaultValue() string {
Expand Down Expand Up @@ -80,7 +80,7 @@ func (a addressValue) Type() string {
type consensusAddressStringType struct{}

func (a consensusAddressStringType) NewValue(ctx context.Context, b *Builder) Value {
return &consensusAddressValue{addressValue: addressValue{addressCodec: b.ConsensusAddressCodec, keyring: b.Keyring}}
return &consensusAddressValue{addressValue: addressValue{addressCodec: b.ClientCtx.ConsensusAddressCodec, keyring: b.ClientCtx.Keyring}}
}

func (a consensusAddressStringType) DefaultValue() string {
Expand Down
11 changes: 5 additions & 6 deletions client/v2/autocli/flag/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
"cosmossdk.io/client/v2/autocli/keyring"
"cosmossdk.io/client/v2/internal/util"
"cosmossdk.io/core/address"

"github.com/cosmos/cosmos-sdk/client"
)

// Builder manages options for building pflag flags for protobuf messages.
Expand All @@ -38,13 +39,11 @@ type Builder struct {
messageFlagTypes map[protoreflect.FullName]Type
scalarFlagTypes map[string]Type

// AddressCodec is the address codec used for the address flag
AddressCodec address.Codec
ValidatorAddressCodec address.Codec
ConsensusAddressCodec address.Codec

// Keyring implementation
Keyring keyring.Keyring

// ClientCtx contains the necessary information needed to execute the commands.
ClientCtx *client.Context
}

func (b *Builder) init() {
Expand Down
17 changes: 17 additions & 0 deletions client/v2/autocli/msg.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package autocli

import (
"context"
"fmt"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
"github.com/cockroachdb/errors"
"github.com/cosmos/cosmos-sdk/client"
"github.com/spf13/cobra"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/reflect/protoreflect"
Expand Down Expand Up @@ -113,6 +115,21 @@ func (b *Builder) BuildMsgMethodCommand(descriptor protoreflect.MethodDescriptor
return err
}

clientCtx, err := client.ReadPersistentCommandFlags(*b.ClientCtx, cmd.Flags())
if err != nil {
return err
}

cmd.SetContext(context.WithValue(context.Background(), client.ClientContextKey, &clientCtx))
if err = client.SetCmdClientContextHandler(clientCtx, cmd); err != nil {
return err
}

clientCtx, err = client.GetClientTxContext(cmd)
if err != nil {
return err
}

return b.outOrStdoutFormat(cmd, bz)
})

Expand Down
3 changes: 1 addition & 2 deletions client/v2/autocli/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ func TestErrorBuildMsgCommand(t *testing.T) {
Tx: commandDescriptor,
},
},
AddressCodec: b.AddressCodec,
ValidatorAddressCodec: b.ValidatorAddressCodec,
ClientCtx: b.ClientCtx,
}

_, err := b.BuildMsgCommand(appOptions, nil)
Expand Down
12 changes: 4 additions & 8 deletions client/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module cosmossdk.io/client/v2
go 1.21

require (
cosmossdk.io/api v0.7.0
cosmossdk.io/core v0.11.0
cosmossdk.io/api v0.7.1-0.20230820170544-1bd37053e0c0
cosmossdk.io/core v0.12.0
cosmossdk.io/depinject v1.0.0-alpha.4
cosmossdk.io/log v1.2.1
cosmossdk.io/x/tx v0.10.0
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
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230915113003-c7e0bd7b54d0
github.com/cosmos/gogoproto v1.4.11
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
Expand Down Expand Up @@ -73,7 +73,6 @@ require (
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/orderedcode v0.0.1 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
Expand All @@ -96,20 +95,18 @@ require (
github.com/klauspost/compress v1.16.7 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lib/pq v1.10.7 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/linxGnu/grocksdb v1.8.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/minio/highwayhash v1.0.2 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mtibben/percent v0.2.1 // indirect
github.com/oasisprotocol/curve25519-voi v0.0.0-20230110094441-db37f07504ce // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
github.com/petermattis/goid v0.0.0-20230808133559-b036b712a89b // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down Expand Up @@ -137,7 +134,6 @@ require (
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
Expand Down
Loading

0 comments on commit a0bd4e9

Please sign in to comment.