Skip to content

Commit

Permalink
fix: check for view not table for openedcommitmentsall and don't cras…
Browse files Browse the repository at this point in the history
…h script
  • Loading branch information
Evan-Kim2028 committed Nov 22, 2024
1 parent 035d9bc commit da1d99f
Showing 1 changed file with 46 additions and 20 deletions.
66 changes: 46 additions & 20 deletions fetch_l1_txs.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,55 @@ async def get_manager():

def get_transaction_hashes(db: DatabaseConnection) -> list[str]:
"""
Query OpenedCommitmentStored table for transaction hashes after the last processed block
Query OpenedCommitmentStored table for transaction hashes after the last processed block.
Returns empty list if dependencies aren't ready yet.
"""
try:
conn = db.get_connection()
max_block = get_max_block_number(conn, "l1transactions")
print(f'max block number: {max_block}')

with conn.cursor() as cursor:
query = """
SELECT txnhash
FROM openedcommitmentstoredall
WHERE blocknumber > %s
"""
cursor.execute(query, (max_block,))
results = cursor.fetchall()

return [row[0] for row in results] if results else []
except psycopg.errors.UndefinedTable as e:
logger.warning(
f"Table 'openedcommitmentstoredall' does not exist yet: {e}")
return []
with db.transaction():
conn = db.get_connection()

# Get max block number, returns 0 if table doesn't exist
max_block = get_max_block_number(conn, "l1transactions")
logger.info(f'Current max block number: {max_block}')

with conn.cursor() as cursor:
# Check for materialized view without failing
cursor.execute("""
SELECT EXISTS (
SELECT FROM pg_matviews
WHERE matviewname = 'openedcommitmentstoredall'
)
""")
view_exists = cursor.fetchone()[0]

if not view_exists:
logger.info(
"Waiting for openedcommitmentstoredall materialized view to be created")
return []

# Try to query the view
try:
query = """
SELECT txnhash
FROM openedcommitmentstoredall
WHERE blocknumber > %s
"""
cursor.execute(query, (max_block,))
results = cursor.fetchall()

tx_hashes = [row[0] for row in results] if results else []
if tx_hashes:
logger.info(
f"Found {len(tx_hashes)} new transactions to process")
return tx_hashes

except Exception as view_error:
logger.info(
f"View query failed (will retry next iteration): {view_error}")
return []

except Exception as e:
logger.error(f"Error fetching transaction hashes: {e}", exc_info=True)
logger.info(f"Transaction hash fetch skipped (will retry): {e}")
return []


Expand Down

0 comments on commit da1d99f

Please sign in to comment.