2019-05-16 08:25:32 -07:00
package cli
import (
"encoding/json"
"path/filepath"
2019-08-30 06:59:41 -07:00
"github.com/pkg/errors"
2019-05-16 08:25:32 -07:00
"github.com/spf13/cobra"
tmtypes "github.com/tendermint/tendermint/types"
2020-07-07 08:40:46 -07:00
"github.com/cosmos/cosmos-sdk/client"
2020-07-08 10:57:45 -07:00
"github.com/cosmos/cosmos-sdk/client/flags"
2019-05-16 08:25:32 -07:00
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/x/genutil"
2019-07-24 14:37:03 -07:00
"github.com/cosmos/cosmos-sdk/x/genutil/types"
2019-05-16 08:25:32 -07:00
)
const flagGenTxDir = "gentx-dir"
// CollectGenTxsCmd - return the cobra command to collect genesis transactions
2020-07-17 07:20:31 -07:00
func CollectGenTxsCmd ( genBalIterator types . GenesisBalancesIterator , defaultNodeHome string ) * cobra . Command {
2019-05-16 08:25:32 -07:00
cmd := & cobra . Command {
Use : "collect-gentxs" ,
Short : "Collect genesis txs and output a genesis.json file" ,
2020-07-06 14:48:54 -07:00
RunE : func ( cmd * cobra . Command , _ [ ] string ) error {
2020-07-07 08:40:46 -07:00
serverCtx := server . GetServerContextFromCmd ( cmd )
config := serverCtx . Config
clientCtx := client . GetClientContextFromCmd ( cmd )
2021-06-11 04:49:39 -07:00
cdc := clientCtx . Codec
2020-07-07 08:40:46 -07:00
2020-07-07 10:20:09 -07:00
config . SetRoot ( clientCtx . HomeDir )
2020-07-07 08:40:46 -07:00
2019-05-16 08:25:32 -07:00
nodeID , valPubKey , err := genutil . InitializeNodeValidatorFiles ( config )
if err != nil {
2019-08-30 06:59:41 -07:00
return errors . Wrap ( err , "failed to initialize node validator files" )
2019-05-16 08:25:32 -07:00
}
genDoc , err := tmtypes . GenesisDocFromFile ( config . GenesisFile ( ) )
if err != nil {
2019-08-30 06:59:41 -07:00
return errors . Wrap ( err , "failed to read genesis doc from file" )
2019-05-16 08:25:32 -07:00
}
2020-07-06 14:48:54 -07:00
genTxDir , _ := cmd . Flags ( ) . GetString ( flagGenTxDir )
genTxsDir := genTxDir
2019-05-16 08:25:32 -07:00
if genTxsDir == "" {
genTxsDir = filepath . Join ( config . RootDir , "config" , "gentx" )
}
toPrint := newPrintInfo ( config . Moniker , genDoc . ChainID , nodeID , genTxsDir , json . RawMessage ( "" ) )
2020-07-06 14:48:54 -07:00
initCfg := types . NewInitConfig ( genDoc . ChainID , genTxsDir , nodeID , valPubKey )
2019-05-16 08:25:32 -07:00
2020-07-25 01:10:04 -07:00
appMessage , err := genutil . GenAppStateFromConfig ( cdc ,
clientCtx . TxConfig ,
config , initCfg , * genDoc , genBalIterator )
2019-05-16 08:25:32 -07:00
if err != nil {
2019-08-30 06:59:41 -07:00
return errors . Wrap ( err , "failed to get genesis app state from config" )
2019-05-16 08:25:32 -07:00
}
toPrint . AppMessage = appMessage
2020-08-15 01:52:33 -07:00
return displayInfo ( toPrint )
2019-05-16 08:25:32 -07:00
} ,
}
2020-07-17 07:20:31 -07:00
cmd . Flags ( ) . String ( flags . FlagHome , defaultNodeHome , "The application home directory" )
2020-07-07 08:40:46 -07:00
cmd . Flags ( ) . String ( flagGenTxDir , "" , "override default \"gentx\" directory from which collect and execute genesis transactions; default [--home]/config/gentx/" )
2020-05-02 12:26:59 -07:00
2019-05-16 08:25:32 -07:00
return cmd
}