Beispiel #1
0
def test_get_date_address_tuples():
    peer1, peer2 = create_peers()

    DA_TUPLES = [(1392770000 + i, PeerAddress(1, "127.0.0.%d" % i, 8000 + i))
                 for i in range(100)]

    from pycoinnet.message import pack_from_data
    msg_data = pack_from_data("addr", date_address_tuples=DA_TUPLES)

    @asyncio.coroutine
    def run_peer1():
        yield from standards.initial_handshake(peer1, VERSION_MSG)
        next_message = peer1.new_get_next_message_f()
        name, data = yield from next_message()
        peer1.send_msg("addr", date_address_tuples=DA_TUPLES)
        return name, data

    @asyncio.coroutine
    def run_peer2():
        yield from standards.initial_handshake(peer2, VERSION_MSG_2)
        date_address_tuples = yield from standards.get_date_address_tuples(
            peer2)
        return DA_TUPLES

    f1 = asyncio.Task(run_peer1())
    f2 = asyncio.Task(run_peer2())

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

    name, data = f1.result()
    assert name == 'getaddr'
    assert data == {}

    date_address_tuples = f2.result()
    assert date_address_tuples == DA_TUPLES
Beispiel #2
0
def test_get_date_address_tuples():
    peer1, peer2 = create_peers()

    DA_TUPLES = [(1392770000+i, PeerAddress(1, "127.0.0.%d" % i, 8000+i)) for i in range(100)]

    from pycoinnet.message import pack_from_data
    msg_data = pack_from_data("addr", date_address_tuples=DA_TUPLES)

    @asyncio.coroutine
    def run_peer1():
        yield from standards.initial_handshake(peer1, VERSION_MSG)
        next_message = peer1.new_get_next_message_f()
        name, data = yield from next_message()
        peer1.send_msg("addr", date_address_tuples=DA_TUPLES)
        return name, data

    @asyncio.coroutine
    def run_peer2():
        yield from standards.initial_handshake(peer2, VERSION_MSG_2)
        date_address_tuples = yield from standards.get_date_address_tuples(peer2)
        return DA_TUPLES

    f1 = asyncio.Task(run_peer1())
    f2 = asyncio.Task(run_peer2())

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

    name, data = f1.result()
    assert name == 'getaddr'
    assert data == {}

    date_address_tuples = f2.result()
    assert date_address_tuples == DA_TUPLES
Beispiel #3
0
 def send_msg(self, message_name, **kwargs):
     message_data = pack_from_data(message_name, **kwargs)
     message_type = message_name.encode("utf8")
     message_type_padded = (message_type+(b'\0'*12))[:12]
     message_size = struct.pack("<L", len(message_data))
     message_checksum = encoding.double_sha256(message_data)[:4]
     packet = b"".join([
         self.magic_header, message_type_padded, message_size, message_checksum, message_data
     ])
     logging.debug("sending message %s [%d bytes] to %s", message_type.decode("utf8"), len(packet), self)
     self.bytes_writ += len(packet)
     self.transport.write(packet)
 def send_msg(self, message_name, **kwargs):
     message_data = pack_from_data(message_name, **kwargs)
     message_type = message_name.encode("utf8")
     message_type_padded = (message_type+(b'\0'*12))[:12]
     message_size = struct.pack("<L", len(message_data))
     message_checksum = encoding.double_sha256(message_data)[:4]
     packet = b"".join([
         self.magic_header, message_type_padded, message_size, message_checksum, message_data
     ])
     logging.debug("sending message %s [%d bytes] to %s", message_type.decode("utf8"), len(packet), self)
     self.bytes_writ += len(packet)
     self.transport.write(packet)
Beispiel #5
0
def test_fetcher():
    peer1, peer2 = create_peers()

    TX_LIST = [make_tx(i) for i in range(100)]

    from pycoinnet.message import pack_from_data
    msg_data = pack_from_data("tx", tx=TX_LIST[0])

    item1 = [InvItem(1, TX_LIST[0].hash())]
    item2 = [InvItem(1, TX_LIST[i].hash()) for i in range(2)]
    msg_data = pack_from_data("getdata", items=item1)
    msg_data = pack_from_data("getdata", items=item2)
    msg_data = pack_from_data("notfound", items=item1)
    msg_data = pack_from_data("notfound", items=item2)

    @asyncio.coroutine
    def run_peer1():
        r = []
        
        yield from standards.initial_handshake(peer1, VERSION_MSG)
        next_message = peer1.new_get_next_message_f()

        t = yield from next_message()
        r.append(t)
        peer1.send_msg("tx", tx=TX_LIST[0])

        t = yield from next_message()
        r.append(t)
        peer1.send_msg("notfound", items=[InvItem(ITEM_TYPE_TX, TX_LIST[1].hash())])

        t = yield from next_message()
        r.append(t)
        peer1.send_msg("tx", tx=TX_LIST[2])

        t = yield from next_message()
        r.append(t)
        items = [InvItem(ITEM_TYPE_TX, TX_LIST[3].hash())]
        items.append([InvItem(ITEM_TYPE_TX, TX_LIST[5].hash())])
        peer1.send_msg("notfound", items=items)
        peer1.send_msg("tx", tx=TX_LIST[4])

        return r

    @asyncio.coroutine
    def run_peer2():
        r = []
        yield from standards.initial_handshake(peer2, VERSION_MSG_2)
        tx_fetcher = Fetcher(peer2)

        tx = yield from tx_fetcher.fetch(mi(TX_LIST[0].hash()), timeout=5)
        r.append(tx)

        tx = yield from tx_fetcher.fetch(mi(TX_LIST[1].hash()))
        r.append(tx)

        tx = yield from tx_fetcher.fetch(mi(TX_LIST[2].hash()))
        r.append(tx)

        f1 = asyncio.Task(tx_fetcher.fetch(mi(TX_LIST[3].hash())))
        f2 = asyncio.Task(tx_fetcher.fetch(mi(TX_LIST[4].hash())))
        f3 = asyncio.Task(tx_fetcher.fetch(mi(TX_LIST[5].hash())))
        yield from asyncio.wait([f1, f2, f3])

        r.append(f1.result())
        r.append(f2.result())
        r.append(f3.result())

        return r


    f1 = asyncio.Task(run_peer1())
    f2 = asyncio.Task(run_peer2())

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

    r = f1.result()
    assert len(r) == 4
    assert r[0] == ('getdata', dict(items=(InvItem(ITEM_TYPE_TX, TX_LIST[0].hash()),)))
    assert r[1] == ('getdata', dict(items=(InvItem(ITEM_TYPE_TX, TX_LIST[1].hash()),)))
    assert r[2] == ('getdata', dict(items=(InvItem(ITEM_TYPE_TX, TX_LIST[2].hash()),)))
    assert r[3] == ('getdata', dict(items=tuple(InvItem(ITEM_TYPE_TX, TX_LIST[i].hash()) for i in range(3,6))))

    r = f2.result()
    assert len(r) == 6
    assert r[0].hash() == TX_LIST[0].hash()
    assert r[1] == None
    assert r[2].hash() == TX_LIST[2].hash()
    assert r[3] == None
    assert r[4].hash() == TX_LIST[4].hash()
    assert r[5] == None