@optimystic/db-p2p 0.13.4 → 0.14.0

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 (352) 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 +8 -1
  134. package/dist/src/libp2p-key-network.d.ts.map +1 -1
  135. package/dist/src/libp2p-key-network.js +67 -8
  136. package/dist/src/libp2p-key-network.js.map +1 -1
  137. package/dist/src/libp2p-node-base.d.ts +57 -3
  138. package/dist/src/libp2p-node-base.d.ts.map +1 -1
  139. package/dist/src/libp2p-node-base.js +523 -6
  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 +25 -0
  186. package/dist/src/protocol-client.d.ts.map +1 -1
  187. package/dist/src/protocol-client.js +126 -4
  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 -19
  235. package/dist/src/repo/client.js.map +1 -1
  236. package/dist/src/repo/cluster-coordinator.d.ts +9 -0
  237. package/dist/src/repo/cluster-coordinator.d.ts.map +1 -1
  238. package/dist/src/repo/cluster-coordinator.js +65 -22
  239. package/dist/src/repo/cluster-coordinator.js.map +1 -1
  240. package/dist/src/repo/coordinator-repo.d.ts +20 -0
  241. package/dist/src/repo/coordinator-repo.d.ts.map +1 -1
  242. package/dist/src/repo/coordinator-repo.js +101 -16
  243. package/dist/src/repo/coordinator-repo.js.map +1 -1
  244. package/dist/src/repo/service.d.ts +46 -1
  245. package/dist/src/repo/service.d.ts.map +1 -1
  246. package/dist/src/repo/service.js +79 -45
  247. package/dist/src/repo/service.js.map +1 -1
  248. package/dist/src/rpc-deadline.d.ts +37 -0
  249. package/dist/src/rpc-deadline.d.ts.map +1 -0
  250. package/dist/src/rpc-deadline.js +31 -0
  251. package/dist/src/rpc-deadline.js.map +1 -0
  252. package/dist/src/storage/block-storage.d.ts +2 -0
  253. package/dist/src/storage/block-storage.d.ts.map +1 -1
  254. package/dist/src/storage/block-storage.js +95 -2
  255. package/dist/src/storage/block-storage.js.map +1 -1
  256. package/dist/src/storage/i-block-storage.d.ts +25 -0
  257. package/dist/src/storage/i-block-storage.d.ts.map +1 -1
  258. package/dist/src/storage/storage-repo.d.ts +53 -2
  259. package/dist/src/storage/storage-repo.d.ts.map +1 -1
  260. package/dist/src/storage/storage-repo.js +193 -9
  261. package/dist/src/storage/storage-repo.js.map +1 -1
  262. package/dist/src/sync/client.d.ts +4 -1
  263. package/dist/src/sync/client.d.ts.map +1 -1
  264. package/dist/src/sync/client.js +5 -2
  265. package/dist/src/sync/client.js.map +1 -1
  266. package/dist/src/testing/cohort-topic-mesh-harness.d.ts +219 -0
  267. package/dist/src/testing/cohort-topic-mesh-harness.d.ts.map +1 -0
  268. package/dist/src/testing/cohort-topic-mesh-harness.js +467 -0
  269. package/dist/src/testing/cohort-topic-mesh-harness.js.map +1 -0
  270. package/dist/src/testing/matchmaking-mesh-harness.d.ts +187 -0
  271. package/dist/src/testing/matchmaking-mesh-harness.d.ts.map +1 -0
  272. package/dist/src/testing/matchmaking-mesh-harness.js +358 -0
  273. package/dist/src/testing/matchmaking-mesh-harness.js.map +1 -0
  274. package/dist/src/testing/mesh-harness.d.ts.map +1 -1
  275. package/dist/src/testing/mesh-harness.js +23 -1
  276. package/dist/src/testing/mesh-harness.js.map +1 -1
  277. package/dist/src/testing/reactivity-mesh-harness.d.ts +298 -0
  278. package/dist/src/testing/reactivity-mesh-harness.d.ts.map +1 -0
  279. package/dist/src/testing/reactivity-mesh-harness.js +663 -0
  280. package/dist/src/testing/reactivity-mesh-harness.js.map +1 -0
  281. package/package.json +7 -4
  282. package/src/cluster/block-transfer-service.ts +133 -68
  283. package/src/cluster/client.ts +64 -45
  284. package/src/cluster/cluster-error.ts +64 -0
  285. package/src/cluster/cluster-repo.ts +405 -55
  286. package/src/cluster/commit-cert.ts +139 -0
  287. package/src/cluster/service.ts +116 -44
  288. package/src/cluster/spread-on-churn.ts +328 -285
  289. package/src/cohort-topic/bootstrap-evidence-builder.ts +122 -0
  290. package/src/cohort-topic/bootstrap-evidence-verifiers.ts +132 -0
  291. package/src/cohort-topic/bootstrap-parent-reference.ts +159 -0
  292. package/src/cohort-topic/change-bridge.ts +109 -0
  293. package/src/cohort-topic/cohort-gossip-driver.ts +144 -0
  294. package/src/cohort-topic/cohort-gossip-transport.ts +74 -0
  295. package/src/cohort-topic/fret-trust-anchor.ts +153 -0
  296. package/src/cohort-topic/host.ts +2175 -0
  297. package/src/cohort-topic/index.ts +13 -0
  298. package/src/cohort-topic/membership-publish-sink.ts +20 -0
  299. package/src/cohort-topic/membership-source.ts +68 -0
  300. package/src/cohort-topic/peer-codec.ts +31 -0
  301. package/src/cohort-topic/peer-sig.ts +86 -0
  302. package/src/cohort-topic/protocols.ts +71 -0
  303. package/src/cohort-topic/reactivity-membership-gate.ts +77 -0
  304. package/src/cohort-topic/size-estimator.ts +16 -0
  305. package/src/cohort-topic/stream-util.ts +87 -0
  306. package/src/cohort-topic/threshold-crypto.ts +239 -0
  307. package/src/cohort-topic/topic-router.ts +77 -0
  308. package/src/dispute/cascade.ts +517 -0
  309. package/src/dispute/client.ts +12 -2
  310. package/src/dispute/dispute-service.ts +171 -24
  311. package/src/dispute/index.ts +42 -0
  312. package/src/dispute/invalidation.ts +587 -0
  313. package/src/dispute/types.ts +14 -0
  314. package/src/index.ts +4 -0
  315. package/src/libp2p-key-network.ts +67 -9
  316. package/src/libp2p-node-base.ts +633 -14
  317. package/src/matchmaking/aggregate-counts.ts +104 -0
  318. package/src/matchmaking/index.ts +20 -0
  319. package/src/matchmaking/module.ts +363 -0
  320. package/src/matchmaking/protocols.ts +51 -0
  321. package/src/matchmaking/provider-manager.ts +95 -0
  322. package/src/matchmaking/query-handler.ts +88 -0
  323. package/src/matchmaking/query-transport.ts +483 -0
  324. package/src/matchmaking/seeker-manager.ts +64 -0
  325. package/src/matchmaking/seeker-walk-client.ts +293 -0
  326. package/src/matchmaking/traffic-validation.ts +195 -0
  327. package/src/network/network-manager-service.ts +9 -2
  328. package/src/protocol-client.ts +119 -5
  329. package/src/reactivity/forwarder-host.ts +438 -0
  330. package/src/reactivity/index.ts +19 -0
  331. package/src/reactivity/notify-transport.ts +144 -0
  332. package/src/reactivity/origination-manager.ts +192 -0
  333. package/src/reactivity/protocols.ts +61 -0
  334. package/src/reactivity/push-state-gossip.ts +291 -0
  335. package/src/reactivity/recover-transport.ts +408 -0
  336. package/src/reactivity/rotation-rereg-scheduler.ts +256 -0
  337. package/src/reactivity/subscriber-registry.ts +96 -0
  338. package/src/reactivity/subscription-manager.ts +450 -0
  339. package/src/reactivity/topic-bytes.ts +37 -0
  340. package/src/repo/client.ts +54 -19
  341. package/src/repo/cluster-coordinator.ts +66 -26
  342. package/src/repo/coordinator-repo.ts +99 -15
  343. package/src/repo/service.ts +90 -42
  344. package/src/rpc-deadline.ts +45 -0
  345. package/src/storage/block-storage.ts +106 -3
  346. package/src/storage/i-block-storage.ts +27 -0
  347. package/src/storage/storage-repo.ts +209 -13
  348. package/src/sync/client.ts +5 -2
  349. package/src/testing/cohort-topic-mesh-harness.ts +613 -0
  350. package/src/testing/matchmaking-mesh-harness.ts +475 -0
  351. package/src/testing/mesh-harness.ts +22 -2
  352. package/src/testing/reactivity-mesh-harness.ts +922 -0
