@optimystic/db-p2p 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) hide show
  1. package/dist/index.min.js +52 -0
  2. package/dist/index.min.js.map +7 -0
  3. package/dist/src/cluster/client.d.ts +12 -0
  4. package/dist/src/cluster/client.d.ts.map +1 -0
  5. package/dist/src/cluster/client.js +65 -0
  6. package/dist/src/cluster/client.js.map +1 -0
  7. package/dist/src/cluster/cluster-repo.d.ts +79 -0
  8. package/dist/src/cluster/cluster-repo.d.ts.map +1 -0
  9. package/dist/src/cluster/cluster-repo.js +613 -0
  10. package/dist/src/cluster/cluster-repo.js.map +1 -0
  11. package/dist/src/cluster/partition-detector.d.ts +59 -0
  12. package/dist/src/cluster/partition-detector.d.ts.map +1 -0
  13. package/dist/src/cluster/partition-detector.js +129 -0
  14. package/dist/src/cluster/partition-detector.js.map +1 -0
  15. package/dist/src/cluster/service.d.ts +49 -0
  16. package/dist/src/cluster/service.d.ts.map +1 -0
  17. package/dist/src/cluster/service.js +107 -0
  18. package/dist/src/cluster/service.js.map +1 -0
  19. package/dist/src/index.d.ts +29 -0
  20. package/dist/src/index.d.ts.map +1 -0
  21. package/dist/src/index.js +29 -0
  22. package/dist/src/index.js.map +1 -0
  23. package/dist/src/it-utility.d.ts +4 -0
  24. package/dist/src/it-utility.d.ts.map +1 -0
  25. package/dist/src/it-utility.js +32 -0
  26. package/dist/src/it-utility.js.map +1 -0
  27. package/dist/src/libp2p-key-network.d.ts +59 -0
  28. package/dist/src/libp2p-key-network.d.ts.map +1 -0
  29. package/dist/src/libp2p-key-network.js +278 -0
  30. package/dist/src/libp2p-key-network.js.map +1 -0
  31. package/dist/src/libp2p-node.d.ts +28 -0
  32. package/dist/src/libp2p-node.d.ts.map +1 -0
  33. package/dist/src/libp2p-node.js +270 -0
  34. package/dist/src/libp2p-node.js.map +1 -0
  35. package/dist/src/logger.d.ts +3 -0
  36. package/dist/src/logger.d.ts.map +1 -0
  37. package/dist/src/logger.js +6 -0
  38. package/dist/src/logger.js.map +1 -0
  39. package/dist/src/network/get-network-manager.d.ts +4 -0
  40. package/dist/src/network/get-network-manager.d.ts.map +1 -0
  41. package/dist/src/network/get-network-manager.js +17 -0
  42. package/dist/src/network/get-network-manager.js.map +1 -0
  43. package/dist/src/network/network-manager-service.d.ts +82 -0
  44. package/dist/src/network/network-manager-service.d.ts.map +1 -0
  45. package/dist/src/network/network-manager-service.js +283 -0
  46. package/dist/src/network/network-manager-service.js.map +1 -0
  47. package/dist/src/peer-utils.d.ts +2 -0
  48. package/dist/src/peer-utils.d.ts.map +1 -0
  49. package/dist/src/peer-utils.js +28 -0
  50. package/dist/src/peer-utils.js.map +1 -0
  51. package/dist/src/protocol-client.d.ts +12 -0
  52. package/dist/src/protocol-client.d.ts.map +1 -0
  53. package/dist/src/protocol-client.js +34 -0
  54. package/dist/src/protocol-client.js.map +1 -0
  55. package/dist/src/repo/client.d.ts +17 -0
  56. package/dist/src/repo/client.d.ts.map +1 -0
  57. package/dist/src/repo/client.js +82 -0
  58. package/dist/src/repo/client.js.map +1 -0
  59. package/dist/src/repo/cluster-coordinator.d.ts +59 -0
  60. package/dist/src/repo/cluster-coordinator.d.ts.map +1 -0
  61. package/dist/src/repo/cluster-coordinator.js +539 -0
  62. package/dist/src/repo/cluster-coordinator.js.map +1 -0
  63. package/dist/src/repo/coordinator-repo.d.ts +29 -0
  64. package/dist/src/repo/coordinator-repo.d.ts.map +1 -0
  65. package/dist/src/repo/coordinator-repo.js +102 -0
  66. package/dist/src/repo/coordinator-repo.js.map +1 -0
  67. package/dist/src/repo/redirect.d.ts +14 -0
  68. package/dist/src/repo/redirect.d.ts.map +1 -0
  69. package/dist/src/repo/redirect.js +9 -0
  70. package/dist/src/repo/redirect.js.map +1 -0
  71. package/dist/src/repo/service.d.ts +52 -0
  72. package/dist/src/repo/service.d.ts.map +1 -0
  73. package/dist/src/repo/service.js +181 -0
  74. package/dist/src/repo/service.js.map +1 -0
  75. package/dist/src/repo/types.d.ts +7 -0
  76. package/dist/src/repo/types.d.ts.map +1 -0
  77. package/dist/src/repo/types.js +2 -0
  78. package/dist/src/repo/types.js.map +1 -0
  79. package/dist/src/routing/libp2p-known-peers.d.ts +4 -0
  80. package/dist/src/routing/libp2p-known-peers.d.ts.map +1 -0
  81. package/dist/src/routing/libp2p-known-peers.js +19 -0
  82. package/dist/src/routing/libp2p-known-peers.js.map +1 -0
  83. package/dist/src/routing/responsibility.d.ts +14 -0
  84. package/dist/src/routing/responsibility.d.ts.map +1 -0
  85. package/dist/src/routing/responsibility.js +45 -0
  86. package/dist/src/routing/responsibility.js.map +1 -0
  87. package/dist/src/routing/simple-cluster-coordinator.d.ts +23 -0
  88. package/dist/src/routing/simple-cluster-coordinator.d.ts.map +1 -0
  89. package/dist/src/routing/simple-cluster-coordinator.js +59 -0
  90. package/dist/src/routing/simple-cluster-coordinator.js.map +1 -0
  91. package/dist/src/storage/arachnode-fret-adapter.d.ts +65 -0
  92. package/dist/src/storage/arachnode-fret-adapter.d.ts.map +1 -0
  93. package/dist/src/storage/arachnode-fret-adapter.js +93 -0
  94. package/dist/src/storage/arachnode-fret-adapter.js.map +1 -0
  95. package/dist/src/storage/block-storage.d.ts +31 -0
  96. package/dist/src/storage/block-storage.d.ts.map +1 -0
  97. package/dist/src/storage/block-storage.js +154 -0
  98. package/dist/src/storage/block-storage.js.map +1 -0
  99. package/dist/src/storage/file-storage.d.ts +30 -0
  100. package/dist/src/storage/file-storage.d.ts.map +1 -0
  101. package/dist/src/storage/file-storage.js +127 -0
  102. package/dist/src/storage/file-storage.js.map +1 -0
  103. package/dist/src/storage/helpers.d.ts +3 -0
  104. package/dist/src/storage/helpers.d.ts.map +1 -0
  105. package/dist/src/storage/helpers.js +28 -0
  106. package/dist/src/storage/helpers.js.map +1 -0
  107. package/dist/src/storage/i-block-storage.d.ts +32 -0
  108. package/dist/src/storage/i-block-storage.d.ts.map +1 -0
  109. package/dist/src/storage/i-block-storage.js +2 -0
  110. package/dist/src/storage/i-block-storage.js.map +1 -0
  111. package/dist/src/storage/i-raw-storage.d.ts +20 -0
  112. package/dist/src/storage/i-raw-storage.d.ts.map +1 -0
  113. package/dist/src/storage/i-raw-storage.js +2 -0
  114. package/dist/src/storage/i-raw-storage.js.map +1 -0
  115. package/dist/src/storage/memory-storage.d.ts +27 -0
  116. package/dist/src/storage/memory-storage.d.ts.map +1 -0
  117. package/dist/src/storage/memory-storage.js +87 -0
  118. package/dist/src/storage/memory-storage.js.map +1 -0
  119. package/dist/src/storage/restoration-coordinator-v2.d.ts +63 -0
  120. package/dist/src/storage/restoration-coordinator-v2.d.ts.map +1 -0
  121. package/dist/src/storage/restoration-coordinator-v2.js +157 -0
  122. package/dist/src/storage/restoration-coordinator-v2.js.map +1 -0
  123. package/dist/src/storage/ring-selector.d.ts +56 -0
  124. package/dist/src/storage/ring-selector.d.ts.map +1 -0
  125. package/dist/src/storage/ring-selector.js +118 -0
  126. package/dist/src/storage/ring-selector.js.map +1 -0
  127. package/dist/src/storage/storage-monitor.d.ts +23 -0
  128. package/dist/src/storage/storage-monitor.d.ts.map +1 -0
  129. package/dist/src/storage/storage-monitor.js +40 -0
  130. package/dist/src/storage/storage-monitor.js.map +1 -0
  131. package/dist/src/storage/storage-repo.d.ts +17 -0
  132. package/dist/src/storage/storage-repo.d.ts.map +1 -0
  133. package/dist/src/storage/storage-repo.js +267 -0
  134. package/dist/src/storage/storage-repo.js.map +1 -0
  135. package/dist/src/storage/struct.d.ts +29 -0
  136. package/dist/src/storage/struct.d.ts.map +1 -0
  137. package/dist/src/storage/struct.js +2 -0
  138. package/dist/src/storage/struct.js.map +1 -0
  139. package/dist/src/sync/client.d.ts +27 -0
  140. package/dist/src/sync/client.d.ts.map +1 -0
  141. package/dist/src/sync/client.js +32 -0
  142. package/dist/src/sync/client.js.map +1 -0
  143. package/dist/src/sync/protocol.d.ts +58 -0
  144. package/dist/src/sync/protocol.d.ts.map +1 -0
  145. package/dist/src/sync/protocol.js +12 -0
  146. package/dist/src/sync/protocol.js.map +1 -0
  147. package/dist/src/sync/service.d.ts +62 -0
  148. package/dist/src/sync/service.d.ts.map +1 -0
  149. package/dist/src/sync/service.js +168 -0
  150. package/dist/src/sync/service.js.map +1 -0
  151. package/package.json +73 -0
  152. package/readme.md +497 -0
  153. package/src/cluster/client.ts +63 -0
  154. package/src/cluster/cluster-repo.ts +711 -0
  155. package/src/cluster/partition-detector.ts +158 -0
  156. package/src/cluster/service.ts +156 -0
  157. package/src/index.ts +30 -0
  158. package/src/it-utility.ts +36 -0
  159. package/src/libp2p-key-network.ts +334 -0
  160. package/src/libp2p-node.ts +335 -0
  161. package/src/logger.ts +9 -0
  162. package/src/network/get-network-manager.ts +17 -0
  163. package/src/network/network-manager-service.ts +334 -0
  164. package/src/peer-utils.ts +24 -0
  165. package/src/protocol-client.ts +54 -0
  166. package/src/repo/client.ts +112 -0
  167. package/src/repo/cluster-coordinator.ts +592 -0
  168. package/src/repo/coordinator-repo.ts +137 -0
  169. package/src/repo/redirect.ts +17 -0
  170. package/src/repo/service.ts +219 -0
  171. package/src/repo/types.ts +7 -0
  172. package/src/routing/libp2p-known-peers.ts +26 -0
  173. package/src/routing/responsibility.ts +63 -0
  174. package/src/routing/simple-cluster-coordinator.ts +70 -0
  175. package/src/storage/arachnode-fret-adapter.ts +128 -0
  176. package/src/storage/block-storage.ts +182 -0
  177. package/src/storage/file-storage.ts +163 -0
  178. package/src/storage/helpers.ts +29 -0
  179. package/src/storage/i-block-storage.ts +40 -0
  180. package/src/storage/i-raw-storage.ts +30 -0
  181. package/src/storage/memory-storage.ts +108 -0
  182. package/src/storage/restoration-coordinator-v2.ts +191 -0
  183. package/src/storage/ring-selector.ts +155 -0
  184. package/src/storage/storage-monitor.ts +59 -0
  185. package/src/storage/storage-repo.ts +320 -0
  186. package/src/storage/struct.ts +34 -0
  187. package/src/sync/client.ts +42 -0
  188. package/src/sync/protocol.ts +71 -0
  189. package/src/sync/service.ts +229 -0
