Пример #1
0
    def set_state(self, entries, timeout=None):
        """
        set an address to a value in the validator's merkle state
        Args:
            entries (dict): dictionary where addresses are the keys and data is
                the value.
            timeout: optional timeout, in seconds

        Returns:
            addresses (list): a list of addresses that were set

        """
        state_entries = [
            state_context_pb2.Entry(address=e, data=entries[e])
            for e in entries
        ]
        request = state_context_pb2.TpStateSetRequest(
            entries=state_entries,
            context_id=self._context_id).SerializeToString()
        response = state_context_pb2.TpStateSetResponse()
        response.ParseFromString(
            self._stream.send(Message.TP_STATE_SET_REQUEST,
                              request).result(timeout).content)
        if response.status == \
                state_context_pb2.TpStateSetResponse.AUTHORIZATION_ERROR:
            addresses = [e.address for e in entries]
            raise AuthorizationException(
                'Tried to set unauthorized address: {}'.format(addresses))
        return response.addresses
Пример #2
0
 def get_state(self, addresses, timeout=None):
     """
     Get the value at a given list of address in the validator's merkle
     state.
     Args:
         addressses (list): the addresss to fetch
         timeout: optional timeout, in seconds
     Returns:
         results (list): a list of Entries (address, data), for the
         addresses that have a value
     """
     request = state_context_pb2.TpStateGetRequest(
         context_id=self._context_id, addresses=addresses)
     response_string = self._stream.send(
         Message.TP_STATE_GET_REQUEST,
         request.SerializeToString()).result(timeout).content
     response = state_context_pb2.TpStateGetResponse()
     response.ParseFromString(response_string)
     if response.status == \
             state_context_pb2.TpStateGetResponse.AUTHORIZATION_ERROR:
         raise AuthorizationException(
             'Tried to get unauthorized address: {}'.format(addresses))
     entries = response.entries if response is not None else []
     results = [e for e in entries if len(e.data) != 0]
     return results
Пример #3
0
    def delete_state(self, addresses, timeout=None):
        """
        delete_state requests that each of the provided addresses be unset
        in validator state. A list of successfully deleted addresses
        is returned.

        Args:
            addresses (list): list of addresses to delete
            timeout: optional timeout, in seconds

        Returns:
            addresses (list): a list of addresses that were deleted

        Raises:
            AuthorizationException
        """
        request = state_context_pb2.TpStateDeleteRequest(
            context_id=self._context_id,
            addresses=addresses).SerializeToString()
        response = state_context_pb2.TpStateDeleteResponse()
        response.ParseFromString(
            self._stream.send(Message.TP_STATE_DELETE_REQUEST,
                              request).result(timeout).content)
        if response.status == \
                state_context_pb2.TpStateDeleteResponse.AUTHORIZATION_ERROR:
            raise AuthorizationException(
                'Tried to delete unauthorized address: {}'.format(addresses))
        return response.addresses
Пример #4
0
    def get_state(self, addresses, timeout=None):
        """
        Get a list of addresses with its data as key-value tuple from state.

        Return list of addresses:
            - if address match requested addresses;
            - excluding address when its data is None.

        Arguments:
            addresses (list): list of address to get state by.

        Raises:
            AuthorizationError: if request address isn't presented in inputs.

        Returns list of StubContextTpStateEntry objects contain addresses with its data.
        """
        for address in addresses:
            if address not in self.inputs:
                raise AuthorizationException(
                    f'Tried to get unauthorized address: {addresses}')

        response = [
            StubContextTpStateEntry(address=address,
                                    data=self._state.get(address))
            for address in addresses
        ]

        return response
Пример #5
0
    def set_state(self, entries, timeout=None):
        """
        Set a list of addresses with its data as key-value tuple to state.

        Arguments:
            entries (dict): address-data as key-value.

        Raises:
            AuthorizationError: if some of entries address isn't presented in outputs.

        Returns list of addresses that were set.
        """
        response = []

        for address, data in entries.items():

            if address not in self.outputs:
                raise AuthorizationException(
                    f'Tried to set unauthorized address: {list(entries.keys())}'
                )

            self._state.update({
                address: data,
            })

            response.append(address)

        return response
Пример #6
0
    def delete_state(self, addresses, timeout=None):
        """
        delete an address in the validator's merkle state
        Args:
            addresses (list): list of addresses
            timeout: optional timeout, in seconds

        Returns:
            addresses (list): a list of addresses that were deleted

        """
        request = state_context_pb2.TpStateDeleteRequest(
            context_id=self._context_id,
            addresses=addresses).SerializeToString()
        response = state_context_pb2.TpStateDeleteResponse()
        response.ParseFromString(
            self._stream.send(Message.TP_STATE_DEL_REQUEST,
                              request).result(timeout).content)
        if response.status == \
                state_context_pb2.TpStateDeleteResponse.AUTHORIZATION_ERROR:
            raise AuthorizationException(
                'Tried to delete unauthorized address: {}'.format(addresses))
        return response.addresses