github.com/annchain/OG@v0.0.9/deployment/cmd/gen.go (about) 1 // Copyright © 2019 Annchain Authors <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package cmd 15 16 import ( 17 "fmt" 18 "github.com/annchain/OG/arefactor/common/utilfuncs" 19 "os" 20 "path" 21 "strings" 22 23 "github.com/annchain/OG/account" 24 "github.com/annchain/OG/arefactor/common/io" 25 "github.com/spf13/cobra" 26 "github.com/spf13/viper" 27 ) 28 29 var ( 30 genCmd = &cobra.Command{ 31 Use: "gen", 32 Short: "generate config.toml files for deployment", 33 Run: gen, 34 } 35 //normal bool 36 solo bool 37 private bool 38 privateServer bool 39 embededBootstrap bool 40 nodesNum int 41 ) 42 43 var ( 44 privateServerDir = "private_server" 45 privateDir = "private" 46 soloDir = "solo" 47 mainNetDir = "main_net" 48 privateKeyFile = "privkey" 49 port int = 8000 50 ) 51 52 var configFileName = "config.toml" 53 54 var mainNetBootstrap = "onode://d2469187c351fad31b84f4afb2939cb19c03b7c9359f07447aea3a85664cd33d39afc0c531ad4a8e9ff5ed76b58216e19b0ba208b45d5017ca40c9bd351d29ee@47.100.222.11:8001" 55 var mainNetGenesisPk = "0x0104dbae4756ba4688b1d5637b97e07ce2502a55455f8494665ec9d73dbb3bf65ac0186f042ec77c69df44e2be60a6ab1715b33c852a13d37acc568562b712411ebf;0x01042e4d0369975c98a8880314994e9cbe8f45b1a374c75b98de776f5e9df57793249e7ad16c395d56cad540f660c9e18a04463f3888e4277dffbc317b31f7c61567;0x0104113f6d149d3b5ca450182a6af302aa4c59ae9678189608f4dc3f9139698600abac1b8490613bae74d389ce4266b9dae91caaef26d9b4e7a6ce867c8dfbfdec2d;0x0104062c0ee4be8f4965dd3553f9437b5f53954b391482abf05f7d89c9a1153f86e58b57b4497ca0030dd30658951aa695975c613367191cef16bc3f268f7122fe7d" 56 57 func genInit() { 58 //genCmd.PersistentFlags().BoolVarP(&normal,"normal", "m", true, "normal node that connect to main network") 59 genCmd.PersistentFlags().BoolVarP(&solo, "solo", "s", false, "solo node that use auto client to produce sequencer") 60 genCmd.PersistentFlags().BoolVarP(&private, "private", "p", false, "private nodes that use your own boot-strap nodes") 61 genCmd.PersistentFlags().BoolVarP(&privateServer, "privateserver", "k", false, "private nodes that will use a bootstrap server to decide bootstrap nodes") 62 genCmd.PersistentFlags().BoolVarP(&embededBootstrap, "embeded_bootstrap", "e", true, "if put bootstrap node info inside config") 63 genCmd.PersistentFlags().IntVarP(&nodesNum, "node_num", "n", 4, "the number of nodes that will participate in consensus system。 At least 2.") 64 genCmd.PersistentFlags().IntVarP(&port, "port", "t", 8000, "the port of private network") 65 } 66 67 func privateChainWithServerConfig() { 68 viper.Set("rpc.port", port) 69 viper.Set("p2p.port", port+1) 70 viper.Set("websocket.port", port+2) 71 viper.Set("profiling.port", port+3) 72 viper.Set("annsensus.campaign", true) 73 viper.Set("p2p.bootstrap_config_server", "http://localhost:8008") 74 //generate consensus group keys 75 var privateSet []string 76 for i := 0; i < nodesNum; i++ { 77 priv, _ := account.GenAccount() 78 privateSet = append(privateSet, priv.String()) 79 } 80 81 err := io.MkDirIfNotExists(privateServerDir) 82 if err != nil { 83 fmt.Println(fmt.Sprintf("check and make dir %s error: %v", privateServerDir, err)) 84 return 85 } 86 87 for i := 0; i < len(privateSet); i++ { 88 viper.Set("rpc.port", port+10*i) 89 viper.Set("p2p.port", port+10*i+1) 90 viper.Set("websocket.port", port+10*i+2) 91 viper.Set("profiling.port", port+10*i+3) 92 viper.Set("leveldb.path", fmt.Sprintf("rw/datadir_%d", i)) 93 viper.Set("annsensus.consensus_path", fmt.Sprintf("consensus%d.json", i)) 94 //nodekey, _ := genBootONode(port + 10*i + 1) 95 fmt.Println("private key: ", i) 96 97 configDir := path.Join(privateServerDir, fmt.Sprintf("/node_%d", i)) 98 err = io.MkDirIfNotExists(configDir) 99 if err != nil { 100 fmt.Println(fmt.Sprintf("check and make dir %s error: %v", configDir, err)) 101 return 102 } 103 account.SavePrivateKey(path.Join(configDir, privateKeyFile), privateSet[i]) 104 //viper.Set("dag.my_private_key", privateSet[i]) 105 err = viper.WriteConfigAs(path.Join(configDir, configFileName)) 106 utilfuncs.PanicIfError(err, "error on dump config") 107 io.CopyFile("genesis.json", path.Join(configDir, "genesis.json")) 108 } 109 } 110 111 func privateChainConfig() { 112 nodekeyBoot, nodeBoot := genBootONode(port + 1) 113 if embededBootstrap { 114 viper.Set("p2p.bootstrap_nodes", nodeBoot) 115 } 116 viper.Set("rpc.port", port) 117 viper.Set("p2p.port", port+1) 118 viper.Set("websocket.port", port+2) 119 viper.Set("profiling.port", port+3) 120 121 //generate consensus group keys 122 var privateSet []string 123 var publicSet []string 124 for i := 0; i < nodesNum; i++ { 125 priv, pub := account.GenAccount() 126 privateSet = append(privateSet, priv.String()) 127 publicSet = append(publicSet, pub.String()) 128 } 129 genesisPk := strings.Join(publicSet, ";") 130 viper.Set("annsensus.genesis_pk", genesisPk) 131 viper.Set("annsensus.campaign", true) 132 viper.Set("annsensus.partner_number", nodesNum) 133 viper.Set("annsensus.threshold", 2*nodesNum/3+1) 134 135 err := io.MkDirIfNotExists(privateDir) 136 if err != nil { 137 fmt.Println(fmt.Sprintf("check and make dir %s error: %v", privateDir, err)) 138 return 139 } 140 141 privateDirNode0 := path.Join(privateDir, "node_0") 142 err = io.MkDirIfNotExists(privateDirNode0) 143 if err != nil { 144 fmt.Println(fmt.Sprintf("check and make dir %s error: %v", privateDirNode0, err)) 145 return 146 } 147 // init private key 148 // viper.Set("dag.my_private_key", privateSet[0]) 149 account.SavePrivateKey(path.Join(privateDirNode0, privateKeyFile), privateSet[0]) 150 151 //init bootstrap 152 viper.Set("p2p.node_key", nodekeyBoot) 153 viper.Set("p2p.bootstrap_node", true) 154 viper.Set("leveldb.path", "rw/datadir_0") 155 viper.Set("annsensus.consensus_path", "consensus0.json") 156 err = viper.WriteConfigAs(path.Join(privateDirNode0, configFileName)) 157 utilfuncs.PanicIfError(err, "error on dump config") 158 159 // copy genesis 160 io.CopyFile("genesis.json", path.Join(privateDirNode0, "genesis.json")) 161 162 //init other nodes 163 viper.Set("annsensus.campaign", true) 164 viper.Set("p2p.bootstrap_node", false) 165 for i := 1; i < len(privateSet); i++ { 166 viper.Set("rpc.port", port+10*i) 167 viper.Set("p2p.port", port+10*i+1) 168 viper.Set("websocket.port", port+10*i+2) 169 viper.Set("profiling.port", port+10*i+3) 170 viper.Set("leveldb.path", fmt.Sprintf("rw/datadir_%d", i)) 171 viper.Set("annsensus.consensus_path", fmt.Sprintf("consensus%d.json", i)) 172 nodekey, _ := genBootONode(port + 10*i + 1) 173 viper.Set("p2p.node_key", nodekey) 174 fmt.Println("private key: ", i) 175 176 configDir := path.Join(privateDir, fmt.Sprintf("/node_%d", i)) 177 err = io.MkDirIfNotExists(configDir) 178 if err != nil { 179 fmt.Println(fmt.Sprintf("check and make dir %s error: %v", configDir, err)) 180 return 181 } 182 account.SavePrivateKey(path.Join(configDir, privateKeyFile), privateSet[i]) 183 //viper.Set("dag.my_private_key", privateSet[i]) 184 err = viper.WriteConfigAs(path.Join(configDir, configFileName)) 185 utilfuncs.PanicIfError(err, "error on dump config") 186 io.CopyFile("genesis.json", path.Join(configDir, "genesis.json")) 187 } 188 } 189 190 func soloChainConfig() { 191 nodekeyBoot, nodeBoot := genBootONode(port + 1) 192 if embededBootstrap { 193 viper.Set("p2p.bootstrap_nodes", nodeBoot) 194 } 195 196 viper.Set("p2p.node_key", nodekeyBoot) 197 viper.Set("annsensus.disable", true) 198 viper.Set("auto_client.sequencer.enabled", true) 199 viper.Set("p2p.bootstrap_node", true) 200 201 priv, _ := account.GenAccount() 202 203 viper.Set("rpc.port", port) 204 viper.Set("annsensus.campaign", false) 205 viper.Set("annsensus.disable", true) 206 viper.Set("p2p.port", port+1) 207 viper.Set("websocket.port", port+2) 208 viper.Set("profiling.port", port+3) 209 err := io.MkDirIfNotExists(soloDir) 210 if err != nil { 211 fmt.Println(fmt.Sprintf("check and make dir %s error: %v", soloDir, err)) 212 return 213 } 214 215 // viper.Set("dag.my_private_key", priv.String()) 216 account.SavePrivateKey(path.Join(soloDir, privateKeyFile), priv.String()) 217 err = viper.WriteConfigAs(path.Join(soloDir, configFileName)) 218 utilfuncs.PanicIfError(err, "error on dump config") 219 io.CopyFile("genesis.json", path.Join(soloDir, "genesis.json")) 220 } 221 222 func mainChainConfig() { 223 if embededBootstrap { 224 viper.Set("p2p.bootstrap_nodes", mainNetBootstrap) 225 } 226 227 viper.Set("annsensus.genesis_pk", mainNetGenesisPk) 228 229 priv, _ := account.GenAccount() 230 viper.Set("rpc.port", port) 231 viper.Set("p2p.port", port+1) 232 viper.Set("websocket.port", port+2) 233 viper.Set("profiling.port", port+3) 234 235 err := io.MkDirIfNotExists(mainNetDir) 236 if err != nil { 237 fmt.Println(fmt.Sprintf("check and make dir %s error: %v", mainNetDir, err)) 238 return 239 } 240 241 //viper.Set("dag.my_private_key", priv.String()) 242 account.SavePrivateKey(path.Join(mainNetDir, privateKeyFile), priv.String()) 243 err = viper.WriteConfigAs(path.Join(mainNetDir, configFileName)) 244 utilfuncs.PanicIfError(err, "error on dump config") 245 io.CopyFile("genesis.json", path.Join(mainNetDir, "genesis.json")) 246 } 247 248 func gen(cmd *cobra.Command, args []string) { 249 readConfig() 250 if privateServer { 251 privateChainWithServerConfig() 252 } else if private { 253 privateChainConfig() 254 } else if solo { 255 soloChainConfig() 256 } else { 257 mainChainConfig() 258 } 259 } 260 261 func readConfig() { 262 file, err := os.Open(configFileName) 263 utilfuncs.PanicIfError(err, "open file error") 264 defer file.Close() 265 266 viper.SetConfigType("toml") 267 err = viper.MergeConfig(file) 268 269 utilfuncs.PanicIfError(err, "merge viper config err") 270 }