feat: Updating syncing to be more correct

This commit is contained in:
Nathan Woodburn 2025-02-05 23:43:22 +11:00
parent 42f4a59797
commit 26603a4777
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1

70
main.py
View File

@ -62,22 +62,34 @@ def indexBlock(blockHeight):
if blockData.status_code != 200: if blockData.status_code != 200:
print(f"Error fetching block {blockHeight}: {blockData.status_code}") print(f"Error fetching block {blockHeight}: {blockData.status_code}")
return -1 return -1
print(blockHeight,end='') print(blockHeight,end='',flush=True)
saveBlock(blockData.json()) saveBlock(blockData.json())
return 0 return 0
def saveTransaction(txData,blockHeight): def saveTransaction(txData,blockHeight):
with db.cursor() as cursor: 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:
# 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()
if txExists: if txExists:
print('*',end='',flush=True)
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"]))
db.commit() dbT.commit()
print('.',end='') dbT.close()
print('.',end='',flush=True)
def saveBlock(blockData): def saveBlock(blockData):
hashes = [] hashes = []
@ -85,16 +97,29 @@ def saveBlock(blockData):
saveTransaction(tx,blockData["height"]) saveTransaction(tx,blockData["height"])
hashes.append(tx["hash"]) hashes.append(tx["hash"])
with db.cursor() as cursor: # 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:
# 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()
if blockExists: if blockExists:
print('-',flush=True)
return return
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)))
db.commit()
print('.') dbB.commit()
dbB.close()
print('')
def setupDB(): def setupDB():
"""Creates the database tables""" """Creates the database tables"""
@ -105,28 +130,25 @@ def setupDB():
# Get the newest block height in the database # Get the newest block height in the database
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"""
with db.cursor() as cursor:
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:
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 indexChain():
startBlock = getNewestBlock()
if startBlock == -1:
print("ERROR GETTING NEWEST BLOCK")
sys.exit(1)
count = 0
while True:
if count >= 1000:
break
if indexBlock(startBlock + count) != 0:
break
count += 1
print(f"Indexing from block {startBlock} to {startBlock + count} complete.")
def dbCheck(): def dbCheck():
# For the first 100 blocks, check for transactions # For the first 100 blocks, check for transactions
@ -250,7 +272,7 @@ class BlockWatcher:
class CatchUp: class CatchUp:
def __init__(self, currentHeight, targetHeight): def __init__(self, currentHeight, targetHeight):
self.currentHeight = currentHeight self.currentHeight = currentHeight - 1
self.targetHeight = targetHeight self.targetHeight = targetHeight
self.running = True self.running = True
self.closing = False self.closing = False