def test_message_operators(): a = Ack(ADDRESS, HASH) b = Ack(ADDRESS, HASH) c = Ack(ADDRESS2, HASH2) assert a == b assert not a != b assert a != c assert not a == c
def test_ack(): echo = sha3(PRIVKEY) ack = Ack(ADDRESS, echo) assert ack.echo == echo data = ack.encode() msghash = sha3(data) decoded_ack = decode(data) assert decoded_ack.echo == ack.echo assert decoded_ack.sender == ack.sender assert sha3(decoded_ack.encode()) == msghash
def test_ack(): echo = sha3(privkey) msg = Ack(address, echo) assert msg.echo == echo d = msg.encode() msghash = sha3(d) msg2 = decode(d) assert msg2.echo == echo assert msg2.sender == address assert sha3(msg2.encode()) == msghash
def test_message_operators(): a = Ack(ADDRESS, HASH) b = Ack(ADDRESS, HASH) c = Ack(ADDRESS2, HASH2) # pylint: disable=unneeded-not assert a == b assert not a != b assert a != c assert not a == c
def receive_message(self, message, echohash): is_debug_log_enabled = log.isEnabledFor(logging.DEBUG) if is_debug_log_enabled: log.info('MESSAGE RECEIVED', node=pex(self.raiden.address), echohash=pex(echohash), message=message, message_sender=pex(message.sender)) try: on_udp_message(self.raiden, message) # only send the Ack if the message was handled without exceptions ack = Ack( self.raiden.address, echohash, ) self.maybe_send_ack( message.sender, ack, ) except (InvalidAddress, UnknownAddress, UnknownTokenAddress) as e: if is_debug_log_enabled: log.warn(str(e)) else: if is_debug_log_enabled: log.debug( 'ACK', node=pex(self.raiden.address), to=pex(message.sender), echohash=pex(echohash), )
def receive(self, data): # ignore large packets if len(data) > self.max_message_size: log.error('receive packet larger than maximum size', length=len(data)) return msghash = sha3(data + self.raiden.address) # check if we handled this message already, if so repeat Ack if msghash in self.msghash_acks: return self._send_ack(*self.msghash_acks[msghash]) # We ignore the sending endpoint as this can not be known w/ UDP message = decode(data) if isinstance(message, Ack): ack_result = self.msghash_asyncresult[message.echo] if ack_result.ready(): if log.isEnabledFor(logging.INFO): log.info( 'DUPLICATED ACK RECEIVED node:%s [echo=%s]', pex(self.raiden.address), pex(message.echo) ) else: if log.isEnabledFor(logging.INFO): log.info( 'ACK RECEIVED node:%s [echo=%s]', pex(self.raiden.address), pex(message.echo) ) ack_result.set(True) elif message is not None: assert isinstance(message, Secret) or message.sender # this might exit with an exception self.raiden.on_message(message, msghash) # only send the Ack if the message was handled without exceptions ack = Ack( self.raiden.address, msghash, ) self.send_ack( message.sender, ack, ) else: # payload was not a valid message and decoding failed if log.isEnabledFor(logging.ERROR): log.error( 'could not decode message %s', pex(data), )
def on_message(self, message, msghash): """ Handles `message` and sends a ACK on success. """ cmdid = message.cmdid # using explicity dispatch to make the code grepable if cmdid == messages.ACK: pass elif cmdid == messages.PING: self.message_ping(message) elif cmdid == messages.SECRETREQUEST: self.message_secretrequest(message) elif cmdid == messages.SECRET: self.message_secret(message) elif cmdid == messages.DIRECTTRANSFER: self.message_directtransfer(message) elif cmdid == messages.MEDIATEDTRANSFER: self.message_mediatedtransfer(message) elif cmdid == messages.REFUNDTRANSFER: self.message_refundtransfer(message) elif cmdid == messages.TRANSFERTIMEOUT: self.message_transfertimeout(message) elif cmdid == messages.CONFIRMTRANSFER: self.message_confirmtransfer(message) else: raise Exception("Unknow cmdid '{}'.".format(cmdid)) ack = Ack( self.raiden.address, msghash, ) self.raiden.protocol.send_ack( message.sender, ack, )
def receive_message(self, message, echohash): if log.isEnabledFor(logging.INFO): log.info( 'MESSAGE RECEIVED', node=pex(self.raiden.address), echohash=pex(echohash), message=message, message_sender=pex(message.sender) ) try: self.raiden.on_message(message, echohash) # only send the Ack if the message was handled without exceptions ack = Ack( self.raiden.address, echohash, ) try: if log.isEnabledFor(logging.DEBUG): log.debug( 'SENDING ACK', node=pex(self.raiden.address), to=pex(message.sender), echohash=pex(echohash), ) self.maybe_send_ack( message.sender, ack, ) except (InvalidAddress, UnknownAddress) as e: log.debug("Couldn't send the ACK", e=e) except (UnknownAddress, InvalidNonce, TransferWhenClosed, TransferUnwanted) as e: if log.isEnabledFor(logging.WARN): log.warn('maybe unwanted transfer', e=e) except (UnknownTokenAddress, InvalidLocksRoot) as e: if log.isEnabledFor(logging.WARN): log.warn(str(e))
def receive_ping(self, ping, echohash): if ping_log.isEnabledFor(logging.DEBUG): ping_log.debug( 'PING RECEIVED', node=pex(self.raiden.address), echohash=pex(echohash), message=ping, sender=pex(ping.sender), ) ack = Ack( self.raiden.address, echohash, ) try: self.maybe_send_ack( ping.sender, ack, ) except (InvalidAddress, UnknownAddress) as e: log.debug("Couldn't send the ACK", e=e)
def test_ack(iterations=ITERATIONS): msg = Ack(ADDRESS, HASH) run_timeit('Ack', msg, iterations=iterations)
def receive(self, data): # ignore large packets if len(data) > self.max_message_size: log.error('receive packet larger than maximum size', length=len(data)) return echohash = sha3(data + self.raiden.address) # check if we handled this message already, if so repeat Ack if echohash in self.echohash_acks: return self._send_ack(*self.echohash_acks[echohash]) # We ignore the sending endpoint as this can not be known w/ UDP message = decode(data) # note down the time we got a message from the address self.last_received_time[message.sender] = time.time() if isinstance(message, Ack): waitack = self.echohash_asyncresult[message.echo] if waitack.ack_result.ready(): if log.isEnabledFor(logging.INFO): log.info( 'DUPLICATED ACK RECEIVED node:%s receiver:%s echohash:%s', pex(self.raiden.address), pex(waitack.receiver_address), pex(message.echo), ) else: if log.isEnabledFor(logging.INFO): log.info('ACK RECEIVED node:%s receiver:%s echohash:%s', pex(self.raiden.address), pex(waitack.receiver_address), pex(message.echo)) waitack.ack_result.set(True) elif message is not None: # all messages require an Ack, to send it back an address is required assert isinstance(message, SignedMessage) if log.isEnabledFor(logging.INFO): log.info( 'MESSAGE RECEIVED node:%s echohash:%s %s', pex(self.raiden.address), pex(echohash), message, ) try: # this might exit with an exception self.raiden.on_message(message, echohash) # only send the Ack if the message was handled without exceptions ack = Ack( self.raiden.address, echohash, ) try: self.send_ack( message.sender, ack, ) except InvalidAddress: log.debug("Couldn't send the ACK") except (UnknownAddress, InvalidNonce, TransferWhenClosed) as e: if log.isEnabledFor(logging.DEBUG): log.debug(str(e)) except (UnknownTokenAddress, InvalidLocksRoot) as e: if log.isEnabledFor(logging.WARN): log.warn(str(e)) except: log.exception('unexpected exception raised.') # payload was not a valid message and decoding failed elif log.isEnabledFor(logging.ERROR): log.error( 'could not decode message %s', pex(data), )
def receive(self, data): if len(data) > UDP_MAX_MESSAGE_SIZE: log.error('receive packet larger than maximum size', length=len(data)) return echohash = sha3(data + self.raiden.address) # check if we handled this message already, if so repeat Ack if echohash in self.receivedhashes_to_acks: return self._send_ack(*self.receivedhashes_to_acks[echohash]) # We ignore the sending endpoint as this can not be known w/ UDP message = decode(data) if isinstance(message, Ack): waitack = self.senthashes_to_states.get(message.echo) if waitack is None: if log.isEnabledFor(logging.INFO): log.info('ACK FOR UNKNOWN ECHO node:%s echohash:%s', pex(self.raiden.address), pex(message.echo)) elif waitack.async_result.ready(): if log.isEnabledFor(logging.INFO): log.info( 'DUPLICATED ACK RECEIVED node:%s receiver:%s echohash:%s', pex(self.raiden.address), pex(waitack.receiver_address), pex(message.echo), ) else: if log.isEnabledFor(logging.INFO): log.info('ACK RECEIVED node:%s receiver:%s echohash:%s', pex(self.raiden.address), pex(waitack.receiver_address), pex(message.echo)) waitack.async_result.set(True) elif message is not None: # all messages require an Ack, to send it back an address is required assert isinstance(message, SignedMessage) if log.isEnabledFor(logging.INFO): log.info( 'MESSAGE RECEIVED node:%s echohash:%s %s', pex(self.raiden.address), pex(echohash), message, ) try: # this might exit with an exception self.raiden.on_message(message, echohash) # only send the Ack if the message was handled without exceptions ack = Ack( self.raiden.address, echohash, ) try: self.send_ack( message.sender, ack, ) except InvalidAddress: log.debug("Couldn't send the ACK") except (UnknownAddress, InvalidNonce, TransferWhenClosed, TransferUnwanted) as e: log.DEV('maybe unwanted transfer', e=e) if log.isEnabledFor(logging.DEBUG): log.debug(str(e)) except (UnknownTokenAddress, InvalidLocksRoot) as e: if log.isEnabledFor(logging.WARN): log.warn(str(e)) except: # pylint: disable=bare-except log.exception('unexpected exception raised.') # payload was not a valid message and decoding failed elif log.isEnabledFor(logging.ERROR): log.error( 'could not decode message %s', pex(data), )
def receive(self, data): # ignore large packets if len(data) > self.max_message_size: log.error('receive packet larger than maximum size', length=len(data)) return echohash = sha3(data + self.raiden.address) # check if we handled this message already, if so repeat Ack if echohash in self.echohash_acks: return self._send_ack(*self.echohash_acks[echohash]) # We ignore the sending endpoint as this can not be known w/ UDP message = decode(data) if isinstance(message, Ack): waitack = self.echohash_asyncresult[message.echo] if waitack.ack_result.ready(): if log.isEnabledFor(logging.INFO): log.info( 'DUPLICATED ACK RECEIVED node:%s receiver:%s echohash:%s', pex(self.raiden.address), pex(waitack.receiver_address), pex(message.echo), ) else: if log.isEnabledFor(logging.INFO): log.info('ACK RECEIVED node:%s receiver:%s echohash:%s', pex(self.raiden.address), pex(waitack.receiver_address), pex(message.echo)) waitack.ack_result.set(True) elif message is not None: # all messages require an Ack, to send it back an address is required assert isinstance(message, SignedMessage) if log.isEnabledFor(logging.INFO): log.info( 'MESSAGE RECEIVED node:%s echohash:%s %s', pex(self.raiden.address), pex(echohash), message, ) # this might exit with an exception self.raiden.on_message(message, echohash) # only send the Ack if the message was handled without exceptions ack = Ack( self.raiden.address, echohash, ) self.send_ack( message.sender, ack, ) else: # payload was not a valid message and decoding failed if log.isEnabledFor(logging.ERROR): log.error( 'could not decode message %s', pex(data), )
def receive(self, data): if len(data) > UDP_MAX_MESSAGE_SIZE: log.error('receive packet larger than maximum size', length=len(data)) return # Repeat the ACK if the message has been handled before echohash = sha3(data + self.raiden.address) if echohash in self.receivedhashes_to_acks: return self._maybe_send_ack(*self.receivedhashes_to_acks[echohash]) message = decode(data) if isinstance(message, Ack): waitack = self.senthashes_to_states.get(message.echo) if waitack is None: if log.isEnabledFor(logging.DEBUG): log.debug( 'ACK FOR UNKNOWN ECHO', node=pex(self.raiden.address), echohash=pex(message.echo), ) else: if log.isEnabledFor(logging.DEBUG): log.debug( 'ACK RECEIVED', node=pex(self.raiden.address), receiver=pex(waitack.receiver_address), echohash=pex(message.echo), ) waitack.async_result.set(True) elif isinstance(message, Ping): if ping_log.isEnabledFor(logging.DEBUG): ping_log.debug( 'PING RECEIVED', node=pex(self.raiden.address), echohash=pex(echohash), message=message, sender=pex(message.sender), ) ack = Ack( self.raiden.address, echohash, ) self.maybe_send_ack( message.sender, ack, ) elif isinstance(message, SignedMessage): if log.isEnabledFor(logging.INFO): log.info('MESSAGE RECEIVED', node=pex(self.raiden.address), echohash=pex(echohash), message=message, message_sender=pex(message.sender)) try: self.raiden.on_message(message, echohash) # only send the Ack if the message was handled without exceptions ack = Ack( self.raiden.address, echohash, ) try: if log.isEnabledFor(logging.DEBUG): log.debug( 'SENDING ACK', node=pex(self.raiden.address), to=pex(message.sender), echohash=pex(echohash), ) self.maybe_send_ack( message.sender, ack, ) except (InvalidAddress, UnknownAddress) as e: log.debug("Couldn't send the ACK", e=e) except (UnknownAddress, InvalidNonce, TransferWhenClosed, TransferUnwanted) as e: log.DEV('maybe unwanted transfer', e=e) except (UnknownTokenAddress, InvalidLocksRoot) as e: if log.isEnabledFor(logging.WARN): log.warn(str(e)) elif log.isEnabledFor(logging.ERROR): log.error( 'Invalid message', message=data.encode('hex'), )