-
Notifications
You must be signed in to change notification settings - Fork 107
/
main.go
174 lines (161 loc) 路 4.29 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"time"
"github.com/drand/drand/demo/lib"
)
func installDrand() {
fmt.Println("[+] Building & installing drand")
curr, err := os.Getwd()
checkErr(err)
checkErr(os.Chdir("../"))
install := exec.Command("go", "install")
runCommand(install)
checkErr(os.Chdir(curr))
}
var build = flag.Bool("build", false, "build the drand binary first")
var binaryF = flag.String("binary", "drand", "path to drand binary")
var testF = flag.Bool("test", false, "run it as a test that finishes")
var tls = flag.Bool("tls", false, "run the nodes with self signed certs")
var noCurl = flag.Bool("nocurl", false, "skip commands using curl")
var debug = flag.Bool("debug", false, "prints the log when panic occurs")
func main() {
flag.Parse()
if *build {
installDrand()
}
if *testF {
defer func() { fmt.Println("[+] Leaving test - all good") }()
}
nRound := 2
n := 6
thr := 4
period := "10s"
newThr := 5
orch := lib.NewOrchestrator(n, thr, period, true, *binaryF, !*noCurl)
// NOTE: this line should be before "StartNewNodes". The reason it is here
// is that we are using self signed certificates, so when the first drand nodes
// start, they need to know about all self signed certificates. So we create
// already the new nodes here, such that when calling "StartCurrentNodes",
// the drand nodes will load all of them already.
orch.SetupNewNodes(3)
defer orch.Shutdown()
defer func() {
// print logs in case things panic
if err := recover(); err != nil {
if *debug {
fmt.Println(err)
orch.PrintLogs()
}
os.Exit(1)
}
}()
setSignal(orch)
orch.StartCurrentNodes()
orch.RunDKG("4s")
orch.WaitGenesis()
for i := 0; i < nRound; i++ {
orch.WaitPeriod()
orch.CheckCurrentBeacon()
}
// stop a node and look if the beacon still continues
nodeToStop := 3
orch.StopNodes(nodeToStop)
for i := 0; i < nRound; i++ {
orch.WaitPeriod()
orch.CheckCurrentBeacon(nodeToStop)
}
// stop the whole network, wait a bit and see if it can restart at the right
// round
/*orch.StopAllNodes(nodeToStop)*/
//orch.WaitPeriod()
//orch.WaitPeriod()
//// start all but the one still down
//orch.StartCurrentNodes(nodeToStop)
//// leave time to network to sync
//periodD, _ := time.ParseDuration(period)
//orch.Wait(time.Duration(2) * periodD)
//for i := 0; i < nRound; i++ {
//orch.WaitPeriod()
//orch.CheckCurrentBeacon(nodeToStop)
//}
// stop only more than a threshold of the network, wait a bit and see if it
// can restart at the right round correctly
/*nodesToStop := []int{1, 2}*/
//fmt.Printf("[+] Stopping more than threshold of nodes (1,2,3)\n")
//orch.StopNodes(nodesToStop...)
//orch.WaitPeriod()
//orch.WaitPeriod()
//fmt.Printf("[+] Trying to start them again and check beacons\n")
//orch.StartNode(nodesToStop...)
orch.StartNode(nodeToStop)
orch.WaitPeriod()
orch.WaitPeriod()
// at this point node should have catched up
for i := 0; i < nRound; i++ {
orch.WaitPeriod()
orch.CheckCurrentBeacon()
}
/// --- RESHARING PART ---
orch.StartNewNodes()
// exclude first node
orch.CreateResharingGroup(1, newThr)
orch.RunResharing("4s")
orch.WaitTransition()
limit := 10000
if *testF {
limit = 4
}
// look if beacon is still up even with the nodeToExclude being offline
for i := 0; i < limit; i++ {
orch.WaitPeriod()
orch.CheckNewBeacon()
}
}
func findTransitionTime(period time.Duration, genesis int64, secondsFromNow int64) int64 {
transition := genesis
for transition < time.Now().Unix()+secondsFromNow {
transition += int64(period.Seconds())
}
return transition
}
func setSignal(orch *lib.Orchestrator) {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
s := <-sigc
fmt.Println("[+] Received signal ", s.String())
orch.PrintLogs()
orch.Shutdown()
}()
}
func runCommand(c *exec.Cmd, add ...string) []byte {
out, err := c.CombinedOutput()
if err != nil {
if len(add) > 0 {
fmt.Printf("[-] Msg failed command: %s\n", add[0])
}
fmt.Printf("[-] Command \"%s\" gave\n%s\n", strings.Join(c.Args, " "), string(out))
panic(err)
}
return out
}
func checkErr(err error, out ...string) {
if err != nil {
if len(out) > 0 {
panic(fmt.Errorf("%s: %v", out[0], err))
} else {
panic(err)
}
}
}