Beispiel #1
0
def test_ping(raiden_network):
    app0, app1 = raiden_network  # pylint: disable=unbalanced-tuple-unpacking

    messages = setup_messages_cb()

    ping_message = Ping(nonce=0)
    app0.raiden.sign(ping_message)
    ping_encoded = ping_message.encode()

    async_result = app0.raiden.protocol.send_raw_with_result(
        ping_encoded,
        app1.raiden.address,
    )
    assert async_result.wait(2), 'The message was not processed'

    expected_messageid = messageid_from_data(ping_encoded, app1.raiden.address)
    messages_decoded = [decode(m) for m in messages]
    processed_message = next(
        decoded
        for decoded in messages_decoded
        if isinstance(decoded, Processed) and
        decoded.processed_message_identifier == expected_messageid
    )

    # the ping message was sent and processed
    assert ping_encoded in messages
    assert processed_message
Beispiel #2
0
def test_hash():
    ping = Ping(nonce=0)
    ping.sign(PRIVKEY)
    data = ping.encode()
    msghash = sha3(data)
    decoded_ping = decode(data)
    assert sha3(decoded_ping.encode()) == msghash
Beispiel #3
0
    def send_ping(self, receiver_address):
        if not isaddress(receiver_address):
            raise ValueError('Invalid address {}'.format(
                pex(receiver_address)))

        nonce = self._ping_nonces[receiver_address]
        self._ping_nonces[receiver_address] += 1

        message = Ping(nonce)
        self.raiden.sign(message)

        if log.isEnabledFor(logging.INFO):
            log.info('SENDING PING %s > %s', pex(self.raiden.address),
                     pex(receiver_address))

        message_data = message.encode()
        echohash = sha3(message_data + receiver_address)
        async_result = AsyncResult()
        if echohash not in self.echohash_asyncresult:
            self.echohash_asyncresult[echohash] = WaitAck(
                async_result, receiver_address)
        # Just like ACK, a PING message is sent directly. No need for queuing
        self.transport.send(self.raiden, self.discovery.get(receiver_address),
                            message_data)
        return async_result
def test_ping_unreachable(raiden_network):
    app0, app1 = raiden_network  # pylint: disable=unbalanced-tuple-unpacking

    # drop everything to force disabling of re-sends
    app0.raiden.protocol.transport.droprate = 1
    app1.raiden.protocol.transport.droprate = 1

    app0.raiden.protocol.retry_interval = 0.1  # for fast tests

    messages = setup_messages_cb()

    ping_message = Ping(nonce=0)
    app0.raiden.sign(ping_message)
    ping_encoded = ping_message.encode()

    async_result = app0.raiden.protocol.send_raw_with_result(
        ping_encoded,
        app1.raiden.address,
    )

    assert async_result.wait(2) is None, "The message was dropped, it can't be acknowledged"

    # Raiden node will start pinging as soon as a new channel
    #  is established. We need to test if
    #  a) there is our original message in the queue
    #  b) there are only Ping message types in
    messages_decoded = [decode(m) for m in messages]
    assert ping_message in messages_decoded
    for message in messages_decoded:
        assert isinstance(message, Ping)
Beispiel #5
0
def test_ping_unreachable(raiden_network):
    app0, app1 = raiden_network  # pylint: disable=unbalanced-tuple-unpacking

    UnreliableTransport.droprate = 1  # drop everything to force disabling of re-sends
    app0.raiden.protocol.retry_interval = 0.1  # for fast tests

    messages = setup_messages_cb()
    UnreliableTransport.network.counter = 0

    ping_message = Ping(nonce=0)
    app0.raiden.sign(ping_message)
    ping_encoded = ping_message.encode()

    async_result = app0.raiden.protocol.send_raw_with_result(
        ping_encoded,
        app1.raiden.address,
    )

    assert async_result.wait(
        2) is None, "The message was dropped, it can't be acknowledged"

    # Raiden node will start pinging as soon as a new channel
    #  is established. We need to test if
    #  a) there is our original message in the queue
    #  b) there are only Ping message types in
    messages_decoded = [decode(m) for m in messages]
    assert ping_message in messages_decoded
    for message in messages_decoded:
        assert isinstance(message, Ping)
