示例#1
0
        def compute_work():
            t = self.node.bitcoind_work.value
            bb = self.node.best_block_header.value
            if bb is not None and bb['previous_block'] == t[
                    'previous_block'] and bitcoin_data.scrypt(
                        bitcoin_data.block_header_type.pack(
                            bb)) <= t['bits'].target:
                print 'Skipping from block %x to block %x!' % (
                    bb['previous_block'],
                    bitcoin_data.hash256(
                        bitcoin_data.block_header_type.pack(bb)))
                t = dict(
                    version=bb['version'],
                    previous_block=bitcoin_data.scrypt(
                        bitcoin_data.block_header_type.pack(bb)),
                    bits=self.node.pow_bits,  # not always true
                    coinbaseflags='',
                    height=t['height'] + 1,
                    time=t['time'] + 30,  # better way?
                    transactions=[],
                    transaction_fees=[],
                    txn_timestamp=0,
                    merkle_link=bitcoin_data.calculate_merkle_link([None], 0),
                    subsidy=self.node.pow_subsidy,
                    last_update=self.node.bitcoind_work.value['last_update'],
                )

            self.current_work.set(t)
示例#2
0
 def compute_work():
     t = self.node.bitcoind_work.value
     bb = self.node.best_block_header.value
     if bb is not None and bb['previous_block'] == t['previous_block'] and bitcoin_data.scrypt(bitcoin_data.block_header_type.pack(bb)) <= t['bits'].target:
         print 'Skipping from block %x to block %x!' % (bb['previous_block'],
             bitcoin_data.hash256(bitcoin_data.block_header_type.pack(bb)))
         t = dict(
             version=bb['version'],
             previous_block=bitcoin_data.scrypt(bitcoin_data.block_header_type.pack(bb)),
             bits=self.node.pow_bits, # not always true
             coinbaseflags='',
             height=t['height'] + 1,
             time=t['time'] + 30, # better way?
             transactions=[],
             transaction_fees=[],
             txn_timestamp=0,
             merkle_link=bitcoin_data.calculate_merkle_link([None], 0),
             subsidy=self.node.pow_subsidy,
             last_update=self.node.bitcoind_work.value['last_update'],
         )
     
     self.current_work.set(t)
