From 801b64868dd1c0ab8296d97a78fe85159fca40c3 Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Tue, 4 Aug 2020 12:51:27 -0400 Subject: [PATCH] Merge PR #6935: Fix Testnet Command --- simapp/simd/cmd/testnet.go | 64 ++++++++++++++++++-------------------- x/auth/tx/decoder.go | 3 +- 2 files changed, 32 insertions(+), 35 deletions(-) diff --git a/simapp/simd/cmd/testnet.go b/simapp/simd/cmd/testnet.go index 340478f1cb89..b74f76b62ffc 100644 --- a/simapp/simd/cmd/testnet.go +++ b/simapp/simd/cmd/testnet.go @@ -21,6 +21,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" clientkeys "github.com/cosmos/cosmos-sdk/client/keys" + "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/server" @@ -58,7 +59,6 @@ Example: `, RunE: func(cmd *cobra.Command, _ []string) error { clientCtx := client.GetClientContextFromCmd(cmd) - cdc := clientCtx.JSONMarshaler serverCtx := server.GetServerContextFromCmd(cmd) config := serverCtx.Config @@ -74,7 +74,7 @@ Example: numValidators, _ := cmd.Flags().GetInt(flagNumValidators) return InitTestnet( - cmd, config, cdc, clientCtx.TxConfig, mbm, genBalIterator, outputDir, chainID, minGasPrices, + clientCtx, cmd, config, mbm, genBalIterator, outputDir, chainID, minGasPrices, nodeDirPrefix, nodeDaemonHome, nodeCLIHome, startingIPAddress, keyringBackend, numValidators, ) }, @@ -97,8 +97,7 @@ const nodeDirPerm = 0755 // Initialize the testnet func InitTestnet( - cmd *cobra.Command, nodeConfig *tmconfig.Config, cdc codec.JSONMarshaler, - txEncodingConfig client.TxEncodingConfig, + clientCtx client.Context, cmd *cobra.Command, nodeConfig *tmconfig.Config, mbm module.BasicManager, genBalIterator banktypes.GenesisBalancesIterator, outputDir, chainID, minGasPrices, nodeDirPrefix, nodeDaemonHome, nodeCLIHome, startingIPAddress, keyringBackend string, numValidators int, @@ -198,7 +197,7 @@ func InitTestnet( genAccounts = append(genAccounts, authtypes.NewBaseAccount(addr, nil, 0, 0)) valTokens := sdk.TokensFromConsensusPower(100) - msg := stakingtypes.NewMsgCreateValidator( + createValMsg := stakingtypes.NewMsgCreateValidator( sdk.ValAddress(addr), valPubKeys[i], sdk.NewCoin(sdk.DefaultBondDenom, valTokens), @@ -207,41 +206,42 @@ func InitTestnet( sdk.OneInt(), ) - tx := authtypes.NewStdTx([]sdk.Msg{msg}, authtypes.StdFee{}, []authtypes.StdSignature{}, memo) //nolint:staticcheck // SA1019: authtypes.StdFee is deprecated - txBldr, err := authtypes.NewTxBuilderFromFlags(inBuf, cmd.Flags(), clientDir) - if err != nil { - return fmt.Errorf("error creating tx: %w", err) + txBuilder := clientCtx.TxConfig.NewTxBuilder() + if err := txBuilder.SetMsgs(createValMsg); err != nil { + return err } - txBldr = txBldr.WithChainID(chainID).WithMemo(memo).WithKeybase(kb) + txBuilder.SetMemo(memo) - signedTx, err := txBldr.SignStdTx(nodeDirName, tx, false) - if err != nil { - _ = os.RemoveAll(outputDir) + txFactory := tx.Factory{} + txFactory = txFactory. + WithChainID(chainID). + WithMemo(memo). + WithKeybase(kb). + WithTxConfig(clientCtx.TxConfig) + + if err := tx.Sign(txFactory, nodeDirName, txBuilder); err != nil { return err } - txBytes, err := cdc.MarshalJSON(signedTx) + txBz, err := clientCtx.TxConfig.TxJSONEncoder()(txBuilder.GetTx()) if err != nil { - _ = os.RemoveAll(outputDir) return err } - // gather gentxs folder - if err := writeFile(fmt.Sprintf("%v.json", nodeDirName), gentxsDir, txBytes); err != nil { - _ = os.RemoveAll(outputDir) + if err := writeFile(fmt.Sprintf("%v.json", nodeDirName), gentxsDir, txBz); err != nil { return err } srvconfig.WriteConfigFile(filepath.Join(nodeDir, "config/app.toml"), simappConfig) } - if err := initGenFiles(cdc, mbm, chainID, genAccounts, genBalances, genFiles, numValidators); err != nil { + if err := initGenFiles(clientCtx, mbm, chainID, genAccounts, genBalances, genFiles, numValidators); err != nil { return err } err := collectGenFiles( - cdc, txEncodingConfig, nodeConfig, chainID, nodeIDs, valPubKeys, numValidators, + clientCtx, nodeConfig, chainID, nodeIDs, valPubKeys, numValidators, outputDir, nodeDirPrefix, nodeDaemonHome, genBalIterator, ) if err != nil { @@ -253,16 +253,16 @@ func InitTestnet( } func initGenFiles( - cdc codec.JSONMarshaler, mbm module.BasicManager, chainID string, + clientCtx client.Context, mbm module.BasicManager, chainID string, genAccounts []authtypes.GenesisAccount, genBalances []banktypes.Balance, genFiles []string, numValidators int, ) error { - appGenState := mbm.DefaultGenesis(cdc) + appGenState := mbm.DefaultGenesis(clientCtx.JSONMarshaler) // set the accounts in the genesis state var authGenState authtypes.GenesisState - cdc.MustUnmarshalJSON(appGenState[authtypes.ModuleName], &authGenState) + clientCtx.JSONMarshaler.MustUnmarshalJSON(appGenState[authtypes.ModuleName], &authGenState) accounts, err := authtypes.PackAccounts(genAccounts) if err != nil { @@ -270,16 +270,16 @@ func initGenFiles( } authGenState.Accounts = accounts - appGenState[authtypes.ModuleName] = cdc.MustMarshalJSON(authGenState) + appGenState[authtypes.ModuleName] = clientCtx.JSONMarshaler.MustMarshalJSON(authGenState) // set the balances in the genesis state var bankGenState banktypes.GenesisState - cdc.MustUnmarshalJSON(appGenState[banktypes.ModuleName], &bankGenState) + clientCtx.JSONMarshaler.MustUnmarshalJSON(appGenState[banktypes.ModuleName], &bankGenState) bankGenState.Balances = genBalances - appGenState[banktypes.ModuleName] = cdc.MustMarshalJSON(bankGenState) + appGenState[banktypes.ModuleName] = clientCtx.JSONMarshaler.MustMarshalJSON(bankGenState) - appGenStateJSON, err := codec.MarshalJSONIndent(cdc, appGenState) + appGenStateJSON, err := codec.MarshalJSONIndent(clientCtx.JSONMarshaler, appGenState) if err != nil { return err } @@ -300,11 +300,9 @@ func initGenFiles( } func collectGenFiles( - cdc codec.JSONMarshaler, txEncodingConfig client.TxEncodingConfig, - nodeConfig *tmconfig.Config, chainID string, - nodeIDs []string, valPubKeys []crypto.PubKey, - numValidators int, outputDir, nodeDirPrefix, nodeDaemonHome string, - genBalIterator banktypes.GenesisBalancesIterator, + clientCtx client.Context, nodeConfig *tmconfig.Config, chainID string, + nodeIDs []string, valPubKeys []crypto.PubKey, numValidators int, + outputDir, nodeDirPrefix, nodeDaemonHome string, genBalIterator banktypes.GenesisBalancesIterator, ) error { var appState json.RawMessage @@ -326,7 +324,7 @@ func collectGenFiles( return err } - nodeAppState, err := genutil.GenAppStateFromConfig(cdc, txEncodingConfig, nodeConfig, initCfg, *genDoc, genBalIterator) + nodeAppState, err := genutil.GenAppStateFromConfig(clientCtx.JSONMarshaler, clientCtx.TxConfig, nodeConfig, initCfg, *genDoc, genBalIterator) if err != nil { return err } diff --git a/x/auth/tx/decoder.go b/x/auth/tx/decoder.go index 908c6e433c11..2aa560b8943f 100644 --- a/x/auth/tx/decoder.go +++ b/x/auth/tx/decoder.go @@ -3,9 +3,8 @@ package tx import ( "github.com/tendermint/tendermint/crypto" - "github.com/cosmos/cosmos-sdk/codec/unknownproto" - "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/unknownproto" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"