Esempio n. 1
0
    def start(self, known_ids=None):
        """Subscribes to state delta events, and then waits to receive deltas.
        Sends any events received to delta handlers.
        """
        if not known_ids:
            known_ids = [NULL_BLOCK_ID]

        self._stream.wait_for_ready()
        LOGGER.debug('Subscribing to state delta events')

        block_sub = EventSubscription(event_type='sawtooth/block-commit')
        delta_sub = EventSubscription(
            event_type='sawtooth/state-delta',
            filters=[
                EventFilter(key='address',
                            match_string='^{}.*'.format(NAMESPACE),
                            filter_type=EventFilter.REGEX_ANY)
            ])

        request = ClientEventsSubscribeRequest(
            last_known_block_ids=known_ids,
            subscriptions=[block_sub, delta_sub])
        response_future = self._stream.send(
            Message.CLIENT_EVENTS_SUBSCRIBE_REQUEST,
            request.SerializeToString())
        response = ClientEventsSubscribeResponse()
        response.ParseFromString(response_future.result().content)

        # Forked all the way back to genesis, restart with no known_ids
        if (response.status == ClientEventsSubscribeResponse.UNKNOWN_BLOCK
                and known_ids):
            return self.start()

        if response.status != ClientEventsSubscribeResponse.OK:
            raise RuntimeError('Subscription failed with status: {}'.format(
                ClientEventsSubscribeResponse.Status.Name(response.status)))

        self._is_active = True

        LOGGER.debug('Successfully subscribed to state delta events')
        while self._is_active:
            message_future = self._stream.receive()

            event_list = EventList()
            event_list.ParseFromString(message_future.result().content)
            for handler in self._event_handlers:
                handler(event_list.events)
Esempio n. 2
0
    def subscribe_to_event(self):

        LOGGER.info("Subscribing to event {}".format(self._event))

        done_sub = EventSubscription(event_type=self._event)
        request = ClientEventsSubscribeRequest(subscriptions=[done_sub])

        response_future = self._stream.send(
            Message.CLIENT_EVENTS_SUBSCRIBE_REQUEST,
            request.SerializeToString())

        sub_response = ClientEventsSubscribeResponse()
        sub_response.ParseFromString(response_future.result().content)

        if sub_response.status == ClientEventsSubscribeResponse.OK:
            self._is_active = True
            LOGGER.info('Subscription with response {}'.format(
                sub_response.status))
        else:
            LOGGER.error('Could not Subscribe to event')
            raise SubscriberError()
Esempio n. 3
0
    def listen_to_event(self):
        self._stream.wait_for_ready()
        # Step 1: Construct a Subscription
        block_sub = EventSubscription(event_type='sawtooth/block-commit')

        # Step 2: Submit the Event Subscription
        request = ClientEventsSubscribeRequest(subscriptions=[block_sub])

        response_future = self._stream.send(
            Message.CLIENT_EVENTS_SUBSCRIBE_REQUEST,
            request.SerializeToString())

        response = ClientEventsSubscribeResponse()
        response.ParseFromString(response_future.result().content)

        # Listen for events in an infinite loop
        LOGGER.warning("Listening to events.")
        while True:
            msg = self._stream.receive()
            event_list = EventList()
            event_list.ParseFromString(msg.result().content)
            for handler in self._event_handlers:
                handler(event_list.events)