|
19 | 19 | import chia_rs.datalayer
|
20 | 20 | import zstd
|
21 | 21 | from chia_rs.datalayer import (
|
| 22 | + DeltaFileCache, |
22 | 23 | DeltaReader,
|
23 | 24 | KeyAlreadyPresentError,
|
24 | 25 | KeyId,
|
@@ -1632,27 +1633,21 @@ async def get_nodes_for_file(
|
1632 | 1633 | node_hash: bytes32,
|
1633 | 1634 | store_id: bytes32,
|
1634 | 1635 | deltas_only: bool,
|
1635 |
| - merkle_blob: MerkleBlob, |
1636 |
| - hash_to_index: dict[bytes32, TreeIndex], |
1637 |
| - existing_hashes: set[bytes32], |
| 1636 | + delta_file_cache: DeltaFileCache, |
1638 | 1637 | tree_nodes: list[SerializedNode],
|
1639 | 1638 | ) -> None:
|
1640 | 1639 | if deltas_only:
|
1641 |
| - if node_hash in existing_hashes: |
| 1640 | + if delta_file_cache.seen_previous_hash(node_hash): |
1642 | 1641 | return
|
1643 | 1642 |
|
1644 |
| - raw_index = hash_to_index[node_hash] |
1645 |
| - raw_node = merkle_blob.get_raw_node(raw_index) |
| 1643 | + raw_index = delta_file_cache.get_index(node_hash) |
| 1644 | + raw_node = delta_file_cache.get_raw_node(raw_index) |
1646 | 1645 |
|
1647 | 1646 | if isinstance(raw_node, chia_rs.datalayer.InternalNode):
|
1648 |
| - left_hash = merkle_blob.get_hash_at_index(raw_node.left) |
1649 |
| - right_hash = merkle_blob.get_hash_at_index(raw_node.right) |
1650 |
| - await self.get_nodes_for_file( |
1651 |
| - root, left_hash, store_id, deltas_only, merkle_blob, hash_to_index, existing_hashes, tree_nodes |
1652 |
| - ) |
1653 |
| - await self.get_nodes_for_file( |
1654 |
| - root, right_hash, store_id, deltas_only, merkle_blob, hash_to_index, existing_hashes, tree_nodes |
1655 |
| - ) |
| 1647 | + left_hash = delta_file_cache.get_hash_at_index(raw_node.left) |
| 1648 | + right_hash = delta_file_cache.get_hash_at_index(raw_node.right) |
| 1649 | + await self.get_nodes_for_file(root, left_hash, store_id, deltas_only, delta_file_cache, tree_nodes) |
| 1650 | + await self.get_nodes_for_file(root, right_hash, store_id, deltas_only, delta_file_cache, tree_nodes) |
1656 | 1651 | tree_nodes.append(SerializedNode(False, bytes(left_hash), bytes(right_hash)))
|
1657 | 1652 | elif isinstance(raw_node, chia_rs.datalayer.LeafNode):
|
1658 | 1653 | tree_nodes.append(
|
@@ -1703,20 +1698,20 @@ async def write_tree_to_file(
|
1703 | 1698 | if node_hash == bytes32.zeros:
|
1704 | 1699 | return
|
1705 | 1700 |
|
1706 |
| - merkle_blob = await self.get_merkle_blob(store_id=store_id, root_hash=root.node_hash) |
1707 |
| - hash_to_index = merkle_blob.get_hashes_indexes() |
1708 |
| - if root.generation == 0: |
1709 |
| - existing_hashes = set() |
1710 |
| - else: |
| 1701 | + with log_exceptions(log=log, message="Error while getting merkle blob"): |
| 1702 | + root_path = self.get_merkle_path(store_id=store_id, root_hash=root.node_hash) |
| 1703 | + delta_file_cache = DeltaFileCache(root_path) |
| 1704 | + |
| 1705 | + if root.generation > 0: |
1711 | 1706 | previous_root = await self.get_tree_root(store_id=store_id, generation=root.generation - 1)
|
1712 |
| - previous_merkle_blob = await self.get_merkle_blob(store_id=store_id, root_hash=previous_root.node_hash) |
1713 |
| - previous_hashes_indexes = previous_merkle_blob.get_hashes_indexes() |
1714 |
| - existing_hashes = {hash for hash in previous_hashes_indexes.keys()} |
| 1707 | + if previous_root.node_hash is not None: |
| 1708 | + with log_exceptions(log=log, message="Error while getting previous merkle blob"): |
| 1709 | + previous_root_path = self.get_merkle_path(store_id=store_id, root_hash=previous_root.node_hash) |
| 1710 | + delta_file_cache.load_previous_hashes(previous_root_path) |
| 1711 | + |
1715 | 1712 | tree_nodes: list[SerializedNode] = []
|
1716 | 1713 |
|
1717 |
| - await self.get_nodes_for_file( |
1718 |
| - root, node_hash, store_id, deltas_only, merkle_blob, hash_to_index, existing_hashes, tree_nodes |
1719 |
| - ) |
| 1714 | + await self.get_nodes_for_file(root, node_hash, store_id, deltas_only, delta_file_cache, tree_nodes) |
1720 | 1715 | kv_ids = (
|
1721 | 1716 | KeyOrValueId.from_bytes(raw_id)
|
1722 | 1717 | for node in tree_nodes
|
|
0 commit comments