@@ -7,6 +7,39 @@ import { createLogger } from './logger.js';
7
7
 
8
8
  const log = createLogger('protocol-client');
9
9
 
10
+ /**
11
+ * Thrown when the per-peer dial deadline expires before a stream is established.
12
+ * Distinct from a libp2p dial failure (no route, refused, etc.) so the
13
+ * batch-retry loop and diagnostic surfaces can identify a slow/unreachable peer
14
+ * specifically. `.code === DIAL_TIMEOUT_ERROR_CODE`.
15
+ */
16
+ export const DIAL_TIMEOUT_ERROR_CODE = 'DIAL_TIMEOUT';
17
+
18
+ export class DialTimeoutError extends Error {
19
+ readonly code = DIAL_TIMEOUT_ERROR_CODE;
20
+ constructor(peer: string, protocol: string, ms: number) {
21
+ super(`dial timeout: peer=${peer} protocol=${protocol} after ${ms}ms`);
22
+ this.name = 'DialTimeoutError';
23
+ }
24
+ }
25
+
26
+ /**
27
+ * Thrown when a peer dialed successfully but the response-read deadline expired
28
+ * before it wrote a reply (it connected, then went silent). Distinct from
29
+ * {@link DialTimeoutError} (never connected) and from a parent cancellation
30
+ * (`options.signal`), so callers/diagnostics can tell "peer went quiet" apart
31
+ * from "peer was unreachable" and "we cancelled". `.code === RESPONSE_TIMEOUT_ERROR_CODE`.
32
+ */
33
+ export const RESPONSE_TIMEOUT_ERROR_CODE = 'RESPONSE_TIMEOUT';
34
+
35
+ export class ResponseTimeoutError extends Error {
36
+ readonly code = RESPONSE_TIMEOUT_ERROR_CODE;
37
+ constructor(peer: string, protocol: string, ms: number) {
38
+ super(`response timeout: peer=${peer} protocol=${protocol} after ${ms}ms`);
39
+ this.name = 'ResponseTimeoutError';
40
+ }
41
+ }
42
+
10
43
  /** Base class for clients that communicate via a libp2p protocol */