Beispiel #6
0
def test_udp_unreachable_node(raiden_network, skip_if_not_udp):  # pylint: disable=unused-argument
    """A node that does *not* answer the ping message must have its state set to
    reachable.
    """
    app0, app1 = raiden_network

    app1.raiden.transport.stop()

    ping_message = Ping(nonce=0, current_protocol_version=0)
    app0.raiden.sign(ping_message)
    ping_encoded = ping_message.encode()

    messageid = ("ping", ping_message.nonce, app1.raiden.address)
    async_result = app0.raiden.transport.maybe_sendraw_with_result(
        app1.raiden.address, ping_encoded, messageid)

    nat_keepalive_fail = (
        app0.config["transport"]["udp"]["nat_keepalive_timeout"] *
        app0.config["transport"]["udp"]["nat_keepalive_retries"] *
        2  # wait a bit longer to avoid races
    )
    msg = "The message was dropped, it can't be acknowledged"
    assert async_result.wait(nat_keepalive_fail) is None, msg

    network_state = views.get_node_network_status(views.state_from_app(app0),
                                                  app1.raiden.address)
    assert network_state is state.NODE_NETWORK_UNREACHABLE
def test_ping(raiden_network):
    app0, app1 = raiden_network  # pylint: disable=unbalanced-tuple-unpacking

    messages = setup_messages_cb()

    ping_message = Ping(nonce=0)
    app0.raiden.sign(ping_message)
    ping_encoded = ping_message.encode()

    async_result = app0.raiden.protocol.send_raw_with_result(
        ping_encoded,
        app1.raiden.address,
    )
    assert async_result.wait(2), 'The message was not acknowledged'

    expected_echohash = sha3(ping_encoded + app1.raiden.address)

    messages_decoded = [decode(m) for m in messages]
    ack_message = next(
        decoded
        for decoded in messages_decoded
        if isinstance(decoded, Ack) and decoded.echo == expected_echohash
    )

    # the ping message was sent and acknowledged
    assert ping_encoded in messages
    assert ack_message
Beispiel #8
0
def test_ping(raiden_network):
    app0, app1 = raiden_network  # pylint: disable=unbalanced-tuple-unpacking

    messages = setup_messages_cb()

    ping_message = Ping(nonce=0)
    app0.raiden.sign(ping_message)
    ping_encoded = ping_message.encode()

    async_result = app0.raiden.protocol.send_raw_with_result(
        ping_encoded,
        app1.raiden.address,
    )
    assert async_result.wait(2), "The message was not acknowledged"

    expected_echohash = sha3(ping_encoded + app1.raiden.address)

    messages_decoded = [decode(m) for m in messages]
    ack_message = next(
        decoded for decoded in messages_decoded
        if isinstance(decoded, Ack) and decoded.echo == expected_echohash)

    # the ping message was sent and acknowledged
    assert ping_encoded in messages
    assert ack_message
Beispiel #9
0
def test_udp_ping_pong_unreachable_node(raiden_network, skip_if_not_udp):
    app0, app1 = raiden_network

    app1.raiden.transport.stop_and_wait()

    ping_message = Ping(nonce=0)
    app0.raiden.sign(ping_message)
    ping_encoded = ping_message.encode()

    messageid = ('ping', ping_message.nonce, app1.raiden.address)
    async_result = app0.raiden.transport.maybe_sendraw_with_result(
        app1.raiden.address,
        ping_encoded,
        messageid,
    )

    nat_keepalive_fail = (
        app0.config['transport']['nat_keepalive_timeout'] *
        app0.config['transport']['nat_keepalive_retries'] *
        2  # wait a bit longer to avoid races
    )
    msg = "The message was dropped, it can't be acknowledged"
    assert async_result.wait(nat_keepalive_fail) is None, msg

    network_state = views.get_node_network_status(
        views.state_from_app(app0),
        app1.raiden.address,
    )
    assert network_state is state.NODE_NETWORK_UNREACHABLE
Beispiel #10
0
def test_hash():
    ping = Ping(nonce=0, current_protocol_version=constants.PROTOCOL_VERSION)
    ping.sign(signer)
    data = ping.encode()
    msghash = sha3(data)
    decoded_ping = decode(data)
    assert sha3(decoded_ping.encode()) == msghash
