Last active
February 18, 2022 22:53
-
-
Save pinheadmz/b51a6ee3c1b414bd7d053951b83f6162 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/expiring-plug.js | |
'use strict'; | |
const { | |
Namestate, | |
Network | |
} = require('hsd'); | |
const plugin = exports; | |
const network = Network.get('main'); | |
class Plugin { | |
constructor(node) { | |
this.chain = node.chain; | |
this.tree = node.chain.db.tree; | |
this.map = {}; | |
this.chain.on('tip', async (entry) => { | |
await this.run(entry.height); | |
}); | |
} | |
async run(height) { | |
const start = Date.now(); | |
const out = {}; | |
let total = 0; | |
const iter = this.tree.iterator(); | |
while (await iter.next()) { | |
total++; | |
const {value} = iter; | |
const ns = Namestate.decode(value); | |
// Skip reserved | |
if (ns.claimed) | |
continue; | |
// Aution is not yet finished | |
if (!ns.isClosed(height, network)) | |
continue; | |
// No reveals, no winner, no owner | |
if (ns.owner.isNull()) | |
continue; | |
const {renewalPeriodEnd} = ns.toStats( | |
this.chain.height, | |
this.chain.network | |
); | |
const keys = Object.keys(out); | |
if (keys.length >= 20) { | |
if (renewalPeriodEnd >= keys[keys.length - 1]) | |
continue; | |
else | |
delete out[keys[keys.length -1]]; | |
} | |
if (!out[renewalPeriodEnd]) | |
out[renewalPeriodEnd] = [ns.name.toString('ascii')]; | |
else | |
out[renewalPeriodEnd].push(ns.name.toString('ascii')); | |
console.log('\n\n\n'); | |
console.log(out); | |
console.log(total); | |
} | |
console.log('DONE'); | |
console.log(`elapsed: ${Date.now() - start}`); | |
} | |
} | |
plugin.id = 'expiring'; | |
plugin.init = function init(node) { | |
return new Plugin(node); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment