Skip to content

Commit

Permalink
Merge PR #31: Update to SDK 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 1b33618 commit 0846cc5
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 142 deletions.
3 changes: 3 additions & 0 deletions .pending/breaking/gaiacli/Bank-and-Auth-module
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Bank and Auth module commands are now mounted under their respective module:
* `sign` and `multisign` are under `gaiacli tx auth`
* `send` is under `gaiacli tx bank`
23 changes: 13 additions & 10 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,29 @@ import (
"io"
"os"

abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"

bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/genaccounts"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/crisis"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
distrclient "github.com/cosmos/cosmos-sdk/x/distribution/client"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/params"
paramsclient "github.com/cosmos/cosmos-sdk/x/params/client"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"

abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
)

const appName = "GaiaApp"
Expand All @@ -38,19 +41,19 @@ var (
// The ModuleBasicManager is in charge of setting up basic,
// non-dependant module elements, such as codec registration
// and genesis verification.
ModuleBasics sdk.ModuleBasicManager
ModuleBasics module.BasicManager
)

func init() {
ModuleBasics = sdk.NewModuleBasicManager(
ModuleBasics = module.NewBasicManager(
genaccounts.AppModuleBasic{},
genutil.AppModuleBasic{},
auth.AppModuleBasic{},
bank.AppModuleBasic{},
staking.AppModuleBasic{},
mint.AppModuleBasic{},
distr.AppModuleBasic{},
gov.AppModuleBasic{},
gov.NewAppModuleBasic(paramsclient.ProposalHandler, distrclient.ProposalHandler),
params.AppModuleBasic{},
crisis.AppModuleBasic{},
slashing.AppModuleBasic{},
Expand Down Expand Up @@ -100,7 +103,7 @@ type GaiaApp struct {
paramsKeeper params.Keeper

// the module manager
mm *sdk.ModuleManager
mm *module.Manager
}

// NewGaiaApp returns a reference to an initialized GaiaApp.
Expand Down Expand Up @@ -169,7 +172,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b
app.stakingKeeper = *stakingKeeper.SetHooks(
staking.NewMultiStakingHooks(app.distrKeeper.Hooks(), app.slashingKeeper.Hooks()))

app.mm = sdk.NewModuleManager(
app.mm = module.NewManager(
genaccounts.NewAppModule(app.accountKeeper),
genutil.NewAppModule(app.accountKeeper, app.stakingKeeper, app.BaseApp.DeliverTx),
auth.NewAppModule(app.accountKeeper, app.feeCollectionKeeper),
Expand Down
5 changes: 2 additions & 3 deletions app/export.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/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/staking/expected"
)

// export the state of gaia for a genesis file
Expand Down Expand Up @@ -61,7 +60,7 @@ func (app *GaiaApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []st
/* Handle fee distribution state. */

// withdraw all validator commission
app.stakingKeeper.IterateValidators(ctx, func(_ int64, val expected.ValidatorI) (stop bool) {
app.stakingKeeper.IterateValidators(ctx, func(_ int64, val staking.ValidatorI) (stop bool) {
_, _ = app.distrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
return false
})
Expand All @@ -83,7 +82,7 @@ func (app *GaiaApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []st
ctx = ctx.WithBlockHeight(0)

// reinitialize all validators
app.stakingKeeper.IterateValidators(ctx, func(_ int64, val expected.ValidatorI) (stop bool) {
app.stakingKeeper.IterateValidators(ctx, func(_ int64, val staking.ValidatorI) (stop bool) {

// donate any unwithdrawn outstanding reward fraction tokens to the community pool
scraps := app.distrKeeper.GetValidatorOutstandingRewards(ctx, val.GetOperator())
Expand Down
8 changes: 4 additions & 4 deletions cli_test/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,21 +309,21 @@ func (f *Fixtures) CLIConfig(key, value string, flags ...string) {

// TxSend is gaiacli tx send
func (f *Fixtures) TxSend(from string, to sdk.AccAddress, amount sdk.Coin, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx send %s %s %s %v", f.GaiacliBinary, from, to, amount, f.Flags())
cmd := fmt.Sprintf("%s tx bank send %s %s %s %v", f.GaiacliBinary, from, to, amount, f.Flags())
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), client.DefaultKeyPass)
}

func (f *Fixtures) txSendWithConfirm(
from string, to sdk.AccAddress, amount sdk.Coin, confirm string, flags ...string,
) (bool, string, string) {

cmd := fmt.Sprintf("%s tx send %s %s %s %v", f.GaiacliBinary, from, to, amount, f.Flags())
cmd := fmt.Sprintf("%s tx bank send %s %s %s %v", f.GaiacliBinary, from, to, amount, f.Flags())
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), confirm, client.DefaultKeyPass)
}

// TxSign is gaiacli tx sign
func (f *Fixtures) TxSign(signer, fileName string, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx sign %v --from=%s %v", f.GaiacliBinary, f.Flags(), signer, fileName)
cmd := fmt.Sprintf("%s tx auth sign %v --from=%s %v", f.GaiacliBinary, f.Flags(), signer, fileName)
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), client.DefaultKeyPass)
}

Expand All @@ -343,7 +343,7 @@ func (f *Fixtures) TxEncode(fileName string, flags ...string) (bool, string, str
func (f *Fixtures) TxMultisign(fileName, name string, signaturesFiles []string,
flags ...string) (bool, string, string) {

cmd := fmt.Sprintf("%s tx multisign %v %s %s %s", f.GaiacliBinary, f.Flags(),
cmd := fmt.Sprintf("%s tx auth multisign %v %s %s %s", f.GaiacliBinary, f.Flags(),
fileName, name, strings.Join(signaturesFiles, " "),
)
return executeWriteRetStdStreams(f.T, cmd)
Expand Down
76 changes: 13 additions & 63 deletions cmd/gaiacli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,11 @@ import (
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
at "github.com/cosmos/cosmos-sdk/x/auth"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
auth "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
bank "github.com/cosmos/cosmos-sdk/x/bank/client/rest"
crisisclient "github.com/cosmos/cosmos-sdk/x/crisis/client"
distcmd "github.com/cosmos/cosmos-sdk/x/distribution"
distClient "github.com/cosmos/cosmos-sdk/x/distribution/client"
distrcli "github.com/cosmos/cosmos-sdk/x/distribution/client/cli"
dist "github.com/cosmos/cosmos-sdk/x/distribution/client/rest"
gv "github.com/cosmos/cosmos-sdk/x/gov"
govClient "github.com/cosmos/cosmos-sdk/x/gov/client"
gov "github.com/cosmos/cosmos-sdk/x/gov/client/rest"
"github.com/cosmos/cosmos-sdk/x/mint"
mintclient "github.com/cosmos/cosmos-sdk/x/mint/client"
mintrest "github.com/cosmos/cosmos-sdk/x/mint/client/rest"
paramcli "github.com/cosmos/cosmos-sdk/x/params/client/cli"
paramsrest "github.com/cosmos/cosmos-sdk/x/params/client/rest"
sl "github.com/cosmos/cosmos-sdk/x/slashing"
slashingclient "github.com/cosmos/cosmos-sdk/x/slashing/client"
slashing "github.com/cosmos/cosmos-sdk/x/slashing/client/rest"
st "github.com/cosmos/cosmos-sdk/x/staking"
stakingclient "github.com/cosmos/cosmos-sdk/x/staking/client"
staking "github.com/cosmos/cosmos-sdk/x/staking/client/rest"

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

amino "github.com/tendermint/go-amino"
"github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/libs/cli"
)

Expand All @@ -64,17 +40,6 @@ func main() {
// the below functions and eliminate global vars, like we do
// with the cdc

// Module clients hold cli commnads (tx,query) and lcd routes
// TODO: Make the lcd command take a list of ModuleClient
mc := []sdk.ModuleClient{
govClient.NewModuleClient(gv.StoreKey, cdc, paramcli.GetCmdSubmitProposal(cdc), distrcli.GetCmdSubmitProposal(cdc)),
distClient.NewModuleClient(distcmd.StoreKey, cdc),
stakingclient.NewModuleClient(st.StoreKey, cdc),
mintclient.NewModuleClient(mint.StoreKey, cdc),
slashingclient.NewModuleClient(sl.StoreKey, cdc),
crisisclient.NewModuleClient(sl.StoreKey, cdc),
}

rootCmd := &cobra.Command{
Use: "gaiacli",
Short: "Command line interface for interacting with gaiad",
Expand All @@ -90,8 +55,8 @@ func main() {
rootCmd.AddCommand(
rpc.StatusCommand(),
client.ConfigCmd(app.DefaultCLIHome),
queryCmd(cdc, mc),
txCmd(cdc, mc),
queryCmd(cdc),
txCmd(cdc),
client.LineBreak,
lcd.ServeCommand(cdc, registerRoutes),
client.LineBreak,
Expand All @@ -111,51 +76,43 @@ func main() {
}
}

func queryCmd(cdc *amino.Codec, mc []sdk.ModuleClient) *cobra.Command {
func queryCmd(cdc *amino.Codec) *cobra.Command {
queryCmd := &cobra.Command{
Use: "query",
Aliases: []string{"q"},
Short: "Querying subcommands",
}

queryCmd.AddCommand(
authcmd.GetAccountCmd(cdc),
client.LineBreak,
rpc.ValidatorCommand(cdc),
rpc.BlockCommand(),
tx.SearchTxCmd(cdc),
tx.QueryTxCmd(cdc),
client.LineBreak,
authcmd.GetAccountCmd(at.StoreKey, cdc),
)

for _, m := range mc {
mQueryCmd := m.GetQueryCmd()
if mQueryCmd != nil {
queryCmd.AddCommand(mQueryCmd)
}
}
// add modules' query commands
app.ModuleBasics.AddQueryCommands(queryCmd, cdc)

return queryCmd
}

func txCmd(cdc *amino.Codec, mc []sdk.ModuleClient) *cobra.Command {
func txCmd(cdc *amino.Codec) *cobra.Command {
txCmd := &cobra.Command{
Use: "tx",
Short: "Transactions subcommands",
}

txCmd.AddCommand(
bankcmd.SendTxCmd(cdc),
client.LineBreak,
authcmd.GetSignCommand(cdc),
authcmd.GetMultiSignCommand(cdc),
tx.GetBroadcastCommand(cdc),
tx.GetEncodeCommand(cdc),
client.LineBreak,
)

for _, m := range mc {
txCmd.AddCommand(m.GetTxCmd())
}
// add modules' tx commands
app.ModuleBasics.AddTxCommands(txCmd, cdc)

return txCmd
}
Expand All @@ -164,15 +121,8 @@ func txCmd(cdc *amino.Codec, mc []sdk.ModuleClient) *cobra.Command {
// 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) {
rpc.RegisterRPCRoutes(rs.CliCtx, rs.Mux)
tx.RegisterTxRoutes(rs.CliCtx, rs.Mux, rs.Cdc)
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)
staking.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
slashing.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
gov.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, paramsrest.ProposalRESTHandler(rs.CliCtx, rs.Cdc), dist.ProposalRESTHandler(rs.CliCtx, rs.Cdc))
mintrest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc)
client.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc)
app.ModuleBasics.RegisterRESTRoutes(rs.CliCtx, rs.Mux, rs.Cdc)
}

func initConfig(cmd *cobra.Command) error {
Expand Down
10 changes: 5 additions & 5 deletions cmd/gaiad/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"github.com/cosmos/cosmos-sdk/server"
srvconfig "github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
authtx "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
"github.com/cosmos/cosmos-sdk/x/auth/genaccounts"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/cosmos/cosmos-sdk/x/staking"
Expand All @@ -41,7 +41,7 @@ var (

// get cmd to initialize all files for tendermint testnet and application
func testnetCmd(ctx *server.Context, cdc *codec.Codec,
mbm sdk.ModuleBasicManager, genAccIterator genutil.GenesisAccountsIterator) *cobra.Command {
mbm module.BasicManager, genAccIterator genutil.GenesisAccountsIterator) *cobra.Command {

cmd := &cobra.Command{
Use: "testnet",
Expand Down Expand Up @@ -94,7 +94,7 @@ Example:
const nodeDirPerm = 0755

// Initialize the testnet
func InitTestnet(config *tmconfig.Config, cdc *codec.Codec, mbm sdk.ModuleBasicManager,
func InitTestnet(config *tmconfig.Config, cdc *codec.Codec, mbm module.BasicManager,
genAccIterator genutil.GenesisAccountsIterator,
outputDir, chainID, minGasPrices, nodeDirPrefix, nodeDaemonHome,
nodeCLIHome, startingIPAddress string, numValidators int) error {
Expand Down Expand Up @@ -214,7 +214,7 @@ func InitTestnet(config *tmconfig.Config, cdc *codec.Codec, mbm sdk.ModuleBasicM
return err
}
tx := auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, []auth.StdSignature{}, memo)
txBldr := authtx.NewTxBuilderFromCLI().WithChainID(chainID).WithMemo(memo).WithKeybase(kb)
txBldr := auth.NewTxBuilderFromCLI().WithChainID(chainID).WithMemo(memo).WithKeybase(kb)

signedTx, err := txBldr.SignStdTx(nodeDirName, client.DefaultKeyPass, tx, false)
if err != nil {
Expand Down Expand Up @@ -257,7 +257,7 @@ func InitTestnet(config *tmconfig.Config, cdc *codec.Codec, mbm sdk.ModuleBasicM
return nil
}

func initGenFiles(cdc *codec.Codec, mbm sdk.ModuleBasicManager, chainID string,
func initGenFiles(cdc *codec.Codec, mbm module.BasicManager, chainID string,
accs []genaccounts.GenesisAccount, genFiles []string, numValidators int) error {

appGenState := mbm.DefaultGenesis()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.12

require (
github.com/btcsuite/btcd v0.0.0-20190523000118-16327141da8c // indirect
github.com/cosmos/cosmos-sdk v0.28.2-0.20190604220658-d322e234253a
github.com/cosmos/cosmos-sdk v0.28.2-0.20190605232616-5f9c3fdf8895
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d // indirect
github.com/google/gofuzz v1.0.0 // indirect
github.com/gorilla/mux v1.7.2 // indirect
Expand Down
Loading

0 comments on commit 0846cc5

Please sign in to comment.