Merge pull request #16250 from gluk256/317-fatalf

whisper: refactoring go-routines workflow

Move the call mailServer.Init() down (to the bottom of the function) because if the function initialize() completes successfully, then it will be followed by mailServer.Close() in shutdown(). The workflow of the corresponding goroutines is clearer now.
pull/16254/head
Guillaume Ballet 7 years ago committed by GitHub
commit abed63c38f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 56
      cmd/wnode/main.go

@ -110,6 +110,7 @@ func main() {
processArgs() processArgs()
initialize() initialize()
run() run()
shutdown()
} }
func processArgs() { func processArgs() {
@ -209,21 +210,6 @@ func initialize() {
MinimumAcceptedPOW: *argPoW, MinimumAcceptedPOW: *argPoW,
} }
if *mailServerMode {
if len(msPassword) == 0 {
msPassword, err = console.Stdin.PromptPassword("Please enter the Mail Server password: ")
if err != nil {
utils.Fatalf("Failed to read Mail Server password: %s", err)
}
}
shh = whisper.New(cfg)
shh.RegisterServer(&mailServer)
mailServer.Init(shh, *argDBPath, msPassword, *argServerPoW)
} else {
shh = whisper.New(cfg)
}
if *argPoW != whisper.DefaultMinimumPoW { if *argPoW != whisper.DefaultMinimumPoW {
err := shh.SetMinimumPoW(*argPoW) err := shh.SetMinimumPoW(*argPoW)
if err != nil { if err != nil {
@ -265,6 +251,26 @@ func initialize() {
maxPeers = 800 maxPeers = 800
} }
_, err = crand.Read(entropy[:])
if err != nil {
utils.Fatalf("crypto/rand failed: %s", err)
}
if *mailServerMode {
if len(msPassword) == 0 {
msPassword, err = console.Stdin.PromptPassword("Please enter the Mail Server password: ")
if err != nil {
utils.Fatalf("Failed to read Mail Server password: %s", err)
}
}
shh = whisper.New(cfg)
shh.RegisterServer(&mailServer)
mailServer.Init(shh, *argDBPath, msPassword, *argServerPoW)
} else {
shh = whisper.New(cfg)
}
server = &p2p.Server{ server = &p2p.Server{
Config: p2p.Config{ Config: p2p.Config{
PrivateKey: nodeid, PrivateKey: nodeid,
@ -278,17 +284,13 @@ func initialize() {
TrustedNodes: peers, TrustedNodes: peers,
}, },
} }
_, err = crand.Read(entropy[:])
if err != nil {
utils.Fatalf("crypto/rand failed: %s", err)
}
} }
func startServer() { func startServer() error {
err := server.Start() err := server.Start()
if err != nil { if err != nil {
utils.Fatalf("Failed to start Whisper peer: %s.", err) fmt.Printf("Failed to start Whisper peer: %s.", err)
return err
} }
fmt.Printf("my public key: %s \n", common.ToHex(crypto.FromECDSAPub(&asymKey.PublicKey))) fmt.Printf("my public key: %s \n", common.ToHex(crypto.FromECDSAPub(&asymKey.PublicKey)))
@ -307,6 +309,7 @@ func startServer() {
if !*forwarderMode { if !*forwarderMode {
fmt.Printf("Please type the message. To quit type: '%s'\n", quitCommand) fmt.Printf("Please type the message. To quit type: '%s'\n", quitCommand)
} }
return nil
} }
func isKeyValid(k *ecdsa.PublicKey) bool { func isKeyValid(k *ecdsa.PublicKey) bool {
@ -420,8 +423,10 @@ func waitForConnection(timeout bool) {
} }
func run() { func run() {
defer mailServer.Close() err := startServer()
startServer() if err != nil {
return
}
defer server.Stop() defer server.Stop()
shh.Start(nil) shh.Start(nil)
defer shh.Stop() defer shh.Stop()
@ -439,8 +444,11 @@ func run() {
} else { } else {
sendLoop() sendLoop()
} }
}
func shutdown() {
close(done) close(done)
mailServer.Close()
} }
func sendLoop() { func sendLoop() {

Loading…
Cancel
Save