def decode(self, server, block_header, target, job_id=None, extranonce2=None): if block_header: job = Object() binary_data = block_header.decode('hex') data0 = list(unpack('<16I', binary_data[:64])) + ([0] * 48) job.target = unpack('<8I', target.decode('hex')) job.header = binary_data[:68] job.merkle_end = uint32(unpack('<I', binary_data[64:68])[0]) job.time = uint32(unpack('<I', binary_data[68:72])[0]) job.difficulty = uint32(unpack('<I', binary_data[72:76])[0]) job.state = sha256(STATE, data0) job.targetQ = 2**256 / int(''.join(list(chunks(target, 2))[::-1]), 16) job.job_id = job_id job.extranonce2 = extranonce2 job.server = server if job.difficulty != self.difficulty: self.set_difficulty(job.difficulty) return job
def decode(self, server, block_header, target, job_id = None, extranonce2 = None): if block_header: job = Object() binary_data = block_header.decode('hex') #data0 = list(unpack('<16I', binary_data[:64])) + ([0] * 48) job.headerX = binary_data[:76] job.dataX = unpack('<19I', job.headerX) job.target = unpack('<8I', target.decode('hex')) job.header = binary_data[:68] job.merkle_end = uint32(unpack('<I', binary_data[64:68])[0]) job.time = uint32(unpack('<I', binary_data[68:72])[0]) job.difficulty = uint32(unpack('<I', binary_data[72:76])[0]) # job.state = sha256(STATE, data0) job.targetQ = 2**256 / int(''.join(list(chunks(target, 2))[::-1]), 16) job.job_id = job_id job.extranonce2 = extranonce2 job.server = server if job.difficulty != self.difficulty: self.set_difficulty(job.difficulty) return job
def partial(state, merkle_end, time, difficulty, f): state2 = list(state) data = [merkle_end, time, difficulty] for i in xrange(3): (state2[~(i - 4) & 7], state2[~(i - 8) & 7]) = sharound( state2[(~(i - 1) & 7)], state2[~(i - 2) & 7], state2[~(i - 3) & 7], state2[~(i - 4) & 7], state2[~(i - 5) & 7], state2[~(i - 6) & 7], state2[~(i - 7) & 7], state2[~(i - 8) & 7], data[i], K[i]) f[0] = uint32(data[0] + (rotr(data[1], 7) ^ rotr(data[1], 18) ^ (data[1] >> 3))) f[1] = uint32(data[1] + (rotr(data[2], 7) ^ rotr(data[2], 18) ^ (data[2] >> 3)) + 0x01100000) f[2] = uint32(data[2] + (rotr(f[0], 17) ^ rotr(f[0], 19) ^ (f[0] >> 10))) f[3] = uint32(0x11002000 + (rotr(f[1], 17) ^ rotr(f[1], 19) ^ (f[1] >> 10))) f[4] = uint32(0x00000280 + (rotr(f[0], 7) ^ rotr(f[0], 18) ^ (f[0] >> 3))) f[5] = uint32(f[0] + (rotr(f[1], 7) ^ rotr(f[1], 18) ^ (f[1] >> 3))) f[6] = uint32(state[4] + (rotr(state2[1], 6) ^ rotr(state2[1], 11) ^ rotr(state2[1], 25)) + (state2[3] ^ (state2[1] & (state2[2] ^ state2[3]))) + 0xe9b5dba5) f[7] = uint32( (rotr(state2[5], 2) ^ rotr(state2[5], 13) ^ rotr(state2[5], 22)) + ((state2[5] & state2[6]) | (state2[7] & (state2[5] | state2[6])))) return state2
def calculateF(state, merkle_end, time, difficulty, f, state2): data = [merkle_end, time, difficulty] #W2 f[0] = uint32(data[2]) #W16 f[1] = uint32(data[0] + (rotr(data[1], 7) ^ rotr(data[1], 18) ^ (data[1] >> 3))) #W17 f[2] = uint32(data[1] + (rotr(data[2], 7) ^ rotr(data[2], 18) ^ (data[2] >> 3)) + 0x01100000) #2 parts of the first SHA round f[3] = uint32(state[4] + (rotr(state2[1], 6) ^ rotr(state2[1], 11) ^ rotr(state2[1], 25)) + (state2[3] ^ (state2[1] & (state2[2] ^ state2[3]))) + 0xe9b5dba5) f[4] = uint32((rotr(state2[5], 2) ^ rotr(state2[5], 13) ^ rotr(state2[5], 22)) + ((state2[5] & state2[6]) | (state2[7] & (state2[5] | state2[6]))))
def calculateF(state, merkle_end, time, difficulty, f, state2): data = [merkle_end, time, difficulty] #W2 f[0] = uint32(data[2]) #W16 f[1] = uint32(data[0] + (rotr(data[1], 7) ^ rotr(data[1], 18) ^ (data[1] >> 3))) #W17 f[2] = uint32(data[1] + (rotr(data[2], 7) ^ rotr(data[2], 18) ^ (data[2] >> 3)) + 0x01100000) #2 parts of the first SHA round f[3] = uint32(state[4] + (rotr(state2[1], 6) ^ rotr(state2[1], 11) ^ rotr(state2[1], 25)) + (state2[3] ^ (state2[1] & (state2[2] ^ state2[3]))) + 0xe9b5dba5) f[4] = uint32( (rotr(state2[5], 2) ^ rotr(state2[5], 13) ^ rotr(state2[5], 22)) + ((state2[5] & state2[6]) | (state2[7] & (state2[5] | state2[6]))))
def partial(state, merkle_end, time, difficulty, f): state2 = list(state) data = [merkle_end, time, difficulty] for i in xrange(3): (state2[~(i-4)&7], state2[~(i-8)&7]) = sharound(state2[(~(i-1)&7)],state2[~(i-2)&7],state2[~(i-3)&7],state2[~(i-4)&7],state2[~(i-5)&7],state2[~(i-6)&7],state2[~(i-7)&7],state2[~(i-8)&7],data[i],K[i]) f[0] = uint32(data[0] + (rotr(data[1], 7) ^ rotr(data[1], 18) ^ (data[1] >> 3))) f[1] = uint32(data[1] + (rotr(data[2], 7) ^ rotr(data[2], 18) ^ (data[2] >> 3)) + 0x01100000) f[2] = uint32(data[2] + (rotr(f[0], 17) ^ rotr(f[0], 19) ^ (f[0] >> 10))) f[3] = uint32(0x11002000 + (rotr(f[1], 17) ^ rotr(f[1], 19) ^ (f[1] >> 10))) f[4] = uint32(0x00000280 + (rotr(f[0], 7) ^ rotr(f[0], 18) ^ (f[0] >> 3))) f[5] = uint32(f[0] + (rotr(f[1], 7) ^ rotr(f[1], 18) ^ (f[1] >> 3))) f[6] = uint32(state[4] + (rotr(state2[1], 6) ^ rotr(state2[1], 11) ^ rotr(state2[1], 25)) + (state2[3] ^ (state2[1] & (state2[2] ^ state2[3]))) + 0xe9b5dba5) f[7] = uint32((rotr(state2[5], 2) ^ rotr(state2[5], 13) ^ rotr(state2[5], 22)) + ((state2[5] & state2[6]) | (state2[7] & (state2[5] | state2[6])))) return state2
def sha256(state, data): digest = list(state) for i in xrange(64): if i > 15: data[i] = R(data[i-2], data[i-7], data[i-15], data[i-16]) (digest[~(i-4)&7], digest[~(i-8)&7]) = sharound(digest[(~(i-1)&7)],digest[~(i-2)&7],digest[~(i-3)&7],digest[~(i-4)&7],digest[~(i-5)&7],digest[~(i-6)&7],digest[~(i-7)&7],digest[~(i-8)&7],data[i],K[i]) result = [] result.append(uint32(digest[0] + state[0])) result.append(uint32(digest[1] + state[1])) result.append(uint32(digest[2] + state[2])) result.append(uint32(digest[3] + state[3])) result.append(uint32(digest[4] + state[4])) result.append(uint32(digest[5] + state[5])) result.append(uint32(digest[6] + state[6])) result.append(uint32(digest[7] + state[7])) return result
def put_job(self): if self.busy: return temperature = self.get_temperature() if temperature < self.cutoff_temp: response = request(self.device, b'ZDX') if self.is_ok(response): if self.switch.update_time: self.job.time = bytereverse( uint32(long(time())) - self.job.time_delta) data = b''.join([ pack('<8I', *self.job.state), pack('<3I', self.job.merkle_end, self.job.time, self.job.difficulty) ]) response = request(self.device, b''.join([b'>>>>>>>>', data, b'>>>>>>>>'])) if self.is_ok(response): self.busy = True self.job_started = time() self.last_job = Object() self.last_job.header = self.job.header self.last_job.merkle_end = self.job.merkle_end self.last_job.time = self.job.time self.last_job.difficulty = self.job.difficulty self.last_job.target = self.job.target self.last_job.state = self.job.state self.last_job.job_id = self.job.job_id self.last_job.extranonce2 = self.job.extranonce2 self.last_job.server = self.job.server self.last_job.miner = self self.check_interval = CHECK_INTERVAL if not self.switch.update_time or bytereverse( self.job.time) - bytereverse( self.job.original_time) > 55: self.update = True self.job = None else: say_line('%s: bad response when sending block data: %s', (self.id(), response)) else: say_line('%s: bad response when submitting job (ZDX): %s', (self.id(), response)) else: say_line('%s: temperature exceeds cutoff, waiting...', self.id())
def sha256(state, data): digest = list(state) for i in xrange(64): if i > 15: data[i] = R(data[i - 2], data[i - 7], data[i - 15], data[i - 16]) (digest[~(i - 4) & 7], digest[~(i - 8) & 7]) = sharound( digest[(~(i - 1) & 7)], digest[~(i - 2) & 7], digest[~(i - 3) & 7], digest[~(i - 4) & 7], digest[~(i - 5) & 7], digest[~(i - 6) & 7], digest[~(i - 7) & 7], digest[~(i - 8) & 7], data[i], K[i]) result = [] result.append(uint32(digest[0] + state[0])) result.append(uint32(digest[1] + state[1])) result.append(uint32(digest[2] + state[2])) result.append(uint32(digest[3] + state[3])) result.append(uint32(digest[4] + state[4])) result.append(uint32(digest[5] + state[5])) result.append(uint32(digest[6] + state[6])) result.append(uint32(digest[7] + state[7])) return result
def put_job(self): if self.busy: return temperature = self.get_temperature() if temperature < self.cutoff_temp: response = request(self.device, b'ZDX') if self.is_ok(response): if self.switch.update_time: self.job.time = bytereverse(uint32(long(time())) - self.job.time_delta) data = b''.join([pack('<8I', *self.job.state), pack('<3I', self.job.merkle_end, self.job.time, self.job.difficulty)]) response = request(self.device, b''.join([b'>>>>>>>>', data, b'>>>>>>>>'])) if self.is_ok(response): self.busy = True self.job_started = time() self.last_job = Object() self.last_job.header = self.job.header self.last_job.merkle_end = self.job.merkle_end self.last_job.time = self.job.time self.last_job.difficulty = self.job.difficulty self.last_job.target = self.job.target self.last_job.state = self.job.state self.last_job.job_id = self.job.job_id self.last_job.extranonce2 = self.job.extranonce2 self.last_job.server = self.job.server self.last_job.miner = self self.check_interval = CHECK_INTERVAL if not self.switch.update_time or bytereverse(self.job.time) - bytereverse(self.job.original_time) > 55: self.update = True self.job = None else: say_line('%s: bad response when sending block data: %s', (self.id(), response)) else: say_line('%s: bad response when submitting job (ZDX): %s', (self.id(), response)) else: say_line('%s: temperature exceeds cutoff, waiting...', self.id())
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 R(x2, x7, x15, x16): return uint32((rot(x2,15)^rot(x2,13)^((x2)>>10)) + x7 + (rot(x15,25)^rot(x15,14)^((x15)>>3)) + x16)
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 R(x2, x7, x15, x16): return uint32((rot(x2, 15) ^ rot(x2, 13) ^ ((x2) >> 10)) + x7 + (rot(x15, 25) ^ rot(x15, 14) ^ ((x15) >> 3)) + x16)
def mining_thread(self): say_line('started BFL miner on %s', (self.id())) while not self.should_stop: try: self.device = open_device(self.port) response = init_device(self.device) if not is_good_init(response): say_line('Failed to initialize %s (response: %s), retrying...', (self.id(), response)) self.device.close() self.device = None sleep(1) continue last_rated = time() iterations = 0 self.job = None self.busy = False while not self.should_stop: if (not self.job) or (not self.work_queue.empty()): try: self.job = self.work_queue.get(True, 1) except Empty: if not self.busy: continue else: if not self.job and not self.busy: continue targetQ = self.job.targetQ self.job.original_time = self.job.time self.job.time_delta = uint32(long(time())) - bytereverse(self.job.time) if not self.busy: self.put_job() else: result = self.check_result() if result: now = time() self.busy = False r = self.last_job job_duration = now - self.job_started self.put_job() self.min_interval = min(self.min_interval, job_duration) iterations += 4294967296 t = now - last_rated if t > self.options.rate: self.update_rate(now, iterations, t, targetQ) last_rated = now; iterations = 0 if result != b'NO-NONCE\n': r.nonces = result self.switch.put(r) sleep(self.min_interval - (CHECK_INTERVAL * 2)) else: if result is None: self.check_interval = min(self.check_interval * 2, 1) sleep(self.check_interval) except Exception: say_exception() if self.device: self.device.close() self.device = None sleep(1)
def mining_thread(self): say_line('started OpenCL miner on platform %d, device %d (%s)', (self.options.platform, self.device_index, self.device_name)) (self.defines, rate_divisor, hashspace) = ('', 1000, 0xFFFFFFFF) self.defines += (' -D%sOUTPUT_SIZE=%s' % (self.defspace, str(self.output_size))) self.defines += (' -D%sOUTPUT_MASK=%s' % (self.defspace, str(self.output_size - 1))) self.defines += (' -D%sENDIAN_LITTLE=%d' % (self.defspace, self.device.endian_little)) self.defines += (' -D%sGPU_AMD=%d' % (self.defspace, self.gpu_amd)) self.defines += (' -I%s%s' % (self.defspace, os.getcwd())) say_line("Compiler defines: %s", self.defines) self.load_kernel() frame = 1.0 / max(self.frames, 3) unit = self.worksize * 256 global_threads = unit * 10 queue = cl.CommandQueue(self.context) last_rated_pace = last_rated = last_n_time = last_temperature = time() base = last_hash_rate = threads_run_pace = threads_run = 0 output = bytearray((self.output_size + 1) * 4) output_buffer = cl.Buffer(self.context, cl.mem_flags.WRITE_ONLY | cl.mem_flags.USE_HOST_PTR, hostbuf=output) self.kernel.set_arg(12, output_buffer) work = None temperature = 0 while True: if self.should_stop: return sleep(self.frameSleep) if (not work) or (not self.work_queue.empty()): try: work = self.work_queue.get(True, 1) except Empty: continue else: if not work: continue nonces_left = hashspace self.queue_kernel_parameters(work) if temperature < self.cutoff_temp: self.kernel.set_arg(11, pack('<I', base)) cl.enqueue_nd_range_kernel(queue, self.kernel, (global_threads,), (self.worksize,)) nonces_left -= global_threads threads_run_pace += global_threads threads_run += global_threads base = uint32(base + global_threads) else: threads_run_pace = 0 last_rated_pace = time() sleep(self.cutoff_interval) now = time() if self.adapterIndex is not None: t = now - last_temperature if temperature >= self.cutoff_temp or t > 1: last_temperature = now with adl_lock: self.temperature = self.get_temperature() temperature = self.temperature t = now - last_rated_pace if t > 1: rate = (threads_run_pace / t) / rate_divisor last_rated_pace = now; threads_run_pace = 0 r = last_hash_rate / rate if r < 0.9 or r > 1.1: global_threads = max(unit * int((rate * frame * rate_divisor) / unit), unit) last_hash_rate = rate t = now - last_rated if t > self.options.rate: self.update_rate(now, threads_run, t, work.targetQ, rate_divisor) last_rated = now; threads_run = 0 queue.finish() cl.enqueue_read_buffer(queue, output_buffer, output) queue.finish() if output[-1]: result = Object() result.header = work.header result.headerX = work.headerX result.merkle_end = work.merkle_end result.time = work.time result.difficulty = work.difficulty result.target = work.target result.dataX = work.dataX[:] result.nonces = output[:] result.job_id = work.job_id result.extranonce2 = work.extranonce2 result.server = work.server result.miner = self self.switch.put(result) output[:] = b'\x00' * len(output) cl.enqueue_write_buffer(queue, output_buffer, output) for miner in self.switch.miners: miner.update = True if not self.switch.update_time: if nonces_left < 6 * global_threads * self.frames: self.update = True nonces_left += 0xFFFFFFFFFFFF elif 0xFFFFFFFFFFF < nonces_left < 0xFFFFFFFFFFFF: say_line('warning: job finished, %s is idle', self.id()) work = None elif now - last_n_time > 1: last_n_time = now self.update_time_counter += 1 if self.update_time_counter >= self.switch.max_update_time: self.update = True self.update_time_counter = 1
def mining_thread(self): say_line('started OpenCL miner on platform %d, device %d (%s)', (self.options.platform, self.device_index, self.device_name)) (self.defines, rate_divisor, hashspace) = if_else(self.vectors, ('-DVECTORS', 500, 0x7FFFFFFF), ('', 1000, 0xFFFFFFFF)) self.defines += (' -DOUTPUT_SIZE=' + str(self.output_size)) self.defines += (' -DOUTPUT_MASK=' + str(self.output_size - 1)) self.load_kernel() frame = 1.0 / max(self.frames, 3) unit = self.worksize * 256 global_threads = unit * 10 queue = cl.CommandQueue(self.context) last_rated_pace = last_rated = last_n_time = last_temperature = time() base = last_hash_rate = threads_run_pace = threads_run = 0 output = np.zeros(self.output_size + 1, np.uint32) output_buffer = cl.Buffer(self.context, cl.mem_flags.WRITE_ONLY | cl.mem_flags.USE_HOST_PTR, hostbuf=output) self.kernel.set_arg(20, output_buffer) work = None temperature = 0 while True: if self.should_stop: return sleep(self.frameSleep) if (not work) or (not self.work_queue.empty()): try: work = self.work_queue.get(True, 1) except Empty: continue else: if not work: continue nonces_left = hashspace state = work.state state2 = work.state2 f = work.f self.kernel.set_arg(0, state[0]) self.kernel.set_arg(1, state[1]) self.kernel.set_arg(2, state[2]) self.kernel.set_arg(3, state[3]) self.kernel.set_arg(4, state[4]) self.kernel.set_arg(5, state[5]) self.kernel.set_arg(6, state[6]) self.kernel.set_arg(7, state[7]) self.kernel.set_arg(8, state2[1]) self.kernel.set_arg(9, state2[2]) self.kernel.set_arg(10, state2[3]) self.kernel.set_arg(11, state2[5]) self.kernel.set_arg(12, state2[6]) self.kernel.set_arg(13, state2[7]) self.kernel.set_arg(15, f[0]) self.kernel.set_arg(16, f[1]) self.kernel.set_arg(17, f[2]) self.kernel.set_arg(18, f[3]) self.kernel.set_arg(19, f[4]) if temperature < self.cutoff_temp: self.kernel.set_arg(14, pack('I', base)) cl.enqueue_nd_range_kernel(queue, self.kernel, (global_threads,), (self.worksize,)) nonces_left -= global_threads threads_run_pace += global_threads threads_run += global_threads base = uint32(base + global_threads) else: threads_run_pace = 0 last_rated_pace = time() sleep(self.cutoff_interval) now = time() if self.adapterIndex != None: t = now - last_temperature if temperature >= self.cutoff_temp or t > 1: last_temperature = now with adl_lock: temperature = self.get_temperature() t = now - last_rated_pace if t > 1: rate = (threads_run_pace / t) / rate_divisor last_rated_pace = now; threads_run_pace = 0 r = last_hash_rate / rate if r < 0.9 or r > 1.1: global_threads = max(unit * int((rate * frame * rate_divisor) / unit), unit) last_hash_rate = rate t = now - last_rated if t > self.options.rate: self.update_rate(now, threads_run, t, work.targetQ, rate_divisor) last_rated = now; threads_run = 0 queue.finish() cl.enqueue_read_buffer(queue, output_buffer, output) queue.finish() if output[self.output_size]: result = Object() result.header = work.header result.merkle_end = work.merkle_end result.time = work.time result.difficulty = work.difficulty result.target = work.target result.state = np.array(state) result.nonces = np.array(output) result.job_id = work.job_id result.extranonce2 = work.extranonce2 result.server = work.server result.miner = self self.switch.put(result) output.fill(0) cl.enqueue_write_buffer(queue, output_buffer, output) if not self.switch.update_time: if nonces_left < 3 * global_threads * self.frames: self.update = True nonces_left += 0xFFFFFFFFFFFF elif 0xFFFFFFFFFFF < nonces_left < 0xFFFFFFFFFFFF: say_line('warning: job finished, %s is idle', self.id()) work = None elif now - last_n_time > 1: work.time = bytereverse(bytereverse(work.time) + 1) state2 = partial(state, work.merkle_end, work.time, work.difficulty, f) calculateF(state, work.merkle_end, work.time, work.difficulty, f, state2) self.kernel.set_arg(8, state2[1]) self.kernel.set_arg(9, state2[2]) self.kernel.set_arg(10, state2[3]) self.kernel.set_arg(11, state2[5]) self.kernel.set_arg(12, state2[6]) self.kernel.set_arg(13, state2[7]) self.kernel.set_arg(15, f[0]) self.kernel.set_arg(16, f[1]) self.kernel.set_arg(17, f[2]) self.kernel.set_arg(18, f[3]) self.kernel.set_arg(19, f[4]) last_n_time = now self.update_time_counter += 1 if self.update_time_counter >= self.switch.max_update_time: self.update = True self.update_time_counter = 1
def mining_thread(self): say_line('started BFL miner on %s', (self.id())) while not self.should_stop: try: self.device = open_device(self.port) response = init_device(self.device) if not is_good_init(response): say_line( 'Failed to initialize %s (response: %s), retrying...', (self.id(), response)) self.device.close() self.device = None sleep(1) continue last_rated = time() iterations = 0 self.job = None self.busy = False while not self.should_stop: if (not self.job) or (not self.work_queue.empty()): try: self.job = self.work_queue.get(True, 1) except Empty: if not self.busy: continue else: if not self.job and not self.busy: continue targetQ = self.job.targetQ self.job.original_time = self.job.time self.job.time_delta = uint32(long( time())) - bytereverse(self.job.time) if not self.busy: self.put_job() else: result = self.check_result() if result: now = time() self.busy = False r = self.last_job job_duration = now - self.job_started self.put_job() self.min_interval = min(self.min_interval, job_duration) iterations += 4294967296 t = now - last_rated if t > self.options.rate: self.update_rate(now, iterations, t, targetQ) last_rated = now iterations = 0 if result != b'NO-NONCE\n': r.nonces = result self.switch.put(r) sleep(self.min_interval - (CHECK_INTERVAL * 2)) else: if result is None: self.check_interval = min( self.check_interval * 2, 1) sleep(self.check_interval) except Exception: say_exception() if self.device: self.device.close() self.device = None sleep(1)