Updates
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
node_modules/
|
||||
*-storage/
|
||||
@@ -0,0 +1,19 @@
|
||||
# Changelog
|
||||
|
||||
<!-- legacy: v0.1.0 -->
|
||||
|
||||
- Initial release.
|
||||
<!-- legacy: v0.2.0 -->
|
||||
|
||||
- Production-grade docs, validation, and expanded tests.
|
||||
<!-- legacy: v0.3.0 -->
|
||||
|
||||
- Wave 6: presence-tier API tables, architecture wire section, validation test.
|
||||
|
||||
<!-- legacy: v0.3.1 -->
|
||||
|
||||
- Wave 7: correct protocol in docs, getStats(), wire tables, category README.
|
||||
## [0.3.2] - 2026-05-21
|
||||
|
||||
### Changed
|
||||
- Exhaustive documentation pass (api, architecture, README) per DOC_STANDARDS.md.
|
||||
@@ -0,0 +1,43 @@
|
||||
# hyper-p2p-paradox-clock
|
||||
|
||||
Production experimental (wave 4) module: Hyperswarm discovery + Protomux when `topic` is set.
|
||||
|
||||
**Category:** Experimental (Wave 4)
|
||||
|
||||
**Composes with:** `hyper-p2p-pheromone-trail`, `hyper-p2p-whisper-mesh`, `hyper-p2p-entropy-beacon`
|
||||
|
||||
**Protocol:** `paradox-clock/v1`
|
||||
|
||||
## When to use
|
||||
|
||||
Multi-peer apps that need experimental (wave 4) over a shared Hyperswarm topic.
|
||||
|
||||
## When not to use
|
||||
|
||||
Single-process tools with no P2P topic (use local APIs only or skip `ready()`).
|
||||
|
||||
## Quick start
|
||||
|
||||
```js
|
||||
const { HyperP2PParadoxClock } = require('hyper-p2p-paradox-clock')
|
||||
const topic = process.argv[2] // 64-char hex or string
|
||||
const mod = new HyperP2PParadoxClock({ topic, enableBackgroundTimers: false })
|
||||
await mod.ready() // joins swarm when topic set
|
||||
// ... application logic ...
|
||||
await mod.close()
|
||||
```
|
||||
|
||||
## Docs
|
||||
|
||||
- [docs/api.md](docs/api.md) — constructor, methods, events, errors
|
||||
- [docs/architecture.md](docs/architecture.md) — wire types, state, composition
|
||||
- [../_shared/PRODUCTION.md](../../_shared/PRODUCTION.md) — production checklist
|
||||
- [../_shared/DOC_STANDARDS.md](../../_shared/DOC_STANDARDS.md) — documentation standards
|
||||
|
||||
- Integration: [`../../real_tests/integration/`](../../../real_tests/integration/) — `paradox-clock-two-node.js`
|
||||
|
||||
## Test
|
||||
|
||||
```bash
|
||||
npm install && npm test
|
||||
```
|
||||
@@ -0,0 +1,91 @@
|
||||
# API: hyper-p2p-paradox-clock
|
||||
|
||||
**Protocol:** `paradox-clock/v1`
|
||||
|
||||
**Export:** `HyperP2PParadoxClock`
|
||||
|
||||
## Overview
|
||||
|
||||
Production experimental (wave 4) module: Hyperswarm discovery + Protomux when `topic` is set.
|
||||
|
||||
## Constructor
|
||||
|
||||
```js
|
||||
const mod = new HyperP2PParadoxClock(opts)
|
||||
```
|
||||
|
||||
| Option | Type | Default | Description |
|
||||
|--------|------|---------|-------------|
|
||||
| `topic` | varies | null | topic |
|
||||
| `keyPair` | KeyPair | random Ed25519 | keyPair |
|
||||
|
||||
## Methods
|
||||
|
||||
### `stamp(eventId, opts = {})`
|
||||
|
||||
- **Returns:** `value`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
### `resolve(a, b)`
|
||||
|
||||
- **Returns:** `value`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
### `getStamp(eventId)`
|
||||
|
||||
- **Returns:** `value`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
### `merge(remote)`
|
||||
|
||||
- **Returns:** `value`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
### `toJSON(—)`
|
||||
|
||||
- **Returns:** `value`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
### `ready(—)`
|
||||
|
||||
- **Returns:** `Promise`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
### `getStats(—)`
|
||||
|
||||
- **Returns:** `object`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
### `close(—)`
|
||||
|
||||
- **Returns:** `Promise<void>`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
## Events
|
||||
|
||||
| Event | Payload |
|
||||
|-------|---------|
|
||||
| `closed` | no payload |
|
||||
| `merge` | updated |
|
||||
| `stamp` | stamp |
|
||||
|
||||
## getStats()
|
||||
|
||||
Returns `{ ...this._stats }` — typically `ops`, `errors`, and module-specific counters (`created`, `relays`, `open`, `peers`, etc.).
|
||||
Library-only modules may include `mode: 'local'`.
|
||||
|
||||
## Errors
|
||||
|
||||
Stable message substrings: see [`../_shared/ERROR_CODES.md`](../../_shared/ERROR_CODES.md).
|
||||
|
||||
## P2P
|
||||
|
||||
When `topic` is set, `ready()` joins Hyperswarm and opens Protomux `paradox-clock/v1`.
|
||||
|
||||
## Testing
|
||||
|
||||
```bash
|
||||
npm install && npm test
|
||||
```
|
||||
|
||||
Integration: [`../../real_tests/integration/paradox-clock-two-node.js`](../../../real_tests/integration/paradox-clock-two-node.js)
|
||||
@@ -0,0 +1,43 @@
|
||||
# Architecture: hyper-p2p-paradox-clock
|
||||
|
||||
**Category:** Experimental (Wave 4)
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
App[Application] --> Mod[HyperP2PParadoxClock]
|
||||
Mod --> Mux[Protomux paradox-clock/v1]
|
||||
Mux --> Swarm[Hyperswarm]
|
||||
```
|
||||
|
||||
## Sequence (P2P)
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant App
|
||||
participant Mod as Module
|
||||
participant SW as Hyperswarm
|
||||
participant Peer
|
||||
App->>Mod: ready(topic)
|
||||
Mod->>SW: join(topic)
|
||||
SW->>Peer: connection
|
||||
Mod->>Peer: gossip / Protomux
|
||||
Peer-->>Mod: onmessage
|
||||
Mod-->>App: emit(event)
|
||||
```
|
||||
|
||||
## Wire messages
|
||||
|
||||
| type | fields | direction | behavior |
|
||||
|------|--------|-----------|----------|
|
||||
| `stamp` | stamp | gossip | Handled in onmessage / gossipSend |
|
||||
|
||||
## State model
|
||||
|
||||
- In-memory `Map` / `Set` structures for hot path
|
||||
- Optional Hyperbee/Hypercore persistence when `storageDir` or `memoryOnly` is configured
|
||||
- `close()` tears down swarm, timers, and clears ephemeral state
|
||||
|
||||
## Composition
|
||||
|
||||
Composes with: `hyper-p2p-pheromone-trail`, `hyper-p2p-whisper-mesh`, `hyper-p2p-entropy-beacon`.
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
require('bare-process/global')
|
||||
const { HyperP2PParadoxClock } = require('../index.js')
|
||||
async function main () {
|
||||
const c = new HyperP2PParadoxClock()
|
||||
console.log(c.stamp('event-1', { skew: 5000 }))
|
||||
await c.close()
|
||||
}
|
||||
main().catch(console.error)
|
||||
@@ -0,0 +1,96 @@
|
||||
require('bare-process/global')
|
||||
const EventEmitter = require('bare-events')
|
||||
const b4a = require('b4a')
|
||||
const { initModuleSwarm, gossipSend } = require('../../_shared/p2p-bare.js')
|
||||
|
||||
const PROTOCOL = 'paradox-clock/v1'
|
||||
|
||||
class HyperP2PParadoxClock extends EventEmitter {
|
||||
constructor (opts = {}) {
|
||||
super()
|
||||
this._stats = { ops: 0, errors: 0 }
|
||||
|
||||
this.topic = opts.topic || null
|
||||
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
|
||||
this.peerId = b4a.toString(this.keyPair.publicKey, 'hex')
|
||||
this._logical = 0
|
||||
this._stamps = new Map()
|
||||
this.swarm = null
|
||||
this._peerMsgs = null
|
||||
}
|
||||
|
||||
stamp (eventId, opts = {}) {
|
||||
const id = String(eventId)
|
||||
const wall = opts.wallTime ?? Date.now()
|
||||
const skew = opts.skew ?? 0
|
||||
this._logical++
|
||||
const stamp = {
|
||||
eventId: id,
|
||||
logical: this._logical,
|
||||
wall: wall + skew,
|
||||
peerId: this.peerId,
|
||||
paradox: Math.abs(skew) > 60000
|
||||
}
|
||||
this._stamps.set(id, stamp)
|
||||
if (this._peerMsgs) gossipSend(this, { type: 'stamp', stamp })
|
||||
this.emit('stamp', stamp)
|
||||
return stamp
|
||||
}
|
||||
|
||||
resolve (a, b) {
|
||||
if (!a || !b) return a || b
|
||||
if (a.logical !== b.logical) return a.logical > b.logical ? a : b
|
||||
if (a.wall !== b.wall) return a.wall > b.wall ? a : b
|
||||
return a.peerId > b.peerId ? a : b
|
||||
}
|
||||
|
||||
getStamp (eventId) {
|
||||
return this._stamps.get(String(eventId)) || null
|
||||
}
|
||||
|
||||
merge (remote) {
|
||||
if (!remote || !remote.stamps) return 0
|
||||
let n = 0
|
||||
for (const s of remote.stamps) {
|
||||
const cur = this._stamps.get(s.eventId)
|
||||
const winner = this.resolve(cur, s)
|
||||
if (winner && winner !== cur) {
|
||||
this._stamps.set(s.eventId, winner)
|
||||
this._logical = Math.max(this._logical, winner.logical)
|
||||
n++
|
||||
}
|
||||
}
|
||||
if (n) this.emit('merge', { updated: n })
|
||||
return n
|
||||
}
|
||||
|
||||
toJSON () {
|
||||
return { stamps: [...this._stamps.values()] }
|
||||
}
|
||||
|
||||
async ready () {
|
||||
if (this.swarm || !this.topic) return this
|
||||
await initModuleSwarm(this, {
|
||||
keyPair: this.keyPair,
|
||||
topic: this.topic,
|
||||
protocol: PROTOCOL,
|
||||
onmessage: (data) => {
|
||||
if (data && data.type === 'stamp' && data.stamp) this.merge({ stamps: [data.stamp] })
|
||||
}
|
||||
})
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
getStats () {
|
||||
return { ...this._stats }
|
||||
}
|
||||
|
||||
async close () {
|
||||
if (this.swarm) await this.swarm.destroy().catch(() => {})
|
||||
this.swarm = null
|
||||
this.emit('closed')
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { HyperP2PParadoxClock, PROTOCOL }
|
||||
+1774
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "hyper-p2p-paradox-clock",
|
||||
"version": "0.3.1",
|
||||
"description": "Skew-forgiving paradox clock for Bare/Pear P2P.",
|
||||
"main": "index.js",
|
||||
"type": "commonjs",
|
||||
"license": "Apache-2.0",
|
||||
"scripts": { "test": "brittle-bare test/test.js" },
|
||||
"dependencies": {
|
||||
"bare-events": "^2.8.0",
|
||||
"bare-process": "^4.4.0",
|
||||
"b4a": "^1.6.7",
|
||||
"hypercore-crypto": "^3.0.0",
|
||||
"protomux": "^3.0.0",
|
||||
"compact-encoding": "^2.0.0"
|
||||
},
|
||||
"peerDependencies": { "hyperswarm": "^4.0.0", "bare": ">=1.0.0" },
|
||||
"devDependencies": { "brittle": "^3.0.0" },
|
||||
"imports": {
|
||||
"process": { "bare": "bare-process", "default": "process" },
|
||||
"events": { "bare": "bare-events", "default": "events" }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
require('bare-process/global')
|
||||
const test = require('brittle')
|
||||
const { HyperP2PParadoxClock } = require('../index.js')
|
||||
|
||||
test('paradox-clock: stamp and resolve', async (t) => {
|
||||
const c = new HyperP2PParadoxClock()
|
||||
const a = c.stamp('e1', { skew: 100000 })
|
||||
const b = c.stamp('e2', { skew: -50000 })
|
||||
const w = c.resolve(a, b)
|
||||
t.ok(w.logical >= 1)
|
||||
await c.close()
|
||||
})
|
||||
|
||||
test('paradox-clock: merge', async (t) => {
|
||||
const a = new HyperP2PParadoxClock()
|
||||
const b = new HyperP2PParadoxClock()
|
||||
a.stamp('evt')
|
||||
b.merge(a.toJSON())
|
||||
t.ok(b.getStamp('evt'))
|
||||
await a.close()
|
||||
await b.close()
|
||||
})
|
||||
test('hyper-p2p-paradox-clock: close without leak', async (t) => {
|
||||
const m = new HyperP2PParadoxClock()
|
||||
await m.close()
|
||||
t.pass()
|
||||
})
|
||||
Reference in New Issue
Block a user