This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
/
churn.go
316 lines (272 loc) · 7.64 KB
/
churn.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
package services
// This file contains the logic to handle churn.
import (
"github.com/dedis/prifi/sda/protocols"
"go.dedis.ch/onet/v3"
"go.dedis.ch/onet/v3/log"
"go.dedis.ch/onet/v3/network"
"sync"
)
/*
* When the relay boots :
* he reads group.toml, identifies the trustees
* he inits an empty list of nodes
*
* When a node connects :
* the relay identifies him as client or trustee using the stored group.toml
* he adds it to the list of nodes
* if PriFi was running, he kills it, and rerun it if > threshold
*
* When a node disconnect :
* He sends STOP messages to every other node
* He kills his local instance of PriFi protocol
* He empties the list of waiting nodes
*
* Every X seconds :
* if the protocol is not running
* count the number of participants, if > threshold, start prifi
*/
type waitQueueEntry struct {
serverID *network.ServerIdentity
numericID int
role protocols.PriFiRole
}
// waitQueue contains the list of nodes that are currently willing
// to participate to the protocol.
type waitQueue struct {
writeMutex sync.Mutex
trustees map[string]*waitQueueEntry
clients map[string]*waitQueueEntry
}
func idFromMsg(msg *network.Envelope) string {
return idFromServerIdentity(msg.ServerIdentity)
}
func idFromServerIdentity(si *network.ServerIdentity) string {
public := si.Public.String()
identifier := public
return identifier
}
type churnHandler struct {
waitQueue *waitQueue
nextFreeClientID int
nextFreeTrusteeID int
relayIdentity *network.ServerIdentity //necessary to call createRoster
trusteesIDs []*network.ServerIdentity
//to be specified when instantiated
startProtocol func()
stopProtocol func()
isProtocolRunning func() bool
}
func (c *churnHandler) init(relayID *network.ServerIdentity, trusteesIDs []*network.ServerIdentity) {
if relayID == nil {
log.Fatal("Can't start the churnHandler without the relayID")
}
if trusteesIDs == nil {
log.Fatal("Can't start the churnHandler without the trusteesIDs")
}
c.waitQueue = &waitQueue{
clients: make(map[string]*waitQueueEntry),
trustees: make(map[string]*waitQueueEntry),
}
c.nextFreeClientID = 0
c.nextFreeTrusteeID = 0
c.relayIdentity = relayID
c.trusteesIDs = trusteesIDs
}
/**
* Checks whether an ID is in the waiting clients/trustees (given isTrustee)
*/
func (wq *waitQueue) contains(stringID string, isTrustee bool) bool {
if isTrustee {
_, ok := wq.trustees[stringID]
return ok
}
_, ok := wq.clients[stringID]
return ok
}
/**
* Returns nClients, nTrustees waiting
*/
func (wq *waitQueue) count() (int, int) {
return len(wq.clients), len(wq.trustees)
}
/**
* Creates a roster from waiting nodes, used by SDA
*/
func (c *churnHandler) createRoster() *onet.Roster {
n, m := c.waitQueue.count()
nParticipants := n + m + 1
participants := make([]*network.ServerIdentity, nParticipants)
participants[0] = c.relayIdentity
i := 1
for _, v := range c.waitQueue.clients {
participants[i] = v.serverID
i++
}
for _, v := range c.waitQueue.trustees {
participants[i] = v.serverID
i++
}
roster := onet.NewRoster(participants)
return roster
}
// CountParticipants returns nTrustees, nClients already connected
func (c *churnHandler) CountParticipants() (int, int) {
return c.waitQueue.count()
}
/**
* Tests if the given serverIdentity represents a trustee
*/
func (c *churnHandler) isATrustee(ID *network.ServerIdentity) bool {
for _, v := range c.trusteesIDs {
if v.Equal(ID) {
return true
}
}
return false
}
/**
* Creates an IdentityMap from the waiting nodes, used by PriFi-lib
*/
func (c *churnHandler) createIdentitiesMap() map[string]protocols.PriFiIdentity {
res := make(map[string]protocols.PriFiIdentity)
//add relay
res[idFromServerIdentity(c.relayIdentity)] = protocols.PriFiIdentity{
Role: protocols.Relay,
ID: 0,
ServerID: c.relayIdentity,
}
//add clients
for _, v := range c.waitQueue.clients {
res[idFromServerIdentity(v.serverID)] = protocols.PriFiIdentity{
Role: protocols.Client,
ID: v.numericID,
ServerID: v.serverID,
}
}
//add trustees
for _, v := range c.waitQueue.trustees {
res[idFromServerIdentity(v.serverID)] = protocols.PriFiIdentity{
Role: protocols.Trustee,
ID: v.numericID,
ServerID: v.serverID,
}
}
return res
}
func (c *churnHandler) getClientsIdentities() []*network.ServerIdentity {
nClients := len(c.waitQueue.clients)
clients := make([]*network.ServerIdentity, nClients)
i := 0
for _, v := range c.waitQueue.clients {
clients[i] = v.serverID
i++
}
return clients
}
func (c *churnHandler) getTrusteesIdentities() []*network.ServerIdentity {
nTrustees := len(c.waitQueue.trustees)
trustees := make([]*network.ServerIdentity, nTrustees)
i := 0
for _, v := range c.waitQueue.trustees {
trustees[i] = v.serverID
i++
}
return trustees
}
/**
* Handles a "Connection" message
*/
func (c *churnHandler) handleConnection(msg *network.Envelope) {
c.waitQueue.writeMutex.Lock()
defer c.waitQueue.writeMutex.Unlock()
ID := idFromMsg(msg)
isTrustee := c.isATrustee(msg.ServerIdentity)
node := "client"
if isTrustee {
node = "trustee"
}
if c.waitQueue.contains(ID, isTrustee) {
log.Lvl4("Ignored new connection request from", node, ID, "already in the list")
return
}
log.Lvl2("Received new connection request from", node, ID)
if isTrustee {
c.waitQueue.trustees[ID] = &waitQueueEntry{
serverID: msg.ServerIdentity,
role: protocols.Trustee,
numericID: c.nextFreeTrusteeID,
}
log.Lvl3("ID ", ID, " assigned to trustee #", c.nextFreeTrusteeID)
c.nextFreeTrusteeID++
} else {
c.waitQueue.clients[ID] = &waitQueueEntry{
serverID: msg.ServerIdentity,
role: protocols.Client,
numericID: c.nextFreeClientID,
}
log.Lvl3("ID ", ID, " assigned to client #", c.nextFreeClientID)
c.nextFreeClientID++
}
c.tryStartProtocol()
}
func (c *churnHandler) handleUnknownDisconnection() {
c.waitQueue.writeMutex.Lock()
defer c.waitQueue.writeMutex.Unlock()
c.waitQueue.clients = make(map[string]*waitQueueEntry)
c.waitQueue.trustees = make(map[string]*waitQueueEntry)
c.nextFreeClientID = 0
c.nextFreeTrusteeID = 0
c.stopProtocol()
c.tryStartProtocol()
}
/**
* Handles a "Disconnection" message
*/
func (c *churnHandler) handleDisconnection(msg *network.Envelope) {
ID := idFromMsg(msg)
isTrustee := c.isATrustee(msg.ServerIdentity)
if !c.waitQueue.contains(ID, isTrustee) {
log.Lvl4("Ignored new disconnection request from", ID, " (isATrustee:", isTrustee, "), not in the list")
return
}
log.Lvl3("Received new disconnection request from", ID, " (isATrustee:", isTrustee, ")")
/* This is the smart way. Dumb way first
if isTrustee {
delete(c.waitQueue.trustees, ID)
c.nextFreeTrusteeID = 0
for k := range c.waitQueue.trustees {
c.waitQueue.trustees[k].numericID = c.nextFreeTrusteeID
c.nextFreeTrusteeID++
}
c.nextFreeTrusteeID += 1
} else {
delete(c.waitQueue.clients, ID)
c.nextFreeClientID = 0
for k := range c.waitQueue.clients {
c.waitQueue.clients[k].numericID = c.nextFreeClientID
c.nextFreeClientID++
}
c.nextFreeClientID += 1
}
*/
c.handleUnknownDisconnection()
}
/**
* restarts the protocol (stop + start) if nClients waiting & nTrustees waiting both > 1
*/
func (c *churnHandler) tryStartProtocol() {
nClients, nTrustees := c.waitQueue.count()
if nClients >= 1 && nTrustees >= 1 {
if c.isProtocolRunning() {
c.stopProtocol()
}
if c.startProtocol == nil {
log.Lvl1("Enough participants (", nClients, "clients and", nTrustees, "trustees), but no handler to start.")
return
}
c.startProtocol()
} else {
log.Lvl1("Too few participants (", nClients, "clients and", nTrustees, "trustees), waiting...")
}
}