github.com/aergoio/aergo@v1.3.1/cmd/aergosvr/init.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/aergoio/aergo/consensus/impl" 7 "os" 8 9 "github.com/aergoio/aergo/chain" 10 "github.com/aergoio/aergo/internal/enc" 11 "github.com/aergoio/aergo/types" 12 "github.com/spf13/cobra" 13 ) 14 15 var ( 16 testNet bool 17 jsonGenesis string 18 ) 19 20 func init() { 21 initGenesis.Flags().BoolVar(&testNet, "testnet", false, "create genesis block for Aergo TestNet") 22 initGenesis.Flags().StringVar(&jsonGenesis, "genesis", "", "genesis json file for private net") 23 24 rootCmd.AddCommand(initGenesis) 25 } 26 27 var initGenesis = &cobra.Command{ 28 Use: "init", 29 Short: "Create genesis block", 30 Args: cobra.NoArgs, 31 Run: func(cmd *cobra.Command, args []string) { 32 33 var genesis *types.Genesis 34 35 core := getCore(cfg.DataDir) 36 if core != nil { 37 exist := core.GetGenesisInfo() 38 if exist != nil { 39 fmt.Printf("genesis block(%s) is already initialized\n", enc.ToString(exist.Block().GetHash())) 40 core.Close() 41 return 42 } 43 } 44 45 if jsonGenesis != "" { 46 fmt.Println("create genesis block for PrivateNet") 47 genesis = getGenesis(jsonGenesis) 48 if genesis == nil { 49 fmt.Printf("failed to obtain GenesisInfo\n") 50 return 51 } 52 53 if err := impl.ValidateGenesis(genesis); err != nil { 54 fmt.Printf(" %s (error:%s)\n", jsonGenesis, err) 55 return 56 } 57 } 58 59 if genesis == nil { 60 if testNet == false { 61 fmt.Println("create genesis block for Aergo Mainnet") 62 } else { 63 fmt.Println("create genesis block for Aergo Testnet") 64 } 65 } 66 67 if core != nil { 68 err := core.InitGenesisBlock(genesis, !testNet) 69 if err != nil { 70 fmt.Printf("fail to init genesis block data (error:%s)\n", err) 71 } 72 73 g := core.GetGenesisInfo() 74 fmt.Printf("genesis block[%s] is created in (%s)\n", enc.ToString(g.Block().GetHash()), cfg.DataDir) 75 } 76 }, 77 } 78 79 func getGenesis(path string) *types.Genesis { 80 file, err := os.Open(jsonGenesis) 81 if err != nil { 82 fmt.Printf("fail to open %s \n", jsonGenesis) 83 return nil 84 } 85 defer file.Close() 86 genesis := new(types.Genesis) 87 if err := json.NewDecoder(file).Decode(genesis); err != nil { 88 fmt.Printf("fail to deserialize %s (error:%s)\n", jsonGenesis, err) 89 return nil 90 } 91 return genesis 92 } 93 94 func getCore(dataDir string) *chain.Core { 95 // if initpath is feeded, gaurantee initpath is accessible directory 96 fi, err := os.Stat(dataDir) 97 if err == nil && !fi.IsDir() { 98 fmt.Printf("%s is not a directory\n", dataDir) 99 return nil 100 } 101 if err != nil { 102 if !os.IsNotExist(err) { 103 fmt.Printf("cannot access %s(error:%s)\n", dataDir, err) 104 return nil 105 } 106 107 err := os.MkdirAll(dataDir, 0755) 108 if err != nil { 109 fmt.Printf("fail to create %s (error:%s)\n", dataDir, err) 110 return nil 111 } 112 } 113 114 core, err := chain.NewCore(cfg.DbType, dataDir, false, 0) 115 if err != nil { 116 fmt.Printf("fail to init a blockchain core (error:%s)\n", err) 117 return nil 118 } 119 120 return core 121 }