-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcast.py
48 lines (38 loc) · 1.57 KB
/
broadcast.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
import data
from flask import Flask, jsonify, request
import requests
import json
import threading
import time
def broadcast_transaction(new_trans):
kwargs = {}
kwargs['timeout'] = 25
trans_dict=new_trans.asDictionary()
try:#αρχικα το στελνω στον εαυτο μου
response=requests.post(data.myUrl+"/receive_transaction",json=trans_dict,**kwargs)
except requests.exceptions.Timeout:
print(f'broadcast: Request {data.myUrl}/receive_transaction timed out')
neighbours = [a for a in data.allUrls if a!=data.myUrl]
for node in neighbours:
try:
response=requests.post(node+"/receive_transaction",json=trans_dict,**kwargs)
except requests.exceptions.Timeout:
print(f'broadcast: Request {node}/receive_transaction timed out')
return
def broadcast_a_block(block):
kwargs = {}
kwargs['timeout'] = 25
try:#αρχικα το στελνω στον εαυτο μου
response=requests.post(data.myUrl+"/receiveABlock",json=block.asDictionary(),**kwargs)
print ( response.text)
except requests.exceptions.Timeout:
print(f'broadcast: Block {data.myUrl}/receiveABlock timed out')
neighbours = [a for a in data.allUrls if a!=data.myUrl]
for node in neighbours:
try:
response=requests.post(node+"/receiveABlock",json=block.asDictionary(),**kwargs)
print ( response.text)
except requests.exceptions.Timeout:
print(f'broadcast: Block {node}/receiveABlock timed out')
return
#print(response.status_code)