Repository: Chia-Network/chia-blockchain
File: chia/data_layer/data_store.py (Python)

Code before:
    ) -> TerminalNode:
        key = self.get_blob_from_table_blob(table_blobs[KeyOrValueId(kid.raw)], store_id)
        value = self.get_blob_from_table_blob(table_blobs[KeyOrValueId(vid.raw)], store_id)
        return TerminalNode(hash=leaf_hash(key, value), key=key, value=value)

    async def get_keys_values(
        self,
        store_id: bytes32,
        root_hash: Union[bytes32, Unspecified] = unspecified,
    ) -> list[TerminalNode]:
        async with self.db_wrapper.reader():
            resolved_root_hash: Optional[bytes32]
            if root_hash is unspecified:
                root = await self.get_tree_root(store_id=store_id)
                resolved_root_hash = root.node_hash
            else:
                resolved_root_hash = root_hash

            try:
                merkle_blob = await self.get_merkle_blob(store_id=store_id, root_hash=resolved_root_hash)
            except MerkleBlobNotFoundError:
                return []

            kv_ids = merkle_blob.get_keys_values()
            kv_ids_unpacked = [KeyOrValueId(id.raw) for pair in kv_ids.items() for id in pair]
            table_blobs = await self.get_table_blobs(kv_ids_unpacked, store_id)

            terminal_nodes: list[TerminalNode] = []
            for kid, vid in kv_ids.items():
                terminal_nodes.append(self.get_terminal_node_from_table_blobs(kid, vid, table_blobs, store_id))

        return terminal_nodes

    async def get_keys_values_compressed(
        self,
        store_id: bytes32,
        root_hash: Union[bytes32, Unspecified] = unspecified,
    ) -> KeysValuesCompressed:
        async with self.db_wrapper.reader():
            resolved_root_hash: Optional[bytes32]
            if root_hash is unspecified:
                root = await self.get_tree_root(store_id=store_id)
                resolved_root_hash = root.node_hash
            else:
                resolved_root_hash = root_hash

            keys_values_hashed: dict[bytes32, bytes32] = {}
            key_hash_to_length: dict[bytes32, int] = {}
            leaf_hash_to_length: dict[bytes32, int] = {}
            if resolved_root_hash is not None:
                try:

Review comment (suggestion):
if `.get_table_blobs()` took an iterable rather than a list then we could avoid generating this list.  it already does `kv_ids = list(set(kv_ids))` right away inside.  i wrote up a way to avoid that with islice too, but threw it away.

anyways, probably none of this is actually a big deal but i figured i'd mention it.  close this discussion if you prefer.

```suggestion
            kv_ids_unpacked = (KeyOrValueId(id.raw) for pair in kv_ids.items() for id in pair)
            table_blobs = await self.get_table_blobs(kv_ids_unpacked, store_id)
```

Code after:
        key = self.get_blob_from_table_blob(table_blobs[KeyOrValueId(kid.raw)], store_id)
        value = self.get_blob_from_table_blob(table_blobs[KeyOrValueId(vid.raw)], store_id)
        return TerminalNode(hash=leaf_hash(key, value), key=key, value=value)

    async def get_keys_values(
        self,
        store_id: bytes32,
        root_hash: Union[bytes32, Unspecified] = unspecified,
    ) -> list[TerminalNode]:
        async with self.db_wrapper.reader():
            resolved_root_hash: Optional[bytes32]
            if root_hash is unspecified:
                root = await self.get_tree_root(store_id=store_id)
                resolved_root_hash = root.node_hash
            else:
                resolved_root_hash = root_hash

            try:
                merkle_blob = await self.get_merkle_blob(store_id=store_id, root_hash=resolved_root_hash)
            except MerkleBlobNotFoundError:
                return []

            kv_ids = merkle_blob.get_keys_values()
            kv_ids_unpacked = (KeyOrValueId(id.raw) for pair in kv_ids.items() for id in pair)
            table_blobs = await self.get_table_blobs(kv_ids_unpacked, store_id)

            terminal_nodes: list[TerminalNode] = []
            for kid, vid in kv_ids.items():
                terminal_nodes.append(self.get_terminal_node_from_table_blobs(kid, vid, table_blobs, store_id))

        return terminal_nodes

    async def get_keys_values_compressed(
        self,
        store_id: bytes32,
        root_hash: Union[bytes32, Unspecified] = unspecified,
    ) -> KeysValuesCompressed:
        async with self.db_wrapper.reader():
            resolved_root_hash: Optional[bytes32]
            if root_hash is unspecified:
                root = await self.get_tree_root(store_id=store_id)
                resolved_root_hash = root.node_hash
            else:
                resolved_root_hash = root_hash

            keys_values_hashed: dict[bytes32, bytes32] = {}
            key_hash_to_length: dict[bytes32, int] = {}
            leaf_hash_to_length: dict[bytes32, int] = {}
            if resolved_root_hash is not None:
                try:
                    merkle_blob = await self.get_merkle_blob(store_id=store_id, root_hash=resolved_root_hash)