def test_hash():
    ping = Ping(nonce=0, current_protocol_version=constants.PROTOCOL_VERSION)
    ping.sign(PRIVKEY)
    data = ping.encode()
    msghash = sha3(data)
    decoded_ping = decode(data)
    assert sha3(decoded_ping.encode()) == msghash
Beispiel #12
0
def test_udp_ping_pong(raiden_network):
    app0, app1 = raiden_network  # pylint: disable=unbalanced-tuple-unpacking

    messages = setup_messages_cb()

    ping_message = Ping(nonce=0)
    app0.raiden.sign(ping_message)
    ping_encoded = ping_message.encode()

    messageid = ('ping', ping_message.nonce, app1.raiden.address)
    async_result = app0.raiden.protocol.maybe_sendraw_with_result(
        app1.raiden.address,
        ping_encoded,
        messageid,
    )
    assert async_result.wait(2), 'The message was not processed'

    messages_decoded = [decode(m) for m in messages]
    processed_message = next(
        pong for pong in messages_decoded
        if isinstance(pong, Pong) and pong.nonce == ping_message.nonce)

    # the ping message was sent and processed
    assert ping_encoded in messages
    assert processed_message
Beispiel #13
0
def test_udp_ping_pong_unreachable_node(raiden_network, skip_if_not_udp):
    app0, app1 = raiden_network

    app1.raiden.transport.stop()

    ping_message = Ping(nonce=0)
    app0.raiden.sign(ping_message)
    ping_encoded = ping_message.encode()

    messageid = ('ping', ping_message.nonce, app1.raiden.address)
    async_result = app0.raiden.transport.maybe_sendraw_with_result(
        app1.raiden.address,
        ping_encoded,
        messageid,
    )

    nat_keepalive_fail = (
        app0.config['transport']['udp']['nat_keepalive_timeout'] *
        app0.config['transport']['udp']['nat_keepalive_retries'] *
        2  # wait a bit longer to avoid races
    )
    msg = "The message was dropped, it can't be acknowledged"
    assert async_result.wait(nat_keepalive_fail) is None, msg

    network_state = views.get_node_network_status(
        views.state_from_app(app0),
        app1.raiden.address,
    )
    assert network_state is state.NODE_NETWORK_UNREACHABLE
Beispiel #14
0
    def send_ping(self, receiver_address):
        if not isaddress(receiver_address):
            raise ValueError('Invalid address {}'.format(pex(receiver_address)))

        nonce = self._ping_nonces[receiver_address]
        self._ping_nonces[receiver_address] += 1

        message = Ping(nonce)
        self.raiden.sign(message)

        if log.isEnabledFor(logging.INFO):
            log.info(
                'SENDING PING %s > %s',
                pex(self.raiden.address),
                pex(receiver_address)
            )

        message_data = message.encode()
        echohash = sha3(message_data + receiver_address)
        async_result = AsyncResult()
        if echohash not in self.echohash_asyncresult:
            self.echohash_asyncresult[echohash] = WaitAck(async_result, receiver_address)
        # Just like ACK, a PING message is sent directly. No need for queuing
        self.transport.send(
            self.raiden,
            self.discovery.get(receiver_address),
            message_data
        )
        return async_result
def test_encoding():
    ping = Ping(nonce=0, current_protocol_version=constants.PROTOCOL_VERSION)
    ping.sign(PRIVKEY)
    decoded_ping = decode(ping.encode())
    assert isinstance(decoded_ping, Ping)
    assert decoded_ping.sender == ADDRESS == ping.sender
    assert ping.nonce == decoded_ping.nonce
    assert ping.signature == decoded_ping.signature
    assert ping.cmdid == decoded_ping.cmdid
    assert ping.hash == decoded_ping.hash
Beispiel #16
0
def test_encoding():
    ping = Ping(nonce=0, current_protocol_version=constants.PROTOCOL_VERSION)
    ping.sign(signer)
    decoded_ping = decode(ping.encode())
    assert isinstance(decoded_ping, Ping)
    assert decoded_ping.sender == ADDRESS == ping.sender
    assert ping.nonce == decoded_ping.nonce
    assert ping.signature == decoded_ping.signature
    assert ping.cmdid == decoded_ping.cmdid
    assert ping.hash == decoded_ping.hash
