def on_raiden_events(self, raiden: RaidenService, chain_state: ChainState, events: List[RaidenEvent]): events_to_dispatch = list() for event in events: for hook in self.pre_hooks: hook(event) event_type = type(event) # First check that there are no overlapping holds, otherwise the test # is likely flaky. It should either reuse the hold for the same event # or different holds must match a unique event. for hold in self.eventtype_to_holdings[event_type]: if check_nested_attrs(event, hold.attributes): msg = ( f"Matching event of type {event.__class__.__name__} emitted " f"twice, this should not happen. Either there is a bug in the " f"state machine or the hold.attributes is too generic and " f"multiple different events are matching. Event: {event} " f"Attributes: {hold.attributes}") raise RuntimeError(msg) waitingholds = self.eventtype_to_waitingholds[event_type] for pos, waiting_hold in enumerate(waitingholds): # If it is a match: # - Delete the waiting hold and add it to the holding # - Do not dispatch the event # - Notify the test by setting the async_result if check_nested_attrs(event, waiting_hold.attributes): holding = Holding( event=event, chain_state=chain_state, event_type=waiting_hold.event_type, async_result=waiting_hold.async_result, attributes=waiting_hold.attributes, ) del self.eventtype_to_waitingholds[event_type][pos] self.eventtype_to_holdings[event_type].append(holding) waiting_hold.async_result.set(event) break else: # Only dispatch the event if it didn't match any of the holds events_to_dispatch.append(event) if events_to_dispatch: self.wrapped.on_raiden_events(raiden, chain_state, events_to_dispatch)
def on_message(self, raiden: RaidenService, message: Message): # First handle the message, and then set the events, to ensure the # expected side-effects of the message is applied super().on_message(raiden, message) for waiting in self.waiting[type(message)]: if check_nested_attrs(message, waiting.attributes): waiting.message_received_event.set()
def on_message(self, raiden: RaidenService, message: Message): # First handle the message, and then set the events, to ensure the # expected side-effects of the message is applied super().on_message(raiden, message) for waiting in self.waiting[type(message)]: if check_nested_attrs(message, waiting.attributes): waiting.message_received_event.set()
def on_messages(self, raiden: RaidenService, messages: List[Message]) -> None: # First handle the message, and then set the events, to ensure the # expected side-effects of the message are applied super().on_messages(raiden, messages) for message in messages: for waiting in self.waiting[type(message)]: if check_nested_attrs(message, waiting.attributes): waiting.async_result.set(message)
def on_raiden_event(self, raiden: RaidenService, event: RaidenEvent): holds = self.eventtype_to_holds[type(event)] found = None for pos, hold in enumerate(holds): if check_nested_attrs(event, hold.attributes): msg = ('Same event emitted twice, should not happen. ' 'Either there is a bug in the state machine or ' 'the hold.attributes is too generic and multiple ' 'different events are matching.') assert hold.event is None, msg newhold = hold._replace(event=event) found = (pos, newhold) break if found is not None: hold = found[1] holds[found[0]] = found[1] hold.async_result.set(event) else: super().on_raiden_event(raiden, event)
def on_message(self, raiden: RaidenService, message: Message): super().on_message(raiden, message) for waiting in self.waiting[type(message)]: if check_nested_attrs(message, waiting.attributes): waiting.message_received_event.set()