Exemplo n.º 1
0
    async def process_content(self, content_key: ContentKey,
                              content: bytes) -> None:
        if self.content_storage.has_content(content_key):
            local_content = self.content_storage.get_content(content_key)
            if local_content == content:
                self.logger.debug(
                    "Ignoring content we already have: content_key=%s",
                    content_key.hex(),
                )
                return

        self.logger.debug(
            "Processing content: content_key=%s  content=%s",
            content_key.hex(),
            content.hex(),
        )
        content_id = content_key_to_content_id(content_key)

        # TODO: computationally expensive
        hash_tree_root = ssz.get_hash_tree_root(content, sedes=content_sedes)

        known_hash_tree_roots = set(
            self._local_advertisement_db.get_hash_tree_roots_for_content_id(
                content_id, ))

        # We should avoid "polution" of our content database with mismatching
        # roots.  This is a stop gap right now because we will need a mechanism
        # for inserting our own "correct" content into the system even in the
        # case where the existing "network" content doesn't agree on the hash
        # tree root.
        if known_hash_tree_roots and hash_tree_root not in known_hash_tree_roots:
            known_roots_display = "|".join(
                (root.hex() for root in known_hash_tree_roots))
            raise NotImplementedError(
                f"Content hash tree root mismatch: "
                f"content_key={content_key.hex()}  root={hash_tree_root.hex()}  "
                f"known={known_roots_display}")

        self.content_storage.set_content(content_key, content, exists_ok=True)

        advertisement = self._get_or_create_advertisement(
            content_key, hash_tree_root)
        await self._network.broadcast(advertisement)

        self.logger.debug(
            "Processed content: content_key=%s  content=%s",
            content_key.hex(),
            content.hex(),
        )
Exemplo n.º 2
0
    def extract_params(self, request: RPCRequest) -> ContentKey:
        raw_params = extract_params(request)

        validate_params_length(raw_params, 1)

        content_key_hex = raw_params[0]
        content_key = ContentKey(
            validate_and_convert_hexstr(content_key_hex)[0])

        return content_key
Exemplo n.º 3
0
    def extract_params(self, request: RPCRequest) -> Tuple[ContentKey, bytes]:
        raw_params = extract_params(request)

        validate_params_length(raw_params, 2)

        content_key_hex, content_hex = raw_params

        content_key = ContentKey(
            validate_and_convert_hexstr(content_key_hex)[0])
        content = validate_and_convert_hexstr(content_hex)[0]

        return content_key, content
Exemplo n.º 4
0
def header_key(block_hash: Hash32) -> ContentKey:
    return ContentKey(b"\x01" + block_hash)