diff --git a/main.py b/main.py index 2222e21..1daa956 100644 --- a/main.py +++ b/main.py @@ -76,38 +76,58 @@ def indexBlock(blockHeight): return 0 -def saveTransaction(txData,blockHeight): - 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"])) - dbSave.commit() +def saveTransactions(txList, blockHeight): + if not txList: + return - print('.',end='',flush=True) - + # 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: + cursor.executemany(query, txValues) + dbSave.commit() + 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(query, blockValues) - 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))) - dbSave.commit() print('')