Created
October 8, 2022 18:53
-
-
Save rithvikvibhu/4bebb0985bc4a583849faea698ba6e82 to your computer and use it in GitHub Desktop.
This file contains 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
// USAGE: hsd --log-console=false --plugins=/path/to/this/file/renewal-count-plugin.js | |
'use strict'; | |
const fs = require('fs').promises; | |
const layout = require('hsd/lib/blockchain/layout'); | |
const { types } = require('hsd/lib/covenants/rules'); | |
const TXMeta = require('hsd/lib/primitives/txmeta'); | |
const plugin = exports; | |
class Plugin { | |
/** | |
* Renewal Count Plugin | |
* @param {import('hsd/lib/node/node')} node | |
*/ | |
constructor(node) { | |
/** @type {import('hsd/lib/blockchain/chain')} */ | |
this.chain = node.chain; | |
// Run after chain is ready | |
this.chain.once('tip', async (entry) => { | |
console.log('::: Count renewals by block :::'); | |
this.run(); | |
}); | |
} | |
async run() { | |
const start = Date.now(); | |
let count = 0; // Total number of txs scanned | |
let maxHeight = 0; | |
const registers = []; | |
const renews = []; | |
const finalizes = []; | |
const blockTotals = []; // sum of all 3, per block | |
// Extended txs iterator | |
const iter = this.chain.db.db.iterator({ | |
gte: layout.t.min(), | |
lte: layout.t.max(), | |
values: true, | |
}); | |
while (await iter.next()) { | |
count++; | |
if (count % 100000 === 0) console.log('Scanned', count, 'txs.') | |
// if (count > 10000) break; | |
const { value } = iter; | |
const meta = TXMeta.decode(value); | |
const {tx, height} = meta; | |
if (height > maxHeight) maxHeight = height; | |
// Skip if coinbase | |
if (tx.isCoinbase()) continue; | |
for (const {covenant} of tx.outputs) { | |
switch (covenant.type) { | |
case types.REGISTER: | |
registers[height] = (registers[height]+1) || 1; | |
blockTotals[height] = (blockTotals[height]+1) || 1; | |
break; | |
case types.RENEW: | |
renews[height] = (renews[height]+1) || 1; | |
blockTotals[height] = (blockTotals[height]+1) || 1; | |
break; | |
case types.FINALIZE: | |
finalizes[height] = (finalizes[height]+1) || 1; | |
blockTotals[height] = (blockTotals[height]+1) || 1; | |
break; | |
} | |
} | |
} | |
console.log('DONE.'); | |
console.log(`elapsed time: ${Date.now() - start} ms`); | |
console.log(`Scanned txs: ${count}\tmax height: ${maxHeight}`); | |
for (let i = 0; i < maxHeight; i++) { | |
if (!registers[i]) registers[i] = 0; | |
if (!renews[i]) renews[i] = 0; | |
if (!finalizes[i]) finalizes[i] = 0; | |
if (!blockTotals[i]) blockTotals[i] = 0; | |
} | |
await fs.writeFile('renewalCounts.json', JSON.stringify({ | |
registers, | |
renews, | |
finalizes, | |
blockTotals, | |
})); | |
console.log('Wrote to file.'); | |
const registersCount = sum(registers); | |
const renewsCount = sum(renews); | |
const finalizesCount = sum(finalizes); | |
const blockTotalsCount = sum(blockTotals); | |
console.table({ | |
registers: registersCount, | |
renews: renewsCount, | |
finalizes: finalizesCount, | |
total: blockTotalsCount, | |
}); | |
process.exit(); | |
} | |
} | |
plugin.id = 'renewal-count'; | |
plugin.init = function init(node) { | |
return new Plugin(node); | |
}; | |
function sum(arr) { | |
var res = 0; | |
for (var x of arr) { | |
if (x) res += x; | |
} | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment