Exemple #1
0
    def _receive_delivered(self, delivered: Delivered):
        # FIXME: check if UDPTransport also checks Delivered sender and message presence
        # checks there's a respective message on sender's queue
        for queue_identifier, events in self._queueids_to_queues.items():
            if delivered.sender != queue_identifier.recipient:
                continue
            if any(delivered.sender == event.recipient for event in events):
                break
        else:
            self.log.debug(
                'DELIVERED MESSAGE UNKNOWN',
                sender=pex(delivered.sender),
                message=delivered,
            )
            return

        self._raiden_service.handle_state_change(
            ReceiveDelivered(delivered.delivered_message_identifier),
        )

        self.log.debug(
            'DELIVERED MESSAGE RECEIVED',
            sender=pex(delivered.sender),
            message=delivered,
        )
Exemple #2
0
    def _receive_delivered(self, delivered: Delivered):
        # FIXME: The signature doesn't seem to be verified - check in UDPTransport as well
        self._raiden_service.handle_state_change(
            ReceiveDelivered(delivered.delivered_message_identifier), )

        async_result = self._messageids_to_asyncresult.pop(
            delivered.delivered_message_identifier,
            None,
        )

        if async_result is not None:
            async_result.set(True)
            self.log.debug(
                'DELIVERED MESSAGE RECEIVED',
                node=pex(self._raiden_service.address),
                receiver=pex(delivered.sender),
                message_identifier=delivered.delivered_message_identifier,
            )

        else:
            self.log.debug(
                'DELIVERED MESSAGE UNKNOWN',
                node=pex(self._raiden_service.address),
                message_identifier=delivered.delivered_message_identifier,
            )
Exemple #3
0
 def handle_message_delivered(
     raiden: "RaidenService",
     message: Delivered  # pylint: disable=unused-argument
 ) -> List[StateChange]:
     assert message.sender, "message must be signed"
     delivered = ReceiveDelivered(message.sender,
                                  message.delivered_message_identifier)
     return [delivered]
Exemple #4
0
 def handle_message_delivered(cls,
                              raiden: RaidenService,
                              message: Delivered,
                              is_light_client: bool = False) -> None:
     delivered = ReceiveDelivered(message.sender,
                                  message.delivered_message_identifier)
     raiden.handle_and_track_state_change(delivered)
     if is_light_client:
         LightClientMessageHandler.store_lc_delivered(message, raiden.wal)
Exemple #5
0
def test_inplace_delete_message_queue(chain_state):
    sender = factories.make_address()
    canonical_identifier = factories.make_canonical_identifier()
    message_id = factories.make_message_identifier()
    delivered_state_change = ReceiveDelivered(sender=sender,
                                              message_identifier=message_id)
    processed_state_change = ReceiveProcessed(sender=sender,
                                              message_identifier=message_id)

    global_identifier = QueueIdentifier(
        recipient=sender,
        canonical_identifier=CANONICAL_IDENTIFIER_GLOBAL_QUEUE)

    chain_state.queueids_to_queues[global_identifier] = None
    assert global_identifier in chain_state.queueids_to_queues, "queue mapping insertion failed"
    inplace_delete_message_queue(chain_state=chain_state,
                                 state_change=delivered_state_change,
                                 queueid=global_identifier)
    assert global_identifier not in chain_state.queueids_to_queues, "did not clear queue"

    chain_state.queueids_to_queues[global_identifier] = [
        SendMessageEvent(
            recipient=sender,
            canonical_identifier=canonical_identifier,
            message_identifier=message_id,
        )
    ]
    assert global_identifier in chain_state.queueids_to_queues, "queue mapping insertion failed"
    handle_receive_delivered(chain_state=chain_state,
                             state_change=delivered_state_change)
    assert global_identifier not in chain_state.queueids_to_queues, "did not clear queue"

    queue_identifier = QueueIdentifier(
        recipient=sender, canonical_identifier=canonical_identifier)
    assert queue_identifier not in chain_state.queueids_to_queues, "queue not empty"
    chain_state.queueids_to_queues[queue_identifier] = [
        SendMessageEvent(
            recipient=sender,
            canonical_identifier=canonical_identifier,
            message_identifier=message_id,
        )
    ]
    assert queue_identifier in chain_state.queueids_to_queues, "queue mapping not mutable"
    handle_receive_processed(chain_state=chain_state,
                             state_change=processed_state_change)
    assert queue_identifier not in chain_state.queueids_to_queues, "queue did not clear"
Exemple #6
0
    def receive_delivered(self, delivered: Delivered):
        """ Handle a Delivered message.

        The Delivered message is how the UDP transport guarantees persistence
        by the partner node. The message itself is not part of the raiden
        protocol, but it's required by this transport to provide the required
        properties.
        """
        processed = ReceiveDelivered(delivered.delivered_message_identifier)
        self.raiden.handle_state_change(processed)

        message_id = delivered.delivered_message_identifier
        async_result = self.raiden.transport.messageids_to_asyncresults.get(message_id)

        # clear the async result, otherwise we have a memory leak
        if async_result is not None:
            del self.messageids_to_asyncresults[message_id]
            async_result.set()
 def handle_message_delivered(self, raiden: RaidenService,
                              message: Delivered):
     delivered = ReceiveDelivered(message.sender,
                                  message.delivered_message_identifier)
     raiden.handle_state_change(delivered)
