def calc_heavy(self): if self.heavy is None: r = [] r.append(struct.pack("<i", self.nVersion)) r.append(ser_uint256(self.hashPrevBlock)) r.append(ser_uint256(self.hashMerkleRoot)) r.append(struct.pack("<I", self.nTime)) r.append(struct.pack("<I", self.nBits)) r.append(struct.pack("<I", self.nNonce)) r.append(struct.pack("<H", self.nVote)) r.append(struct.pack("<H", self.nReward)) self.heavy = uint256_from_str(heavycoin_hash.getPoWHash(''.join(r))) return self.heavy
def calc_heavy(self): if self.heavy is None: r = [] r.append(struct.pack("<i", self.nVersion)) r.append(ser_uint256(self.hashPrevBlock)) r.append(ser_uint256(self.hashMerkleRoot)) r.append(struct.pack("<I", self.nTime)) r.append(struct.pack("<I", self.nBits)) r.append(struct.pack("<I", self.nNonce)) r.append(struct.pack("<H", self.nVote)) r.append(struct.pack("<H", self.nReward)) self.heavy = uint256_from_str( heavycoin_hash.getPoWHash(''.join(r))) return self.heavy
#!/usr/bin/env python import heavycoin_hash import weakref import binascii import StringIO from binascii import hexlify, unhexlify assert heavycoin_hash.getHash(unhexlify('28505e47680dc604864f9cf5366852c5222457981c'), 21) == unhexlify('0ebdb93812d6d4aab39511faa794bdb90718225804c94bb97cdfaee69ee1ba94') assert heavycoin_hash.getPoWHash(unhexlify('700000005d385ba114d079970b29a9418fd0549e7d68a95c7f168621a314201000000000578586d149fd07b22f3a8a347c516de7052f034d2b76ff68e0d6ecff9b77a45489e3fd511732011df0731000fafafa00')) == unhexlify('f0be3bccf0bbdf037cc3d300dd13bb4307a6fab7d6772ae24cddc30ecf190ef4') assert heavycoin_hash.getPoWHash(unhexlify('ea658e90946d34cc6b94c02a673aacd3d4ff89463caec174713be382f4a19eb6b09314294c5d72c53948ec6fc25ea7efef76e7ae2cc373614706d6e1275528361a0d8566a1098ec99a3abf4b0a35107b8aad12ca')) == unhexlify('3a046b355df0a75d900dfd11a90e68930c3bcd8213e9103740a39191c437d61f') assert heavycoin_hash.getPoWHash(unhexlify('da658e90946d34cc6b94c02a673aacd3d4ff89463caec174713be382f4a19eb6b09314294c5d72c53948ec6fc25ea7efef76e7ae2cc373614706d6e1275528361a0d8566a1098ec99a3abf4b0a35107b8aad12ca')) == unhexlify('4679a0f3977ed8a45a866c2b3f2d8ba6747d181bb8fad4d561b38f952069f30b') assert heavycoin_hash.getPoWHash(unhexlify('ea658e90946d34cc6b94c02a673aacd3d4ff89463caec174713be382f4a19eb6b09314294c5d72c53948ec6ec25ea7efef76e7ae2cc373614706d6e1275528361a0d8566a1098ec99a3abf4b0a35107b8aad12ca')) == unhexlify('8097c6d00343ed6b51580acef8a13812baa35641533b7147ca9cc136b30d8e84') assert heavycoin_hash.getPoWHash(unhexlify('ea658e90946d34cc6b94c02a673aacd3d4ff89463caec174713be382f4a19eb6b09314294c5d72c53948ec6fc25ea7efef76e7ae2cc373614706d6e1275528361a0d8566a1098ec99a3abf4b0a35107b8aad12cadd')) == unhexlify('3a046b355df0a75d900dfd11a90e68930c3bcd8213e9103740a39191c437d61f') assert heavycoin_hash.getPoWHash(unhexlify('ea658e90946d34cc6b94c02a673aacd3d4ff89463caec174713be382f4a19eb6b09314294c5d72c53948ec6fc25ea7efef76e7ae2cc373614706d6e1275528361a0d8566a1098ec99a3abf4b0a35107b8aad12ca999999')) == unhexlify('3a046b355df0a75d900dfd11a90e68930c3bcd8213e9103740a39191c437d61f') assert heavycoin_hash.getPoWHash(unhexlify('ea658e90946d34cc6b94c02a673aacd3d4ff89463caec174713be382f4a19eb6b09314294c5d72c53948ec6fc25ea7efef76e7ae2cc373614706d6e1275528361a0d8566a1098ec99a3abf4b0a35107b8aad12ca000000')) == unhexlify('3a046b355df0a75d900dfd11a90e68930c3bcd8213e9103740a39191c437d61f') assert heavycoin_hash.getPoWHash(unhexlify('fffffe90946d34cc6b94c02a673aacd3d4ff89463caec174713be382f4a19eb6b09314294c5d72c53948ec6fc25ea7efef76e7ae2cc373614706d6e1275528361a0d8566a1098ec99a3abf4b0a35107b8aad12ca')) == unhexlify('725909c4d43d191f2bd683cfec9e1590759930a0a054fb4b09055316d0084ace') assert heavycoin_hash.getPoWHash(unhexlify('000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')) == unhexlify('f2668b99b08850fd27ffe6d280ef552fce3ec364c3193bcbeb81e9a0e9cf9926') assert heavycoin_hash.getPoWHash(unhexlify('00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')) == unhexlify('f2668b99b08850fd27ffe6d280ef552fce3ec364c3193bcbeb81e9a0e9cf9926')
def hvc_powhash(data): return heavycoin_hash.getPoWHash(data)
def submit_share(self, job_id, worker_name, session, extranonce1_bin, extranonce2, time, nonce, difficulty, vote = None): '''Check parameters and finalize block template. If it leads to valid block candidate, asynchronously submits the block back to the bitcoin network. - extranonce1_bin is binary. No checks performed, it should be from session data - job_id, extranonce2, time, nonce, [vote] - in hex form sent by the client - difficulty - decimal number from session, again no checks performed - submitblock_callback - reference to method which receive result of submitblock() ''' if settings.COINDAEMON_ALGO == 'heavy': time = binascii.hexlify(struct.pack('<I', int(time, 16))) nonce = binascii.hexlify(struct.pack('<I', int(nonce, 16))) # Check if extranonce2 looks correctly. extranonce2 is in hex form... if len(extranonce2) != self.extranonce2_size * 2: raise SubmitException("Incorrect size of extranonce2. Expected %d chars" % (self.extranonce2_size*2)) # Check for job job = self.get_job(job_id) if job == None: raise SubmitException("Job '%s' not found" % job_id) # Check if ntime looks correct if len(time) != 8: raise SubmitException("Incorrect size of time. Expected 8 chars") if not job.check_ntime(int(time, 16)): raise SubmitException("Ntime out of range") # Check nonce if len(nonce) != 8: raise SubmitException("Incorrect size of nonce. Expected 8 chars") # Check for duplicated submit if not job.register_submit(extranonce1_bin, extranonce2, time, nonce): log.info("Duplicate from %s, (%s %s %s %s)" % \ (worker_name, binascii.hexlify(extranonce1_bin), extranonce2, time, nonce)) raise SubmitException("Duplicate share") # Now let's do the hard work! # --------------------------- # 0. Some sugar extranonce2_bin = binascii.unhexlify(extranonce2) ntime = int(time, 16) nnonce = int(nonce, 16) if settings.COINDAEMON_ALGO == 'heavy': # vote_bin = binascii.unhexlify(vote) nvote = int(vote, 16) # 1. Build coinbase coinbase_bin = job.serialize_coinbase(extranonce1_bin, extranonce2_bin) if settings.COINDAEMON_ALGO == 'heavy': coinbase_hash = heavycoin_hash.getHash(coinbase_bin, len(coinbase_bin)) else: coinbase_hash = util.doublesha(coinbase_bin) # 2. Calculate merkle root merkle_root_bin = job.merkletree.withFirst(coinbase_hash) merkle_root_int = util.uint256_from_str(merkle_root_bin) # 3. Serialize header with given merkle, ntime and nonce header_bin = job.serialize_header(merkle_root_int, ntime, nnonce, nvote) # 4. Reverse header and compare it with target of the user if settings.COINDAEMON_ALGO == 'scrypt': hash_bin = ltc_scrypt.getPoWHash(''.join([ header_bin[i*4:i*4+4][::-1] for i in range(0, 20) ])) elif settings.COINDAEMON_ALGO == 'scrypt-jane': hash_bin = yac_scrypt.getPoWHash(''.join([ header_bin[i*4:i*4+4][::-1] for i in range(0, 20) ]), int(ntime, 16)) elif settings.COINDAEMON_ALGO == 'quark': hash_bin = quark_hash.getPoWHash(''.join([ header_bin[i*4:i*4+4][::-1] for i in range(0, 20) ])) elif settings.COINDAEMON_ALGO == 'heavy': hash_bin = heavycoin_hash.getPoWHash(header_bin) # hash_bin = heavycoin_hash.getPoWHash(''.join([ header_bin[i*4:i*4+4][::-1] for i in range(0, 21) ])) elif settings.COINDAEMON_ALGO == 'skeinhash': hash_bin = skeinhash.skeinhash(''.join([ header_bin[i*4:i*4+4][::-1] for i in range(0, 20) ])) else: hash_bin = util.doublesha(''.join([ header_bin[i*4:i*4+4][::-1] for i in range(0, 20) ])) hash_int = util.uint256_from_str(hash_bin) scrypt_hash_hex = "%064x" % hash_int header_hex = binascii.hexlify(header_bin) if settings.COINDAEMON_ALGO == 'scrypt' or settings.COINDAEMON_ALGO == 'scrypt-jane': header_hex = header_hex+"000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000" elif settings.COINDAEMON_ALGO == 'quark': header_hex = header_hex+"000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000" elif settings.COINDAEMON_ALGO == 'heavy': header_hex = header_hex+"000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000" else: pass target_user = self.diff_to_target(difficulty) if hash_int > target_user: raise SubmitException("Share is above target") # Mostly for debugging purposes target_info = self.diff_to_target(100000) if hash_int <= target_info: log.info("Yay, share with diff above 100000") # Algebra tells us the diff_to_target is the same as hash_to_diff share_diff = int(self.diff_to_target(hash_int)) # 5. Compare hash with target of the network if hash_int <= job.target: # Yay! It is block candidate! log.info("We found a block candidate! %s" % scrypt_hash_hex) # Reverse the header and get the potential block hash (for scrypt only) #if settings.COINDAEMON_ALGO == 'scrypt' or settings.COINDAEMON_ALGO == 'sha256d': # if settings.COINDAEMON_Reward == 'POW': if settings.COINDAEMON_ALGO == 'heavy': block_hash_bin = heavycoin_hash.getHash(header_bin, 84) else: block_hash_bin = util.doublesha(''.join([ header_bin[i*4:i*4+4][::-1] for i in range(0, 20) ])) block_hash_hex = block_hash_bin[::-1].encode('hex_codec') #else: block_hash_hex = hash_bin[::-1].encode('hex_codec') #else: block_hash_hex = hash_bin[::-1].encode('hex_codec') # 6. Finalize and serialize block object if settings.COINDAEMON_ALGO == 'heavy': job.finalize(merkle_root_int, extranonce1_bin, extranonce2_bin, ntime, nnonce, nvote) else: job.finalize(merkle_root_int, extranonce1_bin, extranonce2_bin, ntime, nnonce) if not job.is_valid(): # Should not happen log.exception("FINAL JOB VALIDATION FAILED!(Try enabling/disabling tx messages)") # 7. Submit block to the network serialized = binascii.hexlify(job.serialize()) on_submit = self.bitcoin_rpc.submitblock(serialized, block_hash_hex, scrypt_hash_hex) if on_submit: self.update_block() if settings.SOLUTION_BLOCK_HASH: return (header_hex, block_hash_hex, share_diff, on_submit) else: return (header_hex, scrypt_hash_hex, share_diff, on_submit) if settings.SOLUTION_BLOCK_HASH: # Reverse the header and get the potential block hash (for scrypt only) only do this if we want to send in the block hash to the shares table block_hash_bin = util.doublesha(''.join([ header_bin[i*4:i*4+4][::-1] for i in range(0, 20) ])) block_hash_hex = block_hash_bin[::-1].encode('hex_codec') return (header_hex, block_hash_hex, share_diff, None) else: return (header_hex, scrypt_hash_hex, share_diff, None)
def block_header_hash(chain, header): import heavycoin_hash return heavycoin_hash.getPoWHash(header)
def submit_share(self, job_id, worker_name, session, extranonce1_bin, extranonce2, time, nonce, difficulty, vote=None): '''Check parameters and finalize block template. If it leads to valid block candidate, asynchronously submits the block back to the bitcoin network. - extranonce1_bin is binary. No checks performed, it should be from session data - job_id, extranonce2, time, nonce, [vote] - in hex form sent by the client - difficulty - decimal number from session, again no checks performed - submitblock_callback - reference to method which receive result of submitblock() ''' if settings.COINDAEMON_ALGO == 'heavy': time = binascii.hexlify(struct.pack('<I', int(time, 16))) nonce = binascii.hexlify(struct.pack('<I', int(nonce, 16))) # Check if extranonce2 looks correctly. extranonce2 is in hex form... if len(extranonce2) != self.extranonce2_size * 2: raise SubmitException( "Incorrect size of extranonce2. Expected %d chars" % (self.extranonce2_size * 2)) # Check for job job = self.get_job(job_id) if job == None: raise SubmitException("Job '%s' not found" % job_id) # Check if ntime looks correct if len(time) != 8: raise SubmitException("Incorrect size of time. Expected 8 chars") if not job.check_ntime(int(time, 16)): raise SubmitException("Ntime out of range") # Check nonce if len(nonce) != 8: raise SubmitException("Incorrect size of nonce. Expected 8 chars") # Check for duplicated submit if not job.register_submit(extranonce1_bin, extranonce2, time, nonce): log.info("Duplicate from %s, (%s %s %s %s)" % \ (worker_name, binascii.hexlify(extranonce1_bin), extranonce2, time, nonce)) raise SubmitException("Duplicate share") # Now let's do the hard work! # --------------------------- # 0. Some sugar extranonce2_bin = binascii.unhexlify(extranonce2) ntime = int(time, 16) nnonce = int(nonce, 16) if settings.COINDAEMON_ALGO == 'heavy': # vote_bin = binascii.unhexlify(vote) nvote = int(vote, 16) # 1. Build coinbase coinbase_bin = job.serialize_coinbase(extranonce1_bin, extranonce2_bin) if settings.COINDAEMON_ALGO == 'heavy': coinbase_hash = heavycoin_hash.getHash(coinbase_bin, len(coinbase_bin)) else: coinbase_hash = util.doublesha(coinbase_bin) # 2. Calculate merkle root merkle_root_bin = job.merkletree.withFirst(coinbase_hash) merkle_root_int = util.uint256_from_str(merkle_root_bin) # 3. Serialize header with given merkle, ntime and nonce header_bin = job.serialize_header(merkle_root_int, ntime, nnonce, nvote) # 4. Reverse header and compare it with target of the user if settings.COINDAEMON_ALGO == 'scrypt': hash_bin = ltc_scrypt.getPoWHash(''.join( [header_bin[i * 4:i * 4 + 4][::-1] for i in range(0, 20)])) elif settings.COINDAEMON_ALGO == 'scrypt-jane': hash_bin = yac_scrypt.getPoWHash( ''.join( [header_bin[i * 4:i * 4 + 4][::-1] for i in range(0, 20)]), int(ntime, 16)) elif settings.COINDAEMON_ALGO == 'quark': hash_bin = quark_hash.getPoWHash(''.join( [header_bin[i * 4:i * 4 + 4][::-1] for i in range(0, 20)])) elif settings.COINDAEMON_ALGO == 'heavy': hash_bin = heavycoin_hash.getPoWHash(header_bin) # hash_bin = heavycoin_hash.getPoWHash(''.join([ header_bin[i*4:i*4+4][::-1] for i in range(0, 21) ])) elif settings.COINDAEMON_ALGO == 'skeinhash': hash_bin = skeinhash.skeinhash(''.join( [header_bin[i * 4:i * 4 + 4][::-1] for i in range(0, 20)])) else: hash_bin = util.doublesha(''.join( [header_bin[i * 4:i * 4 + 4][::-1] for i in range(0, 20)])) hash_int = util.uint256_from_str(hash_bin) scrypt_hash_hex = "%064x" % hash_int header_hex = binascii.hexlify(header_bin) if settings.COINDAEMON_ALGO == 'scrypt' or settings.COINDAEMON_ALGO == 'scrypt-jane': header_hex = header_hex + "000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000" elif settings.COINDAEMON_ALGO == 'quark': header_hex = header_hex + "000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000" elif settings.COINDAEMON_ALGO == 'heavy': header_hex = header_hex + "000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000" else: pass target_user = self.diff_to_target(difficulty) if hash_int > target_user: raise SubmitException("Share is above target") # Mostly for debugging purposes target_info = self.diff_to_target(100000) if hash_int <= target_info: log.info("Yay, share with diff above 100000") # Algebra tells us the diff_to_target is the same as hash_to_diff share_diff = int(self.diff_to_target(hash_int)) # 5. Compare hash with target of the network if hash_int <= job.target: # Yay! It is block candidate! log.info("We found a block candidate! %s" % scrypt_hash_hex) # Reverse the header and get the potential block hash (for scrypt only) #if settings.COINDAEMON_ALGO == 'scrypt' or settings.COINDAEMON_ALGO == 'sha256d': # if settings.COINDAEMON_Reward == 'POW': if settings.COINDAEMON_ALGO == 'heavy': block_hash_bin = heavycoin_hash.getHash(header_bin, 84) else: block_hash_bin = util.doublesha(''.join( [header_bin[i * 4:i * 4 + 4][::-1] for i in range(0, 20)])) block_hash_hex = block_hash_bin[::-1].encode('hex_codec') #else: block_hash_hex = hash_bin[::-1].encode('hex_codec') #else: block_hash_hex = hash_bin[::-1].encode('hex_codec') # 6. Finalize and serialize block object if settings.COINDAEMON_ALGO == 'heavy': job.finalize(merkle_root_int, extranonce1_bin, extranonce2_bin, ntime, nnonce, nvote) else: job.finalize(merkle_root_int, extranonce1_bin, extranonce2_bin, ntime, nnonce) if not job.is_valid(): # Should not happen log.exception( "FINAL JOB VALIDATION FAILED!(Try enabling/disabling tx messages)" ) # 7. Submit block to the network serialized = binascii.hexlify(job.serialize()) on_submit = self.bitcoin_rpc.submitblock(serialized, block_hash_hex, scrypt_hash_hex) if on_submit: self.update_block() if settings.SOLUTION_BLOCK_HASH: return (header_hex, block_hash_hex, share_diff, on_submit) else: return (header_hex, scrypt_hash_hex, share_diff, on_submit) if settings.SOLUTION_BLOCK_HASH: # Reverse the header and get the potential block hash (for scrypt only) only do this if we want to send in the block hash to the shares table block_hash_bin = util.doublesha(''.join( [header_bin[i * 4:i * 4 + 4][::-1] for i in range(0, 20)])) block_hash_hex = block_hash_bin[::-1].encode('hex_codec') return (header_hex, block_hash_hex, share_diff, None) else: return (header_hex, scrypt_hash_hex, share_diff, None)
#!/usr/bin/env python import heavycoin_hash import weakref import binascii import StringIO from binascii import hexlify, unhexlify assert heavycoin_hash.getHash( unhexlify('28505e47680dc604864f9cf5366852c5222457981c'), 21) == unhexlify( '0ebdb93812d6d4aab39511faa794bdb90718225804c94bb97cdfaee69ee1ba94') assert heavycoin_hash.getPoWHash( unhexlify( '700000005d385ba114d079970b29a9418fd0549e7d68a95c7f168621a314201000000000578586d149fd07b22f3a8a347c516de7052f034d2b76ff68e0d6ecff9b77a45489e3fd511732011df0731000fafafa00' )) == unhexlify( 'f0be3bccf0bbdf037cc3d300dd13bb4307a6fab7d6772ae24cddc30ecf190ef4') assert heavycoin_hash.getPoWHash( unhexlify( 'ea658e90946d34cc6b94c02a673aacd3d4ff89463caec174713be382f4a19eb6b09314294c5d72c53948ec6fc25ea7efef76e7ae2cc373614706d6e1275528361a0d8566a1098ec99a3abf4b0a35107b8aad12ca' )) == unhexlify( '3a046b355df0a75d900dfd11a90e68930c3bcd8213e9103740a39191c437d61f') assert heavycoin_hash.getPoWHash( unhexlify( 'da658e90946d34cc6b94c02a673aacd3d4ff89463caec174713be382f4a19eb6b09314294c5d72c53948ec6fc25ea7efef76e7ae2cc373614706d6e1275528361a0d8566a1098ec99a3abf4b0a35107b8aad12ca' )) == unhexlify( '4679a0f3977ed8a45a866c2b3f2d8ba6747d181bb8fad4d561b38f952069f30b') assert heavycoin_hash.getPoWHash( unhexlify( 'ea658e90946d34cc6b94c02a673aacd3d4ff89463caec174713be382f4a19eb6b09314294c5d72c53948ec6ec25ea7efef76e7ae2cc373614706d6e1275528361a0d8566a1098ec99a3abf4b0a35107b8aad12ca' )) == unhexlify(