-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimplexorrequestor.py
867 lines (616 loc) · 24.8 KB
/
simplexorrequestor.py
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
"""
<Author>
Daniel Demmler
(inspired from upPIR by Justin Cappos)
(inspired from a previous version by Geremy Condra)
<Date>
Janaury 2019
"""
# I'll use this to XOR the result together
import fastsimplexordatastore as xordatastore
# helper functions that are shared
import raidpirlib as lib
# used for locking parallel requests
import threading
import sys
import socket
import session
# to sleep...
_timer = lib._timer
try:
#for packing more complicated messages
import msgpack
except ImportError:
print("Requires MsgPack module (http://msgpack.org/)")
sys.exit(1)
import os
_randomnumberfunction = os.urandom
# used for mirror selection...
import random
########################### XORRequestGenerator ################################
# receive thread
def rcvlet(mirror, rxgobj):
sock = mirror['info']['sock']
# first, check if params were received correctly
if session.recvmessage(sock) != b'PARAMS OK':
raise Exception("Params were not delivered correctly or wrong format.")
data = "0"
first = True
while data != '' and len(mirror['blocksrequested']) > 0 or first:
first = False
data = session.recvmessage(sock)
rxgobj.notify_success(mirror['info'], data)
def _reconstruct_block(blockinfolist):
# private helper to reconstruct a block
# xor the blocks together
ret = blockinfolist[0]
for xorblock in blockinfolist[1:]:
ret = xordatastore.do_xor(ret, xorblock)
# and return the answer
return ret
def _reconstruct_block_parallel(responses, chunklen, k, blocklen, blocknumbers):
#reconstruct block(s) from parallel query answer
results = {}
for blocknum in blocknumbers:
#map blocknum to chunk
index = min(int(blocknum/chunklen), k-1)
if index not in results:
results[index] = blocklen*b'\0'
for m in range(k):
for c in results:
if c in responses[m]:
results[c] = xordatastore.do_xor(results[c], responses[m][c])
return results
class InsufficientMirrors(Exception):
"""There are insufficient mirrors to handle your request"""
# Super class of requestors that offers identical functions
class Requestor(object):
def cleanup(self):
"""cleanup. here: maybe request debug timing info and always close sockets"""
for mirror in self.activemirrors:
if self.timing:
# request total computation time and measure delay
ping_start = _timer()
session.sendmessage(mirror['info']['sock'], "T")
mirror['info']['comptime'] = float(session.recvmessage(mirror['info']['sock'])[1:])
mirror['info']['ping'] = _timer() - ping_start
session.sendmessage(mirror['info']['sock'], "Q")
mirror['info']['sock'].close()
def return_timings(self):
comptimes = []
pings = []
for mirror in self.activemirrors:
comptimes.append(mirror['info']['comptime'])
pings.append(mirror['info']['ping'])
return self.recons_time, comptimes, pings
def return_block(self, blocknum):
return self.finishedblockdict[blocknum]
# These provide an easy way for the client XOR request behavior to be
# modified. If you wanted to change the policy by which mirrors are selected,
# the failure behavior for offline mirrors, or the way in which blocks
# are selected.
class RandomXORRequestor(Requestor):
"""
<Purpose>
Basic XORRequestGenerator that just picks some number of random mirrors
and then retrieves all blocks from them. If any mirror fails or is
offline, the operation fails.
The strategy this uses is very, very simple. First we randomly choose
$k$ mirrors we want to retrieve blocks from. If at any point, we have
a failure when retrieving a block, we replace that mirror with a
mirror we haven't chosen yet.
<Side Effects>
None.
"""
def __init__(self, mirrorinfolist, blocklist, manifestdict, privacythreshold, batch, timing):
"""
<Purpose>
Get ready to handle requests for XOR block strings, etc.
<Arguments>
mirrorinfolist: a list of dictionaries with information about mirrors
blocklist: the blocks that need to be retrieved
manifestdict: the manifest with information about the release
privacythreshold: the number of mirrors that would need to collude to break privacy
timing: collect timing info
<Exceptions>
TypeError may be raised if invalid parameters are given.
InsufficientMirrors if there are not enough mirrors
"""
self.blocklist = blocklist
self.manifestdict = manifestdict
self.privacythreshold = privacythreshold
self.timing = timing
if timing:
self.recons_time = 0
if len(mirrorinfolist) < self.privacythreshold:
raise InsufficientMirrors("Requested the use of "+str(self.privacythreshold)+" mirrors, but only "+str(len(mirrorinfolist))+" were available.")
# now we do the 'random' part. I copy the mirrorinfolist to avoid changing the list in place.
self.fullmirrorinfolist = mirrorinfolist[:]
random.shuffle(self.fullmirrorinfolist)
# let's make a list of mirror information (what has been retrieved, etc.)
self.activemirrors = []
for mirrorinfo in self.fullmirrorinfolist[:self.privacythreshold]:
mirrors = {}
mirrors['info'] = mirrorinfo
mirrors['blocksneeded'] = blocklist[:]
mirrors['blockbitstringlist'] = []
mirrors['blocksrequested'] = []
# open a socket once:
mirrors['info']['sock'] = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mirrors['info']['sock'].setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) #TODO check this in the cloud
mirrors['info']['sock'].connect((mirrorinfo['ip'], mirrorinfo['port']))
self.activemirrors.append(mirrors)
for thisrequestinfo in self.activemirrors:
#send parameters to mirrors once
params = {}
params['cn'] = 1 # chunk numbers, here fixed to 1
params['k'] = privacythreshold
params['r'] = privacythreshold # r is irrelevant here, thus fixed to k
params['cl'] = 1 # chunk length, here fixed to 1
params['lcl'] = 1 # last chunk length, here fixed to 1
params['b'] = batch
params['p'] = False
#send the params, rcvlet will check response
session.sendmessage(thisrequestinfo['info']['sock'], b"P" + msgpack.packb(params, use_bin_type=True))
# start separate receiving thread for this socket
t = threading.Thread(target=rcvlet, args=[thisrequestinfo, self], name=("rcv_thread_" + str((thisrequestinfo['info']['ip'], thisrequestinfo['info']['port']))))
thisrequestinfo['rt'] = t
t.start()
bitstringlength = lib.bits_to_bytes(manifestdict['blockcount'])
# let's generate the random bitstrings for k-1 mirrors
for thisrequestinfo in self.activemirrors[:-1]:
for _ in blocklist:
thisrequestinfo['blockbitstringlist'].append(lib.randombits(manifestdict['blockcount']))
# now, let's do the 'derived' ones...
for blocknum in range(len(blocklist)):
thisbitstring = b'\0'*bitstringlength
# xor the random strings together
for requestinfo in self.activemirrors[:-1]:
thisbitstring = xordatastore.do_xor(thisbitstring, requestinfo['blockbitstringlist'][blocknum])
# flip the appropriate bit for the block we want
thisbitstring = lib.flip_bitstring_bit(thisbitstring, blocklist[blocknum])
# store the result for the last mirror
self.activemirrors[-1]['blockbitstringlist'].append(thisbitstring)
# want to have a structure for locking
self.tablelock = threading.Lock()
# and we'll keep track of the ones that are waiting in the wings...
self.backupmirrorinfolist = self.fullmirrorinfolist[self.privacythreshold:]
# the returned blocks are put here...
self.returnedxorblocksdict = {}
for blocknum in blocklist:
# make these all empty lists to start with
self.returnedxorblocksdict[blocknum] = []
# and here is where they are put when reconstructed
self.finishedblockdict = {}
# and we're ready!
def get_next_xorrequest(self, tid):
"""
<Purpose>
Gets the next requesttuple that should be returned
<Arguments>
None
<Exceptions>
InsufficientMirrors if there are not enough mirrors
<Returns>
Either a requesttuple (mirrorinfo, blocknumber, bitstring) or ()
when all strings have been retrieved...
"""
# Two cases I need to worry about:
# 1) nothing that still needs to be requested -> return ()
# 2) there is a request ready -> return the request
mirror = self.activemirrors[tid]
# this mirror is done...
if len(mirror['blocksneeded']) == 0:
return ()
# otherwise set it to be taken...
blocknum = mirror['blocksneeded'].pop()
mirror['blocksrequested'].append(blocknum)
return (mirror['info'], blocknum, mirror['blockbitstringlist'].pop())
def notify_failure(self, xorrequesttuple):
"""
<Purpose>
Handles that a mirror has failed
<Arguments>
The XORrequesttuple that was returned by get_next_xorrequest
<Exceptions>
InsufficientMirrors if there are not enough mirrors
An internal error is raised if the XORrequesttuple is bogus
<Returns>
None
"""
# I should lock the table...
self.tablelock.acquire()
# but *always* release it
try:
# if we're out of replacements, quit
if len(self.backupmirrorinfolist) == 0:
raise InsufficientMirrors("There are no replacement mirrors")
nextmirrorinfo = self.backupmirrorinfolist.pop(0)
failedmirrorsinfo = xorrequesttuple[0]
# now, let's find the activemirror this corresponds to.
for mirror in self.activemirrors:
if mirror['info'] == failedmirrorsinfo:
# let's mark it as inactive and set up a different mirror
mirror['info'] = nextmirrorinfo
return
raise Exception("InternalError: Unknown mirror in notify_failure")
finally:
# release the lock
self.tablelock.release()
def notify_success(self, thismirrorsinfo, xorblock):
"""
<Purpose>
Handles the receipt of an xorblock
<Arguments>
xorrequesttuple: The tuple that was returned by get_next_xorrequest
xorblock: the data returned by the mirror
<Exceptions>
Assertions / IndexError / TypeError / InternalError if the
XORrequesttuple is bogus
<Returns>
None
"""
if self.timing:
stime = _timer()
# acquire the lock...
self.tablelock.acquire()
#... but always release it
try:
# now, let's find the activemirror this corresponds to.
for mirror in self.activemirrors:
if mirror['info'] == thismirrorsinfo:
# remove the block and bitstring (asserting they match what we said before)
blocknumber = mirror['blocksrequested'].pop(0)
# add the xorblockinfo to the dict
self.returnedxorblocksdict[blocknumber].append(xorblock)
# if we don't have all of the pieces, continue
if len(self.returnedxorblocksdict[blocknumber]) != self.privacythreshold:
return
# if we have all of the pieces, reconstruct it
resultingblock = _reconstruct_block(self.returnedxorblocksdict[blocknumber])
# let's check the hash...
resultingblockhash = lib.find_hash(resultingblock, self.manifestdict['hashalgorithm'])
if resultingblockhash != self.manifestdict['blockhashlist'][blocknumber]:
# TODO: We should notify the vendor!
raise Exception('Should notify vendor that one of the mirrors or manifest is corrupt')
# otherwise, let's put this in the finishedblockdict
self.finishedblockdict[blocknumber] = resultingblock
# it should be safe to delete this
del self.returnedxorblocksdict[blocknumber]
return
raise Exception("InternalError: Unknown mirror in notify_success")
finally:
# release the lock
self.tablelock.release()
if self.timing:
self.recons_time = self.recons_time + _timer() - stime
######################################################################
class RandomXORRequestorChunks(Requestor):
def __init__(self, mirrorinfolist, blocklist, manifestdict, privacythreshold, redundancy, rng, parallel, batch, timing):
"""
<Purpose>
Get ready to handle requests for XOR block strings, etc.
This is meant to be used for queries partitioned in chunks
(parallel or SB queries with redundancy parameter)
<Exceptions>
TypeError may be raised if invalid parameters are given.
InsufficientMirrors if there are not enough mirrors
"""
self.blocklist = blocklist
self.manifestdict = manifestdict
self.privacythreshold = privacythreshold # aka k, the number of mirrors to use
self.redundancy = redundancy # aka r
self.rng = rng
self.parallel = parallel
self.blockcount = manifestdict['blockcount']
self.timing = timing
if timing:
self.recons_time = 0
#length of one chunk in BITS (1 bit per block)
#chunk length of the first chunks must be a multiple of 8, last chunk can be longer than first chunks
self.chunklen = int(self.blockcount/8/privacythreshold) * 8
self.lastchunklen = self.blockcount - (privacythreshold-1)*self.chunklen
if len(mirrorinfolist) < self.privacythreshold:
raise InsufficientMirrors("Requested the use of " + str(self.privacythreshold) + " mirrors, but only " + str(len(mirrorinfolist)) + " were available.")
# now we do the 'random' part. I copy the mirrorinfolist to avoid changing the list in place.
self.fullmirrorinfolist = mirrorinfolist[:]
random.shuffle(self.fullmirrorinfolist)
# let's make a list of mirror information (what has been retrieved, etc.)
self.activemirrors = []
#initialize queries for mirrors
i = 0
for mirrorinfo in self.fullmirrorinfolist[:self.privacythreshold]:
mirror = {}
mirror['info'] = mirrorinfo
mirror['blocksneeded'] = blocklist[:] # only for the client, obviously
mirror['blocksrequested'] = []
if parallel:
mirror['parallelblocksneeded'] = []
mirror['blockchunklist'] = []
# chunk numbers [0, ..., r-1]
mirror['chunknumbers'] = [i]
for j in range(1, redundancy):
mirror['chunknumbers'].append((i+j) % privacythreshold)
i = i + 1
#open a socket once:
mirror['info']['sock'] = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mirror['info']['sock'].connect((mirrorinfo['ip'], mirrorinfo['port']))
if rng:
#pick a random seed (key) and initialize AES
seed = _randomnumberfunction(16) # random 128 bit key
mirror['seed'] = seed
mirror['cipher'] = lib.initAES(seed)
self.activemirrors.append(mirror)
for mirror in self.activemirrors:
#send parameters to mirrors once
params = {}
params['cn'] = mirror['chunknumbers']
params['k'] = privacythreshold
params['r'] = redundancy
params['cl'] = self.chunklen
params['lcl'] = self.lastchunklen
params['b'] = batch
params['p'] = parallel
if rng:
params['s'] = mirror['seed']
#send the params, rcvlet will check response
session.sendmessage(mirror['info']['sock'], b"P" + msgpack.packb(params, use_bin_type=True))
# start separate receiving thread for this socket
t = threading.Thread(target=rcvlet, args=[mirror, self], name=("rcv_thread_" + str((mirror['info']['ip'], mirror['info']['port']))))
mirror['rt'] = t
t.start()
#multi block query. map the blocks to the minimum amount of queries
if parallel:
#create dictionary for each chunk, will hold block indices per chunk
blockchunks = {}
for i in range(0, privacythreshold):
blockchunks[i] = []
#map block numbers to chunks
for blocknum in blocklist:
index = min(int(blocknum/self.chunklen), privacythreshold-1)
blockchunks[index].append(blocknum)
#remove chunks that are still empty
for i in range(0, privacythreshold):
if len(blockchunks[i]) == 0:
del blockchunks[i]
#do until all blocks are in queries
while len(blockchunks)>0:
#iterate through mirrors
for mirror in self.activemirrors:
#dicitonary of chunk requests
chunks = {}
#iterate through r-1 random chunks, skipping the head (flip) chunk
for c in mirror['chunknumbers'][1:]:
#pick correct length in bits
if c == self.privacythreshold - 1:
length = self.lastchunklen
else:
length = self.chunklen
if rng:
#set random bytes for the latter chunk(s) from AES (will be deleted later)
chunks[c] = lib.nextrandombitsAES(mirror['cipher'], length)
else:
#set random bytes for the latter chunk(s) randomly
chunks[c] = lib.randombits(length)
mirror['blockchunklist'].append(chunks)
#list of blocknumbers
blocks = []
# now derive the first chunks
for mirror in self.activemirrors:
#number of the first chunk
c = mirror['chunknumbers'][0]
#pick correct length for the chunk
if c == self.privacythreshold - 1:
length = self.lastchunklen
else:
length = self.chunklen
#fill it with zero
thisbitstring = lib.bits_to_bytes(length)*b'\0'
#xor all other rnd chunks onto it
for rqi in self.activemirrors:
if c in rqi['blockchunklist'][-1]:
thisbitstring = xordatastore.do_xor(thisbitstring, rqi['blockchunklist'][-1][c])
if rng:
del rqi['blockchunklist'][-1][c] #remove the pre-computed random chunk from the packet to send
#if there is a block within this chunk, then add it to the bitstring by flipping the bit
if c in blockchunks:
blocknum = blockchunks[c].pop(0)
thisbitstring = lib.flip_bitstring_bit(thisbitstring, blocknum - c*self.chunklen)
blocks.append(blocknum)
if len(blockchunks[c]) == 0:
del blockchunks[c]
mirror['parallelblocksneeded'].append(blocks)
mirror['blockchunklist'][-1][c] = thisbitstring
#single block query:
else:
#iterate through all blocks
for blocknum in blocklist:
#iterate through mirrors
for mirror in self.activemirrors:
chunks = {}
#iterate through r-1 random chunks
for c in mirror['chunknumbers'][1:]:
#pick correct length in bits
if c == self.privacythreshold - 1:
length = self.lastchunklen
else:
length = self.chunklen
if rng:
chunks[c] = lib.nextrandombitsAES(mirror['cipher'], length)
else:
#set random bytes for the latter chunk(s)
chunks[c] = lib.randombits(length)
mirror['blockchunklist'].append(chunks)
# now derive the first chunks
for mirror in self.activemirrors:
#number of the first chunk
c = mirror['chunknumbers'][0]
#pick correct length for the chunk
if c == self.privacythreshold - 1:
length = self.lastchunklen
else:
length = self.chunklen
#fill it with zero
thisbitstring = lib.bits_to_bytes(length)*b'\0'
#xor all other rnd chunks onto it
for rqi in self.activemirrors:
if c in rqi['blockchunklist'][-1]:
thisbitstring = xordatastore.do_xor(thisbitstring, rqi['blockchunklist'][-1][c])
if rng:
del rqi['blockchunklist'][-1][c] #remove the pre-computed random chunk from the packet to send
#if the desired block is within this chunk, flip the bit
if c*self.chunklen <= blocknum and blocknum < c*self.chunklen + length:
thisbitstring = lib.flip_bitstring_bit(thisbitstring, blocknum - c*self.chunklen)
mirror['blockchunklist'][-1][c] = thisbitstring
########################################
# want to have a structure for locking
self.tablelock = threading.Lock()
# and we'll keep track of the ones that are waiting in the wings...
self.backupmirrorinfolist = self.fullmirrorinfolist[self.privacythreshold:]
# the returned blocks are put here...
self.returnedxorblocksdict = {}
for blocknum in blocklist:
# make these all empty lists to start with
self.returnedxorblocksdict[blocknum] = []
# and here is where they are put when reconstructed
self.finishedblockdict = {}
# preparation done. queries are ready to be sent.
# chunked version:
def get_next_xorrequest(self, tid):
"""
<Purpose>
Gets the next request tuple that should be returned
<Arguments>
None
<Exceptions>
InsufficientMirrors if there are not enough mirrors
<Returns>
Either a requesttuple (mirrorinfo, blocknumber, bitstring) or ()
when all strings have been retrieved...
"""
requestinfo = self.activemirrors[tid]
if self.parallel:
if len(requestinfo['parallelblocksneeded']) == 0:
return ()
blocknums = requestinfo['parallelblocksneeded'].pop(0)
requestinfo['blocksrequested'].append(blocknums)
if self.rng:
return (requestinfo['info'], blocknums, requestinfo['blockchunklist'].pop(0), 2)
else:
raise Exception("Parallel Query without RNG not yet implemented!")
#single block
else:
# this mirror is done...
if len(requestinfo['blocksneeded']) == 0:
return ()
blocknum = requestinfo['blocksneeded'].pop(0)
requestinfo['blocksrequested'].append(blocknum)
if self.rng:
return (requestinfo['info'], blocknum, requestinfo['blockchunklist'].pop(0), 1)
else:
return (requestinfo['info'], blocknum, requestinfo['blockchunklist'].pop(0), 0)
def notify_failure(self, xorrequesttuple):
"""
<Purpose>
Handles that a mirror has failed
<Arguments>
The XORrequesttuple that was returned by get_next_xorrequest
<Exceptions>
InsufficientMirrors if there are not enough mirrors
An internal error is raised if the XORrequesttuple is bogus
<Returns>
None
"""
# I should lock the table...
self.tablelock.acquire()
# but *always* release it
try:
# if we're out of replacements, quit
if len(self.backupmirrorinfolist) == 0:
raise InsufficientMirrors("There are no replacement mirrors")
nextmirrorinfo = self.backupmirrorinfolist.pop(0)
failedmirrorsinfo = xorrequesttuple[0]
# now, let's find the activemirror this corresponds to.
for mirror in self.activemirrors:
if mirror['info'] == failedmirrorsinfo:
# let's mark it as inactive and set up a different mirror
mirror['info'] = nextmirrorinfo
return
raise Exception("InternalError: Unknown mirror in notify_failure")
finally:
# release the lock
self.tablelock.release()
def notify_success(self, thismirrorsinfo, xorblock):
"""
<Purpose>
Handles the receipt of an xorblock
<Arguments>
xorrequesttuple: The tuple that was returned by get_next_xorrequest
xorblock: the data returned by the mirror
<Exceptions>
Assertions / IndexError / TypeError / InternalError if the
XORrequesttuple is bogus
<Returns>
None
"""
if self.timing:
stime = _timer()
# acquire the lock...
self.tablelock.acquire()
try:
# now, let's find the activemirror this corresponds to.
for mirror in self.activemirrors:
if mirror['info'] == thismirrorsinfo:
if self.parallel:
#use blocknumbers[0] as index from now on
blocknumbers = mirror['blocksrequested'].pop(0)
# add the xorblocks to the dict
self.returnedxorblocksdict[blocknumbers[0]].append(msgpack.unpackb(xorblock, raw=False))
#print "Appended blocknumber", blocknumbers[0], "from", thismirrorsinfo['port']
# if we don't have all of the pieces, continue
if len(self.returnedxorblocksdict[blocknumbers[0]]) != self.privacythreshold:
return
# if we have all of the pieces, reconstruct it
resultingblockdict = _reconstruct_block_parallel(self.returnedxorblocksdict[blocknumbers[0]], self.chunklen, self.privacythreshold, self.manifestdict['blocksize'], blocknumbers)
#parse resultingblocks into single blocks
for blocknumber in blocknumbers:
index = min(int(blocknumber/self.chunklen), self.privacythreshold-1)
# let's check the hash...
resultingblockhash = lib.find_hash(resultingblockdict[index], self.manifestdict['hashalgorithm'])
if resultingblockhash != self.manifestdict['blockhashlist'][blocknumber]:
print(mirror)
# TODO: We should notify the vendor!
raise Exception('Should notify vendor that one of the mirrors or manifest is corrupt, for blocknumber ' + str(blocknumber))
# otherwise, let's put this in the finishedblockdict
self.finishedblockdict[blocknumber] = resultingblockdict[index]
# it should be safe to delete this
del self.returnedxorblocksdict[blocknumbers[0]]
return
#single block query:
else:
# remove the block and bitstring (asserting they match what we said before)
blocknumber = mirror['blocksrequested'].pop(0)
# add the xorblock to the dict
self.returnedxorblocksdict[blocknumber].append(xorblock)
# if we don't have all of the pieces, continue
if len(self.returnedxorblocksdict[blocknumber]) != self.privacythreshold:
return
# if we have all of the pieces, reconstruct it
resultingblock = _reconstruct_block(self.returnedxorblocksdict[blocknumber])
# let's check the hash...
resultingblockhash = lib.find_hash(resultingblock, self.manifestdict['hashalgorithm'])
if resultingblockhash != self.manifestdict['blockhashlist'][blocknumber]:
print(mirror)
# TODO: We should notify the vendor!
raise Exception('Should notify vendor that one of the mirrors or manifest is corrupt')
# otherwise, let's put this in the finishedblockdict
self.finishedblockdict[blocknumber] = resultingblock
# it should be safe to delete this
del self.returnedxorblocksdict[blocknumber]
return
raise Exception("InternalError: Unknown mirror in notify_success")
finally:
# release the lock
self.tablelock.release()
if self.timing:
self.recons_time = self.recons_time + _timer() - stime