コード例 #1
0
ファイル: main.py プロジェクト: finway-china/p2pool
 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']
コード例 #2
0
ファイル: main.py プロジェクト: gyver/p2pool
 def compute(state, payout_script):
     if payout_script is None:
         payout_script = my_script
     if state['best_share_hash'] is None and args.net.PERSIST:
         raise jsonrpc.Error(-12345, u'p2pool is downloading shares')
     pre_extra_txs = [tx for tx in tx_pool.itervalues() if tx.is_good()]
     pre_extra_txs = pre_extra_txs[:2**16 - 1] # merkle_branch limit
     extra_txs = []
     size = 0
     for tx in pre_extra_txs:
         this_size = len(bitcoin.data.tx_type.pack(tx.tx))
         if size + this_size > 500000:
             break
         extra_txs.append(tx)
         size += this_size
     # XXX check sigops!
     # XXX assuming generate_tx is smallish here..
     generate_tx = p2pool.generate_transaction(
         tracker=tracker,
         previous_share_hash=state['best_share_hash'],
         new_script=payout_script,
         subsidy=(50*100000000 >> (state['height'] + 1)//210000) + sum(tx.value_in - tx.value_out for tx in extra_txs),
         nonce=run_identifier + struct.pack('<Q', random.randrange(2**64)),
         block_target=state['target'],
         net=args.net,
     )
     print 'Generating! Difficulty: %.06f Payout if block: %.6f BTC' % (0xffff*2**208/p2pool.coinbase_type.unpack(generate_tx['tx_ins'][0]['script'])['share_data']['target'], generate_tx['tx_outs'][-1]['value']*1e-8)
     #print 'Target: %x' % (p2pool.coinbase_type.unpack(generate_tx['tx_ins'][0]['script'])['share_data']['target'],)
     #, have', shares.count(my_script) - 2, 'share(s) in the current chain. Fee:', sum(tx.value_in - tx.value_out for tx in extra_txs)/100000000
     transactions = [generate_tx] + [tx.tx for tx in extra_txs]
     merkle_root = bitcoin.data.merkle_hash(transactions)
     merkle_root_to_transactions[merkle_root] = transactions # will stay for 1000 seconds
     
     timestamp = int(time.time() - current_work2.value['clock_offset'])
     if state['best_share_hash'] is not None:
         timestamp2 = math.median((s.timestamp for s in itertools.islice(tracker.get_chain_to_root(state['best_share_hash']), 11)), use_float=False) + 1
         if timestamp2 > timestamp:
             print 'Toff', timestamp2 - timestamp
             timestamp = timestamp2
     target2 = p2pool.coinbase_type.unpack(generate_tx['tx_ins'][0]['script'])['share_data']['target']
     times[p2pool.coinbase_type.unpack(generate_tx['tx_ins'][0]['script'])['share_data']['nonce']] = time.time()
     #print 'SENT', 2**256//p2pool.coinbase_type.unpack(generate_tx['tx_ins'][0]['script'])['share_data']['target']
     return bitcoin.getwork.BlockAttempt(state['version'], state['previous_block'], merkle_root, timestamp, state['target'], target2)
コード例 #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']