def test_message_handler():
    """
    Test for MessageHandler.on_message and the different methods it dispatches into.
    Each of them results in a call to a RaidenService method, which is checked with a Mock.
    """

    our_address = factories.make_address()
    sender_privkey, sender = factories.make_privkey_address()
    signer = LocalSigner(sender_privkey)
    message_handler = MessageHandler()
    mock_raiden = Mock(
        address=our_address, default_secret_registry=Mock(is_secret_registered=lambda **_: False)
    )

    properties = factories.LockedTransferProperties(sender=sender, pkey=sender_privkey)
    locked_transfer = factories.create(properties)
    message_handler.on_message(mock_raiden, locked_transfer)
    assert_method_call(mock_raiden, "mediate_mediated_transfer", locked_transfer)

    locked_transfer_for_us = factories.create(factories.replace(properties, target=our_address))
    message_handler.on_message(mock_raiden, locked_transfer_for_us)
    assert_method_call(mock_raiden, "target_mediated_transfer", locked_transfer_for_us)

    mock_raiden.default_secret_registry.is_secret_registered = lambda **_: True
    message_handler.on_message(mock_raiden, locked_transfer)
    assert not mock_raiden.mediate_mediated_transfer.called
    assert not mock_raiden.target_mediated_transfer.called
    mock_raiden.default_secret_registry.is_secret_registered = lambda **_: False

    params = dict(
        payment_identifier=13, amount=14, expiration=15, secrethash=factories.UNIT_SECRETHASH
    )
    secret_request = SecretRequest(
        message_identifier=16, signature=factories.EMPTY_SIGNATURE, **params
    )
    secret_request.sign(signer)
    receive = ReceiveSecretRequest(sender=sender, **params)
    message_handler.on_message(mock_raiden, secret_request)
    assert_method_call(mock_raiden, "handle_and_track_state_changes", [receive])

    secret = factories.make_secret()
    reveal_secret = RevealSecret(
        message_identifier=100, signature=factories.EMPTY_SIGNATURE, secret=secret
    )
    reveal_secret.sign(signer)
    receive = ReceiveSecretReveal(sender=sender, secret=secret)
    message_handler.on_message(mock_raiden, reveal_secret)
    assert_method_call(mock_raiden, "handle_and_track_state_changes", [receive])

    properties: factories.UnlockProperties = factories.create_properties(
        factories.UnlockProperties()
    )
    unlock = factories.create(properties)
    unlock.sign(signer)
    balance_proof = factories.make_signed_balance_proof_from_unsigned(
        factories.create(properties.balance_proof), signer, unlock.message_hash
    )
    receive = ReceiveUnlock(
        message_identifier=properties.message_identifier,
        secret=properties.secret,
        balance_proof=balance_proof,
        sender=sender,
    )
    message_handler.on_message(mock_raiden, unlock)
    assert_method_call(mock_raiden, "handle_and_track_state_changes", [receive])

    properties: factories.LockExpiredProperties = factories.create_properties(
        factories.LockExpiredProperties()
    )
    lock_expired = factories.create(properties)
    lock_expired.sign(signer)
    balance_proof = factories.make_signed_balance_proof_from_unsigned(
        factories.create(properties.balance_proof), signer, lock_expired.message_hash
    )
    receive = ReceiveLockExpired(
        balance_proof=balance_proof,
        message_identifier=properties.message_identifier,
        secrethash=properties.secrethash,  # pylint: disable=no-member
        sender=sender,
    )
    message_handler.on_message(mock_raiden, lock_expired)
    assert_method_call(mock_raiden, "handle_and_track_state_changes", [receive])

    delivered = Delivered(delivered_message_identifier=1, signature=factories.EMPTY_SIGNATURE)
    delivered.sign(signer)
    receive = ReceiveDelivered(message_identifier=1, sender=sender)
    message_handler.on_message(mock_raiden, delivered)
    assert_method_call(mock_raiden, "handle_and_track_state_changes", [receive])

    processed = Processed(message_identifier=42, signature=factories.EMPTY_SIGNATURE)
    processed.sign(signer)
    receive = ReceiveProcessed(message_identifier=42, sender=sender)
    message_handler.on_message(mock_raiden, processed)
    assert_method_call(mock_raiden, "handle_and_track_state_changes", [receive])
 def handle_message_delivered(raiden: "RaidenService",
                              message: Delivered) -> None:
     assert message.sender, "message must be signed"
     delivered = ReceiveDelivered(message.sender,
                                  message.delivered_message_identifier)
     raiden.handle_and_track_state_changes([delivered])
Exemple #10
0
 def handle_message_delivered(raiden: RaidenService,
                              message: Delivered) -> None:
     delivered = ReceiveDelivered(message.sender,
                                  message.delivered_message_identifier)
     raiden.handle_and_track_state_change(delivered)