Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions node/rustchain_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,22 @@ def calculate_table_hash(self, table_name: str) -> str:
if not schema:
return ""

conn = self._get_connection()
cursor = conn.cursor()

pk = schema["pk"]
cursor.execute(f"SELECT * FROM {table_name} ORDER BY {pk} ASC")
rows = cursor.fetchall()
conn = self._get_connection()
try:
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM {table_name} ORDER BY {pk} ASC")
rows = cursor.fetchall()

hasher = hashlib.sha256()
for row in rows:
row_dict = dict(row)
row_str = json.dumps(row_dict, sort_keys=True, separators=(",", ":"))
hasher.update(row_str.encode())
hasher = hashlib.sha256()
for row in rows:
row_dict = dict(row)
row_str = json.dumps(row_dict, sort_keys=True, separators=(",", ":"))
hasher.update(row_str.encode())

conn.close()
return hasher.hexdigest()
return hasher.hexdigest()
finally:
conn.close()

def get_merkle_root(self) -> str:
"""Generates a master Merkle root hash for all synced tables."""
Expand Down Expand Up @@ -262,8 +263,10 @@ def _get_count(self, table_name: str) -> int:
if table_name not in self.SYNC_TABLES:
return 0
conn = self._get_connection()
cursor = conn.cursor()
cursor.execute(f"SELECT COUNT(*) FROM {table_name}")
count = cursor.fetchone()[0]
conn.close()
return int(count)
try:
cursor = conn.cursor()
cursor.execute(f"SELECT COUNT(*) FROM {table_name}")
count = cursor.fetchone()[0]
return int(count)
finally:
conn.close()