Skip to content

Commit 7d077cd

Browse files
authored
DL Code Cleanup (#19782)
* DL code cleanup * Lint.
1 parent 4d188e0 commit 7d077cd

File tree

3 files changed

+19
-197
lines changed

3 files changed

+19
-197
lines changed

chia/_tests/core/data_layer/test_data_store.py

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from chia.data_layer.data_layer_util import (
3030
DiffData,
3131
InternalNode,
32-
Node,
3332
OperationType,
3433
Root,
3534
SerializedNode,
@@ -1279,6 +1278,23 @@ async def mock_http_download(
12791278
assert sinfo.ignore_till == start_timestamp # we don't increase on second failure
12801279

12811280

1281+
async def get_first_generation(data_store: DataStore, node_hash: bytes32, store_id: bytes32) -> Optional[int]:
1282+
async with data_store.db_wrapper.reader() as reader:
1283+
cursor = await reader.execute(
1284+
"SELECT generation FROM nodes WHERE hash = ? AND store_id = ?",
1285+
(
1286+
node_hash,
1287+
store_id,
1288+
),
1289+
)
1290+
1291+
row = await cursor.fetchone()
1292+
if row is None:
1293+
return None
1294+
1295+
return int(row[0])
1296+
1297+
12821298
async def write_tree_to_file_old_format(
12831299
data_store: DataStore,
12841300
root: Root,
@@ -1296,7 +1312,7 @@ async def write_tree_to_file_old_format(
12961312
if hash_to_index is None:
12971313
hash_to_index = merkle_blob.get_hashes_indexes()
12981314

1299-
generation = await data_store.get_first_generation(node_hash, store_id)
1315+
generation = await get_first_generation(data_store, node_hash, store_id)
13001316
# Root's generation is not the first time we see this hash, so it's not a new delta.
13011317
if root.generation != generation:
13021318
return
@@ -2059,35 +2075,6 @@ async def test_update_keys(data_store: DataStore, store_id: bytes32, use_upsert:
20592075
num_keys += new_keys
20602076

20612077

2062-
async def _check_ancestors(
2063-
data_store: DataStore, store_id: bytes32, root_hash: bytes32
2064-
) -> dict[bytes32, Optional[bytes32]]:
2065-
ancestors: dict[bytes32, Optional[bytes32]] = {}
2066-
root_node: Node = await data_store.get_node(root_hash)
2067-
queue: list[Node] = [root_node]
2068-
2069-
while queue:
2070-
node = queue.pop(0)
2071-
if isinstance(node, InternalNode):
2072-
left_node = await data_store.get_node(node.left_hash)
2073-
right_node = await data_store.get_node(node.right_hash)
2074-
ancestors[left_node.hash] = node.hash
2075-
ancestors[right_node.hash] = node.hash
2076-
queue.append(left_node)
2077-
queue.append(right_node)
2078-
2079-
ancestors[root_hash] = None
2080-
for node_hash, ancestor_hash in ancestors.items():
2081-
ancestor_node = await data_store._get_one_ancestor(node_hash, store_id)
2082-
if ancestor_hash is None:
2083-
assert ancestor_node is None
2084-
else:
2085-
assert ancestor_node is not None
2086-
assert ancestor_node.hash == ancestor_hash
2087-
2088-
return ancestors
2089-
2090-
20912078
@pytest.mark.anyio
20922079
async def test_migration_unknown_version(data_store: DataStore, tmp_path: Path) -> None:
20932080
async with data_store.db_wrapper.writer() as writer:

chia/data_layer/data_layer_util.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ async def _dot_dump(
135135
root_hash: bytes32,
136136
) -> str:
137137
terminal_nodes = await data_store.get_keys_values(store_id=store_id, root_hash=root_hash)
138-
internal_nodes = await data_store.get_internal_nodes(store_id=store_id, root_hash=root_hash)
139138

140139
n = 8
141140

@@ -149,16 +148,7 @@ async def _dot_dump(
149148
value = terminal_node.value.hex()
150149
dot_nodes.append(f"""node_{hash} [shape=box, label="{hash[:n]}\\nkey: {key}\\nvalue: {value}"];""")
151150

152-
for internal_node in internal_nodes:
153-
hash = internal_node.hash.hex()
154-
left = internal_node.left_hash.hex()
155-
right = internal_node.right_hash.hex()
156-
dot_nodes.append(f"""node_{hash} [label="{hash[:n]}"]""")
157-
dot_connections.append(f"""node_{hash} -> node_{left} [label="L"];""")
158-
dot_connections.append(f"""node_{hash} -> node_{right} [label="R"];""")
159-
dot_pair_boxes.append(
160-
f"node [shape = box]; {{rank = same; node_{left}->node_{right}[style=invis]; rankdir = LR}}"
161-
)
151+
# TODO: implement for internal nodes. currently this prints only terminal nodes
162152

163153
lines = [
164154
"digraph {",
@@ -171,11 +161,6 @@ async def _dot_dump(
171161
return "\n".join(lines)
172162

173163

174-
def row_to_node(row: aiosqlite.Row) -> Node:
175-
cls = node_type_to_class[row["node_type"]]
176-
return cls.from_row(row=row)
177-
178-
179164
class Status(IntEnum):
180165
PENDING = 1
181166
COMMITTED = 2

chia/data_layer/data_store.py

Lines changed: 0 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
internal_hash,
5757
key_hash,
5858
leaf_hash,
59-
row_to_node,
6059
unspecified,
6160
)
6261
from chia.util.batches import to_batches
@@ -711,22 +710,6 @@ async def get_terminal_nodes_by_hashes(
711710

712711
return terminal_nodes
713712

714-
async def get_first_generation(self, node_hash: bytes32, store_id: bytes32) -> Optional[int]:
715-
async with self.db_wrapper.reader() as reader:
716-
cursor = await reader.execute(
717-
"SELECT generation FROM nodes WHERE hash = ? AND store_id = ?",
718-
(
719-
node_hash,
720-
store_id,
721-
),
722-
)
723-
724-
row = await cursor.fetchone()
725-
if row is None:
726-
return None
727-
728-
return int(row[0])
729-
730713
async def get_existing_hashes(self, node_hashes: list[bytes32], store_id: bytes32) -> set[bytes32]:
731714
result: set[bytes32] = set()
732715
batch_size = min(500, SQLITE_MAX_VARIABLE_NUMBER - 10)
@@ -976,25 +959,6 @@ async def get_roots_between(self, store_id: bytes32, generation_begin: int, gene
976959

977960
return roots
978961

979-
async def get_last_tree_root_by_hash(
980-
self, store_id: bytes32, hash: Optional[bytes32], max_generation: Optional[int] = None
981-
) -> Optional[Root]:
982-
async with self.db_wrapper.reader() as reader:
983-
max_generation_str = "AND generation < :max_generation " if max_generation is not None else ""
984-
node_hash_str = "AND node_hash == :node_hash " if hash is not None else "AND node_hash is NULL "
985-
cursor = await reader.execute(
986-
"SELECT * FROM root WHERE tree_id == :tree_id "
987-
f"{max_generation_str}"
988-
f"{node_hash_str}"
989-
"ORDER BY generation DESC LIMIT 1",
990-
{"tree_id": store_id, "node_hash": hash, "max_generation": max_generation},
991-
)
992-
row = await cursor.fetchone()
993-
994-
if row is None:
995-
return None
996-
return Root.from_row(row=row)
997-
998962
async def get_ancestors(
999963
self,
1000964
node_hash: bytes32,
@@ -1026,35 +990,6 @@ async def get_ancestors(
1026990
)
1027991
return result
1028992

1029-
async def get_internal_nodes(self, store_id: bytes32, root_hash: Optional[bytes32] = None) -> list[InternalNode]:
1030-
async with self.db_wrapper.reader() as reader:
1031-
if root_hash is None:
1032-
root = await self.get_tree_root(store_id=store_id)
1033-
root_hash = root.node_hash
1034-
cursor = await reader.execute(
1035-
"""
1036-
WITH RECURSIVE
1037-
tree_from_root_hash(hash, node_type, left, right, key, value) AS (
1038-
SELECT node.* FROM node WHERE node.hash == :root_hash
1039-
UNION ALL
1040-
SELECT node.* FROM node, tree_from_root_hash WHERE node.hash == tree_from_root_hash.left
1041-
OR node.hash == tree_from_root_hash.right
1042-
)
1043-
SELECT * FROM tree_from_root_hash
1044-
WHERE node_type == :node_type
1045-
""",
1046-
{"root_hash": root_hash, "node_type": NodeType.INTERNAL},
1047-
)
1048-
1049-
internal_nodes: list[InternalNode] = []
1050-
async for row in cursor:
1051-
node = row_to_node(row=row)
1052-
if not isinstance(node, InternalNode):
1053-
raise Exception(f"Unexpected internal node found: {node.hash.hex()}")
1054-
internal_nodes.append(node)
1055-
1056-
return internal_nodes
1057-
1058993
def get_terminal_node_from_table_blobs(
1059994
self,
1060995
kid: KeyId,
@@ -1234,19 +1169,6 @@ async def get_kv_diff_paginated(
12341169
kv_diff,
12351170
)
12361171

1237-
async def get_node_type(self, node_hash: bytes32) -> NodeType:
1238-
async with self.db_wrapper.reader() as reader:
1239-
cursor = await reader.execute(
1240-
"SELECT node_type FROM node WHERE hash == :hash LIMIT 1",
1241-
{"hash": node_hash},
1242-
)
1243-
raw_node_type = await cursor.fetchone()
1244-
1245-
if raw_node_type is None:
1246-
raise Exception(f"No node found for specified hash: {node_hash.hex()}")
1247-
1248-
return NodeType(raw_node_type["node_type"])
1249-
12501172
async def autoinsert(
12511173
self,
12521174
key: bytes,
@@ -1265,14 +1187,6 @@ async def autoinsert(
12651187
root=root,
12661188
)
12671189

1268-
async def get_keys_values_dict(
1269-
self,
1270-
store_id: bytes32,
1271-
root_hash: Union[bytes32, Unspecified] = unspecified,
1272-
) -> dict[bytes, bytes]:
1273-
pairs = await self.get_keys_values(store_id=store_id, root_hash=root_hash)
1274-
return {node.key: node.value for node in pairs}
1275-
12761190
async def get_keys(
12771191
self,
12781192
store_id: bytes32,
@@ -1490,59 +1404,6 @@ async def insert_batch(
14901404
new_root = await self.insert_root_from_merkle_blob(merkle_blob, store_id, status, old_root)
14911405
return new_root.node_hash
14921406

1493-
async def _get_one_ancestor(
1494-
self,
1495-
node_hash: bytes32,
1496-
store_id: bytes32,
1497-
generation: Optional[int] = None,
1498-
) -> Optional[InternalNode]:
1499-
async with self.db_wrapper.reader() as reader:
1500-
if generation is None:
1501-
generation = await self.get_tree_generation(store_id=store_id)
1502-
cursor = await reader.execute(
1503-
"""
1504-
SELECT * from node INNER JOIN (
1505-
SELECT ancestors.ancestor AS hash, MAX(ancestors.generation) AS generation
1506-
FROM ancestors
1507-
WHERE ancestors.hash == :hash
1508-
AND ancestors.tree_id == :tree_id
1509-
AND ancestors.generation <= :generation
1510-
GROUP BY hash
1511-
) asc on asc.hash == node.hash
1512-
""",
1513-
{"hash": node_hash, "tree_id": store_id, "generation": generation},
1514-
)
1515-
row = await cursor.fetchone()
1516-
if row is None:
1517-
return None
1518-
return InternalNode.from_row(row=row)
1519-
1520-
async def _get_one_ancestor_multiple_hashes(
1521-
self,
1522-
node_hashes: list[bytes32],
1523-
store_id: bytes32,
1524-
generation: Optional[int] = None,
1525-
) -> list[InternalNode]:
1526-
async with self.db_wrapper.reader() as reader:
1527-
node_hashes_place_holders = ",".join("?" for _ in node_hashes)
1528-
if generation is None:
1529-
generation = await self.get_tree_generation(store_id=store_id)
1530-
cursor = await reader.execute(
1531-
f"""
1532-
SELECT * from node INNER JOIN (
1533-
SELECT ancestors.ancestor AS hash, MAX(ancestors.generation) AS generation
1534-
FROM ancestors
1535-
WHERE ancestors.hash IN ({node_hashes_place_holders})
1536-
AND ancestors.tree_id == ?
1537-
AND ancestors.generation <= ?
1538-
GROUP BY hash
1539-
) asc on asc.hash == node.hash
1540-
""",
1541-
[*node_hashes, store_id, generation],
1542-
)
1543-
rows = await cursor.fetchall()
1544-
return [InternalNode.from_row(row=row) for row in rows]
1545-
15461407
async def get_node_by_key(
15471408
self,
15481409
key: bytes,
@@ -1568,17 +1429,6 @@ async def get_node_by_key(
15681429
kid = KeyId(kvid)
15691430
return await self.get_terminal_node_from_kid(merkle_blob, kid, store_id)
15701431

1571-
async def get_node(self, node_hash: bytes32) -> Node:
1572-
async with self.db_wrapper.reader() as reader:
1573-
cursor = await reader.execute("SELECT * FROM node WHERE hash == :hash LIMIT 1", {"hash": node_hash})
1574-
row = await cursor.fetchone()
1575-
1576-
if row is None:
1577-
raise Exception(f"Node not found for requested hash: {node_hash.hex()}")
1578-
1579-
node = row_to_node(row=row)
1580-
return node
1581-
15821432
async def get_tree_as_nodes(self, store_id: bytes32) -> Node:
15831433
async with self.db_wrapper.reader():
15841434
root = await self.get_tree_root(store_id=store_id)

0 commit comments

Comments
 (0)