This repository has been archived by the owner on May 7, 2018. It is now read-only.
forked from m0mchil/poclbm
/
BitcoinMiner.py
192 lines (158 loc) · 5.92 KB
/
BitcoinMiner.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
import sys
import numpy as np
import pyopencl as cl
from struct import *
from Queue import Queue
from Queue import Empty
from threading import Thread
from time import sleep, time
from datetime import datetime
from jsonrpc import ServiceProxy
from jsonrpc.proxy import JSONRPCException
from jsonrpc.json import JSONDecodeException
def uint32(x):
return x & 0xffffffffL
def rot(x, y):
return (x<<y | x>>(32-y))
def sharound(a,b,c,d,e,f,g,h,x,K):
t1=h+(rot(e, 26)^rot(e, 21)^rot(e, 7))+(g^(e&(f^g)))+K+x
t2=(rot(a, 30)^rot(a, 19)^rot(a, 10))+((a&b)|(c&(a|b)))
return (uint32(d + t1), uint32(t1+t2))
def partial(state, data):
partial = np.array(state)
(partial[3], partial[7]) = sharound(partial[0],partial[1],partial[2],partial[3],partial[4],partial[5],partial[6],partial[7],data[0],0x428A2F98)
(partial[2], partial[6]) = sharound(partial[7],partial[0],partial[1],partial[2],partial[3],partial[4],partial[5],partial[6],data[1],0x71374491)
(partial[1], partial[5]) = sharound(partial[6],partial[7],partial[0],partial[1],partial[2],partial[3],partial[4],partial[5],data[2],0xB5C0FBCF)
return partial
def if_else(condition, trueVal, falseVal):
if condition:
return trueVal
else:
return falseVal
def bytereverse(x):
return uint32(( ((x) << 24) | (((x) << 8) & 0x00ff0000) | (((x) >> 8) & 0x0000ff00) | ((x) >> 24) ))
class BitcoinMiner(Thread):
def __init__(self, platform, context, host, user, password, port=8332, frames=60, rate=1, askrate=5, worksize=-1, vectors=False):
Thread.__init__(self)
(defines, self.rateDivisor) = if_else(vectors, ('-DVECTORS', 500), ('', 1000))
self.context = context
self.rate = float(rate)
self.askrate = int(askrate)
self.worksize = int(worksize)
self.frames = frames
if (self.context.devices[0].extensions.find('cl_amd_media_ops') != -1):
defines += ' -DBITALIGN'
kernelFile = open('BitcoinMiner.cl', 'r')
self.miner = cl.Program(self.context, kernelFile.read()).build(defines)
kernelFile.close()
if (self.worksize == -1):
self.worksize = self.miner.search.get_work_group_info(cl.kernel_work_group_info.WORK_GROUP_SIZE, self.context.devices[0])
self.workQueue = Queue()
self.resultQueue = Queue()
self.bitcoin = ServiceProxy('http://%s:%s@%s:%s' % (user, password, host, port))
def say(self, format, args=()):
sys.stdout.write('\r \r' + format % args)
sys.stdout.flush()
def sayLine(self, format, args=()):
self.say(format + '\n', args)
def blockFound(self, hash, accepted):
# designed to be overridden
self.sayLine('%s, %s, %s', (datetime.now().strftime("%d/%m/%Y %H:%M"), hash, if_else(accepted, 'accepted', 'invalid or stale')))
def mine(self):
self.start()
lastWork = 0
work = result = None
try:
while True:
if not work:
try:
work = self.bitcoin.getwork()
except JSONRPCException, e:
self.say('%s', e.error['message'])
except (JSONDecodeException, IOError):
self.say('Problems communicating with bitcoin RPC')
try:
result = self.resultQueue.get(True, 1)
except Empty:
pass
if result or (time() - lastWork > self.askrate):
self.workQueue.put(work)
lastWork = time()
work = None
if result:
try:
accepted = self.bitcoin.getwork(result['data'])
except (JSONDecodeException, IOError):
self.say('Problems communicating with bitcoin RPC')
else:
self.blockFound(pack('I', long(result['hash'])).encode('hex'), accepted)
result = None
except KeyboardInterrupt:
self.workQueue.put('stop')
print '\nbye'
sleep(0.1)
def run(self):
frame = float(1)/float(self.frames)
window = frame/30
upper = frame + window
lower = frame - window
unit = self.worksize * 256
globalThreads = unit
queue = cl.CommandQueue(self.context)
base = lastRate = threadsRun = lastNTime = 0
output = np.zeros(2, np.uint32)
output_buf = cl.Buffer(self.context, cl.mem_flags.WRITE_ONLY | cl.mem_flags.USE_HOST_PTR, hostbuf=output)
work = None
while True:
if (not work) or (not self.workQueue.empty()):
try:
work = self.workQueue.get(True, 1)
except Empty:
continue
else:
if not work:
continue
elif work == 'stop':
return
try:
data = np.array(unpack('IIIIIIIIIIIIIIII', work['data'][128:].decode('hex')), dtype=np.uint32)
state = np.array(unpack('IIIIIIII', work['midstate'].decode('hex')), dtype=np.uint32)
target = np.array(unpack('IIIIIIII', work['target'].decode('hex')), dtype=np.uint32)
state2 = partial(state, data)
except Exception as e:
self.sayLine('Wrong data format from RPC!')
sys.exit()
kernelStart = time()
self.miner.search( queue, (globalThreads, ), (self.worksize, ),
data[0], data[1], data[2],
state[0], state[1], state[2], state[3], state[4], state[5], state[6], state[7],
state2[1], state2[2], state2[3], state2[5], state2[6], state2[7],
target[6], target[7], pack('I', base), output_buf)
cl.enqueue_read_buffer(queue, output_buf, output)
if (time() - lastRate > self.rate):
self.say('%s khash/s', int((threadsRun / (time() - lastRate)) / self.rateDivisor))
threadsRun = 0
lastRate = time()
queue.finish()
kernelTime = time() - kernelStart
threadsRun += globalThreads
base = uint32(base + globalThreads)
if (kernelTime < lower):
globalThreads += unit
elif (kernelTime > upper and globalThreads > unit):
globalThreads -= unit
if output[0]:
result = {}
d = work['data']
d = d[:136] + pack('I', long(data[1])).encode('hex') + d[144:152] + pack('I', long(output[1])).encode('hex') + d[160:]
result['data'] = d
result['hash'] = output[0]
self.resultQueue.put(result)
output[0] = 0
cl.enqueue_write_buffer(queue, output_buf, output)
work = None
continue
if (time() - lastNTime > 1):
data[1] = bytereverse(bytereverse(data[1]) + 1)
state2 = partial(state, data)
lastNTime = time()