Skip to content

Commit

Permalink
Migrate x/auth cmd's to TxGenerator marshaling (cosmos#6391)
Browse files Browse the repository at this point in the history
* Migrate encode, decode, and broadcast cmd's to use TxGenerator marshal methods

* Fix tests, add EncodingConfig

* Add simapp/encoding.go and wire up with simcli

* add godocs

* fix tests

* Debugging CLI Tests

* Fix integration test

* 6391 - lint issues and code coverage (cosmos#6414)

* fixed lint issue of "txEncodeRespStr"

* added tests for encode.go

* WIP: added tests for decode.go

* added txEncoder at bytes

* updated decode test

* updated txBytes to use TxEncoder in decoder test

* added a require after TxEncoder

* removed file save

* debug decode command

* fixed decode tests

* added decode cli

* updated encode and decode in a single file

* separated decode test from encode test

* review changes

* removed register interface

* review change

* Fix tests

* WIP add test for tx sign

* removed commented code

* Fix flags

* WIP add test for sign

* Address review suggestions

* fixed command issue

* Add tests for TxEncoder

* Revert sign changes

* Fix TxEncoder tests

* Fix GetSign Cmd

* Add tx test

* Remove duplicate validation

* Add tests for TxDecoder

* Fix tests

* Fix tests

* Output to clientCtx.Output

* Fix cli_tests

Co-authored-by: atheeshp <[email protected]>
Co-authored-by: atheesh <[email protected]>
Co-authored-by: anilCSE <[email protected]>
Co-authored-by: sahith-narahari <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
6 people authored Jun 18, 2020
1 parent 653353f commit d6d10d0
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 30 deletions.
15 changes: 5 additions & 10 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/testdata"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/std"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -130,7 +129,7 @@ var _ App = (*SimApp)(nil)
type SimApp struct {
*baseapp.BaseApp
cdc *codec.Codec
appCodec *std.Codec
appCodec codec.Marshaler

invCheckPeriod uint

Expand Down Expand Up @@ -401,13 +400,9 @@ func NewSimApp(
// MakeCodecs constructs the *std.Codec and *codec.Codec instances used by
// simapp. It is useful for tests and clients who do not want to construct the
// full simapp
func MakeCodecs() (*std.Codec, *codec.Codec) {
cdc := std.MakeCodec(ModuleBasics)
interfaceRegistry := types.NewInterfaceRegistry()
std.RegisterInterfaces(interfaceRegistry)
ModuleBasics.RegisterInterfaceModules(interfaceRegistry)
appCodec := std.NewAppCodec(cdc, interfaceRegistry)
return appCodec, cdc
func MakeCodecs() (codec.Marshaler, *codec.Codec) {
config := MakeEncodingConfig()
return config.Marshaler, config.Amino
}

// Name returns the name of the App
Expand Down Expand Up @@ -468,7 +463,7 @@ func (app *SimApp) Codec() *codec.Codec {
//
// NOTE: This is solely to be used for testing purposes as it may be desirable
// for modules to register their own custom testing types.
func (app *SimApp) AppCodec() *std.Codec {
func (app *SimApp) AppCodec() codec.Marshaler {
return app.appCodec
}

Expand Down
38 changes: 21 additions & 17 deletions cmd/simcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"path"

simappparams "github.com/cosmos/cosmos-sdk/simapp/params"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/cli"
Expand All @@ -13,7 +15,6 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
Expand All @@ -23,11 +24,11 @@ import (
)

var (
appCodec, cdc = simapp.MakeCodecs()
encodingConfig = simapp.MakeEncodingConfig()
)

func init() {
authclient.Codec = appCodec
authclient.Codec = encodingConfig.Marshaler
}

func main() {
Expand Down Expand Up @@ -60,8 +61,8 @@ func main() {
rootCmd.AddCommand(
rpc.StatusCommand(),
client.ConfigCmd(simapp.DefaultCLIHome),
queryCmd(cdc),
txCmd(cdc),
queryCmd(encodingConfig),
txCmd(encodingConfig),
flags.LineBreak,
flags.LineBreak,
keys.Commands(),
Expand All @@ -79,7 +80,7 @@ func main() {
}
}

func queryCmd(cdc *codec.Codec) *cobra.Command {
func queryCmd(config simappparams.EncodingConfig) *cobra.Command {
queryCmd := &cobra.Command{
Use: "query",
Aliases: []string{"q"},
Expand All @@ -89,6 +90,8 @@ func queryCmd(cdc *codec.Codec) *cobra.Command {
RunE: client.ValidateCmd,
}

cdc := config.Amino

queryCmd.AddCommand(
authcmd.GetAccountCmd(cdc),
flags.LineBreak,
Expand All @@ -102,14 +105,14 @@ func queryCmd(cdc *codec.Codec) *cobra.Command {
// add modules' query commands
clientCtx := client.Context{}
clientCtx = clientCtx.
WithJSONMarshaler(appCodec).
WithJSONMarshaler(config.Marshaler).
WithCodec(cdc)
simapp.ModuleBasics.AddQueryCommands(queryCmd, clientCtx)

return queryCmd
}

func txCmd(cdc *codec.Codec) *cobra.Command {
func txCmd(config simappparams.EncodingConfig) *cobra.Command {
txCmd := &cobra.Command{
Use: "tx",
Short: "Transactions subcommands",
Expand All @@ -118,24 +121,25 @@ func txCmd(cdc *codec.Codec) *cobra.Command {
RunE: client.ValidateCmd,
}

cdc := config.Amino
clientCtx := client.Context{}
clientCtx = clientCtx.
WithJSONMarshaler(appCodec).
WithTxGenerator(types.StdTxGenerator{Cdc: cdc}).
WithAccountRetriever(types.NewAccountRetriever(appCodec)).
WithJSONMarshaler(config.Marshaler).
WithTxGenerator(config.TxGenerator).
WithAccountRetriever(types.NewAccountRetriever(config.Marshaler)).
WithCodec(cdc)

txCmd.AddCommand(
bankcmd.NewSendTxCmd(clientCtx),
flags.LineBreak,
authcmd.GetSignCommand(cdc),
authcmd.GetSignCommand(clientCtx),
authcmd.GetSignBatchCommand(cdc),
authcmd.GetMultiSignCommand(cdc),
authcmd.GetValidateSignaturesCommand(cdc),
authcmd.GetMultiSignCommand(clientCtx),
authcmd.GetValidateSignaturesCommand(clientCtx),
flags.LineBreak,
authcmd.GetBroadcastCommand(cdc),
authcmd.GetEncodeCommand(cdc),
authcmd.GetDecodeCommand(cdc),
authcmd.GetBroadcastCommand(clientCtx),
authcmd.GetEncodeCommand(clientCtx),
authcmd.GetDecodeCommand(clientCtx),
flags.LineBreak,
)

Expand Down
4 changes: 1 addition & 3 deletions cmd/simd/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (

"github.com/cosmos/cosmos-sdk/codec"

"github.com/cosmos/cosmos-sdk/std"

"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand All @@ -34,7 +32,7 @@ const (

// AddGenesisAccountCmd returns add-genesis-account cobra Command.
func AddGenesisAccountCmd(
ctx *server.Context, depCdc codec.JSONMarshaler, cdc *std.Codec, defaultNodeHome, defaultClientHome string,
ctx *server.Context, depCdc codec.JSONMarshaler, cdc codec.Marshaler, defaultNodeHome, defaultClientHome string,
) *cobra.Command {

cmd := &cobra.Command{
Expand Down
18 changes: 18 additions & 0 deletions encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package simapp

import (
"github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/std"
)

// MakeEncodingConfig creates an EncodingConfig for an amino based test configuration.
//
// TODO: this file should add a "+build test_amino" flag for #6190 and a proto.go file with a protobuf configuration
func MakeEncodingConfig() params.EncodingConfig {
encodingConfig := params.MakeEncodingConfig()
std.RegisterCodec(encodingConfig.Amino)
std.RegisterInterfaces(encodingConfig.InterfaceRegistry)
ModuleBasics.RegisterCodec(encodingConfig.Amino)
ModuleBasics.RegisterInterfaceModules(encodingConfig.InterfaceRegistry)
return encodingConfig
}
23 changes: 23 additions & 0 deletions params/amino.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package params

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

// MakeEncodingConfig creates an EncodingConfig for an amino based test configuration.
//
// TODO: this file should add a "+build test_amino" flag for #6190 and a proto.go file with a protobuf configuration
func MakeEncodingConfig() EncodingConfig {
cdc := codec.New()
interfaceRegistry := types.NewInterfaceRegistry()
marshaler := codec.NewHybridCodec(cdc, interfaceRegistry)

return EncodingConfig{
InterfaceRegistry: interfaceRegistry,
Marshaler: marshaler,
TxGenerator: authtypes.StdTxGenerator{Cdc: cdc},
Amino: cdc,
}
}
16 changes: 16 additions & 0 deletions params/encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package params

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
)

// EncodingConfig specifies the concrete encoding types to use for a given app.
// This is provided for compatibility between protobuf and amino implementations.
type EncodingConfig struct {
InterfaceRegistry types.InterfaceRegistry
Marshaler codec.Marshaler
TxGenerator client.TxGenerator
Amino *codec.Codec
}

0 comments on commit d6d10d0

Please sign in to comment.