// NOTE: this is totally unsafe. // it's only suitable for testnets. func reset_priv_validator() { // Get PrivValidator var privValidator *types.PrivValidator privValidatorFile := config.GetString("priv_validator_file") if _, err := os.Stat(privValidatorFile); err == nil { privValidator = types.LoadPrivValidator(privValidatorFile) privValidator.LastHeight = 0 privValidator.LastRound = 0 privValidator.LastStep = 0 privValidator.Save() log.Notice("Reset PrivValidator", "file", privValidatorFile) } else { privValidator = types.GenPrivValidator() privValidator.SetFile(privValidatorFile) privValidator.Save() log.Notice("Generated PrivValidator", "file", privValidatorFile) } }
// initialize a new validator set func cmdValidatorsInit(c *cli.Context) { args := c.Args() if len(args) != 1 { cli.ShowAppHelp(c) return } base := args[0] N := c.Int("N") vals := make([]*Validator, N) // Initialize priv_validator.json's for i := 0; i < N; i++ { err := initValDirectory(base, i) if err != nil { Exit(err.Error()) } // Read priv_validator.json to populate vals name := fmt.Sprintf("val%d", i) privValFile := path.Join(base, name, "priv_validator.json") privVal := tmtypes.LoadPrivValidator(privValFile) vals[i] = &Validator{ ID: name, PubKey: privVal.PubKey, } } valSet := ValidatorSet{ ID: path.Base(base), Validators: vals, } // write the validator set file b := wire.JSONBytesPretty(valSet) err := WriteFile(path.Join(base, "validator_set.json"), b, 0444) if err != nil { Exit(err.Error()) } fmt.Println(Fmt("Successfully initialized %v validators", N)) }
// Initialize directories for each node func cmdChainInit(c *cli.Context) { args := c.Args() if len(args) != 1 { cli.ShowAppHelp(c) return } base := args[0] machines := ParseMachines(c.GlobalString("machines")) app := c.String("app") var appHash []byte appHashString := c.String("app-hash") if appHashString != "" { if len(appHashString) >= 2 && appHashString[:2] == "0x" { var err error appHash, err = hex.DecodeString(appHashString[2:]) if err != nil { Exit(err.Error()) } } else { appHash = []byte(appHashString) } } err := initDataDirectory(base) if err != nil { Exit(err.Error()) } err = initAppDirectory(base, app) if err != nil { Exit(err.Error()) } err = initCoreDirectory(base) if err != nil { Exit(err.Error()) } genVals := make([]tmtypes.GenesisValidator, len(machines)) var valSetID string valSetDir := c.String("validator-set") if valSetDir != "" { // validator-set name is the last element of the path valSetID = path.Base(valSetDir) var valSet ValidatorSet err := ReadJSONFile(&valSet, path.Join(valSetDir, "validator_set.json")) if err != nil { Exit(err.Error()) } vals := valSet.Validators if len(machines) != len(vals) { Exit(fmt.Sprintf("Validator set size must match number of machines. Got %d validators, %d machines", len(vals), len(machines))) } for i, val := range vals { // build the directory mach := machines[i] err := initMachCoreDirectory(base, mach) if err != nil { Exit(err.Error()) } // overwrite the priv validator privValFile := path.Join(valSetDir, val.ID, "priv_validator.json") privVal := tmtypes.LoadPrivValidator(privValFile) privVal.SetFile(path.Join(base, mach, "core", "priv_validator.json")) privVal.Save() } // copy the vals into genVals for i, val := range vals { genVals[i] = tmtypes.GenesisValidator{ Name: val.ID, PubKey: val.PubKey, Amount: 1, // TODO } } } else { valSetID = ValSetAnon // Initialize core dir and priv_validator.json's for i, mach := range machines { err := initMachCoreDirectory(base, mach) if err != nil { Exit(err.Error()) } // Read priv_validator.json to populate vals privValFile := path.Join(base, mach, "core", "priv_validator.json") privVal := tmtypes.LoadPrivValidator(privValFile) genVals[i] = tmtypes.GenesisValidator{ PubKey: privVal.PubKey, Amount: 1, Name: mach, } } } // Generate genesis doc from generated validators genDoc := &tmtypes.GenesisDoc{ GenesisTime: time.Now(), ChainID: "chain-" + RandStr(6), Validators: genVals, AppHash: appHash, } // Write genesis file. for _, mach := range machines { genDoc.SaveAs(path.Join(base, mach, "core", "genesis.json")) } // Write genesis file. for _, mach := range machines { genDoc.SaveAs(path.Join(base, mach, "core", "genesis.json")) } // write the chain meta data (ie. validator set name and validators) blockchainCfg := &BlockchainInfo{ ValSetID: valSetID, Validators: make([]*CoreInfo, len(genVals)), } for i, v := range genVals { blockchainCfg.Validators[i] = &CoreInfo{ Validator: &Validator{ID: v.Name, PubKey: v.PubKey}, Index: i, // XXX: we may want more control here } } err = WriteBlockchainInfo(base, blockchainCfg) if err != nil { Exit(err.Error()) } fmt.Println(Fmt("Successfully initialized %v node directories", len(machines))) }
func NewNode() *Node { // Get BlockStore blockStoreDB := dbm.GetDB("blockstore") blockStore := bc.NewBlockStore(blockStoreDB) // Get State stateDB := dbm.GetDB("state") state := sm.LoadState(stateDB) var genDoc *stypes.GenesisDoc if state == nil { genDoc, state = sm.MakeGenesisStateFromFile(stateDB, config.GetString("genesis_file")) state.Save() // write the gendoc to db buf, n, err := new(bytes.Buffer), new(int64), new(error) wire.WriteJSON(genDoc, buf, n, err) stateDB.Set(stypes.GenDocKey, buf.Bytes()) if *err != nil { Exit(Fmt("Unable to write gendoc to db: %v", err)) } } else { genDocBytes := stateDB.Get(stypes.GenDocKey) err := new(error) wire.ReadJSONPtr(&genDoc, genDocBytes, err) if *err != nil { Exit(Fmt("Unable to read gendoc from db: %v", err)) } } // add the chainid to the global config config.Set("chain_id", state.ChainID) // Get PrivValidator var privValidator *types.PrivValidator privValidatorFile := config.GetString("priv_validator_file") if _, err := os.Stat(privValidatorFile); err == nil { privValidator = types.LoadPrivValidator(privValidatorFile) log.Notice("Loaded PrivValidator", "file", privValidatorFile, "privValidator", privValidator) } else { privValidator = types.GenPrivValidator() privValidator.SetFile(privValidatorFile) privValidator.Save() log.Notice("Generated PrivValidator", "file", privValidatorFile) } // Generate node PrivKey privKey := acm.GenPrivKeyEd25519() // Make event switch eventSwitch := events.NewEventSwitch() _, err := eventSwitch.Start() if err != nil { Exit(Fmt("Failed to start switch: %v", err)) } // Make PEXReactor book := p2p.NewAddrBook(config.GetString("addrbook_file")) pexReactor := p2p.NewPEXReactor(book) // Make BlockchainReactor bcReactor := bc.NewBlockchainReactor(state.Copy(), blockStore, config.GetBool("fast_sync")) // Make MempoolReactor mempool := mempl.NewMempool(state.Copy()) mempoolReactor := mempl.NewMempoolReactor(mempool) // Make ConsensusReactor consensusState := consensus.NewConsensusState(state.Copy(), blockStore, mempoolReactor) consensusReactor := consensus.NewConsensusReactor(consensusState, blockStore, config.GetBool("fast_sync")) if privValidator != nil { consensusReactor.SetPrivValidator(privValidator) } // Make p2p network switch sw := p2p.NewSwitch() sw.AddReactor("PEX", pexReactor) sw.AddReactor("MEMPOOL", mempoolReactor) sw.AddReactor("BLOCKCHAIN", bcReactor) sw.AddReactor("CONSENSUS", consensusReactor) // add the event switch to all services // they should all satisfy events.Eventable SetFireable(eventSwitch, pexReactor, bcReactor, mempoolReactor, consensusReactor) return &Node{ sw: sw, evsw: eventSwitch, book: book, blockStore: blockStore, pexReactor: pexReactor, bcReactor: bcReactor, mempoolReactor: mempoolReactor, consensusState: consensusState, consensusReactor: consensusReactor, privValidator: privValidator, genDoc: genDoc, privKey: privKey, } }