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
Esempio n. 2
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
def items_for_client(initial_blocks=[]):
    block_store = {}
    block_chain = BlockChain()
    blockfetcher = Blockfetcher()
    inv_collector = InvCollector()
    block_handler = BlockHandler(inv_collector, block_chain, block_store)
    fast_forward_add_peer = fast_forwarder_add_peer_f(block_chain)

    for block in initial_blocks:
        inv_collector.advertise_item(InvItem(ITEM_TYPE_BLOCK, block.hash()))
        block_store[block.hash()] = block
    block_chain.add_headers(initial_blocks)

    inv_q = inv_collector.new_inv_item_queue()
    ap = make_add_peer(fast_forward_add_peer, blockfetcher, block_handler, inv_collector, block_chain, block_store)
    ap.block_getter_task = asyncio.Task(block_getter(inv_q, inv_collector, block_handler, block_chain, block_store))
    return block_handler, block_chain, block_store, ap
Esempio n. 4
0
def items_for_client(initial_blocks=[]):
    block_store = {}
    block_chain = BlockChain()
    blockfetcher = Blockfetcher()
    inv_collector = InvCollector()
    block_handler = BlockHandler(inv_collector, block_chain, block_store)
    fast_forward_add_peer = fast_forwarder_add_peer_f(block_chain)

    for block in initial_blocks:
        inv_collector.advertise_item(InvItem(ITEM_TYPE_BLOCK, block.hash()))
        block_store[block.hash()] = block
    block_chain.add_headers(initial_blocks)

    inv_q = inv_collector.new_inv_item_queue()
    ap = make_add_peer(fast_forward_add_peer, blockfetcher, block_handler,
                       inv_collector, block_chain, block_store)
    ap.block_getter_task = asyncio.Task(
        block_getter(inv_q, inv_collector, block_handler, block_chain,
                     block_store))
    return block_handler, block_chain, block_store, ap
Esempio n. 5
0
def test_simple_getheader():
    BLOCKS = make_blocks(20)
    blockchain1 = BlockChain()
    blockchain1.add_headers(BLOCKS)

    block_store = dict((b.hash(), b) for b in BLOCKS)

    peer1, peer2 = create_handshaked_peers()

    block_store = {}
    block_chain = BlockChain()
    inv_collector = InvCollector()
    block_handler = BlockHandler(inv_collector, block_chain, block_store)

    for block in BLOCKS:
        inv_collector.advertise_item(InvItem(ITEM_TYPE_BLOCK, block.hash()))
        block_store[block.hash()] = block
    block_chain.add_headers(BLOCKS)

    inv_collector.add_peer(peer1)
    block_handler.add_peer(peer1)

    @asyncio.coroutine
    def run_peer2():
        r = []
        headers = yield from standards.get_headers_hashes(
            peer2, after_block_hash=b'\0' * 32)
        r.append(headers)
        return r

    f2 = asyncio.Task(run_peer2())

    asyncio.get_event_loop().run_until_complete(asyncio.wait([f2]))

    r = f2.result()
    assert len(r) == 1
    assert [b.hash() for b in r[0]] == [b.hash() for b in BLOCKS]
def test_simple_getheader():
    BLOCKS = make_blocks(20)
    blockchain1 = BlockChain()
    blockchain1.add_headers(BLOCKS)

    block_store = dict((b.hash(), b) for b in BLOCKS)

    peer1, peer2 = create_handshaked_peers()

    block_store = {}
    block_chain = BlockChain()
    inv_collector = InvCollector()
    block_handler = BlockHandler(inv_collector, block_chain, block_store)

    for block in BLOCKS:
        inv_collector.advertise_item(InvItem(ITEM_TYPE_BLOCK, block.hash()))
        block_store[block.hash()] = block
    block_chain.add_headers(BLOCKS)

    inv_collector.add_peer(peer1)
    block_handler.add_peer(peer1)

    @asyncio.coroutine
    def run_peer2():
        r = []
        headers = yield from standards.get_headers_hashes(peer2, after_block_hash=b"\0" * 32)
        r.append(headers)
        return r

    f2 = asyncio.Task(run_peer2())

    asyncio.get_event_loop().run_until_complete(asyncio.wait([f2]))

    r = f2.result()
    assert len(r) == 1
    assert [b.hash() for b in r[0]] == [b.hash() for b in BLOCKS]