def test_get_state(self):
        self.mock_stream.send.return_value = self._make_future(
            message_type=Message.CONSENSUS_STATE_GET_RESPONSE,
            content=consensus_pb2.ConsensusStateGetResponse(
                status=consensus_pb2.ConsensusStateGetResponse.OK,
                entries=[
                    consensus_pb2.ConsensusStateEntry(address='address1',
                                                      data=b'data1'),
                    consensus_pb2.ConsensusStateEntry(address='address2',
                                                      data=b'data2')
                ]).SerializeToString())

        entries = self.service.get_state(block_id=b'test',
                                         addresses=['test1', 'test2'])

        self.mock_stream.send.assert_called_with(
            message_type=Message.CONSENSUS_STATE_GET_REQUEST,
            content=consensus_pb2.ConsensusStateGetRequest(
                block_id=b'test', addresses=['test1',
                                             'test2']).SerializeToString())

        self.assertEqual(entries, {
            'address1': b'data1',
            'address2': b'data2',
        })
Пример #2
0
    def get_state(self, block_id, addresses):
        request = consensus_pb2.ConsensusStateGetRequest(
            block_id=block_id,
            addresses=addresses)

        response_type = consensus_pb2.ConsensusStateGetResponse

        response = self._send(
            request=request,
            message_type=Message.CONSENSUS_STATE_GET_REQUEST,
            response_type=response_type)

        status = response.status

        if status == response_type.UNKNOWN_BLOCK:
            raise exceptions.UnknownBlock()

        if status != response_type.OK:
            raise exceptions.ReceiveError(
                'Failed with status {}'.format(status))

        return {
            entry.address: entry.data
            for entry in response.entries
        }