-
Notifications
You must be signed in to change notification settings - Fork 229
/
make_superblocks.py
82 lines (64 loc) · 2.39 KB
/
make_superblocks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import sqlite3
import threading
import time
import logging
from collections import defaultdict
logging.basicConfig(
level=logging.INFO, format="%(asctime)s %(levelname)s - %(message)s"
)
def update_super_blocks():
logging.info("Updating super_blocks table")
max_retries = 3
for i in range(max_retries):
try:
# Connect to SQLite database
conn = sqlite3.connect("blocks.db", timeout=10) # 10 seconds timeout
cursor = conn.cursor()
# Create the table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS super_blocks (
account TEXT PRIMARY KEY,
super_block_count INTEGER
);
"""
)
# Fetch all hash_to_verify and account records from blocks table
cursor.execute("SELECT hash_to_verify, LOWER(account) AS account FROM blocks")
rows = cursor.fetchall()
# Prepare a dictionary to keep counts
super_block_counts = defaultdict(int)
for row in rows:
hash_to_verify, account_to_update = row
last_element = hash_to_verify.split("$")[-1]
hash_uppercase_only = "".join(filter(str.isupper, last_element))
capital_count = len(hash_uppercase_only)
if capital_count >= 50:
super_block_counts[account_to_update] += 1
# Insert all rows in one go
cursor.executemany(
"""
INSERT OR REPLACE INTO super_blocks (account, super_block_count)
VALUES (?, ?)
""",
super_block_counts.items(),
)
# Commit the changes to the database
conn.commit()
# Close the connection
conn.close()
# If successful, break the retry loop
break
except sqlite3.OperationalError as e:
logging.error(f"Database is locked, retrying {i+1}/{max_retries} - {e}")
time.sleep(1) # wait for 1 second before retrying
logging.info("Super_blocks table updated successfully")
def run_db_operations():
while True:
update_super_blocks()
time.sleep(300)
if __name__ == "__main__":
logging.info("Starting super_blocks")
t = threading.Thread(target=run_db_operations, daemon=True)
t.start()
t.join()