func ensurePrivValidator(file string) { if FileExists(file) { return } privValidator := tmtypes.GenPrivValidator() privValidator.SetFile(file) privValidator.Save() }
func gen_validator() { privValidator := types.GenPrivValidator() privValidatorJSONBytes := wire.JSONBytes(privValidator) fmt.Printf(`Generated a new validator! Paste the following JSON into your %v file %v `, config.GetString("priv_validator_file"), string(privValidatorJSONBytes), ) }
func init_files() { privValidator := types.GenPrivValidator() privValidator.SetFile(config.GetString("priv_validator_file")) privValidator.Save() genDoc := types.GenesisDoc{ ChainID: Fmt("test-chain-%v", RandStr(6)), } genDoc.Validators = []types.GenesisValidator{types.GenesisValidator{ PubKey: privValidator.PubKey, Amount: 10, }} genDoc.SaveAs(config.GetString("genesis_file")) }
// 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) } }
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, } }