| … | … | @@ -0,0 +1,66 @@ |
| 1 | // REPRO part 1 — the mechanism, in isolation. No network, no fork, no hypercore. |
| 2 | // |
| 3 | // hypercore/lib/core.js (conflict detection) persists the fork flag like this: |
| 4 | // |
| 5 | // this.header.frozen = true |
| 6 | // tx.setAuth({ key, discoveryKey, manifest, keyPair, frozen: true }) |
| 7 | // await this.state.flush() |
| 8 | // |
| 9 | // hypercore-storage/lib/tx.js:31 encodes that object through |
| 10 | // const CORE_AUTH = schema.getEncoding('@core/auth') |
| 11 | // and @core/auth declares exactly five fields: |
| 12 | // key, discoveryKey, manifest, keyPair, encryptionKey |
| 13 | // |
| 14 | // So `frozen` is not part of the struct and hyperschema drops it. The write |
| 15 | // "succeeds", flushes, and the flag is gone. This script proves that with a |
| 16 | // straight encode -> decode round-trip. |
| 17 | // |
| 18 | // Run: node 1-encoding.mjs /path/to/node_modules |
| 19 | |
| 20 | import { createRequire } from 'node:module' |
| 21 | import path from 'node:path' |
| 22 | |
| 23 | const modules = process.argv[2] |
| 24 | if (!modules) { |
| 25 | console.error('usage: node 1-encoding.mjs <path-to-node_modules>') |
| 26 | process.exit(2) |
| 27 | } |
| 28 | |
| 29 | const require = createRequire(path.join(modules, 'x.js')) |
| 30 | const storagePkg = require('hypercore-storage/package.json') |
| 31 | // same path tx.js itself uses (hypercore-storage/lib/tx.js:5) |
| 32 | const schema = require('hypercore-storage/encoding/spec/hyperschema') |
| 33 | |
| 34 | console.log('hypercore-storage', storagePkg.version) |
| 35 | |
| 36 | const CORE_AUTH = schema.getEncoding('@core/auth') |
| 37 | |
| 38 | // A minimal but structurally valid auth record, plus the flag core.js sets. |
| 39 | const auth = { |
| 40 | key: Buffer.alloc(32, 1), |
| 41 | discoveryKey: Buffer.alloc(32, 2), |
| 42 | manifest: null, |
| 43 | keyPair: null, |
| 44 | encryptionKey: null, |
| 45 | frozen: true |
| 46 | } |
| 47 | |
| 48 | const c = require('compact-encoding') |
| 49 | const buf = c.encode(CORE_AUTH, auth) |
| 50 | const out = c.decode(CORE_AUTH, buf) |
| 51 | |
| 52 | console.log('\nwrote frozen =', auth.frozen) |
| 53 | console.log('read frozen =', out.frozen) |
| 54 | console.log('fields round-tripped:', Object.keys(out).join(', ')) |
| 55 | |
| 56 | if (out.frozen === true) { |
| 57 | console.log('\nPASS — frozen survives the encoding (bug is fixed)') |
| 58 | process.exit(0) |
| 59 | } |
| 60 | |
| 61 | console.log('\nFAIL — frozen was silently dropped by @core/auth.') |
| 62 | console.log(' The write in hypercore/lib/core.js can never persist, so on') |
| 63 | console.log(' reopen getCoreInfo() spreads an authInfo with no `frozen`,') |
| 64 | console.log(' header.frozen is undefined, and Replicator.Peer.isActive()') |
| 65 | console.log(' (the ONLY reader of the flag in hypercore) never sees it.') |
| 66 | process.exit(1) |