示例#3
0
 def got_response(header, user, coinbase_nonce):
     assert len(coinbase_nonce) == self.COINBASE_NONCE_LENGTH
     new_packed_gentx = packed_gentx[:-self.COINBASE_NONCE_LENGTH-4] + coinbase_nonce + packed_gentx[-4:] if coinbase_nonce != '\0'*self.COINBASE_NONCE_LENGTH else packed_gentx
     new_gentx = bitcoin_data.tx_type.unpack(new_packed_gentx) if coinbase_nonce != '\0'*self.COINBASE_NONCE_LENGTH else gentx
     
     header_hash = pow_hash = bitcoin_data.scrypt(bitcoin_data.block_header_type.pack(header))
     try:
         if pow_hash <= header['bits'].target or p2pool.DEBUG:
             helper.submit_block(dict(header=header, txs=[new_gentx] + other_transactions, signature=''), False, self.node.factory, self.node.bitcoind, self.node.bitcoind_work, self.node.net)
             if pow_hash <= header['bits'].target:
                 print
                 print 'GOT BLOCK FROM MINER! Passing to bitcoind! %s%064x' % (self.node.net.PARENT.BLOCK_EXPLORER_URL_PREFIX, header_hash)
                 print
     except:
         log.err(None, 'Error while processing potential block:')
     
     user, _, _, _ = self.get_user_details(user)
     assert header['previous_block'] == ba['previous_block']
     assert header['merkle_root'] == bitcoin_data.check_merkle_link(bitcoin_data.hash256(new_packed_gentx), merkle_link)
     assert header['bits'] == ba['bits']
     
     on_time = self.new_work_event.times == lp_count
     
     for aux_work, index, hashes in mm_later:
         try:
             if pow_hash <= aux_work['target'] or p2pool.DEBUG:
                 df = deferral.retry('Error submitting merged block: (will retry)', 10, 10)(aux_work['merged_proxy'].rpc_getauxblock)(
                     pack.IntType(256, 'big').pack(aux_work['hash']).encode('hex'),
                     bitcoin_data.aux_pow_type.pack(dict(
                         merkle_tx=dict(
                             tx=new_gentx,
                             block_hash=header_hash,
                             merkle_link=merkle_link,
                         ),
                         merkle_link=bitcoin_data.calculate_merkle_link(hashes, index),
                         parent_block_header=header,
                     )).encode('hex'),
                 )
                 @df.addCallback
                 def _(result, aux_work=aux_work):
                     if result != (pow_hash <= aux_work['target']):
                         print >>sys.stderr, 'Merged block submittal result: %s Expected: %s' % (result, pow_hash <= aux_work['target'])
                     else:
                         print 'Merged block submittal result: %s' % (result,)
                 @df.addErrback
                 def _(err):
                     log.err(err, 'Error submitting merged block:')
         except:
             log.err(None, 'Error while processing merged mining POW:')
     
     if pow_hash <= share_info['bits'].target and header_hash not in received_header_hashes:
         last_txout_nonce = pack.IntType(8*self.COINBASE_NONCE_LENGTH).unpack(coinbase_nonce)
         share = get_share(header, last_txout_nonce)
         
         print 'GOT SHARE! %s %s prev %s age %.2fs%s' % (
             user,
             p2pool_data.format_hash(share.hash),
             p2pool_data.format_hash(share.previous_hash),
             time.time() - getwork_time,
             ' DEAD ON ARRIVAL' if not on_time else '',
         )
         self.my_share_hashes.add(share.hash)
         if not on_time:
             self.my_doa_share_hashes.add(share.hash)
         
         self.node.tracker.add(share)
         self.node.set_best_share()
         
         try:
             if (pow_hash <= header['bits'].target or p2pool.DEBUG) and self.node.p2p_node is not None:
                 self.node.p2p_node.broadcast_share(share.hash)
         except:
             log.err(None, 'Error forwarding block solution:')
         
         self.share_received.happened(bitcoin_data.target_to_average_attempts(share.target), not on_time, share.hash)
     
     if pow_hash > target:
         print 'Worker %s submitted share with hash > target:' % (user,)
         print '    Hash:   %56x' % (pow_hash,)
         print '    Target: %56x' % (target,)
     elif header_hash in received_header_hashes:
         print >>sys.stderr, 'Worker %s submitted share more than once!' % (user,)
     else:
         received_header_hashes.add(header_hash)
         
         self.pseudoshare_received.happened(bitcoin_data.target_to_average_attempts(target), not on_time, user)
         self.recent_shares_ts_work.append((time.time(), bitcoin_data.target_to_average_attempts(target)))
         while len(self.recent_shares_ts_work) > 50:
             self.recent_shares_ts_work.pop(0)
         self.local_rate_monitor.add_datum(dict(work=bitcoin_data.target_to_average_attempts(target), dead=not on_time, user=user, share_target=share_info['bits'].target))
         self.local_addr_rate_monitor.add_datum(dict(work=bitcoin_data.target_to_average_attempts(target), pubkey_hash=pubkey_hash))
     
     return on_time
