-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
107 lines (82 loc) · 3.01 KB
/
client.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
import json
import requests
import sys
import time
import numpy as np
from client_method import Client
from pprint import pprint
SPLIT_SIZE = 10
CONNECTED_NODE_ADDRESS = "http://127.0.0.1:8001"
def weights_update(all_weights):
models = []
for model in all_weights:
z = []
for j in model:
z.append(np.array(j))
models.append(z)
all_weights = models
m = all_weights[0]
for num in range(1, len(all_weights)):
a = all_weights[num]
m = np.add(m, a)
m /= len(all_weights)
return m
time1 = time.time()
# Initialize the client
client = Client()
model = (client.model_build()).get_weights()
models = np.array([0, 0])
f = open("output.txt", "a")
time2 = time.time()
for i in range(10):
# Get the recent most block from the chain, which contains the avg weights
response = requests.get("{}/recent_block".format(CONNECTED_NODE_ADDRESS))
# The first iteration won't have any avg weights from server
if i != 0:
if response.status_code == 200:
model = []
content = json.loads(response.content)
modeld = json.loads(content["block"][0]["content"])
for j in modeld:
model.append(np.array(j))
# print(model, file=f)
# print("\n\n WEIGHTS", weights_update(models), file=f)
resp = requests.get("{}/last_self_added_block".format(CONNECTED_NODE_ADDRESS))
cont = json.loads(resp.content)
submitted_modeld = json.loads(cont["block"][0]["content"])
submitted_model = [np.array(j) for j in submitted_modeld]
avg_model = weights_update(submitted_model)
is_update_valid = True
for k in range(len(model)):
if not np.array_equal(model[k], avg_model[k]):
is_update_valid = False
print(model[k].shape, avg_model[k].shape)
# sys.exit(0)
if is_update_valid:
print("Server OK. Proceeding")
else:
print("Server's update doesn't match. Careful!")
else:
print("Invalid response from the server:", response.content, "\nSkipping this iteration")
continue
models = client.Federated_model(i, model)
# Convert the model into list
model_list = []
for model in models:
z = []
for j in model:
z.append(j.tolist())
model_list.append(z)
post_object = {
"author": "client",
"content": json.dumps(model_list),
}
# Submit a transaction
new_tx_address = "{}/new_transaction".format(CONNECTED_NODE_ADDRESS)
requests.post(
new_tx_address, json=post_object, headers={"Content-type": "application/json"},
)
# Then immediately mine
requests.get("{}/mine".format(CONNECTED_NODE_ADDRESS))
current_time = time.time()
print(f"Total time: {current_time-time1}, {current_time-time2}")