Beispiel #17
0
def test_encoding():
    ping = Ping(nonce=0)
    ping.sign(PRIVKEY)
    decoded_ping = decode(ping.encode())
    assert isinstance(decoded_ping, Ping)
    assert decoded_ping.sender == ADDRESS == ping.sender
    assert ping.nonce == decoded_ping.nonce
    assert ping.signature == decoded_ping.signature
    assert ping.cmdid == decoded_ping.cmdid
    assert ping.hash == decoded_ping.hash
Beispiel #18
0
    def get_ping(self, nonce: Nonce) -> bytes:
        """ Returns a signed Ping message.

        Note: Ping messages don't have an enforced ordering, so a Ping message
        with a higher nonce may be acknowledged first.
        """
        message = Ping(nonce=nonce,
                       current_protocol_version=constants.PROTOCOL_VERSION)
        self.raiden.sign(message)
        return message.encode()
Beispiel #19
0
def test_ping_dropped_message():
    apps = create_network(
        num_nodes=2,
        num_assets=0,
        channels_per_node=0,
        transport_class=UnreliableTransport,
    )
    app0, app1 = apps  # pylint: disable=unbalanced-tuple-unpacking

    # mock transport with packet loss, every 3nd is lost, starting with first message
    UnreliableTransport.droprate = 3
    RaidenProtocol.try_interval = 0.1  # for fast tests
    RaidenProtocol.repeat_messages = True

    messages = setup_messages_cb()
    UnreliableTransport.network.counter = 0

    ping = Ping(nonce=0)
    app0.raiden.sign(ping)
    app0.raiden.protocol.send(app1.raiden.address, ping)
    gevent.sleep(1)

    assert len(messages) == 3  # Ping(dropped), Ping, Ack

    for i in [0, 1]:
        assert decode(messages[i]) == ping

    for i in [2]:
        decoded = decode(messages[i])
        assert isinstance(decoded, Ack)

    assert decoded.echo == ping.hash

    # try failing Ack
    messages = setup_messages_cb()
    assert not messages

    UnreliableTransport.network.counter = 2  # first message sent, 2nd dropped
    ping = Ping(nonce=0)
    app0.raiden.sign(ping)
    app0.raiden.protocol.send(app1.raiden.address, ping)
    gevent.sleep(1)

    for message in messages:
        print decode(message)

    assert len(messages) == 4  # Ping, Ack(dropped), Ping, Ack
    for i in [0, 2]:
        assert decode(messages[i]) == ping
    for i in [1, 3]:
        decoded = decode(messages[i])
        assert isinstance(decoded, Ack)
    assert decoded.echo == ping.hash

    RaidenProtocol.repeat_messages = False
Beispiel #20
0
    def get_ping(self, nonce: int) -> Ping:
        """ Returns a signed Ping message.

        Note: Ping messages don't have an enforced ordering, so a Ping message
        with a higher nonce may be acknowledged first.
        """
        message = Ping(nonce)
        self.raiden.sign(message)
        message_data = message.encode()

        return message_data
Beispiel #21
0
    def get_ping(self, nonce):
        """ Returns a signed Ping message.

        Note: Ping messages don't have an enforced ordering, so a Ping message
        with a higher nonce may be acknowledged first.
        """
        message = Ping(nonce)
        self.raiden.sign(message)
        message_data = message.encode()

        return message_data
Beispiel #22
0
def test_ping_udp(raiden_network):
    app0, app1 = raiden_network  # pylint: disable=unbalanced-tuple-unpacking
    messages = setup_messages_cb()
    ping = Ping(nonce=0)
    app0.raiden.sign(ping)
    app0.raiden.protocol.send_and_wait(app1.raiden.address, ping)
    gevent.sleep(0.1)
    assert len(messages) == 2  # Ping, Ack
    assert decode(messages[0]) == ping
    decoded = decode(messages[1])
    assert isinstance(decoded, Ack)
    assert decoded.echo == sha3(ping.encode() + app1.raiden.address)
