#!/usr/bin/env node // lineage-index: read lineage/v0 events from an ord server and print the trees they imply, with the same reducer the // lab uses. --compare checks that ord and a lab backup tell the same story (tools/regtest/README.md). // node bin/lineage-index.mjs [--ord http://127.0.0.1:8088] [--from ] [--compare ] import { readFileSync } from 'node:fs'; import { parseArgs } from 'node:util'; import { decodeLab } from '../src/orgchain/store.js'; import { census, rows, summary } from '../src/orgchain/lineage.js'; import { ordClient, ordEvents, ordState } from '../src/indexer/ord.js'; import { compareWithLab } from '../src/indexer/compare.js'; const { values } = parseArgs({ options: { ord: { type: 'string', default: 'http://127.0.0.1:8088' }, from: { type: 'string', default: '0' }, compare: { type: 'string' } } }); const events = await ordEvents(ordClient(values.ord), { from: Number(values.from) }); const state = ordState(events), plural = (n, w) => `${n} ${w}${n === 1 ? '' : 's'}`; for (const root of state.roots) { const c = census(state, root.id); console.log(`\n${root.name} · ${plural(c.orgs, 'org')}, ${plural(c.agents, 'agent')}, ${plural(c.belts, 'belt')}, ${plural(c.decisions, 'decision')}`); console.log(rows(state, root.id).map(r => (r.type === 'group' ? `${r.guide}${r.label}` : `${r.guide}${r.path} ${r.node.name}${r.away ? ' · graduated' : ''}`)).join('\n')); } const rejected = state.log.filter(e => !e.ok); console.log(`\n${plural(events.length, 'lineage/v0 event')} on ord, ${plural(state.items.size, 'item')}${rejected.length ? `, ${rejected.length} rejected:` : ''}`); for (const e of rejected) console.log(` ${summary(e, state)} (${e.id})`); const rings = [...state.nodes.values()].filter(n => n.keyring && !n.keyring.retired); if (rings.length) console.log(`${plural(rings.length, 'keyring')} bind ${plural(state.bound.size, 'key')}: ${rings.map(n => n.name).join(', ')}`); if (values.compare) { const { problems, ...n } = compareWithLab(decodeLab(readFileSync(values.compare, 'utf8')), events); console.log(problems.length ? `\nord and the lab differ:\n${problems.map(p => ` ${p}`).join('\n')}` : `\nSame story as ${values.compare}: ${plural(n.events, "event")}, ${plural(n.nodes, "node")} and ${plural(n.items, "item")}, each event in the same beat and every inscription in the same output.${n.reordered ? ` The miner ordered ${n.reordered} independent events differently within their blocks, which changes nothing.` : ""}`); process.exitCode = problems.length ? 1 : 0; }