@optimystic/db-p2p 0.13.5 → 0.14.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 (342) hide show
  1. package/README.md +6 -6
  2. package/dist/src/cluster/block-transfer-service.d.ts +67 -8
  3. package/dist/src/cluster/block-transfer-service.d.ts.map +1 -1
  4. package/dist/src/cluster/block-transfer-service.js +101 -62
  5. package/dist/src/cluster/block-transfer-service.js.map +1 -1
  6. package/dist/src/cluster/client.d.ts +18 -1
  7. package/dist/src/cluster/client.d.ts.map +1 -1
  8. package/dist/src/cluster/client.js +55 -39
  9. package/dist/src/cluster/client.js.map +1 -1
  10. package/dist/src/cluster/cluster-error.d.ts +38 -0
  11. package/dist/src/cluster/cluster-error.d.ts.map +1 -0
  12. package/dist/src/cluster/cluster-error.js +51 -0
  13. package/dist/src/cluster/cluster-error.js.map +1 -0
  14. package/dist/src/cluster/cluster-repo.d.ts +135 -3
  15. package/dist/src/cluster/cluster-repo.d.ts.map +1 -1
  16. package/dist/src/cluster/cluster-repo.js +333 -58
  17. package/dist/src/cluster/cluster-repo.js.map +1 -1
  18. package/dist/src/cluster/commit-cert.d.ts +63 -0
  19. package/dist/src/cluster/commit-cert.d.ts.map +1 -0
  20. package/dist/src/cluster/commit-cert.js +108 -0
  21. package/dist/src/cluster/commit-cert.js.map +1 -0
  22. package/dist/src/cluster/service.d.ts +46 -11
  23. package/dist/src/cluster/service.d.ts.map +1 -1
  24. package/dist/src/cluster/service.js +97 -33
  25. package/dist/src/cluster/service.js.map +1 -1
  26. package/dist/src/cluster/spread-on-churn.d.ts +13 -0
  27. package/dist/src/cluster/spread-on-churn.d.ts.map +1 -1
  28. package/dist/src/cluster/spread-on-churn.js +34 -4
  29. package/dist/src/cluster/spread-on-churn.js.map +1 -1
  30. package/dist/src/cohort-topic/bootstrap-evidence-builder.d.ts +59 -0
  31. package/dist/src/cohort-topic/bootstrap-evidence-builder.d.ts.map +1 -0
  32. package/dist/src/cohort-topic/bootstrap-evidence-builder.js +79 -0
  33. package/dist/src/cohort-topic/bootstrap-evidence-builder.js.map +1 -0
  34. package/dist/src/cohort-topic/bootstrap-evidence-verifiers.d.ts +81 -0
  35. package/dist/src/cohort-topic/bootstrap-evidence-verifiers.d.ts.map +1 -0
  36. package/dist/src/cohort-topic/bootstrap-evidence-verifiers.js +93 -0
  37. package/dist/src/cohort-topic/bootstrap-evidence-verifiers.js.map +1 -0
  38. package/dist/src/cohort-topic/bootstrap-parent-reference.d.ts +108 -0
  39. package/dist/src/cohort-topic/bootstrap-parent-reference.d.ts.map +1 -0
  40. package/dist/src/cohort-topic/bootstrap-parent-reference.js +108 -0
  41. package/dist/src/cohort-topic/bootstrap-parent-reference.js.map +1 -0
  42. package/dist/src/cohort-topic/change-bridge.d.ts +65 -0
  43. package/dist/src/cohort-topic/change-bridge.d.ts.map +1 -0
  44. package/dist/src/cohort-topic/change-bridge.js +76 -0
  45. package/dist/src/cohort-topic/change-bridge.js.map +1 -0
  46. package/dist/src/cohort-topic/cohort-gossip-driver.d.ts +74 -0
  47. package/dist/src/cohort-topic/cohort-gossip-driver.d.ts.map +1 -0
  48. package/dist/src/cohort-topic/cohort-gossip-driver.js +87 -0
  49. package/dist/src/cohort-topic/cohort-gossip-driver.js.map +1 -0
  50. package/dist/src/cohort-topic/cohort-gossip-transport.d.ts +42 -0
  51. package/dist/src/cohort-topic/cohort-gossip-transport.d.ts.map +1 -0
  52. package/dist/src/cohort-topic/cohort-gossip-transport.js +57 -0
  53. package/dist/src/cohort-topic/cohort-gossip-transport.js.map +1 -0
  54. package/dist/src/cohort-topic/fret-trust-anchor.d.ts +100 -0
  55. package/dist/src/cohort-topic/fret-trust-anchor.d.ts.map +1 -0
  56. package/dist/src/cohort-topic/fret-trust-anchor.js +115 -0
  57. package/dist/src/cohort-topic/fret-trust-anchor.js.map +1 -0
  58. package/dist/src/cohort-topic/host.d.ts +573 -0
  59. package/dist/src/cohort-topic/host.d.ts.map +1 -0
  60. package/dist/src/cohort-topic/host.js +1460 -0
  61. package/dist/src/cohort-topic/host.js.map +1 -0
  62. package/dist/src/cohort-topic/index.d.ts +14 -0
  63. package/dist/src/cohort-topic/index.d.ts.map +1 -0
  64. package/dist/src/cohort-topic/index.js +14 -0
  65. package/dist/src/cohort-topic/index.js.map +1 -0
  66. package/dist/src/cohort-topic/membership-publish-sink.d.ts +14 -0
  67. package/dist/src/cohort-topic/membership-publish-sink.d.ts.map +1 -0
  68. package/dist/src/cohort-topic/membership-publish-sink.js +17 -0
  69. package/dist/src/cohort-topic/membership-publish-sink.js.map +1 -0
  70. package/dist/src/cohort-topic/membership-source.d.ts +36 -0
  71. package/dist/src/cohort-topic/membership-source.d.ts.map +1 -0
  72. package/dist/src/cohort-topic/membership-source.js +58 -0
  73. package/dist/src/cohort-topic/membership-source.js.map +1 -0
  74. package/dist/src/cohort-topic/peer-codec.d.ts +18 -0
  75. package/dist/src/cohort-topic/peer-codec.d.ts.map +1 -0
  76. package/dist/src/cohort-topic/peer-codec.js +26 -0
  77. package/dist/src/cohort-topic/peer-codec.js.map +1 -0
  78. package/dist/src/cohort-topic/peer-sig.d.ts +52 -0
  79. package/dist/src/cohort-topic/peer-sig.d.ts.map +1 -0
  80. package/dist/src/cohort-topic/peer-sig.js +83 -0
  81. package/dist/src/cohort-topic/peer-sig.js.map +1 -0
  82. package/dist/src/cohort-topic/protocols.d.ts +49 -0
  83. package/dist/src/cohort-topic/protocols.d.ts.map +1 -0
  84. package/dist/src/cohort-topic/protocols.js +58 -0
  85. package/dist/src/cohort-topic/protocols.js.map +1 -0
  86. package/dist/src/cohort-topic/reactivity-membership-gate.d.ts +49 -0
  87. package/dist/src/cohort-topic/reactivity-membership-gate.d.ts.map +1 -0
  88. package/dist/src/cohort-topic/reactivity-membership-gate.js +42 -0
  89. package/dist/src/cohort-topic/reactivity-membership-gate.js.map +1 -0
  90. package/dist/src/cohort-topic/size-estimator.d.ts +16 -0
  91. package/dist/src/cohort-topic/size-estimator.d.ts.map +1 -0
  92. package/dist/src/cohort-topic/size-estimator.js +16 -0
  93. package/dist/src/cohort-topic/size-estimator.js.map +1 -0
  94. package/dist/src/cohort-topic/stream-util.d.ts +19 -0
  95. package/dist/src/cohort-topic/stream-util.d.ts.map +1 -0
  96. package/dist/src/cohort-topic/stream-util.js +76 -0
  97. package/dist/src/cohort-topic/stream-util.js.map +1 -0
  98. package/dist/src/cohort-topic/threshold-crypto.d.ts +94 -0
  99. package/dist/src/cohort-topic/threshold-crypto.d.ts.map +1 -0
  100. package/dist/src/cohort-topic/threshold-crypto.js +178 -0
  101. package/dist/src/cohort-topic/threshold-crypto.js.map +1 -0
  102. package/dist/src/cohort-topic/topic-router.d.ts +41 -0
  103. package/dist/src/cohort-topic/topic-router.d.ts.map +1 -0
  104. package/dist/src/cohort-topic/topic-router.js +63 -0
  105. package/dist/src/cohort-topic/topic-router.js.map +1 -0
  106. package/dist/src/dispute/cascade.d.ts +179 -0
  107. package/dist/src/dispute/cascade.d.ts.map +1 -0
  108. package/dist/src/dispute/cascade.js +315 -0
  109. package/dist/src/dispute/cascade.js.map +1 -0
  110. package/dist/src/dispute/client.d.ts +2 -1
  111. package/dist/src/dispute/client.d.ts.map +1 -1
  112. package/dist/src/dispute/client.js +12 -3
  113. package/dist/src/dispute/client.js.map +1 -1
  114. package/dist/src/dispute/dispute-service.d.ts +41 -1
  115. package/dist/src/dispute/dispute-service.d.ts.map +1 -1
  116. package/dist/src/dispute/dispute-service.js +147 -21
  117. package/dist/src/dispute/dispute-service.js.map +1 -1
  118. package/dist/src/dispute/index.d.ts +2 -0
  119. package/dist/src/dispute/index.d.ts.map +1 -1
  120. package/dist/src/dispute/index.js +2 -0
  121. package/dist/src/dispute/index.js.map +1 -1
  122. package/dist/src/dispute/invalidation.d.ts +266 -0
  123. package/dist/src/dispute/invalidation.d.ts.map +1 -0
  124. package/dist/src/dispute/invalidation.js +405 -0
  125. package/dist/src/dispute/invalidation.js.map +1 -0
  126. package/dist/src/dispute/types.d.ts +14 -0
  127. package/dist/src/dispute/types.d.ts.map +1 -1
  128. package/dist/src/dispute/types.js.map +1 -1
  129. package/dist/src/index.d.ts +4 -0
  130. package/dist/src/index.d.ts.map +1 -1
  131. package/dist/src/index.js +4 -0
  132. package/dist/src/index.js.map +1 -1
  133. package/dist/src/libp2p-key-network.d.ts +7 -0
  134. package/dist/src/libp2p-key-network.d.ts.map +1 -1
  135. package/dist/src/libp2p-key-network.js +49 -5
  136. package/dist/src/libp2p-key-network.js.map +1 -1
  137. package/dist/src/libp2p-node-base.d.ts +45 -3
  138. package/dist/src/libp2p-node-base.d.ts.map +1 -1
  139. package/dist/src/libp2p-node-base.js +522 -5
  140. package/dist/src/libp2p-node-base.js.map +1 -1
  141. package/dist/src/matchmaking/aggregate-counts.d.ts +67 -0
  142. package/dist/src/matchmaking/aggregate-counts.d.ts.map +1 -0
  143. package/dist/src/matchmaking/aggregate-counts.js +60 -0
  144. package/dist/src/matchmaking/aggregate-counts.js.map +1 -0
  145. package/dist/src/matchmaking/index.d.ts +20 -0
  146. package/dist/src/matchmaking/index.d.ts.map +1 -0
  147. package/dist/src/matchmaking/index.js +20 -0
  148. package/dist/src/matchmaking/index.js.map +1 -0
  149. package/dist/src/matchmaking/module.d.ts +181 -0
  150. package/dist/src/matchmaking/module.d.ts.map +1 -0
  151. package/dist/src/matchmaking/module.js +221 -0
  152. package/dist/src/matchmaking/module.js.map +1 -0
  153. package/dist/src/matchmaking/protocols.d.ts +37 -0
  154. package/dist/src/matchmaking/protocols.d.ts.map +1 -0
  155. package/dist/src/matchmaking/protocols.js +42 -0
  156. package/dist/src/matchmaking/protocols.js.map +1 -0
  157. package/dist/src/matchmaking/provider-manager.d.ts +52 -0
  158. package/dist/src/matchmaking/provider-manager.d.ts.map +1 -0
  159. package/dist/src/matchmaking/provider-manager.js +74 -0
  160. package/dist/src/matchmaking/provider-manager.js.map +1 -0
  161. package/dist/src/matchmaking/query-handler.d.ts +34 -0
  162. package/dist/src/matchmaking/query-handler.d.ts.map +1 -0
  163. package/dist/src/matchmaking/query-handler.js +62 -0
  164. package/dist/src/matchmaking/query-handler.js.map +1 -0
  165. package/dist/src/matchmaking/query-transport.d.ts +180 -0
  166. package/dist/src/matchmaking/query-transport.d.ts.map +1 -0
  167. package/dist/src/matchmaking/query-transport.js +315 -0
  168. package/dist/src/matchmaking/query-transport.js.map +1 -0
  169. package/dist/src/matchmaking/seeker-manager.d.ts +39 -0
  170. package/dist/src/matchmaking/seeker-manager.d.ts.map +1 -0
  171. package/dist/src/matchmaking/seeker-manager.js +49 -0
  172. package/dist/src/matchmaking/seeker-manager.js.map +1 -0
  173. package/dist/src/matchmaking/seeker-walk-client.d.ts +127 -0
  174. package/dist/src/matchmaking/seeker-walk-client.d.ts.map +1 -0
  175. package/dist/src/matchmaking/seeker-walk-client.js +189 -0
  176. package/dist/src/matchmaking/seeker-walk-client.js.map +1 -0
  177. package/dist/src/matchmaking/traffic-validation.d.ts +128 -0
  178. package/dist/src/matchmaking/traffic-validation.d.ts.map +1 -0
  179. package/dist/src/matchmaking/traffic-validation.js +105 -0
  180. package/dist/src/matchmaking/traffic-validation.js.map +1 -0
  181. package/dist/src/network/network-manager-service.d.ts +4 -2
  182. package/dist/src/network/network-manager-service.d.ts.map +1 -1
  183. package/dist/src/network/network-manager-service.js +9 -3
  184. package/dist/src/network/network-manager-service.js.map +1 -1
  185. package/dist/src/protocol-client.d.ts +13 -0
  186. package/dist/src/protocol-client.d.ts.map +1 -1
  187. package/dist/src/protocol-client.js +74 -2
  188. package/dist/src/protocol-client.js.map +1 -1
  189. package/dist/src/reactivity/forwarder-host.d.ts +198 -0
  190. package/dist/src/reactivity/forwarder-host.d.ts.map +1 -0
  191. package/dist/src/reactivity/forwarder-host.js +355 -0
  192. package/dist/src/reactivity/forwarder-host.js.map +1 -0
  193. package/dist/src/reactivity/index.d.ts +19 -0
  194. package/dist/src/reactivity/index.d.ts.map +1 -0
  195. package/dist/src/reactivity/index.js +19 -0
  196. package/dist/src/reactivity/index.js.map +1 -0
  197. package/dist/src/reactivity/notify-transport.d.ts +70 -0
  198. package/dist/src/reactivity/notify-transport.d.ts.map +1 -0
  199. package/dist/src/reactivity/notify-transport.js +111 -0
  200. package/dist/src/reactivity/notify-transport.js.map +1 -0
  201. package/dist/src/reactivity/origination-manager.d.ts +110 -0
  202. package/dist/src/reactivity/origination-manager.d.ts.map +1 -0
  203. package/dist/src/reactivity/origination-manager.js +129 -0
  204. package/dist/src/reactivity/origination-manager.js.map +1 -0
  205. package/dist/src/reactivity/protocols.d.ts +43 -0
  206. package/dist/src/reactivity/protocols.d.ts.map +1 -0
  207. package/dist/src/reactivity/protocols.js +50 -0
  208. package/dist/src/reactivity/protocols.js.map +1 -0
  209. package/dist/src/reactivity/push-state-gossip.d.ts +132 -0
  210. package/dist/src/reactivity/push-state-gossip.d.ts.map +1 -0
  211. package/dist/src/reactivity/push-state-gossip.js +231 -0
  212. package/dist/src/reactivity/push-state-gossip.js.map +1 -0
  213. package/dist/src/reactivity/recover-transport.d.ts +167 -0
  214. package/dist/src/reactivity/recover-transport.d.ts.map +1 -0
  215. package/dist/src/reactivity/recover-transport.js +308 -0
  216. package/dist/src/reactivity/recover-transport.js.map +1 -0
  217. package/dist/src/reactivity/rotation-rereg-scheduler.d.ts +142 -0
  218. package/dist/src/reactivity/rotation-rereg-scheduler.d.ts.map +1 -0
  219. package/dist/src/reactivity/rotation-rereg-scheduler.js +223 -0
  220. package/dist/src/reactivity/rotation-rereg-scheduler.js.map +1 -0
  221. package/dist/src/reactivity/subscriber-registry.d.ts +47 -0
  222. package/dist/src/reactivity/subscriber-registry.d.ts.map +1 -0
  223. package/dist/src/reactivity/subscriber-registry.js +87 -0
  224. package/dist/src/reactivity/subscriber-registry.js.map +1 -0
  225. package/dist/src/reactivity/subscription-manager.d.ts +221 -0
  226. package/dist/src/reactivity/subscription-manager.d.ts.map +1 -0
  227. package/dist/src/reactivity/subscription-manager.js +302 -0
  228. package/dist/src/reactivity/subscription-manager.js.map +1 -0
  229. package/dist/src/reactivity/topic-bytes.d.ts +32 -0
  230. package/dist/src/reactivity/topic-bytes.d.ts.map +1 -0
  231. package/dist/src/reactivity/topic-bytes.js +34 -0
  232. package/dist/src/reactivity/topic-bytes.js.map +1 -0
  233. package/dist/src/repo/client.d.ts.map +1 -1
  234. package/dist/src/repo/client.js +51 -23
  235. package/dist/src/repo/client.js.map +1 -1
  236. package/dist/src/repo/service.d.ts +46 -1
  237. package/dist/src/repo/service.d.ts.map +1 -1
  238. package/dist/src/repo/service.js +79 -45
  239. package/dist/src/repo/service.js.map +1 -1
  240. package/dist/src/rpc-deadline.d.ts +37 -0
  241. package/dist/src/rpc-deadline.d.ts.map +1 -0
  242. package/dist/src/rpc-deadline.js +31 -0
  243. package/dist/src/rpc-deadline.js.map +1 -0
  244. package/dist/src/storage/block-storage.d.ts +2 -0
  245. package/dist/src/storage/block-storage.d.ts.map +1 -1
  246. package/dist/src/storage/block-storage.js +95 -2
  247. package/dist/src/storage/block-storage.js.map +1 -1
  248. package/dist/src/storage/i-block-storage.d.ts +25 -0
  249. package/dist/src/storage/i-block-storage.d.ts.map +1 -1
  250. package/dist/src/storage/storage-repo.d.ts +53 -2
  251. package/dist/src/storage/storage-repo.d.ts.map +1 -1
  252. package/dist/src/storage/storage-repo.js +193 -9
  253. package/dist/src/storage/storage-repo.js.map +1 -1
  254. package/dist/src/sync/client.d.ts +4 -1
  255. package/dist/src/sync/client.d.ts.map +1 -1
  256. package/dist/src/sync/client.js +5 -2
  257. package/dist/src/sync/client.js.map +1 -1
  258. package/dist/src/testing/cohort-topic-mesh-harness.d.ts +219 -0
  259. package/dist/src/testing/cohort-topic-mesh-harness.d.ts.map +1 -0
  260. package/dist/src/testing/cohort-topic-mesh-harness.js +467 -0
  261. package/dist/src/testing/cohort-topic-mesh-harness.js.map +1 -0
  262. package/dist/src/testing/matchmaking-mesh-harness.d.ts +187 -0
  263. package/dist/src/testing/matchmaking-mesh-harness.d.ts.map +1 -0
  264. package/dist/src/testing/matchmaking-mesh-harness.js +358 -0
  265. package/dist/src/testing/matchmaking-mesh-harness.js.map +1 -0
  266. package/dist/src/testing/mesh-harness.d.ts.map +1 -1
  267. package/dist/src/testing/mesh-harness.js +23 -1
  268. package/dist/src/testing/mesh-harness.js.map +1 -1
  269. package/dist/src/testing/reactivity-mesh-harness.d.ts +298 -0
  270. package/dist/src/testing/reactivity-mesh-harness.d.ts.map +1 -0
  271. package/dist/src/testing/reactivity-mesh-harness.js +663 -0
  272. package/dist/src/testing/reactivity-mesh-harness.js.map +1 -0
  273. package/package.json +7 -4
  274. package/src/cluster/block-transfer-service.ts +133 -68
  275. package/src/cluster/client.ts +64 -45
  276. package/src/cluster/cluster-error.ts +64 -0
  277. package/src/cluster/cluster-repo.ts +405 -55
  278. package/src/cluster/commit-cert.ts +139 -0
  279. package/src/cluster/service.ts +116 -44
  280. package/src/cluster/spread-on-churn.ts +328 -285
  281. package/src/cohort-topic/bootstrap-evidence-builder.ts +122 -0
  282. package/src/cohort-topic/bootstrap-evidence-verifiers.ts +132 -0
  283. package/src/cohort-topic/bootstrap-parent-reference.ts +159 -0
  284. package/src/cohort-topic/change-bridge.ts +109 -0
  285. package/src/cohort-topic/cohort-gossip-driver.ts +144 -0
  286. package/src/cohort-topic/cohort-gossip-transport.ts +74 -0
  287. package/src/cohort-topic/fret-trust-anchor.ts +153 -0
  288. package/src/cohort-topic/host.ts +2175 -0
  289. package/src/cohort-topic/index.ts +13 -0
  290. package/src/cohort-topic/membership-publish-sink.ts +20 -0
  291. package/src/cohort-topic/membership-source.ts +68 -0
  292. package/src/cohort-topic/peer-codec.ts +31 -0
  293. package/src/cohort-topic/peer-sig.ts +86 -0
  294. package/src/cohort-topic/protocols.ts +71 -0
  295. package/src/cohort-topic/reactivity-membership-gate.ts +77 -0
  296. package/src/cohort-topic/size-estimator.ts +16 -0
  297. package/src/cohort-topic/stream-util.ts +87 -0
  298. package/src/cohort-topic/threshold-crypto.ts +239 -0
  299. package/src/cohort-topic/topic-router.ts +77 -0
  300. package/src/dispute/cascade.ts +517 -0
  301. package/src/dispute/client.ts +12 -2
  302. package/src/dispute/dispute-service.ts +171 -24
  303. package/src/dispute/index.ts +42 -0
  304. package/src/dispute/invalidation.ts +587 -0
  305. package/src/dispute/types.ts +14 -0
  306. package/src/index.ts +4 -0
  307. package/src/libp2p-key-network.ts +49 -6
  308. package/src/libp2p-node-base.ts +620 -12
  309. package/src/matchmaking/aggregate-counts.ts +104 -0
  310. package/src/matchmaking/index.ts +20 -0
  311. package/src/matchmaking/module.ts +363 -0
  312. package/src/matchmaking/protocols.ts +51 -0
  313. package/src/matchmaking/provider-manager.ts +95 -0
  314. package/src/matchmaking/query-handler.ts +88 -0
  315. package/src/matchmaking/query-transport.ts +483 -0
  316. package/src/matchmaking/seeker-manager.ts +64 -0
  317. package/src/matchmaking/seeker-walk-client.ts +293 -0
  318. package/src/matchmaking/traffic-validation.ts +195 -0
  319. package/src/network/network-manager-service.ts +9 -2
  320. package/src/protocol-client.ts +64 -3
  321. package/src/reactivity/forwarder-host.ts +438 -0
  322. package/src/reactivity/index.ts +19 -0
  323. package/src/reactivity/notify-transport.ts +144 -0
  324. package/src/reactivity/origination-manager.ts +192 -0
  325. package/src/reactivity/protocols.ts +61 -0
  326. package/src/reactivity/push-state-gossip.ts +291 -0
  327. package/src/reactivity/recover-transport.ts +408 -0
  328. package/src/reactivity/rotation-rereg-scheduler.ts +256 -0
  329. package/src/reactivity/subscriber-registry.ts +96 -0
  330. package/src/reactivity/subscription-manager.ts +450 -0
  331. package/src/reactivity/topic-bytes.ts +37 -0
  332. package/src/repo/client.ts +54 -23
  333. package/src/repo/service.ts +90 -42
  334. package/src/rpc-deadline.ts +45 -0
  335. package/src/storage/block-storage.ts +106 -3
  336. package/src/storage/i-block-storage.ts +27 -0
  337. package/src/storage/storage-repo.ts +209 -13
  338. package/src/sync/client.ts +5 -2
  339. package/src/testing/cohort-topic-mesh-harness.ts +613 -0
  340. package/src/testing/matchmaking-mesh-harness.ts +475 -0
  341. package/src/testing/mesh-harness.ts +22 -2
  342. package/src/testing/reactivity-mesh-harness.ts +922 -0
