Last active
February 7, 2022 06:01
-
-
Save sharvit/e41e301e90888f4a876c156c578e36df to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* eslint-disable no-await-in-loop, no-console */ | |
const { sleep } = require('../helpers'); | |
const { createNetworkProvider } = require('../web3'); | |
class BlocktimeMonitor { | |
network; | |
protocol; | |
provider; | |
blockCounter; | |
blocksTimeDiff; | |
minDiff; | |
maxDiff; | |
constructor(network, protocol) { | |
this.reset(network, protocol); | |
} | |
async start() { | |
console.clear(); | |
console.log('Monitoring: ', this.provider.connection.url); | |
console.log(); | |
if (this.protocol === 'ws') { | |
return this.startWsTest(); | |
} | |
return this.startHttpTest(); | |
} | |
async startHttpTest() { | |
let blockNumber = await this.provider.getBlockNumber(); | |
let previuesBlockNumber; | |
while (this.blockCounter < 50) { | |
previuesBlockNumber = blockNumber; | |
blockNumber = await this.provider.getBlockNumber(); | |
if (blockNumber !== previuesBlockNumber) { | |
this.logNewBlock(blockNumber); | |
} | |
await sleep(100); | |
} | |
} | |
async startWsTest() { | |
this.provider.on('block', (blockNumber) => this.logNewBlock(blockNumber)); | |
} | |
async logNewBlock(blockNumber) { | |
const recivedDate = new Date(); | |
const blockInfo = await this.provider.getBlock(blockNumber); | |
if (blockInfo && blockInfo.timestamp) { | |
const blockDate = new Date(blockInfo.timestamp * 1000); | |
const diff = (recivedDate - blockDate) / 1000; | |
this.blockCounter += 1; | |
this.blocksTimeDiff += diff; | |
if (diff > this.maxDiff) this.maxDiff = diff; | |
if (diff < this.minDiff) this.minDiff = diff; | |
BlocktimeMonitor.printBlockInfo({ | |
blockNumber, | |
blockDate, | |
recivedDate, | |
diff, | |
}); | |
} | |
} | |
reset(network, protocol) { | |
if (protocol !== 'http' && protocol !== 'ws') { | |
throw new Error( | |
`Unsupported network protocol provided: ${protocol}. Supported values are ['http', 'ws']` | |
); | |
} | |
this.blockCounter = 0; | |
this.blocksTimeDiff = 0; | |
this.minDiff = 10000; | |
this.maxDiff = 0; | |
this.network = network; | |
this.protocol = protocol; | |
this.provider = createNetworkProvider(network, protocol); | |
} | |
printResults() { | |
console.log(); | |
console.log('Monitored blocks:', this.blockCounter); | |
console.log( | |
'Avarage diff:', | |
(this.blocksTimeDiff / this.blockCounter).toFixed(3), | |
'seconds' | |
); | |
console.log('Min diff:', this.minDiff.toFixed(3), 'seconds'); | |
console.log('Max diff:', this.maxDiff.toFixed(3), 'seconds'); | |
} | |
static printBlockInfo({ blockNumber, blockDate, recivedDate, diff }) { | |
console.log('New Block:', blockNumber); | |
console.log('Minned time: ', blockDate.toLocaleString()); | |
console.log('Recived time:', recivedDate.toLocaleString()); | |
console.log('Diff:', diff.toFixed(3), 'seconds'); | |
console.log('---------'); | |
console.log(); | |
} | |
} | |
module.exports = BlocktimeMonitor; |
This file contains hidden or 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
Monitoring: wss://weathered-falling-snow.quiknode.pro/API_KEY/ | |
New Block: 14157218 | |
Minned time: 2/7/2022, 12:54:54 PM | |
Recived time: 2/7/2022, 12:55:12 PM | |
Diff: 18.415 seconds | |
--------- | |
New Block: 14157218 | |
Minned time: 2/7/2022, 12:54:54 PM | |
Recived time: 2/7/2022, 12:55:14 PM | |
Diff: 20.957 seconds | |
--------- | |
New Block: 14157219 | |
Minned time: 2/7/2022, 12:55:08 PM | |
Recived time: 2/7/2022, 12:55:23 PM | |
Diff: 15.227 seconds | |
--------- | |
New Block: 14157220 | |
Minned time: 2/7/2022, 12:55:20 PM | |
Recived time: 2/7/2022, 12:55:34 PM | |
Diff: 14.207 seconds | |
--------- | |
New Block: 14157221 | |
Minned time: 2/7/2022, 12:55:31 PM | |
Recived time: 2/7/2022, 12:55:58 PM | |
Diff: 27.337 seconds | |
--------- | |
New Block: 14157222 | |
Minned time: 2/7/2022, 12:55:55 PM | |
Recived time: 2/7/2022, 12:56:00 PM | |
Diff: 5.904 seconds | |
--------- | |
New Block: 14157223 | |
Minned time: 2/7/2022, 12:55:57 PM | |
Recived time: 2/7/2022, 12:56:29 PM | |
Diff: 32.788 seconds | |
--------- | |
New Block: 14157224 | |
Minned time: 2/7/2022, 12:56:27 PM | |
Recived time: 2/7/2022, 12:57:01 PM | |
Diff: 34.738 seconds | |
--------- | |
New Block: 14157225 | |
Minned time: 2/7/2022, 12:56:58 PM | |
Recived time: 2/7/2022, 12:57:07 PM | |
Diff: 9.554 seconds | |
--------- | |
New Block: 14157226 | |
Minned time: 2/7/2022, 12:57:05 PM | |
Recived time: 2/7/2022, 12:57:23 PM | |
Diff: 18.446 seconds | |
--------- | |
New Block: 14157227 | |
Minned time: 2/7/2022, 12:57:20 PM | |
Recived time: 2/7/2022, 12:57:30 PM | |
Diff: 10.112 seconds | |
--------- | |
New Block: 14157228 | |
Minned time: 2/7/2022, 12:57:27 PM | |
Recived time: 2/7/2022, 12:58:11 PM | |
Diff: 44.267 seconds | |
--------- | |
New Block: 14157229 | |
Minned time: 2/7/2022, 12:58:08 PM | |
Recived time: 2/7/2022, 12:58:19 PM | |
Diff: 11.039 seconds | |
--------- | |
New Block: 14157230 | |
Minned time: 2/7/2022, 12:58:16 PM | |
Recived time: 2/7/2022, 12:58:21 PM | |
Diff: 5.579 seconds | |
--------- | |
New Block: 14157231 | |
Minned time: 2/7/2022, 12:58:17 PM | |
Recived time: 2/7/2022, 12:58:47 PM | |
Diff: 30.007 seconds | |
--------- | |
New Block: 14157232 | |
Minned time: 2/7/2022, 12:58:44 PM | |
Recived time: 2/7/2022, 12:58:49 PM | |
Diff: 5.566 seconds | |
--------- | |
Monitored blocks: 16 | |
Average diff: 19.009 seconds | |
Min diff: 5.566 seconds | |
Max diff: 44.267 seconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment