Skip to content

Instantly share code, notes, and snippets.

@jcortejoso
Created August 14, 2024 15:44
Show Gist options
  • Select an option

  • Save jcortejoso/a6a8575b26fb2d32206a11af7c821c41 to your computer and use it in GitHub Desktop.

Select an option

Save jcortejoso/a6a8575b26fb2d32206a11af7c821c41 to your computer and use it in GitHub Desktop.
Check block metrics. Run in geth console
var main = (blocks = 1000) => {
const j = eth.blockNumber
let i = j - blocks
let gasAverage = 0
let gasMax = 0
let gasMin = 0
let txAverage = 0
let txMax = 0
let txMin = 0
let count = 0
if (i < 0) {
i = 0
}
for (let blockNumber = i; blockNumber <= j; blockNumber++) {
const block = eth.getBlock(blockNumber)
// const gasUsed = parseInt(block.gasUsed, 16)
const gasUsed = block.gasUsed
const txCount = block.transactions.length
if (gasUsed) {
gasAverage += gasUsed
count++
if (gasUsed > gasMax) {
gasMax = gasUsed
}
if (gasMin === 0 || gasUsed < gasMin) {
gasMin = gasUsed
}
}
txAverage += txCount
if (txCount > txMax) {
txMax = txCount
}
if (txMin === 0 || txCount < txMin) {
txMin = txCount
}
}
gasAverage = gasAverage / count
txAverage = txAverage / count
return {
latestBlock: j,
lookback: i,
averageGasUsed: gasAverage,
maxGasUsed: gasMax,
minGasUsed: gasMin,
gasLimit: eth.getBlock('latest').gasLimit,
averageGasUsedPercentage: (gasAverage / eth.getBlock('latest').gasLimit) * 100,
averageTxCount: txAverage,
maxTxCount: txMax,
minTxCount: txMin
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment