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

Migrate x/auth cmd's to TxGenerator marshaling #6391

Merged
merged 38 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
92c9a77
Migrate encode, decode, and broadcast cmd's to use TxGenerator marsha…
aaronc Jun 10, 2020
fed32e2
Fix tests, add EncodingConfig
aaronc Jun 10, 2020
826785e
Add simapp/encoding.go and wire up with simcli
aaronc Jun 10, 2020
91cf429
add godocs
aaronc Jun 10, 2020
eebd0f8
Merge branch 'master' into aaronc/6213-cli-encode
aaronc Jun 10, 2020
1e265f8
fix tests
aaronc Jun 10, 2020
1467cd4
Debugging CLI Tests
aaronc Jun 10, 2020
de78d03
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into aa…
aaronc Jun 11, 2020
23cdeeb
Fix integration test
aaronc Jun 11, 2020
b878734
6391 - lint issues and code coverage (#6414)
atheeshp Jun 15, 2020
7b1fc09
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into aa…
aaronc Jun 15, 2020
e4db342
Fix tests
aaronc Jun 15, 2020
50b56e2
WIP add test for tx sign
atheeshp Jun 16, 2020
796352b
removed commented code
atheeshp Jun 16, 2020
ec3bdfa
Fix flags
anilcse Jun 16, 2020
709b09f
WIP add test for sign
atheeshp Jun 17, 2020
b5e52ff
fixed conflicts
atheeshp Jun 17, 2020
7745688
Address review suggestions
sahith-narahari Jun 17, 2020
1701624
fixed command issue
atheeshp Jun 18, 2020
3496a34
fixed conflicts
atheeshp Jun 18, 2020
5063d7c
Add tests for TxEncoder
sahith-narahari Jun 18, 2020
893e265
Revert sign changes
sahith-narahari Jun 18, 2020
32bbf12
Fix TxEncoder tests
sahith-narahari Jun 18, 2020
a51b36e
Fix GetSign Cmd
sahith-narahari Jun 18, 2020
c33987d
Add tx test
sahith-narahari Jun 18, 2020
766f808
Remove duplicate validation
sahith-narahari Jun 18, 2020
e0cb1d4
Add tests for TxDecoder
sahith-narahari Jun 18, 2020
2621815
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into aa…
aaronc Jun 18, 2020
78daa5e
Fix tests
aaronc Jun 18, 2020
c1d5bf3
Fix tests
aaronc Jun 18, 2020
3bbd081
Merge branch 'master' into aaronc/6213-cli-encode
aaronc Jun 18, 2020
4c5ae0d
Merge branch 'master' into aaronc/6213-cli-encode
mergify[bot] Jun 18, 2020
6cf5831
Output to clientCtx.Output
aaronc Jun 18, 2020
81dc00b
Merge remote-tracking branch 'origin/aaronc/6213-cli-encode' into aar…
aaronc Jun 18, 2020
9fd638a
Merge branch 'master' into aaronc/6213-cli-encode
aaronc Jun 18, 2020
23642fb
Merge branch 'master' into aaronc/6213-cli-encode
mergify[bot] Jun 18, 2020
2f5ac90
Fix cli_tests
sahith-narahari Jun 18, 2020
f578b59
Merge branch 'aaronc/6213-cli-encode' of github.com:cosmos/cosmos-sdk…
sahith-narahari Jun 18, 2020
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
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),
aaronc marked this conversation as resolved.
Show resolved Hide resolved
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 {
aaronc marked this conversation as resolved.
Show resolved Hide resolved
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 {
aaronc marked this conversation as resolved.
Show resolved Hide resolved
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