From 4604676d0ad75522e3951e0f1bfdc9dcb07ac12f Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Mon, 6 May 2019 23:28:13 -0400 Subject: [PATCH 1/2] working further abstractions for client --- client/config.go | 24 ++++++++++ client/setup.go | 19 ++++++++ cmd/gaia/cmd/gaiacli/main.go | 65 +++++----------------------- types/module.go | 42 +++++++++++++++--- x/auth/genesis.go | 42 ------------------ x/auth/keeper.go | 23 ---------- x/auth/module.go | 9 ++++ x/auth/{ => types}/account.go | 2 +- x/auth/{ => types}/account_test.go | 2 +- x/auth/{ => types}/codec.go | 2 +- x/auth/{ => types}/feekeeper.go | 2 +- x/auth/{ => types}/feekeeper_test.go | 2 +- x/auth/types/genesis.go | 47 ++++++++++++++++++++ x/auth/types/keys.go | 26 +++++++++++ x/auth/{ => types}/stdtx.go | 2 +- x/auth/{ => types}/stdtx_test.go | 2 +- x/bank/client/cli/sendtx.go | 4 +- x/bank/client/rest/sendtx.go | 10 +---- x/bank/module.go | 8 ++++ x/bank/{ => types}/codec.go | 2 +- x/bank/{ => types}/errors.go | 2 +- x/bank/{ => types}/msgs.go | 2 +- x/bank/{ => types}/msgs_test.go | 2 +- x/slashing/client/cli/query.go | 2 +- 24 files changed, 197 insertions(+), 146 deletions(-) create mode 100644 client/setup.go rename x/auth/{ => types}/account.go (99%) rename x/auth/{ => types}/account_test.go (99%) rename x/auth/{ => types}/codec.go (98%) rename x/auth/{ => types}/feekeeper.go (99%) rename x/auth/{ => types}/feekeeper_test.go (99%) create mode 100644 x/auth/types/genesis.go create mode 100644 x/auth/types/keys.go rename x/auth/{ => types}/stdtx.go (99%) rename x/auth/{ => types}/stdtx_test.go (99%) rename x/bank/{ => types}/codec.go (96%) rename x/bank/{ => types}/errors.go (98%) rename x/bank/{ => types}/msgs.go (99%) rename x/bank/{ => types}/msgs_test.go (99%) diff --git a/client/config.go b/client/config.go index 4946a96ea8ab..d14d8f40c09e 100644 --- a/client/config.go +++ b/client/config.go @@ -166,3 +166,27 @@ func saveConfigFile(cfgFile string, tree *toml.Tree) error { func errUnknownConfigKey(key string) error { return fmt.Errorf("unknown configuration key: %q", key) } + +// initialize the config command flags +func InitConfig(cmd *cobra.Command) error { + home, err := cmd.PersistentFlags().GetString(cli.HomeFlag) + if err != nil { + return err + } + + cfgFile := path.Join(home, "config", "config.toml") + if _, err := os.Stat(cfgFile); err == nil { + viper.SetConfigFile(cfgFile) + + if err := viper.ReadInConfig(); err != nil { + return err + } + } + if err := viper.BindPFlag(FlagChainID, cmd.PersistentFlags().Lookup(FlagChainID)); err != nil { + return err + } + if err := viper.BindPFlag(cli.EncodingFlag, cmd.PersistentFlags().Lookup(cli.EncodingFlag)); err != nil { + return err + } + return viper.BindPFlag(cli.OutputFlag, cmd.PersistentFlags().Lookup(cli.OutputFlag)) +} diff --git a/client/setup.go b/client/setup.go new file mode 100644 index 000000000000..06703e9059e7 --- /dev/null +++ b/client/setup.go @@ -0,0 +1,19 @@ +package client + +import ( + "github.com/spf13/cobra" + "github.com/tendermint/tendermint/libs/cli" +) + +// PrepareMainCmd is meant for client side libs that want some more flags +func PrepareMainCmd(cmd *cobra.Command, envPrefix, defaultHome string) Executor { + + // Add --chain-id to persistent flags and mark it required + rootCmd.PersistentFlags().String(FlagChainID, "", "Chain ID of tendermint node") + rootCmd.PersistentPreRunE = func(_ *cobra.Command, _ []string) error { + return InitConfig(rootCmd) + } + + // add tendermint setup + return cli.PrepareMainCmd(cmd, envPrefix, defaultHome) +} diff --git a/cmd/gaia/cmd/gaiacli/main.go b/cmd/gaia/cmd/gaiacli/main.go index b9a5543f4f57..9aa0eedccd40 100644 --- a/cmd/gaia/cmd/gaiacli/main.go +++ b/cmd/gaia/cmd/gaiacli/main.go @@ -4,7 +4,6 @@ import ( "fmt" "net/http" "os" - "path" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/keys" @@ -40,10 +39,8 @@ import ( "github.com/rakyll/statik/fs" "github.com/spf13/cobra" - "github.com/spf13/viper" amino "github.com/tendermint/go-amino" - "github.com/tendermint/tendermint/libs/cli" _ "github.com/cosmos/cosmos-sdk/client/lcd/statik" ) @@ -66,6 +63,7 @@ func main() { // the below functions and eliminate global vars, like we do // with the cdc + // XXX abstract this // Module clients hold cli commnads (tx,query) and lcd routes // TODO: Make the lcd command take a list of ModuleClient mc := []sdk.ModuleClient{ @@ -82,18 +80,12 @@ func main() { Short: "Command line interface for interacting with gaiad", } - // Add --chain-id to persistent flags and mark it required - rootCmd.PersistentFlags().String(client.FlagChainID, "", "Chain ID of tendermint node") - rootCmd.PersistentPreRunE = func(_ *cobra.Command, _ []string) error { - return initConfig(rootCmd) - } - // Construct Root Command rootCmd.AddCommand( rpc.StatusCommand(), client.ConfigCmd(app.DefaultCLIHome), - queryCmd(cdc, mc), - txCmd(cdc, mc), + queryCmd(cdc, app.BasicGaiaApp), + txCmd(cdc, app.BasicGaiaApp), client.LineBreak, lcd.ServeCommand(cdc, registerRoutes), client.LineBreak, @@ -104,7 +96,7 @@ func main() { ) // Add flags and prefix all env exposed with GA - executor := cli.PrepareMainCmd(rootCmd, "GA", app.DefaultCLIHome) + executor := client.PrepareMainCmd(rootCmd, "GA", app.DefaultCLIHome) err := executor.Execute() if err != nil { @@ -113,13 +105,12 @@ func main() { } } -func queryCmd(cdc *amino.Codec, mc []sdk.ModuleClient) *cobra.Command { +func queryCmd(cdc *amino.Codec, mbm sdk.ModuleBasicManager) *cobra.Command { queryCmd := &cobra.Command{ Use: "query", Aliases: []string{"q"}, Short: "Querying subcommands", } - queryCmd.AddCommand( rpc.ValidatorCommand(cdc), rpc.BlockCommand(), @@ -128,23 +119,15 @@ func queryCmd(cdc *amino.Codec, mc []sdk.ModuleClient) *cobra.Command { client.LineBreak, authcmd.GetAccountCmd(at.StoreKey, cdc), ) - - for _, m := range mc { - mQueryCmd := m.GetQueryCmd() - if mQueryCmd != nil { - queryCmd.AddCommand(mQueryCmd) - } - } - + mbm.AddQueryCommands(txCmd) return queryCmd } -func txCmd(cdc *amino.Codec, mc []sdk.ModuleClient) *cobra.Command { +func txCmd(cdc *amino.Codec, mbm sdk.ModuleBasicManager) *cobra.Command { txCmd := &cobra.Command{ Use: "tx", Short: "Transactions subcommands", } - txCmd.AddCommand( bankcmd.SendTxCmd(cdc), client.LineBreak, @@ -154,21 +137,20 @@ func txCmd(cdc *amino.Codec, mc []sdk.ModuleClient) *cobra.Command { tx.GetEncodeCommand(cdc), client.LineBreak, ) - - for _, m := range mc { - txCmd.AddCommand(m.GetTxCmd()) - } - + mbm.AddTxCommands(txCmd) return txCmd } // registerRoutes registers the routes from the different modules for the LCD. // NOTE: details on the routes added for each module are in the module documentation // NOTE: If making updates here you also need to update the test helper in client/lcd/test_helper.go -func registerRoutes(rs *lcd.RestServer) { +func registerRoutes(rs *lcd.RestServer, mbm sdk.ModuleBasicManager) { registerSwaggerUI(rs) rpc.RegisterRoutes(rs.CliCtx, rs.Mux) tx.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc) + mbm.RegisterRESTRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase) + + // XXX Abstract this auth.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, at.StoreKey) bank.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase) dist.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, distcmd.StoreKey) @@ -186,26 +168,3 @@ func registerSwaggerUI(rs *lcd.RestServer) { staticServer := http.FileServer(statikFS) rs.Mux.PathPrefix("/swagger-ui/").Handler(http.StripPrefix("/swagger-ui/", staticServer)) } - -func initConfig(cmd *cobra.Command) error { - home, err := cmd.PersistentFlags().GetString(cli.HomeFlag) - if err != nil { - return err - } - - cfgFile := path.Join(home, "config", "config.toml") - if _, err := os.Stat(cfgFile); err == nil { - viper.SetConfigFile(cfgFile) - - if err := viper.ReadInConfig(); err != nil { - return err - } - } - if err := viper.BindPFlag(client.FlagChainID, cmd.PersistentFlags().Lookup(client.FlagChainID)); err != nil { - return err - } - if err := viper.BindPFlag(cli.EncodingFlag, cmd.PersistentFlags().Lookup(cli.EncodingFlag)); err != nil { - return err - } - return viper.BindPFlag(cli.OutputFlag, cmd.PersistentFlags().Lookup(cli.OutputFlag)) -} diff --git a/types/module.go b/types/module.go index 1f09a6c52505..3baa18e9cb26 100644 --- a/types/module.go +++ b/types/module.go @@ -1,26 +1,29 @@ package types import ( + "context" "encoding/json" "github.com/cosmos/cosmos-sdk/codec" "github.com/spf13/cobra" + "github.com/tendermint/go-crypto/keys" abci "github.com/tendermint/tendermint/abci/types" ) -// ModuleClient helps modules provide a standard interface for exporting client functionality -type ModuleClient interface { - GetQueryCmd() *cobra.Command - GetTxCmd() *cobra.Command -} - //__________________________________________________________________________________________ // AppModule is the standard form for basic non-dependant elements of an application module type AppModuleBasic interface { Name() string RegisterCodec(*codec.Codec) + + // genesis DefaultGenesis() json.RawMessage ValidateGenesis(json.RawMessage) error + + // client functionality + RegisterRESTRoutes(context.CLIContext, *mux.Router, *codec.Codec, keys.Keybase) + GetQueryCmd() *cobra.Command + GetTxCmd() *cobra.Command } // collections of AppModuleBasic @@ -56,6 +59,33 @@ func (mbm ModuleBasicManager) ValidateGenesis(genesis map[string]json.RawMessage return nil } +// RegisterRestRoutes registers all module rest routes +func (mbm ModuleBasicManager) RegisterRESTRoutes( + ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec, kb keys.Keybase) { + + for _, mb := range mbm { + mb.RegisterRESTRoutes(ctx, rtr, cdc, kb) + } +} + +// add all tx commands to the rootTxCmd +func (mbm ModuleBasicManager) AddTxCommands(rootTxCmd *cobra.Command) { + for _, mb := range mbm { + if cmd := mb.GetTxCmd(); cmd != nil { + rootTxCmd.AddCommand(cmd) + } + } +} + +// add all query commands to the rootQueryCmd +func (mbm ModuleBasicManager) AddQueryCommands(rootQueryCmd *cobra.Command) { + for _, mb := range mbm { + if cmd := mb.GetQueryCmd(); cmd != nil { + rootQueryCmd.AddCommand(cmd) + } + } +} + //_________________________________________________________ // AppModule is the standard form for an application module type AppModule interface { diff --git a/x/auth/genesis.go b/x/auth/genesis.go index 885234f61c71..90e2a3a32bf6 100644 --- a/x/auth/genesis.go +++ b/x/auth/genesis.go @@ -1,30 +1,9 @@ package auth import ( - "fmt" - sdk "github.com/cosmos/cosmos-sdk/types" ) -// GenesisState - all auth state that must be provided at genesis -type GenesisState struct { - CollectedFees sdk.Coins `json:"collected_fees"` - Params Params `json:"params"` -} - -// NewGenesisState - Create a new genesis state -func NewGenesisState(collectedFees sdk.Coins, params Params) GenesisState { - return GenesisState{ - CollectedFees: collectedFees, - Params: params, - } -} - -// DefaultGenesisState - Return a default genesis state -func DefaultGenesisState() GenesisState { - return NewGenesisState(sdk.NewCoins(), DefaultParams()) -} - // InitGenesis - Init store state from genesis data func InitGenesis(ctx sdk.Context, ak AccountKeeper, fck FeeCollectionKeeper, data GenesisState) { ak.SetParams(ctx, data.Params) @@ -38,24 +17,3 @@ func ExportGenesis(ctx sdk.Context, ak AccountKeeper, fck FeeCollectionKeeper) G return NewGenesisState(collectedFees, params) } - -// ValidateGenesis performs basic validation of auth genesis data returning an -// error for any failed validation criteria. -func ValidateGenesis(data GenesisState) error { - if data.Params.TxSigLimit == 0 { - return fmt.Errorf("invalid tx signature limit: %d", data.Params.TxSigLimit) - } - if data.Params.SigVerifyCostED25519 == 0 { - return fmt.Errorf("invalid ED25519 signature verification cost: %d", data.Params.SigVerifyCostED25519) - } - if data.Params.SigVerifyCostSecp256k1 == 0 { - return fmt.Errorf("invalid SECK256k1 signature verification cost: %d", data.Params.SigVerifyCostSecp256k1) - } - if data.Params.MaxMemoCharacters == 0 { - return fmt.Errorf("invalid max memo characters: %d", data.Params.MaxMemoCharacters) - } - if data.Params.TxSizeCostPerByte == 0 { - return fmt.Errorf("invalid tx size cost per byte: %d", data.Params.TxSizeCostPerByte) - } - return nil -} diff --git a/x/auth/keeper.go b/x/auth/keeper.go index f630381be1e6..105945994f5e 100644 --- a/x/auth/keeper.go +++ b/x/auth/keeper.go @@ -10,24 +10,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/params/subspace" ) -const ( - // StoreKey is string representation of the store key for auth - StoreKey = "acc" - - // FeeStoreKey is a string representation of the store key for fees - FeeStoreKey = "fee" - - // QuerierRoute is the querier route for acc - QuerierRoute = StoreKey -) - -var ( - // AddressStoreKeyPrefix prefix for account-by-address store - AddressStoreKeyPrefix = []byte{0x01} - - globalAccountNumberKey = []byte("globalAccountNumber") -) - // AccountKeeper encodes/decodes accounts using the go-amino (binary) // encoding/decoding library. type AccountKeeper struct { @@ -82,11 +64,6 @@ func (ak AccountKeeper) NewAccount(ctx sdk.Context, acc Account) Account { return acc } -// AddressStoreKey turn an address to key used to get it from the account store -func AddressStoreKey(addr sdk.AccAddress) []byte { - return append(AddressStoreKeyPrefix, addr.Bytes()...) -} - // GetAccount implements sdk.AccountKeeper. func (ak AccountKeeper) GetAccount(ctx sdk.Context, addr sdk.AccAddress) Account { store := ctx.KVStore(ak.key) diff --git a/x/auth/module.go b/x/auth/module.go index f7381e21f335..757735fbb527 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -3,8 +3,11 @@ package auth import ( "encoding/json" + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/gov/client/rest" + "github.com/tendermint/go-crypto/keys" abci "github.com/tendermint/tendermint/abci/types" ) @@ -44,6 +47,12 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } +// XXX +// register rest routes +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec, kb keys.Keybase) { + rest.RegisterRoutes(ctx, rtr, cdc, StoreKey) +} + //___________________________ // app module object type AppModule struct { diff --git a/x/auth/account.go b/x/auth/types/account.go similarity index 99% rename from x/auth/account.go rename to x/auth/types/account.go index 99132d3ba26a..1924f079ccac 100644 --- a/x/auth/account.go +++ b/x/auth/types/account.go @@ -1,4 +1,4 @@ -package auth +package types import ( "errors" diff --git a/x/auth/account_test.go b/x/auth/types/account_test.go similarity index 99% rename from x/auth/account_test.go rename to x/auth/types/account_test.go index d8f7c6fa8ddd..726ae08c96ee 100644 --- a/x/auth/account_test.go +++ b/x/auth/types/account_test.go @@ -1,4 +1,4 @@ -package auth +package types import ( "testing" diff --git a/x/auth/codec.go b/x/auth/types/codec.go similarity index 98% rename from x/auth/codec.go rename to x/auth/types/codec.go index 9b7b35204347..aa67699d25cb 100644 --- a/x/auth/codec.go +++ b/x/auth/types/codec.go @@ -1,4 +1,4 @@ -package auth +package types import ( "github.com/cosmos/cosmos-sdk/codec" diff --git a/x/auth/feekeeper.go b/x/auth/types/feekeeper.go similarity index 99% rename from x/auth/feekeeper.go rename to x/auth/types/feekeeper.go index bc6506b1764a..c0b37a150105 100644 --- a/x/auth/feekeeper.go +++ b/x/auth/types/feekeeper.go @@ -1,4 +1,4 @@ -package auth +package types import ( codec "github.com/cosmos/cosmos-sdk/codec" diff --git a/x/auth/feekeeper_test.go b/x/auth/types/feekeeper_test.go similarity index 99% rename from x/auth/feekeeper_test.go rename to x/auth/types/feekeeper_test.go index ab49305f1c73..5126aa446dd2 100644 --- a/x/auth/feekeeper_test.go +++ b/x/auth/types/feekeeper_test.go @@ -1,4 +1,4 @@ -package auth +package types import ( "testing" diff --git a/x/auth/types/genesis.go b/x/auth/types/genesis.go new file mode 100644 index 000000000000..d7239380463e --- /dev/null +++ b/x/auth/types/genesis.go @@ -0,0 +1,47 @@ +package types + +import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// GenesisState - all auth state that must be provided at genesis +type GenesisState struct { + CollectedFees sdk.Coins `json:"collected_fees"` + Params Params `json:"params"` +} + +// NewGenesisState - Create a new genesis state +func NewGenesisState(collectedFees sdk.Coins, params Params) GenesisState { + return GenesisState{ + CollectedFees: collectedFees, + Params: params, + } +} + +// DefaultGenesisState - Return a default genesis state +func DefaultGenesisState() GenesisState { + return NewGenesisState(sdk.NewCoins(), DefaultParams()) +} + +// ValidateGenesis performs basic validation of auth genesis data returning an +// error for any failed validation criteria. +func ValidateGenesis(data GenesisState) error { + if data.Params.TxSigLimit == 0 { + return fmt.Errorf("invalid tx signature limit: %d", data.Params.TxSigLimit) + } + if data.Params.SigVerifyCostED25519 == 0 { + return fmt.Errorf("invalid ED25519 signature verification cost: %d", data.Params.SigVerifyCostED25519) + } + if data.Params.SigVerifyCostSecp256k1 == 0 { + return fmt.Errorf("invalid SECK256k1 signature verification cost: %d", data.Params.SigVerifyCostSecp256k1) + } + if data.Params.MaxMemoCharacters == 0 { + return fmt.Errorf("invalid max memo characters: %d", data.Params.MaxMemoCharacters) + } + if data.Params.TxSizeCostPerByte == 0 { + return fmt.Errorf("invalid tx size cost per byte: %d", data.Params.TxSizeCostPerByte) + } + return nil +} diff --git a/x/auth/types/keys.go b/x/auth/types/keys.go new file mode 100644 index 000000000000..674e16910042 --- /dev/null +++ b/x/auth/types/keys.go @@ -0,0 +1,26 @@ +package types + +import sdk "github.com/cosmos/cosmos-sdk/types" + +const ( + // StoreKey is string representation of the store key for auth + StoreKey = "acc" + + // FeeStoreKey is a string representation of the store key for fees + FeeStoreKey = "fee" + + // QuerierRoute is the querier route for acc + QuerierRoute = StoreKey +) + +var ( + // AddressStoreKeyPrefix prefix for account-by-address store + AddressStoreKeyPrefix = []byte{0x01} + + globalAccountNumberKey = []byte("globalAccountNumber") +) + +// AddressStoreKey turn an address to key used to get it from the account store +func AddressStoreKey(addr sdk.AccAddress) []byte { + return append(AddressStoreKeyPrefix, addr.Bytes()...) +} diff --git a/x/auth/stdtx.go b/x/auth/types/stdtx.go similarity index 99% rename from x/auth/stdtx.go rename to x/auth/types/stdtx.go index 20efaae424f2..3527df0c1c83 100644 --- a/x/auth/stdtx.go +++ b/x/auth/types/stdtx.go @@ -1,4 +1,4 @@ -package auth +package types import ( "encoding/json" diff --git a/x/auth/stdtx_test.go b/x/auth/types/stdtx_test.go similarity index 99% rename from x/auth/stdtx_test.go rename to x/auth/types/stdtx_test.go index a1fbf4a34ba4..bf1bd6e41d2e 100644 --- a/x/auth/stdtx_test.go +++ b/x/auth/types/stdtx_test.go @@ -1,4 +1,4 @@ -package auth +package types import ( "fmt" diff --git a/x/bank/client/cli/sendtx.go b/x/bank/client/cli/sendtx.go index 900189d9f2d4..c90b5dd539aa 100644 --- a/x/bank/client/cli/sendtx.go +++ b/x/bank/client/cli/sendtx.go @@ -7,7 +7,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" - "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/spf13/cobra" ) @@ -41,7 +41,7 @@ func SendTxCmd(cdc *codec.Codec) *cobra.Command { } // build and sign the transaction, then broadcast to Tendermint - msg := bank.NewMsgSend(cliCtx.GetFromAddress(), to, coins) + msg := types.NewMsgSend(cliCtx.GetFromAddress(), to, coins) return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false) }, } diff --git a/x/bank/client/rest/sendtx.go b/x/bank/client/rest/sendtx.go index 3e634e551f61..bb27bd8c1746 100644 --- a/x/bank/client/rest/sendtx.go +++ b/x/bank/client/rest/sendtx.go @@ -12,7 +12,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/rest" - "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/bank/types" ) // RegisterRoutes - Central function to define routes that get registered by the main application @@ -26,12 +26,6 @@ type SendReq struct { Amount sdk.Coins `json:"amount"` } -var moduleCdc = codec.New() - -func init() { - bank.RegisterCodec(moduleCdc) -} - // SendRequestHandlerFn - http request handler to send coins to a address. func SendRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { @@ -60,7 +54,7 @@ func SendRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIC return } - msg := bank.NewMsgSend(fromAddr, toAddr, req.Amount) + msg := types.NewMsgSend(fromAddr, toAddr, req.Amount) clientrest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg}) } } diff --git a/x/bank/module.go b/x/bank/module.go index 53be045fcdb2..cbd388a8c118 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -3,9 +3,12 @@ package bank import ( "encoding/json" + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/gov/client/rest" + "github.com/tendermint/go-crypto/keys" abci "github.com/tendermint/tendermint/abci/types" ) @@ -47,6 +50,11 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } +// register rest routes +func RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec, kb keys.Keybase) { + rest.RegisterRoutes(ctx, rtr, cdc, StoreKey) +} + //___________________________ // app module type AppModule struct { diff --git a/x/bank/codec.go b/x/bank/types/codec.go similarity index 96% rename from x/bank/codec.go rename to x/bank/types/codec.go index 2c2cc32a3387..2d169d08fe66 100644 --- a/x/bank/codec.go +++ b/x/bank/types/codec.go @@ -1,4 +1,4 @@ -package bank +package types import ( "github.com/cosmos/cosmos-sdk/codec" diff --git a/x/bank/errors.go b/x/bank/types/errors.go similarity index 98% rename from x/bank/errors.go rename to x/bank/types/errors.go index 5b579fabb28d..d9c0c7d11d1d 100644 --- a/x/bank/errors.go +++ b/x/bank/types/errors.go @@ -1,4 +1,4 @@ -package bank +package types import ( sdk "github.com/cosmos/cosmos-sdk/types" diff --git a/x/bank/msgs.go b/x/bank/types/msgs.go similarity index 99% rename from x/bank/msgs.go rename to x/bank/types/msgs.go index 2953488673f2..805873fb1443 100644 --- a/x/bank/msgs.go +++ b/x/bank/types/msgs.go @@ -1,4 +1,4 @@ -package bank +package types import ( sdk "github.com/cosmos/cosmos-sdk/types" diff --git a/x/bank/msgs_test.go b/x/bank/types/msgs_test.go similarity index 99% rename from x/bank/msgs_test.go rename to x/bank/types/msgs_test.go index 3b7813e0d508..2a09c1b9f728 100644 --- a/x/bank/msgs_test.go +++ b/x/bank/types/msgs_test.go @@ -1,4 +1,4 @@ -package bank +package types import ( "fmt" diff --git a/x/slashing/client/cli/query.go b/x/slashing/client/cli/query.go index 0396e8ab91ba..6c14104f7d03 100644 --- a/x/slashing/client/cli/query.go +++ b/x/slashing/client/cli/query.go @@ -7,7 +7,7 @@ import ( "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client/context" - "github.com/cosmos/cosmos-sdk/codec" // XXX fix + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/slashing" ) From f6f3f0064fdda6acdfda97cd8952de21899d0147 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 7 May 2019 20:04:39 -0400 Subject: [PATCH 2/2] restructuring to allow for client generalization --- client/context/context.go | 12 ++--- client/context/query.go | 5 +- cmd/gaia/app/app.go | 2 +- cmd/gaia/cmd/gaiacli/main.go | 6 +-- types/module.go | 9 ++-- x/auth/client/rest/query.go | 12 ++--- x/auth/module.go | 17 +++++-- x/bank/client/rest/sendtx.go | 7 ++- x/bank/module.go | 17 +++++-- x/crisis/client/cli/tx.go | 14 ++++++ x/crisis/client/module_client.go | 42 ---------------- x/crisis/module.go | 19 ++++++- x/distribution/client/cli/query.go | 21 ++++++++ x/distribution/client/cli/tx.go | 3 +- x/distribution/client/module_client.go | 54 -------------------- x/distribution/module.go | 24 ++++++++- x/genaccounts/module.go | 15 +++++- x/genutil/module.go | 15 +++++- x/gov/client/cli/query.go | 24 +++++++++ x/gov/client/cli/tx.go | 26 ++++++++++ x/gov/client/module_client.go | 69 -------------------------- x/gov/module.go | 33 +++++++++++- x/mint/client/cli/query.go | 18 +++++++ x/mint/client/module_client.go | 47 ------------------ x/mint/module.go | 22 +++++++- x/params/client/cli/tx.go | 13 ++--- x/params/module.go | 15 ++++++ x/params/types/codec.go | 7 +-- x/params/types/proposal.go | 1 + x/slashing/client/cli/query.go | 20 ++++++++ x/slashing/client/cli/tx.go | 15 ++++++ x/slashing/client/module_client.go | 53 -------------------- x/slashing/client/rest/rest.go | 5 +- x/slashing/client/rest/tx.go | 7 ++- x/slashing/module.go | 22 +++++++- x/staking/client/cli/query.go | 26 ++++++++++ x/staking/client/cli/tx.go | 19 +++++++ x/staking/client/module_client.go | 63 ----------------------- x/staking/client/rest/rest.go | 5 +- x/staking/client/rest/tx.go | 15 +++--- x/staking/module.go | 22 +++++++- 41 files changed, 441 insertions(+), 400 deletions(-) delete mode 100644 x/crisis/client/module_client.go delete mode 100644 x/distribution/client/module_client.go delete mode 100644 x/gov/client/module_client.go delete mode 100644 x/mint/client/module_client.go delete mode 100644 x/slashing/client/module_client.go delete mode 100644 x/staking/client/module_client.go diff --git a/client/context/context.go b/client/context/context.go index 0a42ff4afe26..d1387583e1ba 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -13,7 +13,7 @@ import ( "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" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/spf13/viper" @@ -35,7 +35,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 @@ -86,7 +86,7 @@ func NewCLIContextWithFrom(from string) CLIContext { Client: rpc, Output: os.Stdout, NodeURI: nodeURI, - AccountStore: auth.StoreKey, + AccountStore: authtypes.StoreKey, From: viper.GetString(client.FlagFrom), OutputFormat: viper.GetString(cli.OutputFlag), Height: viper.GetInt64(client.FlagHeight), @@ -160,9 +160,9 @@ func (ctx CLIContext) WithCodec(cdc *codec.Codec) CLIContext { return ctx } -// GetAccountDecoder gets the account decoder for auth.DefaultAccount. -func GetAccountDecoder(cdc *codec.Codec) auth.AccountDecoder { - return func(accBytes []byte) (acct auth.Account, err error) { +// GetAccountDecoder gets the account decoder for authtypes.DefaultAccount. +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) diff --git a/client/context/query.go b/client/context/query.go index 7f625e801ec9..332c2f21997e 100644 --- a/client/context/query.go +++ b/client/context/query.go @@ -5,6 +5,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/pkg/errors" @@ -61,7 +62,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") } @@ -71,7 +72,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 } diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index d08c383da9ab..d5e4c2bda167 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -50,7 +50,7 @@ func init() { staking.AppModuleBasic{}, mint.AppModuleBasic{}, distr.AppModuleBasic{}, - gov.AppModuleBasic{}, + gov.NewAppModuleBasic(paramcli.GetCmdSubmitProposal()), params.AppModuleBasic{}, crisis.AppModuleBasic{}, slashing.AppModuleBasic{}, diff --git a/cmd/gaia/cmd/gaiacli/main.go b/cmd/gaia/cmd/gaiacli/main.go index 9aa0eedccd40..a3e6c3ef69de 100644 --- a/cmd/gaia/cmd/gaiacli/main.go +++ b/cmd/gaia/cmd/gaiacli/main.go @@ -152,10 +152,10 @@ func registerRoutes(rs *lcd.RestServer, mbm sdk.ModuleBasicManager) { // XXX Abstract this auth.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, at.StoreKey) - bank.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase) + bank.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc) dist.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, distcmd.StoreKey) - staking.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase) - slashing.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase) + staking.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc) + slashing.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc) gov.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, paramsrest.ProposalRESTHandler(rs.CliCtx, rs.Cdc)) mintrest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc) } diff --git a/types/module.go b/types/module.go index 3baa18e9cb26..50b50887b849 100644 --- a/types/module.go +++ b/types/module.go @@ -6,7 +6,6 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/spf13/cobra" - "github.com/tendermint/go-crypto/keys" abci "github.com/tendermint/tendermint/abci/types" ) @@ -21,9 +20,9 @@ type AppModuleBasic interface { ValidateGenesis(json.RawMessage) error // client functionality - RegisterRESTRoutes(context.CLIContext, *mux.Router, *codec.Codec, keys.Keybase) - GetQueryCmd() *cobra.Command + RegisterRESTRoutes(context.CLIContext, *mux.Router, *codec.Codec) GetTxCmd() *cobra.Command + GetQueryCmd() *cobra.Command } // collections of AppModuleBasic @@ -61,10 +60,10 @@ func (mbm ModuleBasicManager) ValidateGenesis(genesis map[string]json.RawMessage // RegisterRestRoutes registers all module rest routes func (mbm ModuleBasicManager) RegisterRESTRoutes( - ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec, kb keys.Keybase) { + ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec) { for _, mb := range mbm { - mb.RegisterRESTRoutes(ctx, rtr, cdc, kb) + mb.RegisterRESTRoutes(ctx, rtr, cdc) } } diff --git a/x/auth/client/rest/query.go b/x/auth/client/rest/query.go index 9df3c14bc055..348fab865abf 100644 --- a/x/auth/client/rest/query.go +++ b/x/auth/client/rest/query.go @@ -10,7 +10,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/rest" - "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/auth/types" ) // register REST routes @@ -29,7 +29,7 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec, // query accountREST Handler func QueryAccountRequestHandlerFn( storeName string, cdc *codec.Codec, - decoder auth.AccountDecoder, cliCtx context.CLIContext, + decoder types.AccountDecoder, cliCtx context.CLIContext, ) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) @@ -41,7 +41,7 @@ func QueryAccountRequestHandlerFn( return } - res, err := cliCtx.QueryStore(auth.AddressStoreKey(addr), storeName) + res, err := cliCtx.QueryStore(types.AddressStoreKey(addr), storeName) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return @@ -49,7 +49,7 @@ func QueryAccountRequestHandlerFn( // the query will return empty account if there is no data if len(res) == 0 { - rest.PostProcessResponse(w, cdc, auth.BaseAccount{}, cliCtx.Indent) + rest.PostProcessResponse(w, cdc, types.BaseAccount{}, cliCtx.Indent) return } @@ -67,7 +67,7 @@ func QueryAccountRequestHandlerFn( // query accountREST Handler func QueryBalancesRequestHandlerFn( storeName string, cdc *codec.Codec, - decoder auth.AccountDecoder, cliCtx context.CLIContext, + decoder types.AccountDecoder, cliCtx context.CLIContext, ) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") @@ -80,7 +80,7 @@ func QueryBalancesRequestHandlerFn( return } - res, err := cliCtx.QueryStore(auth.AddressStoreKey(addr), storeName) + res, err := cliCtx.QueryStore(types.AddressStoreKey(addr), storeName) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return diff --git a/x/auth/module.go b/x/auth/module.go index 757735fbb527..7ddb5b744502 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -3,12 +3,13 @@ package auth import ( "encoding/json" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/gov/client/rest" - "github.com/tendermint/go-crypto/keys" - abci "github.com/tendermint/tendermint/abci/types" + "github.com/cosmos/cosmos-sdk/x/auth/client/rest" ) var ( @@ -49,10 +50,18 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { // XXX // register rest routes -func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec, kb keys.Keybase) { +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec) { rest.RegisterRoutes(ctx, rtr, cdc, StoreKey) } +// TODO +// get the root tx command of this module +func (AppModuleBasic) GetTxCmd() *cobra.Command { return nil } + +// TODO +// get the root query command of this module +func (AppModuleBasic) GetQueryCmd() *cobra.Command { return nil } + //___________________________ // app module object type AppModule struct { diff --git a/x/bank/client/rest/sendtx.go b/x/bank/client/rest/sendtx.go index bb27bd8c1746..afc7bb2e3005 100644 --- a/x/bank/client/rest/sendtx.go +++ b/x/bank/client/rest/sendtx.go @@ -8,7 +8,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" clientrest "github.com/cosmos/cosmos-sdk/client/rest" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/crypto/keys" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/rest" @@ -16,8 +15,8 @@ import ( ) // RegisterRoutes - Central function to define routes that get registered by the main application -func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec, kb keys.Keybase) { - r.HandleFunc("/bank/accounts/{address}/transfers", SendRequestHandlerFn(cdc, kb, cliCtx)).Methods("POST") +func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) { + r.HandleFunc("/bank/accounts/{address}/transfers", SendRequestHandlerFn(cdc, cliCtx)).Methods("POST") } // SendReq defines the properties of a send request's body. @@ -27,7 +26,7 @@ type SendReq struct { } // SendRequestHandlerFn - http request handler to send coins to a address. -func SendRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIContext) http.HandlerFunc { +func SendRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) bech32Addr := vars["address"] diff --git a/x/bank/module.go b/x/bank/module.go index cbd388a8c118..553e6ad6f79b 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -3,13 +3,14 @@ package bank import ( "encoding/json" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/cosmos/cosmos-sdk/x/gov/client/rest" - "github.com/tendermint/go-crypto/keys" - abci "github.com/tendermint/tendermint/abci/types" + "github.com/cosmos/cosmos-sdk/x/bank/client/rest" ) var ( @@ -51,10 +52,18 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { } // register rest routes -func RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec, kb keys.Keybase) { +func RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec) { rest.RegisterRoutes(ctx, rtr, cdc, StoreKey) } +// TODO +// get the root tx command of this module +func (AppModuleBasic) GetTxCmd() *cobra.Command { return nil } + +// TODO +// get the root query command of this module +func (AppModuleBasic) GetQueryCmd() *cobra.Command { return nil } + //___________________________ // app module type AppModule struct { diff --git a/x/crisis/client/cli/tx.go b/x/crisis/client/cli/tx.go index d44759ea4509..f58b81f4c7f0 100644 --- a/x/crisis/client/cli/tx.go +++ b/x/crisis/client/cli/tx.go @@ -4,6 +4,7 @@ package cli import ( "github.com/spf13/cobra" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/utils" "github.com/cosmos/cosmos-sdk/codec" @@ -12,6 +13,19 @@ import ( "github.com/cosmos/cosmos-sdk/x/crisis" ) +// GetTxCmd returns the transaction commands for this module +func GetTxCmd(cdc *amino.Codec) *cobra.Command { + txCmd := &cobra.Command{ + Use: crisis.ModuleName, + Short: "crisis transactions subcommands", + } + + txCmd.AddCommand(client.PostCommands( + GetCmdInvariantBroken(cdc), + )...) + return txCmd +} + // command to replace a delegator's withdrawal address func GetCmdInvariantBroken(cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ diff --git a/x/crisis/client/module_client.go b/x/crisis/client/module_client.go deleted file mode 100644 index 12c9d2282b49..000000000000 --- a/x/crisis/client/module_client.go +++ /dev/null @@ -1,42 +0,0 @@ -package client - -import ( - "github.com/spf13/cobra" - amino "github.com/tendermint/go-amino" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/x/crisis" - "github.com/cosmos/cosmos-sdk/x/crisis/client/cli" -) - -// ModuleClient exports all client functionality from this module -type ModuleClient struct { - storeKey string - cdc *amino.Codec -} - -// NewModuleClient creates a new ModuleClient object -func NewModuleClient(storeKey string, cdc *amino.Codec) ModuleClient { - return ModuleClient{ - storeKey: storeKey, - cdc: cdc, - } -} - -// GetQueryCmd returns the cli query commands for this module -func (ModuleClient) GetQueryCmd() *cobra.Command { - return nil -} - -// GetTxCmd returns the transaction commands for this module -func (mc ModuleClient) GetTxCmd() *cobra.Command { - txCmd := &cobra.Command{ - Use: crisis.ModuleName, - Short: "crisis transactions subcommands", - } - - txCmd.AddCommand(client.PostCommands( - cli.GetCmdInvariantBroken(mc.cdc), - )...) - return txCmd -} diff --git a/x/crisis/module.go b/x/crisis/module.go index 7bb64f10714c..0cc5d983747b 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -3,11 +3,14 @@ package crisis import ( "encoding/json" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - abci "github.com/tendermint/tendermint/abci/types" + "github.com/cosmos/cosmos-sdk/x/gov/client/cli" ) var ( @@ -48,6 +51,20 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } +// register rest routes +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec) { +} + +// get the root tx command of this module +func (AppModuleBasic) GetTxCmd() *cobra.Command { + return cli.GetTxCmd(moduleCdc) +} + +// get the root query command of this module +func (AppModuleBasic) GetQueryCmd() *cobra.Command { + return nil +} + //___________________________ // app module for bank type AppModule struct { diff --git a/x/distribution/client/cli/query.go b/x/distribution/client/cli/query.go index bc5777ac07a3..be9b1574648c 100644 --- a/x/distribution/client/cli/query.go +++ b/x/distribution/client/cli/query.go @@ -6,7 +6,9 @@ import ( "strings" "github.com/spf13/cobra" + amino "github.com/tendermint/go-amino" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -15,6 +17,25 @@ import ( "github.com/cosmos/cosmos-sdk/x/distribution/types" ) +// GetQueryCmd returns the cli query commands for this module +func GetQueryCmd(storeKey string, cdc *amino.Codec) *cobra.Command { + distQueryCmd := &cobra.Command{ + Use: "distr", + Short: "Querying commands for the distribution module", + } + + distQueryCmd.AddCommand(client.GetCommands( + GetCmdQueryParams(storeKey, cdc), + GetCmdQueryValidatorOutstandingRewards(storeKey, cdc), + GetCmdQueryValidatorCommission(storeKey, cdc), + GetCmdQueryValidatorSlashes(storeKey, cdc), + GetCmdQueryDelegatorRewards(storeKey, cdc), + GetCmdQueryCommunityPool(storeKey, cdc), + )...) + + return distQueryCmd +} + // GetCmdQueryParams implements the query params command. func GetCmdQueryParams(queryRoute string, cdc *codec.Codec) *cobra.Command { return &cobra.Command{ diff --git a/x/distribution/client/cli/tx.go b/x/distribution/client/cli/tx.go index 8bf143ce953f..42e56893a936 100644 --- a/x/distribution/client/cli/tx.go +++ b/x/distribution/client/cli/tx.go @@ -28,13 +28,14 @@ var ( // GetTxCmd returns the transaction commands for this module func GetTxCmd(storeKey string, cdc *amino.Codec) *cobra.Command { distTxCmd := &cobra.Command{ - Use: "dist", + Use: "distr", Short: "Distribution transactions subcommands", } distTxCmd.AddCommand(client.PostCommands( GetCmdWithdrawRewards(cdc), GetCmdSetWithdrawAddr(cdc), + GetCmdWithdrawAllRewards(cdc, storeKey), )...) return distTxCmd diff --git a/x/distribution/client/module_client.go b/x/distribution/client/module_client.go deleted file mode 100644 index 780b0cabd8f5..000000000000 --- a/x/distribution/client/module_client.go +++ /dev/null @@ -1,54 +0,0 @@ -package client - -import ( - "github.com/spf13/cobra" - amino "github.com/tendermint/go-amino" - - "github.com/cosmos/cosmos-sdk/client" - distCmds "github.com/cosmos/cosmos-sdk/x/distribution/client/cli" -) - -// ModuleClient exports all client functionality from this module -type ModuleClient struct { - storeKey string - cdc *amino.Codec -} - -func NewModuleClient(storeKey string, cdc *amino.Codec) ModuleClient { - return ModuleClient{storeKey, cdc} -} - -// GetQueryCmd returns the cli query commands for this module -func (mc ModuleClient) GetQueryCmd() *cobra.Command { - distQueryCmd := &cobra.Command{ - Use: "distr", - Short: "Querying commands for the distribution module", - } - - distQueryCmd.AddCommand(client.GetCommands( - distCmds.GetCmdQueryParams(mc.storeKey, mc.cdc), - distCmds.GetCmdQueryValidatorOutstandingRewards(mc.storeKey, mc.cdc), - distCmds.GetCmdQueryValidatorCommission(mc.storeKey, mc.cdc), - distCmds.GetCmdQueryValidatorSlashes(mc.storeKey, mc.cdc), - distCmds.GetCmdQueryDelegatorRewards(mc.storeKey, mc.cdc), - distCmds.GetCmdQueryCommunityPool(mc.storeKey, mc.cdc), - )...) - - return distQueryCmd -} - -// GetTxCmd returns the transaction commands for this module -func (mc ModuleClient) GetTxCmd() *cobra.Command { - distTxCmd := &cobra.Command{ - Use: "distr", - Short: "Distribution transactions subcommands", - } - - distTxCmd.AddCommand(client.PostCommands( - distCmds.GetCmdWithdrawRewards(mc.cdc), - distCmds.GetCmdSetWithdrawAddr(mc.cdc), - distCmds.GetCmdWithdrawAllRewards(mc.cdc, mc.storeKey), - )...) - - return distTxCmd -} diff --git a/x/distribution/module.go b/x/distribution/module.go index 03e7a5df1aa3..160e26cabb16 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -3,9 +3,15 @@ package distribution import ( "encoding/json" + "github.com/spf13/cobra" + + abci "github.com/tendermint/tendermint/abci/types" + + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - abci "github.com/tendermint/tendermint/abci/types" + "github.com/cosmos/cosmos-sdk/x/distribution/client/rest" + "github.com/cosmos/cosmos-sdk/x/gov/client/cli" ) var ( @@ -41,6 +47,22 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } +// register rest routes +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec) { + rest.RegisterRoutes(ctx, rtr, cdc, StoreKey) +} + +// get the root tx command of this module +func (AppModuleBasic) GetTxCmd() *cobra.Command { + return cli.GetTxCmd(StoreKey, moduleCdc) +} + +// get the root query command of this module +func (AppModuleBasic) GetQueryCmd() *cobra.Command { + return cli.GetQueryCmd(StoreKey, moduleCdc) +} + +//___________________________ // app module type AppModule struct { AppModuleBasic diff --git a/x/genaccounts/module.go b/x/genaccounts/module.go index a59620eae66a..95acdf773df1 100644 --- a/x/genaccounts/module.go +++ b/x/genaccounts/module.go @@ -3,10 +3,13 @@ package genaccounts import ( "encoding/json" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" + + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" - abci "github.com/tendermint/tendermint/abci/types" ) var ( @@ -43,6 +46,16 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } +// register rest routes +func (AppModuleBasic) RegisterRESTRoutes(_ context.CLIContext, _ *mux.Router, _ *codec.Codec) { +} + +// get the root tx command of this module +func (AppModuleBasic) GetTxCmd() *cobra.Command { return nil } + +// get the root query command of this module +func (AppModuleBasic) GetQueryCmd() *cobra.Command { return nil } + // extra function from sdk.AppModuleBasic // iterate the genesis accounts and perform an operation at each of them // - to used by other modules diff --git a/x/genutil/module.go b/x/genutil/module.go index 028395ba5982..8174285a447c 100644 --- a/x/genutil/module.go +++ b/x/genutil/module.go @@ -3,9 +3,12 @@ package genutil import ( "encoding/json" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" + + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - abci "github.com/tendermint/tendermint/abci/types" ) var ( @@ -42,6 +45,16 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } +// register rest routes +func (AppModuleBasic) RegisterRESTRoutes(_ context.CLIContext, _ *mux.Router, _ *codec.Codec) { +} + +// get the root tx command of this module +func (AppModuleBasic) GetTxCmd() *cobra.Command { return nil } + +// get the root query command of this module +func (AppModuleBasic) GetQueryCmd() *cobra.Command { return nil } + //___________________________ // app module type AppModule struct { diff --git a/x/gov/client/cli/query.go b/x/gov/client/cli/query.go index 9aa52383dc46..251bc9f3058d 100644 --- a/x/gov/client/cli/query.go +++ b/x/gov/client/cli/query.go @@ -8,6 +8,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -15,6 +16,29 @@ import ( gcutils "github.com/cosmos/cosmos-sdk/x/gov/client/utils" ) +// GetQueryCmd returns the cli query commands for this module +func GetQueryCmd(storeKey string, cdc *amino.Codec) *cobra.Command { + // Group gov queries under a subcommand + govQueryCmd := &cobra.Command{ + Use: gov.ModuleName, + Short: "Querying commands for the governance module", + } + + govQueryCmd.AddCommand(client.GetCommands( + GetCmdQueryProposal(storeKey, cdc), + GetCmdQueryProposals(storeKey, cdc), + GetCmdQueryVote(storeKey, cdc), + GetCmdQueryVotes(storeKey, cdc), + GetCmdQueryParam(storeKey, cdc), + GetCmdQueryParams(storeKey, cdc), + GetCmdQueryProposer(storeKey, cdc), + GetCmdQueryDeposit(storeKey, cdc), + GetCmdQueryDeposits(storeKey, cdc), + GetCmdQueryTally(storeKey, cdc))...) + + return govQueryCmd +} + // GetCmdQueryProposal implements the query proposal command. func GetCmdQueryProposal(queryRoute string, cdc *codec.Codec) *cobra.Command { return &cobra.Command{ diff --git a/x/gov/client/cli/tx.go b/x/gov/client/cli/tx.go index a488be2f4c2e..0ac06f3a3366 100644 --- a/x/gov/client/cli/tx.go +++ b/x/gov/client/cli/tx.go @@ -4,6 +4,7 @@ import ( "fmt" "strconv" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/utils" "github.com/cosmos/cosmos-sdk/codec" @@ -48,6 +49,31 @@ var ProposalFlags = []string{ FlagDeposit, } +// GetTxCmd returns the transaction commands for this module +// governance ModuleClient is slightly different from other ModuleClients in that +// it contains a slice of "proposal" child commands. These commands are respective +// to proposal type handlers that are implemented in other modules but are mounted +// under the governance CLI (eg. parameter change proposals). +func GetTxCmd(storeKey string, cdc *amino.Codec, pcmds ...*cobra.Command) *cobra.Command { + govTxCmd := &cobra.Command{ + Use: gov.ModuleName, + Short: "Governance transactions subcommands", + } + + cmdSubmitProp := GetCmdSubmitProposal(cdc) + for _, pcmd := range pcmds { + cmdSubmitProp.AddCommand(client.PostCommands(pcmd)[0]) + } + + govTxCmd.AddCommand(client.PostCommands( + GetCmdDeposit(storeKey, cdc), + GetCmdVote(storeKey, cdc), + cmdSubmitProp, + )...) + + return govTxCmd +} + // GetCmdSubmitProposal implements submitting a proposal transaction command. func GetCmdSubmitProposal(cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ diff --git a/x/gov/client/module_client.go b/x/gov/client/module_client.go deleted file mode 100644 index 80858ff13f22..000000000000 --- a/x/gov/client/module_client.go +++ /dev/null @@ -1,69 +0,0 @@ -package client - -import ( - "github.com/spf13/cobra" - amino "github.com/tendermint/go-amino" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/x/gov" - govCli "github.com/cosmos/cosmos-sdk/x/gov/client/cli" -) - -// ModuleClient exports all client functionality from the governance module. The -// governance ModuleClient is slightly different from other ModuleClients in that -// it contains a slice of "proposal" child commands. These commands are respective -// to proposal type handlers that are implemented in other modules but are mounted -// under the governance CLI (eg. parameter change proposals). -type ModuleClient struct { - storeKey string - cdc *amino.Codec - pcmds []*cobra.Command -} - -func NewModuleClient(storeKey string, cdc *amino.Codec, pcmds ...*cobra.Command) ModuleClient { - return ModuleClient{storeKey, cdc, pcmds} -} - -// GetQueryCmd returns the cli query commands for this module -func (mc ModuleClient) GetQueryCmd() *cobra.Command { - // Group gov queries under a subcommand - govQueryCmd := &cobra.Command{ - Use: gov.ModuleName, - Short: "Querying commands for the governance module", - } - - govQueryCmd.AddCommand(client.GetCommands( - govCli.GetCmdQueryProposal(mc.storeKey, mc.cdc), - govCli.GetCmdQueryProposals(mc.storeKey, mc.cdc), - govCli.GetCmdQueryVote(mc.storeKey, mc.cdc), - govCli.GetCmdQueryVotes(mc.storeKey, mc.cdc), - govCli.GetCmdQueryParam(mc.storeKey, mc.cdc), - govCli.GetCmdQueryParams(mc.storeKey, mc.cdc), - govCli.GetCmdQueryProposer(mc.storeKey, mc.cdc), - govCli.GetCmdQueryDeposit(mc.storeKey, mc.cdc), - govCli.GetCmdQueryDeposits(mc.storeKey, mc.cdc), - govCli.GetCmdQueryTally(mc.storeKey, mc.cdc))...) - - return govQueryCmd -} - -// GetTxCmd returns the transaction commands for this module -func (mc ModuleClient) GetTxCmd() *cobra.Command { - govTxCmd := &cobra.Command{ - Use: gov.ModuleName, - Short: "Governance transactions subcommands", - } - - cmdSubmitProp := govCli.GetCmdSubmitProposal(mc.cdc) - for _, pcmd := range mc.pcmds { - cmdSubmitProp.AddCommand(client.PostCommands(pcmd)[0]) - } - - govTxCmd.AddCommand(client.PostCommands( - govCli.GetCmdDeposit(mc.storeKey, mc.cdc), - govCli.GetCmdVote(mc.storeKey, mc.cdc), - cmdSubmitProp, - )...) - - return govTxCmd -} diff --git a/x/gov/module.go b/x/gov/module.go index 7db8824a0db5..135db58d4987 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -3,10 +3,15 @@ package gov import ( "encoding/json" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" + + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/gov/client/cli" + "github.com/cosmos/cosmos-sdk/x/gov/client/rest" "github.com/cosmos/cosmos-sdk/x/gov/types" - abci "github.com/tendermint/tendermint/abci/types" ) var ( @@ -15,7 +20,16 @@ var ( ) // app module basics object -type AppModuleBasic struct{} +type AppModuleBasic struct { + proposalCmds []*cobra.Command // proposal subcommands which live in goverance +} + +// NewAppModuleBasic creates a new AppModuleBasic object +func NewAppModuleBasic(proposalCmds []*cobra.Command) AppModuleBasic { + return AppModuleBasic{ + proposalCmds: proposalCmds, + } +} var _ sdk.AppModuleBasic = AppModuleBasic{} @@ -44,6 +58,21 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } +// register rest routes +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec) { + rest.RegisterRoutes(ctx, rtr, cdc, StoreKey) +} + +// get the root tx command of this module +func (am AppModuleBasic) GetTxCmd() *cobra.Command { + return cli.GetTxCmd(StoreKey, moduleCdc, a.pcmds...) +} + +// get the root query command of this module +func (AppModuleBasic) GetQueryCmd() *cobra.Command { + return cli.GetQueryCmd(StoreKey, moduleCdc) +} + //___________________________ // app module type AppModule struct { diff --git a/x/mint/client/cli/query.go b/x/mint/client/cli/query.go index 65bde6adeb32..91f3f88aabf3 100644 --- a/x/mint/client/cli/query.go +++ b/x/mint/client/cli/query.go @@ -11,6 +11,24 @@ import ( "github.com/cosmos/cosmos-sdk/x/mint" ) +// GetQueryCmd returns the cli query commands for the minting module. +func GetQueryCmd(cdc *codec.Codec) *cobra.Command { + mintingQueryCmd := &cobra.Command{ + Use: mint.ModuleName, + Short: "Querying commands for the minting module", + } + + mintingQueryCmd.AddCommand( + sdkclient.GetCommands( + GetCmdQueryParams(cdc), + GetCmdQueryInflation(cdc), + GetCmdQueryAnnualProvisions(cdc), + )..., + ) + + return mintingQueryCmd +} + // GetCmdQueryParams implements a command to return the current minting // parameters. func GetCmdQueryParams(cdc *codec.Codec) *cobra.Command { diff --git a/x/mint/client/module_client.go b/x/mint/client/module_client.go deleted file mode 100644 index 4a22b6e763b8..000000000000 --- a/x/mint/client/module_client.go +++ /dev/null @@ -1,47 +0,0 @@ -package client - -import ( - "github.com/spf13/cobra" - amino "github.com/tendermint/go-amino" - - sdkclient "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/x/mint" - "github.com/cosmos/cosmos-sdk/x/mint/client/cli" -) - -type ModuleClient struct { - storeKey string - cdc *amino.Codec -} - -func NewModuleClient(storeKey string, cdc *amino.Codec) ModuleClient { - return ModuleClient{storeKey, cdc} -} - -// GetQueryCmd returns the cli query commands for the minting module. -func (mc ModuleClient) GetQueryCmd() *cobra.Command { - mintingQueryCmd := &cobra.Command{ - Use: mint.ModuleName, - Short: "Querying commands for the minting module", - } - - mintingQueryCmd.AddCommand( - sdkclient.GetCommands( - cli.GetCmdQueryParams(mc.cdc), - cli.GetCmdQueryInflation(mc.cdc), - cli.GetCmdQueryAnnualProvisions(mc.cdc), - )..., - ) - - return mintingQueryCmd -} - -// GetTxCmd returns the transaction commands for the minting module. -func (mc ModuleClient) GetTxCmd() *cobra.Command { - mintTxCmd := &cobra.Command{ - Use: mint.ModuleName, - Short: "Minting transaction subcommands", - } - - return mintTxCmd -} diff --git a/x/mint/module.go b/x/mint/module.go index d645624fa09f..f4f8921147a1 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -3,9 +3,14 @@ package mint import ( "encoding/json" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" + + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - abci "github.com/tendermint/tendermint/abci/types" + "github.com/cosmos/cosmos-sdk/x/gov/client/cli" + "github.com/cosmos/cosmos-sdk/x/mint/client/rest" ) var ( @@ -44,6 +49,21 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } +// register rest routes +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec) { + rest.RegisterRoutes(ctx, rtr, cdc, StoreKey) +} + +// get the root tx command of this module +func (AppModuleBasic) GetTxCmd() *cobra.Command { + return nil +} + +// get the root query command of this module +func (AppModuleBasic) GetQueryCmd() *cobra.Command { + return cli.GetQueryCmd(moduleCdc) +} + //___________________________ // app module type AppModule struct { diff --git a/x/params/client/cli/tx.go b/x/params/client/cli/tx.go index 1997512f1cb1..eef8e99d9e76 100644 --- a/x/params/client/cli/tx.go +++ b/x/params/client/cli/tx.go @@ -7,17 +7,17 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/utils" - "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" "github.com/cosmos/cosmos-sdk/x/gov" "github.com/cosmos/cosmos-sdk/x/params" paramscutils "github.com/cosmos/cosmos-sdk/x/params/client/utils" + "github.com/cosmos/cosmos-sdk/x/params/types" ) // GetCmdSubmitProposal implements a command handler for submitting a parameter // change proposal transaction. -func GetCmdSubmitProposal(cdc *codec.Codec) *cobra.Command { +func GetCmdSubmitProposal() *cobra.Command { cmd := &cobra.Command{ Use: "param-change [proposal-file]", Args: cobra.ExactArgs(1), @@ -56,12 +56,13 @@ where proposal.json contains: } `), RunE: func(cmd *cobra.Command, args []string) error { - txBldr := authtxb.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc)) + + txBldr := authtxb.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(types.ModuleCdc)) cliCtx := context.NewCLIContext(). - WithCodec(cdc). - WithAccountDecoder(cdc) + WithCodec(types.ModuleCdc). + WithAccountDecoder(types.ModuleCdc) - proposal, err := paramscutils.ParseParamChangeProposalJSON(cdc, args[0]) + proposal, err := paramscutils.ParseParamChangeProposalJSON(types.ModuleCdc, args[0]) if err != nil { return err } diff --git a/x/params/module.go b/x/params/module.go index c1a65bf9b273..fec9828e672f 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -3,8 +3,12 @@ package params import ( "encoding/json" + "github.com/spf13/cobra" + + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/params/client/rest" "github.com/cosmos/cosmos-sdk/x/params/types" ) @@ -31,3 +35,14 @@ func (AppModuleBasic) DefaultGenesis() json.RawMessage { return nil } // module validate genesis func (AppModuleBasic) ValidateGenesis(_ json.RawMessage) error { return nil } + +// register rest routes +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec) { + rest.RegisterRoutes(ctx, rtr, cdc, StoreKey) +} + +// get the root tx command of this module +func (AppModuleBasic) GetTxCmd() *cobra.Command { return nil } + +// get the root query command of this module +func (AppModuleBasic) GetQueryCmd() *cobra.Command { return nil } diff --git a/x/params/types/codec.go b/x/params/types/codec.go index fbbc5ffd7da3..d985ed3f64d6 100644 --- a/x/params/types/codec.go +++ b/x/params/types/codec.go @@ -4,13 +4,14 @@ import ( "github.com/cosmos/cosmos-sdk/codec" ) -var msgCdc = codec.New() +// module codec +var ModuleCdc = codec.New() func init() { - RegisterCodec(msgCdc) + RegisterCodec(ModuleCdc) } // RegisterCodec registers all necessary param module types with a given codec. -func RegisterCodec(cdc *codec.Codec) { +func RegisterCodec(ModuleCdc *codec.Codec) { cdc.RegisterConcrete(ParameterChangeProposal{}, "cosmos-sdk/ParameterChangeProposal", nil) } diff --git a/x/params/types/proposal.go b/x/params/types/proposal.go index d4bb2de490a2..e0ef568532d5 100644 --- a/x/params/types/proposal.go +++ b/x/params/types/proposal.go @@ -45,6 +45,7 @@ func (pcp ParameterChangeProposal) ProposalRoute() string { return RouterKey } // ProposalType returns the type of a parameter change proposal. func (pcp ParameterChangeProposal) ProposalType() string { return ProposalTypeChange } +// validate the ParameterChangeProposal func (pcp ParameterChangeProposal) ValidateBasic() sdk.Error { err := govtypes.ValidateAbstract(DefaultCodespace, pcp) if err != nil { diff --git a/x/slashing/client/cli/query.go b/x/slashing/client/cli/query.go index 6c14104f7d03..4d2edee62145 100644 --- a/x/slashing/client/cli/query.go +++ b/x/slashing/client/cli/query.go @@ -6,12 +6,32 @@ import ( "github.com/spf13/cobra" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/slashing" ) +// GetQueryCmd returns the cli query commands for this module +func GetQueryCmd(storeKey string, cdc *codec.Codec) *cobra.Command { + // Group slashing queries under a subcommand + slashingQueryCmd := &cobra.Command{ + Use: slashing.ModuleName, + Short: "Querying commands for the slashing module", + } + + slashingQueryCmd.AddCommand( + client.GetCommands( + GetCmdQuerySigningInfo(storeKey, cdc), + GetCmdQueryParams(cdc), + )..., + ) + + return slashingQueryCmd + +} + // GetCmdQuerySigningInfo implements the command to query signing info. func GetCmdQuerySigningInfo(storeName string, cdc *codec.Codec) *cobra.Command { return &cobra.Command{ diff --git a/x/slashing/client/cli/tx.go b/x/slashing/client/cli/tx.go index 1da2dc651142..ab8f546fddd5 100644 --- a/x/slashing/client/cli/tx.go +++ b/x/slashing/client/cli/tx.go @@ -1,6 +1,7 @@ package cli import ( + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/utils" "github.com/cosmos/cosmos-sdk/codec" @@ -11,6 +12,20 @@ import ( "github.com/spf13/cobra" ) +// GetTxCmd returns the transaction commands for this module +func GetTxCmd(cdc *codec.Codec) *cobra.Command { + slashingTxCmd := &cobra.Command{ + Use: slashing.ModuleName, + Short: "Slashing transactions subcommands", + } + + slashingTxCmd.AddCommand(client.PostCommands( + GetCmdUnjail(cdc), + )...) + + return slashingTxCmd +} + // GetCmdUnjail implements the create unjail validator command. func GetCmdUnjail(cdc *codec.Codec) *cobra.Command { return &cobra.Command{ diff --git a/x/slashing/client/module_client.go b/x/slashing/client/module_client.go deleted file mode 100644 index aca581f2e8a8..000000000000 --- a/x/slashing/client/module_client.go +++ /dev/null @@ -1,53 +0,0 @@ -package client - -import ( - "github.com/spf13/cobra" - amino "github.com/tendermint/go-amino" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/x/slashing" - "github.com/cosmos/cosmos-sdk/x/slashing/client/cli" -) - -// ModuleClient exports all client functionality from this module -type ModuleClient struct { - storeKey string - cdc *amino.Codec -} - -func NewModuleClient(storeKey string, cdc *amino.Codec) ModuleClient { - return ModuleClient{storeKey, cdc} -} - -// GetQueryCmd returns the cli query commands for this module -func (mc ModuleClient) GetQueryCmd() *cobra.Command { - // Group slashing queries under a subcommand - slashingQueryCmd := &cobra.Command{ - Use: slashing.ModuleName, - Short: "Querying commands for the slashing module", - } - - slashingQueryCmd.AddCommand( - client.GetCommands( - cli.GetCmdQuerySigningInfo(mc.storeKey, mc.cdc), - cli.GetCmdQueryParams(mc.cdc), - )..., - ) - - return slashingQueryCmd - -} - -// GetTxCmd returns the transaction commands for this module -func (mc ModuleClient) GetTxCmd() *cobra.Command { - slashingTxCmd := &cobra.Command{ - Use: slashing.ModuleName, - Short: "Slashing transactions subcommands", - } - - slashingTxCmd.AddCommand(client.PostCommands( - cli.GetCmdUnjail(mc.cdc), - )...) - - return slashingTxCmd -} diff --git a/x/slashing/client/rest/rest.go b/x/slashing/client/rest/rest.go index d06cd719df20..d3bfc07350b9 100644 --- a/x/slashing/client/rest/rest.go +++ b/x/slashing/client/rest/rest.go @@ -5,11 +5,10 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/crypto/keys" ) // RegisterRoutes registers staking-related REST handlers to a router -func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec, kb keys.Keybase) { +func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) { registerQueryRoutes(cliCtx, r, cdc) - registerTxRoutes(cliCtx, r, cdc, kb) + registerTxRoutes(cliCtx, r, cdc) } diff --git a/x/slashing/client/rest/tx.go b/x/slashing/client/rest/tx.go index 95b93740b0ac..1ca16cbc85ee 100644 --- a/x/slashing/client/rest/tx.go +++ b/x/slashing/client/rest/tx.go @@ -9,16 +9,15 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" clientrest "github.com/cosmos/cosmos-sdk/client/rest" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/crypto/keys" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/rest" "github.com/cosmos/cosmos-sdk/x/slashing" ) -func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec, kb keys.Keybase) { +func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) { r.HandleFunc( "/slashing/validators/{validatorAddr}/unjail", - unjailRequestHandlerFn(cdc, kb, cliCtx), + unjailRequestHandlerFn(cdc, cliCtx), ).Methods("POST") } @@ -27,7 +26,7 @@ type UnjailReq struct { BaseReq rest.BaseReq `json:"base_req"` } -func unjailRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIContext) http.HandlerFunc { +func unjailRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) diff --git a/x/slashing/module.go b/x/slashing/module.go index 15caaf6bb429..7b5bf17340c2 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -3,9 +3,14 @@ package slashing import ( "encoding/json" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" + + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - abci "github.com/tendermint/tendermint/abci/types" + "github.com/cosmos/cosmos-sdk/x/gov/client/cli" + "github.com/cosmos/cosmos-sdk/x/slashing/client/rest" ) var ( @@ -46,6 +51,21 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } +// register rest routes +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec) { + rest.RegisterRoutes(ctx, rtr, cdc, StoreKey) +} + +// get the root tx command of this module +func (AppModuleBasic) GetTxCmd() *cobra.Command { + return cli.GetTxCmd(StoreKey, moduleCdc) +} + +// get the root query command of this module +func (AppModuleBasic) GetQueryCmd() *cobra.Command { + return cli.GetQueryCmd(moduleCdc) +} + //___________________________ // app module type AppModule struct { diff --git a/x/staking/client/cli/query.go b/x/staking/client/cli/query.go index 7366dc5ae3ff..454edc882a75 100644 --- a/x/staking/client/cli/query.go +++ b/x/staking/client/cli/query.go @@ -6,6 +6,7 @@ import ( "github.com/spf13/cobra" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -13,6 +14,31 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/types" ) +// GetQueryCmd returns the cli query commands for this module +func GetQueryCmd(storeKey string, cdc *codec.Codec) *cobra.Command { + stakingQueryCmd := &cobra.Command{ + Use: types.ModuleName, + Short: "Querying commands for the staking module", + } + stakingQueryCmd.AddCommand(client.GetCommands( + GetCmdQueryDelegation(mc.storeKey, mc.cdc), + GetCmdQueryDelegations(mc.storeKey, mc.cdc), + GetCmdQueryUnbondingDelegation(mc.storeKey, mc.cdc), + GetCmdQueryUnbondingDelegations(mc.storeKey, mc.cdc), + GetCmdQueryRedelegation(mc.storeKey, mc.cdc), + GetCmdQueryRedelegations(mc.storeKey, mc.cdc), + GetCmdQueryValidator(mc.storeKey, mc.cdc), + GetCmdQueryValidators(mc.storeKey, mc.cdc), + GetCmdQueryValidatorDelegations(mc.storeKey, mc.cdc), + GetCmdQueryValidatorUnbondingDelegations(mc.storeKey, mc.cdc), + GetCmdQueryValidatorRedelegations(mc.storeKey, mc.cdc), + GetCmdQueryParams(mc.storeKey, mc.cdc), + GetCmdQueryPool(mc.storeKey, mc.cdc))...) + + return stakingQueryCmd + +} + // GetCmdQueryValidator implements the validator query command. func GetCmdQueryValidator(storeName string, cdc *codec.Codec) *cobra.Command { return &cobra.Command{ diff --git a/x/staking/client/cli/tx.go b/x/staking/client/cli/tx.go index 0507fb7eaee3..be7663a90149 100644 --- a/x/staking/client/cli/tx.go +++ b/x/staking/client/cli/tx.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" @@ -18,6 +19,24 @@ import ( "github.com/spf13/viper" ) +// GetTxCmd returns the transaction commands for this module +func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command { + stakingTxCmd := &cobra.Command{ + Use: types.ModuleName, + Short: "Staking transaction subcommands", + } + + stakingTxCmd.AddCommand(client.PostCommands( + GetCmdCreateValidator(mc.cdc), + GetCmdEditValidator(mc.cdc), + GetCmdDelegate(mc.cdc), + GetCmdRedelegate(mc.storeKey, mc.cdc), + GetCmdUnbond(mc.storeKey, mc.cdc), + )...) + + return stakingTxCmd +} + // GetCmdCreateValidator implements the create validator command handler. // TODO: Add full description func GetCmdCreateValidator(cdc *codec.Codec) *cobra.Command { diff --git a/x/staking/client/module_client.go b/x/staking/client/module_client.go deleted file mode 100644 index 754e8c5c8500..000000000000 --- a/x/staking/client/module_client.go +++ /dev/null @@ -1,63 +0,0 @@ -package client - -import ( - "github.com/spf13/cobra" - amino "github.com/tendermint/go-amino" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/x/staking/client/cli" - "github.com/cosmos/cosmos-sdk/x/staking/types" -) - -// ModuleClient exports all client functionality from this module -type ModuleClient struct { - storeKey string - cdc *amino.Codec -} - -func NewModuleClient(storeKey string, cdc *amino.Codec) ModuleClient { - return ModuleClient{storeKey, cdc} -} - -// GetQueryCmd returns the cli query commands for this module -func (mc ModuleClient) GetQueryCmd() *cobra.Command { - stakingQueryCmd := &cobra.Command{ - Use: types.ModuleName, - Short: "Querying commands for the staking module", - } - stakingQueryCmd.AddCommand(client.GetCommands( - cli.GetCmdQueryDelegation(mc.storeKey, mc.cdc), - cli.GetCmdQueryDelegations(mc.storeKey, mc.cdc), - cli.GetCmdQueryUnbondingDelegation(mc.storeKey, mc.cdc), - cli.GetCmdQueryUnbondingDelegations(mc.storeKey, mc.cdc), - cli.GetCmdQueryRedelegation(mc.storeKey, mc.cdc), - cli.GetCmdQueryRedelegations(mc.storeKey, mc.cdc), - cli.GetCmdQueryValidator(mc.storeKey, mc.cdc), - cli.GetCmdQueryValidators(mc.storeKey, mc.cdc), - cli.GetCmdQueryValidatorDelegations(mc.storeKey, mc.cdc), - cli.GetCmdQueryValidatorUnbondingDelegations(mc.storeKey, mc.cdc), - cli.GetCmdQueryValidatorRedelegations(mc.storeKey, mc.cdc), - cli.GetCmdQueryParams(mc.storeKey, mc.cdc), - cli.GetCmdQueryPool(mc.storeKey, mc.cdc))...) - - return stakingQueryCmd - -} - -// GetTxCmd returns the transaction commands for this module -func (mc ModuleClient) GetTxCmd() *cobra.Command { - stakingTxCmd := &cobra.Command{ - Use: types.ModuleName, - Short: "Staking transaction subcommands", - } - - stakingTxCmd.AddCommand(client.PostCommands( - cli.GetCmdCreateValidator(mc.cdc), - cli.GetCmdEditValidator(mc.cdc), - cli.GetCmdDelegate(mc.cdc), - cli.GetCmdRedelegate(mc.storeKey, mc.cdc), - cli.GetCmdUnbond(mc.storeKey, mc.cdc), - )...) - - return stakingTxCmd -} diff --git a/x/staking/client/rest/rest.go b/x/staking/client/rest/rest.go index 507fcb517fc4..ff429456da38 100644 --- a/x/staking/client/rest/rest.go +++ b/x/staking/client/rest/rest.go @@ -3,13 +3,12 @@ package rest import ( "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/crypto/keys" "github.com/gorilla/mux" ) // RegisterRoutes registers staking-related REST handlers to a router -func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec, kb keys.Keybase) { +func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) { registerQueryRoutes(cliCtx, r, cdc) - registerTxRoutes(cliCtx, r, cdc, kb) + registerTxRoutes(cliCtx, r, cdc) } diff --git a/x/staking/client/rest/tx.go b/x/staking/client/rest/tx.go index f42a90c9d02a..134177694739 100644 --- a/x/staking/client/rest/tx.go +++ b/x/staking/client/rest/tx.go @@ -9,24 +9,23 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" clientrest "github.com/cosmos/cosmos-sdk/client/rest" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/crypto/keys" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/rest" "github.com/cosmos/cosmos-sdk/x/staking" ) -func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec, kb keys.Keybase) { +func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) { r.HandleFunc( "/staking/delegators/{delegatorAddr}/delegations", - postDelegationsHandlerFn(cdc, kb, cliCtx), + postDelegationsHandlerFn(cdc, cliCtx), ).Methods("POST") r.HandleFunc( "/staking/delegators/{delegatorAddr}/unbonding_delegations", - postUnbondingDelegationsHandlerFn(cdc, kb, cliCtx), + postUnbondingDelegationsHandlerFn(cdc, cliCtx), ).Methods("POST") r.HandleFunc( "/staking/delegators/{delegatorAddr}/redelegations", - postRedelegationsHandlerFn(cdc, kb, cliCtx), + postRedelegationsHandlerFn(cdc, cliCtx), ).Methods("POST") } @@ -57,7 +56,7 @@ type ( } ) -func postDelegationsHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIContext) http.HandlerFunc { +func postDelegationsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req DelegateRequest @@ -91,7 +90,7 @@ func postDelegationsHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context. } } -func postRedelegationsHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIContext) http.HandlerFunc { +func postRedelegationsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req RedelegateRequest @@ -125,7 +124,7 @@ func postRedelegationsHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx contex } } -func postUnbondingDelegationsHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIContext) http.HandlerFunc { +func postUnbondingDelegationsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req UndelegateRequest diff --git a/x/staking/module.go b/x/staking/module.go index 520829054b9a..617da6e271ac 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -3,10 +3,15 @@ package staking import ( "encoding/json" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" + + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/staking/client/cli" + "github.com/cosmos/cosmos-sdk/x/staking/client/rest" "github.com/cosmos/cosmos-sdk/x/staking/types" - abci "github.com/tendermint/tendermint/abci/types" ) var ( @@ -44,6 +49,21 @@ func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { return ValidateGenesis(data) } +// register rest routes +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec) { + rest.RegisterRoutes(ctx, rtr, cdc, StoreKey) +} + +// get the root tx command of this module +func (AppModuleBasic) GetTxCmd() *cobra.Command { + return cli.GetTxCmd(moduleCdc, StoreKey) +} + +// get the root query command of this module +func (AppModuleBasic) GetQueryCmd() *cobra.Command { + return cli.GetQueryCmd(moduleCdc, StoreKey) +} + // app module type AppModule struct { AppModuleBasic