@@ -2,7 +2,6 @@ package servicesunlynx
2
2
3
3
import (
4
4
"strconv"
5
- "time"
6
5
7
6
"github.com/Knetic/govaluate"
8
7
"github.com/btcsuite/goleveldb/leveldb/errors"
@@ -39,6 +38,8 @@ type SurveyCreationQuery struct {
39
38
MapDPs map [string ]int64
40
39
Proofs bool
41
40
AppFlag bool
41
+ IntraMessage bool
42
+ Source * network.ServerIdentity
42
43
43
44
// query statement
44
45
Sum []string
@@ -67,9 +68,10 @@ type Survey struct {
67
68
68
69
// MsgTypes defines the Message Type ID for all the service's intra-messages.
69
70
type MsgTypes struct {
70
- msgSurveyCreationQuery network.MessageTypeID
71
- msgSurveyResultsQuery network.MessageTypeID
72
- msgDDTfinished network.MessageTypeID
71
+ msgSurveyCreationQuery network.MessageTypeID
72
+ msgSurveyResultsQuery network.MessageTypeID
73
+ msgDDTfinished network.MessageTypeID
74
+ msgQueryBroadcastFinished network.MessageTypeID
73
75
}
74
76
75
77
var msgTypes = MsgTypes {}
@@ -79,14 +81,20 @@ func init() {
79
81
log .ErrFatal (err )
80
82
81
83
msgTypes .msgSurveyCreationQuery = network .RegisterMessage (& SurveyCreationQuery {})
82
- network .RegisterMessage (& SurveyResponseQuery {})
83
84
msgTypes .msgSurveyResultsQuery = network .RegisterMessage (& SurveyResultsQuery {})
84
85
msgTypes .msgDDTfinished = network .RegisterMessage (& DDTfinished {})
86
+ msgTypes .msgQueryBroadcastFinished = network .RegisterMessage (& QueryBroadcastFinished {})
85
87
88
+ network .RegisterMessage (& SurveyResponseQuery {})
86
89
network .RegisterMessage (& ServiceState {})
87
90
network .RegisterMessage (& ServiceResult {})
88
91
}
89
92
93
+ // QueryBroadcastFinished is used to ensure that all servers have received the query/survey
94
+ type QueryBroadcastFinished struct {
95
+ SurveyID SurveyID
96
+ }
97
+
90
98
// DDTfinished is used to ensure that all servers perform the shuffling+DDT before collectively aggregating the results
91
99
type DDTfinished struct {
92
100
SurveyID SurveyID
@@ -122,17 +130,10 @@ type Service struct {
122
130
Survey * concurrent.ConcurrentMap
123
131
}
124
132
125
- func castToSurvey (object interface {}, err error ) Survey {
126
- if err != nil || object == nil {
127
- log .Fatal ("Error reading map:" , err )
128
- }
129
- return object .(Survey )
130
- }
131
-
132
- func (s * Service ) getSurvey (sid SurveyID , step int ) Survey {
133
+ func (s * Service ) getSurvey (sid SurveyID ) Survey {
133
134
surv , err := s .Survey .Get (string (sid ))
134
135
if err != nil || surv == nil {
135
- log .Fatalf ("Error '%s' while getting surveyID %x %d, %s " , err , sid , step , s . ServerIdentity (). String () )
136
+ log .Fatal ("Error" , err , " while getting surveyID" , sid )
136
137
}
137
138
return surv .(Survey )
138
139
}
@@ -161,10 +162,14 @@ func NewService(c *onet.Context) (onet.Service, error) {
161
162
if cerr = newUnLynxInstance .RegisterHandler (newUnLynxInstance .HandleDDTfinished ); cerr != nil {
162
163
log .Fatal ("Wrong Handler." , cerr )
163
164
}
165
+ if cerr = newUnLynxInstance .RegisterHandler (newUnLynxInstance .HandleQueryBroadcastFinished ); cerr != nil {
166
+ log .Fatal ("Wrong Handler." , cerr )
167
+ }
164
168
165
169
c .RegisterProcessor (newUnLynxInstance , msgTypes .msgSurveyCreationQuery )
166
170
c .RegisterProcessor (newUnLynxInstance , msgTypes .msgSurveyResultsQuery )
167
171
c .RegisterProcessor (newUnLynxInstance , msgTypes .msgDDTfinished )
172
+ c .RegisterProcessor (newUnLynxInstance , msgTypes .msgQueryBroadcastFinished )
168
173
return newUnLynxInstance , cerr
169
174
}
170
175
@@ -178,6 +183,10 @@ func (s *Service) Process(msg *network.Envelope) {
178
183
tmp := (msg .Msg ).(* SurveyResultsQuery )
179
184
_ , err := s .HandleSurveyResultsQuery (tmp )
180
185
log .ErrFatal (err )
186
+ } else if msg .MsgType .Equal (msgTypes .msgQueryBroadcastFinished ) {
187
+ tmp := (msg .Msg ).(* QueryBroadcastFinished )
188
+ _ , err := s .HandleQueryBroadcastFinished (tmp )
189
+ log .ErrFatal (err )
181
190
} else if msg .MsgType .Equal (msgTypes .msgDDTfinished ) {
182
191
tmp := (msg .Msg ).(* DDTfinished )
183
192
_ , err := s .HandleDDTfinished (tmp )
@@ -187,7 +196,7 @@ func (s *Service) Process(msg *network.Envelope) {
187
196
188
197
// PushData is used to store incoming data by servers
189
198
func (s * Service ) PushData (resp * SurveyResponseQuery , proofs bool ) {
190
- survey := s .getSurvey (resp .SurveyID , 1 )
199
+ survey := s .getSurvey (resp .SurveyID )
191
200
for _ , v := range resp .Responses {
192
201
dr := libunlynx.DpResponse {}
193
202
dr .FromDpResponseToSend (v )
@@ -207,20 +216,12 @@ func (s *Service) HandleSurveyCreationQuery(recq *SurveyCreationQuery) (network.
207
216
log .Lvl1 (s .ServerIdentity ().String (), " received a Survey Creation Query" )
208
217
209
218
// if this server is the one receiving the query from the client
210
- if recq .SurveyID == "" {
219
+ if recq .IntraMessage == false {
211
220
id := uuid .NewV4 ()
212
221
newID := SurveyID (id .String ())
213
222
recq .SurveyID = newID
214
-
215
223
log .Lvl1 (s .ServerIdentity ().String (), " handles this new survey " , recq .SurveyID )
216
224
217
- // broadcasts the query
218
- err := libunlynxtools .SendISMOthers (s .ServiceProcessor , & recq .Roster , recq )
219
- if err != nil {
220
- log .Error ("broadcasting error " , err )
221
- }
222
- log .Lvl1 (s .ServerIdentity (), " initiated the survey " , newID )
223
-
224
225
}
225
226
226
227
// chooses an ephemeral secret for this survey
@@ -244,8 +245,25 @@ func (s *Service) HandleSurveyCreationQuery(recq *SurveyCreationQuery) (network.
244
245
if err != nil {
245
246
return nil , err
246
247
}
248
+ log .Lvl1 (s .ServerIdentity (), " initiated the survey " , recq .SurveyID )
249
+
250
+ if recq .IntraMessage == false {
251
+ recq .IntraMessage = true
252
+ recq .Source = s .ServerIdentity ()
253
+ // broadcasts the query
254
+ err := libunlynxtools .SendISMOthers (s .ServiceProcessor , & recq .Roster , recq )
255
+ if err != nil {
256
+ log .Error ("broadcasting error " , err )
257
+ }
258
+ recq .IntraMessage = false
259
+ } else {
260
+ // warn 'root' node that it has received the query
261
+ err := s .SendRaw (recq .Source , & QueryBroadcastFinished {SurveyID : recq .SurveyID })
262
+ if err != nil {
263
+ return nil , err
264
+ }
265
+ }
247
266
248
- log .Lvl1 (s .ServerIdentity (), " created the survey " , recq .SurveyID )
249
267
// if it is a app download the data from the test file
250
268
if recq .AppFlag {
251
269
index := 0
@@ -259,63 +277,33 @@ func (s *Service) HandleSurveyCreationQuery(recq *SurveyCreationQuery) (network.
259
277
s .PushData (resp , recq .Proofs )
260
278
261
279
//number of data providers who have already pushed the data
262
- s .getSurvey (resp .SurveyID , 2 ).DpChannel <- 1
280
+ s .getSurvey (resp .SurveyID ).DpChannel <- 1
263
281
}
264
282
265
- // update surveyChannel so that the server knows he can start to process data from DPs
266
- s .getSurvey (recq .SurveyID , 3 ).SurveyChannel <- 1
283
+ if recq .IntraMessage == false {
284
+ counter := len (recq .Roster .List ) - 1
285
+ for counter > 0 {
286
+ counter = counter - (<- s .getSurvey (recq .SurveyID ).SurveyChannel )
287
+ }
288
+ }
267
289
return & ServiceState {recq .SurveyID }, nil
268
290
}
269
291
270
292
// HandleSurveyResponseQuery handles a survey answers submission by a subject.
271
293
func (s * Service ) HandleSurveyResponseQuery (resp * SurveyResponseQuery ) (network.Message , error ) {
272
- var el interface {}
273
- el = nil
274
- for el == nil {
275
- el , _ = s .Survey .Get ((string )(resp .SurveyID ))
276
-
277
- if el != nil {
278
- break
279
- }
280
-
281
- time .Sleep (time .Millisecond * 100 )
282
- }
283
-
284
- survey := el .(Survey )
285
- if survey .Query .SurveyID == resp .SurveyID {
286
- <- s .getSurvey (resp .SurveyID , 3 ).SurveyChannel
287
-
288
- s .PushData (resp , survey .Query .Proofs )
294
+ survey := s .getSurvey (resp .SurveyID )
295
+ s .PushData (resp , survey .Query .Proofs )
289
296
290
- //unblock the channel to allow another DP to send its data
291
- s .getSurvey (resp .SurveyID , 4 ).SurveyChannel <- 1
292
- //number of data providers who have already pushed the data
293
- s .getSurvey (resp .SurveyID , 5 ).DpChannel <- 1
294
-
295
- return & ServiceState {"1" }, nil
296
- }
297
-
298
- log .Lvl1 (s .ServerIdentity (), " does not know about this survey!" )
299
- return & ServiceState {resp .SurveyID }, nil
297
+ //number of data providers who have already pushed the data
298
+ s .getSurvey (resp .SurveyID ).DpChannel <- 1
299
+ return & ServiceState {"1" }, nil
300
300
}
301
301
302
302
// HandleSurveyResultsQuery handles the survey result query by the surveyor.
303
303
func (s * Service ) HandleSurveyResultsQuery (resq * SurveyResultsQuery ) (network.Message , error ) {
304
- var el interface {}
305
- el = nil
306
- for el == nil {
307
- el , _ = s .Survey .Get ((string )(resq .SurveyID ))
308
-
309
- if el != nil {
310
- break
311
- }
312
-
313
- time .Sleep (time .Millisecond * 100 )
314
- }
315
-
316
304
log .Lvl1 (s .ServerIdentity (), " received a survey result query" )
317
305
318
- survey := s .getSurvey (resq .SurveyID , 6 )
306
+ survey := s .getSurvey (resq .SurveyID )
319
307
survey .Query .ClientPubKey = resq .ClientPublic
320
308
err := s .putSurvey (resq .SurveyID , survey )
321
309
if err != nil {
@@ -337,7 +325,7 @@ func (s *Service) HandleSurveyResultsQuery(resq *SurveyResultsQuery) (network.Me
337
325
338
326
log .Lvl1 (s .ServerIdentity (), " completed the query processing..." )
339
327
340
- survey := s .getSurvey (resq .SurveyID , 7 )
328
+ survey := s .getSurvey (resq .SurveyID )
341
329
results := survey .PullDeliverableResults (false , libunlynx.CipherText {})
342
330
err = s .putSurvey (resq .SurveyID , survey )
343
331
if err != nil {
@@ -350,9 +338,16 @@ func (s *Service) HandleSurveyResultsQuery(resq *SurveyResultsQuery) (network.Me
350
338
return nil , s .StartService (resq .SurveyID , false )
351
339
}
352
340
353
- // HandleDDTfinished handles the message
341
+ // HandleDDTfinished handles the message DDTfinished: one of the nodes is ready to perform a collective aggregation
354
342
func (s * Service ) HandleDDTfinished (recq * DDTfinished ) (network.Message , error ) {
355
- s .getSurvey (recq .SurveyID , 8 ).DDTChannel <- 1
343
+ s .getSurvey (recq .SurveyID ).DDTChannel <- 1
344
+ return nil , nil
345
+ }
346
+
347
+ // HandleQueryBroadcastFinished handles the message QueryBroadcastFinished: one of the nodes has already received the query
348
+ func (s * Service ) HandleQueryBroadcastFinished (recq * QueryBroadcastFinished ) (network.Message , error ) {
349
+ log .LLvl1 (recq .SurveyID )
350
+ s .getSurvey (recq .SurveyID ).SurveyChannel <- 1
356
351
return nil , nil
357
352
}
358
353
@@ -369,7 +364,7 @@ func (s *Service) NewProtocol(tn *onet.TreeNodeInstance, conf *onet.GenericConfi
369
364
var pi onet.ProtocolInstance
370
365
371
366
target := SurveyID (string (conf .Data ))
372
- survey := castToSurvey ( s . Survey . Get ( string (conf .Data ) ))
367
+ survey := s . getSurvey ( SurveyID (conf .Data ))
373
368
374
369
switch tn .ProtocolName () {
375
370
case protocolsunlynx .ShufflingProtocolName :
@@ -449,7 +444,7 @@ func (s *Service) NewProtocol(tn *onet.TreeNodeInstance, conf *onet.GenericConfi
449
444
450
445
counter := len (tn .Roster ().List ) - 1
451
446
for counter > 0 {
452
- counter = counter - (<- castToSurvey ( s . Survey . Get ( string (conf .Data ) )).DDTChannel )
447
+ counter = counter - (<- s . getSurvey ( SurveyID (conf .Data )).DDTChannel )
453
448
}
454
449
455
450
case protocolsunlynx .DROProtocolName :
@@ -519,7 +514,7 @@ func (s *Service) NewProtocol(tn *onet.TreeNodeInstance, conf *onet.GenericConfi
519
514
520
515
// StartProtocol starts a specific protocol (Pipeline, Shuffling, etc.)
521
516
func (s * Service ) StartProtocol (name string , targetSurvey SurveyID ) (onet.ProtocolInstance , error ) {
522
- tmp := s .getSurvey (targetSurvey , 9 )
517
+ tmp := s .getSurvey (targetSurvey )
523
518
tree := tmp .Query .Roster .GenerateNaryTreeWithRoot (2 , s .ServerIdentity ())
524
519
525
520
var tn * onet.TreeNodeInstance
@@ -551,22 +546,20 @@ func (s *Service) StartProtocol(name string, targetSurvey SurveyID) (onet.Protoc
551
546
552
547
// StartService starts the service (with all its different steps/protocols)
553
548
func (s * Service ) StartService (targetSurvey SurveyID , root bool ) error {
554
-
555
549
log .Lvl1 (s .ServerIdentity (), " is waiting on channel" )
556
- <- s .getSurvey (targetSurvey , 10 ).SurveyChannel
557
550
558
- survey := s .getSurvey (targetSurvey , 11 )
551
+ survey := s .getSurvey (targetSurvey )
559
552
560
553
counter := survey .Query .MapDPs [s .ServerIdentity ().String ()]
561
554
for counter > int64 (0 ) {
562
555
log .Lvl1 (s .ServerIdentity (), " is waiting for " , counter , " data providers to send their data" )
563
- counter = counter - int64 (<- s .getSurvey (targetSurvey , 12 ).DpChannel )
556
+ counter = counter - int64 (<- s .getSurvey (targetSurvey ).DpChannel )
564
557
}
565
558
log .Lvl1 ("All data providers (" , survey .Query .MapDPs [s .ServerIdentity ().String ()], ") for server " , s .ServerIdentity (), " have sent their data" )
566
559
567
560
log .Lvl1 (s .ServerIdentity (), " starts a UnLynx Protocol for survey " , targetSurvey )
568
561
569
- target := s .getSurvey (targetSurvey , 13 )
562
+ target := s .getSurvey (targetSurvey )
570
563
571
564
// Shuffling Phase
572
565
start := libunlynx .StartTimer (s .ServerIdentity ().String () + "_ShufflingPhase" )
@@ -635,7 +628,7 @@ func (s *Service) StartService(targetSurvey SurveyID, root bool) error {
635
628
636
629
// ShufflingPhase performs the shuffling of the ClientResponses
637
630
func (s * Service ) ShufflingPhase (targetSurvey SurveyID ) error {
638
- survey := s .getSurvey (targetSurvey , 14 )
631
+ survey := s .getSurvey (targetSurvey )
639
632
640
633
if len (survey .DpResponses ) == 0 && len (survey .DpResponsesAggr ) == 0 {
641
634
log .Lvl1 (s .ServerIdentity (), " no data to shuffle" )
@@ -647,7 +640,7 @@ func (s *Service) ShufflingPhase(targetSurvey SurveyID) error {
647
640
return err
648
641
}
649
642
tmpShufflingResult := <- pi .(* protocolsunlynx.ShufflingProtocol ).FeedbackChannel
650
- shufflingResult := protocolsunlynx .MatrixCipherTextToProcessResponse (tmpShufflingResult , s .getSurvey (targetSurvey , 14 ).Lengths )
643
+ shufflingResult := protocolsunlynx .MatrixCipherTextToProcessResponse (tmpShufflingResult , s .getSurvey (targetSurvey ).Lengths )
651
644
652
645
survey .PushShuffledProcessResponses (shufflingResult )
653
646
err = s .putSurvey (targetSurvey , survey )
@@ -656,7 +649,7 @@ func (s *Service) ShufflingPhase(targetSurvey SurveyID) error {
656
649
657
650
// TaggingPhase performs the private grouping on the currently collected data.
658
651
func (s * Service ) TaggingPhase (targetSurvey SurveyID ) error {
659
- survey := s .getSurvey (targetSurvey , 15 )
652
+ survey := s .getSurvey (targetSurvey )
660
653
661
654
if len (survey .ShuffledProcessResponses ) == 0 {
662
655
log .Lvl1 (s .ServerIdentity (), " for survey " , survey .Query .SurveyID , " has no data to det tag" )
@@ -669,7 +662,7 @@ func (s *Service) TaggingPhase(targetSurvey SurveyID) error {
669
662
}
670
663
671
664
tmpDeterministicTaggingResult := <- pi .(* protocolsunlynx.DeterministicTaggingProtocol ).FeedbackChannel
672
- deterministicTaggingResult := protocolsunlynx .DeterCipherVectorToProcessResponseDet (tmpDeterministicTaggingResult , s .getSurvey (targetSurvey , 16 ).TargetOfSwitch )
665
+ deterministicTaggingResult := protocolsunlynx .DeterCipherVectorToProcessResponseDet (tmpDeterministicTaggingResult , s .getSurvey (targetSurvey ).TargetOfSwitch )
673
666
674
667
var queryWhereTag []libunlynx.WhereQueryAttributeTagged
675
668
for i , v := range deterministicTaggingResult [:len (survey .Query .Where )] {
@@ -698,7 +691,7 @@ func (s *Service) AggregationPhase(targetSurvey SurveyID) error {
698
691
}
699
692
cothorityAggregatedData := <- pi .(* protocolsunlynx.CollectiveAggregationProtocol ).FeedbackChannel
700
693
701
- survey := s .getSurvey (targetSurvey , 16 )
694
+ survey := s .getSurvey (targetSurvey )
702
695
survey .PushCothorityAggregatedFilteredResponses (cothorityAggregatedData .GroupedData )
703
696
err = s .putSurvey (targetSurvey , survey )
704
697
return err
@@ -711,7 +704,7 @@ func (s *Service) DROPhase(targetSurvey SurveyID) error {
711
704
return err
712
705
}
713
706
714
- survey := s .getSurvey (targetSurvey , 17 )
707
+ survey := s .getSurvey (targetSurvey )
715
708
716
709
tmpShufflingResult := <- pi .(* protocolsunlynx.ShufflingProtocol ).FeedbackChannel
717
710
shufflingResult := protocolsunlynx .MatrixCipherTextToProcessResponse (tmpShufflingResult , survey .Lengths )
@@ -728,7 +721,7 @@ func (s *Service) KeySwitchingPhase(targetSurvey SurveyID) error {
728
721
return err
729
722
}
730
723
731
- survey := s .getSurvey (targetSurvey , 18 )
724
+ survey := s .getSurvey (targetSurvey )
732
725
733
726
tmpKeySwitchedAggregatedResponses := <- pi .(* protocolsunlynx.KeySwitchingProtocol ).FeedbackChannel
734
727
keySwitchedAggregatedResponses := protocolsunlynx .CipherVectorToFilteredResponse (tmpKeySwitchedAggregatedResponses , survey .Lengths )
0 commit comments