-
Notifications
You must be signed in to change notification settings - Fork 4
/
pool.py
executable file
·147 lines (107 loc) · 4.13 KB
/
pool.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
import json
import logging
import signal
import socket
import sys
import urllib2
import httplib
import datetime
from log import Log
import os
import re
import pickle
from config import Config
import calibration
import yaml
import pools
import time
class Pool:
def __init__(self, pool_name):
self.data_path = os.getenv("HOME") + "/.minotaur"
if not os.path.exists(self.data_path):
os.mkdir(self.data_path, 0755)
self.pool_name = pool_name
for key in ['shortname','algorithm','whattomine_coin_no','endpoints','miners']:
if not Config().get('pools.%s.%s' % (self.pool_name, key)):
raise Exception("%s is not specified for pool: %s" % (key, self.pool_name))
self.shortname = Config().get('pools.%s.shortname' % (self.pool_name))
self.algorithm = Config().get('pools.%s.algorithm' % (self.pool_name))
self.coin_no = Config().get('pools.%s.whattomine_coin_no' % (self.pool_name))
self.pool_fee = Config().get('pools.%s.pool_fee' % (self.pool_name))
self.endpoints = Config().get('pools.%s.endpoints' % (self.pool_name))
self.supported_miners = Config().get('pools.%s.miners' % (self.pool_name))
self.paying_file = self.data_path + "/%s.yml" % (pool_name)
if os.path.exists(self.paying_file):
self.last_refresh = os.stat(self.paying_file).st_mtime
self.paying = yaml.load(open(self.paying_file).read())
else:
self.paying = {}
self.last_refresh = None
def shortened(self, region):
return self.shortname
def refresh_data(self, save=True):
if self.last_refresh != None and (time.time() - self.last_refresh) < 300:
return True
try:
opener = urllib2.build_opener()
opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
response = opener.open("http://whattomine.com/coins/%d.json" % (self.coin_no))
network = json.loads(response.read())
base_block_time = float(1) / float(network['nethash'])
daily = (float(86400) / float(network['block_time']) * base_block_time * float(network['block_reward']))
self.paying = daily * float(network['exchange_rate']) * 1000
if self.pool_fee:
self.paying -= (self.paying / 100) * self.pool_fee
self.save_paying_data()
self.last_refresh = os.stat(self.paying_file).st_mtime
return True
except urllib2.HTTPError, e:
return False
except urllib2.URLError, e:
return False
except httplib.HTTPException, e:
return False
except Exception:
return False
def save_paying_data(self):
with open(self.paying_file + ".new", "w") as f:
f.write(yaml.dump(self.paying))
os.rename(self.paying_file + ".new", self.paying_file)
def mbtc_per_day(self, benchmarks, paying=False):
if paying == 'cached':
paying = yaml.load(open(self.paying_file).read())
if not paying:
paying = self.paying
if self.algorithm not in benchmarks.keys():
return {
None: {
self.algorithm: 0
}
}
return {
None: {
self.algorithm: paying * benchmarks[self.algorithm]
}
}
def get_best_miner_and_algorithm(self, device):
hashrates = calibration.Calibration().get_hashrates(device)
if hashrates == None:
return [None, None, None, None]
best_hashrates = calibration.Calibration().get_best_algorithm_benchmarks(hashrates)
if self.algorithm not in best_hashrates.keys():
return [None, None, None, None]
payrates = self.mbtc_per_day(best_hashrates)
payrates[None] = pools.Pools().deduct_power_from_payrates(device, payrates[None])
best_miner = device.get_best_miner_for_algorithm(self.algorithm, self.supported_miners)
if best_miner == None:
return [None, None, None, None]
return [best_miner, self.algorithm, None, payrates[None][self.algorithm]]
def get_payrate(self, device, hashrates, miner_name, algo, region):
benchmarks = {
algo: hashrates[miner_name][algo]['hashrate']
}
payrates = self.mbtc_per_day(benchmarks)
payrates[None] = pools.Pools().deduct_power_from_payrates(device, payrates[None])
return payrates[None][algo]
def get_endpoints(self, algo, region):
return self.endpoints