Ethereum: Heuristic to find tx in a block

node v12.22.12
version: master
endpointsharetweet
const BLOCK_NUMBER_TX_COUNT_MAP = { // BLOCK_NUMBER:TX_COUNT 4: 3, // start 5: 3, 6: 3, 7: 3, 8: 3, 9: 3, 10: 3, 11: 3, 12: 3, 13: 3, 14: 3, 15: 3, 16: 3, 17: 3, 18: 3, 19: 3, 20: 4, // this! 21: 4, 22: 4, 23: 4, 24: 4, 25: 4, 26: 4 // end } let n = 0 function isTxInBlock (blockNumber) { console.log(`${++n}: eth_getBlockByNumber(${blockNumber})`) return blockNumber === 20 } function txCountInBlock (blockNumber) { const value = BLOCK_NUMBER_TX_COUNT_MAP[blockNumber] console.log(`${++n}: eth_getTransactionCount(${blockNumber}) -> ${value}`) return value } const JUMP = 5 const startBlock = 4 const endBlock = 26 if (txCountInBlock(startBlock) === txCountInBlock(endBlock)) { // the transaction hasn't been mined yet // keep looking for next block return false } for (let i = startBlock; i < endBlock;) { const inA = isTxInBlock(i) if (inA) { console.log('Found!', i) break } const inB = isTxInBlock(i + JUMP) if (inB) { console.log('Found!', i + JUMP) break } const countA = txCountInBlock(i) const countB = txCountInBlock(i + JUMP) if (countA === countB) { console.log(`Skip ${JUMP} blocks`) i = i + JUMP + 1 } else { i++ } }
Loading…

no comments

    sign in to comment