OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-proto-peer / scripts / hypercore-frozen-encoding.mjs

67 lines · javascript ref:
// REPRO part 1 — the mechanism, in isolation. No network, no fork, no hypercore.
//
// hypercore/lib/core.js (conflict detection) persists the fork flag like this:
//
//     this.header.frozen = true
//     tx.setAuth({ key, discoveryKey, manifest, keyPair, frozen: true })
//     await this.state.flush()
//
// hypercore-storage/lib/tx.js:31 encodes that object through
//     const CORE_AUTH = schema.getEncoding('@core/auth')
// and @core/auth declares exactly five fields:
//     key, discoveryKey, manifest, keyPair, encryptionKey
//
// So `frozen` is not part of the struct and hyperschema drops it. The write
// "succeeds", flushes, and the flag is gone. This script proves that with a
// straight encode -> decode round-trip.
//
// Run:  node 1-encoding.mjs /path/to/node_modules

import { createRequire } from 'node:module'
import path from 'node:path'

const modules = process.argv[2]
if (!modules) {
  console.error('usage: node 1-encoding.mjs <path-to-node_modules>')
  process.exit(2)
}

const require = createRequire(path.join(modules, 'x.js'))
const storagePkg = require('hypercore-storage/package.json')
// same path tx.js itself uses (hypercore-storage/lib/tx.js:5)
const schema = require('hypercore-storage/encoding/spec/hyperschema')

console.log('hypercore-storage', storagePkg.version)

const CORE_AUTH = schema.getEncoding('@core/auth')

// A minimal but structurally valid auth record, plus the flag core.js sets.
const auth = {
  key: Buffer.alloc(32, 1),
  discoveryKey: Buffer.alloc(32, 2),
  manifest: null,
  keyPair: null,
  encryptionKey: null,
  frozen: true
}

const c = require('compact-encoding')
const buf = c.encode(CORE_AUTH, auth)
const out = c.decode(CORE_AUTH, buf)

console.log('\nwrote  frozen =', auth.frozen)
console.log('read   frozen =', out.frozen)
console.log('fields round-tripped:', Object.keys(out).join(', '))

if (out.frozen === true) {
  console.log('\nPASS — frozen survives the encoding (bug is fixed)')
  process.exit(0)
}

console.log('\nFAIL — frozen was silently dropped by @core/auth.')
console.log('       The write in hypercore/lib/core.js can never persist, so on')
console.log('       reopen getCoreInfo() spreads an authInfo with no `frozen`,')
console.log('       header.frozen is undefined, and Replicator.Peer.isActive()')
console.log('       (the ONLY reader of the flag in hypercore) never sees it.')
process.exit(1)