fix: Remove cursors

This commit is contained in:
Nathan Woodburn 2025-02-09 21:56:18 +11:00
parent 6d6eae297d
commit 1e783c9775
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
2 changed files with 44 additions and 87 deletions

View File

@ -228,6 +228,9 @@ class Covenant:
self.renewalCount = self.items[5]
self.blockHash = self.items[6]
# TYPE 11 - REVOKE (Only has namehash and height)
else:
raise ValueError("Invalid data type")

94
main.py
View File

@ -110,13 +110,10 @@ def saveTransactions(txList, blockHeight):
query = """
INSERT INTO transactions (hash, witnessHash, fee, rate, mtime, block, tx_index, version,
inputs, outputs, locktime, hex)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE hash=hash
VALUES
"""
return dbSave.execute(query, txValues)
with dbSave.cursor() as cursor:
cursor.executemany(query, txValues)
dbSave.commit()
def saveBlock(blockData):
hashes = [tx["hash"] for tx in blockData["txs"]]
@ -128,8 +125,7 @@ def saveBlock(blockData):
query = """
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)
ON DUPLICATE KEY UPDATE hash=hash
VALUES
"""
blockValues = (
@ -140,18 +136,15 @@ def saveBlock(blockData):
blockData["mask"], json.dumps(hashes)
)
with dbSave.cursor() as cursor:
cursor.execute(query, blockValues)
dbSave.commit()
dbSave.execute(query, blockValues)
print('')
def setupDB():
"""Creates the database tables"""
with dbSave.cursor() as cursor:
cursor.execute("CREATE TABLE IF NOT EXISTS blocks ( hash String, height UInt64, depth Int32, version Int32, prevBlock String, merkleRoot String, witnessRoot String, treeRoot String, reservedRoot String, time UInt32, bits Int32, nonce UInt64, extraNonce String, mask String, txs String ) ENGINE = MergeTree() ORDER BY (hash, height)")
cursor.execute("CREATE TABLE IF NOT EXISTS transactions ( hash String, witnessHash String, fee Int64, rate Int64, mtime Int64, block UInt64, tx_index Int32, version Int32, inputs String, outputs String, locktime Int64, hex String ) ENGINE = MergeTree() ORDER BY (hash, block)")
cursor.execute("CREATE TABLE IF NOT EXISTS names ( name String, nameHash String, state String, height UInt64, lastRenewal Int64, owner String, value Int64, highest Int64, data String, transfer Int64, revoked Int64, claimed Int64, renewals Int64, registered UInt8, expired UInt8, weak UInt8, stats String, start String, txs String, bids String ) ENGINE = MergeTree() ORDER BY (name, height)")
# def setupDB():
# """Creates the database tables"""
# dbSave.execute("CREATE TABLE IF NOT EXISTS blocks ( hash String, height UInt64, depth Int32, version Int32, prevBlock String, merkleRoot String, witnessRoot String, treeRoot String, reservedRoot String, time UInt32, bits Int32, nonce UInt64, extraNonce String, mask String, txs String ) ENGINE = MergeTree() ORDER BY (hash, height)")
# dbSave.execute("CREATE TABLE IF NOT EXISTS transactions ( hash String, witnessHash String, fee Int64, rate Int64, mtime Int64, block UInt64, tx_index Int32, version Int32, inputs String, outputs String, locktime Int64, hex String ) ENGINE = MergeTree() ORDER BY (hash, block)")
# dbSave.execute("CREATE TABLE IF NOT EXISTS names ( name String, nameHash String, state String, height UInt64, lastRenewal Int64, owner String, value Int64, highest Int64, data String, transfer Int64, revoked Int64, claimed Int64, renewals Int64, registered UInt8, expired UInt8, weak UInt8, stats String, start String, txs String, bids String ) ENGINE = MergeTree() ORDER BY (name, height)")
# Get the newest block height in the database
def getNewestBlock() -> int:
@ -161,25 +154,18 @@ def getNewestBlock() -> int:
host=DB_HOST,
user=DB_USER,
password=DB_PASSWORD,
database=DB_NAME,
charset='utf8mb4',
collation='utf8mb4_unicode_ci',
database=DB_NAME
)
with dbNB.cursor() as cursor:
cursor.execute("SELECT height FROM blocks ORDER BY height DESC LIMIT 1")
newestBlock = cursor.fetchone()
newestBlock = dbNB.execute("SELECT height FROM blocks ORDER BY height DESC LIMIT 1")
if newestBlock:
return int(newestBlock[0])
dbNB.close()
return -1
def dbCheck():
# For the first 100 blocks, check for transactions
for i in range(100):
with dbGet.cursor() as cursor:
cursor.execute("SELECT * FROM blocks WHERE height = %s", (i,))
block = cursor.fetchone()
block = dbGet.execute("SELECT * FROM blocks WHERE height = %s", (i,))
if not block:
return
block = Block(block)
@ -187,26 +173,20 @@ def dbCheck():
def getBlock(height) -> Block | None:
with dbGet.cursor() as cursor:
cursor.execute("SELECT * FROM blocks WHERE height = %s", (height,))
block = cursor.fetchone()
block = dbGet.execute("SELECT * FROM blocks WHERE height = %s", (height,))
if not block:
return None
return Block(block)
def getTransaction(hash) -> Transaction | None:
with dbGet.cursor() as cursor:
cursor.execute("SELECT * FROM transactions WHERE hash = %s", (hash,))
tx = cursor.fetchone()
tx = dbGet.execute("SELECT * FROM transactions WHERE hash = %s", (hash,))
if not tx:
return None
return Transaction(tx)
def getTransactions(height) -> list[Transaction] | None:
with dbGet.cursor() as cursor:
cursor.execute("SELECT * FROM transactions WHERE block = %s", (height,))
txs = cursor.fetchall()
txs = dbGet.execute("SELECT * FROM transactions WHERE block = %s", (height,))
if not txs:
return None
# Convert to list of Transaction objects
@ -214,9 +194,7 @@ def getTransactions(height) -> list[Transaction] | None:
def getNameFromHash(nameHash):
# Connect to db
with dbGet.cursor() as cursor:
cursor.execute("SELECT * FROM names WHERE nameHash = %s", (nameHash,))
name = cursor.fetchone()
name = dbGet.execute("SELECT * FROM names WHERE nameHash = %s", (nameHash,))
if not name:
return -1
return Name(name)
@ -282,32 +260,9 @@ def getNamesFromBlock(height):
query = """
INSERT INTO names (name, nameHash, state, height, lastRenewal, owner, value, highest, data, transfer, revoked, claimed, renewals, registered, expired, weak, stats, start, txs, bids)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
state=VALUES(state),
height=VALUES(height),
lastRenewal=VALUES(lastRenewal),
owner=VALUES(owner),
value=VALUES(value),
highest=VALUES(highest),
data=VALUES(data),
transfer=VALUES(transfer),
revoked=VALUES(revoked),
claimed=VALUES(claimed),
renewals=VALUES(renewals),
registered=VALUES(registered),
expired=VALUES(expired),
weak=VALUES(weak),
stats=VALUES(stats),
start=VALUES(start),
txs=VALUES(txs),
bids=VALUES(bids)
VALUES
"""
with dbSave.cursor() as cursor:
cursor.executemany(query, queryData)
dbSave.commit()
return 0
return dbSave.execute(query, queryData)
@ -322,9 +277,8 @@ def getNodeHeight():
def getFirstMissingBlock():
"""Finds missing block heights in the database."""
with dbGet.cursor() as cursor:
cursor.execute("SELECT height FROM blocks ORDER BY height ASC")
heights = [row[0] for row in cursor.fetchall()]
height = dbGet.execute("SELECT height FROM blocks ORDER BY height ASC")
heights = [row[0] for row in height]
if not heights:
return 0
@ -394,7 +348,7 @@ class BlockWatcher:
self.block = self.block - 1
else:
# Check if there are any new names
if getNamesFromBlock(height) != 0:
if getNamesFromBlock(height) < 0:
print("Error indexing names")
await asyncio.sleep(self.checkInterval)
@ -519,7 +473,7 @@ def start_flask_in_thread():
if __name__ == "__main__":
# Webserver in background
start_flask_in_thread()
setupDB()
# setupDB()
# Check if DB needs to catch up
newestBlock = getFirstMissingBlock()
NodeHeight = getNodeHeight()