Beispiel #23
0
def test_udp_reachable_node(raiden_network, skip_if_not_udp):  # pylint: disable=unused-argument
    """A node that answers the ping message must have its state set to
    reachable.
    """
    app0, app1 = raiden_network

    ping_message = Ping(nonce=0, current_protocol_version=0)
    app0.raiden.sign(ping_message)
    ping_encoded = ping_message.encode()

    messageid = ("ping", ping_message.nonce, app1.raiden.address)
    async_result = app0.raiden.transport.maybe_sendraw_with_result(
        app1.raiden.address, ping_encoded, messageid)
    assert async_result.wait(2), "The message was not processed"

    network_state = views.get_node_network_status(views.state_from_app(app0),
                                                  app1.raiden.address)
    assert network_state is state.NODE_NETWORK_REACHABLE
Beispiel #24
0
def test_udp_ping_pong(raiden_network, skip_if_not_udp):
    app0, app1 = raiden_network

    ping_message = Ping(nonce=0)
    app0.raiden.sign(ping_message)
    ping_encoded = ping_message.encode()

    messageid = ('ping', ping_message.nonce, app1.raiden.address)
    async_result = app0.raiden.transport.maybe_sendraw_with_result(
        app1.raiden.address,
        ping_encoded,
        messageid,
    )
    assert async_result.wait(2), 'The message was not processed'

    network_state = views.get_node_network_status(
        views.state_from_app(app0),
        app1.raiden.address,
    )
    assert network_state is state.NODE_NETWORK_REACHABLE
Beispiel #25
0
def test_udp_ping_pong(raiden_network, skip_if_not_udp):
    app0, app1 = raiden_network

    ping_message = Ping(nonce=0)
    app0.raiden.sign(ping_message)
    ping_encoded = ping_message.encode()

    messageid = ('ping', ping_message.nonce, app1.raiden.address)
    async_result = app0.raiden.transport.maybe_sendraw_with_result(
        app1.raiden.address,
        ping_encoded,
        messageid,
    )
    assert async_result.wait(2), 'The message was not processed'

    network_state = views.get_node_network_status(
        views.state_from_app(app0),
        app1.raiden.address,
    )
    assert network_state is state.NODE_NETWORK_REACHABLE
Beispiel #26
0
def test_ping():
    apps = create_network(num_nodes=2, num_assets=0, channels_per_node=0)
    app0, app1 = apps  # pylint: disable=unbalanced-tuple-unpacking
    messages = setup_messages_cb()
    ping = Ping(nonce=0)
    app0.raiden.sign(ping)
    app0.raiden.protocol.send(app1.raiden.address, ping)
    gevent.sleep(0.1)
    assert len(messages) == 2  # Ping, Ack
    assert decode(messages[0]) == ping
    decoded = decode(messages[1])
    assert isinstance(decoded, Ack)
    assert decoded.echo == ping.hash
Beispiel #27
0
def test_ping_ordering(raiden_network):
    app0, app1 = raiden_network  # pylint: disable=unbalanced-tuple-unpacking

    # mock transport with packet loss, every 3rd is lost, starting with first message
    droprate = UnreliableTransport.droprate = 3
    RaidenProtocol.try_interval = 0.1  # for fast tests
    RaidenProtocol.repeat_messages = True

    messages = setup_messages_cb()
    UnreliableTransport.network.counter = 0

    ping_amount = 5

    hashes = []
    for nonce in range(ping_amount):
        ping = Ping(nonce=nonce)
        app0.raiden.sign(ping)
        app0.raiden.protocol.send_and_wait(app1.raiden.address, ping)
        pinghash = sha3(ping.encode() + app1.raiden.address)
        hashes.append(pinghash)

    gevent.sleep(2)  # give some time for messages to be handled

    expected_message_amount = ping_amount * droprate
    assert len(messages) == expected_message_amount

    for i in range(0, expected_message_amount, droprate):
        assert isinstance(decode(messages[i]), Ping)

    for i in range(1, expected_message_amount, droprate):
        assert isinstance(decode(messages[i]), Ping)

    for i, j in zip(range(2, expected_message_amount, droprate),
                    range(ping_amount)):
        decoded = decode(messages[i])
        assert isinstance(decoded, Ack)
        assert decoded.echo == hashes[j]

    RaidenProtocol.repeat_messages = False
