// index.js — hypercompute entry point. P0: a local, in-memory demo of the core
// (content-addressed submit -> execute -> memoized result). Networked
// own/join modes are scaffolded in lib/enroll.js + lib/transport.js (P1).
//
// Run: bare index.js demo (or: node index.js demo)
const b4a = require('b4a')
const crypto = require('hypercore-crypto')
const { ComputeNode } = require('./lib/node')
function inputHashOf (input) {
return b4a.toString(crypto.hash(b4a.from(JSON.stringify(input))), 'hex')
}
async function demo () {
const codeHash = 'square-v1'
const executors = { [codeHash]: async (i) => i.n * i.n }
// corestore 7 / hypercore 11 is RocksDB-backed (no in-memory backend) —
// the demo uses a throwaway temp dir.
const storage = '/tmp/hypercompute-demo-' + Date.now()
const node = await ComputeNode.create(storage, null, { executors })
await node.grantSelf('owner')
const input = { n: 7 }
const ih = inputHashOf(input)
console.log('submit n=7 ...')
const a = await node.submit(codeHash, ih, input)
console.log(' ->', a.memoHit ? 'memo hit' : 'memo miss (task queued: ' + a.id.slice(0, 12) + ')')
await node.runPendingTasks()
console.log('result (via the fold):', await node.lookup(codeHash, ih))
const b = await node.submit(codeHash, ih, input)
console.log('re-submit n=7 ->', b.memoHit ? 'memo HIT, output ' + b.output : 'miss')
await node.close()
}
const cmd = (typeof Bare !== 'undefined' ? Bare.argv : process.argv).slice(2)[0] || 'demo'
if (cmd === 'demo') {
demo().catch((err) => { console.error(err); (typeof Bare !== 'undefined' ? Bare : process).exit(1) })
} else {
console.log('hypercompute (P0)\n usage: bare index.js demo\n own/join (networked) — see lib/enroll.js + lib/transport.js (P1)\n see SPEC.md for the full design.')
}