/
node_subprocess.go
475 lines (416 loc) 路 12.4 KB
/
node_subprocess.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package node
import (
"context"
"encoding/hex"
"fmt"
"net"
"os"
"os/exec"
"path"
"strconv"
"strings"
"time"
"github.com/BurntSushi/toml"
pdkg "github.com/drand/drand/v2/protobuf/dkg"
clock "github.com/jonboulle/clockwork"
json "github.com/nikkolasg/hexjson"
"github.com/drand/drand/v2/common/key"
"github.com/drand/drand/v2/common/log"
"github.com/drand/drand/v2/crypto"
"github.com/drand/drand/v2/demo/cfg"
"github.com/drand/drand/v2/internal/chain"
"github.com/drand/drand/v2/internal/core"
"github.com/drand/drand/v2/internal/dkg"
drandnet "github.com/drand/drand/v2/internal/net"
"github.com/drand/drand/v2/internal/test"
"github.com/drand/drand/v2/internal/util"
"github.com/drand/drand/v2/protobuf/drand"
)
type NodeProc struct {
base string
i int
period string
publicPath string
startCmd *exec.Cmd
lg log.Logger
logPath string
privAddr string
pubAddr string
priv *key.Pair
store key.Store
cancel context.CancelFunc
ctrl string
isCandidate bool
groupPath string
proposalPath string
binary string
scheme *crypto.Scheme
beaconID string
dkgRunner *dkg.TestRunner
dbEngineType chain.StorageType
memDBSize int
pgDSN string
metricPort string
}
func NewNode(i int, cfg cfg.Config) *NodeProc {
nbase := path.Join(cfg.BasePath, fmt.Sprintf("node-%d", i))
os.MkdirAll(nbase, 0740)
logPath := path.Join(nbase, "log")
publicPath := path.Join(nbase, "public.toml")
groupPath := path.Join(nbase, "group.toml")
proposalPath := path.Join(nbase, "proposal.toml")
os.Remove(logPath)
lg := log.New(nil, log.DefaultLevel, false).
Named(fmt.Sprintf("sub-proc-node-%d", i))
n := &NodeProc{
base: nbase,
i: i,
lg: lg,
logPath: logPath,
publicPath: publicPath,
groupPath: groupPath,
proposalPath: proposalPath,
period: cfg.Period,
scheme: cfg.Scheme,
binary: cfg.Binary,
beaconID: cfg.BeaconID,
isCandidate: cfg.IsCandidate,
dbEngineType: cfg.DBEngineType,
pgDSN: cfg.PgDSN(),
memDBSize: cfg.MemDBSize,
}
n.setup()
return n
}
// UpdateBinary updates the binary this node uses for control, to e.g. simulate an upgrade
func (n *NodeProc) UpdateBinary(binary string, isCandidate bool) {
n.binary = binary
n.isCandidate = isCandidate
}
func (n *NodeProc) setup() {
var err error
// find a free port
freePort := test.FreePort()
freePortREST := test.FreePort()
host := "127.0.0.1"
n.privAddr = host + ":" + freePort
n.pubAddr = host + ":" + freePortREST
ctrlPort := test.FreePort()
dkgClient, err := drandnet.NewDKGControlClient(n.lg, ctrlPort)
if err != nil {
panic("could not create DKG client")
}
n.dkgRunner = &dkg.TestRunner{
BeaconID: n.beaconID,
Client: dkgClient,
Clock: clock.NewRealClock(),
}
// call drand binary
n.priv, err = key.NewKeyPair(n.privAddr, n.scheme)
if err != nil {
panic(err)
}
args := []string{"generate-keypair", "--folder", n.base, "--id", n.beaconID, "--scheme", n.scheme.Name}
args = append(args, n.privAddr)
newKey := exec.Command(n.binary, args...)
runCommand(newKey)
config := core.NewConfig(n.lg, core.WithConfigFolder(n.base))
n.store = key.NewFileStore(config.ConfigFolderMB(), n.beaconID)
// verify it's done
n.priv, err = n.store.LoadKeyPair()
if n.priv.Public.Address() != n.privAddr {
panic(fmt.Errorf("[-] Private key stored has address %s vs generated %s || base %s", n.priv.Public.Address(), n.privAddr, n.base))
}
checkErr(key.Save(n.publicPath, n.priv.Public, false))
n.ctrl = ctrlPort
checkErr(err)
}
func (n *NodeProc) Start(dbEngineType chain.StorageType, pgDSN func() string, memDBSize int) error {
if dbEngineType != "" {
n.dbEngineType = dbEngineType
}
if pgDSN != nil {
n.pgDSN = pgDSN()
}
if memDBSize != 0 {
n.memDBSize = memDBSize
}
// create log file
// logFile, err := os.Create(n.logPath)
flags := os.O_RDWR | os.O_APPEND | os.O_CREATE
logFile, err := os.OpenFile(n.logPath, flags, 0640)
checkErr(err)
_, _ = logFile.Write([]byte("\n\nNEW LOG for NodeProc subprocess\n\n"))
var args = []string{"start"}
args = append(args, pair("--folder", n.base)...)
args = append(args, pair("--control", n.ctrl)...)
_, privPort, _ := net.SplitHostPort(n.privAddr)
_, pubPort, _ := net.SplitHostPort(n.pubAddr)
args = append(args, pair("--private-listen", "0.0.0.0:"+privPort)...)
args = append(args, pair("--public-listen", "0.0.0.0:"+pubPort)...)
args = append(args, pair("--db", string(n.dbEngineType))...)
args = append(args, pair("--pg-dsn", n.pgDSN)...)
args = append(args, pair("--memdb-size", fmt.Sprintf("%d", n.memDBSize))...)
args = append(args, "--verbose")
fmt.Printf("starting node %s with cmd: %s \n", n.privAddr, args)
ctx, cancel := context.WithCancel(context.Background())
n.cancel = cancel
cmd := exec.CommandContext(ctx, n.binary, args...)
n.startCmd = cmd
cmd.Stdout = logFile
cmd.Stderr = logFile
go func() {
defer func() {
_ = logFile.Close()
}()
// TODO make the "stop" command returns a graceful error code when stopped
err := cmd.Run()
fmt.Printf("Error while running node %s: %s", n.privAddr, err)
}()
return nil
}
func (n *NodeProc) PrivateAddr() string {
return n.privAddr
}
func (n *NodeProc) CtrlAddr() string {
return n.ctrl
}
func (n *NodeProc) PublicAddr() string {
return n.pubAddr
}
func (n *NodeProc) Index() int {
return n.i
}
func (n *NodeProc) StartLeaderDKG(thr int, _ int, joiners []*pdkg.Participant) error {
proposal := ProposalFile{
Joining: joiners,
}
err := WriteProposalFile(n.proposalPath, proposal)
if err != nil {
return err
}
proposeArgs := []string{
"dkg", "init",
"--genesis-delay", "20s",
"--control", n.ctrl,
"--id", n.beaconID,
"--scheme", n.scheme.Name,
"--period", n.period,
"--catchup-period", "1s",
"--proposal", n.proposalPath,
"--threshold", strconv.Itoa(thr),
"--timeout", (5 * time.Minute).String(),
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
proposeCmd := exec.CommandContext(ctx, n.binary, proposeArgs...)
_ = runCommand(proposeCmd)
return nil
}
func (n *NodeProc) ExecuteLeaderDKG() error {
executeArgs := []string{"dkg", "execute", "--control", n.ctrl, "--id", n.beaconID}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
executeCmd := exec.CommandContext(ctx, n.binary, executeArgs...)
out := runCommand(executeCmd)
fmt.Println(n.priv.Public.Address(), string(out))
return nil
}
func (n *NodeProc) JoinDKG() error {
args := []string{"dkg", "join", "--control", n.ctrl, "--id", n.beaconID}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, n.binary, args...)
_ = runCommand(cmd)
return nil
}
func (n *NodeProc) JoinReshare(oldGroup key.Group) error {
groupFilePath := "group.toml"
joinArgs := []string{
"dkg", "join",
"--control", n.ctrl,
"--id", n.beaconID,
"--group", groupFilePath,
}
f, err := os.Create(groupFilePath)
if err != nil {
return err
}
err = toml.NewEncoder(f).Encode(oldGroup.TOML())
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
proposeCmd := exec.CommandContext(ctx, n.binary, joinArgs...)
_ = runCommand(proposeCmd)
return nil
}
func (n *NodeProc) StartLeaderReshare(thr int, catchupPeriod int, joiners []*pdkg.Participant, remainers []*pdkg.Participant, leavers []*pdkg.Participant) error {
proposalFileName := "proposal.toml"
proposal := ProposalFile{
Joining: joiners,
Remaining: remainers,
Leaving: leavers,
}
err := WriteProposalFile(proposalFileName, proposal)
if err != nil {
return err
}
proposeArgs := []string{
"dkg", "reshare",
"--control", n.ctrl,
"--id", n.beaconID,
"--catchup-period", fmt.Sprintf("%ds", catchupPeriod),
"--proposal", proposalFileName,
"--threshold", strconv.Itoa(thr),
}
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
proposeCmd := exec.CommandContext(ctx, n.binary, proposeArgs...)
_ = runCommand(proposeCmd)
return nil
}
func (n *NodeProc) ExecuteLeaderReshare() error {
executeArgs := []string{"dkg", "execute", "--control", n.ctrl, "--id", n.beaconID}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
executeCmd := exec.CommandContext(ctx, n.binary, executeArgs...)
out := runCommand(executeCmd)
fmt.Println(n.priv.Public.Address(), string(out))
return nil
}
func (n *NodeProc) AcceptReshare() error {
args := []string{"dkg", "accept", "--control", n.ctrl, "--id", n.beaconID}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, n.binary, args...)
out := runCommand(cmd)
fmt.Println(n.priv.Public.Address(), string(out))
return nil
}
func (n *NodeProc) WaitDKGComplete(epoch uint32, timeout time.Duration) (*key.Group, error) {
err := n.dkgRunner.WaitForDKG(n.lg, epoch, int(timeout.Seconds()))
if err != nil {
return nil, err
}
return n.store.LoadGroup()
}
func (n *NodeProc) GetGroup() *key.Group {
args := []string{"show", "group", "--control", n.ctrl}
args = append(args, pair("--out", n.groupPath)...)
cmd := exec.Command(n.binary, args...)
runCommand(cmd)
group := new(key.Group)
checkErr(key.Load(n.groupPath, group))
return group
}
func (n *NodeProc) ChainInfo(_ string) bool {
args := []string{"show", "chain-info", "--control", n.ctrl}
args = append(args, n.privAddr)
cmd := exec.Command(n.binary, args...)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("get chain info %s : %s: err: %v:\n\tout:%s\n", n.privAddr, args, err, string(out))
return false
}
var r = new(drand.ChainInfoPacket)
err = json.Unmarshal(out, r)
if err != nil {
fmt.Println(fmt.Sprintf("\n\n-----\nerr %v json decoding %q\n\n-----\n", err, out))
}
checkErr(err)
sdist := hex.EncodeToString(r.PublicKey)
fmt.Printf("\t- Node %s has chain-info %s\n", n.privAddr, sdist[10:14])
return true
}
func (n *NodeProc) Ping() bool {
cmd := exec.Command(n.binary, "util", "ping", "--control", n.ctrl)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf(" -- ping output : %s - err %s\n", out, err)
} else {
fmt.Printf(" -- ping output : %s\n", out)
}
return err == nil
}
func (n *NodeProc) GetBeacon(groupPath string, round uint64) (*drand.PublicRandResponse, string) {
args := []string{"--no-progress-meter", n.pubAddr + "/public/" + strconv.Itoa(int(round))}
cmd := exec.Command("curl", args...)
out := runCommand(cmd)
if len(out) < 48 {
fmt.Println("Unable to GetBeacon, got an empty response")
return nil, "error"
}
s := new(drand.PublicRandResponse)
err := json.Unmarshal(out, s)
if err != nil {
fmt.Printf("error %v, failed to unmarshal beacon response: %s\n", err, out)
}
checkErr(err)
return s, strings.Join(cmd.Args, " ")
}
func (n *NodeProc) WritePublic(path string) {
checkErr(key.Save(path, n.priv.Public, false))
}
func (n *NodeProc) Stop() {
if n.cancel != nil {
n.cancel()
}
stopCmd := exec.Command(n.binary, "stop", "--control", n.ctrl)
stopCmd.Run()
if n.startCmd != nil {
killPid := exec.Command("kill", "-9", strconv.Itoa(n.startCmd.Process.Pid))
killPid.Run()
}
fmt.Printf(" -- trying to ping %s, expecting it to fail.", n.ctrl)
for i := 0; i < 3; i++ {
if n.Ping() {
time.Sleep(500 * time.Millisecond)
continue
}
fmt.Printf("\t + node successfully shutdown\n")
return
}
panic("node should have stopped but is still running")
}
func (n *NodeProc) PrintLog() {
fmt.Printf("[-] Printing logs of node %s:\n", n.privAddr)
buff, err := os.ReadFile(n.logPath)
if err != nil {
fmt.Printf("[-] Can't read logs at %s !\n\n", n.logPath)
return
}
fmt.Printf("%s\n", string(buff))
}
func (n *NodeProc) Identity() (*pdkg.Participant, error) {
keypair, err := n.store.LoadKeyPair()
if err != nil {
return nil, err
}
return util.PublicKeyAsParticipant(keypair.Public)
}
func pair(k, v string) []string {
return []string{k, v}
}
func runCommand(c *exec.Cmd, add ...string) []byte {
out, err := c.CombinedOutput()
if err != nil {
fmt.Printf("[-] Error: %v", err)
if len(add) > 0 {
fmt.Printf("[-] Msg failed command: %s\n", add[0])
}
fmt.Printf("[-] Command %q gave\n%s\n", strings.Join(c.Args, " "), string(out))
panic(err)
}
return out
}
func checkErr(err error, out ...string) {
if err == nil {
return
}
if len(out) > 0 {
panic(fmt.Errorf("%s: %v", out[0], err))
}
panic(err)
}