Exemple #1
0
    def delete(self, context_id, address_list):
        """Delete the values associated with list of addresses, for a specific
        context referenced by context_id.

        Args:
            context_id (str): the return value of create_context, referencing
                a particular context.
            address_list (list): a list of address strs

        Returns:
            (bool): True if the operation is successful, False if
                the context_id doesn't reference a known context.

        Raises:
            AuthorizationException: Raised when an address in address_list is
                not authorized either by not being in the inputs for the
                txn associated with this context, or it is under a namespace
                but the characters that are under the namespace are not valid
                address characters.
        """

        if context_id not in self._contexts:
            return False

        context = self._contexts[context_id]

        for add in address_list:
            if not self.address_is_valid(address=add):
                raise AuthorizationException(address=add)

        context.delete_direct(address_list)

        return True
Exemple #2
0
    def set(self, context_id, address_value_list):
        """Within a context, sets addresses to a value.

        Args:
            context_id (str): the context id returned by create_context
            address_value_list (list): list of {address: value} dicts

        Returns:
            (bool): True if the operation is successful, False if
                the context_id doesn't reference a known context.

        Raises:
            AuthorizationException if an address is given in the
                address_value_list that was not in the original
                transaction's outputs, or was under a namespace but the
                characters after the namespace are not valid address
                characters.
        """

        if context_id not in self._contexts:
            LOGGER.warning("Context_id not in contexts, %s", context_id)
            return False

        context = self._contexts.get(context_id)

        add_value_dict = {}
        for d in address_value_list:
            for add, val in d.items():
                if not self.address_is_valid(address=add):
                    raise AuthorizationException(address=add)
                add_value_dict[add] = val
        LOGGER.debug("ContextManager::set set_direct(%s)", len(add_value_dict))
        context.set_direct(add_value_dict)
        return True
    def get(self, context_id, address_list):
        """Get the values associated with list of addresses, for a specific
        context referenced by context_id.

        Args:
            context_id (str): the return value of create_context, referencing
                a particular context.
            address_list (list): a list of address strs

        Returns:
            values_list (list): a list of (address, value) tuples

        Raises:
            AuthorizationException: Raised when an address in address_list is
                not authorized either by not being in the inputs for the
                txn associated with this context, or it is under a namespace
                but the characters that are under the namespace are not valid
                address characters.
        """

        if context_id not in self._contexts:
            return []
        for add in address_list:
            if not self.address_is_valid(address=add):
                raise AuthorizationException(address=add)

        context = self._contexts[context_id]

        addresses_in_ctx = [add for add in address_list if add in context]
        addresses_not_in_ctx = list(set(address_list) - set(addresses_in_ctx))

        values = context.get(addresses_in_ctx)
        values_list = list(zip(addresses_in_ctx, values))
        if addresses_not_in_ctx:
            # Validate the addresses that won't be validated by a direct get on
            # the context.
            for address in addresses_not_in_ctx:
                context.validate_read(address)
            address_values, reads = self._find_address_values_in_chain(
                base_contexts=[context_id],
                addresses_to_find=addresses_not_in_ctx)

            values_list.extend(address_values)

            if reads:
                tree = MerkleDatabase(self._database, context.merkle_root)
                add_values = []
                for add in reads:
                    value = None
                    try:
                        value = tree.get(add)
                    except KeyError:
                        # The address is not in the radix tree/merkle tree
                        pass
                    add_values.append((add, value))
                values_list.extend(add_values)

            values_list.sort(key=lambda x: address_list.index(x[0]))

        return values_list