Created
January 9, 2022 19:03
-
-
Save pinheadmz/8c95ddf3889aa0b8665956945c71e7dd 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
'use strict'; | |
const assert = require('assert'); | |
const {SPVNode, Namestate, Rules, resource} = require('hsd'); | |
const argv = process.argv; | |
assert(argv.length === 3, 'Usage:\n $ node zooko-spv.js <name>'); | |
assert(Rules.verifyName(argv[2], 'Invalid name.')); | |
const nameHash = Rules.hashName(argv[2]); | |
const node = new SPVNode({ | |
network: 'main', | |
memory: true, | |
workers: true | |
}); | |
async function resolveAt(nameHash, root) { | |
const peer = node.pool.pickProver(nameHash); | |
// NameRequest class not exported from pool.js | |
const item = {root}; | |
node.pool.nameMap.set(nameHash, item); | |
peer.nameMap.set(nameHash, Date.now()); | |
peer.sendGetProof(root, nameHash); | |
return new Promise((resolve, reject) => { | |
item.resolve = resolve; | |
item.reject = reject; | |
}); | |
} | |
const queue = []; | |
(async() => { | |
node.on('connect', async (entry) => { | |
if (entry.height % 100 === 0) | |
console.log(`SPV headers chain height: ${entry.height}`); | |
if (entry.height % 36 !== 0) | |
return; | |
const {treeRoot} = entry; | |
queue.push({height: entry.height, root: treeRoot}); | |
}); | |
console.log('Opening in-memory SPV node...'); | |
await node.open(); | |
await node.connect(); | |
node.startSync(); | |
for(;;) { | |
if (!queue.length) { | |
await new Promise(r => setImmediate(r)); | |
continue; | |
} | |
const {height, root} = queue.shift(); | |
let records = null; | |
try { | |
const data = await resolveAt(nameHash, root); | |
if (data) { | |
const ns = Namestate.decode(data); | |
if (ns.data.length > 0) { | |
const res = resource.Resource.decode(ns.data); | |
records = JSON.stringify(res.toJSON()); | |
} | |
} | |
console.log( | |
' ' + | |
`height: ${height} ` + | |
`tree root: ${root.toString('hex')} `+ | |
`records: ${records}` | |
); | |
} catch (e) { | |
// Probably a peer timed out getting proof, retry... | |
queue.unshift({height, root}); | |
} finally { | |
await new Promise(r => setTimeout(r, 100)); | |
} | |
} | |
})(); | |
// Example output snippet: | |
// SPV headers chain height: 16700 | |
// SPV headers chain height: 16800 | |
// SPV headers chain height: 16900 | |
// SPV headers chain height: 17000 | |
// SPV headers chain height: 17100 | |
// Error (Timed out.) retrying... | |
// height: 4392 tree root: d88bbfd5c5d20035ce3d7b71579732a02b4105a8afdad9ef09e2b155b1a3a222 records: null | |
// height: 4428 tree root: 2e1a8c84b182b85f8a072b95b3a42d21a55fb13c227e271153b72a0f681f7246 records: null | |
// height: 4464 tree root: f0ab4c73fa171e1ab6fa48281b86b44a0f94e425a1ce0f96dc1eaa73f2419912 records: null | |
// height: 4500 tree root: bdf23fdbe9796a10957e348aaf1b86be949a1fb3df725e94607f7270456ffc2e records: null | |
// SPV headers chain height: 17200 | |
// height: 4536 tree root: cabce5e0c39f2f76f81983dd7fd436329eb330426fa780ff44cfbec4fa17bcaa records: {"records":[{"type":"NS","ns":"a.dns.park.io."}]} | |
// height: 4572 tree root: 42a1dddd8c50174dbd8b2897ab984f00c7d4c5cba5fa4da483c7528306fb5da3 records: {"records":[{"type":"NS","ns":"a.dns.park.io."}]} | |
// height: 4608 tree root: 2c80023af98b1f21ca29cd992ca0c4a786ddf13253e0e2b478d05f88bab278ca records: {"records":[{"type":"NS","ns":"a.dns.park.io."}]} | |
// height: 4644 tree root: c4ac14b925f8a742ccbf93f098b26762a6bd5719413031eecb98cc9cb187b147 records: {"records":[{"type":"NS","ns":"a.dns.park.io."}]} | |
// SPV headers chain height: 17300 | |
// height: 4680 tree root: af81df51f70494c60179f3bd1ecdc7871a542bffa09b37e928aa03f4dcea598c records: {"records":[{"type":"NS","ns":"a.dns.park.io."}]} | |
// SPV headers chain height: 17400 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment