forked from f-prime/zCoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_difficulty.py
More file actions
49 lines (47 loc) · 1.27 KB
/
Copy pathget_difficulty.py
File metadata and controls
49 lines (47 loc) · 1.27 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
import socket
import json
import sqlite3
def get_difficulty(obj, data):
node = sqlite3.connect("nodes.db")
nodes = node.execute("SELECT ip, port FROM data WHERE relay=1")
nodes = nodes.fetchall()
difficulties = []
for x in nodes:
s = socket.socket()
try:
s.settimeout(1)
s.connect((x[0], x[1]))
except:
s.close()
continue
else:
s.send(json.dumps({"cmd":"get_raw_difficulty"}))
data = s.recv(1024)
if not data:
s.close()
continue
try:
data = int(data)
except:
continue
else:
difficulties.append(data)
out = 0
for x in difficulties:
out += x
try:
out = out/len(difficulties)
if out < 7:
out = 7
except ZeroDivisionError:
out = 7
try: # Without this it will raise an error when check_coin is called.
obj.send(json.dumps({"difficulty":out}))
except:
pass
return {"difficulty":out}
def get_raw_difficulty(obj, data):
db = sqlite3.connect("db.db")
check = db.execute("SELECT level FROM difficulty")
check = check.fetchall()[0]
obj.send(str(check[0]))