11
44
  export class ProtocolClient {
12
45
  constructor(
@@ -17,27 +50,91 @@ export class ProtocolClient {
17
50
  protected async processMessage<T>(
18
51
  message: unknown,
19
52
  protocol: string,
20
- options?: { signal?: AbortSignal; correlationId?: string }
53
+ options?: { signal?: AbortSignal; correlationId?: string; dialTimeoutMs?: number; responseTimeoutMs?: number }
21
54
  ): Promise<T> {
22
55
  const peer = this.peerId.toString();
23
56
  const cid = options?.correlationId;
24
57
  log('dial peer=%s protocol=%s%s', peer, protocol, cid ? ` cid=${cid}` : '');
25
58
  const t0 = Date.now();
26
59
 
60
+ // Per-peer dial deadline. When set, an unreachable peer fails fast so the
61
+ // caller can re-pick a different coordinator — independent of any overall
62
+ // transaction budget the caller may also be enforcing.
63
+ const dialTimeoutMs = options?.dialTimeoutMs;
64
+ const dialController = dialTimeoutMs && dialTimeoutMs > 0 ? new AbortController() : undefined;
65
+ let dialTimer: ReturnType<typeof setTimeout> | undefined;
66
+ const onParentAbort = () => dialController?.abort(options?.signal?.reason);
67
+ if (dialController) {
68
+ dialTimer = setTimeout(() => {
69
+ dialController.abort(new DialTimeoutError(peer, protocol, dialTimeoutMs!));
70
+ }, dialTimeoutMs);
71
+ if (options?.signal) {
72
+ if (options.signal.aborted) dialController.abort(options.signal.reason);
73
+ else options.signal.addEventListener('abort', onParentAbort, { once: true });
74
+ }
75
+ }
76
+ const dialSignal = dialController?.signal ?? options?.signal;
77
+
27
78
  let stream: Libp2pStream;
28
79
  try {
29
80
  stream = await this.peerNetwork.connect(
30
81
  this.peerId,
31
82
  protocol,
32
- { signal: options?.signal }
83
+ { signal: dialSignal }
33
84
  ) as unknown as Libp2pStream;
34
85
  } catch (err) {
35
- log('dial:fail peer=%s protocol=%s ms=%d%s', peer, protocol, Date.now() - t0, cid ? ` cid=${cid}` : '');
86
+ const elapsed = Date.now() - t0;
87
+ // If the dial AbortController fired due to our own timer, surface the
88
+ // dial-timeout error rather than the underlying AbortError so callers
89
+ // can distinguish "peer was slow" from "user/parent cancelled".
90
+ if (dialController?.signal.aborted && dialController.signal.reason instanceof DialTimeoutError) {
91
+ log('dial:timeout peer=%s protocol=%s ms=%d%s', peer, protocol, elapsed, cid ? ` cid=${cid}` : '');
92
+ throw dialController.signal.reason;
93
+ }
94
+ const errCode = (err as { code?: unknown })?.code;
95
+ const errMessage = err instanceof Error ? err.message : String(err);
96
+ const truncatedMsg = errMessage.length > 200 ? errMessage.slice(0, 200) + '…' : errMessage;
97
+ log('dial:fail peer=%s protocol=%s ms=%d code=%s msg=%s%s',
98
+ peer, protocol, elapsed,
99
+ typeof errCode === 'string' && errCode.length > 0 ? errCode : 'none',
100
+ truncatedMsg,
101
+ cid ? ` cid=${cid}` : ''
102
+ );
36
103
  throw err;
104
+ } finally {
105
+ if (dialTimer) clearTimeout(dialTimer);
106
+ if (options?.signal) options.signal.removeEventListener('abort', onParentAbort);
37
107
  }
38
108
  log('dial:ok peer=%s ms=%d%s', peer, Date.now() - t0, cid ? ` cid=${cid}` : '');
39
109
 
110
+ // Per-peer response deadline. The dial controller is already cleared once a
111
+ // stream is established, so a peer that connects but then never writes a frame
112
+ // (and never closes the stream) would hang the `first(...)` read below forever.
113
+ // Setting a timer alone is not enough — the underlying `for await` over the
114
+ // libp2p stream keeps awaiting regardless. The decisive action is to actively
115
+ // `stream.abort(...)`, which rejects the stream's async iterator and unblocks
116
+ // the read. A parent `options.signal` abort is forwarded the same way so a
117
+ // cancelled caller tears the stream down rather than leaking it. When neither
118
+ // `responseTimeoutMs` nor `signal` is supplied, no cap is imposed — preserving
119
+ // every existing caller (mirrors how omitting `dialTimeoutMs` imposes no dial cap).
120
+ const responseTimeoutMs = options?.responseTimeoutMs;
121
+ let responseTimer: ReturnType<typeof setTimeout> | undefined;
122
+ let responseTimeoutError: ResponseTimeoutError | undefined;
123
+ const onParentAbortResponse = () => {
124
+ try { stream.abort(options?.signal?.reason ?? new Error('aborted')); } catch { /* already torn down */ }
125
+ };
40
126
  try {
127
+ if (responseTimeoutMs && responseTimeoutMs > 0) {
128
+ responseTimer = setTimeout(() => {
129
+ responseTimeoutError = new ResponseTimeoutError(peer, protocol, responseTimeoutMs);
130
+ try { stream.abort(responseTimeoutError); } catch { /* already torn down */ }
131
+ }, responseTimeoutMs);
132
+ }
133
+ if (options?.signal) {
134
+ if (options.signal.aborted) onParentAbortResponse();
135
+ else options.signal.addEventListener('abort', onParentAbortResponse, { once: true });
136
+ }
137
+
41
138
  // Send the request using length-prefixed encoding
42
139
  const encoded = pipe(
43
140
  [new TextEncoder().encode(JSON.stringify(message))],
@@ -65,11 +162,28 @@ export class ProtocolClient {
65
162
  }
66
163
  ) as AsyncIterable<T>;
67
164
 
68
- const result = await first(() => source, () => { throw new Error('No response received') });
165
+ let result: T;
166
+ try {
167
+ result = await first(() => source, () => { throw new Error('No response received') });
168
+ } catch (err) {
169
+ // Aborting the stream (our timer or a parent abort) surfaces here as an
170
+ // iterator error. Translate it so callers see why the read ended.
171
+ if (responseTimeoutError) {
172
+ log('response:timeout peer=%s protocol=%s ms=%d%s', peer, protocol, Date.now() - t0, cid ? ` cid=${cid}` : '');
173
+ throw responseTimeoutError;
174
+ }
175
+ if (options?.signal?.aborted) {
176
+ throw options.signal.reason;
177
+ }
178
+ throw err;
179
+ }
69
180
  log('response peer=%s protocol=%s ms=%d%s', peer, protocol, Date.now() - t0, cid ? ` cid=${cid}` : '');
70
181
  return result;
71
182
  } finally {
72
- await stream.close();
183
+ if (responseTimer) clearTimeout(responseTimer);
184
+ if (options?.signal) options.signal.removeEventListener('abort', onParentAbortResponse);
185
+ // Closing an already-aborted stream must be safe.
186
+ try { await stream.close(); } catch { /* already torn down */ }
73
187
  }
74
188
  }
75
189
  }
@@ -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";