Beispiel #28
0
def test_ping():
    apps = create_network(num_nodes=2, num_assets=0, channels_per_node=0)
    a0, a1 = apps
    messages = setup_messages_cb()
    p = Ping(nonce=0)
    a0.raiden.sign(p)
    a0.raiden.protocol.send(a1.raiden.address, p)
    gevent.sleep(0.1)
    assert len(messages) == 2  # Ping, Ack
    assert decode(messages[0]) == p
    a = decode(messages[1])
    assert isinstance(a, Ack)
    assert a.echo == p.hash
Beispiel #29
0
def test_ping_unreachable(raiden_network):
    app0, app1 = raiden_network  # pylint: disable=unbalanced-tuple-unpacking

    UnreliableTransport.droprate = 1  # drop everything to force disabling of re-sends
    app0.raiden.protocol.retry_interval = 0.1  # for fast tests

    messages = setup_messages_cb()
    UnreliableTransport.network.counter = 0

    ping_message = Ping(nonce=0)
    app0.raiden.sign(ping_message)
    ping_encoded = ping_message.encode()

    async_result = app0.raiden.protocol.send_raw_with_result(
        ping_encoded,
        app1.raiden.address,
    )

    assert async_result.wait(2) is None, "The message was dropped, it can't be acknowledged"

    for message in messages:
        assert decode(message) == ping_message
Beispiel #30
0
def test_ping_ordering(raiden_network):
    app0, app1 = raiden_network  # pylint: disable=unbalanced-tuple-unpacking

    # mock transport with packet loss, every 3rd is lost, starting with first message
    droprate = UnreliableTransport.droprate = 3
    RaidenProtocol.try_interval = 0.1  # for fast tests
    RaidenProtocol.repeat_messages = True

    messages = setup_messages_cb()
    UnreliableTransport.network.counter = 0

    ping_amount = 5

    hashes = []
    for nonce in range(ping_amount):
        ping = Ping(nonce=nonce)
        app0.raiden.sign(ping)
        app0.raiden.protocol.send_and_wait(app1.raiden.address, ping)
        pinghash = sha3(ping.encode() + app1.raiden.address)
        hashes.append(pinghash)

    gevent.sleep(2)  # give some time for messages to be handled

    expected_message_amount = ping_amount * droprate
    assert len(messages) == expected_message_amount

    for i in range(0, expected_message_amount, droprate):
        assert isinstance(decode(messages[i]), Ping)

    for i in range(1, expected_message_amount, droprate):
        assert isinstance(decode(messages[i]), Ping)

    for i, j in zip(range(2, expected_message_amount, droprate), range(ping_amount)):
        decoded = decode(messages[i])
        assert isinstance(decoded, Ack)
        assert decoded.echo == hashes[j]

    RaidenProtocol.repeat_messages = False
Beispiel #31
0
def test_encoding():
    p = Ping(nonce=0)
    with pytest.raises(SignatureMissingError):
        p.encode()
    p.sign(privkey)
    d = p.encode()
    p2 = decode(d)
    assert isinstance(p2, Ping)
    assert p2.sender == address == p.sender
    assert p.nonce == p2.nonce
    assert p.signature == p2.signature
    assert p.cmdid == p.cmdid
    assert p.hash == p2.hash
Beispiel #32
0
    def ping(self, peer):
        """See, if a peer is discoverable and up.
        Args:
            peer (string): the hex-encoded (ethereum) address of the peer.
        Returns:
            success (boolean): True if ping succeeded, False otherwise.
        """
        # Check, if peer is discoverable
        try:
            self._discovery.get(peer.decode('hex'))
        except KeyError:
            print("Error: peer {} not found in discovery".format(peer))
            return

        nonce = self._ping_nonces[peer]
        self._ping_nonces[peer] += 1
        msg = Ping(nonce)
        self._raiden.sign(msg)
        try:
            self._raiden.protocol._repeat_until_ack(peer.decode('hex'), msg)
        except Exception as e:
            if not e.message.startswith('DEACTIVATED'):
                return e
        return sha3(msg.encode()) not in self._raiden.protocol.number_of_tries
