-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
171 lines (159 loc) · 5.72 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
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
import argparse, re, json, time, requests
class Client:
def __init__(self,host,port):
self.host = host
self.port = port
@property
def url(self):
url = ""
url = self.host+":"+str(self.port)
p = r'^(?:https?|\w+:\/\/)?([-\w.]+)(?::?(\d+)?([\/\w]+)?|\/.*)$'
m = re.match(p, url, re.I)
return "http://{}:{}".format(m.group(1),m.group(2))
def resolve_nodes(self,nodes=None):
print("="*50)
print("Starting nodes resolution")
if nodes is None:
print("Requesting nodes...")
nodes = self.get_nodes()
if nodes:
# If we recived the node list
for node in nodes:
print("Resolving:",node)
# Iterate over each node
data = {'node': node}
r = requests.post(self.url+"/nodes/resolve",data=json.dumps(data))
if r.status_code==201:
print("Executed resolution for:",node)
else:
print("Resolution request for {} failed".format(node))
else:
print("Error requesting nodes")
print("Ended nodes resolution")
print("="*50)
def get_nodes(self):
try:
r = requests.get(self.url+"/nodes")
if r.status_code==200:
nodes = json.loads(r.text)
return [str(n) for n in nodes]
else:
raise Exception("Couldn't pull nodes from "+self.url)
except Exception as e:
print("Error:",str(e))
return False
def resolve_transactions(self, nodes):
print("="*50)
print("Resolving transactions")
for node in nodes:
print("Resolving transactiond with node:",node)
try:
url = self.url+"/transactions/resolve"
print("URL:",url)
data = {'node':node}
r = requests.post(url,data=json.dumps(data))
if r.status_code==201:
print("Transaction resolve started")
else:
print("Error requesting transaction resolve:",r.status_code)
print("Content:",r.content)
except Exception as e:
print("Error resolving transactions with node:",node)
print("Ended resolving transactions")
print("="*50)
def clean_transactions(self):
print("Cleaning transactions...")
try:
r = requests.get(self.url+"/transactions/clean")
if r.status_code==201:
print("Done!")
else:
print("Error")
except Exception as e:
print("Error cleaning transactions:",str(e))
def resolve_transactions_all(self):
print("Resolving transactions")
try:
url = self.url+"/transactions/resolve"
r = requests.get(url)
if r.status_code==201:
print("Resolve started!")
else:
print("Request error!")
except Exception as e:
print("Error:",str(e))
def resolve_nodes_all(self):
print("Resolving nodes")
try:
url = self.url+"/nodes/resolve"
r = requests.get(url)
if r.status_code==201:
print("Resolve started")
else:
print("Request error")
except Exception as e:
print("Error:",str(e))
def is_working(self):
try:
r = requests.get(self.url+"/working")
j = r.json()
if r.status_code==200:
return j['chains'] or j['transactions']
else:
raise Exception("Error, status code:",r.status_code)
except Exception as e:
print("Error:",str(e))
return False
def is_mining(self):
r = requests.get(self.url+"/mining")
if r.status_code == 200:
return r.json()
else:
raise Exception("Request error")
def mine(self):
print("Makinng mine request")
try:
r = requests.get(self.url+"/mine")
if r.status_code==200:
if r.json():
print("Mining process started!")
else:
print("Mining not started")
else:
raise Exception("Error on mine request")
except Exception as e:
print("Error:",str(e))
def main(args):
client = Client(args.host, args.port)
n = 0
print("Client started!")
while True:
st = time.time()
print("Starting iteration:",n)
client.clean_transactions()
client.resolve_nodes_all()
client.resolve_transactions_all()
try:
mining = client.is_mining()
if not mining:
client.mine()
else:
print("Client already mining")
except Exception as e:
print("Error:",str(e))
print("Ended iteration:",n)
print("-Time elapsed: {:.2f}s".format(time.time()-st))
wts = time.time()
while True:
if client.is_working() or (time.time()-st)<args.seconds:
time.sleep(1)
else:
break
print("-Waiting time: {:.2f}s".format(time.time()-wts))
if __name__=="__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-H","--host",default="http://localhost",type=str,help="Host where the node runs on.")
parser.add_argument("-p","--port",default=5000,type=int, help="Port where node listens")
parser.add_argument("-s","--seconds",default=10,type=int,help="Min seconds between loop")
args = parser.parse_args()
main(args)