本文整理汇总了Python中util.uint256_from_str函数的典型用法代码示例。如果您正苦于以下问题:Python uint256_from_str函数的具体用法?Python uint256_from_str怎么用?Python uint256_from_str使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了uint256_from_str函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: verify
def verify(self, extranonce2, ntime, nonce):
return
coinbase = self.coinb1 + self.extranonce1 + extranonce2 + self.coinb2
coinbase_hash_bin = doublesha(binascii.unhexlify(coinbase))
merkle_root = build_merkle_root(self.merkle_branch, coinbase_hash_bin)
merkle_root = ser_uint256_be(uint256_from_str(merkle_root))
preheader = self.version + self.prevhash + merkle_root.encode("hex") + ntime + self.nbits
preheader_bin = preheader.decode("hex")
preheader_bin = ''.join([preheader_bin[i*4:i*4+4][::-1] for i in range(0,19)])
hash_bin = doublesha(preheader_bin + nonce.decode("hex")[::-1])
print hash_bin.encode("hex")
val = struct.unpack("<I", hash_bin[-4:])[0]
assert val < THRESH, (val, THRESH)
开发者ID:kmod,项目名称:bitcoin_mining,代码行数:14,代码来源:miner.py
示例2: _target
def _target(self, job_id, extranonce2, ntime, preheader_bin):
try:
start = time.time()
first_sha = hashlib.sha256(preheader_bin)
if sys.maxint > 2**32:
max_nonce = 2**32
else:
max_nonce = 2**31 - 1
i = 0
while i < max_nonce:
if i % 100000 == 0:
print i, "%.1f kh/s" % (i * .001 / (time.time() - start + .001))
if self._quit:
print "QUITTING WORKER"
break
nonce_bin = struct.pack(">I", i)
if TEST:
nonce_bin = "b2957c02".decode("hex")[::-1]
# header_bin = preheader_bin + nonce_bin
# hash_bin = doublesha(header_bin)
# assert hash_bin == finish_dsha(first_sha, nonce_bin)
hash_bin = finish_dsha(first_sha, nonce_bin)
val = struct.unpack("<I", hash_bin[-4:])[0]
if val < THRESH:
nonce = nonce_bin[::-1].encode("hex")
print nonce, extranonce2, ntime
print hash_bin.encode("hex")
hash_int = uint256_from_str(hash_bin)
block_hash_hex = "%064x" % hash_int
print block_hash_hex
self._cl.submit(job_id, extranonce2, ntime, nonce)
break
elif val < THRESH*10:
print "almost: %d (<%d)" % (val, THRESH)
i += 1
# elif i == 0:
# print hash_bin.encode("hex")
self._done_ev.set()
except:
traceback.print_exc()
os._exit(1)
开发者ID:kmod,项目名称:bitcoin_mining,代码行数:48,代码来源:sha_mining.py
示例3: submit_share
def submit_share(self, job_id, worker_name, session, extranonce1_bin, extranonce2, ntime, nonce,
difficulty):
'''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, ntime, nonce - 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()
'''
# 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(ntime) != 8:
raise SubmitException("Incorrect size of ntime. Expected 8 chars")
if not job.check_ntime(int(ntime, 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, ntime, nonce):
log.info("Duplicate from %s, (%s %s %s %s)" % \
(worker_name, binascii.hexlify(extranonce1_bin), extranonce2, ntime, nonce))
raise SubmitException("Duplicate share")
# Now let's do the hard work!
# ---------------------------
# 0. Some sugar
extranonce2_bin = binascii.unhexlify(extranonce2)
ntime_bin = binascii.unhexlify(ntime)
nonce_bin = binascii.unhexlify(nonce)
# 1. Build coinbase
coinbase_bin = job.serialize_coinbase(extranonce1_bin, extranonce2_bin)
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_bin, nonce_bin)
# 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) ]))
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"
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':
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')
#.........这里部分代码省略.........
开发者ID:Neozonz,项目名称:blockie,代码行数:101,代码来源:template_registry.py
示例4: submit_share
def submit_share(self, job_id, worker_name, session, extranonce1_bin, extranonce2, ntime, nonce,
difficulty):
'''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, ntime, nonce - 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()
'''
# 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(ntime) != 8:
raise SubmitException("Incorrect size of ntime. Expected 8 chars")
if not job.check_ntime(int(ntime, 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, ntime, nonce):
log.info("Duplicate from %s, (%s %s %s %s)" % \
(worker_name, binascii.hexlify(extranonce1_bin), extranonce2, ntime, nonce))
raise SubmitException("Duplicate share")
# Now let's do the hard work!
# ---------------------------
# 0. Some sugar
extranonce2_bin = binascii.unhexlify(extranonce2)
ntime_bin = binascii.unhexlify(ntime)
nonce_bin = binascii.unhexlify(nonce)
# 1. Build coinbase
coinbase_bin = job.serialize_coinbase(extranonce1_bin, extranonce2_bin)
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_bin, nonce_bin)
# 4. Reverse header and compare it with target of the user
hash_bin = util.scrypt(''.join([ header_bin[i*4:i*4+4][::-1] for i in range(0, 20) ]))
hash_int = util.uint256_from_str(hash_bin)
block_hash_hex = "%064x" % hash_int
header_hex = binascii.hexlify(header_bin)
target_user = self.diff_to_target(difficulty)
if hash_int > target_user and \
( 'prev_jobid' not in session or session['prev_jobid'] < job_id \
or 'prev_diff' not in session or hash_int > self.diff_to_target(session['prev_diff']) ):
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" % block_hash_hex)
# 6. Finalize and serialize block object
job.finalize(merkle_root_int, extranonce1_bin, extranonce2_bin, int(ntime, 16), int(nonce, 16))
if not job.is_valid():
# Should not happen
log.error("Final job validation failed!")
# 7. Submit block to the network
serialized = binascii.hexlify(job.serialize())
on_submit = self.bitcoin_rpc.submitblock(serialized)
return (header_hex, block_hash_hex, share_diff, on_submit)
return (header_hex, block_hash_hex, share_diff, None)
开发者ID:CryptoManiac,项目名称:stratum-mining,代码行数:96,代码来源:template_registry.py
示例5: submit_share
def submit_share(self, job_id, worker_name, session, extranonce1_bin, extranonce2, ntime, nonce, difficulty, ip=False):
'''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, ntime, nonce - in hex form sent by the client
- difficulty - decimal number from session
- submitblock_callback - reference to method which receive result of submitblock()
- difficulty is checked to see if its lower than the vardiff minimum target or pool target
from conf/config.py and if it is the share is rejected due to it not meeting the requirements for a share
'''
log.debug("Session: %s" % session)
# Share Difficulty should never be 0 or below
if difficulty <= 0 :
log.exception("Worker %s @ IP: %s seems to be submitting Fake Shares"%(worker_name,ip))
raise SubmitException("Diff is %s Share Rejected Reporting to Admin"%(difficulty))
# 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, worker_name, ip)
if job == None:
if settings.REJECT_STALE_SHARES:
# Reject stale share
raise SubmitException("Job '%s' not found" % job_id)
else:
# Accept stale share but do not continue checking, return a bunch of nothingness
log.info("Accepted Stale Share from %s, (%s %s %s %s)" % \
(worker_name, binascii.hexlify(extranonce1_bin), extranonce2, ntime, nonce))
return (None, None, None, None, None, None)
# Check if ntime looks correct
check_result, error_message = util.check_ntime(ntime)
if not check_result:
raise SubmitException(error_message)
if not job.check_ntime(int(ntime, 16), getattr(settings, 'NTIME_AGE')):
raise SubmitException("Ntime out of range")
# Check nonce
check_result, error_message = util.check_nonce(nonce)
if not check_result:
raise SubmitException(error_message)
# Check for duplicated submit
if not job.register_submit(extranonce1_bin, extranonce2, ntime, nonce):
log.info("Duplicate from %s, (%s %s %s %s)" % \
(worker_name, binascii.hexlify(extranonce1_bin), extranonce2, ntime, nonce))
raise SubmitException("Duplicate share")
# Now let's do the hard work!
# ---------------------------
# 0. Some sugar
extranonce2_bin = binascii.unhexlify(extranonce2)
ntime_bin = util.get_ntime_bin(ntime)
nonce_bin = util.get_nonce_bin(nonce)
target_user = self.diff_to_target(difficulty)
target_info = self.diff_to_target(100000)
# 1. Build coinbase
coinbase_bin = job.serialize_coinbase(extranonce1_bin, extranonce2_bin)
coinbase_hash = util.get_coinbase_hash(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_bin, nonce_bin)
# 4. Convert header into hex according to hash algorythim
block_hash = util.get_hash_hex(header_bin, ntime, nonce)
# 5a Compare it with target of the user
check_result, message = util.check_header_target(block_hash['int'], target_user)
if not check_result:
log.debug("Oops, somthing is wrong: target_user=%s, difficulty=%s, share_diff=%s" % (target_user, difficulty, int(self.diff_to_target(block_hash['int']))))
raise SubmitException("%s. Hash: %s" % (message, block_hash['hex']))
# Mostly for debugging purposes, just a celebratory message that's being carried over from older versions
check_result, message = util.check_above_yay_target(block_hash['int'], target_info)
if check_result:
log.info(message)
# Algebra tells us the diff_to_target is the same as hash_to_diff
if settings.VDIFF_FLOAT:
share_diff = float(self.diff_to_target(block_hash['int']))
else:
share_diff = int(self.diff_to_target(block_hash['int']))
log.debug("share_diff: %s" % share_diff)
log.debug("job.target: %s" % job.target)
log.debug("block_hash_int: %s" % block_hash['int'])
#.........这里部分代码省略.........
开发者ID:tuaris,项目名称:TidePool,代码行数:101,代码来源:template_registry.py
示例6: _target
def _target(self, difficulty, job_id, extranonce2, ntime, preheader_bin):
try:
start = time.time()
if sys.maxint > 2**32:
max_nonce = 2**32
else:
max_nonce = 2**31 - 1
i = 0
THRESH = 0x00010000 / difficulty
# # THRESH = 1 << difficulty
# # THRESH /= 4
# THRESH = 0x00002000
print "thresh:", THRESH
hashes_per_thresh = (1 << 32) / THRESH
if TEST:
print preheader_bin.encode("hex")
preheader_bin = "01000000f615f7ce3b4fc6b8f61e8f89aedb1d0852507650533a9e3b10b9bbcc30639f279fcaa86746e1ef52d3edb3c4ad8259920d509bd073605c9bf1d59983752a6b06b817bb4ea78e011d012d59d4".decode("hex")[:-4]
while i < max_nonce:
if TEST:
i = 0x012d59d4
if self._quit:
print "QUITTING WORKER"
break
if i and i % 10000 == 0:
hashrate = i * 1.0 / (time.time() - start + .001)
print i, "%.1f kh/s (%.1fs/share))" % (hashrate * 0.001, hashes_per_thresh / hashrate)
nonce_bin = struct.pack(">I", i)
header_bin = preheader_bin + nonce_bin
hash_bin = scrypt.hash(header_bin, header_bin, 1024, 1, 1, 32)
# print
# print header_bin.encode("hex")
# print hash_bin.encode("hex")
val = struct.unpack("<I", hash_bin[-4:])[0]
# print val
# print
if val < THRESH:
nonce = nonce_bin[::-1].encode("hex")
print nonce, extranonce2, ntime
print hash_bin.encode("hex")
hash_int = uint256_from_str(hash_bin)
block_hash_hex = "%064x" % hash_int
print block_hash_hex
self._cl.submit(job_id, extranonce2, ntime, nonce)
elif val < THRESH*10:
print "almost: %d (<%d)" % (val, THRESH)
i += 1
# elif i == 0:
# print hash_bin.encode("hex")
self._done_ev.set()
except:
traceback.print_exc()
os._exit(1)
开发者ID:kmod,项目名称:bitcoin_mining,代码行数:64,代码来源:scrypt_mining.py
示例7: submit_share
def submit_share(self, job_id, worker_name, extranonce1_bin, extranonce2, ntime, nonce,
difficulty, payment_type):
'''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, ntime, nonce - 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()
'''
# 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(ntime) != 8:
raise SubmitException("Incorrect size of ntime. Expected 8 chars")
if not job.check_ntime(int(ntime, 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, ntime, nonce):
logger.log('bad_client', worker_name, "Duplicate from %s, (%s %s %s %s)" % \
(worker_name, binascii.hexlify(extranonce1_bin), extranonce2, ntime, nonce))
raise SubmitException("Duplicate share")
# Now let's do the hard work!
# ---------------------------
# 0. Some sugar
extranonce2_bin = binascii.unhexlify(extranonce2)
ntime_bin = binascii.unhexlify(ntime)
nonce_bin = binascii.unhexlify(nonce)
# 1. Build coinbase
coinbase_bin = job.serialize_coinbase(extranonce1_bin, extranonce2_bin)
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_bin, nonce_bin)
# 4. Reverse header and compare it with target of the user
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)
block_hash_hex = "%064x" % hash_int
header_hex = binascii.hexlify(header_bin)
target_user = self.diff_to_target(difficulty)
#logger.log('share', worker_name, 'HASH %064x' % hash_int, 'TARGET %064x' % job.target, 'DIFF %d' % difficulty)
if hash_int > target_user:
logger.log('bad_client', 'Share is above target')
raise SubmitException("Share is above target")
if payment_type == 'PPS':
logger.log('share', worker_name, 'HASH %064x' % hash_int, 'TARGET %064x' % job.target, 'DIFF %d' % difficulty)
logger.log('pplns', worker_name, 'HASH %064x' % hash_int, 'DIFF %d' % difficulty, 'PAY 0')
if payment_type == 'PPLNS':
logger.log('pplns', worker_name, 'HASH %064x' % hash_int, 'DIFF %d' % difficulty, 'PAY 1')
# 5. Compare hash with target of the network
if hash_int <= job.target:
# Yay! It is block candidate!
logger.log('block', worker_name, block_hash_hex)
# 6. Finalize and serialize block object
job.finalize(merkle_root_int, extranonce1_bin, extranonce2_bin, int(ntime, 16), int(nonce, 16))
if not job.is_valid():
# Should not happen
logger.log('error', "Final job validation failed!")
# 7. Submit block to the network
serialized = binascii.hexlify(job.serialize())
on_submit = self.bitcoin_rpc.submitblock(serialized)
logger.log('submitblock', serialized)
return (True, worker_name, block_hash_hex)
return (False, worker_name, block_hash_hex)
开发者ID:sumory,项目名称:BTC,代码行数:96,代码来源:template_registry.py
示例8: submit_share
def submit_share(self, job_id, worker_name, session, extranonce1_bin, extranonce2, ntime, nonce,
difficulty, ip=False):
'''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, ntime, nonce - 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()
'''
# 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, worker_name, ip)
if job == None:
raise SubmitException("Job '%s' not found" % job_id)
# Check if ntime looks correct
if len(ntime) != 8:
raise SubmitException("Incorrect size of ntime. Expected 8 chars")
if not job.check_ntime(int(ntime, 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, ntime, nonce):
log.info("Duplicate from %s, (%s %s %s %s)" % \
(worker_name, binascii.hexlify(extranonce1_bin), extranonce2, ntime, nonce))
raise SubmitException("Duplicate share")
# Now let's do the hard work!
# ---------------------------
# 0. Some sugar
extranonce2_bin = binascii.unhexlify(extranonce2)
ntime_bin = binascii.unhexlify(ntime)
nonce_bin = binascii.unhexlify(nonce)
# 1. Build coinbase
coinbase_bin = job.serialize_coinbase(extranonce1_bin, extranonce2_bin)
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_bin, nonce_bin)
# 4. Reverse header and compare it with target of the user
hash_bin = algo.getPoWHash(''.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 coindef.header() = True:
header_hex = header_hex+"000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000"
开发者ID:Lexomatico,项目名称:stratum-mining,代码行数:66,代码来源:template_registry.py
示例9: submit_share
def submit_share(self, job_id, worker_name, session, extranonce1_bin, extranonce2, ntime, nonce,
difficulty, ip, submit_time):
'''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, ntime, nonce - 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()
'''
# 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(ntime) != 8:
raise SubmitException("Incorrect size of ntime. Expected 8 chars")
if not job.check_ntime(int(ntime, 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, ntime, nonce):
log.info("Duplicate from %s, (%s %s %s %s)" % \
(worker_name, binascii.hexlify(extranonce1_bin), extranonce2, ntime, nonce))
raise SubmitException("Duplicate share")
# Now let's do the hard work!
# ---------------------------
# 0. Some sugar
extranonce2_bin = binascii.unhexlify(extranonce2)
ntime_bin = binascii.unhexlify(ntime)
nonce_bin = binascii.unhexlify(nonce)
# 1. Build coinbase
coinbase_bin = job.serialize_coinbase(extranonce1_bin, extranonce2_bin)
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_bin, nonce_bin)
# 4. Reverse header and compare it with target of the user
if settings.DAEMON_ALGO == 'scrypt':
hash_bin = ltc_scrypt.getPoWHash(header_bin)
elif settings.DAEMON_ALGO == 'yescrypt':
hash_bin = yescrypt_hash.getPoWHash(header_bin)
elif settings.DAEMON_ALGO == 'qubit':
hash_bin = qubit_hash.getPoWHash(header_bin)
else:
hash_bin = util.doublesha(header_bin)
hash_int = util.uint256_from_str(hash_bin)
pow_hash_hex = "%064x" % hash_int
header_hex = binascii.hexlify(header_bin)
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(1000)
if hash_int <= target_info:
log.info("Yay, share with diff above 1000")
# Algebra tells us the diff_to_target is the same as hash_to_diff
share_diff = float(self.diff_to_target(hash_int))
on_submit = None
aux_submit = None
block_hash_bin = util.doublesha(header_bin)
block_hash_hex = block_hash_bin[::-1].encode('hex_codec')
if hash_int <= job.target:
log.info("MAINNET BLOCK CANDIDATE! %s diff(%f/%f)" % (block_hash_hex, share_diff, self.diff_to_target(job.target)))
job.finalize(merkle_root_int, extranonce1_bin, extranonce2_bin, int(ntime, 16), int(nonce, 16))
if not job.is_valid():
log.exception("FINAL JOB VALIDATION FAILED!")
serialized = binascii.hexlify(job.serialize())
if settings.SOLUTION_BLOCK_HASH:
on_submit = self.bitcoin_rpc.submitblock(serialized, block_hash_hex)
else:
#.........这里部分代码省略.........
开发者ID:McKenzy83,项目名称:merged-stratum,代码行数:101,代码来源:template_registry.py
示例10: run
def run(self):
difficulty = 1
while True:
s = self.f.readline()
if not s:
break
if TEST:
s = """{"params": ["bf", "4d16b6f85af6e2198f44ae2a6de67f78487ae5611b77c6c0440b921e00000000",
"01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff20020862062f503253482f04b8864e5008",
"072f736c7573682f000000000100f2052a010000001976a914d23fcdf86f7e756a64a7a9688ef9903327048ed988ac00000000", [],
"00000002", "1c2ac4af", "504e86b9", false], "id": null, "method": "mining.notify"}"""
extranonce1 = "08000002"
s = s.strip()
assert s
d = simplejson.loads(s)
# if d.get('method', None) == "mining.notify" and self.done:
# continue
print d
if d.get('error', None):
raise Exception()
if d['id'] == 1 and 'method' not in d:
subscription, extranonce1, extranonce2_size = d['result']
elif d.get('method', None) == "mining.set_difficulty":
difficulty = d['params'][0]
elif d.get('method', None) == "mining.notify":
print "stopping worker"
self.w.stop()
print "stopped"
params, clean_jobs = d['params'][:-1], d['params'][:-1]
j = JobInfo(extranonce1, *params)
self.jobs[j.id] = j
extranonce2 = ((int(time.time()) << 16) + os.getpid()) & 0xffffffff
extranonce2 = struct.pack(">I", extranonce2).encode("hex")
if TEST:
extranonce2 = "00000001"
print "extranonce2 = %s" % extranonce2
coinbase = j.coinb1 + extranonce1 + extranonce2 + j.coinb2
coinbase_hash_bin = doublesha(binascii.unhexlify(coinbase))
merkle_root = build_merkle_root(j.merkle_branch, coinbase_hash_bin)
merkle_root = ser_uint256_be(uint256_from_str(merkle_root))
# ntime = "504e86ed"
ntime = j.ntime
if TEST:
ntime = "504e86ed"
preheader = j.version + j.prevhash + merkle_root.encode("hex") + ntime + j.nbits
preheader_bin = preheader.decode("hex")
preheader_bin = ''.join([preheader_bin[i*4:i*4+4][::-1] for i in range(0,19)])
self.w.start(difficulty, j.id, extranonce2, ntime, preheader_bin)
else:
assert d['id'] < self.mid
开发者ID:kmod,项目名称:bitcoin_mining,代码行数:65,代码来源:miner.py
注:本文中的util.uint256_from_str函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论