Exemplo n.º 1
0
 def compute(request):
     state = current_work.value
     user = worker_interface.get_username(request)
     
     payout_script = get_payout_script_from_username(user)
     if payout_script is None or random.uniform(0, 100) < args.worker_fee:
         payout_script = my_script
     
     if len(p2p_node.peers) == 0 and net.PERSIST:
         raise jsonrpc.Error(-12345, u'p2pool is not connected to any peers')
     if state['best_share_hash'] is None and net.PERSIST:
         raise jsonrpc.Error(-12345, u'p2pool is downloading shares')
     if time.time() > current_work2.value['last_update'] + 60:
         raise jsonrpc.Error(-12345, u'lost contact with bitcoind')
     
     previous_share = None if state['best_share_hash'] is None else tracker.shares[state['best_share_hash']]
     subsidy = current_work2.value['subsidy']
     share_info, generate_tx = p2pool_data.generate_transaction(
         tracker=tracker,
         share_data=dict(
             previous_share_hash=state['best_share_hash'],
             coinbase='' if state['aux_work'] is None else '\xfa\xbemm' + bitcoin_data.HashType().pack(state['aux_work']['hash'])[::-1] + struct.pack('<ii', 1, 0),
             nonce=run_identifier + struct.pack('<Q', random.randrange(2**64)),
             new_script=payout_script,
             subsidy=subsidy,
             donation=math.perfect_round(65535*args.donation_percentage/100),
             stale_frac=(lambda shares, stales:
                 255 if shares == 0 else math.perfect_round(254*stales/shares)
             )(*get_share_counts()),
         ),
         block_target=state['target'],
         desired_timestamp=int(time.time() - current_work2.value['clock_offset']),
         net=net,
     )
     
     print 'New work for worker %s! Difficulty: %.06f Payout if block: %.6f %s Total block value: %.6f %s including %i transactions' % (
         user,
         bitcoin_data.target_to_difficulty(share_info['target']),
         (sum(t['value'] for t in generate_tx['tx_outs'] if t['script'] == payout_script) - subsidy//200)*1e-8, net.BITCOIN_SYMBOL,
         subsidy*1e-8, net.BITCOIN_SYMBOL,
         len(current_work2.value['transactions']),
     )
     
     transactions = [generate_tx] + list(current_work2.value['transactions'])
     merkle_root = bitcoin_data.merkle_hash(transactions)
     merkle_root_to_transactions[merkle_root] = share_info, transactions, time.time()
     
     return bitcoin_getwork.BlockAttempt(state['version'], state['previous_block'], merkle_root, current_work2.value['time'], state['target'], share_info['target']), state['best_share_hash']
Exemplo n.º 2
0
        def got_response(header, request):
            try:
                user = worker_interface.get_username(request)
                # match up with transactions
                xxx = merkle_root_to_transactions.get(header['merkle_root'],
                                                      None)
                if xxx is None:
                    print '''Couldn't link returned work's merkle root with its transactions - should only happen if you recently restarted p2pool'''
                    return False
                share_info, transactions, getwork_time = xxx

                hash_ = bitcoin_data.block_header_type.hash256(header)

                pow_hash = net.BITCOIN_POW_FUNC(header)

                if pow_hash <= header['target'] or p2pool.DEBUG:
                    if factory.conn.value is not None:
                        factory.conn.value.send_block(
                            block=dict(header=header, txs=transactions))
                    else:
                        print 'No bitcoind connection! Erp!'
                    if pow_hash <= header['target']:
                        print
                        print 'GOT BLOCK! Passing to bitcoind! bitcoin: %x' % (
                            hash_, )
                        print

                if current_work.value[
                        'aux_work'] is not None and pow_hash <= current_work.value[
                            'aux_work']['target']:
                    try:
                        aux_pow = dict(
                            merkle_tx=dict(
                                tx=transactions[0],
                                block_hash=hash_,
                                merkle_branch=[
                                    x['hash'] for x in p2pool_data.
                                    calculate_merkle_branch(transactions, 0)
                                ],
                                index=0,
                            ),
                            merkle_branch=[],
                            index=0,
                            parent_block_header=header,
                        )

                        a, b = transactions[0]['tx_ins'][0]['script'][
                            -32 - 8:-8].encode(
                                'hex'), bitcoin_data.aux_pow_type.pack(
                                    aux_pow).encode('hex')
                        #print a, b
                        merged = jsonrpc.Proxy(args.merged_url,
                                               (args.merged_userpass, ))

                        def _(res):
                            print "MERGED RESULT:", res

                        merged.rpc_getauxblock(a, b).addBoth(_)
                    except:
                        log.err(None,
                                'Error while processing merged mining POW:')

                target = share_info['target']
                if pow_hash > target:
                    print 'Worker submitted share with hash > target:\nhash  : %x\ntarget: %x' % (
                        pow_hash, target)
                    return False
                share = p2pool_data.Share(net,
                                          header,
                                          share_info,
                                          other_txs=transactions[1:])
                my_shares.add(share.hash)
                if share.previous_hash != current_work.value['best_share_hash']:
                    doa_shares.add(share.hash)
                print 'GOT SHARE! %s %s prev %s age %.2fs' % (
                    user, p2pool_data.format_hash(share.hash),
                    p2pool_data.format_hash(share.previous_hash), time.time() -
                    getwork_time) + (' DEAD ON ARRIVAL' if share.previous_hash
                                     != current_work.value['best_share_hash']
                                     else '')
                good = share.previous_hash == current_work.value[
                    'best_share_hash']
                # maybe revert back to tracker being non-blocking so 'good' can be more accurate?
                p2p_shares([share])
                # eg. good = share.hash == current_work.value['best_share_hash'] here
                return good
            except:
                log.err(None, 'Error processing data received from worker:')
                return False
Exemplo n.º 3
0
        def compute(request):
            state = current_work.value
            user = worker_interface.get_username(request)

            payout_script = get_payout_script_from_username(user)
            if payout_script is None or random.uniform(0,
                                                       100) < args.worker_fee:
                payout_script = my_script

            if len(p2p_node.peers) == 0 and net.PERSIST:
                raise jsonrpc.Error(-12345,
                                    u'p2pool is not connected to any peers')
            if state['best_share_hash'] is None and net.PERSIST:
                raise jsonrpc.Error(-12345, u'p2pool is downloading shares')
            if time.time() > current_work2.value['last_update'] + 60:
                raise jsonrpc.Error(-12345, u'lost contact with bitcoind')

            previous_share = None if state[
                'best_share_hash'] is None else tracker.shares[
                    state['best_share_hash']]
            subsidy = current_work2.value['subsidy']
            share_info, generate_tx = p2pool_data.generate_transaction(
                tracker=tracker,
                share_data=dict(
                    previous_share_hash=state['best_share_hash'],
                    coinbase='' if state['aux_work'] is None else
                    '\xfa\xbemm' + bitcoin_data.HashType().pack(
                        state['aux_work']['hash'])[::-1] +
                    struct.pack('<ii', 1, 0),
                    nonce=run_identifier +
                    struct.pack('<Q', random.randrange(2**64)),
                    new_script=payout_script,
                    subsidy=subsidy,
                    donation=math.perfect_round(
                        65535 * args.donation_percentage / 100),
                    stale_frac=(lambda shares, stales: 255 if shares == 0 else
                                math.perfect_round(254 * stales / shares))(
                                    *get_share_counts()),
                ),
                block_target=state['target'],
                desired_timestamp=int(time.time() -
                                      current_work2.value['clock_offset']),
                net=net,
            )

            print 'New work for worker %s! Difficulty: %.06f Payout if block: %.6f %s Total block value: %.6f %s including %i transactions' % (
                user,
                bitcoin_data.target_to_difficulty(share_info['target']),
                (sum(t['value'] for t in generate_tx['tx_outs']
                     if t['script'] == payout_script) - subsidy // 200) * 1e-8,
                net.BITCOIN_SYMBOL,
                subsidy * 1e-8,
                net.BITCOIN_SYMBOL,
                len(current_work2.value['transactions']),
            )

            transactions = [generate_tx] + list(
                current_work2.value['transactions'])
            merkle_root = bitcoin_data.merkle_hash(transactions)
            merkle_root_to_transactions[
                merkle_root] = share_info, transactions, time.time()

            return bitcoin_getwork.BlockAttempt(
                state['version'], state['previous_block'], merkle_root,
                current_work2.value['time'], state['target'],
                share_info['target']), state['best_share_hash']
Exemplo n.º 4
0
 def got_response(header, request):
     try:
         user = worker_interface.get_username(request)
         # match up with transactions
         xxx = merkle_root_to_transactions.get(header['merkle_root'], None)
         if xxx is None:
             print '''Couldn't link returned work's merkle root with its transactions - should only happen if you recently restarted p2pool'''
             return False
         share_info, transactions, getwork_time = xxx
         
         hash_ = bitcoin_data.block_header_type.hash256(header)
         
         pow_hash = net.BITCOIN_POW_FUNC(header)
         
         if pow_hash <= header['target'] or p2pool.DEBUG:
             if factory.conn.value is not None:
                 factory.conn.value.send_block(block=dict(header=header, txs=transactions))
             else:
                 print 'No bitcoind connection! Erp!'
             if pow_hash <= header['target']:
                 print
                 print 'GOT BLOCK! Passing to bitcoind! bitcoin: %x' % (hash_,)
                 print
         
         if current_work.value['aux_work'] is not None and pow_hash <= current_work.value['aux_work']['target']:
             try:
                 aux_pow = dict(
                     merkle_tx=dict(
                         tx=transactions[0],
                         block_hash=hash_,
                         merkle_branch=[x['hash'] for x in p2pool_data.calculate_merkle_branch(transactions, 0)],
                         index=0,
                     ),
                     merkle_branch=[],
                     index=0,
                     parent_block_header=header,
                 )
                 
                 a, b = transactions[0]['tx_ins'][0]['script'][-32-8:-8].encode('hex'), bitcoin_data.aux_pow_type.pack(aux_pow).encode('hex')
                 #print a, b
                 merged = jsonrpc.Proxy(args.merged_url, (args.merged_userpass,))
                 def _(res):
                     print "MERGED RESULT:", res
                 merged.rpc_getauxblock(a, b).addBoth(_)
             except:
                 log.err(None, 'Error while processing merged mining POW:')
         
         target = share_info['target']
         if pow_hash > target:
             print 'Worker submitted share with hash > target:\nhash  : %x\ntarget: %x' % (pow_hash, target)
             return False
         share = p2pool_data.Share(net, header, share_info, other_txs=transactions[1:])
         my_shares.add(share.hash)
         if share.previous_hash != current_work.value['best_share_hash']:
             doa_shares.add(share.hash)
         print 'GOT SHARE! %s %s prev %s age %.2fs' % (user, p2pool_data.format_hash(share.hash), p2pool_data.format_hash(share.previous_hash), time.time() - getwork_time) + (' DEAD ON ARRIVAL' if share.previous_hash != current_work.value['best_share_hash'] else '')
         good = share.previous_hash == current_work.value['best_share_hash']
         # maybe revert back to tracker being non-blocking so 'good' can be more accurate?
         p2p_shares([share])
         # eg. good = share.hash == current_work.value['best_share_hash'] here
         return good
     except:
         log.err(None, 'Error processing data received from worker:')
         return False