def _receive_message(self, message: SignedMessage): self.log.info( 'MESSAGE RECEIVED', node=pex(self._raiden_service.address), message=message, sender=pex(message.sender), ) def send_delivered_for(message: SignedMessage): delivered_message = Delivered(message.message_identifier) self._raiden_service.sign(delivered_message) self._send_raw(message.sender, JSONSerializer.serialize(delivered_message)) try: # TODO: Maybe replace with Matrix read receipts. # Unfortunately those work on an 'up to' basis, not on individual messages # which means that message order is important which isn't guaranteed between # federated servers. # See: https://matrix.org/docs/spec/client_server/r0.3.0.html#id57 if not isinstance(message, Processed): self._spawn(send_delivered_for, message) on_message(self._raiden_service, message) except (InvalidAddress, UnknownAddress, UnknownTokenAddress): self.log.warning('Exception while processing message', exc_info=True) return
def test_close_channel_lack_of_balance_proof(raiden_chain, deposit, token_addresses): app0, app1 = raiden_chain token_address = token_addresses[0] token_network_identifier = views.get_token_network_identifier_by_token_address( views.state_from_app(app0), app0.raiden.default_registry.address, token_address, ) token_proxy = app0.raiden.chain.token(token_address) initial_balance0 = token_proxy.balance_of(app0.raiden.address) initial_balance1 = token_proxy.balance_of(app1.raiden.address) amount = 100 identifier = 1 secret = pending_mediated_transfer( raiden_chain, token_network_identifier, amount, identifier, ) # Stop app0 to avoid sending the unlock app0.raiden.transport.stop_and_wait() reveal_secret = RevealSecret( random.randint(0, UINT64_MAX), secret, ) app0.raiden.sign(reveal_secret) message_handler.on_message(app1.raiden, reveal_secret) RaidenAPI(app0.raiden).channel_close( app0.raiden.default_registry.address, token_address, app1.raiden.address, ) channel_state = get_channelstate(app0, app1, token_network_identifier) waiting.wait_for_settle( app0.raiden, app0.raiden.default_registry.address, token_address, [channel_state.identifier], app0.raiden.alarm.sleep_time, ) # wait for the node to call batch unlock with gevent.Timeout(10): wait_for_batch_unlock( app0, token_network_identifier, channel_state.partner_state.address, channel_state.our_state.address, ) expected_balance0 = initial_balance0 + deposit - amount expected_balance1 = initial_balance1 + deposit + amount assert token_proxy.balance_of(app0.raiden.address) == expected_balance0 assert token_proxy.balance_of(app1.raiden.address) == expected_balance1
def receive_message(self, message: Message): """ Handle a Raiden protocol message. The protocol requires durability of the messages. The UDP transport relies on the node's WAL for durability. The message will be converted to a state change, saved to the WAL, and *processed* before the durability is confirmed, which is a stronger property than what is required of any transport. """ # pylint: disable=unidiomatic-typecheck if on_message(self.raiden, message): # Sending Delivered after the message is decoded and *processed* # gives a stronger guarantee than what is required from a # transport. # # Alternatives are, from weakest to strongest options: # - Just save it on disk and asynchronously process the messages # - Decode it, save to the WAL, and asynchronously process the # state change # - Decode it, save to the WAL, and process it (the current # implementation) delivered_message = Delivered(message.message_identifier) self.raiden.sign(delivered_message) self.maybe_send( message.sender, delivered_message, )
def _receive_message(self, message): self.log.info( 'MESSAGE RECEIVED', node=pex(self._raiden_service.address), message=message, message_sender=pex(message.sender), ) try: if on_message(self._raiden_service, message): # TODO: Maybe replace with Matrix read receipts. # Unfortunately those work on an 'up to' basis, not on individual messages # which means that message order is important which isn't guaranteed between # federated servers. # See: https://matrix.org/docs/spec/client_server/r0.3.0.html#id57 delivered_message = Delivered(message.message_identifier) self._raiden_service.sign(delivered_message) self._send_immediate(message.sender, json.dumps(delivered_message.to_dict())) except (InvalidAddress, UnknownAddress, UnknownTokenAddress): self.log.warn('Exception while processing message', exc_info=True) return self.log.debug( 'DELIVERED', node=pex(self._raiden_service.address), to=pex(message.sender), message_identifier=message.message_identifier, )
def test_automatic_secret_registration(raiden_chain, deposit, token_addresses, reveal_timeout): app0, app1 = raiden_chain token_address = token_addresses[0] token_network_identifier = views.get_token_network_identifier_by_token_address( views.state_from_app(app0), app0.raiden.default_registry.address, token_address, ) amount = 100 identifier = 1 secret = pending_mediated_transfer( raiden_chain, token_network_identifier, amount, identifier, ) # Stop app0 to avoid sending the unlock app0.raiden.transport.stop() reveal_secret = RevealSecret( random.randint(0, UINT64_MAX), secret, ) app0.raiden.sign(reveal_secret) message_handler.on_message(app1.raiden, reveal_secret) chain_state = views.state_from_app(app1) secrethash = sha3(secret) target_task = chain_state.payment_mapping.secrethashes_to_task[secrethash] lock_expiration = target_task.target_state.transfer.lock.expiration wait_until_block(app1.raiden.chain, lock_expiration) assert app1.raiden.default_secret_registry.check_registered(secrethash)
def _receive_message(self, message): self.log.info( 'MESSAGE RECEIVED', node=pex(self._raiden_service.address), message=message, sender=pex(message.sender), ) try: if on_message(self._raiden_service, message) and not isinstance(message, Processed): # TODO: Maybe replace with Matrix read receipts. # Unfortunately those work on an 'up to' basis, not on individual messages # which means that message order is important which isn't guaranteed between # federated servers. # See: https://matrix.org/docs/spec/client_server/r0.3.0.html#id57 delivered_message = Delivered(message.message_identifier) self._raiden_service.sign(delivered_message) self._send_immediate(message.sender, json.dumps(delivered_message.to_dict())) except (InvalidAddress, UnknownAddress, UnknownTokenAddress): self.log.warn('Exception while processing message', exc_info=True) return
def sign_and_inject(message, key, address, app): """Sign the message with key and inject it directly in the app transport layer.""" message.sign(key) on_message(app.raiden, message)