Skip to content

Instantly share code, notes, and snippets.

@dapplion
Created October 20, 2022 14:51
Show Gist options
  • Save dapplion/9af62797a450b2c3e760a5671dfe4aa9 to your computer and use it in GitHub Desktop.
Save dapplion/9af62797a450b2c3e760a5671dfe4aa9 to your computer and use it in GitHub Desktop.
import {execSync} from "node:child_process";
// Usage
// ```
// node ttd.mjs https://rpc.eu-central-2.gateway.fm/v3/gnosis/archival/chiado
// ```
const rpcUrl = process.argv[2] ?? "https://rpc.gnosischain.com/";
const blocksPerDay = 17280;
let prevBlock = fetchBlock("latest");
const latestBlockNum = parseInt(prevBlock.number);
for (let num = latestBlockNum - blocksPerDay; num > 0; num -= blocksPerDay) {
const block = fetchBlock(num);
const tdDiff = BigInt(prevBlock.totalDifficulty) - BigInt(block.totalDifficulty);
const secDiff = parseInt(prevBlock.timestamp) - parseInt(block.timestamp);
const numDiff = parseInt(prevBlock.number) - parseInt(block.number);
const tdPerBlock = tdDiff / BigInt(numDiff);
const tdPerSec = tdDiff / BigInt(secDiff);
const secPerBlock = secDiff / numDiff;
console.log(`Blocks ${parseInt(prevBlock.number)} ${parseInt(block.number)}: tdPerBlock ${tdPerBlock} tdPerSec ${tdPerSec} secPerBlock ${secPerBlock}`);
prevBlock = block;
}
// number: '0x1786dd7'
// timestamp: '0x63514d94',
// totalDifficulty: '0x1786dd6ffffffffffffffffffffffffeaac82a5',
function fetchBlock(blockRef) {
const txt = execSync(`curl -s -X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["${blockRef}"],"id":0}' ${rpcUrl}`, {encoding: "utf8"});
const json = JSON.parse(txt);
if (!json.result) throw Error(txt);
return json.result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment