CHIA-2776 Simplify DB checks in invariant_check_mempool (#19526)

Simplify DB checks in invariant_check_mempool.
This commit is contained in:
Amine Khaldi
2025-04-21 14:05:24 -05:00
committed by GitHub
parent fcd20ce543
commit 4d027d03e4
+14 -16
View File
@@ -477,24 +477,22 @@ def create_logger(file: TextIO = sys.stdout) -> logging.Logger:
def invariant_check_mempool(mempool: Mempool) -> None:
with mempool._db_conn as conn:
cursor = conn.execute("SELECT COALESCE(SUM(cost), 0), COALESCE(SUM(fee), 0) FROM tx")
val = cursor.fetchone()
assert (mempool._total_cost, mempool._total_fee) == val
cursor = mempool._db_conn.execute("SELECT COALESCE(SUM(cost), 0), COALESCE(SUM(fee), 0) FROM tx")
val = cursor.fetchone()
assert (mempool._total_cost, mempool._total_fee) == val
with mempool._db_conn as conn:
cursor = conn.execute("SELECT coin_id, tx FROM spends")
for coin_id, item_id in cursor.fetchall():
item = mempool._items.get(item_id)
assert item is not None
# item is expected to contain a spend of coin_id, but it might be a
# fast-forward spend, in which case the dictionary won't help us,
# but we'll have to do a linear search
if coin_id in item.bundle_coin_spends:
assert item.bundle_coin_spends[coin_id].coin_spend.coin.name() == coin_id
continue
cursor = mempool._db_conn.execute("SELECT coin_id, tx FROM spends")
for coin_id, item_id in cursor.fetchall():
item = mempool._items.get(item_id)
assert item is not None
# item is expected to contain a spend of coin_id, but it might be a
# fast-forward spend, in which case the dictionary won't help us,
# but we'll have to do a linear search
if coin_id in item.bundle_coin_spends:
assert item.bundle_coin_spends[coin_id].coin_spend.coin.name() == coin_id
continue
assert any(i.latest_singleton_coin == coin_id for i in item.bundle_coin_spends.values())
assert any(i.latest_singleton_coin == coin_id for i in item.bundle_coin_spends.values())
async def wallet_height_at_least(wallet_node: WalletNode, h: uint32) -> bool: