Skip to content

Commit

Permalink
Merge PR #4451: Client and Module Modularization
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelrozanski authored and alexanderbez committed Jun 5, 2019
1 parent d80a980 commit 5f9c3fd
Show file tree
Hide file tree
Showing 184 changed files with 3,107 additions and 2,395 deletions.
5 changes: 5 additions & 0 deletions .pending/breaking/sdk/4451-Improve-modular
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#4451 Improve modularization of clients and modules:
* Module directory structure improved and standardized
* Aliases autogenerated
* Auth and bank related commands are now mounted under the respective moduels
* Client initialization and mounting standardized
22 changes: 10 additions & 12 deletions client/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@ import (
"path/filepath"

"github.com/pkg/errors"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
cryptokeys "github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/x/auth"

"github.com/spf13/viper"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"

"github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/libs/log"
tmlite "github.com/tendermint/tendermint/lite"
tmliteProxy "github.com/tendermint/tendermint/lite/proxy"
rpcclient "github.com/tendermint/tendermint/rpc/client"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
cryptokeys "github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

var (
Expand All @@ -36,7 +34,7 @@ var (
// transaction handling and queries.
type CLIContext struct {
Codec *codec.Codec
AccDecoder auth.AccountDecoder
AccDecoder authtypes.AccountDecoder
Client rpcclient.Client
Keybase cryptokeys.Keybase
Output io.Writer
Expand Down Expand Up @@ -90,7 +88,7 @@ func NewCLIContextWithFrom(from string) CLIContext {
Client: rpc,
Output: os.Stdout,
NodeURI: nodeURI,
AccountStore: auth.StoreKey,
AccountStore: authtypes.StoreKey,
From: viper.GetString(flags.FlagFrom),
OutputFormat: viper.GetString(cli.OutputFlag),
Height: viper.GetInt64(flags.FlagHeight),
Expand Down Expand Up @@ -165,8 +163,8 @@ func (ctx CLIContext) WithCodec(cdc *codec.Codec) CLIContext {
}

// GetAccountDecoder gets the account decoder for auth.DefaultAccount.
func GetAccountDecoder(cdc *codec.Codec) auth.AccountDecoder {
return func(accBytes []byte) (acct auth.Account, err error) {
func GetAccountDecoder(cdc *codec.Codec) authtypes.AccountDecoder {
return func(accBytes []byte) (acct authtypes.Account, err error) {
err = cdc.UnmarshalBinaryBare(accBytes, &acct)
if err != nil {
panic(err)
Expand Down
10 changes: 5 additions & 5 deletions client/context/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

"github.com/cosmos/cosmos-sdk/store/rootmulti"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

// GetNode returns an RPC client. If the context's client is not defined, an
Expand Down Expand Up @@ -59,7 +59,7 @@ func (ctx CLIContext) QuerySubspace(subspace []byte, storeName string) (res []sd

// GetAccount queries for an account given an address and a block height. An
// error is returned if the query or decoding fails.
func (ctx CLIContext) GetAccount(address []byte) (auth.Account, error) {
func (ctx CLIContext) GetAccount(address []byte) (authtypes.Account, error) {
if ctx.AccDecoder == nil {
return nil, errors.New("account decoder required but not provided")
}
Expand All @@ -69,7 +69,7 @@ func (ctx CLIContext) GetAccount(address []byte) (auth.Account, error) {
return nil, err
}

var account auth.Account
var account authtypes.Account
if err := ctx.Codec.UnmarshalJSON(res, &account); err != nil {
return nil, err
}
Expand Down Expand Up @@ -127,12 +127,12 @@ func (ctx CLIContext) EnsureAccountExistsFromAddr(addr sdk.AccAddress) error {
// queryAccount queries an account using custom query endpoint of auth module
// returns an error if result is `null` otherwise account data
func (ctx CLIContext) queryAccount(addr sdk.AccAddress) ([]byte, error) {
bz, err := ctx.Codec.MarshalJSON(auth.NewQueryAccountParams(addr))
bz, err := ctx.Codec.MarshalJSON(authtypes.NewQueryAccountParams(addr))
if err != nil {
return nil, err
}

route := fmt.Sprintf("custom/%s/%s", ctx.AccountStore, auth.QueryAccount)
route := fmt.Sprintf("custom/%s/%s", ctx.AccountStore, authtypes.QueryAccount)

res, err := ctx.QueryWithData(route, bz)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions client/keys/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var cdc *codec.Codec
func init() {
cdc = codec.New()
codec.RegisterCrypto(cdc)
cdc.Seal()
}

// marshal keys
Expand Down
3 changes: 1 addition & 2 deletions client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
)

//-----------------------------------------------------------------------------
Expand All @@ -32,7 +31,7 @@ func WriteGenerateStdTxResponse(w http.ResponseWriter, cdc *codec.Codec,
return
}

txBldr := authtxb.NewTxBuilder(
txBldr := auth.NewTxBuilder(
utils.GetTxEncoder(cdc), br.AccountNumber, br.Sequence, gas, gasAdj,
br.Simulate, br.ChainID, br.Memo, br.Fees, br.GasPrices,
)
Expand Down
14 changes: 14 additions & 0 deletions client/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package client

import (
"github.com/gorilla/mux"

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

// Register routes
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) {
RegisterRPCRoutes(cliCtx, r)
RegisterTxRoutes(cliCtx, r, cdc)
}
5 changes: 2 additions & 3 deletions client/tx/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"

"github.com/spf13/cobra"
amino "github.com/tendermint/go-amino"

"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth"
Expand Down Expand Up @@ -62,7 +61,7 @@ func BroadcastTxRequest(cliCtx context.CLIContext, cdc *codec.Codec) http.Handle
}

// GetBroadcastCommand returns the tx broadcast command.
func GetBroadcastCommand(codec *amino.Codec) *cobra.Command {
func GetBroadcastCommand(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "broadcast [file_path]",
Short: "Broadcast transactions generated offline",
Expand All @@ -75,7 +74,7 @@ $ <appcli> tx broadcast ./mytxn.json
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
cliCtx := context.NewCLIContext().WithCodec(codec)
cliCtx := context.NewCLIContext().WithCodec(cdc)
stdTx, err := utils.ReadStdTxFromFile(cliCtx.Codec, args[0])
if err != nil {
return
Expand Down
5 changes: 2 additions & 3 deletions client/tx/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http"

"github.com/spf13/cobra"
amino "github.com/tendermint/go-amino"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -69,7 +68,7 @@ func (txr txEncodeRespStr) String() string {

// GetEncodeCommand returns the encode command to take a JSONified transaction and turn it into
// Amino-serialized bytes
func GetEncodeCommand(codec *amino.Codec) *cobra.Command {
func GetEncodeCommand(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "encode [file]",
Short: "Encode transactions generated offline",
Expand All @@ -78,7 +77,7 @@ Read a transaction from <file>, serialize it to the Amino wire protocol, and out
If you supply a dash (-) argument in place of an input filename, the command reads from standard input.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
cliCtx := context.NewCLIContext().WithCodec(codec)
cliCtx := context.NewCLIContext().WithCodec(cdc)

stdTx, err := utils.ReadStdTxFromFile(cliCtx.Codec, args[0])
if err != nil {
Expand Down
41 changes: 20 additions & 21 deletions client/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

// GasEstimateResponse defines a response definition for tx gas estimation.
Expand All @@ -35,7 +34,7 @@ func (gr GasEstimateResponse) String() string {
// the provided context has generate-only enabled, the tx will only be printed
// to STDOUT in a fully offline manner. Otherwise, the tx will be signed and
// broadcasted.
func GenerateOrBroadcastMsgs(cliCtx context.CLIContext, txBldr authtxb.TxBuilder, msgs []sdk.Msg) error {
func GenerateOrBroadcastMsgs(cliCtx context.CLIContext, txBldr authtypes.TxBuilder, msgs []sdk.Msg) error {
if cliCtx.GenerateOnly {
return PrintUnsignedStdTx(txBldr, cliCtx, msgs)
}
Expand All @@ -48,7 +47,7 @@ func GenerateOrBroadcastMsgs(cliCtx context.CLIContext, txBldr authtxb.TxBuilder
// QueryContext. It ensures that the account exists, has a proper number and
// sequence set. In addition, it builds and signs a transaction with the
// supplied messages. Finally, it broadcasts the signed transaction to a node.
func CompleteAndBroadcastTxCLI(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) error {
func CompleteAndBroadcastTxCLI(txBldr authtypes.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) error {
txBldr, err := PrepareTxBuilder(txBldr, cliCtx)
if err != nil {
return err
Expand Down Expand Up @@ -118,7 +117,7 @@ func CompleteAndBroadcastTxCLI(txBldr authtxb.TxBuilder, cliCtx context.CLIConte

// EnrichWithGas calculates the gas estimate that would be consumed by the
// transaction and set the transaction's respective value accordingly.
func EnrichWithGas(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) (authtxb.TxBuilder, error) {
func EnrichWithGas(txBldr authtypes.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) (authtypes.TxBuilder, error) {
_, adjusted, err := simulateMsgs(txBldr, cliCtx, msgs)
if err != nil {
return txBldr, err
Expand Down Expand Up @@ -146,7 +145,7 @@ func CalculateGas(queryFunc func(string, common.HexBytes) ([]byte, error),
}

// PrintUnsignedStdTx builds an unsigned StdTx and prints it to os.Stdout.
func PrintUnsignedStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) error {
func PrintUnsignedStdTx(txBldr authtypes.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) error {
stdTx, err := buildUnsignedStdTxOffline(txBldr, cliCtx, msgs)
if err != nil {
return err
Expand All @@ -165,11 +164,11 @@ func PrintUnsignedStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msg
// is false, it replaces the signatures already attached with the new signature.
// Don't perform online validation or lookups if offline is true.
func SignStdTx(
txBldr authtxb.TxBuilder, cliCtx context.CLIContext, name string,
stdTx auth.StdTx, appendSig bool, offline bool,
) (auth.StdTx, error) {
txBldr authtypes.TxBuilder, cliCtx context.CLIContext, name string,
stdTx authtypes.StdTx, appendSig bool, offline bool,
) (authtypes.StdTx, error) {

var signedStdTx auth.StdTx
var signedStdTx authtypes.StdTx

info, err := txBldr.Keybase().Get(name)
if err != nil {
Expand Down Expand Up @@ -201,9 +200,9 @@ func SignStdTx(
// SignStdTxWithSignerAddress attaches a signature to a StdTx and returns a copy of a it.
// Don't perform online validation or lookups if offline is true, else
// populate account and sequence numbers from a foreign account.
func SignStdTxWithSignerAddress(txBldr authtxb.TxBuilder, cliCtx context.CLIContext,
addr sdk.AccAddress, name string, stdTx auth.StdTx,
offline bool) (signedStdTx auth.StdTx, err error) {
func SignStdTxWithSignerAddress(txBldr authtypes.TxBuilder, cliCtx context.CLIContext,
addr sdk.AccAddress, name string, stdTx authtypes.StdTx,
offline bool) (signedStdTx authtypes.StdTx, err error) {

// check whether the address is a signer
if !isTxSigner(addr, stdTx.GetSigners()) {
Expand All @@ -226,7 +225,7 @@ func SignStdTxWithSignerAddress(txBldr authtxb.TxBuilder, cliCtx context.CLICont
}

// Read and decode a StdTx from the given filename. Can pass "-" to read from stdin.
func ReadStdTxFromFile(cdc *codec.Codec, filename string) (stdTx auth.StdTx, err error) {
func ReadStdTxFromFile(cdc *codec.Codec, filename string) (stdTx authtypes.StdTx, err error) {
var bytes []byte
if filename == "-" {
bytes, err = ioutil.ReadAll(os.Stdin)
Expand All @@ -243,8 +242,8 @@ func ReadStdTxFromFile(cdc *codec.Codec, filename string) (stdTx auth.StdTx, err
}

func populateAccountFromState(
txBldr authtxb.TxBuilder, cliCtx context.CLIContext, addr sdk.AccAddress,
) (authtxb.TxBuilder, error) {
txBldr authtypes.TxBuilder, cliCtx context.CLIContext, addr sdk.AccAddress,
) (authtypes.TxBuilder, error) {

accNum, err := cliCtx.GetAccountNumber(addr)
if err != nil {
Expand All @@ -264,14 +263,14 @@ func populateAccountFromState(
func GetTxEncoder(cdc *codec.Codec) (encoder sdk.TxEncoder) {
encoder = sdk.GetConfig().GetTxEncoder()
if encoder == nil {
encoder = auth.DefaultTxEncoder(cdc)
encoder = authtypes.DefaultTxEncoder(cdc)
}
return
}

// nolint
// SimulateMsgs simulates the transaction and returns the gas estimate and the adjusted value.
func simulateMsgs(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) (estimated, adjusted uint64, err error) {
func simulateMsgs(txBldr authtypes.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) (estimated, adjusted uint64, err error) {
txBytes, err := txBldr.BuildTxForSim(msgs)
if err != nil {
return
Expand All @@ -293,7 +292,7 @@ func parseQueryResponse(cdc *codec.Codec, rawRes []byte) (uint64, error) {
}

// PrepareTxBuilder populates a TxBuilder in preparation for the build of a Tx.
func PrepareTxBuilder(txBldr authtxb.TxBuilder, cliCtx context.CLIContext) (authtxb.TxBuilder, error) {
func PrepareTxBuilder(txBldr authtypes.TxBuilder, cliCtx context.CLIContext) (authtypes.TxBuilder, error) {
if err := cliCtx.EnsureAccountExists(); err != nil {
return txBldr, err
}
Expand Down Expand Up @@ -322,7 +321,7 @@ func PrepareTxBuilder(txBldr authtxb.TxBuilder, cliCtx context.CLIContext) (auth
return txBldr, nil
}

func buildUnsignedStdTxOffline(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) (stdTx auth.StdTx, err error) {
func buildUnsignedStdTxOffline(txBldr authtypes.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) (stdTx authtypes.StdTx, err error) {
if txBldr.SimulateAndExecute() {
if cliCtx.GenerateOnly {
return stdTx, errors.New("cannot estimate gas with generate-only")
Expand All @@ -341,7 +340,7 @@ func buildUnsignedStdTxOffline(txBldr authtxb.TxBuilder, cliCtx context.CLIConte
return stdTx, nil
}

return auth.NewStdTx(stdSignMsg.Msgs, stdSignMsg.Fee, nil, stdSignMsg.Memo), nil
return authtypes.NewStdTx(stdSignMsg.Msgs, stdSignMsg.Fee, nil, stdSignMsg.Memo), nil
}

func isTxSigner(user sdk.AccAddress, signers []sdk.AccAddress) bool {
Expand Down
12 changes: 6 additions & 6 deletions client/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

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

var (
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestCalculateGas(t *testing.T) {
func TestDefaultTxEncoder(t *testing.T) {
cdc := makeCodec()

defaultEncoder := auth.DefaultTxEncoder(cdc)
defaultEncoder := authtypes.DefaultTxEncoder(cdc)
encoder := GetTxEncoder(cdc)

compareEncoders(t, defaultEncoder, encoder)
Expand All @@ -99,8 +99,8 @@ func TestReadStdTxFromFile(t *testing.T) {
sdk.RegisterCodec(cdc)

// Build a test transaction
fee := auth.NewStdFee(50000, sdk.Coins{sdk.NewInt64Coin("atom", 150)})
stdTx := auth.NewStdTx([]sdk.Msg{}, fee, []auth.StdSignature{}, "foomemo")
fee := authtypes.NewStdFee(50000, sdk.Coins{sdk.NewInt64Coin("atom", 150)})
stdTx := authtypes.NewStdTx([]sdk.Msg{}, fee, []authtypes.StdSignature{}, "foomemo")

// Write it to the file
encodedTx, _ := cdc.MarshalJSON(stdTx)
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestValidateCmd(t *testing.T) {

func compareEncoders(t *testing.T, expected sdk.TxEncoder, actual sdk.TxEncoder) {
msgs := []sdk.Msg{sdk.NewTestMsg(addr)}
tx := auth.NewStdTx(msgs, auth.StdFee{}, []auth.StdSignature{}, "")
tx := authtypes.NewStdTx(msgs, authtypes.StdFee{}, []authtypes.StdSignature{}, "")

defaultEncoderBytes, err := expected(tx)
require.NoError(t, err)
Expand All @@ -181,7 +181,7 @@ func makeCodec() *codec.Codec {
var cdc = codec.New()
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
auth.RegisterCodec(cdc)
authtypes.RegisterCodec(cdc)
cdc.RegisterConcrete(sdk.TestMsg{}, "cosmos-sdk/Test", nil)
return cdc
}
Loading

0 comments on commit 5f9c3fd

Please sign in to comment.