-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoRNode.py
More file actions
353 lines (289 loc) · 11.6 KB
/
PoRNode.py
File metadata and controls
353 lines (289 loc) · 11.6 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
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
from quart import Quart, request
from enum import Enum
import asyncio
import httpx
import uuid
import json
import sys
from reputationModel.mock.MockReputation import MockReputationModel
from reputationModel.mock.MockIOC import MockIOCModel
from primitives.block import Block
from primitives.blockchain import Blockchain
class NodeState(Enum):
PREPATING_FOR_ELECTING = 0
ELECTING_LEADER = 1
LEADER_ELECTED = 2
LEADER_CREATING_BLOCK = 3
BLOCK_ADDED_TO_BLOCKCHAIN = 4
app = Quart(__name__)
node_uuid = uuid.uuid4()
pending_nodes = set()
nodes = set()
transactions = set()
iocModel = MockIOCModel()
isAttachedToNodes = False;
reputationModel = MockReputationModel()
nodeState = NodeState.PREPATING_FOR_ELECTING
blockchain = Blockchain()
async def _background_task(timeout):
async with httpx.AsyncClient() as client:
response = await client.get('http://0.0.0.0:5000/test')
print("Response from test route", response.text)
await asyncio.sleep(timeout)
print("I am completed")
@app.route("/", methods=["POST", "GET"])
async def main_route():
print("Hello from main route")
app.add_background_task(_background_task, 10)
return "Hello from root"
@app.route("/test", methods=["POST", "GET"])
async def test_route():
print("Hello from test route")
#await asyncio.sleep(0.5)
return "Hello from test"
###################################################################
############### VIEW NODES FUNCTIONALITY ###############
@app.route("/healthcheck", methods=["GET", "POST"])
async def healthcheck():
return {'status':'live'}
@app.route("/node_uuid", methods=["GET", "POST"])
async def get_node_uuid():
return node_uuid
@app.route("/node_state", methods=["GET", "POST"])
async def get_node_state():
return nodeState
@app.route("/nodes", methods=["GET", "POST"])
async def getNodes():
return str(nodes)
@app.route("/address", methods=["GET", "POST"])
async def getAddress():
local_node_socket = request.server #get socket of local node
local_node_host = local_node_socket[0] #retrieve the host from local_node_socket
local_node_port = local_node_socket[1] #retrieve the port from local_node_socket
return json.dumps(f'http://{local_node_host}:{local_node_port}')
@app.route("/getAddressAsString", methods=["GET", "POST"])
async def getAddressAsString():
local_node_socket = request.server #get socket of local node
local_node_host = local_node_socket[0] #retrieve the host from local_node_socket
local_node_port = local_node_socket[1] #retrieve the port from local_node_socket
return str(f'http://{local_node_host}:{local_node_port}')
###################################################################
############### CONNECT NODES FUNCTIONALITY ###############
@app.route("/attach", methods=["POST"])
async def attach():
'''
Attach this node to remote blockchain node
Request example: http://<node_host>:<node_port>/attach?remote_node_url='<remote_address>'
Example: http://127.0.0.1:5000/attach?remote_node_url='http://127.0.0.1:5001'
Steo 0. Verify that this node is not attacher
Step 1. Check if remote node is live
Step 2. If remote node is live, than add to set nodes
Step 3. Call on remote node method broadcastNewPeer
Step 4. The responce from remote node will be the nodes in blockchain network. Add this remote nodes
Step 5.
'''
# Step 0
global isAttachedToNodes
if isAttachedToNodes: # check that node attached to nodes
return {'error':'attached'}
# Step 1
remote_node_url = request.args.get('remote_node_url') #getting the remote_node_url from request
#print(remote_node_url)
try:
async with httpx.AsyncClient() as client:
healthcheck_responce = await client.get(f'{remote_node_url}/healthcheck') #send request to remote node to healthcheck
except httpx.ConnectError:
return {'error':'remote server is not live'}
# Step 2
nodes.add(remote_node_url) #add remote node to set of nodes
# Step 3
local_node_socket = request.server #get socket of local node
local_node_host = local_node_socket[0] #retrieve the host from local_node_socket
local_node_port = local_node_socket[1] #retrieve the port from local_node_socket
broadcastNewPeer_url = f'{remote_node_url}/broadcastNewPeer?peer=http://{local_node_host}:{local_node_port}' #form the request to remote node
print(broadcastNewPeer_url)
addPeerResponse = None
try:
async with httpx.AsyncClient() as client:
addPeerResponse = await client.post(broadcastNewPeer_url) #send request to remote node
print("Response from test route", addPeerResponse.text)
except:
return {'error':'failed to attach to network'}
print(f'AddPeerResponce: {addPeerResponse.text}')
# Step 4.
responce_nodes = json.loads(addPeerResponse.text) #list of response nodes
# print(type(responce_nodes))
# print(responce_nodes)
# print(len(responce_nodes))
for responce_node in responce_nodes:
print(f'Responce_node: {responce_node}')
if responce_node in nodes:
continue
else:
nodes.add(responce_node)
print(f'Nodes: {nodes}')
isAttachedToNodes = True
return str(node_uuid)
@app.route("/broadcastNewPeer", methods=["POST"])
async def broadcastNewPeer():
'''
Broadcasting peer to blockchain network.
Request example: http://<node_host>:<node_port>/broadcastNewPeer?peer=http://<peer_node_host>:<peer_node_port>
Step 1. Check if remote node is live
Step 2. Add peer to nodes
Step 3. Call on nodes method addPeer()
'''
# http://local_host:local_port/addPeer?peer='http://remote_host:remote_port'
peer = request.args.get('peer')
print(f'Peer: {peer}')
try:
async with httpx.AsyncClient() as client:
healthcheck_responce = await client.get(f'{peer}/healthcheck')
except httpx.ConnectError:
return {'error':'remote server is not live'}
try:
for node in nodes:
print(f'Node: {node}')
async with httpx.AsyncClient() as client:
response = await client.post(f'{node}/addPeer?peer={peer}')
print("Response from test route", response.text)
except:
return {'error':'failed to attach to network'}
response_to_requester = json.dumps(list(nodes))
nodes.add(peer)
print(response_to_requester)
return response_to_requester
@app.route("/addPeer", methods=["POST"])
async def addPeer():
'''
Adding peer to blockchain network.
Step 1. Check if remote node is live
Step 2. Add peer to nodes
'''
# http://local_host:local_port/addPeer?peer='http://remote_host:remote_port'
peer = request.args.get('peer')
print(f'Peer: {peer}')
try:
async with httpx.AsyncClient() as client:
healthcheck_responce = await client.get(f'{peer}/healthcheck')
except httpx.ConnectError:
return {'error':'remote server is not live'}
response_to_requester = {"status":"added"}
nodes.add(peer)
print(response_to_requester)
return response_to_requester
###################################################################
############### ELECTING LEADER FUNCTIONALITY ###############
@app.route("/broadcastElectLeader", methods=["POST"])
async def broadcastElectLeader():
for node in nodes:
try:
print(f'Node: {node}')
async with httpx.AsyncClient() as client:
response = await client.post(f'{node}/electLeader')
print("Response from test route", response.text)
except:
return {'error':'failed to attach to network'}
@app.route("/electLeader", methods=["POST"])
async def electLeader():
if nodeState > NodeState.ELECTING_LEADER:
return {'status' : 'leader elected'}
node_ioc = await getIoC()
maxReputation = reputationModel.reputation(node_ioc)
leader = await getAddressAsString()
for node in nodes:
try:
print(f'Elect Node: {node}')
async with httpx.AsyncClient() as client:
node_ioc_responce = await client.get(f'{node}/getIoC')
print("Response from test route", node_ioc_responce.text)
print(f'Type ioc response: {type(node_ioc_responce.text)}')
node_reputation = reputationModel.reputation(node_ioc_responce.text)
if node_reputation > maxReputation:
leader = node
maxReputation = node_reputation
except:
return {'error':'failed to attach to network'}
return json.dumps(leader)
@app.route("/getIoC", methods=["GET"])
async def getIoC():
local_node_socket = request.server #get socket of local node
local_node_port = local_node_socket[1] #retrieve the port from local_node_socket
print(type(local_node_port))
ioc = iocModel.getIoC(local_node_port)
return json.dumps(ioc)
###################################################################
############### MINING BLOCK FUNCTIONALITY ###############
@app.route("/mineBlock", methods=["POST"])
async def mineBlock():
blockNumber = blockchain.getBlockchainLength()
leader = str(node_uuid)
block = Block(blockNumber=blockNumber, leader=leader)
# add transactions to block
blockchain.addBlock(block)
data = block.__repr__()
for node in nodes:
try:
print(f'Node: {node}')
async with httpx.AsyncClient() as client:
addBlock_responce = await client.post(f'{node}/addBlock',data=data)
except:
return {'error':'failed to attach to network'}
return {'asd':'sda'}
@app.route("/addBlock", methods=["POST"])
async def addBlock():
'''
Request example: http://<node_host>:<node_port>/addBlock
with body that contains JSON format of block
'''
data = await request.get_data()
data_decoded = data.decode() # Type: <class 'str'>
block = Block()
block.loadBlockFromJSON(data_decoded)
blockchain.addBlock(block=block)
return {'sda':"123"}
@app.route("/getBlock", methods=["GET"])
async def getBlock():
'''
Request example: http://<node_host>:<node_port>/getBlock?blockNumber=<blockNumber>
'''
blockNumber = request.args.get('blockNumber')
block = blockchain.getBlock(blockNumber)
return block.toJSON()
@app.route("/getLastBlock", methods=["GET"])
async def getLastBlock():
'''
Request example: http://<node_host>:<node_port>/getLastBlock
'''
block = blockchain.getLastBlock()
return block.toJSON()
@app.route("/getBlockchainLength", methods=["GET"])
async def getBlockchainLength():
'''
Request example: http://<node_host>:<node_port>/getBlockchainLength
'''
blockchainLength = blockchain.getBlockchainLength()
return {'blockchainLength' : blockchainLength}
###################################################################
############### TRANSACTION FUNCTIONALITY ###############
@app.route("/transact", methods=["POST"])
async def transact():
'''
Request example: http://<node_host>:<node_port>/transact
with body that contains transaction
'''
pass
@app.route("/appendTransaction", methods=["POST"])
async def appendTransaction():
pass
###################################################################
if __name__ == '__main__':
from hypercorn.config import Config
from hypercorn.asyncio import serve
argv = sys.argv
port = 5000
if '--port' in argv:
port = argv[argv.index('--port') + 1] # receive the port from console
print(port)
Config.bind = [f'0.0.0.0:{port}']
asyncio.run(serve(app, Config()))