-
Notifications
You must be signed in to change notification settings - Fork 1
/
stratum_listener.py
82 lines (64 loc) · 2.29 KB
/
stratum_listener.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
import asyncio
import configparser
from datetime import datetime
import json
import signal
import sys
import time
from urllib.parse import urlparse
RETRIES = 10
async def send_auth(reader, writer, user, password):
auth = {"params": [user, password], "id": 2, "method": "mining.authorize"}
print(f'send: {auth}')
writer.write(f'{json.dumps(auth)}\n'.encode())
line = await reader.readline()
print(f'auth response: {line}')
def send_subscribe(writer):
subscribe = {"id": 1, "method": "mining.subscribe", "params": []}
writer.write(f'{json.dumps(subscribe)}\n'.encode())
async def pool_listener(poolname, url, user, password):
parsed = urlparse(url)
timestamp = datetime.now().isoformat(timespec='minutes')
with open(f'./data/{poolname}-{timestamp}.json', 'wt') as file:
for i in range(RETRIES):
try:
reader, writer = await asyncio.open_connection(
parsed.hostname, parsed.port)
send_subscribe(writer)
await send_auth(reader, writer, user, password)
while True:
line = await reader.readline()
msg = json.loads(line.decode())
msg["r"] = datetime.now().isoformat()
file.write(json.dumps(msg)+'\n')
file.flush()
except:
if i < RETRIES:
time.sleep(5)
print(f'Reconnecting {poolname}')
continue
else:
raise
async def run_with(config):
tasks = []
for pool in config.sections():
if not config[pool].getboolean("enabled"):
continue
print(f'Starting {pool}')
task = asyncio.create_task(
pool_listener(pool, config[pool]["url"],
config[pool]["user"], config[pool]["password"]))
tasks.append(task)
def signal_handler(sig, frame):
print('Stopping..')
for task in tasks:
task.cancel()
print('Stopped.')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
for task in tasks:
await task
if __name__ == "__main__":
config = configparser.ConfigParser()
config.read("pools.ini")
asyncio.run(run_with(config))