-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient.py
More file actions
274 lines (216 loc) · 10.4 KB
/
Copy pathclient.py
File metadata and controls
274 lines (216 loc) · 10.4 KB
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
import logging
import shutil
import time
import asyncio
import numpy as np
import tensorflow as tf
from models.perceptron import Perceptron
from models.cnn import CNN
from models.lstm import LSTM
from eth_utils import is_address
from solc import compile_source, compile_files
from blockchain.blockchain_utils import *
from blockchain.ipfs_utils import *
from models.gan import ConversationalNetwork
class Client(object):
def __init__(self, iden, provider, clientAddress=None, delegatorAddress=None):
self.web3 = provider
self.api = ipfsapi.connect('127.0.0.1', 5001)
self.PASSPHRASE = 'panda'
self.TEST_ACCOUNT = '0xb4734dCc08241B46C0D7d22D163d065e8581503e'
self.TEST_KEY = '146396092a127e4cf6ff3872be35d49228c7dc297cf34da5a0808f29cf307da1'
contract_source_path_A = "blockchain/Delegator.sol"
contract_source_path_B = "blockchain/Query.sol"
contract_source_path_C = "blockchain/DAgoraToken.sol"
self.compiled_sol = compile_files([contract_source_path_A, contract_source_path_B,
contract_source_path_C])
self.iden = iden
if clientAddress:
assert(is_address(clientAddress))
self.clientAddress = clientAddress
else:
#TODO: Initialize client 'container' address if it wasn't assigned one
self.clientAddress = self.web3.personal.newAccount(self.PASSPHRASE)
assert(is_address(self.clientAddress))
self.web3.personal.unlockAccount(self.clientAddress, self.PASSPHRASE)
print("Client Address:", self.clientAddress)
self.Delegator_address = delegatorAddress
self.DAgoraToken_address = self.web3.toChecksumAddress('0x1698215a2bea4935ba9e0f5b48347e83450a6774')
def get_money(self):
# get_testnet_eth(self, self.clientAddress, self.web3)
print("Client balance:", self.web3.eth.getBalance(self.clientAddress))
Query_id, self.Query_interface = self.compiled_sol.popitem()
Delegator_id, self.Delegator_interface = self.compiled_sol.popitem()
self.compiled_sol.popitem()
self.compiled_sol.popitem()
DAgoraToken_id, self.DAgoraToken_interface = self.compiled_sol.popitem()
if self.Delegator_address:
assert(is_address(self.Delegator_address))
else:
# self.Query_address = deploy_Query(self.web3, self.Query_interface, self.TEST_ACCOUNT, addr_lst)
self.Delegator_address = deploy_Master(self.web3, self.Delegator_interface, self.clientAddress)
print("Delegator Address", self.Delegator_address)
def launch_query(self, target_address):
contract_obj = self.web3.eth.contract(
address=self.Delegator_address,
abi=self.Delegator_interface['abi'])
tx_hash = contract_obj.functions.query(target_address).transact(
{'from': self.clientAddress})
self.web3.eth.waitForTransactionReceipt(tx_hash)
tx_receipt = self.web3.eth.getTransactionReceipt(tx_hash)
# print(tx_receipt)
self.Query_address = self.web3.toChecksumAddress('0x' + tx_receipt['logs'][0]['data'].split('000000000000000000000000')[2])
# return tx_receipt
def ping_client(self):
contract_obj = self.web3.eth.contract(
address=self.Query_address,
abi=self.Query_interface['abi'])
tx_hash = contract_obj.functions.pingClients().transact({'from': self.clientAddress})
self.web3.eth.waitForTransactionReceipt(tx_hash)
tx_receipt = self.web3.eth.getTransactionReceipt(tx_hash)
return tx_receipt
def setup_model(self, model_type):
self.model_type = model_type
if model_type == "perceptron":
self.model = Perceptron()
self.weights_metadata = self.model.get_weights_shape()
elif model_type == "cnn":
#TODO: Support CNN
self.model = CNN()
elif model_type == "lstm":
#TODO: Support LSTM
self.model = LSTM()
elif model_type == "gan":
self.model = ConversationalNetwork()
self.model.build_model(is_retraining=True)
else:
raise ValueError("Model {0} not supported.".format(model_type))
def train(self, IPFSaddress, config):
#TODO: Make train() only need to take in the config argument ONCE
logging.info('Training just started.')
# Get weights from IPFS and load into model
self.setup_model(config['model_type'])
ipfs2keras(self.model, IPFSaddress) # fix me
# Train model
n_k = self.model.train_model(config)
# Save new weights to IPFS and return address
new_model_address = keras2ipfs(self.model)
return new_model_address, n_k
def handle_ClientSelected_event(self, event_data):
e_data = [x for x in event_data.split('00') if x]
IPFSaddress_receiving = bytearray.fromhex(e_data[3][1:]).decode()[1:]
# address = self.web3.toChecksumAddress('0x' + e_data[1])
# assert(self.clientAddress == address)
print("IPFS address:", IPFSaddress_receiving)
# IPFS cat from IPFS_receiving
contract_obj = self.web3.eth.contract(
address=self.Query_address,
abi=self.Query_interface['abi'])
#will be hardcoded
config = {
"num_clients": 1,
"model_type": 'gan',
"dataset_type": 'iid',
"fraction": 1.0,
"max_rounds": 1,
"batch_size": 10,
"epochs": 1,
"learning_rate": 1e-4,
"save_dir": './results/',
"goal_accuracy": 1.0,
"lr_decay": 0.99
}
# data = api.cat(IPFSaddress_receiving)
# IPFS add
updatedAddress, n_k = self.train(IPFSaddress_receiving, config)
tx_hash = contract_obj.functions.receiveResponse(updatedAddress, n_k).transact(
{'from': self.clientAddress})
self.web3.eth.waitForTransactionReceipt(tx_hash)
tx_receipt = self.web3.eth.getTransactionReceipt(tx_hash)
log = contract_obj.events.ResponseReceived().processReceipt(tx_receipt)
# return log[0]
def handle_QueryCreated_event(self, event_data):
print(event_data)
address = event_data.split("000000000000000000000000")[2]
assert(is_address(address))
self.Query_address = self.web3.toChecksumAddress(address)
return event_data.split("000000000000000000000000")
def handle_BeginAveraging_event(self, IPFSaddress):
# get info at address
contract_obj = self.web3.eth.contract(
address=self.Query_address,
abi=self.Query_interface['abi'])
averagedAddress, num_data = self.runningWeightedAverage(IPFSaddress)
tx_hash = contract_obj.functions.allDone().transact(
{'from': self.clientAddress})
return tx_hash
def runningWeightedAverage(self, new_model_address, n_k_1, n_k_2):
weights1, weights2 = self.model.get_weights(), self.preprocess(new_model_address)
scaled_weights1, scaled_weights2 = model1.scale_weights(weights1, n_k_1), model2.scale_weights(weights2, n_k_2)
summed_weights = model2.sum_weights(weights1, weights2)
new_weights = model2.set_weights(summed_weights)
return keras2ipfs(new_weights), n_k_1 + n_k_2
def preprocess(self, new_model_address):
newModel = ConversationalNetwork()
newModel.build_model(is_retraining=False)
ipfs2keras(newModel, new_model_address)
return newModel.get_weights()
async def start_listening(self, event_to_listen, poll_interval=5):
while True:
lst = event_to_listen.get_new_entries()
if lst:
# print(lst[0])
return lst[0]
await asyncio.sleep(poll_interval)
def filter_set(self, event_sig, contract_addr, handler):
event_signature_hash = self.web3.sha3(text=event_sig).hex()
event_filter = self.web3.eth.filter({
"address": contract_addr.lower(),
"topics": [event_signature_hash]
})
asyncio.set_event_loop(asyncio.new_event_loop())
loop = asyncio.get_event_loop()
try:
inter = loop.run_until_complete(
self.start_listening(event_filter, 2))
check = handler(inter['data'])
finally:
loop.close()
return check
def main(self):
check = self.filter_set("QueryCreated(address,address)", self.Delegator_address, self.handle_QueryCreated_event)
if check[0] + check[1] == self.clientAddress.lower():
target_contract = check[0] + check[2]
# print(target_contract)
# retval = self.filter_set("ClientSelected(address,string)", target_contract, self.handle_ClientSelected_event)
self.filter_set("ClientSelected(address,string)", target_contract, self.handle_ClientSelected_event)
# return "I got chosen:", retval[0] + retval[1]
# print("listening for next round to begin...")
print("receiving reward...")
contract_obj = self.web3.eth.contract(
address=self.DAgoraToken_address,
abi=self.DAgoraToken_interface['abi'])
tx_hash = contract_obj.functions.transfer(self.clientAddress, 50000000).transact({'from': '0xf6419f5c5295a70C702aC21aF0f64Be07B59F3c4'})
self.web3.eth.waitForTransactionReceipt(tx_hash)
print('sent!')
# print('Token Balance:', contract_obj.functions.balanceOf(self.clientAddress))
# alldone = self.filter_set("BeginAveraging(string)", target_contract, self.handle_BeginAveraging_event)
else:
return "not me"
def flatten_weights(self, weights, factor=1e10):
flattened = []
for _, tensor in sorted(weights.items()):
flattened.extend(tensor.flatten().tolist())
return [int(n*factor) for n in flattened]
def unflatten_weights(self, flattened, factor=1e10):
flattened = [n/factor for n in flattened]
weights = {}
index = 0
for name, (shape, size) in sorted(self.weights_metadata.items()):
weights[name] = np.array(flattened[index:index+size]).reshape(shape)
index += size
return weights
def get_checkpoints_folder(self):
return "./checkpoints-{0}/{1}/".format(self.iden, self.model_type)
def get_latest_checkpoint(self):
return tf.train.latest_checkpoint(self.get_checkpoints_folder())