feat: Speed up db pushing
This commit is contained in:
parent
fb28968550
commit
78cb528e45
70
main.py
70
main.py
@ -76,37 +76,57 @@ def indexBlock(blockHeight):
|
||||
return 0
|
||||
|
||||
|
||||
def saveTransaction(txData,blockHeight):
|
||||
def saveTransactions(txList, blockHeight):
|
||||
if not txList:
|
||||
return
|
||||
|
||||
# Prepare data for batch insert
|
||||
txValues = []
|
||||
for txData in txList:
|
||||
txValues.append((
|
||||
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"]
|
||||
))
|
||||
|
||||
# Bulk insert transactions
|
||||
query = """
|
||||
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)
|
||||
ON DUPLICATE KEY UPDATE hash=hash
|
||||
"""
|
||||
|
||||
with dbSave.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"]))
|
||||
cursor.executemany(query, txValues)
|
||||
dbSave.commit()
|
||||
|
||||
print('.',end='',flush=True)
|
||||
|
||||
print('.', end='', flush=True)
|
||||
|
||||
def saveBlock(blockData):
|
||||
hashes = []
|
||||
for tx in blockData["txs"]:
|
||||
saveTransaction(tx,blockData["height"])
|
||||
hashes.append(tx["hash"])
|
||||
hashes = [tx["hash"] for tx in blockData["txs"]]
|
||||
|
||||
# Bulk save transactions
|
||||
saveTransactions(blockData["txs"], blockData["height"])
|
||||
|
||||
# Insert block if it doesn't exist
|
||||
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
|
||||
"""
|
||||
|
||||
blockValues = (
|
||||
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)
|
||||
)
|
||||
|
||||
# Create a new connection
|
||||
with dbSave.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)))
|
||||
cursor.execute(query, blockValues)
|
||||
|
||||
dbSave.commit()
|
||||
print('')
|
||||
|
Loading…
Reference in New Issue
Block a user