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 5040ff8 commit 712d237
Show file tree
Hide file tree
Showing 32 changed files with 440 additions and 179 deletions.
36 changes: 36 additions & 0 deletions client/test_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package client

import (
"fmt"

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

// TestAccountRetriever is an AccountRetriever that can be used in unit tests
type TestAccountRetriever struct {
Accounts map[string]struct {
Address sdk.AccAddress
Num uint64
Seq uint64
}
}

var _ AccountRetriever = TestAccountRetriever{}

// EnsureExists implements AccountRetriever.EnsureExists
func (t TestAccountRetriever) EnsureExists(_ NodeQuerier, addr sdk.AccAddress) error {
_, ok := t.Accounts[addr.String()]
if !ok {
return fmt.Errorf("account %s not found", addr)
}
return nil
}

// GetAccountNumberSequence implements AccountRetriever.GetAccountNumberSequence
func (t TestAccountRetriever) GetAccountNumberSequence(_ NodeQuerier, addr sdk.AccAddress) (accNum uint64, accSeq uint64, err error) {
acc, ok := t.Accounts[addr.String()]
if !ok {
return 0, 0, fmt.Errorf("account %s not found", addr)
}
return acc.Num, acc.Seq, nil
}
4 changes: 2 additions & 2 deletions client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func BroadcastTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
return err
}

txBytes, err := clientCtx.TxGenerator.MarshalTx(tx.GetTx())
txBytes, err := clientCtx.TxGenerator.TxEncoder()(tx.GetTx())
if err != nil {
return err
}
Expand Down Expand Up @@ -244,7 +244,7 @@ func BuildSimTx(txf Factory, msgs ...sdk.Msg) ([]byte, error) {
return nil, err
}

return txf.txGenerator.MarshalTx(tx.GetTx())
return txf.txGenerator.TxEncoder()(tx.GetTx())
}

// CalculateGas simulates the execution of a transaction and returns the
Expand Down
6 changes: 5 additions & 1 deletion client/tx_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ type (
TxGenerator interface {
NewTxBuilder() TxBuilder
SignModeHandler() signing.SignModeHandler
MarshalTx(tx sdk.Tx) ([]byte, error)

TxEncoder() sdk.TxEncoder
TxDecoder() sdk.TxDecoder
TxJSONEncoder() sdk.TxEncoder
TxJSONDecoder() sdk.TxDecoder
}

// TxBuilder defines an interface which an application-defined concrete transaction
Expand Down
3 changes: 2 additions & 1 deletion codec/testdata/test_helper.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package testdata

import (
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/tendermint/go-amino"

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

func NewTestInterfaceRegistry() types.InterfaceRegistry {
Expand Down
15 changes: 5 additions & 10 deletions simapp/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 simapp/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 simapp/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 simapp/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 simapp/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 simapp/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
}
24 changes: 6 additions & 18 deletions std/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
)

// Codec defines the application-level codec. This codec contains all the
// required module-specific codecs that are to be provided upon initialization.
type Codec struct {
codec.Marshaler

// Keep reference to the amino codec to allow backwards compatibility along
// with type, and interface registration.
amino *codec.Codec

anyUnpacker types.AnyUnpacker
}

func NewAppCodec(amino *codec.Codec, anyUnpacker types.AnyUnpacker) *Codec {
return &Codec{Marshaler: codec.NewHybridCodec(amino, anyUnpacker), amino: amino, anyUnpacker: anyUnpacker}
}

// ----------------------------------------------------------------------------
// necessary types and interfaces registered. This codec is provided to all the
// modules the application depends on.
Expand All @@ -35,11 +19,15 @@ func MakeCodec(bm module.BasicManager) *codec.Codec {
cdc := codec.New()

bm.RegisterCodec(cdc)
RegisterCodec(cdc)

return cdc
}

func RegisterCodec(cdc *codec.Codec) {
vesting.RegisterCodec(cdc)
sdk.RegisterCodec(cdc)
cryptocodec.RegisterCrypto(cdc)

return cdc
}

// RegisterInterfaces registers Interfaces from sdk/types and vesting
Expand Down
Loading

0 comments on commit 712d237

Please sign in to comment.