@@ -0,0 +1,334 @@
1
+ import type { Startable, Logger, PeerId, Libp2p } from '@libp2p/interface'
2
+ import type { FretService } from 'p2p-fret'
3
+ import { hashKey } from 'p2p-fret'
4
+
5
+ export type NetworkManagerServiceInit = {
6
+ clusterSize?: number
7
+ seedKeys?: Uint8Array[]
8
+ estimation?: { samples: number, kth: number, timeoutMs: number, ttlMs: number }
9
+ readiness?: { minPeers: number, maxWaitMs: number }
10
+ cacheTTLs?: { coordinatorMs: number, clusterMs: number }
11
+ expectedRemotes?: boolean
12
+ allowClusterDownsize?: boolean
13
+ clusterSizeTolerance?: number
14
+ }
15
+
16
+ type Components = {
17
+ logger: { forComponent: (name: string) => Logger },
18
+ registrar: { handle: (...args: any[]) => Promise<void>, unhandle: (...args: any[]) => Promise<void> },
19
+ libp2p?: Libp2p
20
+ }
21
+
22
+ interface WithFretService {
23
+ services?: { fret?: FretService }
24
+ }
25
+
26
+ export class NetworkManagerService implements Startable {
27
+ private running = false
28
+ private readonly log: Logger
29
+ private readonly cfg: Required<NetworkManagerServiceInit>
30
+ private readyPromise: Promise<void> | null = null
31
+ private readonly coordinatorCache = new Map<string, { id: PeerId, expires: number }>()
32
+ private readonly clusterCache = new Map<string, { ids: PeerId[], expires: number }>()
33
+ private lastEstimate: { estimate: number, samples: number, updated: number } | null = null
34
+ // lightweight blacklist (local reputation)
35
+ private readonly blacklist = new Map<string, { score: number, expires: number }>()
36
+ private libp2pRef: Libp2p | undefined
37
+
38
+ constructor(private readonly components: Components, init: NetworkManagerServiceInit = {}) {
39
+ this.log = components.logger.forComponent('db-p2p:network-manager')
40
+ this.cfg = {
41
+ clusterSize: init.clusterSize ?? 1,
42
+ seedKeys: init.seedKeys ?? [],
43
+ estimation: init.estimation ?? { samples: 8, kth: 5, timeoutMs: 1000, ttlMs: 60_000 },
44
+ readiness: init.readiness ?? { minPeers: 1, maxWaitMs: 2000 },
45
+ cacheTTLs: init.cacheTTLs ?? { coordinatorMs: 30 * 60_000, clusterMs: 5 * 60_000 },
46
+ expectedRemotes: init.expectedRemotes ?? false,
47
+ allowClusterDownsize: init.allowClusterDownsize ?? true,
48
+ clusterSizeTolerance: init.clusterSizeTolerance ?? 0.5
49
+ }
50
+ }
51
+
52
+ setLibp2p(libp2p: Libp2p): void {
53
+ this.libp2pRef = libp2p;
54
+ }
55
+
56
+ private getLibp2p(): Libp2p | undefined {
57
+ return this.libp2pRef ?? this.components.libp2p;
58
+ }
59
+
60
+ private getFret(): FretService | undefined {
61
+ const libp2p = this.getLibp2p();
62
+ if (!libp2p) {
63
+ return undefined;
64
+ }
65
+ return (libp2p as unknown as WithFretService).services?.fret;
66
+ }
67
+
68
+ get [Symbol.toStringTag](): string { return '@libp2p/network-manager' }
69
+
70
+ async start(): Promise<void> {
71
+ if (this.running) return
72
+ this.running = true
73
+ // Do not call ready() here; libp2p components may not be fully set yet.
74
+ // Consumers (e.g., CLI) should invoke ready() after node.start().
75
+ }
76
+
77
+ async stop(): Promise<void> {
78
+ this.running = false
79
+ }
80
+
81
+ async ready(): Promise<void> {
82
+ if (this.readyPromise) return this.readyPromise;
83
+ this.readyPromise = (async () => {
84
+ const results = await Promise.allSettled(
85
+ (this.cfg.seedKeys ?? []).map(k => this.seedKey(k))
86
+ );
87
+ const failures = results.filter(r => r.status === 'rejected');
88
+ if (failures.length > 0) {
89
+ this.log('Failed to seed %d keys', failures.length);
90
+ }
91
+ await new Promise(r => setTimeout(r, 50));
92
+ })();
93
+ return this.readyPromise;
94
+ }
95
+
96
+ private async seedKey(key: Uint8Array): Promise<void> {
97
+ const fret = this.getFret();
98
+ if (!fret) {
99
+ throw new Error('FRET service not available for seeding keys');
100
+ }
101
+ const coord = await hashKey(key);
102
+ const _neighbors = fret.getNeighbors(coord, 'both', 1);
103
+ }
104
+
105
+ private toCacheKey(key: Uint8Array): string {
106
+ return Buffer.from(key).toString('base64url')
107
+ }
108
+
109
+ private getKnownPeers(): PeerId[] {
110
+ const libp2p = this.getLibp2p();
111
+ if (!libp2p) {
112
+ return [];
113
+ }
114
+ const selfId: PeerId = libp2p.peerId;
115
+ const storePeers: Array<{ id: PeerId }> = (libp2p.peerStore as any)?.getPeers?.() ?? [];
116
+ const connPeers: PeerId[] = (libp2p.getConnections?.() ?? []).map((c: any) => c.remotePeer);
117
+ const all = [...storePeers.map(p => p.id), ...connPeers];
118
+ const uniq = all.filter((p, i) => all.findIndex(x => x.toString() === p.toString()) === i);
119
+ return uniq.filter((pid: PeerId) => pid.toString() !== selfId.toString());
120
+ }
121
+
122
+ getStatus(): { mode: 'alone' | 'healthy' | 'degraded', connections: number } {
123
+ const libp2p = this.getLibp2p();
124
+ if (!libp2p) {
125
+ return { mode: this.cfg.expectedRemotes ? 'degraded' : 'alone', connections: 0 };
126
+ }
127
+ const peers: Array<{ id: PeerId }> = (libp2p.peerStore as any)?.getPeers?.() ?? [];
128
+ const remotes = peers.filter(p => p.id.toString() !== libp2p.peerId.toString()).length;
129
+ if (remotes === 0) {
130
+ return { mode: this.cfg.expectedRemotes ? 'degraded' : 'alone', connections: 0 };
131
+ }
132
+ return { mode: 'healthy', connections: remotes };
133
+ }
134
+
135
+ async awaitHealthy(minRemotes: number, timeoutMs: number): Promise<boolean> {
136
+ const start = Date.now()
137
+ while (Date.now() - start < timeoutMs) {
138
+ const libp2p = this.getLibp2p()
139
+ if (libp2p) {
140
+ // Require actual active connections, not just peerStore knowledge
141
+ const connections = libp2p.getConnections?.() ?? []
142
+ const connectedPeers = new Set(connections.map((c: any) => c.remotePeer.toString()))
143
+ if (connectedPeers.size >= minRemotes) {
144
+ this.log('awaitHealthy: satisfied with %d connections', connectedPeers.size)
145
+ return true
146
+ }
147
+ }
148
+ await new Promise(r => setTimeout(r, 100))
149
+ }
150
+ // Final check
151
+ const libp2p = this.getLibp2p()
152
+ if (libp2p) {
153
+ const connections = libp2p.getConnections?.() ?? []
154
+ const connectedPeers = new Set(connections.map((c: any) => c.remotePeer.toString()))
155
+ const satisfied = connectedPeers.size >= minRemotes
156
+ this.log('awaitHealthy: timeout - %d connections (needed %d)', connectedPeers.size, minRemotes)
157
+ return satisfied
158
+ }
159
+ return false
160
+ }
161
+
162
+ /**
163
+ * Record a misbehaving peer. Higher score means worse reputation.
164
+ * Entries expire to allow eventual forgiveness.
165
+ */
166
+ reportBadPeer(peerId: PeerId, penalty: number = 1, ttlMs: number = 10 * 60_000): void {
167
+ const id = peerId.toString()
168
+ const prev = this.blacklist.get(id)
169
+ const score = (prev?.score ?? 0) + Math.max(1, penalty)
170
+ this.blacklist.set(id, { score, expires: Date.now() + ttlMs })
171
+ }
172
+
173
+ private isBlacklisted(peerId: PeerId): boolean {
174
+ const id = peerId.toString()
175
+ const rec = this.blacklist.get(id)
176
+ if (!rec) return false
177
+ if (rec.expires <= Date.now()) { this.blacklist.delete(id); return false }
178
+ // simple threshold; can be tuned or exposed later
179
+ return rec.score >= 3
180
+ }
181
+
182
+ recordCoordinator(key: Uint8Array, peerId: PeerId): void {
183
+ const k = this.toCacheKey(key)
184
+ this.coordinatorCache.set(k, { id: peerId, expires: Date.now() + this.cfg.cacheTTLs.coordinatorMs })
185
+ }
186
+
187
+ /**
188
+ * Find the nearest peer to the provided content key using FRET,
189
+ * falling back to self if FRET is unavailable.
190
+ */
191
+ private async findNearestPeerToKey(key: Uint8Array): Promise<PeerId> {
192
+ const fret = this.getFret();
193
+ const libp2p = this.getLibp2p();
194
+
195
+ if (!libp2p) {
196
+ throw new Error('Libp2p not initialized');
197
+ }
198
+
199
+ if (fret) {
200
+ const coord = await hashKey(key);
201
+ const neighbors = fret.getNeighbors(coord, 'both', 1);
202
+ if (neighbors.length > 0) {
203
+ const pidStr = neighbors[0];
204
+ if (pidStr) {
205
+ const { peerIdFromString } = await import('@libp2p/peer-id');
206
+ const pid = peerIdFromString(pidStr);
207
+ if (!this.isBlacklisted(pid)) {
208
+ return pid;
209
+ }
210
+ }
211
+ }
212
+ }
213
+
214
+ // Fallback: choose among self + connected peers + known peers by distance to key
215
+ const connected: PeerId[] = (libp2p.getConnections?.() ?? []).map((c: any) => c.remotePeer);
216
+ const candidates = [libp2p.peerId, ...connected, ...this.getKnownPeers()]
217
+ .filter((p, i, arr) => arr.findIndex(x => x.toString() === p.toString()) === i)
218
+ .filter(p => !this.isBlacklisted(p));
219
+
220
+ if (candidates.length === 0) {
221
+ return libp2p.peerId;
222
+ }
223
+
224
+ const best = candidates.reduce((best: PeerId, cur: PeerId) =>
225
+ this.lexLess(this.xor(best.toMultihash().bytes, key), this.xor(cur.toMultihash().bytes, key)) ? best : cur
226
+ , candidates[0]!);
227
+ return best;
228
+ }
229
+
230
+ /**
231
+ * Compute cluster using FRET's assembleCohort for content-addressed peer selection.
232
+ */
233
+ async getCluster(key: Uint8Array): Promise<PeerId[]> {
234
+ const ck = this.toCacheKey(key);
235
+ const cached = this.clusterCache.get(ck);
236
+ if (cached && cached.expires > Date.now()) {
237
+ return cached.ids;
238
+ }
239
+
240
+ const fret = this.getFret();
241
+ const libp2p = this.getLibp2p();
242
+
243
+ if (!libp2p) {
244
+ throw new Error('Libp2p not initialized');
245
+ }
246
+
247
+ if (fret) {
248
+ const coord = await hashKey(key);
249
+ const diag: any = (fret as any).getDiagnostics?.() ?? {};
250
+ const estimate = typeof diag.estimate === 'number' ? diag.estimate : (typeof diag.n === 'number' ? diag.n : undefined);
251
+ const targetSize = Math.max(1, Math.min(this.cfg.clusterSize, Number.isFinite(estimate) ? (estimate as number) : this.cfg.clusterSize));
252
+ const cohortIds = fret.assembleCohort(coord, targetSize);
253
+ const { peerIdFromString } = await import('@libp2p/peer-id');
254
+
255
+ const ids = cohortIds
256
+ .map(idStr => {
257
+ try {
258
+ return peerIdFromString(idStr);
259
+ } catch (error) {
260
+ this.log('Invalid peer ID in cohort: %s, %o', idStr, error);
261
+ return null;
262
+ }
263
+ })
264
+ .filter((pid): pid is PeerId => pid !== null && !this.isBlacklisted(pid));
265
+
266
+ if (ids.length > 0) {
267
+ this.clusterCache.set(ck, { ids, expires: Date.now() + this.cfg.cacheTTLs.clusterMs });
268
+ this.lastEstimate = estimate != null ? { estimate, samples: diag.samples ?? 0, updated: Date.now() } : this.lastEstimate;
269
+ return ids;
270
+ }
271
+ }
272
+
273
+ // Fallback: peer-centric clustering if FRET unavailable
274
+ const anchor = await this.findNearestPeerToKey(key);
275
+ const anchorMh = anchor.toMultihash().bytes;
276
+ const connected: PeerId[] = (libp2p.getConnections?.() ?? []).map((c: any) => c.remotePeer);
277
+ const candidates = [anchor, libp2p.peerId, ...connected, ...this.getKnownPeers()]
278
+ .filter((p, idx, arr) => !this.isBlacklisted(p) && arr.findIndex(x => x.toString() === p.toString()) === idx);
279
+ const sorted = candidates.sort((a, b) => this.lexLess(this.xor(a.toMultihash().bytes, anchorMh), this.xor(b.toMultihash().bytes, anchorMh)) ? -1 : 1);
280
+ const K = Math.min(this.cfg.clusterSize, sorted.length);
281
+ const ids = sorted.slice(0, K);
282
+ this.clusterCache.set(ck, { ids, expires: Date.now() + this.cfg.cacheTTLs.clusterMs });
283
+ return ids;
284
+ }
285
+
286
+ async getCoordinator(key: Uint8Array): Promise<PeerId> {
287
+ const ck = this.toCacheKey(key);
288
+ const hit = this.coordinatorCache.get(ck);
289
+ if (hit) {
290
+ if (hit.expires > Date.now()) {
291
+ return hit.id;
292
+ } else {
293
+ this.coordinatorCache.delete(ck);
294
+ }
295
+ }
296
+
297
+ const cluster = await this.getCluster(key);
298
+ const libp2p = this.getLibp2p();
299
+ if (!libp2p) {
300
+ throw new Error('Libp2p not initialized');
301
+ }
302
+ const candidate = cluster.find(p => !this.isBlacklisted(p)) ?? libp2p.peerId;
303
+ this.recordCoordinator(key, candidate);
304
+ return candidate;
305
+ }
306
+
307
+ private xor(a: Uint8Array, b: Uint8Array): Uint8Array {
308
+ const len = Math.max(a.length, b.length)
309
+ const out = new Uint8Array(len)
310
+ for (let i = 0; i < len; i++) {
311
+ const ai = a[a.length - 1 - i] ?? 0
312
+ const bi = b[b.length - 1 - i] ?? 0
313
+ out[len - 1 - i] = ai ^ bi
314
+ }
315
+ return out
316
+ }
317
+
318
+ private lexLess(a: Uint8Array, b: Uint8Array): boolean {
319
+ const len = Math.max(a.length, b.length)
320
+ for (let i = 0; i < len; i++) {
321
+ const av = a[i] ?? 0
322
+ const bv = b[i] ?? 0
323
+ if (av < bv) return true
324
+ if (av > bv) return false
325
+ }
326
+ return false
327
+ }
328
+ }
329
+
330
+ export function networkManagerService(init: NetworkManagerServiceInit = {}) {
331
+ return (components: Components) => new NetworkManagerService(components, init)
332
+ }
333
+
334
+
@@ -0,0 +1,24 @@
1
+ function toPeerIdString(id: unknown): string | null {
2
+ try {
3
+ if (id == null) return null
4
+ // PeerId instance
5
+ if (typeof (id as any)?.toString === 'function') return (id as any).toString()
6
+ // Wrapped object { id: PeerId | string }
7
+ const inner = (id as any).id
8
+ if (inner && typeof inner.toString === 'function') return inner.toString()
9
+ if (typeof inner === 'string') return inner
10
+ // Raw string
11
+ if (typeof id === 'string') return id
12
+ return null
13
+ } catch {
14
+ return null
15
+ }
16
+ }
17
+
18
+ export function peersEqual(a: unknown, b: unknown): boolean {
19
+ const as = toPeerIdString(a)
20
+ const bs = toPeerIdString(b)
21
+ return as != null && bs != null && as === bs
22
+ }
23
+
24
+
@@ -0,0 +1,54 @@
1
+ import { pipe } from 'it-pipe';
2
+ import { encode as lpEncode, decode as lpDecode } from 'it-length-prefixed';
3
+ import { pushable } from 'it-pushable';
4
+ import type { PeerId } from '@libp2p/interface';
5
+ import type { IPeerNetwork } from '@optimystic/db-core';
6
+ import { first } from './it-utility.js';
7
+
8
+ /** Base class for clients that communicate via a libp2p protocol */
9
+ export class ProtocolClient {
10
+ constructor(
11
+ protected readonly peerId: PeerId,
12
+ protected readonly peerNetwork: IPeerNetwork,
13
+ ) { }
14
+
15
+ protected async processMessage<T>(
16
+ message: unknown,
17
+ protocol: string,
18
+ options?: { signal?: AbortSignal }
19
+ ): Promise<T> {
20
+ const stream = await this.peerNetwork.connect(
21
+ this.peerId,
22
+ protocol,
23
+ { signal: options?.signal }
24
+ );
25
+
26
+ try {
27
+ const source = pipe(
28
+ stream.source,
29
+ lpDecode,
30
+ async function* (source) {
31
+ for await (const data of source) {
32
+ const decoded = new TextDecoder().decode(data.subarray());
33
+ const parsed = JSON.parse(decoded);
34
+ yield parsed;
35
+ }
36
+ }
37
+ ) as AsyncIterable<T>;
38
+
39
+ const sink = pushable();
40
+ void pipe(
41
+ sink,
42
+ lpEncode,
43
+ stream.sink
44
+ );
45
+
46
+ sink.push(new TextEncoder().encode(JSON.stringify(message)));
47
+ sink.end();
48
+
49
+ return await first(() => source, () => { throw new Error('No response received') });
50
+ } finally {
51
+ stream.close();
52
+ }
53
+ }
54
+ }
@@ -0,0 +1,112 @@
1
+ import type {
2
+ IRepo, GetBlockResults, PendSuccess, StaleFailure, ActionBlocks, MessageOptions, CommitResult,
3
+ PendRequest, CommitRequest, BlockGets, IPeerNetwork
4
+ } from "@optimystic/db-core";
5
+ import type { RepoMessage } from "@optimystic/db-core";
6
+ import type { PeerId } from "@libp2p/interface";
7
+ import { ProtocolClient } from "../protocol-client.js";
8
+ import { peerIdFromString } from "@libp2p/peer-id";
9
+
10
+ export class RepoClient extends ProtocolClient implements IRepo {
11
+ private constructor(peerId: PeerId, peerNetwork: IPeerNetwork, readonly protocolPrefix?: string) {
12
+ super(peerId, peerNetwork);
13
+ }
14
+
15
+ /** Create a new client instance */
16
+ public static create(peerId: PeerId, peerNetwork: IPeerNetwork, protocolPrefix?: string): RepoClient {
17
+ return new RepoClient(peerId, peerNetwork, protocolPrefix);
18
+ }
19
+
20
+ async get(blockGets: BlockGets, options: MessageOptions): Promise<GetBlockResults> {
21
+ return this.processRepoMessage<GetBlockResults>(
22
+ [{ get: blockGets }],
23
+ options
24
+ );
25
+ }
26
+
27
+ async pend(request: PendRequest, options: MessageOptions): Promise<PendSuccess | StaleFailure> {
28
+ return this.processRepoMessage<PendSuccess | StaleFailure>(
29
+ [{ pend: request }],
30
+ options
31
+ );
32
+ }
33
+
34
+ async cancel(actionRef: ActionBlocks, options: MessageOptions): Promise<void> {
35
+ return this.processRepoMessage<void>(
36
+ [{ cancel: { actionRef } }],
37
+ options
38
+ );
39
+ }
40
+
41
+ async commit(request: CommitRequest, options: MessageOptions): Promise<CommitResult> {
42
+ return this.processRepoMessage<CommitResult>(
43
+ [{ commit: request }],
44
+ options
45
+ );
46
+ }
47
+
48
+ private async processRepoMessage<T>(
49
+ operations: RepoMessage['operations'],
50
+ options: MessageOptions,
51
+ hop: number = 0
52
+ ): Promise<T> {
53
+ const message: RepoMessage = {
54
+ operations,
55
+ expiration: options.expiration,
56
+ };
57
+ const deadline = options.expiration ?? (Date.now() + 30_000)
58
+ const msLeft = Math.max(1, deadline - Date.now())
59
+ const withTimeout = async <U>(fn: () => Promise<U>): Promise<U> => {
60
+ return await Promise.race<U>([
61
+ fn(),
62
+ new Promise<never>((_, reject) => setTimeout(() => reject(new Error('RepoClient timeout')), msLeft))
63
+ ])
64
+ }
65
+ let response: any
66
+ const preferred = (this.protocolPrefix ?? '/db-p2p') + '/repo/1.0.0'
67
+ response = await withTimeout(() => super.processMessage<any>(message, preferred, { signal: options?.signal }))
68
+
69
+ if (response?.redirect?.peers?.length) {
70
+ if (hop >= 2) {
71
+ throw new Error('Redirect loop detected in RepoClient (max hops reached)')
72
+ }
73
+ const currentIdStr = this.peerId.toString()
74
+ const next = response.redirect.peers.find((p: any) => p.id !== currentIdStr) ?? response.redirect.peers[0]
75
+ const nextId = peerIdFromString(next.id)
76
+ if (next.id === currentIdStr) {
77
+ throw new Error('Redirect loop detected in RepoClient (same peer)')
78
+ }
79
+ // cache hint
80
+ this.recordCoordinatorForOpsIfSupported(operations, nextId)
81
+ // single-hop retry against target peer using repo protocol
82
+ const nextClient = RepoClient.create(nextId, this.peerNetwork, this.protocolPrefix)
83
+ return await nextClient.processRepoMessage<T>(operations, options, hop + 1)
84
+ }
85
+ return response as T;
86
+ }
87
+
88
+ private extractKeyFromOperations(ops: RepoMessage['operations']): Uint8Array | undefined {
89
+ const op = ops[0];
90
+ if ('get' in op) {
91
+ const id = op.get.blockIds[0];
92
+ return id ? new TextEncoder().encode(id) : undefined;
93
+ }
94
+ if ('pend' in op) {
95
+ const id = Object.keys(op.pend.transforms)[0];
96
+ return id ? new TextEncoder().encode(id) : undefined;
97
+ }
98
+ if ('commit' in op) {
99
+ return new TextEncoder().encode(op.commit.tailId);
100
+ }
101
+ return undefined;
102
+ }
103
+
104
+ private recordCoordinatorForOpsIfSupported(ops: RepoMessage['operations'], peerId: PeerId): void {
105
+ const keyBytes = this.extractKeyFromOperations(ops)
106
+ const pn: any = this.peerNetwork as any
107
+ if (keyBytes != null && typeof pn?.recordCoordinator === 'function') {
108
+ pn.recordCoordinator(keyBytes, peerId)
109
+ }
110
+ }
111
+
112
+ }