-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.go
250 lines (230 loc) · 6.2 KB
/
test.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
package handel
import (
"crypto/rand"
"fmt"
mathRand "math/rand"
"time"
lvl "github.com/go-kit/kit/log/level"
)
// Test is a struct implementing some useful functionality to test specific
// implementations on Handel.
// DO NOT USE IT IN PRODUCTION.
type Test struct {
reg Registry
cons Constructor
msg []byte
nets []Network
handels []*Handel
// notifies when one handel instance have finished
finished chan int
// notifies when the test should be brought down
done chan bool
// complete success channel gets notified when all handel instances have
// output a complete multi-signature
completeSuccess chan bool
// list of IDs that are offline during the test
offline []int32
// threshold of contributions necessary
threshold int
}
// NewTest returns all handels instances ready to go !
func NewTest(keys []SecretKey, pubs []PublicKey, c Constructor, msg []byte, config *Config) *Test {
n := len(keys)
ids := make([]Identity, n)
sigs := make([]Signature, n)
nets := make([]Network, n)
handels := make([]*Handel, n)
var err error
for i := 0; i < n; i++ {
pk := pubs[i]
id := int32(i)
ids[i] = NewStaticIdentity(id, "", pk)
sigs[i], err = keys[i].Sign(msg, rand.Reader)
if err != nil {
panic(err)
}
nets[i] = &TestNetwork{id: id, list: nets}
}
reg := NewArrayRegistry(ids)
logger := NewKitLogger(lvl.AllowDebug())
for i := 0; i < n; i++ {
newPartitioner := func(id int32, reg Registry, logger Logger) Partitioner {
return NewBinPartitioner(id, reg, logger)
}
conf := *config
conf.Logger = logger
conf.NewPartitioner = newPartitioner
handels[i] = NewHandel(nets[i], reg, ids[i], c, msg, sigs[i], &conf)
}
return &Test{
reg: reg,
cons: c,
msg: msg,
nets: nets,
handels: handels,
done: make(chan bool),
finished: make(chan int, n),
completeSuccess: make(chan bool, 1),
offline: make([]int32, 0),
threshold: n,
}
}
// SetOfflineNodes sets the given list of node's ID as offline nodes - the
// designated nodes won't run during the simulation.
func (t *Test) SetOfflineNodes(ids ...int32) {
t.offline = append(t.offline, ids...)
}
// SetRandomOfflines sets n random identities as offline - they wont participate in
// Handel
func (t *Test) SetRandomOfflines(n int) {
perms := mathRand.Perm(len(t.handels))
for i := 0; i < n; i++ {
t.offline = append(t.offline, int32(perms[i]))
}
}
// SetThreshold sets the minimum threshold of contributions required to be
// present in the multisignature created by Handel nodes. By default, it is
// equal to the size of the participant's set.
func (t *Test) SetThreshold(threshold int) {
t.threshold = threshold
for _, h := range t.handels {
h.threshold = threshold
}
}
// Start manually every handel instances and starts go routine to listen to the
// final signatures output from the handel instances.
func (t *Test) Start() {
for i, handel := range t.handels {
if t.isOffline(handel.id.ID()) {
continue
}
idx := i
go handel.Start()
go t.waitFinalSig(idx)
}
go t.watchComplete()
}
func (t *Test) isOffline(nodeID int32) bool {
for _, id := range t.offline {
if id == nodeID {
return true
}
}
return false
}
// Stop manually every handel instances
func (t *Test) Stop() {
close(t.done)
time.Sleep(30 * time.Millisecond)
for _, handel := range t.handels {
if t.isOffline(handel.id.ID()) {
continue
}
handel.Stop()
}
}
// Networks returns the slice of network interface used by handel. It can be
// useful if you want to register your own listener.
func (t *Test) Networks() []Network {
return t.nets
}
// WaitCompleteSuccess waits until *all* handel instance have generated the
// multi-signature containing *all* contributions from each. It returns an
// channel so it's easy to wait for a certain timeout with `select`.
func (t *Test) WaitCompleteSuccess() chan bool {
return t.completeSuccess
}
func (t *Test) watchComplete() {
expected := len(t.handels) - len(t.offline)
var finished []int
for {
select {
case id := <-t.finished:
finished = append(finished, id)
t.info(id, finished)
if len(finished) >= expected {
// signature that to success channel
t.completeSuccess <- true
return
}
case <-t.done:
return
}
}
}
func (t *Test) info(newFinished int, finished []int) {
expected := len(t.handels) - len(t.offline)
s1 := fmt.Sprintf("handel %d\t- finished %d / online %d / total %d\n", newFinished, len(finished), expected, len(t.handels))
for i, h := range t.handels {
var s2 string
if t.isOffline(h.id.ID()) {
s2 = fmt.Sprintf("- %d offline\t", i)
} else if isIncluded(i, finished) {
s2 = fmt.Sprintf("- %d finished\t", i)
} else {
s2 = fmt.Sprintf("- %d waiting X %s\t", i, h.store)
}
if (i+1)%1 == 0 {
s2 += "\n"
}
s1 += s2
}
fmt.Println(s1)
}
func isIncluded(i int, all []int) bool {
for _, v := range all {
if v == i {
return true
}
}
return false
}
// waitFinalSig loops over the final signatures output by a specific handel
// instance until the signature is complete. In that case, it notifies the main
// watch routine.
func (t *Test) waitFinalSig(i int) {
h := t.handels[i]
ch := h.FinalSignatures()
for {
select {
case ms := <-ch:
/*fmt.Println("+++++++ t.reg ", t.reg)*/
//fmt.Println("+++++++ ms", ms)
/*fmt.Println("+++++++ ms.BitSet ", ms.BitSet)*/
if ms.BitSet.Cardinality() >= t.threshold {
if err := VerifyMultiSignature(t.msg, &ms, t.reg, t.cons); err != nil {
fmt.Println(" !!! --- Test verification failed --- !!!")
}
// one full !
t.finished <- i
return
}
case <-t.done:
return
}
}
}
// TestNetwork is a simple Network implementation using local dispatch functions
// in goroutine.
type TestNetwork struct {
id int32
list []Network
lis []Listener
}
// Send implements the Network interface
func (f *TestNetwork) Send(ids []Identity, p *Packet) {
for _, id := range ids {
go func(i Identity) {
f.list[int(i.ID())].(*TestNetwork).dispatch(p)
}(id)
}
}
// RegisterListener implements the Network interface
func (f *TestNetwork) RegisterListener(l Listener) {
f.lis = append(f.lis, l)
}
func (f *TestNetwork) dispatch(p *Packet) {
for _, l := range f.lis {
l.NewPacket(p)
}
}