def summed_votes(): verified, data = util.unpack_request(request, str(server_nr)) if not verified: return make_response("Could not verify", 400) try: vote_ = data['vote'] if type(vote_) == str: vote_ = [vote_] id_ = data['id'] if type(id_) in [str, int]: id_ = [id_] client = data['client'] server_name = data['server'] for i in range(len(vote_)): vote = util.string_to_vote(vote_[i]) assert type(vote) == np.ndarray db.insert_summed_votes(vote, int(id_[i]), client, server_name, my_name) except TypeError as e: print("summed_votes: ", vote_) print("summed_votes: ", e) return Response(status=400) return Response(status=200)
def differenceshareforzeroone(): verified, data = util.unpack_request(request, str(server_nr)) if not verified: return make_response("Could not verify", 400) try: datas_ = data['datas'] for data_ in datas_: diff_ = data_['diff'] diff_ = util.string_to_vote(diff_) x_ = data_['x'] i_ = data_['i'] j_ = data_['j'] server_a_ = data_['server_a'] server_b_ = data_['server_b'] client_ = data_['client'] server_ = data_['server'] # Save each difference in database db.insert_zero_consistency_check(diff=diff_, x=x_, i=i_, j=j_, server_a=server_a_, server_b=server_b_, client_name=client_, server=server_, db_name=my_name) except TypeError as e: print("differenceshareforzeroone: ", e) return Response(status=400) return Response(status=200)
def extra_data(): verified, data = util.unpack_request(request, my_name) if not verified: return make_response("Could not verify", 400) complaint = util.string_to_vote(data["complaint"]) db.insert_mediator_inconsistency_extra_data(data["sender"], complaint, complaint.protocol, data["data"]) return make_response("ok", 200)
def illegal_vote(): verified, data = util.unpack_request(request, str(server_nr)) if not verified: return make_response("Could not verify", 400) bad_votes = data['clients'] server_name = data['server'] # print("illegal: ", "Got votes", bad_votes, "from server", server_name) db.insert_illegal_votes(bad_votes, server_name, my_name) return Response(status=200)
def message_inconsistency(): verified, data = util.unpack_request(request, my_name) if not verified: return make_response("Could not verify", 400) complaint: util.Complaint = util.string_to_vote(data["complaint"]) db.insert_mediator_inconsistency(complaint.sender, complaint, complaint.protocol) timer(5, complaint.protocol, complaint) return make_response("Done", 200)
def vote_validity(): verified, data = util.unpack_request(request, my_name) if not verified: return make_response("Could not verify", 400) illegal_votes_ = data['illegal_votes'] if type(illegal_votes_) == str: illegal_votes_ = [illegal_votes_] server_ = data['server'] db.insert_mediator_illegal_votes(illegal_votes_, server_) return make_response("valid", 200)
def messageinconsistency(): verified, data = util.unpack_request(request, str(server_nr)) if not verified: return make_response("Could not verify", 400) # check_votes = 1 # sum_difference_zero_one = 2 # sum_difference_zero_one_partition = 3 # zero_one_finalize = 4 # ensure_vote_agreement = 5 # compute_result = 6 complaint: util.Complaint = util.string_to_vote(data["complaint"]) relevant_data = [] if complaint.protocol == util.Protocol.check_votes: if complaint.data["votes"][0][2] == "row": my_data = db.get_rows(my_name) else: my_data = db.get_cols(my_name) relevant_data = [x for x in my_data if x[1] == complaint.value_id] elif complaint.protocol == util.Protocol.sum_difference_zero_one: relevant_data = db.get_summed_diffs(my_name) relevant_data = [ x for x in relevant_data if (x[2] == complaint.data["i"] and x[3] == complaint.data["j"] and x[1] == complaint.data["client"]) ] elif complaint.protocol == util.Protocol.sum_difference_zero_one_partition: my_data = db.get_zero_consistency_check_alt(my_name) relevant_data = [ x for x in my_data if (x[2] == complaint.data["i"] and x[3] == complaint.data["j"] and x[1] == complaint.data["client"] and x[4] == complaint.data["x"]) ] elif complaint.protocol == util.Protocol.zero_one_finalize: partition_sums = db.get_zero_partition_sum(my_name) relevant_data = partition_sums[complaint.data["client"]] elif complaint.protocol == util.Protocol.ensure_vote_agreement: relevant_data = db.get_illegal_votes(my_name) elif complaint.protocol == util.Protocol.compute_result: all_votes = db.round_two(my_name) relevant_data = [x for x in all_votes if x[3] != malicious_server] server_util.send_extra_data_to_mediator(relevant_data, complaint, my_name) # Modtager complaints fra andre servere # TODO: send relevant data to mediator return make_response("delivered", 200)
def receive_broadcasted_value(): verified, data = util.unpack_request(request, str(server_nr)) if not verified: return make_response("Could not verify", 400) vote_ = data['vote'] vote = util.string_to_vote(vote_) assert type(vote) == np.ndarray id_ = data['id'] type_ = data['round'] client = data['client'] server_name = data['server'] if type_ == 'row': db.insert_row(vote, id_, type_, client, server_name, my_name) elif type_ == 'column': db.insert_col(vote, id_, type_, client, server_name, my_name) return Response(status=200)
def save_result(): verified, data = util.unpack_request(request, str(server_nr)) if not verified: return make_response("Could not verify", 400) try: result_ = data['result'] server_name = data['server'] result = util.string_to_vote(result_) assert type(result) == np.ndarray db.insert_result(result, server_name, my_name) except TypeError as e: print("save_result: ", result_) print("save_result: ", e) return Response(status=400) return Response(status=200)
def zerocheck(): verified, data = util.unpack_request(request, str(server_nr)) if not verified: return make_response("Could not verify", 400) try: vote_ = data['vote'] vote = util.string_to_vote(vote_) assert type(vote) == np.ndarray client = data['client'] server_name = data['server'] db.insert_zero_check(vote, client, server_name, my_name) except TypeError as e: print("zerocheck: ", vote_) print("zerocheck: ", e) return Response(status=400) return Response(status=200)
def sum_product_receive(): verified, data = util.unpack_request(request, str(server_nr)) if not verified: return make_response("Could not verify", 400) try: sum_matrix_ = data['sum_matrix'] sum_matrix_ = util.string_to_vote(sum_matrix_) client_ = data['client'] server_ = data['server'] # Save on database db.insert_zero_partition_sum(matrix=sum_matrix_, client=client_, server=server_, db_name=my_name) except TypeError as e: print("zeroone_sum_partition: ", "ERROR") return Response(status=200)
def mediator_answer_votes(): global malicious_server, found_malicious_server verified, data = util.unpack_request(request, str(server_nr)) if not verified: return make_response("Could not verify", 400) # TODO: Check at mediator ikke er adversary malicious_server_ = data['malicious_server'] if (malicious_server_ == ""): votes_for_deletion_ = data['votes_for_deletion'] for client in votes_for_deletion_: db.remove_vote(client, my_name) else: found_malicious_server = True malicious_server = malicious_server_ if (my_name != malicious_server_): for sender, votes in db.get_illegal_votes(my_name): if (sender == my_name): for client in votes: db.remove_vote(client, my_name) return Response(status=200)
def zeroonepartions(): verified, data = util.unpack_request(request, str(server_nr)) if not verified: return make_response("Could not verify", 400) try: partitions_ = data['ss'] partitions_ = [(x, util.string_to_vote(y)) for (x, y) in partitions_] i_ = data['i'] j_ = data['j'] server_ = data['server'] id_ = data['id'] client_ = data['client'] for x, ss in partitions_: db.insert_zero_partition(ss, x, i_, j_, client_, server_, my_name) except TypeError as e: print("zeroonepartitions: ", data) print("zeroonepartitions: ", e) return Response(status=400) return Response(status=200)
def vote(): global communication_number verified, data = util.unpack_request(request, str(server_nr)) if not verified: return make_response("Could not verify", 400) try: # Unpack data votes_ = data['votes'] if type(votes_) == str: votes_ = [votes_] ids_ = data['ids'] if type(ids_) in [str, int]: ids_ = [ids_] server_name = data['server'] client = data['client'] # Convert votes_ to list of np.array votes = [] for num, v_ in enumerate(votes_): if num in cheating_nums and cheat_id == 1: votes.append(cheat_util.col_row_cheat(util.string_to_vote(v_))) else: votes.append(util.string_to_vote(v_)) # Attach each share to its proper id id_vote_tuple = list(zip(ids_, votes)) # Insert votes, row and column in db and broadcast for (id, vote) in id_vote_tuple: row_sum = server_util.create_sum_of_row(vote) col_sum = server_util.create_sum_of_row(vote.T) db.insert_vote(vote, id, 1, client, server_name, my_name) db.insert_row(row_sum, id, 'row', client, server_name, my_name) db.insert_col(col_sum, id, 'column', client, server_name, my_name) server_util.broadcast_rows_and_cols(row_sum, col_sum, id, servers, my_name, client) # Insert zero check and broadcast my_id = servers.index(my_name) votes_dict = dict() for id, v in id_vote_tuple: if (len(cheating_nums) > 0 and id == cheating_nums[0] and cheat_id == util.Protocol.sum_difference_zero_one.value): v = np.zeros(v.shape) votes_dict[id] = v local_parts, communcations = server_util.matrix_zero_one_check( my_id, servers, votes_dict, my_name, client) communication_number += communcations for local_part in local_parts: for x, ss in enumerate(local_part[3]): db.insert_zero_partition(matrix=ss, x=x, i=local_part[1], j=local_part[2], client_name=local_part[5], server=my_name, db_name=my_name) except TypeError as e: print("vote", e) return Response(status=400) return Response(status=200)