Esempio n. 1
0
    def handle(self, connection_id, message_content):
        request = GetStateDeltaEventsRequest()
        request.ParseFromString(message_content)

        # Create a temporary subscriber for this response
        temp_subscriber = _DeltaSubscriber(connection_id,
                                           request.address_prefixes)
        events = []
        for block_id in request.block_ids:
            try:
                block = self._block_store[block_id]
            except KeyError:
                LOGGER.debug('Ignoring state delete event request for %s...',
                             block_id[:8])
                continue

            try:
                deltas = self._state_delta_store.get_state_deltas(
                    block.header.state_root_hash)
            except KeyError:
                deltas = []

            event = StateDeltaEvent(
                block_id=block_id,
                block_num=block.header.block_num,
                state_root_hash=block.header.state_root_hash,
                previous_block_id=block.header.previous_block_id,
                state_changes=temp_subscriber.deltas_of_interest(deltas))

            events.append(event)

        status = GetStateDeltaEventsResponse.OK if len(events) > 0 else \
            GetStateDeltaEventsResponse.NO_VALID_BLOCKS_SPECIFIED

        ack = GetStateDeltaEventsResponse(status=status, events=events)

        return HandlerResult(
            HandlerStatus.RETURN,
            message_out=ack,
            message_type=self._msg_type)
Esempio n. 2
0
    def test_get_events_ignore_bad_blocks(self):
        """Tests that the GetStateDeltaEventsHandler will return a response
        containing only the events for blocks that exists.
        """
        block_tree_manager = BlockTreeManager()

        delta_store = StateDeltaStore(DictDatabase())

        delta_store.save_state_deltas(
            block_tree_manager.chain_head.state_root_hash, [
                StateChange(address='deadbeef0000000',
                            value='my_genesis_value'.encode(),
                            type=StateChange.SET),
                StateChange(address='a14ea01',
                            value='some other state value'.encode(),
                            type=StateChange.SET)
            ])

        handler = GetStateDeltaEventsHandler(block_tree_manager.block_store,
                                             delta_store)

        request = GetStateDeltaEventsRequest(
            block_ids=[
                block_tree_manager.chain_head.identifier, 'somebadblockid'
            ],
            address_prefixes=['deadbeef']).SerializeToString()

        response = handler.handle('test_conn_id', request)
        self.assertEqual(HandlerStatus.RETURN, response.status)
        self.assertEqual(GetStateDeltaEventsResponse.OK,
                         response.message_out.status)

        chain_head = block_tree_manager.chain_head
        self.assertEqual([
            StateDeltaEvent(block_id=chain_head.identifier,
                            block_num=chain_head.block_num,
                            state_root_hash=chain_head.state_root_hash,
                            previous_block_id=chain_head.previous_block_id,
                            state_changes=[
                                StateChange(address='deadbeef0000000',
                                            value='my_genesis_value'.encode(),
                                            type=StateChange.SET)
                            ])
        ], [event for event in response.message_out.events])
Esempio n. 3
0
    def test_get_events_no_valid_block_ids(self):
        """Tests that the GetStateDeltaEventsHandler will return a response
        with NO_VALID_BLOCKS_SPECIFIED error when no valid blocks are
        specified in the request.
        """
        block_tree_manager = BlockTreeManager()

        delta_store = StateDeltaStore(DictDatabase())

        handler = GetStateDeltaEventsHandler(block_tree_manager.block_store,
                                             delta_store)

        request = GetStateDeltaEventsRequest(
            block_ids=['somebadblockid'],
            address_prefixes=['deadbeef']).SerializeToString()

        response = handler.handle('test_conn_id', request)

        self.assertEqual(HandlerStatus.RETURN, response.status)
        self.assertEqual(GetStateDeltaEventsResponse.NO_VALID_BLOCKS_SPECIFIED,
                         response.message_out.status)