github.com/lino-network/lino@v0.6.11/app/init.go (about) 1 package app 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "path/filepath" 7 "time" 8 9 "github.com/cosmos/cosmos-sdk/client" 10 "github.com/cosmos/cosmos-sdk/codec" 11 "github.com/cosmos/cosmos-sdk/server" 12 genutil "github.com/cosmos/cosmos-sdk/x/genutil" 13 "github.com/spf13/cobra" 14 "github.com/spf13/viper" 15 cfg "github.com/tendermint/tendermint/config" 16 "github.com/tendermint/tendermint/libs/cli" 17 tmcli "github.com/tendermint/tendermint/libs/cli" 18 "github.com/tendermint/tendermint/libs/common" 19 tmtypes "github.com/tendermint/tendermint/types" 20 // sdk "github.com/cosmos/cosmos-sdk/types" 21 ) 22 23 const ( 24 flagOverwrite = "overwrite" 25 ) 26 27 // InitCmd initializes all files for tendermint and application 28 // XXX(yumin): after upgrade-1, we deprecated previous init function and start to use 29 // cosmos gaia init. 30 func InitCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { 31 cmd := &cobra.Command{ 32 Use: "init", 33 Short: "Initialize genesis config, priv-validator file, and p2p-node file", 34 Args: cobra.NoArgs, 35 RunE: func(_ *cobra.Command, _ []string) error { 36 config := ctx.Config 37 config.SetRoot(viper.GetString(tmcli.HomeFlag)) 38 39 chainID := viper.GetString(client.FlagChainID) 40 if chainID == "" { 41 chainID = fmt.Sprintf("test-chain-%v", common.RandStr(6)) 42 } 43 44 // gen pubkey 45 _, pk, err := genutil.InitializeNodeValidatorFiles(config) 46 if err != nil { 47 return err 48 } 49 50 genFile := config.GenesisFile() 51 if !viper.GetBool(flagOverwrite) && common.FileExists(genFile) { 52 return fmt.Errorf("genesis.json file already exists: %v", genFile) 53 } 54 55 // XXX(yumin): generate genesis file from app state. 56 appGenTx, _, validator, err := LinoBlockchainGenTx(cdc, pk) 57 if err != nil { 58 return err 59 } 60 61 appState, err := LinoBlockchainGenState(cdc, []json.RawMessage{appGenTx}) 62 if err != nil { 63 return err 64 } 65 66 // TODO(yumin): this is broken and we need update this part. 67 if err = genutil.ExportGenesisFile( 68 &tmtypes.GenesisDoc{ 69 GenesisTime: time.Now(), 70 ChainID: chainID, 71 ConsensusParams: nil, 72 Validators: []tmtypes.GenesisValidator{validator}, 73 AppHash: []byte(""), 74 AppState: appState, 75 }, 76 genFile); err != nil { 77 return err 78 } 79 80 cfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config) 81 82 fmt.Printf("Initialized lino configuration and bootstrapping files in %s...\n", viper.GetString(cli.HomeFlag)) 83 return nil 84 }, 85 } 86 87 cmd.Flags().String(cli.HomeFlag, DefaultNodeHome, "node's home directory") 88 cmd.Flags().String(client.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created") 89 cmd.Flags().BoolP(flagOverwrite, "o", false, "overwrite the genesis.json file") 90 91 return cmd 92 }