feat: Updating syncing to be more correct
This commit is contained in:
parent
42f4a59797
commit
26603a4777
70
main.py
70
main.py
@ -62,22 +62,34 @@ def indexBlock(blockHeight):
|
||||
if blockData.status_code != 200:
|
||||
print(f"Error fetching block {blockHeight}: {blockData.status_code}")
|
||||
return -1
|
||||
print(blockHeight,end='')
|
||||
print(blockHeight,end='',flush=True)
|
||||
saveBlock(blockData.json())
|
||||
return 0
|
||||
|
||||
|
||||
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
|
||||
cursor.execute("SELECT * FROM transactions WHERE hash = %s", (txData["hash"],))
|
||||
txExists = cursor.fetchone()
|
||||
if txExists:
|
||||
print('*',end='',flush=True)
|
||||
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"]))
|
||||
db.commit()
|
||||
print('.',end='')
|
||||
dbT.commit()
|
||||
dbT.close()
|
||||
print('.',end='',flush=True)
|
||||
|
||||
|
||||
def saveBlock(blockData):
|
||||
hashes = []
|
||||
@ -85,16 +97,29 @@ def saveBlock(blockData):
|
||||
saveTransaction(tx,blockData["height"])
|
||||
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
|
||||
cursor.execute("SELECT * FROM blocks WHERE height = %s", (blockData["height"],))
|
||||
blockExists = cursor.fetchone()
|
||||
if blockExists:
|
||||
print('-',flush=True)
|
||||
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)))
|
||||
db.commit()
|
||||
print('.')
|
||||
|
||||
dbB.commit()
|
||||
dbB.close()
|
||||
print('')
|
||||
|
||||
def setupDB():
|
||||
"""Creates the database tables"""
|
||||
@ -105,28 +130,25 @@ def setupDB():
|
||||
# Get the newest block height in the database
|
||||
def getNewestBlock() -> int:
|
||||
"""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")
|
||||
newestBlock = cursor.fetchone()
|
||||
if newestBlock:
|
||||
return int(newestBlock[0])
|
||||
|
||||
dbN.close()
|
||||
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():
|
||||
# For the first 100 blocks, check for transactions
|
||||
@ -250,7 +272,7 @@ class BlockWatcher:
|
||||
|
||||
class CatchUp:
|
||||
def __init__(self, currentHeight, targetHeight):
|
||||
self.currentHeight = currentHeight
|
||||
self.currentHeight = currentHeight - 1
|
||||
self.targetHeight = targetHeight
|
||||
self.running = True
|
||||
self.closing = False
|
||||
|
Loading…
Reference in New Issue
Block a user