def test_signature():
    ping = Ping(nonce=0, current_protocol_version=constants.PROTOCOL_VERSION)
    ping.sign(PRIVKEY)
    assert ping.sender == ADDRESS

    # test that the valid v values are accepted
    message_data = ping._data_to_sign()
    # This signature will sometimes end up with v being 0, sometimes 1
    signature = eth_sign(privkey=PRIVKEY, data=message_data, v=0)
    assert ADDRESS == eth_recover(message_data, signature)
    # This signature will sometimes end up with v being 27, sometimes 28
    signature = eth_sign(privkey=PRIVKEY, data=message_data, v=27)
    assert ADDRESS == eth_recover(message_data, signature)

    # test that other v values are rejected
    signature = signature[:-1] + bytes([29])
    with pytest.raises(InvalidSignature):
        eth_recover(message_data, signature)
    signature = signature[:-1] + bytes([37])
    with pytest.raises(InvalidSignature):
        eth_recover(message_data, signature)
    signature = signature[:-1] + bytes([38])
    with pytest.raises(InvalidSignature):
        eth_recover(message_data, signature)
Beispiel #34
0
def test_signature():
    ping = Ping(nonce=0, current_protocol_version=constants.PROTOCOL_VERSION)
    ping.sign(signer)
    assert ping.sender == ADDRESS

    # test that the valid v values are accepted
    message_data = ping._data_to_sign()
    # This signature will sometimes end up with v being 0, sometimes 1
    signature = signer.sign(data=message_data, v=0)
    assert ADDRESS == recover(message_data, signature)
    # This signature will sometimes end up with v being 27, sometimes 28
    signature = signer.sign(data=message_data, v=27)
    assert ADDRESS == recover(message_data, signature)

    # test that other v values are rejected
    signature = signature[:-1] + bytes([29])
    with pytest.raises(InvalidSignature):
        recover(message_data, signature)
    signature = signature[:-1] + bytes([37])
    with pytest.raises(InvalidSignature):
        recover(message_data, signature)
    signature = signature[:-1] + bytes([38])
    with pytest.raises(InvalidSignature):
        recover(message_data, signature)
Beispiel #35
0
def test_ping(raiden_network):
    app0, app1 = raiden_network  # pylint: disable=unbalanced-tuple-unpacking

    messages = setup_messages_cb()

    ping_message = Ping(nonce=0)
    app0.raiden.sign(ping_message)
    ping_encoded = ping_message.encode()

    app0.raiden.protocol.send_and_wait(
        app1.raiden.address,
        ping_message,
    )
    gevent.sleep(0.1)

    # mesages may have more than two entries depending on the retry logic, so
    # fiter duplicates
    assert len(set(messages)) == 2

    assert ping_encoded in messages
    messages_decoded = [decode(m) for m in messages]
    ack_message = next(decoded for decoded in messages_decoded
                       if isinstance(decoded, Ack))
    assert ack_message.echo == sha3(ping_encoded + app1.raiden.address)
Beispiel #36
0
    def ping(self, peer):
        """See, if a peer is discoverable and up.
        Args:
            peer (string): the hex-encoded (ethereum) address of the peer.
        Returns:
            success (boolean): True if ping succeeded, False otherwise.
        """
        # Check, if peer is discoverable
        try:
            self._discovery.get(peer.decode('hex'))
        except KeyError:
            print("Error: peer {} not found in discovery".format(peer))
            return

        nonce = self._ping_nonces[peer]
        self._ping_nonces[peer] += 1
        msg = Ping(nonce)
        self._raiden.sign(msg)
        try:
            self._raiden.protocol._repeat_until_ack(peer.decode('hex'), msg)
        except Exception as e:
            if not e.message.startswith('DEACTIVATED'):
                return e
        return sha3(msg.encode()) not in self._raiden.protocol.number_of_tries
Beispiel #37
0
def test_encoding():
    p = Ping(nonce=0)
    with pytest.raises(SignatureMissingError):
        p.encode()
    p.sign(privkey)
    d = p.encode()
    p2 = decode(d)
    assert isinstance(p2, Ping)
    assert p2.sender == address == p.sender
    assert p.nonce == p2.nonce
    assert p.signature == p2.signature
    assert p.cmdid == p.cmdid
    assert p.hash == p2.hash