@@ -0,0 +1,438 @@
1
+ /**
2
+ * Reactivity — forwarder **host** orchestrator (`docs/reactivity.md` §Propagation, §Slow-subscriber
3
+ * backpressure, §Per-cohort policy).
4
+ *
5
+ * This is the piece that turns the in-process forward *decision* (db-core's {@link ReactivityForwarder})
6
+ * into live fan-out over the {@link ReactivityNotifyTransport}. For one {@link NotificationV1} — whether
7
+ * locally originated (the change-bridge's `emit` seam binds to {@link ReactivityForwarderHost.ingest}) or
8
+ * inbound off a dial ({@link ReactivityForwarderHost.onInbound}) — it:
9
+ *
10
+ * 1. lazily instantiates the per-collection {@link PushState} + forwarder behind the Edge policy gate
11
+ * ({@link instantiateForwarderPushState}: `undefined` on an Edge node ⇒ this node never forwards);
12
+ * 2. runs the db-core receive path (verify → dedupe → buffer) and stops on `"duplicate"` / `"untrusted"`;
13
+ * 3. on `"forward"`, fans the **unmodified** frame out to every direct subscriber (through the
14
+ * per-subscriber bounded queue, so a slow/dead subscriber's drops never stall the rest) and every
15
+ * child cohort.
16
+ *
17
+ * It owns the per-subscriber dequeue→dial delivery loop, the bounded-queue backpressure interaction,
18
+ * subscriber-queue eviction (the memory bound — a departed subscriber's queue is reclaimed lazily each
19
+ * fan-out round), and per-topic serialization so the replay ring + dedupe set never interleave across two
20
+ * concurrent notifications for the same collection.
21
+ *
22
+ * **Subscriber-id space.** `selfPeerId`, the {@link ReactivityForwarderHostDeps.directSubscribers} output,
23
+ * the {@link CohortRef.primary} child targets, and every {@link ReactivityNotifyTransport.send} target are
24
+ * the **same** string space (it is also the `perSubscriberQueue` key). This host is agnostic to the concrete
25
+ * encoding so long as it is consistent across all four seams; the **transport** pins which space —
26
+ * {@link import("./notify-transport.js").Libp2pReactivityNotifyTransport.send} calls `peerIdFromString`, so
27
+ * the space is the **canonical peer-id string** (NOT base64url-of-member-bytes — that would silently fail
28
+ * `peerIdFromString` and never dial). The {@link reactivityDirectSubscribers} adapter and the node-wiring
29
+ * layer ([reactivity-notification-transport]) therefore both emit peer-id strings.
30
+ *
31
+ * This module deliberately touches neither the libp2p node assembly ([reactivity-notification-transport])
32
+ * nor gossip ([reactivity-pushstate-gossip]); it depends only on the transport interface + db-core logic,
33
+ * so it is unit-testable with a fake transport.
34
+ */
35
+
36
+ import {
37
+ reactivityTopicId,
38
+ b64urlToBytes,
39
+ bytesToB64url,
40
+ createReactivityForwarder,
41
+ instantiateForwarderPushState,
42
+ decodeSubscribeAppPayload,
43
+ TailDrainGate,
44
+ type NotificationV1,
45
+ type PeerRef,
46
+ type NodeProfile,
47
+ type PushState,
48
+ type PushStateInit,
49
+ type CohortRef,
50
+ type NotificationVerifier,
51
+ type ReactivityForwarder,
52
+ type RegistrationRecord,
53
+ type RotationRedirectV1,
54
+ } from "@optimystic/db-core";
55
+ import type { ReactivityNotifyTransport } from "./notify-transport.js";
56
+ import { bytesToPeerIdString } from "../cohort-topic/peer-codec.js";
57
+ import { createLogger } from "../logger.js";
58
+
59
+ const log = createLogger("reactivity-forwarder-host");
60
+
61
+ /**
62
+ * The reactivity topic id a notification belongs to: `H(tailId ‖ "reactivity")` over the notification's
63
+ * tail anchor. Kept in one place so it matches origination's `reactivityTailBytes` encoding and the
64
+ * subscriber/forwarder verifier's coord derivation byte-for-byte (both decode `tailId` as base64url first).
65
+ */
66
+ export function reactivityNotificationTopicId(n: NotificationV1): Uint8Array {
67
+ return reactivityTopicId(b64urlToBytes(n.tailId));
68
+ }
69
+
70
+ /** The minimal cohort read the {@link reactivityDirectSubscribers} adapter needs (a {@link CoordEngine} satisfies it). */
71
+ export interface ReactivityRecordSource {
72
+ /** This cohort's locally-known direct registration records for `topicId`. */
73
+ records(topicId: Uint8Array): readonly RegistrationRecord[];
74
+ }
75
+
76
+ /**
77
+ * The direct-subscriber **peer-id strings** for a reactivity topic, read from a cohort's
78
+ * {@link RegistrationRecord} set: a record carries a subscriber iff its opaque `appState` decodes to a
79
+ * reactivity {@link import("@optimystic/db-core").SubscribeAppPayloadV1}. `record.participantId` is the
80
+ * subscriber's dialable member id (db-core's `service` pins `participantId = self`), carried as
81
+ * `peerIdToBytes(peerId) = utf8(peerIdString)`; this decodes it back to the canonical peer-id string via
82
+ * {@link bytesToPeerIdString} so the value is both the `transport.send` dial target (which calls
83
+ * `peerIdFromString`) and the `perSubscriberQueue` key. **The whole host runs in this peer-id-string space**
84
+ * — `selfPeerId`, `resolveChildPrimary`, and every `send` target must agree, or a base64url-vs-peer-id
85
+ * mismatch is a silent no-dial. A record whose `appState` is absent or decodes to a non-reactivity payload
86
+ * (e.g. a matchmaking registration sharing the cohort store) is skipped.
87
+ */
88
+ export function reactivityDirectSubscribers(source: ReactivityRecordSource, topicId: Uint8Array): string[] {
89
+ const out: string[] = [];
90
+ for (const record of source.records(topicId)) {
91
+ if (record.appState === undefined) {
92
+ continue;
93
+ }
94
+ try {
95
+ decodeSubscribeAppPayload(record.appState); // throws on a non-reactivity / malformed payload
96
+ } catch {
97
+ continue; // not a reactivity subscriber — leave the record to its own application
98
+ }
99
+ out.push(bytesToPeerIdString(record.participantId));
100
+ }
101
+ return out;
102
+ }
103
+
104
+ /** Construction inputs for a {@link ReactivityForwarderHost}. */
105
+ export interface ReactivityForwarderHostDeps {
106
+ /** One-way notification transport (unicast send + inbound subscribe); fan-out dials each target through it. */
107
+ readonly transport: ReactivityNotifyTransport;
108
+ /** This node's member id, base64url — never dialed; a co-located subscriber is delivered in-process. */
109
+ readonly selfPeerId: string;
110
+ /** Node profile; gates {@link PushState} instantiation (Edge ⇒ subscriber-only, never forwards). */
111
+ readonly profile: NodeProfile;
112
+ /** Build the per-collection {@link PushStateInit} on first ingest for a topic this node serves. */
113
+ readonly pushStateInit: (topicId: Uint8Array, n: NotificationV1) => PushStateInit;
114
+ /** The notification verifier for a topic (a db-core `createNotificationVerifier` over the host's service verifier, T3). */
115
+ readonly verifierFor: (topicId: Uint8Array) => NotificationVerifier;
116
+ /** Direct-subscriber member ids (base64url) for a topic — e.g. {@link reactivityDirectSubscribers} over the cohort. */
117
+ readonly directSubscribers: (topicId: Uint8Array) => string[];
118
+ /** Resolve a child cohort's dialable primary when {@link CohortRef.primary} is absent (e.g. a FRET resolver). */
119
+ readonly resolveChildPrimary?: (ref: CohortRef) => string | undefined;
120
+ /**
121
+ * Route an inbound notification to a co-located subscription manager, when this node also subscribes the
122
+ * topic. **Must be idempotent on `(collectionId, revision)`**: on the {@link ReactivityForwarderHost.onInbound}
123
+ * path a node that is both a cohort member *and* a subscriber invokes this **twice** for the same
124
+ * notification — once in the subscriber role (directly) and once in the forwarder role (when `self` is in
125
+ * {@link directSubscribers}, via `fanOut`'s self-delivery). The db-core subscriber's `(collectionId,
126
+ * revision)` dedupe (`createReactivitySubscriber`) collapses the second call to a no-op, so a correctly
127
+ * wired manager delivers exactly once; a sink without that dedupe would double-deliver.
128
+ */
129
+ readonly deliverLocal?: (topicId: Uint8Array, n: NotificationV1) => void;
130
+ /** Wall clock (unix ms) stamped on `receive`. Default `Date.now`. */
131
+ readonly clock?: () => number;
132
+ }
133
+
134
+ /** A topic this node forwards: its live {@link PushState} and the forwarder driving the receive path over it. */
135
+ interface ServedTopic {
136
+ readonly pushState: PushState;
137
+ readonly forwarder: ReactivityForwarder;
138
+ }
139
+
140
+ /**
141
+ * The reactivity fan-out orchestrator. Build one per node; it serves every reactivity topic the node is a
142
+ * cohort member for, lazily instantiating per-collection state on first ingest. Bind
143
+ * {@link ReactivityForwarderHost.ingest} to the origination `emit` seam and
144
+ * {@link ReactivityForwarderHost.onInbound} to the transport's inbound subscribe.
145
+ */
146
+ export class ReactivityForwarderHost {
147
+ private readonly transport: ReactivityNotifyTransport;
148
+ private readonly selfPeerId: string;
149
+ private readonly profile: NodeProfile;
150
+ private readonly pushStateInit: (topicId: Uint8Array, n: NotificationV1) => PushStateInit;
151
+ private readonly verifierFor: (topicId: Uint8Array) => NotificationVerifier;
152
+ private readonly directSubscribers: (topicId: Uint8Array) => string[];
153
+ private readonly resolveChildPrimary?: (ref: CohortRef) => string | undefined;
154
+ private readonly deliverLocal?: (topicId: Uint8Array, n: NotificationV1) => void;
155
+ private readonly clock: () => number;
156
+
157
+ /** Per-topic served state. `ServedTopic` ⇒ forwards; `null` ⇒ resolved subscriber-only (Edge); absent ⇒ unresolved. */
158
+ private readonly served = new Map<string, ServedTopic | null>();
159
+ /** Per-topic serialization tail: an ingest chains onto its topic's prior ingest so the ring/dedupe never interleave. */
160
+ private readonly ingestTails = new Map<string, Promise<void>>();
161
+ /**
162
+ * Per **old-topic** drain gate: set by {@link markRotated} when this node observes the collection's tail
163
+ * rotate, keyed by the outgoing tail's topicId (base64url). For `T_drain` after the rotation
164
+ * {@link rotationRedirectFor} answers a recover request reaching the outgoing tail with the gate's
165
+ * {@link RotationRedirectV1} ("this moved — go to the new tree"); once the window closes the entry is
166
+ * evicted along with the topic's served `PushState`.
167
+ */
168
+ private readonly rotationGates = new Map<string, TailDrainGate>();
169
+
170
+ constructor(deps: ReactivityForwarderHostDeps) {
171
+ this.transport = deps.transport;
172
+ this.selfPeerId = deps.selfPeerId;
173
+ this.profile = deps.profile;
174
+ this.pushStateInit = deps.pushStateInit;
175
+ this.verifierFor = deps.verifierFor;
176
+ this.directSubscribers = deps.directSubscribers;
177
+ this.resolveChildPrimary = deps.resolveChildPrimary;
178
+ this.deliverLocal = deps.deliverLocal;
179
+ this.clock = deps.clock ?? ((): number => Date.now());
180
+ }
181
+
182
+ /**
183
+ * Local origination emit **and** the inbound forwarder path: receive → forward → fan-out for one
184
+ * notification on `topicId`. Ingests for one topic run strictly in sequence (verify is async and the
185
+ * replay ring + dedupe set must not interleave); ingests for different topics proceed concurrently.
186
+ * Never rejects — a fan-out fault can never surface as a commit failure on the origination seam.
187
+ */
188
+ ingest(topicId: Uint8Array, n: NotificationV1): Promise<void> {
189
+ const key = this.topicKey(topicId);
190
+ const prev = this.ingestTails.get(key) ?? Promise.resolve();
191
+ // Run regardless of the prior ingest's outcome; `ingestSerialized` isolates its own throws, so the
192
+ // chain never accumulates a rejection that would leak out of a later `ingest`.
193
+ const next = prev.then(
194
+ () => this.ingestSerialized(topicId, n, key),
195
+ () => this.ingestSerialized(topicId, n, key),
196
+ );
197
+ this.ingestTails.set(key, next);
198
+ return next;
199
+ }
200
+
201
+ /** One serialized ingest: resolve served state, run receive, fan out on `"forward"`. Isolates all throws. */
202
+ private async ingestSerialized(topicId: Uint8Array, n: NotificationV1, key: string): Promise<void> {
203
+ try {
204
+ const served = this.resolveServed(topicId, n, key);
205
+ if (served === null) {
206
+ return; // Edge / subscriber-only: this node never forwards (delivery rides onInbound → deliverLocal).
207
+ }
208
+ const decision = await served.forwarder.receive(n, this.clock());
209
+ if (decision !== "forward") {
210
+ return; // "duplicate" (already buffered) or "untrusted" (dropped before any state mutation).
211
+ }
212
+ this.fanOut(topicId, served.pushState, n);
213
+ } catch (err) {
214
+ log("ingest failed for topic=%s rev=%d (isolated): %o", key, n.revision, err);
215
+ }
216
+ }
217
+
218
+ /**
219
+ * Inbound notify dial: run the **subscriber** role (in-process delivery to a co-located manager) and the
220
+ * **forwarder** role (receive + fan-out) as applicable. A node that is both a cohort member and a
221
+ * subscriber does both — the forwarder dedupe + the subscriber's `(collectionId, revision)` dedupe keep
222
+ * it idempotent. Never rejects.
223
+ */
224
+ async onInbound(_from: PeerRef, n: NotificationV1): Promise<void> {
225
+ let topicId: Uint8Array;
226
+ try {
227
+ topicId = reactivityNotificationTopicId(n);
228
+ } catch (err) {
229
+ log("onInbound: undecodable tailId on rev=%d (dropped): %o", n.revision, err);
230
+ return;
231
+ }
232
+ // Subscriber role first: deliver in-process to a co-located subscription manager, if any.
233
+ this.deliverInProcess(topicId, n);
234
+ // Forwarder role: ingest self-gates (Edge ⇒ no PushState; a non-member ⇒ empty fan-out).
235
+ await this.ingest(topicId, n);
236
+ }
237
+
238
+ /** The live {@link PushState} for a served topic, or `undefined` (Edge, or not yet ingested). Test/diagnostic. */
239
+ pushStateFor(topicId: Uint8Array): PushState | undefined {
240
+ const served = this.served.get(this.topicKey(topicId));
241
+ return served === undefined || served === null ? undefined : served.pushState;
242
+ }
243
+
244
+ /**
245
+ * Every live forwarder {@link PushState} this node currently serves: the topics it has ingested at least
246
+ * one notification for and instantiated state behind the Edge gate (subscriber-only `null`s and
247
+ * not-yet-ingested topics are skipped). The push-state-gossip driver iterates these each round to
248
+ * broadcast convergence state to each collection's cohort, so a member that missed an origin dial still
249
+ * ends up holding the replay entry.
250
+ */
251
+ livePushStates(): PushState[] {
252
+ const out: PushState[] = [];
253
+ for (const served of this.served.values()) {
254
+ if (served !== null) {
255
+ out.push(served.pushState);
256
+ }
257
+ }
258
+ return out;
259
+ }
260
+
261
+ /**
262
+ * The live {@link PushState} this node serves for `collectionId` (base64url), or `undefined` if none.
263
+ * The served map is keyed by **topicId**, but a backfill request carries only a collectionId and a
264
+ * resume that lost its tail must still resolve the collection's current tail — so this scans for the
265
+ * served topic whose `PushState.collectionId` matches. Across a tail-rotation **drain window** a
266
+ * collection can briefly have two served topics (the outgoing and incoming tail); the one with the
267
+ * highest `lastRevision` is the current tail, so that is the one returned. (The precise drain-window
268
+ * redirect choreography is `reactivity-tail-rotation-transport`'s; this picks the current tail.)
269
+ */
270
+ pushStateForCollection(collectionId: string): PushState | undefined {
271
+ let best: PushState | undefined;
272
+ for (const served of this.served.values()) {
273
+ if (served === null || served.pushState.collectionId !== collectionId) {
274
+ continue;
275
+ }
276
+ if (best === undefined || served.pushState.lastRevision > best.lastRevision) {
277
+ best = served.pushState;
278
+ }
279
+ }
280
+ return best;
281
+ }
282
+
283
+ /**
284
+ * Record that the topic anchored on `oldTopicId` has **rotated** to a successor tail, starting a
285
+ * {@link TailDrainGate} so the outgoing tail bounces recover requests to the new tree for `T_drain`
286
+ * (`docs/reactivity.md` §Tail rotation step 2). db-core derives the redirect's `newTopicId` internally via
287
+ * `reactivityTopicId(newTailId)`. The trigger is origination observing the collection's `event.tailId`
288
+ * change ({@link reactivity-rotation-host-wiring-e2e}); this seam is called directly in unit tests.
289
+ *
290
+ * **Idempotent / chained.** A second `markRotated` for the **same** successor is a no-op; a `markRotated`
291
+ * to a **later** successor (higher `effectiveAtRevision`) replaces the gate — so a chained OLD→A→B rotation
292
+ * advances the redirect to the most recent successor and restarts its drain window from `now`. An earlier
293
+ * (or equal) successor leaves the existing gate untouched.
294
+ */
295
+ markRotated(oldTopicId: Uint8Array, redirect: { newTailId: string; effectiveAtRevision: number }, now: number): void {
296
+ const key = this.topicKey(oldTopicId);
297
+ const existing = this.rotationGates.get(key);
298
+ if (existing !== undefined && redirect.effectiveAtRevision <= existing.rotationRedirect.effectiveAtRevision) {
299
+ return; // same or earlier successor — the gate already points at this-or-a-later tail.
300
+ }
301
+ this.rotationGates.set(key, new TailDrainGate({
302
+ rotatedAt: now,
303
+ newTailId: redirect.newTailId,
304
+ effectiveAtRevision: redirect.effectiveAtRevision,
305
+ }));
306
+ }
307
+
308
+ /**
309
+ * The drain-window redirect for a recover request that reached the outgoing tail anchored on `oldTopicId`,
310
+ * or `undefined` if this topic never rotated / its drain window has elapsed. While the gate is draining
311
+ * (`isDraining(now)`) returns its {@link RotationRedirectV1}; once `T_drain` closes, evicts the gate **and**
312
+ * the topic's served `PushState` (the old tail originates nothing further — this also reclaims the
313
+ * `served` / `ingestTails` maps the 12.31 review flagged as un-evicted on rotation) and returns `undefined`,
314
+ * so the next request gets no reply and the subscriber re-walks / chain-reads.
315
+ */
316
+ rotationRedirectFor(oldTopicId: Uint8Array, now: number): RotationRedirectV1 | undefined {
317
+ const key = this.topicKey(oldTopicId);
318
+ const gate = this.rotationGates.get(key);
319
+ if (gate === undefined) {
320
+ return undefined;
321
+ }
322
+ if (gate.isDraining(now)) {
323
+ return gate.rotationRedirect;
324
+ }
325
+ // Drain window closed: the outgoing tail is done. Drop the gate and reclaim its served state so the
326
+ // per-topic maps don't leak across rotations.
327
+ this.rotationGates.delete(key);
328
+ this.served.delete(key);
329
+ this.ingestTails.delete(key);
330
+ return undefined;
331
+ }
332
+
333
+ /**
334
+ * Fan the unmodified frame out: reconcile (GC) departed-subscriber queues, enqueue per-subscriber
335
+ * (drop-oldest under pressure), drain each queue and deliver (self in-process, others dialed, per-target
336
+ * isolated), then dial each resolved child cohort. One slow/dead target never blocks the loop for the rest.
337
+ */
338
+ private fanOut(topicId: Uint8Array, pushState: PushState, n: NotificationV1): void {
339
+ const subscriberIds = this.directSubscribers(topicId);
340
+
341
+ // Memory bound: drop any per-subscriber queue whose id left the live set (departed / TTL-expired /
342
+ // withdrawn). The map can never grow past the live subscriber set + the current round (lazy GC, so it
343
+ // shrinks at most one fan-out round late) — this satisfies the `subscribers × queue_max × size` bound.
344
+ this.reconcileQueues(pushState, subscriberIds);
345
+
346
+ // Enqueue onto each subscriber's bounded queue (a slow subscriber's drop-oldest isolates to its queue).
347
+ pushState.enqueueForSubscribers(subscriberIds, n);
348
+
349
+ // Drain + deliver per subscriber, isolated: one dead subscriber's dial never stalls the others.
350
+ for (const subId of subscriberIds) {
351
+ const queue = pushState.perSubscriberQueue.peekQueue(subId);
352
+ if (queue === undefined) {
353
+ continue;
354
+ }
355
+ for (const m of queue.drain()) {
356
+ this.deliverTo(topicId, subId, m);
357
+ }
358
+ }
359
+
360
+ // Child cohorts get the unmodified frame (forwarders never re-sign). No-op until the parent/child link
361
+ // lands ([cohort-topic-parent-child-link]) populates `childCohorts`.
362
+ for (const ref of pushState.childCohorts) {
363
+ const target = ref.primary ?? this.resolveChildPrimary?.(ref);
364
+ if (target !== undefined && target !== this.selfPeerId) {
365
+ this.dispatch(target, n);
366
+ }
367
+ }
368
+ }
369
+
370
+ /** Reclaim the queues of subscribers no longer in the live set (lazy, one round late). */
371
+ private reconcileQueues(pushState: PushState, subscriberIds: readonly string[]): void {
372
+ const live = new Set(subscriberIds);
373
+ // Snapshot the keys: `remove` mutates the backing map, which would invalidate a live iterator.
374
+ for (const id of [...pushState.perSubscriberQueue.subscribers()]) {
375
+ if (!live.has(id)) {
376
+ pushState.perSubscriberQueue.remove(id);
377
+ }
378
+ }
379
+ }
380
+
381
+ /** Deliver one notification to one subscriber: self in-process, everyone else over the transport. */
382
+ private deliverTo(topicId: Uint8Array, subId: string, n: NotificationV1): void {
383
+ if (subId === this.selfPeerId) {
384
+ this.deliverInProcess(topicId, n);
385
+ } else {
386
+ this.dispatch(subId, n);
387
+ }
388
+ }
389
+
390
+ /** Route to a co-located subscription manager, isolating a throwing subscriber from the rest of the fan-out. */
391
+ private deliverInProcess(topicId: Uint8Array, n: NotificationV1): void {
392
+ if (this.deliverLocal === undefined) {
393
+ return;
394
+ }
395
+ try {
396
+ this.deliverLocal(topicId, n);
397
+ } catch (err) {
398
+ log("local delivery threw (isolated) for rev=%d: %o", n.revision, err);
399
+ }
400
+ }
401
+
402
+ /** Fire one unicast send, isolated: a synchronous throw or a rejection is swallowed, never the loop's concern. */
403
+ private dispatch(target: string, n: NotificationV1): void {
404
+ let pending: Promise<void>;
405
+ try {
406
+ pending = this.transport.send(target, n);
407
+ } catch (err) {
408
+ log("notify send to %s threw synchronously (isolated): %o", target, err);
409
+ return;
410
+ }
411
+ void pending.catch((err: unknown) => {
412
+ log("notify send to %s rejected (isolated): %o", target, err);
413
+ });
414
+ }
415
+
416
+ /** Resolve (instantiating once, behind the Edge gate) the served state for a topic. */
417
+ private resolveServed(topicId: Uint8Array, n: NotificationV1, key: string): ServedTopic | null {
418
+ const existing = this.served.get(key);
419
+ if (existing !== undefined) {
420
+ return existing; // a `ServedTopic` or a remembered `null` (Edge / subscriber-only)
421
+ }
422
+ const pushState = instantiateForwarderPushState(this.profile, this.pushStateInit(topicId, n));
423
+ if (pushState === undefined) {
424
+ this.served.set(key, null); // Edge node: remember it never forwards this topic.
425
+ return null;
426
+ }
427
+ const served: ServedTopic = {
428
+ pushState,
429
+ forwarder: createReactivityForwarder({ state: pushState, verifier: this.verifierFor(topicId) }),
430
+ };
431
+ this.served.set(key, served);
432
+ return served;
433
+ }
434
+
435
+ private topicKey(topicId: Uint8Array): string {
436
+ return bytesToB64url(topicId);
437
+ }
438
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Reactivity — db-p2p wiring to the cohort-topic substrate.
3
+ *
4
+ * The db-core `reactivity` module owns the wire codecs, topic anchor, config, origination assembler,
5
+ * forwarder/dedupe/replay logic, and subscriber-side verify/deliver; these managers bind that to the
6
+ * participant-facing `CohortTopicService` (subscribe/renew/withdraw at tier T3) and install the
7
+ * `onLocalCommit` origination hook. See `docs/reactivity.md` §Subscription / §Notification origination.
8
+ */
9
+
10
+ export * from "./topic-bytes.js";
11
+ export * from "./protocols.js";
12
+ export * from "./notify-transport.js";
13
+ export * from "./recover-transport.js";
14
+ export * from "./subscription-manager.js";
15
+ export * from "./rotation-rereg-scheduler.js";
16
+ export * from "./origination-manager.js";
17
+ export * from "./forwarder-host.js";
18
+ export * from "./push-state-gossip.js";
19
+ export * from "./subscriber-registry.js";
@@ -0,0 +1,144 @@
1
+ /**
2
+ * Reactivity notify transport — the one-way `NotificationV1` delivery primitive (`docs/reactivity.md`
3
+ * §Propagation).
4
+ *
5
+ * Unlike the cohort-gossip transport (which *broadcasts* a frame to a FRET-assembled cohort), notify is
6
+ * **unicast**: the fan-out orchestration above this layer (the `reactivity-forwarder-host` ticket) decides
7
+ * who to dial and calls {@link ReactivityNotifyTransport.send} once per named target. This module owns only
8
+ * the framing + dial + inbound-decode plumbing — no fan-out, no role decision, no gossip.
9
+ *
10
+ * The db-core `NotificationV1` codec ({@link encodeNotificationV1} / {@link decodeNotificationV1}) does the
11
+ * length-prefixed JSON framing; this layer rides one self-delimiting frame each way over the notify
12
+ * protocol, reusing the cohort-topic {@link sendOneWay} / {@link readAllBounded} stream lifecycle so the
13
+ * two protocol families behave identically on the wire.
14
+ *
15
+ * Failure isolation is the load-bearing property: notify is fire-and-forget and hint-only, so a dead /
16
+ * unreachable target's rejection is swallowed (logged), never propagated to the caller's fan-out loop or a
17
+ * commit. There is no reply frame — a handler that tried to send one back would desync the dialer's
18
+ * {@link sendOneWay} (which closes after send).
19
+ */
20
+
21
+ import type { NotificationV1, PeerRef } from "@optimystic/db-core";
22
+ import { encodeNotificationV1, decodeNotificationV1 } from "@optimystic/db-core";
23
+ import type { Libp2p } from "libp2p";
24
+ import type { Connection, Stream } from "@libp2p/interface";
25
+ import { peerIdFromString } from "@libp2p/peer-id";
26
+ import { readAllBounded } from "p2p-fret";
27
+ import { peerIdToBytes } from "../cohort-topic/peer-codec.js";
28
+ import { sendOneWay, DEFAULT_STREAM_MAX_BYTES } from "../cohort-topic/stream-util.js";
29
+ import { PROTOCOL_REACTIVITY_NOTIFY } from "./protocols.js";
30
+ import { createLogger } from "../logger.js";
31
+
32
+ const log = createLogger("reactivity-notify");
33
+
34
+ /** One-way `NotificationV1` transport: unicast send, inbound subscribe, and the host's deliver seam. */
35
+ export interface ReactivityNotifyTransport {
36
+ /**
37
+ * Frame `n` ({@link encodeNotificationV1}) and dial `target` (peer-id string) over the notify protocol.
38
+ * Fire-and-forget, failure-isolated: a dead/unreachable target's rejection is swallowed (logged), never
39
+ * propagated to the caller's fan-out loop.
40
+ */
41
+ send(target: string, n: NotificationV1): Promise<void>;
42
+ /** Subscribe to inbound notifications (after decode); returns an unsubscribe handle. */
43
+ onNotification(handler: (from: PeerRef, n: NotificationV1) => void): () => void;
44
+ /** Feed an inbound notify frame (called by the host's notify protocol handler). */
45
+ deliver(fromPeerId: string, frame: Uint8Array): void;
46
+ }
47
+
48
+ /** Construction options for {@link Libp2pReactivityNotifyTransport}. */
49
+ export interface ReactivityNotifyTransportOptions {
50
+ /** Notify protocol ID; default {@link PROTOCOL_REACTIVITY_NOTIFY}. */
51
+ readonly notifyProtocol?: string;
52
+ /** Per-frame ceiling for the inbound decode bound. Default {@link DEFAULT_STREAM_MAX_BYTES}. */
53
+ readonly maxBytes?: number;
54
+ /** This node's peer-id string; when set, {@link Libp2pReactivityNotifyTransport.send} never dials self. */
55
+ readonly selfPeerId?: string;
56
+ }
57
+
58
+ /**
59
+ * libp2p-backed {@link ReactivityNotifyTransport}: {@link send} frames + dials a single target over
60
+ * `/optimystic/reactivity/1.0.0/notify` (fire-and-forget, failure-isolated); inbound frames arrive through
61
+ * the host's notify protocol handler ({@link registerNotifyHandler}), which calls {@link deliver}.
62
+ * Subscribers registered via {@link onNotification} see every decoded notification.
63
+ */
64
+ export class Libp2pReactivityNotifyTransport implements ReactivityNotifyTransport {
65
+ private readonly handlers = new Set<(from: PeerRef, n: NotificationV1) => void>();
66
+ private readonly notifyProtocol: string;
67
+ private readonly maxBytes: number;
68
+ private readonly selfPeerId?: string;
69
+
70
+ constructor(private readonly node: Libp2p, options: ReactivityNotifyTransportOptions = {}) {
71
+ this.notifyProtocol = options.notifyProtocol ?? PROTOCOL_REACTIVITY_NOTIFY;
72
+ this.maxBytes = options.maxBytes ?? DEFAULT_STREAM_MAX_BYTES;
73
+ this.selfPeerId = options.selfPeerId;
74
+ }
75
+
76
+ send(target: string, n: NotificationV1): Promise<void> {
77
+ if (this.selfPeerId !== undefined && target === this.selfPeerId) {
78
+ // Never dial self; a co-located subscriber is delivered in-process by the forwarder host.
79
+ return Promise.resolve();
80
+ }
81
+ try {
82
+ const frame = encodeNotificationV1(n);
83
+ return sendOneWay(this.node, peerIdFromString(target), this.notifyProtocol, frame).catch((err: unknown) => {
84
+ // Best-effort, failure-isolated: a dead/unreachable target must not break the fan-out or a commit.
85
+ log("send to %s failed (swallowed): %o", target, err);
86
+ });
87
+ } catch (err) {
88
+ // Malformed notification or peer-id string: log + drop, never reject (reactivity is hint-only).
89
+ log("dropped a send to %s: %o", target, err);
90
+ return Promise.resolve();
91
+ }
92
+ }
93
+
94
+ onNotification(handler: (from: PeerRef, n: NotificationV1) => void): () => void {
95
+ this.handlers.add(handler);
96
+ return () => this.handlers.delete(handler);
97
+ }
98
+
99
+ /** Feed an inbound notify frame (called by the host's notify protocol handler). */
100
+ deliver(fromPeerId: string, frame: Uint8Array): void {
101
+ let n: NotificationV1;
102
+ try {
103
+ n = decodeNotificationV1(frame, this.maxBytes);
104
+ } catch (err) {
105
+ // A malformed frame must never throw out of a stream handler: log + drop.
106
+ log("dropped an undecodable inbound frame from %s: %o", fromPeerId, err);
107
+ return;
108
+ }
109
+ const from: PeerRef = { id: peerIdToBytes(fromPeerId) };
110
+ for (const handler of this.handlers) {
111
+ handler(from, n);
112
+ }
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Register the inbound notify protocol handler: read one bounded frame and hand it to
118
+ * {@link ReactivityNotifyTransport.deliver}, then close. One-way — no reply frame (notify is strictly
119
+ * fire-and-forget; a reply would desync the dialer's {@link sendOneWay}). Mirrors the cohort-topic
120
+ * one-way handlers: a read error aborts the stream, and {@link ReactivityNotifyTransport.deliver}
121
+ * swallows a decode failure, so the handler never throws on the stream.
122
+ */
123
+ export function registerNotifyHandler(
124
+ node: Libp2p,
125
+ protocol: string,
126
+ transport: ReactivityNotifyTransport,
127
+ maxBytes = DEFAULT_STREAM_MAX_BYTES,
128
+ ): void {
129
+ void node.handle(protocol, (stream: Stream, connection: Connection) => {
130
+ void (async (): Promise<void> => {
131
+ try {
132
+ const frame = await readAllBounded(stream, maxBytes);
133
+ transport.deliver(connection.remotePeer.toString(), frame);
134
+ await stream.close();
135
+ } catch {
136
+ try {
137
+ stream.abort(new Error("reactivity notify stream handler error"));
138
+ } catch {
139
+ /* already aborted */
140
+ }
141
+ }
142
+ })();
143
+ });
144
+ }