-
Notifications
You must be signed in to change notification settings - Fork 115
/
control.go
215 lines (191 loc) · 5.76 KB
/
control.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/dedis/drand/core"
"github.com/dedis/drand/key"
"github.com/dedis/drand/net"
json "github.com/nikkolasg/hexjson"
"github.com/urfave/cli"
)
// shareCmd decides whether the command is for a DKG or for a resharing and
// dispatch to the respective sub-commands.
func shareCmd(c *cli.Context) error {
if !c.Args().Present() {
fatal("drand: needs at least one group.toml file argument")
}
groupPath := c.Args().First()
groupPath, err := filepath.Abs(groupPath)
if err != nil {
fatal("can't open group path absolute path from %s", c.Args().First())
}
testEmptyGroup(groupPath)
if c.IsSet(oldGroupFlag.Name) {
testEmptyGroup(c.String(oldGroupFlag.Name))
fmt.Println("drand: old group file given for resharing protocol")
return initReshare(c, groupPath)
}
conf := contextToConfig(c)
fs := key.NewFileStore(conf.ConfigFolder())
_, 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
if freshRun {
fmt.Println("drand: no current distributed key -> running DKG protocol.")
err = initDKG(c, groupPath)
} else {
fmt.Println("drand: found distributed key -> running resharing protocol.")
err = initReshare(c, groupPath)
}
return err
}
// initDKG indicates to the daemon to start the DKG protocol, as a leader or
// not. The method waits until the DKG protocol finishes or an error occured.
// If the DKG protocol finishes successfully, the beacon randomness loop starts.
func initDKG(c *cli.Context, groupPath string) error {
// still trying to load it ourself now for the moment
// just to test if it's a valid thing or not
conf := contextToConfig(c)
client, err := net.NewControlClient(conf.ControlPort())
if err != nil {
fatal("drand: error creating control client: %s", err)
}
fmt.Print("drand: waiting the end of DKG protocol ... " +
"(you can CTRL-C to not quit waiting)")
_, err = client.InitDKG(groupPath, c.Bool(leaderFlag.Name), c.String(timeoutFlag.Name))
if err != nil {
fatal("drand: initdkg %s", err)
}
return nil
}
// initReshare indicates to the daemon to start the resharing protocol, as a
// leader or not. The method waits until the resharing protocol finishes or
// an error occured. TInfofhe "old group" toml is inferred either from the local
// informations that the drand node is keeping (saved in filesystem), and can be
// superseeded by the command line flag "old-group".
// If the DKG protocol finishes successfully, the beacon randomness loop starts.
// NOTE: If the contacted node is not present in the new list of nodes, the
// waiting *can* be infinite in some cases. It's an issue that is low priority
// though.
func initReshare(c *cli.Context, newGroupPath string) error {
var isLeader = c.Bool(leaderFlag.Name)
var oldGroupPath string
if c.IsSet(oldGroupFlag.Name) {
oldGroupPath = c.String(oldGroupFlag.Name)
}
if oldGroupPath == "" {
fmt.Print("drand: old group path not specified. Using daemon's own group if possible.")
}
client := controlClient(c)
fmt.Println("drand: initiating resharing protocol. Waiting to the end ...")
_, err := client.InitReshare(oldGroupPath, newGroupPath, isLeader, c.String(timeoutFlag.Name))
if err != nil {
fatal("drand: error resharing: %s", err)
}
return nil
}
func getShare(c *cli.Context) error {
client := controlClient(c)
resp, err := client.Share()
if err != nil {
fatal("drand: could not request the share: %s", err)
}
printJSON(resp)
return nil
}
func pingpongCmd(c *cli.Context) error {
client := controlClient(c)
if err := client.Ping(); err != nil {
fatal("drand: can't ping the daemon ... %s", err)
}
fmt.Printf("drand daemon is alive on port %s", controlPort(c))
return nil
}
func showGroupCmd(c *cli.Context) error {
client := controlClient(c)
r, err := client.GroupFile()
if err != nil {
fatal("drand: fetching group file error: %s", err)
}
if c.IsSet(outFlag.Name) {
filePath := c.String(outFlag.Name)
err := ioutil.WriteFile(filePath, []byte(r.GroupToml), 0750)
if err != nil {
fatal("drand: can't write to file: %s", err)
}
fmt.Printf("group file written to %s", filePath)
} else {
fmt.Printf("\n\n%s", r.GroupToml)
}
return nil
}
func showCokeyCmd(c *cli.Context) error {
client := controlClient(c)
resp, err := client.CollectiveKey()
if err != nil {
fatal("drand: could not request drand.cokey: %s", err)
}
printJSON(resp)
return nil
}
func showPrivateCmd(c *cli.Context) error {
client := controlClient(c)
resp, err := client.PrivateKey()
if err != nil {
fatal("drand: could not request drand.private: %s", err)
}
printJSON(resp)
return nil
}
func showPublicCmd(c *cli.Context) error {
client := controlClient(c)
resp, err := client.PublicKey()
if err != nil {
fatal("drand: could not request drand.public: %s", err)
}
printJSON(resp)
return nil
}
func showShareCmd(c *cli.Context) error {
client := controlClient(c)
resp, err := client.Share()
if err != nil {
fatal("drand: could not request drand.share: %s", err)
}
printJSON(resp)
return nil
}
func controlPort(c *cli.Context) string {
port := c.String("control")
if port == "" {
port = core.DefaultControlPort
}
return port
}
func controlClient(c *cli.Context) *net.ControlClient {
port := controlPort(c)
client, err := net.NewControlClient(port)
if err != nil {
fatal("drand: can't instantiate control client: %s", err)
}
return client
}
func printJSON(j interface{}) {
buff, err := json.MarshalIndent(j, "", " ")
if err != nil {
fatal("drand: could not JSON marshal: %s", err)
}
fmt.Print(string(buff))
}
func fileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}