Skip to content

Commit

Permalink
Merge PR cosmos#6935: Fix Testnet Command
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored Aug 4, 2020
1 parent 79cee06 commit 801b648
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
64 changes: 31 additions & 33 deletions simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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,
)
},
Expand All @@ -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,
Expand Down Expand Up @@ -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),
Expand All @@ -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 {
Expand All @@ -253,33 +253,33 @@ 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 {
return err
}

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
}
Expand All @@ -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
Expand All @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions x/auth/tx/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 801b648

Please sign in to comment.