Beispiel #38
0
    def ping(self, peer):
        """See, if a peer is discoverable and up.
        Args:
            peer (string): the hex-encoded (ethereum) address of the peer.
        Returns:
            success (boolean): True if ping succeeded, False otherwise.
        """
        # Check, if peer is discoverable
        try:
            self._discovery.get(peer.decode('hex'))
        except KeyError:
            print("Error: peer {} not found in discovery".format(peer))
            return

        nonce = self._ping_nonces[peer]
        self._ping_nonces[peer] += 1
        msg = Ping(nonce)
        self._raiden.sign(msg)
        return self._raiden.protocol.send_and_wait(peer.decode('hex'), msg)
Beispiel #39
0
def test_ping_unreachable(raiden_network):
    app0, app1 = raiden_network  # pylint: disable=unbalanced-tuple-unpacking

    UnreliableTransport.droprate = 1  # drop everything to force disabling of re-sends
    RaidenProtocol.try_interval = 0.1  # for fast tests
    RaidenProtocol.repeat_messages = True

    messages = setup_messages_cb()
    UnreliableTransport.network.counter = 0

    ping = Ping(nonce=0)
    app0.raiden.sign(ping)
    app0.raiden.protocol.send_and_wait(app1.raiden.address, ping)
    gevent.sleep(2)

    assert len(messages) == 5  # 5 dropped Pings
    for i, message in enumerate(messages):
        assert decode(message) == ping

    RaidenProtocol.repeat_messages = False
Beispiel #40
0
def test_ping_dropped_message(raiden_network):
    app0, app1 = raiden_network  # pylint: disable=unbalanced-tuple-unpacking

    # mock transport with packet loss, every 3rd is lost, starting with first message
    UnreliableTransport.droprate = 3
    RaidenProtocol.try_interval = 0.1  # for fast tests
    RaidenProtocol.repeat_messages = True

    messages = setup_messages_cb()
    UnreliableTransport.network.counter = 0

    ping = Ping(nonce=0)
    app0.raiden.sign(ping)
    app0.raiden.protocol.send_and_wait(app1.raiden.address, ping)
    gevent.sleep(1)

    assert len(messages) == 3  # Ping(dropped), Ping, Ack

    for i in [0, 1]:
        assert decode(messages[i]) == ping

    for i in [2]:
        decoded = decode(messages[i])
        assert isinstance(decoded, Ack)

    assert decoded.echo == sha3(ping.encode() + app1.raiden.address)

    messages = setup_messages_cb()
    assert not messages

    UnreliableTransport.network.counter = 2  # first message sent, 2nd dropped
    ping = Ping(nonce=1)
    app0.raiden.sign(ping)
    app0.raiden.protocol.send_and_wait(app1.raiden.address, ping)
    gevent.sleep(1)

    assert len(messages) == 4  # Ping, Ack(dropped), Ping, Ack
    for i in [0, 2]:
        assert decode(messages[i]) == ping
    for i in [1, 3]:
        decoded = decode(messages[i])
        assert isinstance(decoded, Ack)
    assert decoded.echo == sha3(ping.encode() + app1.raiden.address)

    RaidenProtocol.repeat_messages = False
Beispiel #41
0
def test_signature():
    ping = Ping(nonce=0, current_protocol_version=0)
    ping.sign(PRIVKEY)
    assert ping.sender == ADDRESS
Beispiel #42
0
def test_ping(iterations=ITERATIONS):
    msg = Ping(nonce=0)
    msg.sign(PRIVKEY, ADDRESS)
    run_timeit('Ping', msg, iterations=iterations)
Beispiel #43
0
def test_ping(iterations=ITERATIONS):
    msg = Ping(nonce=0)
    msg.sign(PRIVKEY, ADDRESS)
    run_timeit('Ping', msg, iterations=iterations)
Beispiel #44
0
def test_signature():
    p = Ping(nonce=0)
    with pytest.raises(SignatureMissingError):
        p.sender
    p.sign(privkey)
    assert p.sender == address
Beispiel #45
0
def test_signature():
    ping = Ping(nonce=0, current_protocol_version=0)
    ping.sign(PRIVKEY)
    assert ping.sender == ADDRESS
Beispiel #46
0
def test_hash():
    msg = Ping(nonce=0).sign(privkey)
    d = msg.encode()
    msghash = sha3(d)
    msg2 = decode(d)
    assert sha3(msg2.encode()) == msghash
Beispiel #47
0
def test_signature():
    ping = Ping(nonce=0)
    ping.sign(PRIVKEY)
    assert ping.sender == ADDRESS