Beispiel #1
0
    def run_remote_peer(peer, txs, in_db_count, delay):
        # this peer will immediately advertise the ten transactions
        # But when they are requested, it will only send one,
        # and "notfound" eight.
        yield from asyncio.sleep(delay)
        tx_db = dict((tx.hash(), tx) for tx in txs[:in_db_count])
        r = []

        inv_items = [InvItem(ITEM_TYPE_TX, tx.hash()) for tx in txs]
        peer.send_msg("inv", items=inv_items)

        next_message = peer.new_get_next_message_f()

        while True:
            t = yield from next_message()
            r.append(t)
            if t[0] == 'getdata':
                found = []
                not_found = []
                yield from asyncio.sleep(0.1)
                for inv_item in t[1]["items"]:
                    if inv_item.data in tx_db:
                        found.append(tx_db[inv_item.data])
                    else:
                        not_found.append(inv_item)
                if not_found:
                    if len(not_found) == 9:
                        not_found = not_found[:8]
                    peer.send_msg("notfound", items=not_found)
                for tx in found:
                    peer.send_msg("tx", tx=tx)
        return r
 def run_client(peer_list, tx_list):
     tx_store = {}
     inv_collector = InvCollector()
     tx_handler = TxHandler(inv_collector, tx_store)
     for tx in tx_list:
         tx_handler.add_tx(tx)
     for peer in peer_list:
         inv_collector.add_peer(peer)
         tx_handler.add_peer(peer)
     while len(tx_store) < 20:
         yield from asyncio.sleep(0.1)
     return tx_store
Beispiel #3
0
 def run_client(peer_list, tx_list):
     tx_store = {}
     inv_collector = InvCollector()
     tx_handler = TxHandler(inv_collector, tx_store)
     for tx in tx_list:
         tx_handler.add_tx(tx)
     for peer in peer_list:
         inv_collector.add_peer(peer)
         tx_handler.add_peer(peer)
     while len(tx_store) < 20:
         yield from asyncio.sleep(0.1)
     return tx_store
 def run_client(peer_list, block_list):
     block_chain = BlockChain()
     block_store = {}
     inv_collector = InvCollector()
     block_handler = BlockHandler(inv_collector, block_chain, block_store)
     for peer in peer_list:
         inv_collector.add_peer(peer)
         block_handler.add_peer(peer)
     for block in block_list:
         inv_collector.advertise_item(InvItem(ITEM_TYPE_BLOCK, block.hash()))
         block_store[block.hash()] = block
     while len(block_store) < 2:
         yield from asyncio.sleep(0.1)
     return block_store
Beispiel #5
0
 def run_client(peer_list, block_list):
     block_chain = BlockChain()
     block_store = {}
     inv_collector = InvCollector()
     block_handler = BlockHandler(inv_collector, block_chain, block_store)
     for peer in peer_list:
         inv_collector.add_peer(peer)
         block_handler.add_peer(peer)
     for block in block_list:
         inv_collector.advertise_item(InvItem(ITEM_TYPE_BLOCK,
                                              block.hash()))
         block_store[block.hash()] = block
     while len(block_store) < 2:
         yield from asyncio.sleep(0.1)
     return block_store
Beispiel #6
0
 def run_local_peer(peer_list):
     inv_collector = InvCollector()
     for peer in peer_list:
         inv_collector.add_peer(peer)
     r = []
     inv_item_q = inv_collector.new_inv_item_queue()
     while len(r) < 5:
         yield from asyncio.sleep(0.1)
         inv_item = yield from inv_item_q.get()
         try:
             v = yield from asyncio.wait_for(inv_collector.fetch(inv_item), timeout=0.5)
             if v:
                 r.append(v)
         except asyncio.TimeoutError:
             pass
     return r
Beispiel #7
0
    def run_local_peer(peer_list, r):
        inv_collector = InvCollector()
        for peer in peer_list:
            inv_collector.add_peer(peer)
        inv_item_q = inv_collector.new_inv_item_queue()
        
        @asyncio.coroutine
        def _do_fetch(inv_collector, inv_item, r):
            v = yield from inv_collector.fetch(inv_item, peer_timeout=3.0)
            if v:
                r.append(v)

        # keep a strong reference to these tasks
        tasks = []
        for i in range(10):
            inv_item = yield from inv_item_q.get()
            tasks.append(asyncio.Task(_do_fetch(inv_collector, inv_item, r)))
        while len(r) < 10:
            yield from asyncio.sleep(0.1)
        return r