From fb2896855038a96465aaa021b89f4b72bbcd2208 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 6 Feb 2025 12:23:19 +1100 Subject: [PATCH] feat: Add second db connection --- main.py | 63 ++++++++++++++++++++------------------------------------- 1 file changed, 22 insertions(+), 41 deletions(-) diff --git a/main.py b/main.py index 1e43d93..2222e21 100644 --- a/main.py +++ b/main.py @@ -46,7 +46,16 @@ if os.getenv("DB_NAME"): # 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, user=DB_USER, password=DB_PASSWORD, @@ -68,16 +77,7 @@ def indexBlock(blockHeight): def saveTransaction(txData,blockHeight): - dbT = mysql.connector.connect( - host=DB_HOST, - user=DB_USER, - password=DB_PASSWORD, - database=DB_NAME, - charset='utf8mb4', - collation='utf8mb4_unicode_ci', - ) - - with dbT.cursor() as cursor: + with dbSave.cursor() as cursor: # Check if transaction exists in database cursor.execute("SELECT * FROM transactions WHERE hash = %s", (txData["hash"],)) txExists = cursor.fetchone() @@ -86,8 +86,8 @@ def saveTransaction(txData,blockHeight): 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"])) - dbT.commit() - dbT.close() + dbSave.commit() + print('.',end='',flush=True) @@ -98,16 +98,7 @@ def saveBlock(blockData): hashes.append(tx["hash"]) # Create a new connection - dbB = mysql.connector.connect( - host=DB_HOST, - user=DB_USER, - password=DB_PASSWORD, - database=DB_NAME, - charset='utf8mb4', - collation='utf8mb4_unicode_ci', - ) - - with dbB.cursor() as cursor: + with dbSave.cursor() as cursor: # Check if block exists in database cursor.execute("SELECT * FROM blocks WHERE height = %s", (blockData["height"],)) 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))) - dbB.commit() - dbB.close() + dbSave.commit() print('') def setupDB(): """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 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: """Returns the height of the newest block in the database""" - dbN = mysql.connector.connect( - host=DB_HOST, - user=DB_USER, - password=DB_PASSWORD, - database=DB_NAME, - charset='utf8mb4', - collation='utf8mb4_unicode_ci', - ) - - with dbN.cursor() as cursor: + with dbGet.cursor() as cursor: cursor.execute("SELECT height FROM blocks ORDER BY height DESC LIMIT 1") newestBlock = cursor.fetchone() if newestBlock: return int(newestBlock[0]) - dbN.close() return -1 def dbCheck(): # For the first 100 blocks, check for transactions for i in range(100): - with db.cursor() as cursor: + with dbGet.cursor() as cursor: cursor.execute("SELECT * FROM blocks WHERE height = %s", (i,)) block = cursor.fetchone() if not block: @@ -163,7 +143,7 @@ def dbCheck(): def getBlock(height): - with db.cursor() as cursor: + with dbGet.cursor() as cursor: cursor.execute("SELECT * FROM blocks WHERE height = %s", (height,)) block = cursor.fetchone() if not block: @@ -171,7 +151,7 @@ def getBlock(height): return Block(block) def getTransaction(hash): - with db.cursor() as cursor: + with dbGet.cursor() as cursor: cursor.execute("SELECT * FROM transactions WHERE hash = %s", (hash,)) tx = cursor.fetchone() if not tx: @@ -191,7 +171,7 @@ def getNodeHeight(): def getFirstMissingBlock(): """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") heights = [row[0] for row in cursor.fetchall()] @@ -261,6 +241,7 @@ class BlockWatcher: print(f"New block: {height}") if indexBlock(height) != 0: print("Error indexing block") + self.block = self.block - 1 await asyncio.sleep(self.checkInterval)