Last active
June 12, 2017 17:48
-
-
Save davidhq/34c2fbd0b877134c5a06192dfe376e77 to your computer and use it in GitHub Desktop.
get the report on active local Ethereum clients (client version and latest block)
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
#!/usr/bin/env node | |
var colors = require('colors'); // npm install colors | |
var moment = require('moment'); // npm install moment | |
var Web3 = require('web3'); // npm install web3 | |
let port = process.argv[2]; | |
function tryWeb3(port) { | |
let web3 = new Web3(); | |
web3.setProvider(new web3.providers.HttpProvider(`http://localhost:${port}`)); | |
try { | |
const dummy = web3.version.node; | |
return web3; | |
} catch (e) {} | |
return false; | |
} | |
function latestBlock(web3) { | |
return new Date(web3.eth.getBlock(web3.eth.blockNumber).timestamp * 1000); | |
} | |
function isRecent(web3) { | |
return (Date.now() - latestBlock(web3)) / 1000.0 < 40; | |
} | |
function isOld(web3) { | |
return (Date.now() - latestBlock(web3)) / 1000.0 > 86400; | |
} | |
function inquireClient(client, port) { | |
let web3 = tryWeb3(port); | |
if (web3) { | |
web3.setProvider(new web3.providers.HttpProvider(`http://localhost:${port}`)); | |
const mark = isRecent(web3) ? colors.green(' ✓') : isOld(web3) ? colors.red(' ✗') : ''; | |
const version = web3.version.node; | |
let msg; | |
if (client) { | |
msg = version.match(new RegExp(client, 'i')) ? colors.green(client) : colors.red(`*not* ${client} on ${port}`); | |
} else { | |
msg = colors.green(`port ${port}`); | |
} | |
console.log(`${msg} ${moment(latestBlock(web3)).fromNow()}${mark} ${colors.gray(version)}`); | |
} else if (!client) { | |
console.log(colors.red(`no Ethereum client running on port ${port}`)); | |
} | |
} | |
inquireClient('geth', 8545); | |
inquireClient('parity', 8546); | |
inquireClient('testrpc', 8550); | |
if (port) { | |
inquireClient('', port); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment