-
Notifications
You must be signed in to change notification settings - Fork 2
/
GetWork.py
236 lines (197 loc) · 6.73 KB
/
GetWork.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
"""
GetWork.py -- Gets work from a mining pool.
by elchupa
"""
from Config import Config
import json, base64
import httplib2
from binascii import a2b_hex, b2a_hex
from Utils import swap32, sha2int, sha2x
from datetime import datetime
import logging
#resource/thread pool stuff
from ResourcePool import ResourcePool
from ThreadPool import ThreadPool
class GetWork:
def __init__( self, database, poolname ):
self.db = database
self.poolname = poolname
self.config = self.db.getPool( self.poolname )
self.address = self.config['address']
self.port = self.config['port']
self.username = self.config['username']
self.password = self.config['password']
self.timeout = int( self.config['timeout'] )
self.authorizationStr = base64.b64encode(self.username + ":" + self.password).replace('\n','')
self.id = 0
self.target = ""
self.logger = logging.getLogger( "PoolManager.Pool.GetWork" )
try:
self.target = self.config['target']
except:
self.target = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000"
self.targetInt = sha2int( sha2x( self.target ) )
self.header = { "X-Mining-Extensions": "longpoll", "Authorization": "Basic " + self.authorizationStr, "User-Agent": "polcbm", "Content-Type": "text/plain" }
self.url = "http://" + self.address + ":" + str(self.port)
#Pool of http objects which will hopefully fix the memory issue.
self.httpPool = ResourcePool( self.makeHttp )
#Used to generate httplib2 objects for the pool
def makeHttp( self ):
http = httplib2.Http( timeout=self.timeout )
return http
def changePool( self, poolName ):
config = None
try:
config = self.db.getPool( poolName )
except:
pass
if config != None:
self.config = config
self.address = self.config['address']
self.port = self.config['port']
self.username = self.config['username']
self.password = self.config['password']
self.timeout = int( self.config['timeout'] )
self.authorizationStr = base64.b64encode( self.username + ":" + self.password ).replace( '\n', '' )
self.header = { "X-Mining-Extensions": "longpoll", "Authorization": "Base " + self.authorizationStr, "User-Agent": "polcbm", "Content-Type": "text/plain" }
self.url = "http://" + self.address + ":" + str( self.port )
else:
self.logger.error( "No Pool with name: " + poolName )
return False
return True
def getWork( self, minerName, minerPassword ):
request = json.dumps( { "method": "getwork", "params": [], "id": "json" } )
self.id += 1
message = {
"result": None,
"error": {
"code": -1,
"message": "unregistered miner"
},
"id": 1
}
with self.httpPool as http:
self.logger.debug( "Making getwork request" )
ret, content = http.request( self.url, "POST", headers=self.header, body=request )
miner = self.db.addUser( self.poolname, minerName, minerPassword )
success = None
if miner:
success = True
try:
message = json.loads( content )
self.db.incGetWork( self.poolname )
except:
self.logger.warn( "Error decoding json" )
else:
success = False
#try:
# http = httplib2.Http( timeout=self.timeout )
# ret, content = http.request( self.url, "POST", headers=self.header, body=request )
#
# miner = self.db.addUser( self.poolname, minerName, minerPassword )
#
# if miner:
# try:
# message = json.loads( content )
# self.db.incGetWork( self.poolname )
# del http
# return message, ret, minerName
# except:
# del http
# self.logger.warn( "Error decoding json" )
# else:
# del http
# return message, None, minerName
#except Exception, e:
# self.logger.warn( "Error getting work" )
self.logger.debug( "Was " + minerName + "'s request successful? " + str( success ) )
self.db.con.end_request()
if success:
return message, ret, minerName
else:
return message, None, minerName
def submit( self, minerName, work):
validity = self.checkWorkValidity( minerName, work )
request = json.dumps( work )
ret = ""
content = ""
message = {
"version": "1.1",
"id": 1,
"error": None,
"result": False
}
with self.httpPool as http:
self.logger.info( "Submitting work from: " + str( minerName ) )
ret, content = http.request( self.url, "POST", headers=self.header, body=request )
success = False
try:
message = json.loads( content )
if validity['status']:
message['result'] = False
share = self.db.addShare( self.poolname, validity['share'] )
if share:
message['result'] = False
else:
self.db.incShares( self.poolname )
messagep['result'] = True
success = True
if not message['result']:
self.db.incStales( self.poolname )
except:
success = False
self.db.con.end_request()
if success:
self.logger.info( str( minerName ) + " subbmited a valid share" )
return message, ret, minerName
else:
self.logger.info( str( minerName ) + " subbmited a invalid share" )
return message, None, minerName
#try:
# http = httplib2.Http( timeout=self.timeout )
# ret, content = http.request( self.url, "POST", headers=self.header, body=request )
#
# try:
# message = json.loads( content )
#
# #if message['result']:
# # self.db.incBlocksFound( self.poolname )
#
# if validity['status']:
# message['result'] = False
#
# share = self.db.addShare( self.poolname, validity['share'] )
#
# if share:
# message['result'] = False
# else:
# self.db.incShares( self.poolname )
# message['result'] = True
#
# if not message['result']:
# self.db.incStales( self.poolname )
# del http
# return message, ret, minerName
# except Exception, e:
# del http
# self.logger.warn( "Error Decoding Json submit()" )
#except Exception, e:
# self.logger.warn( "Error submitting work" )
#
#return message, None, minerName
def checkWorkValidity( self, minerName, work ):
share = self.getShare( minerName, work )
if share['data'][:4] != b'\1\0\0\0':
{ "share": share, "status": False }
hash = sha2x( share['data'] )
if hash[28:] != b'\0\0\0\0':
return { "share": share, "status": False }
hashInt = sha2int( hash )
if hashInt >= self.targetInt:
return { "share": share, "status": False }
return { "share": share, "status": True }
def getShare( self, minerName, work ):
return { "miner": minerName, "odata": work['params'][0], "data": swap32( a2b_hex( work['params'][0] ) )[:80], "time": datetime.now() }
if __name__ == "__main__":
g = GetWork( "test.json" )
print g.getWork( "test" )