-
-
Save tomusdrw/ffd102326522d415ddea1b2aa1938cc9 to your computer and use it in GitHub Desktop.
Fetch transactions receipt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ipcPath = process.env['HOME'] + '/.local/share/io.parity.ethereum/jsonrpc.ipc' | |
const net = require('net') | |
const Web3 = require('web3') | |
const web3 = new Web3() | |
web3.setProvider(new web3.providers.IpcProvider(ipcPath, net)) | |
web3.extend({ | |
property: 'parity', | |
methods: [{ | |
name: 'getBlockReceipts', | |
call: 'parity_getBlockReceipts', | |
params: 1 | |
}] | |
}) | |
function parseHrtimeToSeconds (hrtime) { | |
var seconds = (hrtime[0] + (hrtime[1] / 1e9)).toFixed(3) | |
return seconds | |
} | |
async function getBlockTransactionsAsync () { | |
let transactions = [] | |
try { | |
let blocksPromises = [] | |
let index | |
let startTime = process.hrtime() | |
// fetch all blocks async | |
for (index = 4832000; index < 4832100; index++) { | |
// fetch full block, include transactions | |
blocksPromises.push(web3.eth.getBlock(index, true)) | |
} | |
console.info(`getBlockTransactionsAsync waiting for blocks 4832000 - 4832099 requests`) | |
// wait for blocks | |
let resBlocks = await Promise.all(blocksPromises) | |
let elapsedSeconds = parseHrtimeToSeconds(process.hrtime(startTime)) | |
console.info(`getBlockTransactionsAsync got blocks, duration in sec:, ${elapsedSeconds}`) | |
// for every block, handle transactions | |
let startTimeTransactions = process.hrtime() | |
let transactionCount = 0 | |
for (let blockIndex = 0; blockIndex < resBlocks.length;) { | |
let block = resBlocks[blockIndex] | |
if (block) { | |
let txstartTime = process.hrtime() | |
let transactionReceiptPromises = [] | |
// fetch all transactions async | |
for (let index = 0; index < block.transactions.length; index++) { | |
transactionReceiptPromises.push(web3.eth.getTransactionReceipt(block.transactions[index].hash)) | |
} | |
console.info(`wait for ${block.transactions.length} transactions receipt requests .`) | |
let resTransactionReceipt = await Promise.all(transactionReceiptPromises) | |
let elapsedTxSeconds = parseHrtimeToSeconds(process.hrtime(txstartTime)) | |
console.info(`getTransactionsFromBlockAsync for block: ${block.number}, txs: ${resTransactionReceipt.length}, duration in sec:, ${elapsedTxSeconds}`) | |
let receiptStartTime = process.hrtime() | |
let resReceipts = await web3.parity.getBlockReceipts(web3.utils.toHex(block.number)) | |
let elapsedReceiptSeconds = parseHrtimeToSeconds(process.hrtime(receiptStartTime)) | |
console.info(`parity_getBlockReceipts for block ${block.number}, receipts: ${resReceipts.length}, duration in sec: ${elapsedReceiptSeconds}`) | |
transactionCount += block.transactions.length | |
blockIndex++ | |
} | |
} | |
let elapsedSecondsTransactions = parseHrtimeToSeconds(process.hrtime(startTimeTransactions)) | |
console.info(`getting ${transactionCount} txs, duration in sec:, ${elapsedSecondsTransactions}`) | |
} catch (error) { | |
console.error(error) | |
} | |
} | |
getBlockTransactionsAsync().then(async () => { | |
console.info('done') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment