-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminer-control.py
130 lines (115 loc) · 4.04 KB
/
miner-control.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
import argparse
import random
import requests
import os
import time
import signal
import subprocess
from multiprocessing import Event, Process, Manager
dir_path = os.path.dirname(os.path.realpath(__file__))
parser = argparse.ArgumentParser(description='Let us do some mining')
parser.add_argument("--numWorkers", type=int, default=3, help="The private password")
parser.add_argument("mineCmd", default="go_miner.exe", help="Miner binary")
parser.add_argument("--offset", type=int, default=15459021544, help="Space between workers")
parser.add_argument("--idServer", help="Set an ID server to ping")
args = parser.parse_args()
with open("prev_hash", "rb") as prev_hash_file:
hash_of_preceding_coin = prev_hash_file.read()
jobs = []
event = Event() # event for found desired hash
manager = Manager()
d = manager.dict()
def get_or_update_id(desired):
resp = requests.get(args.idServer+"/id/%d" % desired)
return int(resp.content)
if args.idServer is None:
myId = 0
else:
myId = get_or_update_id(0)
print("Got ID: %d" % myId)
with open("public_id", "r") as public_id_file:
id_of_miner = public_id_file.read()
print("Mining using public ID: %s" % id_of_miner)
def get_last_coin():
"""
Ping the server to get the last coin ID
"""
resp = requests.post("http://cpen442coin.ece.ubc.ca/last_coin")
if resp.status_code != 200 or "coin_id" not in resp.json():
raise Exception("Couldn't get last coin")
return resp.json()["coin_id"].encode()
def claim_coin_blob(coin_blob):
data = {
"coin_blob": coin_blob,
"id_of_miner": id_of_miner,
}
print("Submitting: %s" % data)
return requests.post("http://cpen442coin.ece.ubc.ca/claim_coin", json=data)
def f(event, i, d):
"""
Helper for mining in a pool
"""
cmd = args.mineCmd.split(" ")
# probably won't be mining with more than 15 workers
cmd.append(str(myId*args.offset*15 + i*args.offset))
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
d['pid%d' % i] = proc.pid
(output, error) = proc.communicate()
coin_blob = output.strip().decode('utf-8')
print("COIN BLOB: %s" % coin_blob)
resp = claim_coin_blob(coin_blob)
if resp.status_code == 200:
print("Yay, found a coin!!")
event.set()
def create_workers():
"""
Helper for creating workers
"""
print("Creating workers")
for i in range(args.numWorkers):
p = Process(
target=f,
args=(event, i, d,))
p.start()
jobs.append(p)
print('created worker with PID:', p.pid)
def terminate_workers():
"""
Helper for terminating workers
"""
print("Terminating workers")
print(d)
for i, p in enumerate(jobs):
p.terminate()
pidid = "pid%d" % i
if pidid in d:
pid = d[pidid]
if pid != -1:
try:
os.kill(pid, signal.SIGINT)
except:
pass # this will die if we found a coin blob
d[pidid] = - 1
jobs.clear()
while True:
# if we found something, we can terminate all the
# workers
if event.is_set():
terminate_workers()
new_hash_of_preceding_coin = get_last_coin()
if args.idServer is not None:
get_or_update_id(myId)
if new_hash_of_preceding_coin != hash_of_preceding_coin:
hash_of_preceding_coin = new_hash_of_preceding_coin
print("Head changed: %s" % hash_of_preceding_coin)
with open("prev_hash", "wb") as prev_hash_file:
prev_hash_file.write(hash_of_preceding_coin)
terminate_workers()
create_workers()
elif not jobs:
# reploy workers initally so we don't have to wait for next block
print("Initalizing with head: %s" % hash_of_preceding_coin)
create_workers()
# wait 13 seconds
# TODO: update the hashes in central server for even faster updates?
time.sleep(13)