def worker(q, s): started = time.time() hash_count = 0 while 1: job = q.get() if job.get('login_id'): login_id = job.get('login_id') print('Login ID: {}'.format(login_id)) blob = job.get('blob') target = job.get('target') job_id = job.get('job_id') height = job.get('height') block_major = int(blob[:2], 16) cnv = 0 if block_major >= 7: cnv = block_major - 6 if cnv > 5: seed_hash = binascii.unhexlify(job.get('seed_hash')) print('New job with target: {}, RandomX, height: {}'.format(target, height)) else: print('New job with target: {}, CNv{}, height: {}'.format(target, cnv, height)) target = struct.unpack('I', binascii.unhexlify(target))[0] if target >> 32 == 0: target = int(0xFFFFFFFFFFFFFFFF / int(0xFFFFFFFF / target)) nonce = 1 while 1: bin = pack_nonce(blob, nonce) if cnv > 5: hash = pyrx.get_rx_hash(bin, seed_hash, height) else: hash = pycryptonight.cn_slow_hash(bin, cnv, 0, height) hash_count += 1 sys.stdout.write('.') sys.stdout.flush() hex_hash = binascii.hexlify(hash).decode() r64 = struct.unpack('Q', hash[24:])[0] if r64 < target: elapsed = time.time() - started hr = int(hash_count / elapsed) print('{}Hashrate: {} H/s'.format(os.linesep, hr)) if nicehash: nonce = struct.unpack('I', bin[39:43])[0] submit = { 'method':'submit', 'params': { 'id': login_id, 'job_id': job_id, 'nonce': binascii.hexlify(struct.pack('<I', nonce)).decode(), 'result': hex_hash }, 'id':1 } print('Submitting hash: {}'.format(hex_hash)) s.sendall(str(json.dumps(submit)+'\n').encode('utf-8')) select.select([s], [], [], 3) if not q.empty(): break nonce += 1
import pyrx import binascii assert pyrx.__version__ >= '0.0.3' expected = [ 'a86260b7bf2c35910177ae47bad732b415d977d865a3d64c12e06a3f012b2ee7', 'aa820e6869feccba3e58790437de698bafc2eeaf1a135c928d1b59b2473fb74d', '776a5cb55b212950c55911a99b1fc35f36e937e2b1ff6cdf6aff27e0bb36f637', 'f2f1ae1030f53b743da7c13f9c2f85db6991f0617e360663c815158d72897ec4', '0ea23341e489a9720ff4bfbd0391338918a295d46416b87dfe8a785cce9eb51d' ] seed_hash = binascii.unhexlify( '63eceef7919087068ac5d1b7faffa23fc90a58ad0ca89ecb224a2ef7ba282d48') for x in range(5): m = "Hello RandomX {}".format(x) print("Hashing: {}".format(m)) if x == 0: print("(first takes a while, please wait)") h = 1 + x bh = pyrx.get_rx_hash(m, seed_hash, h) hh = binascii.hexlify(bh).decode() print("Result: {}".format(hh)) assert hh == expected[x]
def main(): base_diff = 2**256 - 1 payload = { 'jsonrpc': '2.0', 'id': '0', 'method': 'get_block_template', 'params': { 'wallet_address': wallet_address } } print('Fetching block template') req = requests.post(rpc_url, json=payload) result = req.json().get('result') bhb = result.get('blockhashing_blob') btb = result.get('blocktemplate_blob') diff = result.get('difficulty') print('Target difficulty: {}'.format(diff)) height = result.get('height') block_major = int(btb[:2], 16) cnv = 0 if block_major >= 7: cnv = block_major - 6 if cnv > 5: seed_hash = binascii.unhexlify(result.get('seed_hash')) nonce = 1 hash_count = 0 started = time.time() print('Mining for a valid hash') try: while 1: bin = pack_nonce(bhb, nonce) if cnv > 5: hash = pyrx.get_rx_hash(bin, seed_hash, height) else: hash = pycryptonight.cn_slow_hash(bin, cnv, 0, height) hash_count += 1 sys.stdout.write('.') sys.stdout.flush() hex_hash = binascii.hexlify(hash) if base_diff / int(hex_hash, 16) >= diff: break else: nonce += 1 except KeyboardInterrupt: print('{}Aborting'.format(os.linesep)) sys.exit(-1) elapsed = time.time() - started hr = int(hash_count / elapsed) print('{}Hashrate: {} H/s'.format(os.linesep, hr)) print('Found a valid hash: {}'.format(hex_hash.decode())) btb = binascii.hexlify(pack_nonce(btb, nonce)) payload = { 'jsonrpc': '2.0', 'id': '0', 'method': 'submit_block', 'params': [btb] } print('Submitting block') print(payload) req = requests.post(rpc_url, json=payload) result = req.json() print(result)