forked from drand/drand
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.go
More file actions
238 lines (215 loc) · 6.18 KB
/
Copy pathcontrol.go
File metadata and controls
238 lines (215 loc) · 6.18 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"fmt"
"os"
"time"
"github.com/drand/drand/core"
"github.com/drand/drand/key"
"github.com/drand/drand/net"
control "github.com/drand/drand/protobuf/drand"
json "github.com/nikkolasg/hexjson"
"github.com/urfave/cli/v2"
)
func shareCmd(c *cli.Context) error {
isResharing := c.IsSet(transitionFlag.Name) || c.IsSet(oldGroupFlag.Name)
isLeader := c.Bool(leaderFlag.Name)
var connectPeer net.Peer
if !isLeader {
if !c.IsSet(connectFlag.Name) {
fatal("need to the address of the coordinator to create the group file")
}
coordAddress := c.String(connectFlag.Name)
isTls := !c.IsSet(insecureFlag.Name)
connectPeer = net.CreatePeer(coordAddress, isTls)
}
nodes := c.Int(shareNodeFlag.Name)
thr := c.Int(thresholdFlag.Name)
secret := c.String(secretFlag.Name)
var timeout = core.DefaultDKGTimeout
if c.IsSet(timeoutFlag.Name) {
timeout = c.String(timeoutFlag.Name)
}
conf := contextToConfig(c)
client, err := net.NewControlClient(conf.ControlPort())
if err != nil {
fatal("could not create client: %v", err)
}
var groupP *control.GroupPacket
var shareErr error
if !isResharing {
if c.IsSet(userEntropyOnlyFlag.Name) && !c.IsSet(sourceFlag.Name) {
fmt.Print("drand: userEntropyOnly needs to be used with the source flag, which is not specified here. userEntropyOnly flag is ignored.")
}
entropyInfo := entropyInfoFromReader(c)
if isLeader {
if !c.IsSet(periodFlag.Name) {
fatal("leader flag indicated requires the beacon period flag as well")
}
periodStr := c.String(periodFlag.Name)
period, err := time.ParseDuration(periodStr)
if err != nil {
fatal("period given is invalid: %v", err)
}
offset := int(core.DefaultGenesisOffset.Seconds())
if c.IsSet(beaconOffset.Name) {
offset = c.Int(beaconOffset.Name)
}
fmt.Println("Initiating the DKG as a leader")
fmt.Println("You can stop the command at any point. If so, the group " +
"file will not be written out to the specified output. To get the" +
"group file once the setup phase is done, you can run the `drand show" +
"group` command")
groupP, shareErr = client.InitDKGLeader(nodes, thr, period, timeout, entropyInfo, secret, offset)
fmt.Println(" --- got err", shareErr, "group", groupP)
} else {
fmt.Println("Participating to the setup of the DKG")
groupP, shareErr = client.InitDKG(connectPeer, nodes, thr, timeout, entropyInfo, secret)
fmt.Println(" --- got err", shareErr, "group", groupP)
}
} else {
// resharing case needs the previous group
var oldPath string
if c.IsSet(transitionFlag.Name) {
// daemon will try to the load the one stored
oldPath = ""
} else if c.IsSet(oldGroupFlag.Name) {
var oldGroup = new(key.Group)
if err := key.Load(c.String(oldGroupFlag.Name), oldGroup); err != nil {
fatal("could not load drand from path", err)
}
oldPath = c.String(oldGroupFlag.Name)
}
if isLeader {
offset := int(core.DefaultResharingOffset.Seconds())
if c.IsSet(beaconOffset.Name) {
offset = c.Int(beaconOffset.Name)
}
fmt.Println("Initiating the resharing as a leader")
groupP, shareErr = client.InitReshareLeader(nodes, thr, timeout, secret, oldPath, offset)
} else {
fmt.Println("Participating to the resharing")
groupP, shareErr = client.InitReshare(connectPeer, nodes, thr, timeout, secret, oldPath)
}
}
if shareErr != nil {
fatal("error setting up the network: %v", err)
}
group, err := core.ProtoToGroup(groupP)
if err != nil {
fatal("error interpreting the group from protobuf: %v", err)
}
groupOut(c, group)
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)
}
group, err := core.ProtoToGroup(r)
if err != nil {
return err
}
groupOut(c, group)
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(controlFlag.Name)
if port == "" {
port = core.DefaultControlPort
}
fmt.Println(" --- controlport using ", port)
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.Println(string(buff))
}
func fileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func entropyInfoFromReader(c *cli.Context) *control.EntropyInfo {
if c.IsSet(sourceFlag.Name) {
_, err := os.Lstat(c.String(sourceFlag.Name))
if err != nil {
fatal("drand: cannot use given entropy source: %s", err)
}
source := c.String(sourceFlag.Name)
ei := &control.EntropyInfo{
Script: source,
UserOnly: c.Bool(userEntropyOnlyFlag.Name),
}
return ei
}
return nil
}