-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDevice_bfl.py
1451 lines (1243 loc) · 77 KB
/
Device_bfl.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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ***************************************References*************************************************************
# [1] H. Chen, S. A. Asif, J. Park, C.-C. Shen, and M. Bennis, “Robust Blockchained Federated Learning with Model Validation and Proof-of-Stake Inspired Consensus.” arXiv, Jan. 09, 2021. Accessed: Nov. 12, 2022. [Online]. Available: http://arxiv.org/abs/2101.03300
# Github Link: https://github.com/hanglearning/VBFL.git
# [1] H. Zhao, “Exact Decomposition of Quantum Channels for Non-IID Quantum Federated Learning.” arXiv, Sep. 01, 2022. Accessed: Nov. 06, 2022. [Online]. Available: http://arxiv.org/abs/2209.00768
# Github Link: https://github.com/JasonZHM/quantum-fed-infer.git (MIT Licence)
# ***************************************References*************************************************************
import torch
from torch import optim
import random
import copy
import time
from sys import getsizeof
from Crypto.PublicKey import RSA
from hashlib import sha256
from Blockchain import Blockchain
from tqdm import tqdm
class Device:
def __init__(self, idx, assigned_train_ds, assigned_test_dl, local_batch_size, learning_rate, loss_func, opti,
network_stability, net, dev, miner_acception_wait_time, miner_accepted_transactions_size_limit,
validator_threshold, pow_difficulty, even_link_speed_strength, base_data_transmission_speed,
even_computation_power, is_malicious, noise_variance, check_signature, not_resync_chain,
malicious_updates_discount, knock_out_rounds, lazy_worker_knock_out_rounds):
self.idx = idx
self.train_ds = assigned_train_ds
self.test_dl = assigned_test_dl
self.local_batch_size = local_batch_size
self.loss_func = loss_func
self.network_stability = network_stability
self.net = copy.deepcopy(net)
if opti == "SGD":
self.opti = optim.SGD(self.net.parameters(), lr=learning_rate)
self.dev = dev
self.train_dl = None
self.local_train_parameters = None
self.initial_net_parameters = None
self.global_parameters = None
self.role = None
self.pow_difficulty = pow_difficulty
if even_link_speed_strength:
self.link_speed = base_data_transmission_speed
else:
self.link_speed = random.random() * base_data_transmission_speed
self.devices_dict = None
self.aio = False
''' simulating hardware equipment strength, such as good processors and RAM capacity. Following recorded times will be shrunk by this value of times
'''
if even_computation_power:
self.computation_power = 1
else:
self.computation_power = random.randint(0, 4)
self.peer_list = set()
self.online = True
self.rewards = 0
self.blockchain = Blockchain()
self.modulus = None
self.private_key = None
self.public_key = None
self.generate_rsa_key()
self.black_list = set()
self.knock_out_rounds = knock_out_rounds
self.lazy_worker_knock_out_rounds = lazy_worker_knock_out_rounds
self.worker_accuracy_accross_records = {}
self.has_added_block = False
self.the_added_block = None
self.is_malicious = is_malicious
self.noise_variance = noise_variance
self.check_signature = check_signature
self.not_resync_chain = not_resync_chain
self.malicious_updates_discount = malicious_updates_discount
self.active_worker_record_by_round = {}
self.untrustworthy_workers_record_by_comm_round = {}
self.untrustworthy_validators_record_by_comm_round = {}
self.round_end_time = 0
''' For workers '''
self.local_updates_rewards_per_transaction = 0
self.received_block_from_miner = None
self.accuracy_this_round = float('-inf')
self.worker_associated_validator = None
self.worker_associated_miner = None
self.local_update_time = None
self.local_total_epoch = 0
''' For validators '''
self.validator_associated_worker_set = set()
self.validation_rewards_this_round = 0
self.accuracies_this_round = {}
self.validator_associated_miner = None
self.unordered_arrival_time_accepted_worker_transactions = {}
self.validator_accepted_broadcasted_worker_transactions = None or []
self.final_transactions_queue_to_validate = {}
self.post_validation_transactions_queue = None or []
self.validator_threshold = validator_threshold
self.validator_local_accuracy = None
''' For miners '''
self.miner_associated_worker_set = set()
self.miner_associated_validator_set = set()
self.unconfirmmed_transactions = None or []
self.broadcasted_transactions = None or []
self.mined_block = None
self.received_propagated_block = None
self.received_propagated_validator_block = None
self.miner_acception_wait_time = miner_acception_wait_time
self.miner_accepted_transactions_size_limit = miner_accepted_transactions_size_limit
self.unordered_arrival_time_accepted_validator_transactions = {}
self.miner_accepted_broadcasted_validator_transactions = None or []
self.final_candidate_transactions_queue_to_mine = {}
self.block_generation_time_point = None
self.unordered_propagated_block_processing_queue = {}
''' For malicious node '''
self.variance_of_noises = None or []
def set_devices_dict_and_aio(self, devices_dict, aio):
self.devices_dict = devices_dict
self.aio = aio
def generate_rsa_key(self):
keyPair = RSA.generate(bits=1024)
self.modulus = keyPair.n
self.private_key = keyPair.d
self.public_key = keyPair.e
def init_global_parameters(self):
self.initial_net_parameters = self.net.state_dict()
self.global_parameters = self.net.state_dict()
def assign_role(self):
role_choice = random.randint(0, 2)
if role_choice == 0:
self.role = "worker"
elif role_choice == 1:
self.role = "miner"
else:
self.role = "validator"
def assign_miner_role(self):
self.role = "miner"
def assign_worker_role(self):
self.role = "worker"
def assign_validator_role(self):
self.role = "validator"
def return_idx(self):
return self.idx
def return_rsa_pub_key(self):
return {"modulus": self.modulus, "pub_key": self.public_key}
def return_peers(self):
return self.peer_list
def return_role(self):
return self.role
def is_online(self):
return self.online
def return_is_malicious(self):
return self.is_malicious
def return_black_list(self):
return self.black_list
def return_blockchain_object(self):
return self.blockchain
def return_stake(self):
return self.rewards
def return_computation_power(self):
return self.computation_power
def return_the_added_block(self):
return self.the_added_block
def return_round_end_time(self):
return self.round_end_time
def sign_msg(self, msg):
hash = int.from_bytes(sha256(str(msg).encode('utf-8')).digest(), byteorder='big')
signature = pow(hash, self.private_key, self.modulus)
return signature
def add_peers(self, new_peers):
if isinstance(new_peers, Device):
self.peer_list.add(new_peers)
else:
self.peer_list.update(new_peers)
def remove_peers(self, peers_to_remove):
if isinstance(peers_to_remove, Device):
self.peer_list.discard(peers_to_remove)
else:
self.peer_list.difference_update(peers_to_remove)
def online_switcher(self):
old_status = self.online
if 1 == 1: #no switching online offline for now
self.online = True
# if back online, update peer and resync chain
if old_status == False:
print(f"{self.idx} goes back online.")
self.update_peer_list()
if self.pow_resync_chain():
self.update_model_after_chain_resync()
else:
self.online = False
print(f"{self.idx} goes offline.")
return self.online
def update_peer_list(self):
print(f"\n{self.idx} - {self.role} is updating peer list...")
old_peer_list = copy.copy(self.peer_list)
online_peers = set()
for peer in self.peer_list:
if peer.is_online():
online_peers.add(peer)
for online_peer in online_peers:
self.add_peers(online_peer.return_peers())
self.remove_peers(self)
potential_malicious_peer_set = set()
for peer in self.peer_list:
if peer.return_idx() in self.black_list:
potential_malicious_peer_set.add(peer)
self.remove_peers(potential_malicious_peer_set)
if old_peer_list == self.peer_list:
print("Peer list NOT changed.")
else:
print("Peer list has been changed.")
added_peers = self.peer_list.difference(old_peer_list)
if potential_malicious_peer_set:
print("These malicious peers are removed")
for peer in removed_peers:
print(f"d_{peer.return_idx().split('_')[-1]} - {peer.return_role()[0]}", end=', ')
print()
if added_peers:
print("These peers are added")
for peer in added_peers:
print(f"d_{peer.return_idx().split('_')[-1]} - {peer.return_role()[0]}", end=', ')
print()
print("Final peer list:")
for peer in self.peer_list:
print(f"d_{peer.return_idx().split('_')[-1]} - {peer.return_role()[0]}", end=', ')
print()
return False if not self.peer_list else True
def check_pow_proof(self, block_to_check):
pow_proof = block_to_check.return_pow_proof()
return pow_proof.startswith('0' * self.pow_difficulty) and pow_proof == block_to_check.compute_hash()
def check_chain_validity(self, chain_to_check):
chain_len = chain_to_check.return_chain_length()
if chain_len == 0 or chain_len == 1:
pass
else:
chain_to_check = chain_to_check.return_chain_structure()
for block in chain_to_check[1:]:
if self.check_pow_proof(block) and block.return_previous_block_hash() == chain_to_check[
chain_to_check.index(block) - 1].compute_hash(hash_entire_block=True):
pass
else:
return False
return True
def accumulate_chain_stake(self, chain_to_accumulate):
accumulated_stake = 0
chain_to_accumulate = chain_to_accumulate.return_chain_structure()
for block in chain_to_accumulate:
accumulated_stake += self.devices_dict[block.return_mined_by()].return_stake()
return accumulated_stake
def resync_chain(self, mining_consensus):
if self.not_resync_chain:
return
if mining_consensus == 'PoW':
self.pow_resync_chain()
else:
self.pos_resync_chain()
def pos_resync_chain(self):
print(
f"{self.role} {self.idx} is looking for a chain with the highest accumulated miner's stake in the network...")
highest_stake_chain = None
updated_from_peer = None
curr_chain_stake = self.accumulate_chain_stake(self.return_blockchain_object())
for peer in self.peer_list:
if peer.is_online():
peer_chain = peer.return_blockchain_object()
peer_chain_stake = self.accumulate_chain_stake(peer_chain)
if peer_chain_stake > curr_chain_stake:
if self.check_chain_validity(peer_chain):
print(
f"A chain from {peer.return_idx()} with total stake {peer_chain_stake} has been found (> currently compared chain stake {curr_chain_stake}) and verified.")
curr_chain_stake = peer_chain_stake
highest_stake_chain = peer_chain
updated_from_peer = peer.return_idx()
else:
print(
f"A chain from {peer.return_idx()} with higher stake has been found BUT NOT verified. Skipped this chain for syncing.")
if highest_stake_chain:
highest_stake_chain_structure = highest_stake_chain.return_chain_structure()
self.return_blockchain_object().replace_chain(highest_stake_chain_structure)
print(f"{self.idx} chain resynced from peer {updated_from_peer}.")
return True
print("Chain not resynced.")
return False
def pow_resync_chain(self):
print(f"{self.role} {self.idx} is looking for a longer chain in the network...")
longest_chain = None
updated_from_peer = None
curr_chain_len = self.return_blockchain_object().return_chain_length()
for peer in self.peer_list:
if peer.is_online():
peer_chain = peer.return_blockchain_object()
if peer_chain.return_chain_length() > curr_chain_len:
if self.check_chain_validity(peer_chain):
print(
f"A longer chain from {peer.return_idx()} with chain length {peer_chain.return_chain_length()} has been found (> currently compared chain length {curr_chain_len}) and verified.")
curr_chain_len = peer_chain.return_chain_length()
longest_chain = peer_chain
updated_from_peer = peer.return_idx()
else:
print(
f"A longer chain from {peer.return_idx()} has been found BUT NOT verified. Skipped this chain for syncing.")
if longest_chain:
longest_chain_structure = longest_chain.return_chain_structure()
self.return_blockchain_object().replace_chain(longest_chain_structure)
print(f"{self.idx} chain resynced from peer {updated_from_peer}.")
return True
print("Chain not resynced.")
return False
def receive_rewards(self, rewards):
self.rewards += rewards
def verify_miner_transaction_by_signature(self, transaction_to_verify, miner_device_idx):
if miner_device_idx in self.black_list:
print(f"{miner_device_idx} is in miner's blacklist. Trasaction won't get verified.")
return False
if self.check_signature:
transaction_before_signed = copy.deepcopy(transaction_to_verify)
del transaction_before_signed["miner_signature"]
modulus = transaction_to_verify['miner_rsa_pub_key']["modulus"]
pub_key = transaction_to_verify['miner_rsa_pub_key']["pub_key"]
signature = transaction_to_verify["miner_signature"]
hash = int.from_bytes(sha256(str(sorted(transaction_before_signed.items())).encode('utf-8')).digest(),
byteorder='big')
hashFromSignature = pow(signature, pub_key, modulus)
if hash == hashFromSignature:
print(f"A transaction recorded by miner {miner_device_idx} in the block is verified!")
return True
else:
print(f"Signature invalid. Transaction recorded by {miner_device_idx} is NOT verified.")
return False
else:
print(f"A transaction recorded by miner {miner_device_idx} in the block is verified!")
return True
def verify_block(self, block_to_verify, sending_miner):
if not self.online_switcher():
print(f"{self.idx} goes offline when verifying a block")
return False, False
verification_time = time.time()
mined_by = block_to_verify.return_mined_by()
if sending_miner in self.black_list:
print(
f"The miner propagating/sending this block {sending_miner} is in {self.idx}'s black list. Block will not be verified.")
return False, False
if mined_by in self.black_list:
print(f"The miner {mined_by} mined this block is in {self.idx}'s black list. Block will not be verified.")
return False, False
if not self.check_pow_proof(block_to_verify):
print(f"PoW proof of the block from miner {self.idx} is not verified.")
return False, False
if self.check_signature:
signature_dict = block_to_verify.return_miner_rsa_pub_key()
modulus = signature_dict["modulus"]
pub_key = signature_dict["pub_key"]
signature = block_to_verify.return_signature()
block_to_verify_before_sign = copy.deepcopy(block_to_verify)
block_to_verify_before_sign.remove_signature_for_verification()
hash = int.from_bytes(sha256(str(block_to_verify_before_sign.__dict__).encode('utf-8')).digest(),
byteorder='big')
hashFromSignature = pow(signature, pub_key, modulus)
if hash != hashFromSignature:
print(
f"Signature of the block sent by miner {sending_miner} mined by miner {mined_by} is not verified by {self.role} {self.idx}.")
return False, False
last_block = self.return_blockchain_object().return_last_block()
if last_block is not None:
last_block_hash = last_block.compute_hash(hash_entire_block=True)
if block_to_verify.return_previous_block_hash() != last_block_hash:
print(
f"Block sent by miner {sending_miner} mined by miner {mined_by} has the previous hash recorded as {block_to_verify.return_previous_block_hash()}, but the last block's hash in chain is {last_block_hash}. This is possibly due to a forking event from last round. Block not verified and won't be added. Device needs to resync chain next round.")
return False, False
print(f"Block accepted from miner {sending_miner} mined by {mined_by} has been verified by {self.idx}!")
verification_time = (time.time() - verification_time) / self.computation_power
return block_to_verify, verification_time
def add_block(self, block_to_add):
self.return_blockchain_object().append_block(block_to_add)
print(
f"d_{self.idx.split('_')[-1]} - {self.role[0]} has appened a block to its chain. Chain length now - {self.return_blockchain_object().return_chain_length()}")
self.the_added_block = block_to_add
return True
def process_block(self, block_to_process, log_files_folder_path, conn, conn_cursor, when_resync=False):
processing_time = time.time()
if not self.online_switcher():
print(
f"{self.role} {self.idx} goes offline when processing the added block. Model not updated and rewards information not upgraded. Outdated information may be obtained by this node if it never resyncs to a different chain.")
if block_to_process:
mined_by = block_to_process.return_mined_by()
if mined_by in self.black_list:
print(
f"The added block is mined by miner {block_to_process.return_mined_by()}, which is in this device's black list. Block will not be processed.")
else:
self_rewards_accumulator = 0
valid_transactions_records_by_worker = {}
valid_validator_sig_worker_transacitons_in_block = block_to_process.return_transactions()[
'valid_validator_sig_transacitons']
comm_round = block_to_process.return_block_idx()
self.active_worker_record_by_round[comm_round] = set()
for valid_validator_sig_worker_transaciton in valid_validator_sig_worker_transacitons_in_block:
if self.verify_miner_transaction_by_signature(valid_validator_sig_worker_transaciton, mined_by):
worker_device_idx = valid_validator_sig_worker_transaciton['worker_device_idx']
self.active_worker_record_by_round[comm_round].add(worker_device_idx)
if not worker_device_idx in valid_transactions_records_by_worker.keys():
valid_transactions_records_by_worker[worker_device_idx] = {}
valid_transactions_records_by_worker[worker_device_idx]['positive_epochs'] = set()
valid_transactions_records_by_worker[worker_device_idx]['negative_epochs'] = set()
valid_transactions_records_by_worker[worker_device_idx]['all_valid_epochs'] = set()
valid_transactions_records_by_worker[worker_device_idx]['finally_used_params'] = None
local_epoch_seq = valid_validator_sig_worker_transaciton[
'local_total_accumulated_epochs_this_round']
positive_direction_validators = valid_validator_sig_worker_transaciton[
'positive_direction_validators']
negative_direction_validators = valid_validator_sig_worker_transaciton[
'negative_direction_validators']
if len(positive_direction_validators) >= len(negative_direction_validators):
valid_transactions_records_by_worker[worker_device_idx]['positive_epochs'].add(
local_epoch_seq)
valid_transactions_records_by_worker[worker_device_idx]['all_valid_epochs'].add(
local_epoch_seq)
if local_epoch_seq == max(
valid_transactions_records_by_worker[worker_device_idx]['all_valid_epochs']):
valid_transactions_records_by_worker[worker_device_idx]['finally_used_params'] = \
valid_validator_sig_worker_transaciton['local_updates_params']
if self.idx == worker_device_idx:
self_rewards_accumulator += valid_validator_sig_worker_transaciton[
'local_updates_rewards']
else:
if self.malicious_updates_discount:
valid_transactions_records_by_worker[worker_device_idx]['negative_epochs'].add(
local_epoch_seq)
valid_transactions_records_by_worker[worker_device_idx]['all_valid_epochs'].add(
local_epoch_seq)
if local_epoch_seq == max(
valid_transactions_records_by_worker[worker_device_idx]['all_valid_epochs']):
discounted_valid_validator_sig_worker_transaciton_local_updates_params = copy.deepcopy(
valid_validator_sig_worker_transaciton['local_updates_params'])
for var in discounted_valid_validator_sig_worker_transaciton_local_updates_params:
discounted_valid_validator_sig_worker_transaciton_local_updates_params[
var] *= self.malicious_updates_discount
valid_transactions_records_by_worker[worker_device_idx][
'finally_used_params'] = discounted_valid_validator_sig_worker_transaciton_local_updates_params
if self.idx == worker_device_idx:
self_rewards_accumulator += valid_validator_sig_worker_transaciton[
'local_updates_rewards'] * self.malicious_updates_discount
else:
valid_transactions_records_by_worker[worker_device_idx]['negative_epochs'].add(
local_epoch_seq)
for validator_record in positive_direction_validators + negative_direction_validators:
if self.idx == validator_record['validator']:
self_rewards_accumulator += validator_record['validation_rewards']
if self.idx == validator_record['miner_device_idx']:
self_rewards_accumulator += validator_record['miner_rewards_for_this_tx']
else:
print(
f"one validator transaction miner sig found invalid in this block. {self.idx} will drop this block and roll back rewards information")
return
self.untrustworthy_workers_record_by_comm_round[comm_round] = set()
for worker_idx, local_updates_direction_records in valid_transactions_records_by_worker.items():
if len(local_updates_direction_records['negative_epochs']) > len(
local_updates_direction_records['positive_epochs']):
self.untrustworthy_workers_record_by_comm_round[comm_round].add(worker_idx)
kick_out_accumulator = 1
for comm_round_to_check in range(comm_round - self.knock_out_rounds + 1, comm_round):
if comm_round_to_check in self.untrustworthy_workers_record_by_comm_round.keys():
if worker_idx in self.untrustworthy_workers_record_by_comm_round[comm_round_to_check]:
kick_out_accumulator += 1
if kick_out_accumulator == self.knock_out_rounds:
self.black_list.add(worker_idx)
if when_resync:
msg_end = " when resyncing!\n"
else:
msg_end = "!\n"
if self.devices_dict[worker_idx].return_is_malicious():
msg = f"{self.idx} has successfully identified a malicious worker device {worker_idx} in comm_round {comm_round}{msg_end}"
with open(f"{log_files_folder_path}/correctly_kicked_workers.txt", 'a') as file:
file.write(msg)
conn_cursor.execute("INSERT INTO malicious_workers_log VALUES (?, ?, ?, ?, ?, ?)",
(worker_idx, 1, self.idx, "", comm_round, when_resync))
conn.commit()
else:
msg = f"WARNING: {self.idx} has mistakenly regard {worker_idx} as a malicious worker device in comm_round {comm_round}{msg_end}"
with open(f"{log_files_folder_path}/mistakenly_kicked_workers.txt", 'a') as file:
file.write(msg)
conn_cursor.execute("INSERT INTO malicious_workers_log VALUES (?, ?, ?, ?, ?, ?)",
(worker_idx, 0, "", self.idx, comm_round, when_resync))
conn.commit()
print(msg)
self.untrustworthy_validators_record_by_comm_round[comm_round] = set()
invalid_validator_sig_worker_transacitons_in_block = block_to_process.return_transactions()[
'invalid_validator_sig_transacitons']
for invalid_validator_sig_worker_transaciton in invalid_validator_sig_worker_transacitons_in_block:
if self.verify_miner_transaction_by_signature(invalid_validator_sig_worker_transaciton, mined_by):
validator_device_idx = invalid_validator_sig_worker_transaciton['validator']
self.untrustworthy_validators_record_by_comm_round[comm_round].add(validator_device_idx)
kick_out_accumulator = 1
for comm_round_to_check in range(comm_round - self.knock_out_rounds + 1, comm_round):
if comm_round_to_check in self.untrustworthy_validators_record_by_comm_round.keys():
if validator_device_idx in self.untrustworthy_validators_record_by_comm_round[
comm_round_to_check]:
kick_out_accumulator += 1
if kick_out_accumulator == self.knock_out_rounds:
self.black_list.add(validator_device_idx)
print(
f"{validator_device_idx} has been regarded as a compromised validator by {self.idx} in {comm_round}.")
else:
print(
f"one validator transaction miner sig found invalid in this block. {self.idx} will drop this block and roll back rewards information")
return
if self.idx == invalid_validator_sig_worker_transaciton['miner_device_idx']:
self_rewards_accumulator += invalid_validator_sig_worker_transaciton[
'miner_rewards_for_this_tx']
if self.idx == mined_by:
self_rewards_accumulator += block_to_process.return_mining_rewards()
self.receive_rewards(self_rewards_accumulator)
print(
f"{self.role} {self.idx} has received total {self_rewards_accumulator} rewards for this comm round.")
finally_used_local_params = []
for worker_device_idx, local_params_record in valid_transactions_records_by_worker.items():
if local_params_record['finally_used_params']:
finally_used_local_params.append(
(worker_device_idx, local_params_record['finally_used_params']))
if self.online_switcher():
self.global_update(finally_used_local_params)
else:
print(f"Unfortunately, {self.role} {self.idx} goes offline when it's doing global_updates.")
processing_time = (time.time() - processing_time) / self.computation_power
return processing_time
def add_to_round_end_time(self, time_to_add):
self.round_end_time += time_to_add
def other_tasks_at_the_end_of_comm_round(self, this_comm_round, log_files_folder_path):
self.kick_out_slow_or_lazy_workers(this_comm_round, log_files_folder_path)
def kick_out_slow_or_lazy_workers(self, this_comm_round, log_files_folder_path):
for device in self.peer_list:
if device.return_role() == 'worker':
if this_comm_round in self.active_worker_record_by_round.keys():
if not device.return_idx() in self.active_worker_record_by_round[this_comm_round]:
not_active_accumulator = 1
for comm_round_to_check in range(this_comm_round - self.lazy_worker_knock_out_rounds + 1,
this_comm_round):
if comm_round_to_check in self.active_worker_record_by_round.keys():
if not device.return_idx() in self.active_worker_record_by_round[comm_round_to_check]:
not_active_accumulator += 1
if not_active_accumulator == self.lazy_worker_knock_out_rounds:
self.black_list.add(device.return_idx())
msg = f"worker {device.return_idx()} has been regarded as a lazy worker by {self.idx} in comm_round {this_comm_round}.\n"
with open(f"{log_files_folder_path}/kicked_lazy_workers.txt", 'a') as file:
file.write(msg)
else:
pass
def update_model_after_chain_resync(self, log_files_folder_path, conn, conn_cursor):
self.global_parameters = copy.deepcopy(self.initial_net_parameters)
for block in self.return_blockchain_object().return_chain_structure():
self.process_block(block, log_files_folder_path, conn, conn_cursor, when_resync=True)
def return_pow_difficulty(self):
return self.pow_difficulty
def register_in_the_network(self, check_online=False):
if self.aio:
self.add_peers(set(self.devices_dict.values()))
else:
potential_registrars = set(self.devices_dict.values())
potential_registrars.discard(self)
registrar = random.sample(potential_registrars, 1)[0]
if check_online:
if not registrar.is_online():
online_registrars = set()
for registrar in potential_registrars:
if registrar.is_online():
online_registrars.add(registrar)
if not online_registrars:
return False
registrar = random.sample(online_registrars, 1)[0]
self.add_peers(registrar)
self.add_peers(registrar.return_peers())
registrar.add_peers(self)
return True
def malicious_worker_add_noise_to_weights(self, m):
with torch.no_grad():
if hasattr(m, 'weight'):
noise = self.noise_variance * torch.randn(m.weight.size())
variance_of_noise = torch.var(noise)
m.weight.add_(noise.to(self.dev))
self.variance_of_noises.append(float(variance_of_noise))
def worker_local_update(self, rewards, log_files_folder_path_comm_round, comm_round, local_epochs=1):
print(
f"Worker {self.idx} is doing local_update with computation power {self.computation_power} and link speed {round(self.link_speed, 3)} bytes/s")
self.net.load_state_dict(self.global_parameters, strict=True)
self.local_update_time = time.time()
is_malicious_node = "M" if self.return_is_malicious() else "B"
self.local_updates_rewards_per_transaction = 0
loss_list = []
acc_list = []
for epoch in range(local_epochs):
sum_accu = 0
num = 0
for i, (data, label) in enumerate(self.train_dl):
data, label = data.to(self.dev), label.to(self.dev)
preds = self.net(data)
loss = self.loss_func(preds, label)
preds = torch.argmax(preds, dim=1)
sum_accu += (preds == label).float().mean()
num += 1
if i % 20 == 0:
acc = sum_accu / num
acc_list.append(acc)
loss_list.append(loss)
tqdm.write(
f'world {comm_round}, epoch {epoch}, {i}/{len(self.train_dl)}: loss={loss:.4f}, acc={acc:.4f}')
loss.backward()
self.opti.step()
self.opti.zero_grad()
self.local_updates_rewards_per_transaction += rewards * (label.shape[0])
accrcy, lss = self.validate_model_weights(self.net.state_dict())
with open(
f"{log_files_folder_path_comm_round}/worker_{self.idx}_{is_malicious_node}_local_updating_accuracies_comm_{comm_round}.txt",
"a") as file:
file.write(
f"{self.return_idx()} epoch_{epoch + 1} {self.return_role()} {is_malicious_node}: {accrcy}\n")
self.local_total_epoch += 1
try:
self.local_update_time = (time.time() - self.local_update_time) / self.computation_power
except:
self.local_update_time = float('inf')
if self.is_malicious:
self.net.apply(self.malicious_worker_add_noise_to_weights)
print(f"malicious worker {self.idx} has added noise to its local updated weights before transmitting")
with open(f"{log_files_folder_path_comm_round}/comm_{comm_round}_variance_of_noises.txt", "a") as file:
file.write(
f"{self.return_idx()} {self.return_role()} {is_malicious_node} noise variances: {self.variance_of_noises}\n")
accrcy1, ls1 = self.validate_model_weights(self.net.state_dict())
with open(f"{log_files_folder_path_comm_round}/worker_final_local_accuracies_comm_{comm_round}.txt",
"a") as file:
file.write(
f"{self.return_idx()} {self.return_role()} {is_malicious_node}: {accrcy1}\n")
print(f"Done {local_epochs} epoch(s) and total {self.local_total_epoch} epochs")
self.local_train_parameters = self.net.state_dict()
return self.local_update_time, acc_list, loss_list
def waste_one_epoch_local_update_time(self, opti):
if self.computation_power == 0:
return float('inf'), None
else:
validation_net = copy.deepcopy(self.net)
currently_used_lr = 0.01
for param_group in self.opti.param_groups:
currently_used_lr = param_group['lr']
if opti == 'SGD':
validation_opti = optim.SGD(validation_net.parameters(), lr=currently_used_lr)
local_update_time = time.time()
for data, label in self.train_dl:
data, label = data.to(self.dev), label.to(self.dev)
preds = validation_net(data)
loss = self.loss_func(preds, label)
loss.backward()
validation_opti.step()
validation_opti.zero_grad()
return (time.time() - local_update_time) / self.computation_power, validation_net.state_dict()
def set_accuracy_this_round(self, accuracy):
self.accuracy_this_round = accuracy
def return_accuracy_this_round(self):
return self.accuracy_this_round
def return_link_speed(self):
return self.link_speed
def return_local_updates_and_signature(self, comm_round):
local_updates_dict = {'worker_device_idx': self.idx, 'in_round_number': comm_round,
"local_updates_params": copy.deepcopy(self.local_train_parameters),
"local_updates_rewards": self.local_updates_rewards_per_transaction,
"local_iteration(s)_spent_time": self.local_update_time,
"local_total_accumulated_epochs_this_round": self.local_total_epoch,
"worker_rsa_pub_key": self.return_rsa_pub_key()}
local_updates_dict["worker_signature"] = self.sign_msg(sorted(local_updates_dict.items()))
return local_updates_dict
def worker_reset_vars_for_new_round(self):
self.received_block_from_miner = None
self.accuracy_this_round = float('-inf')
self.local_updates_rewards_per_transaction = 0
self.has_added_block = False
self.the_added_block = None
self.worker_associated_validator = None
self.worker_associated_miner = None
self.local_update_time = None
self.local_total_epoch = 0
self.variance_of_noises.clear()
self.round_end_time = 0
def receive_block_from_miner(self, received_block, source_miner):
if not (received_block.return_mined_by() in self.black_list or source_miner in self.black_list):
self.received_block_from_miner = copy.deepcopy(received_block)
print(
f"{self.role} {self.idx} has received a new block from {source_miner} mined by {received_block.return_mined_by()}.")
else:
print(
f"Either the block sending miner {source_miner} or the miner {received_block.return_mined_by()} mined this block is in worker {self.idx}'s black list. Block is not accepted.")
def toss_received_block(self):
self.received_block_from_miner = None
def return_received_block_from_miner(self):
return self.received_block_from_miner
def validate_model_weights(self, weights_to_eval=None):
with torch.no_grad():
if weights_to_eval:
self.net.load_state_dict(weights_to_eval, strict=True)
else:
self.net.load_state_dict(self.global_parameters, strict=True)
sum_accu = 0
num = 0
for data, label in self.test_dl:
data, label = data.to(self.dev), label.to(self.dev)
preds = self.net(data)
loss = self.loss_func(preds, label)
preds = torch.argmax(preds, dim=1)
sum_accu += (preds == label).float().mean()
num += 1
return sum_accu / num, loss
def global_update(self, local_update_params_potentially_to_be_used):
local_params_by_benign_workers = []
for (worker_device_idx, local_params) in local_update_params_potentially_to_be_used:
if not worker_device_idx in self.black_list:
local_params_by_benign_workers.append(local_params)
else:
print(f"global update skipped for a worker {worker_device_idx} in {self.idx}'s black list")
if local_params_by_benign_workers:
sum_parameters = None
for local_updates_params in local_params_by_benign_workers:
if sum_parameters is None:
sum_parameters = copy.deepcopy(local_updates_params)
else:
for var in sum_parameters:
sum_parameters[var] += local_updates_params[var]
num_participants = len(local_params_by_benign_workers)
for var in self.global_parameters:
self.global_parameters[var] = (sum_parameters[var] / num_participants)
print(f"global updates done by {self.idx}")
else:
print(f"There are no available local params for {self.idx} to perform global updates in this comm round.")
''' miner '''
def request_to_download(self, block_to_download, requesting_time_point):
print(f"miner {self.idx} is requesting its associated devices to download the block it just added to its chain")
devices_in_association = self.miner_associated_validator_set.union(self.miner_associated_worker_set)
for device in devices_in_association:
if self.online_switcher() and device.online_switcher():
miner_link_speed = self.return_link_speed()
device_link_speed = device.return_link_speed()
lower_link_speed = device_link_speed if device_link_speed < miner_link_speed else miner_link_speed
transmission_delay = getsizeof(str(block_to_download.__dict__)) / lower_link_speed
verified_block, verification_time = device.verify_block(block_to_download,
block_to_download.return_mined_by())
if verified_block:
device.add_block(verified_block)
device.add_to_round_end_time(requesting_time_point + transmission_delay + verification_time)
else:
print(
f"Unfortunately, either miner {self.idx} or {device.return_idx()} goes offline while processing this request-to-download block.")
def propagated_the_block(self, propagating_time_point, block_to_propagate):
for peer in self.peer_list:
if peer.is_online():
if peer.return_role() == "miner":
if not peer.return_idx() in self.black_list:
print(
f"{self.role} {self.idx} is propagating its mined block to {peer.return_role()} {peer.return_idx()}.")
if peer.online_switcher():
peer.accept_the_propagated_block(self, self.block_generation_time_point, block_to_propagate)
else:
print(
f"Destination miner {peer.return_idx()} is in {self.role} {self.idx}'s black_list. Propagating skipped for this dest miner.")
def accept_the_propagated_block(self, source_miner, source_miner_propagating_time_point, propagated_block):
if not source_miner.return_idx() in self.black_list:
source_miner_link_speed = source_miner.return_link_speed()
this_miner_link_speed = self.link_speed
lower_link_speed = this_miner_link_speed if this_miner_link_speed < source_miner_link_speed else source_miner_link_speed
transmission_delay = getsizeof(str(propagated_block.__dict__)) / lower_link_speed
self.unordered_propagated_block_processing_queue[
source_miner_propagating_time_point + transmission_delay] = propagated_block
print(
f"{self.role} {self.idx} has accepted accepted a propagated block from miner {source_miner.return_idx()}")
else:
print(
f"Source miner {source_miner.return_role()} {source_miner.return_idx()} is in {self.role} {self.idx}'s black list. Propagated block not accepted.")
def add_propagated_block_to_processing_queue(self, arrival_time, propagated_block):
self.unordered_propagated_block_processing_queue[arrival_time] = propagated_block
def return_unordered_propagated_block_processing_queue(self):
return self.unordered_propagated_block_processing_queue
def return_associated_validators(self):
return self.miner_associated_validator_set
def return_miner_acception_wait_time(self):
return self.miner_acception_wait_time
def return_miner_accepted_transactions_size_limit(self):
return self.miner_accepted_transactions_size_limit
def return_miners_eligible_to_continue(self):
miners_set = set()
for peer in self.peer_list:
if peer.return_role() == 'miner':
miners_set.add(peer)
miners_set.add(self)
return miners_set
def return_accepted_broadcasted_transactions(self):
return self.broadcasted_transactions
def verify_validator_transaction(self, transaction_to_verify):
if self.computation_power == 0:
print(f"miner {self.idx} has computation power 0 and will not be able to verify this transaction in time")
return False, None
else:
transaction_validator_idx = transaction_to_verify['validation_done_by']
if transaction_validator_idx in self.black_list:
print(f"{transaction_validator_idx} is in miner's blacklist. Trasaction won't get verified.")
return False, None
verification_time = time.time()
if self.check_signature:
transaction_before_signed = copy.deepcopy(transaction_to_verify)
del transaction_before_signed["validator_signature"]
modulus = transaction_to_verify['validator_rsa_pub_key']["modulus"]
pub_key = transaction_to_verify['validator_rsa_pub_key']["pub_key"]
signature = transaction_to_verify["validator_signature"]
hash = int.from_bytes(sha256(str(sorted(transaction_before_signed.items())).encode('utf-8')).digest(),
byteorder='big')
hashFromSignature = pow(signature, pub_key, modulus)
if hash == hashFromSignature:
print(
f"Signature of transaction from validator {transaction_validator_idx} is verified by {self.role} {self.idx}!")
verification_time = (time.time() - verification_time) / self.computation_power
return verification_time, True
else:
print(f"Signature invalid. Transaction from validator {transaction_validator_idx} is NOT verified.")
return (time.time() - verification_time) / self.computation_power, False
else:
print(
f"Signature of transaction from validator {transaction_validator_idx} is verified by {self.role} {self.idx}!")
verification_time = (time.time() - verification_time) / self.computation_power
return verification_time, True
def sign_candidate_transaction(self, candidate_transaction):
signing_time = time.time()
candidate_transaction['miner_rsa_pub_key'] = self.return_rsa_pub_key()
if 'miner_signature' in candidate_transaction.keys():
del candidate_transaction['miner_signature']
candidate_transaction["miner_signature"] = self.sign_msg(sorted(candidate_transaction.items()))
signing_time = (time.time() - signing_time) / self.computation_power
return signing_time
def mine_block(self, candidate_block, rewards, starting_nonce=0):
candidate_block.set_mined_by(self.idx)
pow_mined_block = self.proof_of_work(candidate_block)
pow_mined_block.set_mining_rewards(rewards)
return pow_mined_block
def proof_of_work(self, candidate_block, starting_nonce=0):
candidate_block.set_mined_by(self.idx)
''' Brute Force the nonce '''
candidate_block.set_nonce(starting_nonce)
current_hash = candidate_block.compute_hash()
while not current_hash.startswith('0' * self.pow_difficulty):
candidate_block.nonce_increment()
current_hash = candidate_block.compute_hash()
candidate_block.set_pow_proof(current_hash)
return candidate_block
def set_block_generation_time_point(self, block_generation_time_point):
self.block_generation_time_point = block_generation_time_point
def return_block_generation_time_point(self):
return self.block_generation_time_point
def receive_propagated_block(self, received_propagated_block):
if not received_propagated_block.return_mined_by() in self.black_list:
self.received_propagated_block = copy.deepcopy(received_propagated_block)
print(
f"Miner {self.idx} has received a propagated block from {received_propagated_block.return_mined_by()}.")
else:
print(
f"Propagated block miner {received_propagated_block.return_mined_by()} is in miner {self.idx}'s blacklist. Block not accepted.")
def receive_propagated_validator_block(self, received_propagated_validator_block):
if not received_propagated_validator_block.return_mined_by() in self.black_list:
self.received_propagated_validator_block = copy.deepcopy(received_propagated_validator_block)
print(
f"Miner {self.idx} has received a propagated validator block from {received_propagated_validator_block.return_mined_by()}.")
else:
print(
f"Propagated validator block miner {received_propagated_validator_block.return_mined_by()} is in miner {self.idx}'s blacklist. Block not accepted.")
def return_propagated_block(self):
return self.received_propagated_block
def return_propagated_validator_block(self):
return self.received_propagated_validator_block