示例#4
0
 def got_response(header, user, coinbase_nonce):
     assert len(coinbase_nonce) == self.COINBASE_NONCE_LENGTH
     new_packed_gentx = packed_gentx[:-self.COINBASE_NONCE_LENGTH-4] + coinbase_nonce + packed_gentx[-4:] if coinbase_nonce != '\0'*self.COINBASE_NONCE_LENGTH else packed_gentx
     new_gentx = bitcoin_data.tx_type.unpack(new_packed_gentx) if coinbase_nonce != '\0'*self.COINBASE_NONCE_LENGTH else gentx
     
     header_hash = pow_hash = bitcoin_data.scrypt(bitcoin_data.block_header_type.pack(header))
     try:
         if pow_hash <= header['bits'].target or p2pool.DEBUG:
             helper.submit_block(dict(header=header, txs=[new_gentx] + other_transactions, signature=''), False, self.node.factory, self.node.bitcoind, self.node.bitcoind_work, self.node.net)
             if pow_hash <= header['bits'].target:
                 print
                 print 'GOT BLOCK FROM MINER! Passing to bitcoind! %s%064x' % (self.node.net.PARENT.BLOCK_EXPLORER_URL_PREFIX, header_hash)
                 print
     except:
         log.err(None, 'Error while processing potential block:')
     
     user, _, _, _ = self.get_user_details(user)
     assert header['previous_block'] == ba['previous_block']
     assert header['merkle_root'] == bitcoin_data.check_merkle_link(bitcoin_data.hash256(new_packed_gentx), merkle_link)
     assert header['bits'] == ba['bits']
     
     on_time = self.new_work_event.times == lp_count
     
     for aux_work, index, hashes in mm_later:
         try:
             if pow_hash <= aux_work['target'] or p2pool.DEBUG:
                 df = deferral.retry('Error submitting merged block: (will retry)', 10, 10)(aux_work['merged_proxy'].rpc_getauxblock)(
                     pack.IntType(256, 'big').pack(aux_work['hash']).encode('hex'),
                     bitcoin_data.aux_pow_type.pack(dict(
                         merkle_tx=dict(
                             tx=new_gentx,
                             block_hash=header_hash,
                             merkle_link=merkle_link,
                         ),
                         merkle_link=bitcoin_data.calculate_merkle_link(hashes, index),
                         parent_block_header=header,
                     )).encode('hex'),
                 )
                 @df.addCallback
                 def _(result, aux_work=aux_work):
                     if result != (pow_hash <= aux_work['target']):
                         print >>sys.stderr, 'Merged block submittal result: %s Expected: %s' % (result, pow_hash <= aux_work['target'])
                     else:
                         print 'Merged block submittal result: %s' % (result,)
                 @df.addErrback
                 def _(err):
                     log.err(err, 'Error submitting merged block:')
         except:
             log.err(None, 'Error while processing merged mining POW:')
     
     if pow_hash <= share_info['bits'].target and header_hash not in received_header_hashes:
         last_txout_nonce = pack.IntType(8*self.COINBASE_NONCE_LENGTH).unpack(coinbase_nonce)
         share = get_share(header, last_txout_nonce)
         
         print 'GOT SHARE! %s %s prev %s age %.2fs%s' % (
             user,
             p2pool_data.format_hash(share.hash),
             p2pool_data.format_hash(share.previous_hash),
             time.time() - getwork_time,
             ' DEAD ON ARRIVAL' if not on_time else '',
         )
         self.my_share_hashes.add(share.hash)
         if not on_time:
             self.my_doa_share_hashes.add(share.hash)
         
         self.node.tracker.add(share)
         self.node.set_best_share()
         
         try:
             if (pow_hash <= header['bits'].target or p2pool.DEBUG) and self.node.p2p_node is not None:
                 self.node.p2p_node.broadcast_share(share.hash)
         except:
             log.err(None, 'Error forwarding block solution:')
         
         self.share_received.happened(bitcoin_data.target_to_average_attempts(share.target), not on_time, share.hash)
     
     if pow_hash > target:
         print 'Worker %s submitted share with hash > target:' % (user,)
         print '    Hash:   %56x' % (pow_hash,)
         print '    Target: %56x' % (target,)
     elif header_hash in received_header_hashes:
         print >>sys.stderr, 'Worker %s submitted share more than once!' % (user,)
     else:
         received_header_hashes.add(header_hash)
         
         self.pseudoshare_received.happened(bitcoin_data.target_to_average_attempts(target), not on_time, user)
         self.recent_shares_ts_work.append((time.time(), bitcoin_data.target_to_average_attempts(target)))
         while len(self.recent_shares_ts_work) > 50:
             self.recent_shares_ts_work.pop(0)
         self.local_rate_monitor.add_datum(dict(work=bitcoin_data.target_to_average_attempts(target), dead=not on_time, user=user, share_target=share_info['bits'].target))
         self.local_addr_rate_monitor.add_datum(dict(work=bitcoin_data.target_to_average_attempts(target), pubkey_hash=pubkey_hash))
     
     return on_time