229 lines
7.5 KiB
Go
229 lines
7.5 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
"github.com/spf13/cast"
|
|
"github.com/spf13/cobra"
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
tmcli "github.com/tendermint/tendermint/libs/cli"
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
dbm "github.com/tendermint/tm-db"
|
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/client/debug"
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
"github.com/cosmos/cosmos-sdk/client/keys"
|
|
"github.com/cosmos/cosmos-sdk/client/rpc"
|
|
"github.com/cosmos/cosmos-sdk/server"
|
|
servertypes "github.com/cosmos/cosmos-sdk/server/types"
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
"github.com/cosmos/cosmos-sdk/store"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
|
|
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
|
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
|
|
)
|
|
|
|
var (
|
|
rootCmd = &cobra.Command{
|
|
Use: "simd",
|
|
Short: "simulation app",
|
|
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
|
|
if err := client.SetCmdClientContextHandler(initClientCtx, cmd); err != nil {
|
|
return err
|
|
}
|
|
|
|
return server.InterceptConfigsPreRunHandler(cmd)
|
|
},
|
|
}
|
|
|
|
encodingConfig = simapp.MakeEncodingConfig()
|
|
initClientCtx = client.Context{}.
|
|
WithJSONMarshaler(encodingConfig.Marshaler).
|
|
WithInterfaceRegistry(encodingConfig.InterfaceRegistry).
|
|
WithTxConfig(encodingConfig.TxConfig).
|
|
WithLegacyAmino(encodingConfig.Amino).
|
|
WithInput(os.Stdin).
|
|
WithAccountRetriever(types.NewAccountRetriever(encodingConfig.Marshaler)).
|
|
WithBroadcastMode(flags.BroadcastBlock).
|
|
WithHomeDir(simapp.DefaultNodeHome)
|
|
)
|
|
|
|
// Execute executes the root command.
|
|
func Execute() error {
|
|
// Create and set a client.Context on the command's Context. During the pre-run
|
|
// of the root command, a default initialized client.Context is provided to
|
|
// seed child command execution with values such as AccountRetriver, Keyring,
|
|
// and a Tendermint RPC. This requires the use of a pointer reference when
|
|
// getting and setting the client.Context. Ideally, we utilize
|
|
// https://github.com/spf13/cobra/pull/1118.
|
|
ctx := context.Background()
|
|
ctx = context.WithValue(ctx, client.ClientContextKey, &client.Context{})
|
|
ctx = context.WithValue(ctx, server.ServerContextKey, server.NewDefaultContext())
|
|
|
|
executor := tmcli.PrepareBaseCmd(rootCmd, "", simapp.DefaultNodeHome)
|
|
return executor.ExecuteContext(ctx)
|
|
}
|
|
|
|
func init() {
|
|
authclient.Codec = encodingConfig.Marshaler
|
|
|
|
rootCmd.AddCommand(
|
|
withProtoJSON(genutilcli.InitCmd(simapp.ModuleBasics, simapp.DefaultNodeHome)),
|
|
withProtoJSON(genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, simapp.DefaultNodeHome)),
|
|
genutilcli.MigrateGenesisCmd(),
|
|
withProtoJSON(genutilcli.GenTxCmd(simapp.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, simapp.DefaultNodeHome)),
|
|
withProtoJSON(genutilcli.ValidateGenesisCmd(simapp.ModuleBasics, encodingConfig.TxConfig)),
|
|
withProtoJSON(AddGenesisAccountCmd(simapp.DefaultNodeHome)),
|
|
tmcli.NewCompletionCmd(rootCmd, true),
|
|
testnetCmd(simapp.ModuleBasics, banktypes.GenesisBalancesIterator{}),
|
|
debug.Cmd(),
|
|
)
|
|
|
|
server.AddCommands(rootCmd, simapp.DefaultNodeHome, newApp, exportAppStateAndTMValidators)
|
|
|
|
// add keybase, auxiliary RPC, query, and tx child commands
|
|
rootCmd.AddCommand(
|
|
rpc.StatusCommand(),
|
|
queryCommand(),
|
|
txCommand(),
|
|
keys.Commands(simapp.DefaultNodeHome),
|
|
)
|
|
}
|
|
|
|
func queryCommand() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "query",
|
|
Aliases: []string{"q"},
|
|
Short: "Querying subcommands",
|
|
DisableFlagParsing: true,
|
|
SuggestionsMinimumDistance: 2,
|
|
RunE: client.ValidateCmd,
|
|
}
|
|
|
|
cmd.AddCommand(
|
|
authcmd.GetAccountCmd(),
|
|
rpc.ValidatorCommand(),
|
|
rpc.BlockCommand(),
|
|
authcmd.QueryTxsByEventsCmd(),
|
|
authcmd.QueryTxCmd(),
|
|
)
|
|
|
|
simapp.ModuleBasics.AddQueryCommands(cmd)
|
|
cmd.PersistentFlags().String(flags.FlagChainID, "", "The network chain ID")
|
|
|
|
return cmd
|
|
}
|
|
|
|
func txCommand() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "tx",
|
|
Short: "Transactions subcommands",
|
|
DisableFlagParsing: true,
|
|
SuggestionsMinimumDistance: 2,
|
|
RunE: client.ValidateCmd,
|
|
}
|
|
|
|
cmd.AddCommand(
|
|
authcmd.GetSignCommand(),
|
|
authcmd.GetSignBatchCommand(),
|
|
authcmd.GetMultiSignCommand(),
|
|
authcmd.GetValidateSignaturesCommand(),
|
|
flags.LineBreak,
|
|
authcmd.GetBroadcastCommand(),
|
|
authcmd.GetEncodeCommand(),
|
|
authcmd.GetDecodeCommand(),
|
|
flags.LineBreak,
|
|
)
|
|
|
|
simapp.ModuleBasics.AddTxCommands(cmd)
|
|
cmd.PersistentFlags().String(flags.FlagChainID, "", "The network chain ID")
|
|
|
|
return cmd
|
|
}
|
|
|
|
func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, appOpts servertypes.AppOptions) servertypes.Application {
|
|
var cache sdk.MultiStorePersistentCache
|
|
|
|
if cast.ToBool(appOpts.Get(server.FlagInterBlockCache)) {
|
|
cache = store.NewCommitKVStoreCacheManager()
|
|
}
|
|
|
|
skipUpgradeHeights := make(map[int64]bool)
|
|
for _, h := range cast.ToIntSlice(appOpts.Get(server.FlagUnsafeSkipUpgrades)) {
|
|
skipUpgradeHeights[int64(h)] = true
|
|
}
|
|
|
|
pruningOpts, err := server.GetPruningOptionsFromFlags(appOpts)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return simapp.NewSimApp(
|
|
logger, db, traceStore, true, skipUpgradeHeights,
|
|
cast.ToString(appOpts.Get(flags.FlagHome)),
|
|
cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)),
|
|
encodingConfig,
|
|
baseapp.SetPruning(pruningOpts),
|
|
baseapp.SetMinGasPrices(cast.ToString(appOpts.Get(server.FlagMinGasPrices))),
|
|
baseapp.SetHaltHeight(cast.ToUint64(appOpts.Get(server.FlagHaltHeight))),
|
|
baseapp.SetHaltTime(cast.ToUint64(appOpts.Get(server.FlagHaltTime))),
|
|
baseapp.SetInterBlockCache(cache),
|
|
baseapp.SetTrace(cast.ToBool(appOpts.Get(server.FlagTrace))),
|
|
)
|
|
}
|
|
|
|
func exportAppStateAndTMValidators(
|
|
logger log.Logger, db dbm.DB, traceStore io.Writer, height int64, forZeroHeight bool, jailWhiteList []string,
|
|
) (json.RawMessage, []tmtypes.GenesisValidator, *abci.ConsensusParams, error) {
|
|
|
|
encCfg := simapp.MakeEncodingConfig()
|
|
encCfg.Marshaler = codec.NewProtoCodec(encCfg.InterfaceRegistry)
|
|
var simApp *simapp.SimApp
|
|
if height != -1 {
|
|
simApp = simapp.NewSimApp(logger, db, traceStore, false, map[int64]bool{}, "", uint(1), encCfg)
|
|
|
|
if err := simApp.LoadHeight(height); err != nil {
|
|
return nil, nil, nil, err
|
|
}
|
|
} else {
|
|
simApp = simapp.NewSimApp(logger, db, traceStore, true, map[int64]bool{}, "", uint(1), encCfg)
|
|
}
|
|
|
|
return simApp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList)
|
|
}
|
|
|
|
// This is a temporary command middleware to enable proto JSON marshaling for testing.
|
|
// Once proto JSON works everywhere we can remove this and set ProtoCodec as default
|
|
func withProtoJSON(command *cobra.Command) *cobra.Command {
|
|
existing := command.PersistentPreRunE
|
|
if existing != nil {
|
|
command.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
|
|
err := existing(cmd, args)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return setProtoJSON(cmd, args)
|
|
}
|
|
} else {
|
|
command.PersistentPreRunE = setProtoJSON
|
|
}
|
|
return command
|
|
}
|
|
|
|
func setProtoJSON(cmd *cobra.Command, _ []string) error {
|
|
clientCtx := client.GetClientContextFromCmd(cmd)
|
|
clientCtx = clientCtx.WithJSONMarshaler(codec.NewProtoCodec(clientCtx.InterfaceRegistry))
|
|
return client.SetCmdClientContext(cmd, clientCtx)
|
|
}
|