forked from drand/drand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.go
61 lines (56 loc) · 1.56 KB
/
daemon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"fmt"
"os"
"github.com/drand/drand/core"
"github.com/drand/drand/key"
"github.com/drand/drand/metrics"
"github.com/urfave/cli/v2"
)
func startCmd(c *cli.Context) error {
conf := contextToConfig(c)
fs := key.NewFileStore(conf.ConfigFolder())
var drand *core.Drand
// determine if we already ran a DKG or not
_, errG := fs.LoadGroup()
_, errS := fs.LoadShare()
_, errD := fs.LoadDistPublic()
// XXX place that logic inside core/ directly with only one method
freshRun := errG != nil || errS != nil || errD != nil
var err error
if freshRun {
if exit := resetBeaconDB(conf); exit {
os.Exit(0)
}
fmt.Println("drand: will run as fresh install -> expect to run DKG.")
drand, err = core.NewDrand(fs, conf)
if err != nil {
fatal("drand: can't instantiate drand instance %s", err)
}
} else {
fmt.Println("drand: will already start running randomness beacon")
drand, err = core.LoadDrand(fs, conf)
if err != nil {
fatal("drand: can't load drand instance %s", err)
}
// XXX make it configurable so that new share holder can still start if
// nobody started.
//drand.StartBeacon(!c.Bool(pushFlag.Name))
catchup := true
drand.StartBeacon(catchup)
}
// Start metrics server
if c.IsSet(metricsFlag.Name) {
go metrics.Start(c.Int(metricsFlag.Name))
}
<-drand.WaitExit()
return nil
}
func stopDaemon(c *cli.Context) error {
client := controlClient(c)
if _, err := client.Shutdown(); err != nil {
fmt.Printf("Error stopping drand daemon: %v\n", err)
}
fmt.Println("drand daemon stopped correctly. Bye.")
return nil
}