feat: Add second db connection

This commit is contained in:
2025-02-06 12:23:19 +11:00
parent 26603a4777
commit fb28968550

63
main.py
View File

@@ -46,7 +46,16 @@ if os.getenv("DB_NAME"):
# MySQL Database Setup # MySQL Database Setup
db = mysql.connector.connect( dbSave = mysql.connector.connect(
host=DB_HOST,
user=DB_USER,
password=DB_PASSWORD,
database=DB_NAME,
charset='utf8mb4',
collation='utf8mb4_unicode_ci',
)
dbGet = mysql.connector.connect(
host=DB_HOST, host=DB_HOST,
user=DB_USER, user=DB_USER,
password=DB_PASSWORD, password=DB_PASSWORD,
@@ -68,16 +77,7 @@ def indexBlock(blockHeight):
def saveTransaction(txData,blockHeight): def saveTransaction(txData,blockHeight):
dbT = mysql.connector.connect( with dbSave.cursor() as cursor:
host=DB_HOST,
user=DB_USER,
password=DB_PASSWORD,
database=DB_NAME,
charset='utf8mb4',
collation='utf8mb4_unicode_ci',
)
with dbT.cursor() as cursor:
# Check if transaction exists in database # Check if transaction exists in database
cursor.execute("SELECT * FROM transactions WHERE hash = %s", (txData["hash"],)) cursor.execute("SELECT * FROM transactions WHERE hash = %s", (txData["hash"],))
txExists = cursor.fetchone() txExists = cursor.fetchone()
@@ -86,8 +86,8 @@ def saveTransaction(txData,blockHeight):
return return
cursor.execute("INSERT INTO transactions (hash, witnessHash, fee, rate, mtime, block, `index`, version, inputs, outputs, locktime, hex) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", (txData["hash"], txData["witnessHash"], txData["fee"], txData["rate"], txData["mtime"], blockHeight, txData["index"], txData["version"], json.dumps(txData["inputs"]), json.dumps(txData["outputs"]), txData["locktime"], txData["hex"])) cursor.execute("INSERT INTO transactions (hash, witnessHash, fee, rate, mtime, block, `index`, version, inputs, outputs, locktime, hex) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", (txData["hash"], txData["witnessHash"], txData["fee"], txData["rate"], txData["mtime"], blockHeight, txData["index"], txData["version"], json.dumps(txData["inputs"]), json.dumps(txData["outputs"]), txData["locktime"], txData["hex"]))
dbT.commit() dbSave.commit()
dbT.close()
print('.',end='',flush=True) print('.',end='',flush=True)
@@ -98,16 +98,7 @@ def saveBlock(blockData):
hashes.append(tx["hash"]) hashes.append(tx["hash"])
# Create a new connection # Create a new connection
dbB = mysql.connector.connect( with dbSave.cursor() as cursor:
host=DB_HOST,
user=DB_USER,
password=DB_PASSWORD,
database=DB_NAME,
charset='utf8mb4',
collation='utf8mb4_unicode_ci',
)
with dbB.cursor() as cursor:
# Check if block exists in database # Check if block exists in database
cursor.execute("SELECT * FROM blocks WHERE height = %s", (blockData["height"],)) cursor.execute("SELECT * FROM blocks WHERE height = %s", (blockData["height"],))
blockExists = cursor.fetchone() blockExists = cursor.fetchone()
@@ -117,13 +108,12 @@ def saveBlock(blockData):
cursor.execute("INSERT INTO blocks (hash, height, depth, version, prevBlock, merkleRoot, witnessRoot, treeRoot, reservedRoot, time, bits, nonce, extraNonce, mask, txs) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", (blockData["hash"], blockData["height"], blockData["depth"], blockData["version"], blockData["prevBlock"], blockData["merkleRoot"], blockData["witnessRoot"], blockData["treeRoot"], blockData["reservedRoot"], blockData["time"], blockData["bits"], blockData["nonce"], blockData["extraNonce"], blockData["mask"], json.dumps(hashes))) cursor.execute("INSERT INTO blocks (hash, height, depth, version, prevBlock, merkleRoot, witnessRoot, treeRoot, reservedRoot, time, bits, nonce, extraNonce, mask, txs) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", (blockData["hash"], blockData["height"], blockData["depth"], blockData["version"], blockData["prevBlock"], blockData["merkleRoot"], blockData["witnessRoot"], blockData["treeRoot"], blockData["reservedRoot"], blockData["time"], blockData["bits"], blockData["nonce"], blockData["extraNonce"], blockData["mask"], json.dumps(hashes)))
dbB.commit() dbSave.commit()
dbB.close()
print('') print('')
def setupDB(): def setupDB():
"""Creates the database tables""" """Creates the database tables"""
with db.cursor() as cursor: with dbSave.cursor() as cursor:
cursor.execute("CREATE TABLE IF NOT EXISTS blocks (hash VARCHAR(64), height BIGINT, depth INT, version INT, prevBlock VARCHAR(64), merkleRoot VARCHAR(64), witnessRoot VARCHAR(64), treeRoot VARCHAR(64), reservedRoot VARCHAR(64), time INT, bits INT, nonce BIGINT UNSIGNED, extraNonce VARCHAR(64), mask VARCHAR(64), txs JSON)") cursor.execute("CREATE TABLE IF NOT EXISTS blocks (hash VARCHAR(64), height BIGINT, depth INT, version INT, prevBlock VARCHAR(64), merkleRoot VARCHAR(64), witnessRoot VARCHAR(64), treeRoot VARCHAR(64), reservedRoot VARCHAR(64), time INT, bits INT, nonce BIGINT UNSIGNED, extraNonce VARCHAR(64), mask VARCHAR(64), txs JSON)")
cursor.execute("CREATE TABLE IF NOT EXISTS transactions (hash VARCHAR(64), witnessHash VARCHAR(64), fee BIGINT, rate BIGINT, mtime BIGINT, block BIGINT, `index` INT, version INT, inputs JSON, outputs JSON, locktime BIGINT, hex LONGTEXT)") cursor.execute("CREATE TABLE IF NOT EXISTS transactions (hash VARCHAR(64), witnessHash VARCHAR(64), fee BIGINT, rate BIGINT, mtime BIGINT, block BIGINT, `index` INT, version INT, inputs JSON, outputs JSON, locktime BIGINT, hex LONGTEXT)")
@@ -131,29 +121,19 @@ def setupDB():
def getNewestBlock() -> int: def getNewestBlock() -> int:
"""Returns the height of the newest block in the database""" """Returns the height of the newest block in the database"""
dbN = mysql.connector.connect( with dbGet.cursor() as cursor:
host=DB_HOST,
user=DB_USER,
password=DB_PASSWORD,
database=DB_NAME,
charset='utf8mb4',
collation='utf8mb4_unicode_ci',
)
with dbN.cursor() as cursor:
cursor.execute("SELECT height FROM blocks ORDER BY height DESC LIMIT 1") cursor.execute("SELECT height FROM blocks ORDER BY height DESC LIMIT 1")
newestBlock = cursor.fetchone() newestBlock = cursor.fetchone()
if newestBlock: if newestBlock:
return int(newestBlock[0]) return int(newestBlock[0])
dbN.close()
return -1 return -1
def dbCheck(): def dbCheck():
# For the first 100 blocks, check for transactions # For the first 100 blocks, check for transactions
for i in range(100): for i in range(100):
with db.cursor() as cursor: with dbGet.cursor() as cursor:
cursor.execute("SELECT * FROM blocks WHERE height = %s", (i,)) cursor.execute("SELECT * FROM blocks WHERE height = %s", (i,))
block = cursor.fetchone() block = cursor.fetchone()
if not block: if not block:
@@ -163,7 +143,7 @@ def dbCheck():
def getBlock(height): def getBlock(height):
with db.cursor() as cursor: with dbGet.cursor() as cursor:
cursor.execute("SELECT * FROM blocks WHERE height = %s", (height,)) cursor.execute("SELECT * FROM blocks WHERE height = %s", (height,))
block = cursor.fetchone() block = cursor.fetchone()
if not block: if not block:
@@ -171,7 +151,7 @@ def getBlock(height):
return Block(block) return Block(block)
def getTransaction(hash): def getTransaction(hash):
with db.cursor() as cursor: with dbGet.cursor() as cursor:
cursor.execute("SELECT * FROM transactions WHERE hash = %s", (hash,)) cursor.execute("SELECT * FROM transactions WHERE hash = %s", (hash,))
tx = cursor.fetchone() tx = cursor.fetchone()
if not tx: if not tx:
@@ -191,7 +171,7 @@ def getNodeHeight():
def getFirstMissingBlock(): def getFirstMissingBlock():
"""Finds missing block heights in the database.""" """Finds missing block heights in the database."""
with db.cursor() as cursor: with dbGet.cursor() as cursor:
cursor.execute("SELECT height FROM blocks ORDER BY height ASC") cursor.execute("SELECT height FROM blocks ORDER BY height ASC")
heights = [row[0] for row in cursor.fetchall()] heights = [row[0] for row in cursor.fetchall()]
@@ -261,6 +241,7 @@ class BlockWatcher:
print(f"New block: {height}") print(f"New block: {height}")
if indexBlock(height) != 0: if indexBlock(height) != 0:
print("Error indexing block") print("Error indexing block")
self.block = self.block - 1
await asyncio.sleep(self.checkInterval) await asyncio.sleep(self.checkInterval)