155 lines
4.5 KiB
Go
155 lines
4.5 KiB
Go
//go:build !app_v1
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"cosmossdk.io/client/v2/autocli"
|
|
clientv2keyring "cosmossdk.io/client/v2/autocli/keyring"
|
|
"cosmossdk.io/core/address"
|
|
"cosmossdk.io/depinject"
|
|
"cosmossdk.io/log"
|
|
"cosmossdk.io/simapp"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/client/config"
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
"github.com/cosmos/cosmos-sdk/runtime"
|
|
"github.com/cosmos/cosmos-sdk/server"
|
|
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
|
"github.com/cosmos/cosmos-sdk/types/tx/signing"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/tx"
|
|
txmodule "github.com/cosmos/cosmos-sdk/x/auth/tx/config"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
)
|
|
|
|
// NewRootCmd creates a new root command for simd. It is called once in the main function.
|
|
func NewRootCmd() *cobra.Command {
|
|
var (
|
|
txConfigOpts tx.ConfigOptions
|
|
autoCliOpts autocli.AppOptions
|
|
moduleBasicManager module.BasicManager
|
|
initClientCtx *client.Context
|
|
)
|
|
|
|
if err := depinject.Inject(
|
|
depinject.Configs(simapp.AppConfig,
|
|
depinject.Supply(
|
|
log.NewNopLogger(),
|
|
simtestutil.NewAppOptionsWithFlagHome(tempDir()),
|
|
),
|
|
depinject.Provide(
|
|
ProvideClientContext,
|
|
ProvideKeyring,
|
|
),
|
|
),
|
|
&txConfigOpts,
|
|
&autoCliOpts,
|
|
&moduleBasicManager,
|
|
&initClientCtx,
|
|
); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
rootCmd := &cobra.Command{
|
|
Use: "simd",
|
|
Short: "simulation app",
|
|
SilenceErrors: true,
|
|
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
|
|
// set the default command outputs
|
|
cmd.SetOut(cmd.OutOrStdout())
|
|
cmd.SetErr(cmd.ErrOrStderr())
|
|
|
|
clientCtx := *initClientCtx
|
|
clientCtx = clientCtx.WithCmdContext(cmd.Context())
|
|
clientCtx, err := client.ReadPersistentCommandFlags(clientCtx, cmd.Flags())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
customClientTemplate, customClientConfig := initClientConfig()
|
|
clientCtx, err = config.CreateClientConfig(clientCtx, customClientTemplate, customClientConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// This needs to go after CreateClientConfig, as that function sets the RPC client needed for SIGN_MODE_TEXTUAL.
|
|
txConfigOpts.EnabledSignModes = append(txConfigOpts.EnabledSignModes, signing.SignMode_SIGN_MODE_TEXTUAL)
|
|
txConfigOpts.TextualCoinMetadataQueryFn = txmodule.NewGRPCCoinMetadataQueryFn(clientCtx)
|
|
txConfigWithTextual, err := tx.NewTxConfigWithOptions(
|
|
codec.NewProtoCodec(clientCtx.InterfaceRegistry),
|
|
txConfigOpts,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
clientCtx = clientCtx.WithTxConfig(txConfigWithTextual)
|
|
|
|
if err := client.SetCmdClientContextHandler(clientCtx, cmd); err != nil {
|
|
return err
|
|
}
|
|
|
|
customAppTemplate, customAppConfig := initAppConfig()
|
|
customCMTConfig := initCometBFTConfig()
|
|
|
|
return server.InterceptConfigsPreRunHandler(cmd, customAppTemplate, customAppConfig, customCMTConfig)
|
|
},
|
|
}
|
|
|
|
initRootCmd(rootCmd, initClientCtx.TxConfig, initClientCtx.InterfaceRegistry, initClientCtx.Codec, moduleBasicManager)
|
|
|
|
if err := autoCliOpts.EnhanceRootCommand(rootCmd); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return rootCmd
|
|
}
|
|
|
|
func ProvideClientContext(
|
|
appCodec codec.Codec,
|
|
interfaceRegistry codectypes.InterfaceRegistry,
|
|
txConfig client.TxConfig,
|
|
legacyAmino *codec.LegacyAmino,
|
|
addressCodec address.Codec,
|
|
validatorAddressCodec runtime.ValidatorAddressCodec,
|
|
consensusAddressCodec runtime.ConsensusAddressCodec,
|
|
) *client.Context {
|
|
var err error
|
|
|
|
clientCtx := client.Context{}.
|
|
WithCodec(appCodec).
|
|
WithInterfaceRegistry(interfaceRegistry).
|
|
WithTxConfig(txConfig).
|
|
WithLegacyAmino(legacyAmino).
|
|
WithInput(os.Stdin).
|
|
WithAccountRetriever(types.AccountRetriever{}).
|
|
WithAddressCodec(addressCodec).
|
|
WithValidatorAddressCodec(validatorAddressCodec).
|
|
WithConsensusAddressCodec(consensusAddressCodec).
|
|
WithHomeDir(simapp.DefaultNodeHome).
|
|
WithViper("") // In simapp, we don't use any prefix for env variables.
|
|
|
|
// Read the config to overwrite the default values with the values from the config file
|
|
customClientTemplate, customClientConfig := initClientConfig()
|
|
clientCtx, err = config.CreateClientConfig(clientCtx, customClientTemplate, customClientConfig)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return &clientCtx
|
|
}
|
|
|
|
func ProvideKeyring(clientCtx *client.Context, addressCodec address.Codec) (clientv2keyring.Keyring, error) {
|
|
kb, err := client.NewKeyringFromBackend(*clientCtx, clientCtx.Keyring.Backend())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return kb, nil
|
|
}
|