OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-proto-peer / scripts / wj62-state.mjs

39 lines · javascript ref:
// Query get-state for wj62 on BOTH control keys (origin=stortron, device=tabertron) — a read-only
// snapshot of writable / lease holder / head / recordCount, to see which device is the active signer.
import '../lib/bare-globals.js'
import Hyperswarm from 'hyperswarm'
import crypto from 'hypercore-crypto'
import b4a from 'b4a'
import c from 'compact-encoding'
import ProtomuxRPC from 'protomux-rpc'

const CONTROL = {
  origin: '8ae330710aaef922dce79a907f6ff8f57e459de61d4d45f1551d5e0ff23a11d7', // stortron
  device: 'e0cac0d06e67da9c79674a5ca195809300b04635d1a46254f4fd21edd1ce491d' // tabertron
}

async function probe (label, ck) {
  const swarm = new Hyperswarm({ keyPair: crypto.keyPair(crypto.hash(b4a.from('wj62-state-probe-' + label))) })
  const done = new Promise((res) => {
    const to = setTimeout(() => res({ label, error: 'timeout' }), 20000)
    swarm.on('connection', async (conn) => {
      if (b4a.toString(conn.remotePublicKey, 'hex') !== ck) { conn.destroy(); return }
      const rpc = new ProtomuxRPC(conn, { protocol: 'hyper-proto-peer/device/v1', valueEncoding: c.json })
      try { const s = await rpc.request('get-state', {}); clearTimeout(to); res({ label, s }) } catch (e) { clearTimeout(to); res({ label, error: e.message }) }
    })
  })
  swarm.join(b4a.from(ck, 'hex'), { client: true, server: false })
  await swarm.flush()
  const r = await done
  await swarm.destroy()
  return r
}

for (const [label, ck] of Object.entries(CONTROL)) {
  const r = await probe(label, ck)
  if (r.error) { console.log(`${label}: ERROR ${r.error}`); continue }
  const s = r.s
  console.log(`${label}: writable=${s.writable} holdsLease=${s.holdsLease} leaseHolder=${s.leaseHolder ? String(s.leaseHolder).slice(0, 12) : null} indexers=${s.indexers} recordCount=${s.recordCount} headRev=${s.head ? s.head.rev : null}`)
}
globalThis.Bare.exit(0)