@optimystic/db-core 0.13.5 → 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 (447) hide show
  1. package/dist/src/cluster/structs.d.ts +13 -0
  2. package/dist/src/cluster/structs.d.ts.map +1 -1
  3. package/dist/src/cohort-topic/addressing.d.ts +53 -0
  4. package/dist/src/cohort-topic/addressing.d.ts.map +1 -0
  5. package/dist/src/cohort-topic/addressing.js +95 -0
  6. package/dist/src/cohort-topic/addressing.js.map +1 -0
  7. package/dist/src/cohort-topic/antidos/bootstrap-evidence-envelope.d.ts +105 -0
  8. package/dist/src/cohort-topic/antidos/bootstrap-evidence-envelope.d.ts.map +1 -0
  9. package/dist/src/cohort-topic/antidos/bootstrap-evidence-envelope.js +197 -0
  10. package/dist/src/cohort-topic/antidos/bootstrap-evidence-envelope.js.map +1 -0
  11. package/dist/src/cohort-topic/antidos/bootstrap-evidence.d.ts +50 -0
  12. package/dist/src/cohort-topic/antidos/bootstrap-evidence.d.ts.map +1 -0
  13. package/dist/src/cohort-topic/antidos/bootstrap-evidence.js +62 -0
  14. package/dist/src/cohort-topic/antidos/bootstrap-evidence.js.map +1 -0
  15. package/dist/src/cohort-topic/antidos/index.d.ts +6 -0
  16. package/dist/src/cohort-topic/antidos/index.d.ts.map +1 -0
  17. package/dist/src/cohort-topic/antidos/index.js +6 -0
  18. package/dist/src/cohort-topic/antidos/index.js.map +1 -0
  19. package/dist/src/cohort-topic/antidos/rate-limiter.d.ts +95 -0
  20. package/dist/src/cohort-topic/antidos/rate-limiter.d.ts.map +1 -0
  21. package/dist/src/cohort-topic/antidos/rate-limiter.js +146 -0
  22. package/dist/src/cohort-topic/antidos/rate-limiter.js.map +1 -0
  23. package/dist/src/cohort-topic/antidos/replay-guard.d.ts +37 -0
  24. package/dist/src/cohort-topic/antidos/replay-guard.d.ts.map +1 -0
  25. package/dist/src/cohort-topic/antidos/replay-guard.js +78 -0
  26. package/dist/src/cohort-topic/antidos/replay-guard.js.map +1 -0
  27. package/dist/src/cohort-topic/antidos/topic-budget.d.ts +50 -0
  28. package/dist/src/cohort-topic/antidos/topic-budget.d.ts.map +1 -0
  29. package/dist/src/cohort-topic/antidos/topic-budget.js +95 -0
  30. package/dist/src/cohort-topic/antidos/topic-budget.js.map +1 -0
  31. package/dist/src/cohort-topic/antiflood/index.d.ts +3 -0
  32. package/dist/src/cohort-topic/antiflood/index.d.ts.map +1 -0
  33. package/dist/src/cohort-topic/antiflood/index.js +3 -0
  34. package/dist/src/cohort-topic/antiflood/index.js.map +1 -0
  35. package/dist/src/cohort-topic/antiflood/invariants.d.ts +70 -0
  36. package/dist/src/cohort-topic/antiflood/invariants.d.ts.map +1 -0
  37. package/dist/src/cohort-topic/antiflood/invariants.js +86 -0
  38. package/dist/src/cohort-topic/antiflood/invariants.js.map +1 -0
  39. package/dist/src/cohort-topic/antiflood/jitter.d.ts +65 -0
  40. package/dist/src/cohort-topic/antiflood/jitter.d.ts.map +1 -0
  41. package/dist/src/cohort-topic/antiflood/jitter.js +75 -0
  42. package/dist/src/cohort-topic/antiflood/jitter.js.map +1 -0
  43. package/dist/src/cohort-topic/coldstart.d.ts +117 -0
  44. package/dist/src/cohort-topic/coldstart.d.ts.map +1 -0
  45. package/dist/src/cohort-topic/coldstart.js +127 -0
  46. package/dist/src/cohort-topic/coldstart.js.map +1 -0
  47. package/dist/src/cohort-topic/dmax.d.ts +44 -0
  48. package/dist/src/cohort-topic/dmax.d.ts.map +1 -0
  49. package/dist/src/cohort-topic/dmax.js +70 -0
  50. package/dist/src/cohort-topic/dmax.js.map +1 -0
  51. package/dist/src/cohort-topic/gossip/bus.d.ts +85 -0
  52. package/dist/src/cohort-topic/gossip/bus.d.ts.map +1 -0
  53. package/dist/src/cohort-topic/gossip/bus.js +146 -0
  54. package/dist/src/cohort-topic/gossip/bus.js.map +1 -0
  55. package/dist/src/cohort-topic/gossip/index.d.ts +4 -0
  56. package/dist/src/cohort-topic/gossip/index.d.ts.map +1 -0
  57. package/dist/src/cohort-topic/gossip/index.js +4 -0
  58. package/dist/src/cohort-topic/gossip/index.js.map +1 -0
  59. package/dist/src/cohort-topic/gossip/records.d.ts +12 -0
  60. package/dist/src/cohort-topic/gossip/records.d.ts.map +1 -0
  61. package/dist/src/cohort-topic/gossip/records.js +41 -0
  62. package/dist/src/cohort-topic/gossip/records.js.map +1 -0
  63. package/dist/src/cohort-topic/gossip/view.d.ts +39 -0
  64. package/dist/src/cohort-topic/gossip/view.d.ts.map +1 -0
  65. package/dist/src/cohort-topic/gossip/view.js +30 -0
  66. package/dist/src/cohort-topic/gossip/view.js.map +1 -0
  67. package/dist/src/cohort-topic/index.d.ts +21 -0
  68. package/dist/src/cohort-topic/index.d.ts.map +1 -0
  69. package/dist/src/cohort-topic/index.js +21 -0
  70. package/dist/src/cohort-topic/index.js.map +1 -0
  71. package/dist/src/cohort-topic/load/barometer.d.ts +78 -0
  72. package/dist/src/cohort-topic/load/barometer.d.ts.map +1 -0
  73. package/dist/src/cohort-topic/load/barometer.js +93 -0
  74. package/dist/src/cohort-topic/load/barometer.js.map +1 -0
  75. package/dist/src/cohort-topic/load/index.d.ts +2 -0
  76. package/dist/src/cohort-topic/load/index.d.ts.map +1 -0
  77. package/dist/src/cohort-topic/load/index.js +2 -0
  78. package/dist/src/cohort-topic/load/index.js.map +1 -0
  79. package/dist/src/cohort-topic/member-engine.d.ts +134 -0
  80. package/dist/src/cohort-topic/member-engine.d.ts.map +1 -0
  81. package/dist/src/cohort-topic/member-engine.js +264 -0
  82. package/dist/src/cohort-topic/member-engine.js.map +1 -0
  83. package/dist/src/cohort-topic/membership/index.d.ts +4 -0
  84. package/dist/src/cohort-topic/membership/index.d.ts.map +1 -0
  85. package/dist/src/cohort-topic/membership/index.js +4 -0
  86. package/dist/src/cohort-topic/membership/index.js.map +1 -0
  87. package/dist/src/cohort-topic/membership/publisher.d.ts +82 -0
  88. package/dist/src/cohort-topic/membership/publisher.d.ts.map +1 -0
  89. package/dist/src/cohort-topic/membership/publisher.js +107 -0
  90. package/dist/src/cohort-topic/membership/publisher.js.map +1 -0
  91. package/dist/src/cohort-topic/membership/source.d.ts +31 -0
  92. package/dist/src/cohort-topic/membership/source.d.ts.map +1 -0
  93. package/dist/src/cohort-topic/membership/source.js +32 -0
  94. package/dist/src/cohort-topic/membership/source.js.map +1 -0
  95. package/dist/src/cohort-topic/membership/verifier.d.ts +96 -0
  96. package/dist/src/cohort-topic/membership/verifier.d.ts.map +1 -0
  97. package/dist/src/cohort-topic/membership/verifier.js +250 -0
  98. package/dist/src/cohort-topic/membership/verifier.js.map +1 -0
  99. package/dist/src/cohort-topic/ports.d.ts +149 -0
  100. package/dist/src/cohort-topic/ports.d.ts.map +1 -0
  101. package/dist/src/cohort-topic/ports.js +25 -0
  102. package/dist/src/cohort-topic/ports.js.map +1 -0
  103. package/dist/src/cohort-topic/promotion.d.ts +135 -0
  104. package/dist/src/cohort-topic/promotion.d.ts.map +1 -0
  105. package/dist/src/cohort-topic/promotion.js +269 -0
  106. package/dist/src/cohort-topic/promotion.js.map +1 -0
  107. package/dist/src/cohort-topic/registration/bytes.d.ts +16 -0
  108. package/dist/src/cohort-topic/registration/bytes.d.ts.map +1 -0
  109. package/dist/src/cohort-topic/registration/bytes.js +32 -0
  110. package/dist/src/cohort-topic/registration/bytes.js.map +1 -0
  111. package/dist/src/cohort-topic/registration/handoff.d.ts +69 -0
  112. package/dist/src/cohort-topic/registration/handoff.d.ts.map +1 -0
  113. package/dist/src/cohort-topic/registration/handoff.js +98 -0
  114. package/dist/src/cohort-topic/registration/handoff.js.map +1 -0
  115. package/dist/src/cohort-topic/registration/index.d.ts +7 -0
  116. package/dist/src/cohort-topic/registration/index.d.ts.map +1 -0
  117. package/dist/src/cohort-topic/registration/index.js +7 -0
  118. package/dist/src/cohort-topic/registration/index.js.map +1 -0
  119. package/dist/src/cohort-topic/registration/renewal.d.ts +115 -0
  120. package/dist/src/cohort-topic/registration/renewal.d.ts.map +1 -0
  121. package/dist/src/cohort-topic/registration/renewal.js +306 -0
  122. package/dist/src/cohort-topic/registration/renewal.js.map +1 -0
  123. package/dist/src/cohort-topic/registration/sharding.d.ts +31 -0
  124. package/dist/src/cohort-topic/registration/sharding.d.ts.map +1 -0
  125. package/dist/src/cohort-topic/registration/sharding.js +48 -0
  126. package/dist/src/cohort-topic/registration/sharding.js.map +1 -0
  127. package/dist/src/cohort-topic/registration/store.d.ts +13 -0
  128. package/dist/src/cohort-topic/registration/store.d.ts.map +1 -0
  129. package/dist/src/cohort-topic/registration/store.js +72 -0
  130. package/dist/src/cohort-topic/registration/store.js.map +1 -0
  131. package/dist/src/cohort-topic/registration/types.d.ts +71 -0
  132. package/dist/src/cohort-topic/registration/types.d.ts.map +1 -0
  133. package/dist/src/cohort-topic/registration/types.js +27 -0
  134. package/dist/src/cohort-topic/registration/types.js.map +1 -0
  135. package/dist/src/cohort-topic/ring-hash.d.ts +27 -0
  136. package/dist/src/cohort-topic/ring-hash.d.ts.map +1 -0
  137. package/dist/src/cohort-topic/ring-hash.js +45 -0
  138. package/dist/src/cohort-topic/ring-hash.js.map +1 -0
  139. package/dist/src/cohort-topic/service.d.ts +157 -0
  140. package/dist/src/cohort-topic/service.d.ts.map +1 -0
  141. package/dist/src/cohort-topic/service.js +237 -0
  142. package/dist/src/cohort-topic/service.js.map +1 -0
  143. package/dist/src/cohort-topic/sig/index.d.ts +3 -0
  144. package/dist/src/cohort-topic/sig/index.d.ts.map +1 -0
  145. package/dist/src/cohort-topic/sig/index.js +3 -0
  146. package/dist/src/cohort-topic/sig/index.js.map +1 -0
  147. package/dist/src/cohort-topic/sig/payloads.d.ts +25 -0
  148. package/dist/src/cohort-topic/sig/payloads.d.ts.map +1 -0
  149. package/dist/src/cohort-topic/sig/payloads.js +25 -0
  150. package/dist/src/cohort-topic/sig/payloads.js.map +1 -0
  151. package/dist/src/cohort-topic/sig/threshold.d.ts +33 -0
  152. package/dist/src/cohort-topic/sig/threshold.d.ts.map +1 -0
  153. package/dist/src/cohort-topic/sig/threshold.js +51 -0
  154. package/dist/src/cohort-topic/sig/threshold.js.map +1 -0
  155. package/dist/src/cohort-topic/tiers.d.ts +58 -0
  156. package/dist/src/cohort-topic/tiers.d.ts.map +1 -0
  157. package/dist/src/cohort-topic/tiers.js +49 -0
  158. package/dist/src/cohort-topic/tiers.js.map +1 -0
  159. package/dist/src/cohort-topic/traffic.d.ts +90 -0
  160. package/dist/src/cohort-topic/traffic.d.ts.map +1 -0
  161. package/dist/src/cohort-topic/traffic.js +146 -0
  162. package/dist/src/cohort-topic/traffic.js.map +1 -0
  163. package/dist/src/cohort-topic/walk.d.ts +123 -0
  164. package/dist/src/cohort-topic/walk.d.ts.map +1 -0
  165. package/dist/src/cohort-topic/walk.js +150 -0
  166. package/dist/src/cohort-topic/walk.js.map +1 -0
  167. package/dist/src/cohort-topic/willingness.d.ts +123 -0
  168. package/dist/src/cohort-topic/willingness.d.ts.map +1 -0
  169. package/dist/src/cohort-topic/willingness.js +143 -0
  170. package/dist/src/cohort-topic/willingness.js.map +1 -0
  171. package/dist/src/cohort-topic/wire/codec.d.ts +42 -0
  172. package/dist/src/cohort-topic/wire/codec.d.ts.map +1 -0
  173. package/dist/src/cohort-topic/wire/codec.js +163 -0
  174. package/dist/src/cohort-topic/wire/codec.js.map +1 -0
  175. package/dist/src/cohort-topic/wire/index.d.ts +6 -0
  176. package/dist/src/cohort-topic/wire/index.d.ts.map +1 -0
  177. package/dist/src/cohort-topic/wire/index.js +6 -0
  178. package/dist/src/cohort-topic/wire/index.js.map +1 -0
  179. package/dist/src/cohort-topic/wire/payloads.d.ts +38 -0
  180. package/dist/src/cohort-topic/wire/payloads.d.ts.map +1 -0
  181. package/dist/src/cohort-topic/wire/payloads.js +102 -0
  182. package/dist/src/cohort-topic/wire/payloads.js.map +1 -0
  183. package/dist/src/cohort-topic/wire/types.d.ts +304 -0
  184. package/dist/src/cohort-topic/wire/types.d.ts.map +1 -0
  185. package/dist/src/cohort-topic/wire/types.js +16 -0
  186. package/dist/src/cohort-topic/wire/types.js.map +1 -0
  187. package/dist/src/cohort-topic/wire/validate.d.ts +25 -0
  188. package/dist/src/cohort-topic/wire/validate.d.ts.map +1 -0
  189. package/dist/src/cohort-topic/wire/validate.js +427 -0
  190. package/dist/src/cohort-topic/wire/validate.js.map +1 -0
  191. package/dist/src/collection/collection.d.ts +58 -1
  192. package/dist/src/collection/collection.d.ts.map +1 -1
  193. package/dist/src/collection/collection.js +76 -0
  194. package/dist/src/collection/collection.js.map +1 -1
  195. package/dist/src/collections/tree/tree.d.ts +58 -2
  196. package/dist/src/collections/tree/tree.d.ts.map +1 -1
  197. package/dist/src/collections/tree/tree.js +59 -2
  198. package/dist/src/collections/tree/tree.js.map +1 -1
  199. package/dist/src/index.d.ts +3 -0
  200. package/dist/src/index.d.ts.map +1 -1
  201. package/dist/src/index.js +3 -0
  202. package/dist/src/index.js.map +1 -1
  203. package/dist/src/log/log.d.ts +42 -4
  204. package/dist/src/log/log.d.ts.map +1 -1
  205. package/dist/src/log/log.js +71 -3
  206. package/dist/src/log/log.js.map +1 -1
  207. package/dist/src/log/struct.d.ts +122 -2
  208. package/dist/src/log/struct.d.ts.map +1 -1
  209. package/dist/src/log/struct.js.map +1 -1
  210. package/dist/src/matchmaking/capability-filter.d.ts +24 -0
  211. package/dist/src/matchmaking/capability-filter.d.ts.map +1 -0
  212. package/dist/src/matchmaking/capability-filter.js +37 -0
  213. package/dist/src/matchmaking/capability-filter.js.map +1 -0
  214. package/dist/src/matchmaking/config.d.ts +77 -0
  215. package/dist/src/matchmaking/config.d.ts.map +1 -0
  216. package/dist/src/matchmaking/config.js +69 -0
  217. package/dist/src/matchmaking/config.js.map +1 -0
  218. package/dist/src/matchmaking/index.d.ts +21 -0
  219. package/dist/src/matchmaking/index.d.ts.map +1 -0
  220. package/dist/src/matchmaking/index.js +21 -0
  221. package/dist/src/matchmaking/index.js.map +1 -0
  222. package/dist/src/matchmaking/multi-cohort-seeker.d.ts +133 -0
  223. package/dist/src/matchmaking/multi-cohort-seeker.d.ts.map +1 -0
  224. package/dist/src/matchmaking/multi-cohort-seeker.js +142 -0
  225. package/dist/src/matchmaking/multi-cohort-seeker.js.map +1 -0
  226. package/dist/src/matchmaking/provider.d.ts +70 -0
  227. package/dist/src/matchmaking/provider.d.ts.map +1 -0
  228. package/dist/src/matchmaking/provider.js +93 -0
  229. package/dist/src/matchmaking/provider.js.map +1 -0
  230. package/dist/src/matchmaking/query-eval.d.ts +53 -0
  231. package/dist/src/matchmaking/query-eval.d.ts.map +1 -0
  232. package/dist/src/matchmaking/query-eval.js +69 -0
  233. package/dist/src/matchmaking/query-eval.js.map +1 -0
  234. package/dist/src/matchmaking/seeker-walk.d.ts +95 -0
  235. package/dist/src/matchmaking/seeker-walk.d.ts.map +1 -0
  236. package/dist/src/matchmaking/seeker-walk.js +75 -0
  237. package/dist/src/matchmaking/seeker-walk.js.map +1 -0
  238. package/dist/src/matchmaking/seeker.d.ts +51 -0
  239. package/dist/src/matchmaking/seeker.d.ts.map +1 -0
  240. package/dist/src/matchmaking/seeker.js +62 -0
  241. package/dist/src/matchmaking/seeker.js.map +1 -0
  242. package/dist/src/matchmaking/topic-anchor.d.ts +54 -0
  243. package/dist/src/matchmaking/topic-anchor.d.ts.map +1 -0
  244. package/dist/src/matchmaking/topic-anchor.js +67 -0
  245. package/dist/src/matchmaking/topic-anchor.js.map +1 -0
  246. package/dist/src/matchmaking/voting-quorum.d.ts +233 -0
  247. package/dist/src/matchmaking/voting-quorum.d.ts.map +1 -0
  248. package/dist/src/matchmaking/voting-quorum.js +238 -0
  249. package/dist/src/matchmaking/voting-quorum.js.map +1 -0
  250. package/dist/src/matchmaking/wire.d.ts +259 -0
  251. package/dist/src/matchmaking/wire.d.ts.map +1 -0
  252. package/dist/src/matchmaking/wire.js +484 -0
  253. package/dist/src/matchmaking/wire.js.map +1 -0
  254. package/dist/src/network/i-repo.d.ts +9 -0
  255. package/dist/src/network/i-repo.d.ts.map +1 -1
  256. package/dist/src/network/repo-protocol.d.ts +3 -1
  257. package/dist/src/network/repo-protocol.d.ts.map +1 -1
  258. package/dist/src/network/struct.d.ts +23 -1
  259. package/dist/src/network/struct.d.ts.map +1 -1
  260. package/dist/src/reactivity/backfill.d.ts +118 -0
  261. package/dist/src/reactivity/backfill.d.ts.map +1 -0
  262. package/dist/src/reactivity/backfill.js +137 -0
  263. package/dist/src/reactivity/backfill.js.map +1 -0
  264. package/dist/src/reactivity/backpressure.d.ts +99 -0
  265. package/dist/src/reactivity/backpressure.d.ts.map +1 -0
  266. package/dist/src/reactivity/backpressure.js +153 -0
  267. package/dist/src/reactivity/backpressure.js.map +1 -0
  268. package/dist/src/reactivity/checkpoint.d.ts +165 -0
  269. package/dist/src/reactivity/checkpoint.d.ts.map +1 -0
  270. package/dist/src/reactivity/checkpoint.js +232 -0
  271. package/dist/src/reactivity/checkpoint.js.map +1 -0
  272. package/dist/src/reactivity/config.d.ts +131 -0
  273. package/dist/src/reactivity/config.d.ts.map +1 -0
  274. package/dist/src/reactivity/config.js +135 -0
  275. package/dist/src/reactivity/config.js.map +1 -0
  276. package/dist/src/reactivity/dedupe.d.ts +51 -0
  277. package/dist/src/reactivity/dedupe.d.ts.map +1 -0
  278. package/dist/src/reactivity/dedupe.js +90 -0
  279. package/dist/src/reactivity/dedupe.js.map +1 -0
  280. package/dist/src/reactivity/forwarder.d.ts +50 -0
  281. package/dist/src/reactivity/forwarder.d.ts.map +1 -0
  282. package/dist/src/reactivity/forwarder.js +52 -0
  283. package/dist/src/reactivity/forwarder.js.map +1 -0
  284. package/dist/src/reactivity/index.d.ts +34 -0
  285. package/dist/src/reactivity/index.d.ts.map +1 -0
  286. package/dist/src/reactivity/index.js +34 -0
  287. package/dist/src/reactivity/index.js.map +1 -0
  288. package/dist/src/reactivity/notification.d.ts +72 -0
  289. package/dist/src/reactivity/notification.d.ts.map +1 -0
  290. package/dist/src/reactivity/notification.js +92 -0
  291. package/dist/src/reactivity/notification.js.map +1 -0
  292. package/dist/src/reactivity/policy.d.ts +59 -0
  293. package/dist/src/reactivity/policy.d.ts.map +1 -0
  294. package/dist/src/reactivity/policy.js +64 -0
  295. package/dist/src/reactivity/policy.js.map +1 -0
  296. package/dist/src/reactivity/push-state.d.ts +140 -0
  297. package/dist/src/reactivity/push-state.d.ts.map +1 -0
  298. package/dist/src/reactivity/push-state.js +270 -0
  299. package/dist/src/reactivity/push-state.js.map +1 -0
  300. package/dist/src/reactivity/recover.d.ts +75 -0
  301. package/dist/src/reactivity/recover.d.ts.map +1 -0
  302. package/dist/src/reactivity/recover.js +105 -0
  303. package/dist/src/reactivity/recover.js.map +1 -0
  304. package/dist/src/reactivity/replay-buffer.d.ts +57 -0
  305. package/dist/src/reactivity/replay-buffer.d.ts.map +1 -0
  306. package/dist/src/reactivity/replay-buffer.js +88 -0
  307. package/dist/src/reactivity/replay-buffer.js.map +1 -0
  308. package/dist/src/reactivity/resume.d.ts +214 -0
  309. package/dist/src/reactivity/resume.d.ts.map +1 -0
  310. package/dist/src/reactivity/resume.js +358 -0
  311. package/dist/src/reactivity/resume.js.map +1 -0
  312. package/dist/src/reactivity/rotation.d.ts +283 -0
  313. package/dist/src/reactivity/rotation.d.ts.map +1 -0
  314. package/dist/src/reactivity/rotation.js +265 -0
  315. package/dist/src/reactivity/rotation.js.map +1 -0
  316. package/dist/src/reactivity/subscriber.d.ts +66 -0
  317. package/dist/src/reactivity/subscriber.d.ts.map +1 -0
  318. package/dist/src/reactivity/subscriber.js +75 -0
  319. package/dist/src/reactivity/subscriber.js.map +1 -0
  320. package/dist/src/reactivity/subscription.d.ts +52 -0
  321. package/dist/src/reactivity/subscription.d.ts.map +1 -0
  322. package/dist/src/reactivity/subscription.js +28 -0
  323. package/dist/src/reactivity/subscription.js.map +1 -0
  324. package/dist/src/reactivity/topic-anchor.d.ts +46 -0
  325. package/dist/src/reactivity/topic-anchor.d.ts.map +1 -0
  326. package/dist/src/reactivity/topic-anchor.js +61 -0
  327. package/dist/src/reactivity/topic-anchor.js.map +1 -0
  328. package/dist/src/reactivity/verify.d.ts +41 -0
  329. package/dist/src/reactivity/verify.d.ts.map +1 -0
  330. package/dist/src/reactivity/verify.js +48 -0
  331. package/dist/src/reactivity/verify.js.map +1 -0
  332. package/dist/src/reactivity/wire-validate.d.ts +28 -0
  333. package/dist/src/reactivity/wire-validate.d.ts.map +1 -0
  334. package/dist/src/reactivity/wire-validate.js +79 -0
  335. package/dist/src/reactivity/wire-validate.js.map +1 -0
  336. package/dist/src/reactivity/wire.d.ts +102 -0
  337. package/dist/src/reactivity/wire.d.ts.map +1 -0
  338. package/dist/src/reactivity/wire.js +129 -0
  339. package/dist/src/reactivity/wire.js.map +1 -0
  340. package/dist/src/transaction/coordinator.d.ts +9 -5
  341. package/dist/src/transaction/coordinator.d.ts.map +1 -1
  342. package/dist/src/transaction/coordinator.js +48 -25
  343. package/dist/src/transaction/coordinator.js.map +1 -1
  344. package/dist/src/transactor/change-notifier.d.ts +74 -0
  345. package/dist/src/transactor/change-notifier.d.ts.map +1 -0
  346. package/dist/src/transactor/change-notifier.js +4 -0
  347. package/dist/src/transactor/change-notifier.js.map +1 -0
  348. package/dist/src/transactor/index.d.ts +1 -0
  349. package/dist/src/transactor/index.d.ts.map +1 -1
  350. package/dist/src/transactor/index.js +1 -0
  351. package/dist/src/transactor/index.js.map +1 -1
  352. package/dist/src/transactor/network-transactor.d.ts +34 -2
  353. package/dist/src/transactor/network-transactor.d.ts.map +1 -1
  354. package/dist/src/transactor/network-transactor.js +113 -13
  355. package/dist/src/transactor/network-transactor.js.map +1 -1
  356. package/package.json +2 -2
  357. package/src/cluster/structs.ts +13 -0
  358. package/src/cohort-topic/addressing.ts +112 -0
  359. package/src/cohort-topic/antidos/bootstrap-evidence-envelope.ts +253 -0
  360. package/src/cohort-topic/antidos/bootstrap-evidence.ts +94 -0
  361. package/src/cohort-topic/antidos/index.ts +5 -0
  362. package/src/cohort-topic/antidos/rate-limiter.ts +210 -0
  363. package/src/cohort-topic/antidos/replay-guard.ts +109 -0
  364. package/src/cohort-topic/antidos/topic-budget.ts +145 -0
  365. package/src/cohort-topic/antiflood/index.ts +2 -0
  366. package/src/cohort-topic/antiflood/invariants.ts +108 -0
  367. package/src/cohort-topic/antiflood/jitter.ts +117 -0
  368. package/src/cohort-topic/coldstart.ts +204 -0
  369. package/src/cohort-topic/dmax.ts +88 -0
  370. package/src/cohort-topic/gossip/bus.ts +217 -0
  371. package/src/cohort-topic/gossip/index.ts +3 -0
  372. package/src/cohort-topic/gossip/records.ts +45 -0
  373. package/src/cohort-topic/gossip/view.ts +66 -0
  374. package/src/cohort-topic/index.ts +20 -0
  375. package/src/cohort-topic/load/barometer.ts +134 -0
  376. package/src/cohort-topic/load/index.ts +1 -0
  377. package/src/cohort-topic/member-engine.ts +409 -0
  378. package/src/cohort-topic/membership/index.ts +3 -0
  379. package/src/cohort-topic/membership/publisher.ts +171 -0
  380. package/src/cohort-topic/membership/source.ts +41 -0
  381. package/src/cohort-topic/membership/verifier.ts +345 -0
  382. package/src/cohort-topic/ports.ts +157 -0
  383. package/src/cohort-topic/promotion.ts +384 -0
  384. package/src/cohort-topic/registration/bytes.ts +37 -0
  385. package/src/cohort-topic/registration/handoff.ts +154 -0
  386. package/src/cohort-topic/registration/index.ts +6 -0
  387. package/src/cohort-topic/registration/renewal.ts +423 -0
  388. package/src/cohort-topic/registration/sharding.ts +61 -0
  389. package/src/cohort-topic/registration/store.ts +81 -0
  390. package/src/cohort-topic/registration/types.ts +76 -0
  391. package/src/cohort-topic/ring-hash.ts +50 -0
  392. package/src/cohort-topic/service.ts +390 -0
  393. package/src/cohort-topic/sig/index.ts +2 -0
  394. package/src/cohort-topic/sig/payloads.ts +39 -0
  395. package/src/cohort-topic/sig/threshold.ts +64 -0
  396. package/src/cohort-topic/tiers.ts +74 -0
  397. package/src/cohort-topic/traffic.ts +211 -0
  398. package/src/cohort-topic/walk.ts +255 -0
  399. package/src/cohort-topic/willingness.ts +237 -0
  400. package/src/cohort-topic/wire/codec.ts +204 -0
  401. package/src/cohort-topic/wire/index.ts +16 -0
  402. package/src/cohort-topic/wire/payloads.ts +117 -0
  403. package/src/cohort-topic/wire/types.ts +343 -0
  404. package/src/cohort-topic/wire/validate.ts +478 -0
  405. package/src/collection/collection.ts +93 -1
  406. package/src/collections/tree/tree.ts +84 -3
  407. package/src/index.ts +3 -0
  408. package/src/log/log.ts +86 -7
  409. package/src/log/struct.ts +126 -2
  410. package/src/matchmaking/capability-filter.ts +45 -0
  411. package/src/matchmaking/config.ts +98 -0
  412. package/src/matchmaking/index.ts +21 -0
  413. package/src/matchmaking/multi-cohort-seeker.ts +234 -0
  414. package/src/matchmaking/provider.ts +123 -0
  415. package/src/matchmaking/query-eval.ts +105 -0
  416. package/src/matchmaking/seeker-walk.ts +127 -0
  417. package/src/matchmaking/seeker.ts +86 -0
  418. package/src/matchmaking/topic-anchor.ts +90 -0
  419. package/src/matchmaking/voting-quorum.ts +394 -0
  420. package/src/matchmaking/wire.ts +680 -0
  421. package/src/network/i-repo.ts +9 -0
  422. package/src/network/repo-protocol.ts +3 -2
  423. package/src/network/struct.ts +24 -1
  424. package/src/reactivity/backfill.ts +220 -0
  425. package/src/reactivity/backpressure.ts +191 -0
  426. package/src/reactivity/checkpoint.ts +308 -0
  427. package/src/reactivity/config.ts +172 -0
  428. package/src/reactivity/dedupe.ts +132 -0
  429. package/src/reactivity/forwarder.ts +87 -0
  430. package/src/reactivity/index.ts +34 -0
  431. package/src/reactivity/notification.ts +123 -0
  432. package/src/reactivity/policy.ts +79 -0
  433. package/src/reactivity/push-state.ts +343 -0
  434. package/src/reactivity/recover.ts +153 -0
  435. package/src/reactivity/replay-buffer.ts +141 -0
  436. package/src/reactivity/resume.ts +549 -0
  437. package/src/reactivity/rotation.ts +415 -0
  438. package/src/reactivity/subscriber.ts +132 -0
  439. package/src/reactivity/subscription.ts +66 -0
  440. package/src/reactivity/topic-anchor.ts +71 -0
  441. package/src/reactivity/verify.ts +73 -0
  442. package/src/reactivity/wire-validate.ts +87 -0
  443. package/src/reactivity/wire.ts +224 -0
  444. package/src/transaction/coordinator.ts +53 -27
  445. package/src/transactor/change-notifier.ts +80 -0
  446. package/src/transactor/index.ts +1 -0
  447. package/src/transactor/network-transactor.ts +133 -15
@@ -0,0 +1,409 @@
1
+ /**
2
+ * Cohort-topic substrate — cohort-side member engine.
3
+ *
4
+ * This is the logic a cohort member runs when FRET's `RouteAndMaybeAct` lands a `RegisterV1`/`RenewV1`
5
+ * on it (the activity callback in `docs/cohort-topic.md` §FRET integration). It is the cohort-facing
6
+ * half of the substrate, composed from the prereq tickets' pure modules — the participant-facing half
7
+ * lives in {@link import("./service.js").CohortTopicService}. db-p2p's FRET host decodes an inbound
8
+ * frame, supplies the routing context this module can't see (`followOn`, the quorum-willing tally),
9
+ * and serializes whatever reply this returns back over the wire. db-core never imports FRET here.
10
+ *
11
+ * Inbound `RegisterV1` decision pipeline (§Registration mechanics, §Willingness, §Cold-start):
12
+ *
13
+ * 1. **Anti-DoS** — freshness/replay guard, per-(peer,topic) rate limit, and cold-root bootstrap
14
+ * evidence. A failure short-circuits to `unwilling_cohort` (temporal back-off) or, for a replay,
15
+ * a silent `no_state` (no state is served and nothing is recorded).
16
+ * 2. **Hot vs cold** — if this cohort already serves the topic (forwarder state resident), run the
17
+ * willingness check; a promoted topic answers `Promoted(d+1)`. If cold, the topic-budget +
18
+ * cold-start gate decides whether to instantiate, else `no_state` (walk steps toward the root).
19
+ * 3. **Admission** — on `accepted`, assign the deterministic primary/backup slots, persist the
20
+ * soft-state record, count the arrival, run the promotion trigger, and attach the topic-traffic
21
+ * signal to the reply.
22
+ *
23
+ * Every collaborator is injected, so the engine unit-tests without FRET or libp2p.
24
+ */
25
+
26
+ import type { IRingHash } from "./ports.js";
27
+ import { bytesEqual, bytesKey } from "./registration/bytes.js";
28
+ import type { RegistrationRecord, RegistrationStore } from "./registration/types.js";
29
+ import { DEFAULT_TTL_MS } from "./registration/types.js";
30
+ import type { SlotAssigner } from "./registration/sharding.js";
31
+ import type { RenewalCohortSide } from "./registration/renewal.js";
32
+ import type { WillingnessCheck } from "./willingness.js";
33
+ import { backoffRetryMs } from "./willingness.js";
34
+ import type { PromotionLifecycle } from "./promotion.js";
35
+ import { promotedRedirectReply } from "./coldstart.js";
36
+ import type { ColdStartManager } from "./coldstart.js";
37
+ import { shouldInstantiate } from "./coldstart.js";
38
+ import type { TrafficCounters } from "./traffic.js";
39
+ import { attachTopicTraffic } from "./traffic.js";
40
+ import type { RegisterRateLimiter } from "./antidos/rate-limiter.js";
41
+ import type { CorrelationReplayGuard } from "./antidos/replay-guard.js";
42
+ import type { TopicBudget } from "./antidos/topic-budget.js";
43
+ import type { BootstrapEvidence } from "./antidos/bootstrap-evidence.js";
44
+ import type { NodeProfile, Tier } from "./tiers.js";
45
+ import { b64urlToBytes } from "./wire/codec.js";
46
+ import type { DemotionNoticeV1, PromotionNoticeV1, RegisterReplyV1, RegisterV1, RenewReplyV1, RenewV1 } from "./wire/types.js";
47
+
48
+ /** Routing context the FRET host supplies for an inbound register (db-core can't derive it). */
49
+ export interface RegisterContext {
50
+ /** Did this register arrive as a follow-on to a parent cohort's `Promoted` redirect? */
51
+ readonly followOn: boolean;
52
+ /** Tree tier `d` this cohort serves the topic at (the walk position the register landed on). */
53
+ readonly treeTier: number;
54
+ /** The tier-`(d − 1)` parent cohort coord, for a cold-start forwarder's parent registration. */
55
+ readonly parentCoord?: Uint8Array;
56
+ }
57
+
58
+ /** Current cohort snapshot — the FRET host fills this from the membership source. */
59
+ export interface CohortSnapshotView {
60
+ readonly members: readonly Uint8Array[];
61
+ readonly cohortEpoch: Uint8Array;
62
+ }
63
+
64
+ export interface CohortMemberEngineDeps {
65
+ /** This member's own peer id. */
66
+ readonly self: Uint8Array;
67
+ /** This member's static tier profile (Edge/Core willing-tier set). */
68
+ readonly profile: NodeProfile;
69
+ /** Hash used for slot assignment + keys (db-core's own SHA-256). */
70
+ readonly hash: IRingHash;
71
+ /** Replicated soft-state registration store. */
72
+ readonly store: RegistrationStore;
73
+ /** Deterministic primary/backup slot assignment, shared with renewal + handoff. */
74
+ readonly slots: SlotAssigner;
75
+ /** Per-member willingness / admission control. */
76
+ readonly willingness: WillingnessCheck;
77
+ /** Promotion / demotion lifecycle. */
78
+ readonly promotion: PromotionLifecycle;
79
+ /** Cold-start forwarder instantiation + parent registration. */
80
+ readonly coldStart: ColdStartManager;
81
+ /** Per-topic traffic counters (gossip-derived snapshots feed the reply signal). */
82
+ readonly traffic: TrafficCounters;
83
+ /** Cohort-side TTL renewal handler (touch / primary_moved / sweep). */
84
+ readonly renewal: RenewalCohortSide;
85
+ /** Live cohort snapshot (members + epoch). */
86
+ readonly cohort: () => CohortSnapshotView;
87
+ /** Whether a quorum of members is willing to serve `tier` (the cold-start gate input). */
88
+ readonly quorumWilling: (tier: Tier) => boolean;
89
+ // --- anti-DoS guards (all optional; absent = that gate is skipped) ---
90
+ readonly rateLimiter?: RegisterRateLimiter;
91
+ /**
92
+ * Dedicated per-(peer, topic) rate limiter for the read-only {@link CohortMemberEngine.handleRegister}
93
+ * **probe** path — its own budget, separate from {@link rateLimiter}, so a probe flood cannot exhaust a
94
+ * participant's register budget (or vice-versa). An over-rate probe answers `unwilling_cohort`
95
+ * (walk → `retry_later` → `CohortBackoffError`). Absent → the probe rate gate is skipped (the read-only
96
+ * classify still runs), keeping key-less / unit / mock flows composing.
97
+ */
98
+ readonly probeRateLimiter?: RegisterRateLimiter;
99
+ readonly replayGuard?: CorrelationReplayGuard;
100
+ readonly topicBudget?: TopicBudget;
101
+ readonly bootstrapEvidence?: BootstrapEvidence;
102
+ /**
103
+ * Optional participant peer-key signature verifier (db-p2p binds it to the peer-sig primitive over
104
+ * {@link import("./wire/payloads.js").registerSigningPayload}). A `RegisterV1` whose signature does
105
+ * not verify against the claimed `participantCoord` is answered `no_state` (serve nothing, record
106
+ * nothing) — a cohort member cannot trust an unsigned/forged `participantCoord`. Verified first so a
107
+ * forged frame never reaches (or pollutes) the replay/rate guards. Absent → the gate is skipped.
108
+ */
109
+ readonly verifyRegisterSig?: (reg: RegisterV1) => boolean;
110
+ /**
111
+ * Sink for a freshly threshold-signed promotion/demotion notice produced on an arrival (the `accept`
112
+ * path's promotion trigger). db-p2p's host wires this to broadcast the notice over the `promote`
113
+ * protocol to the cohort (and, for a demotion, the parent coord). Absent → notices are produced and
114
+ * applied locally but not broadcast (unit/mock flows). Called fire-and-forget, off the reply path.
115
+ */
116
+ readonly onNotice?: (notice: PromotionNoticeV1 | DemotionNoticeV1) => void;
117
+ /**
118
+ * Optional logger. Used only to record a failed promotion sign/broadcast (the threshold-sign round
119
+ * can reject when the cohort quorum is unreachable) rather than swallow it. Absent → silent.
120
+ */
121
+ readonly log?: (formatter: string, ...args: unknown[]) => void;
122
+ /**
123
+ * Sink for a freshly-admitted registration record (the `accept` path), symmetric to the renewal
124
+ * `gossip.touch` hook. db-p2p wires this to the same per-coord gossip delta queue the renewal side
125
+ * appends to, so an admitted record is replicated to the cohort at the next gossip round — closing the
126
+ * durability window between admission and the participant's first renewal touch. A queue append, not a
127
+ * synchronous broadcast: admission must not block on a round, and last-writer-wins by `lastPing` dedupes
128
+ * an admit-then-touch landing in the same round. Absent → no admission-time replication (unit/mock flows
129
+ * that don't exercise gossip).
130
+ */
131
+ readonly onAdmit?: (rec: RegistrationRecord) => void;
132
+ }
133
+
134
+ /** Cohort-side register/renew handler — the body of the FRET activity callback. */
135
+ export interface CohortMemberEngine {
136
+ /** Classify and admit (or decline) an inbound `RegisterV1`, returning the reply to serialize. */
137
+ handleRegister(reg: RegisterV1, ctx: RegisterContext, now: number): Promise<RegisterReplyV1>;
138
+ /** Handle an inbound `RenewV1` (ping / re-attach), delegating to the renewal cohort side. */
139
+ handleRenew(msg: RenewV1, now: number): RenewReplyV1;
140
+ /** Evict stale records and gossip each eviction; returns the evicted set. */
141
+ sweepStale(now: number): readonly RegistrationRecord[];
142
+ }
143
+
144
+ class StoreCohortMemberEngine implements CohortMemberEngine {
145
+ constructor(private readonly deps: CohortMemberEngineDeps) {}
146
+
147
+ async handleRegister(reg: RegisterV1, ctx: RegisterContext, now: number): Promise<RegisterReplyV1> {
148
+ const topicId = b64urlToBytes(reg.topicId);
149
+ const participantId = b64urlToBytes(reg.participantCoord);
150
+ const tier = reg.tier as Tier;
151
+
152
+ // 0. Read-only lookup probe: classify + return the cohort snapshot without admitting anything.
153
+ // Branches before the admission pipeline (and the durable-state anti-DoS guards), so even a
154
+ // hand-crafted `probe: true, bootstrap: true` frame can never instantiate — defense in depth.
155
+ if (reg.probe === true) {
156
+ return this.handleProbe(reg, topicId, participantId, ctx, now);
157
+ }
158
+
159
+ // 1. Anti-DoS gates. A replay/stale frame is answered with NoState (serve nothing, record
160
+ // nothing); an over-rate source or missing bootstrap evidence backs off in time.
161
+ const guard = this.runGuards(reg, topicId, participantId, tier, now);
162
+ if (guard !== undefined) {
163
+ return guard;
164
+ }
165
+
166
+ // 2. Hot path: this cohort already serves the topic.
167
+ if (this.serves(topicId)) {
168
+ if (this.deps.promotion.isPromoted(topicId)) {
169
+ // Promoted: bounce same-tier registrations onward (the cheap single-RPC redirect).
170
+ return promotedRedirectReply(ctx.treeTier + 1, this.deps.traffic.snapshot(topicId));
171
+ }
172
+ return this.admitOrDecline(reg, topicId, participantId, tier, ctx, now);
173
+ }
174
+
175
+ // 3. Cold path: instantiate only at a legitimate growth point with a willing quorum.
176
+ const quorumWilling = this.deps.quorumWilling(tier);
177
+ if (!shouldInstantiate({ bootstrap: reg.bootstrap === true, followOn: ctx.followOn, quorumWilling })) {
178
+ return { v: 1, result: "no_state" };
179
+ }
180
+ if (this.deps.topicBudget?.admit(topicId) === false) {
181
+ // Forwarder-state budget is full of populated topics — decline in time.
182
+ return this.unwillingCohort(reg, now);
183
+ }
184
+ this.deps.coldStart.instantiate(topicId, ctx.treeTier, ctx.parentCoord, tier);
185
+ return this.admitOrDecline(reg, topicId, participantId, tier, ctx, now);
186
+ }
187
+
188
+ /**
189
+ * Read-only resolution of the cohort for a topic (the `lookup` probe). It walks the same path a
190
+ * register does (the walk loop is byte-for-byte identical) but the **terminal** member action is a
191
+ * classify, never an admission: it persists **no** record, counts **no** arrival, fires **no**
192
+ * promotion trigger, touches **no** topic budget, and **never** instantiates a cold-start forwarder.
193
+ *
194
+ * DoS posture (`docs/cohort-topic.md` §Anti-DoS): a probe still runs the stateless participant-sig gate
195
+ * (forged → `no_state`) and a dedicated probe rate limiter (over-rate → `unwilling_cohort`), but skips
196
+ * the replay guard, the bootstrap-evidence gate, and the topic budget — an idempotent read records
197
+ * nothing, so it is strictly cheaper than the register it replaces.
198
+ *
199
+ * Classification mirrors a register's terminal cases so the walk's existing branches drive a probe
200
+ * unchanged: a served-and-promoted topic answers `promoted(treeTier + 1)` (the walk follows it), a
201
+ * served topic answers `accepted` with the participant-specific slot assignment + read-only traffic
202
+ * snapshot, and a topic this cohort does not serve answers `no_state` (the walk steps inward).
203
+ */
204
+ private handleProbe(reg: RegisterV1, topicId: Uint8Array, participantId: Uint8Array, ctx: RegisterContext, now: number): RegisterReplyV1 {
205
+ if (this.deps.verifyRegisterSig?.(reg) === false) {
206
+ // Unsigned / forged participant signature: serve nothing, exactly like the register path — a
207
+ // cohort member never resolves a cohort snapshot for an untrusted `participantCoord`.
208
+ return { v: 1, result: "no_state" };
209
+ }
210
+ const rate = this.deps.probeRateLimiter?.check(participantId, topicId, now);
211
+ if (rate !== undefined && rate.ok === false) {
212
+ return { v: 1, result: "unwilling_cohort", retryAfterMs: rate.retryAfterMs };
213
+ }
214
+ if (this.serves(topicId)) {
215
+ if (this.deps.promotion.isPromoted(topicId)) {
216
+ return promotedRedirectReply(ctx.treeTier + 1, this.deps.traffic.snapshot(topicId));
217
+ }
218
+ const { members, cohortEpoch } = this.deps.cohort();
219
+ const { primary, backups } = this.deps.slots.assignSlots(participantId, cohortEpoch, members);
220
+ const reply: RegisterReplyV1 = {
221
+ v: 1,
222
+ result: "accepted",
223
+ primary: bytesKey(primary),
224
+ backups: backups.map(bytesKey),
225
+ cohortEpoch: bytesKey(cohortEpoch),
226
+ cohortMembers: members.map(bytesKey),
227
+ };
228
+ return attachTopicTraffic(reply, this.deps.traffic.snapshot(topicId));
229
+ }
230
+ return { v: 1, result: "no_state" };
231
+ }
232
+
233
+ handleRenew(msg: RenewV1, now: number): RenewReplyV1 {
234
+ const reply = this.deps.renewal.onRenew(msg, now);
235
+ if (reply.result === "ok") {
236
+ // A renewal is an arrival proxy for the traffic barometer (§Topic traffic signal).
237
+ this.deps.traffic.recordArrival(b64urlToBytes(msg.topicId), now);
238
+ } else if (reply.result === "withdrawn") {
239
+ // A withdraw evicted a direct participant: re-touch the topic budget DOWN (mirrors the
240
+ // sweepStale drain release below) so the freed slot does not leak. Never an arrival — it is a
241
+ // departure, so the traffic barometer is deliberately not touched.
242
+ const topicId = b64urlToBytes(msg.topicId);
243
+ this.deps.topicBudget?.touch(topicId, this.deps.store.directParticipants(topicId));
244
+ }
245
+ return reply;
246
+ }
247
+
248
+ sweepStale(now: number): readonly RegistrationRecord[] {
249
+ // Reclaim idle (peer, topic) rate-limiter keys on the existing gossip cadence. The limiters' own
250
+ // hard LRU cap already bounds worst-case footprint; this sweep adds proactive steady-state reclaim
251
+ // of long-quiet keys so a long-running coord does not hold register/probe state for departed peers.
252
+ this.deps.rateLimiter?.sweep(now);
253
+ this.deps.probeRateLimiter?.sweep(now);
254
+
255
+ const evicted = this.deps.renewal.sweepStale(now);
256
+ const budget = this.deps.topicBudget;
257
+ if (budget !== undefined && evicted.length > 0) {
258
+ // Mirror the `accept()` up-touch on the drain side: a TTL sweep that removes a topic's last
259
+ // direct participant must re-`touch` the budget down so the topic falls to participantCount 0
260
+ // and becomes the coldest-evictable resident again. Without this the budget slot leaks — a
261
+ // drained topic keeps its stale positive count forever and `coldestEvictable()` never picks it,
262
+ // so the cohort eventually refuses every new topic while serving nothing. Re-touch once per
263
+ // distinct affected topic from `store.directParticipants` (the source of truth, post-eviction).
264
+ const seen = new Set<string>();
265
+ for (const rec of evicted) {
266
+ const key = bytesKey(rec.topicId);
267
+ if (seen.has(key)) continue;
268
+ seen.add(key);
269
+ budget.touch(rec.topicId, this.deps.store.directParticipants(rec.topicId));
270
+ }
271
+ }
272
+ return evicted;
273
+ }
274
+
275
+ // --- admission ---
276
+
277
+ /** Run the willingness check; on `accepted` persist the record and attach the traffic signal. */
278
+ private async admitOrDecline(
279
+ reg: RegisterV1,
280
+ topicId: Uint8Array,
281
+ participantId: Uint8Array,
282
+ tier: Tier,
283
+ ctx: RegisterContext,
284
+ now: number,
285
+ ): Promise<RegisterReplyV1> {
286
+ const outcome = this.deps.willingness.evaluate(reg, this.deps.profile, now);
287
+ switch (outcome.kind) {
288
+ case "unwilling_member": {
289
+ return { v: 1, result: "unwilling_member", candidateMembers: outcome.candidateMembers.map(bytesKey) };
290
+ }
291
+ case "unwilling_cohort": {
292
+ return { v: 1, result: "unwilling_cohort", retryAfterMs: outcome.retryAfterMs };
293
+ }
294
+ case "accepted": {
295
+ return this.accept(reg, topicId, participantId, tier, ctx, now);
296
+ }
297
+ default: {
298
+ return this.unwillingCohort(reg, now);
299
+ }
300
+ }
301
+ }
302
+
303
+ private async accept(
304
+ reg: RegisterV1,
305
+ topicId: Uint8Array,
306
+ participantId: Uint8Array,
307
+ tier: Tier,
308
+ ctx: RegisterContext,
309
+ now: number,
310
+ ): Promise<RegisterReplyV1> {
311
+ const { members, cohortEpoch } = this.deps.cohort();
312
+ const { primary, backups } = this.deps.slots.assignSlots(participantId, cohortEpoch, members);
313
+ const ttl = reg.ttl > 0 ? reg.ttl : DEFAULT_TTL_MS;
314
+ const record: RegistrationRecord = {
315
+ topicId,
316
+ participantId,
317
+ tier,
318
+ primary,
319
+ backups,
320
+ attachedAt: now,
321
+ lastPing: now,
322
+ ttl,
323
+ appState: reg.appPayload === undefined ? undefined : b64urlToBytes(reg.appPayload),
324
+ };
325
+ this.deps.store.put(record);
326
+ this.deps.onAdmit?.(record);
327
+ this.deps.traffic.recordArrival(topicId, now);
328
+ this.deps.topicBudget?.touch(topicId, this.deps.store.directParticipants(topicId));
329
+ // Promotion may fire on this arrival; broadcast whatever notice comes back. Fire-and-forget so the
330
+ // register reply is not delayed by the threshold-sign collection round.
331
+ void this.firePromotion(topicId, now);
332
+
333
+ const reply: RegisterReplyV1 = {
334
+ v: 1,
335
+ result: "accepted",
336
+ primary: bytesKey(primary),
337
+ backups: backups.map(bytesKey),
338
+ cohortEpoch: bytesKey(cohortEpoch),
339
+ cohortMembers: members.map(bytesKey),
340
+ };
341
+ return attachTopicTraffic(reply, this.deps.traffic.snapshot(topicId));
342
+ }
343
+
344
+ /** Run the promotion trigger for an arrival and hand any signed notice to {@link CohortMemberEngineDeps.onNotice}. */
345
+ private async firePromotion(topicId: Uint8Array, now: number): Promise<void> {
346
+ try {
347
+ const notice = await this.deps.promotion.onParticipantCountChange(topicId, now);
348
+ if (notice !== undefined) {
349
+ this.deps.onNotice?.(notice);
350
+ }
351
+ } catch (err) {
352
+ // Threshold signing rejects when the cohort quorum is unreachable this round; the next arrival
353
+ // re-fires, so this is transient. Log rather than crash the register path — or leak the
354
+ // unhandled rejection the bare `void promotion...` did once signing became real (db-p2p ticket 1).
355
+ this.deps.log?.("cohort-topic: promotion sign/broadcast failed for an arrival: %o", err);
356
+ }
357
+ }
358
+
359
+ // --- guards ---
360
+
361
+ /** Returns a short-circuit reply if any anti-DoS gate rejects `reg`, else `undefined`. */
362
+ private runGuards(
363
+ reg: RegisterV1,
364
+ topicId: Uint8Array,
365
+ participantId: Uint8Array,
366
+ tier: Tier,
367
+ now: number,
368
+ ): RegisterReplyV1 | undefined {
369
+ if (this.deps.verifyRegisterSig?.(reg) === false) {
370
+ // Unsigned / forged participant signature: serve nothing and record nothing (the cohort
371
+ // cannot trust this participantCoord). Checked before the replay/rate guards so a forged
372
+ // frame cannot consume their state.
373
+ return { v: 1, result: "no_state" };
374
+ }
375
+ const correlationId = b64urlToBytes(reg.correlationId);
376
+ if (this.deps.replayGuard?.accept(correlationId, participantId, reg.timestamp, now) === false) {
377
+ // Stale / future-skewed / replayed: serve nothing and record nothing.
378
+ return { v: 1, result: "no_state" };
379
+ }
380
+ if (this.deps.bootstrapEvidence?.verify(reg, tier) === false) {
381
+ return this.unwillingCohort(reg, now);
382
+ }
383
+ const rate = this.deps.rateLimiter?.check(participantId, topicId, now);
384
+ if (rate !== undefined && rate.ok === false) {
385
+ return { v: 1, result: "unwilling_cohort", retryAfterMs: rate.retryAfterMs };
386
+ }
387
+ return undefined;
388
+ }
389
+
390
+ private serves(topicId: Uint8Array): boolean {
391
+ return this.deps.coldStart.get(topicId) !== undefined || this.deps.store.directParticipants(topicId) > 0;
392
+ }
393
+
394
+ private unwillingCohort(reg: RegisterV1, now: number): RegisterReplyV1 {
395
+ void reg;
396
+ void now;
397
+ return { v: 1, result: "unwilling_cohort", retryAfterMs: backoffRetryMs(0) };
398
+ }
399
+ }
400
+
401
+ /** Build a {@link CohortMemberEngine} over the injected cohort-side collaborators. */
402
+ export function createCohortMemberEngine(deps: CohortMemberEngineDeps): CohortMemberEngine {
403
+ return new StoreCohortMemberEngine(deps);
404
+ }
405
+
406
+ /** True iff `a` and `b` name the same peer (re-exported convenience for host equality checks). */
407
+ export function samePeer(a: Uint8Array, b: Uint8Array): boolean {
408
+ return bytesEqual(a, b);
409
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./source.js";
2
+ export * from "./verifier.js";
3
+ export * from "./publisher.js";
@@ -0,0 +1,171 @@
1
+ /**
2
+ * Cohort-topic substrate — cohort-side `MembershipCertV1` publication.
3
+ *
4
+ * Per `docs/cohort-topic.md` §Membership snapshots: the cohort publishes a threshold-signed
5
+ * `MembershipCertV1`
6
+ *
7
+ * - at stabilization, and on any change to the first `k − x` members ({@link MembershipCertPublisher.onStabilized});
8
+ * - refreshed every `T_membership_refresh` (default 5 min) ({@link MembershipCertPublisher.tick}).
9
+ *
10
+ * The publisher is db-core logic over the {@link CohortSigner} (threshold sign) and the
11
+ * {@link IMembershipPublishSink} (serve/advertise). It never imports FRET. Members are sorted
12
+ * ascending for the cert (matching the sharding order) and the first `minSigs` (= `k − x`) of that
13
+ * order are the "first `k − x`" whose change forces a fresh publish.
14
+ */
15
+
16
+ import type { IMembershipPublishSink } from "../ports.js";
17
+ import { bytesToB64url, encodeCohortMessage } from "../wire/codec.js";
18
+ import type { MembershipCertV1 } from "../wire/types.js";
19
+ import { compareBytes } from "../registration/bytes.js";
20
+ import { DEFAULT_MIN_SIGS, type CohortSigner } from "../sig/threshold.js";
21
+ import { membershipCertSigningPayload, type MembershipCertSignable } from "../sig/payloads.js";
22
+
23
+ /** Default membership-cert refresh interval (`T_membership_refresh`). */
24
+ export const DEFAULT_T_MEMBERSHIP_REFRESH_MS = 5 * 60_000;
25
+
26
+ /**
27
+ * The canonical {@link MembershipCertSignable} for a stabilized snapshot: members sorted ascending (the
28
+ * cert / sharding order), byte fields base64url-encoded, over exactly the threshold-signed fields. The
29
+ * publisher signs `membershipCertSigningPayload` of this; a rotation-attestation **producer** (db-p2p,
30
+ * `cohort-topic-trust-anchor-rotation-production`) signs the *successor* cert's image through the SAME
31
+ * helper, so signer and verifier never canonicalize the cert independently (signature-image agreement —
32
+ * a mismatch makes the db-core chain check fail).
33
+ */
34
+ export function membershipCertSignable(snapshot: CohortSnapshot): MembershipCertSignable {
35
+ const sorted = [...snapshot.members].sort(compareBytes);
36
+ return {
37
+ cohortCoord: bytesToB64url(snapshot.coord),
38
+ cohortEpoch: bytesToB64url(snapshot.cohortEpoch),
39
+ members: sorted.map(bytesToB64url),
40
+ stabilizedAt: snapshot.stabilizedAt,
41
+ };
42
+ }
43
+
44
+ /** A stabilized cohort snapshot the publisher attests. */
45
+ export interface CohortSnapshot {
46
+ /** Cohort coord (32 bytes). */
47
+ coord: Uint8Array;
48
+ /** Cohort epoch for this snapshot. */
49
+ cohortEpoch: Uint8Array;
50
+ /** Cohort members (any order; the cert sorts them ascending). */
51
+ members: readonly Uint8Array[];
52
+ /** Unix ms this membership stabilized. */
53
+ stabilizedAt: number;
54
+ /** Optional FRET stabilization proof. */
55
+ fretAttestation?: Uint8Array;
56
+ }
57
+
58
+ /**
59
+ * A predecessor-cohort rotation attestation to attach to a fresh cert (an epoch rotation). The
60
+ * predecessor cohort threshold-signs the **successor** cert's `membershipCertSigningPayload`; that
61
+ * `(prevEpoch, rotationSig, rotationSigners)` triple lets a participant holding a trusted predecessor
62
+ * verify the rotation is legitimate. Producing this triple is the db-p2p ticket
63
+ * `cohort-topic-trust-anchor-rotation-production`; the publisher only attaches a provided one.
64
+ */
65
+ export interface RotationAttestation {
66
+ /** Predecessor cohort epoch this cert rotates from (32 bytes). */
67
+ prevEpoch: Uint8Array;
68
+ /** Predecessor cohort's threshold signature over the successor cert's signing payload. */
69
+ rotationSig: Uint8Array;
70
+ /** Predecessor cohort signers that produced {@link rotationSig}; `>= minSigs`. */
71
+ rotationSigners: readonly Uint8Array[];
72
+ }
73
+
74
+ /** Cohort-side membership-cert publisher. */
75
+ export interface MembershipCertPublisher {
76
+ /**
77
+ * Drive publication on a stabilization event. Publishes (and returns the cert) when the first
78
+ * `k − x` members changed since the last publish, or on the first call; otherwise returns `undefined`.
79
+ * Pass `rotation` to attach a predecessor-cohort rotation attestation (an epoch rotation); omit it for
80
+ * the default non-rotation publish (no rotation fields are emitted).
81
+ */
82
+ onStabilized(snapshot: CohortSnapshot, now: number, rotation?: RotationAttestation): Promise<MembershipCertV1 | undefined>;
83
+ /**
84
+ * Periodic tick. Re-publishes (and returns the cert) when `T_membership_refresh` has elapsed since
85
+ * the last publish; otherwise returns `undefined`. Pass `rotation` to attach a rotation attestation.
86
+ */
87
+ tick(snapshot: CohortSnapshot, now: number, rotation?: RotationAttestation): Promise<MembershipCertV1 | undefined>;
88
+ }
89
+
90
+ export interface MembershipCertPublisherDeps {
91
+ signer: CohortSigner;
92
+ sink: IMembershipPublishSink;
93
+ refreshMs?: number;
94
+ minSigs?: number;
95
+ maxMessageBytes?: number;
96
+ }
97
+
98
+ class SigningMembershipCertPublisher implements MembershipCertPublisher {
99
+ private readonly refreshMs: number;
100
+ private readonly minSigs: number;
101
+ /** base64url of the first `minSigs` members of the last published snapshot. */
102
+ private lastFirstKx: string[] | undefined;
103
+ private lastPublishedAt: number | undefined;
104
+
105
+ constructor(private readonly deps: MembershipCertPublisherDeps) {
106
+ this.refreshMs = deps.refreshMs ?? DEFAULT_T_MEMBERSHIP_REFRESH_MS;
107
+ this.minSigs = deps.minSigs ?? DEFAULT_MIN_SIGS;
108
+ }
109
+
110
+ async onStabilized(snapshot: CohortSnapshot, now: number, rotation?: RotationAttestation): Promise<MembershipCertV1 | undefined> {
111
+ const firstKx = this.firstKx(snapshot.members);
112
+ if (this.lastFirstKx !== undefined && sameOrder(this.lastFirstKx, firstKx)) {
113
+ return undefined; // first k − x unchanged — no republish needed
114
+ }
115
+ return this.publish(snapshot, now, rotation);
116
+ }
117
+
118
+ async tick(snapshot: CohortSnapshot, now: number, rotation?: RotationAttestation): Promise<MembershipCertV1 | undefined> {
119
+ if (this.lastPublishedAt !== undefined && now - this.lastPublishedAt < this.refreshMs) {
120
+ return undefined;
121
+ }
122
+ return this.publish(snapshot, now, rotation);
123
+ }
124
+
125
+ /** The first `minSigs` members of the cert's ascending order, base64url — the republish-change key. */
126
+ private firstKx(members: readonly Uint8Array[]): string[] {
127
+ return [...members].sort(compareBytes).slice(0, this.minSigs).map(bytesToB64url);
128
+ }
129
+
130
+ private async publish(snapshot: CohortSnapshot, now: number, rotation?: RotationAttestation): Promise<MembershipCertV1> {
131
+ const signable = membershipCertSignable(snapshot);
132
+ const { thresholdSig, signers } = await this.deps.signer.thresholdSign(membershipCertSigningPayload(signable));
133
+ const cert: MembershipCertV1 = {
134
+ v: 1,
135
+ ...signable,
136
+ thresholdSig: bytesToB64url(thresholdSig),
137
+ signers: signers.map(bytesToB64url),
138
+ };
139
+ if (snapshot.fretAttestation !== undefined) {
140
+ cert.fretAttestation = bytesToB64url(snapshot.fretAttestation);
141
+ }
142
+ // A rotation attestation (when given) signs *over* the cert's signing payload, so it is attached to the
143
+ // envelope after signing and is not part of `membershipCertSigningPayload`. Omitted → no rotation fields.
144
+ if (rotation !== undefined) {
145
+ cert.prevEpoch = bytesToB64url(rotation.prevEpoch);
146
+ cert.rotationSig = bytesToB64url(rotation.rotationSig);
147
+ cert.rotationSigners = rotation.rotationSigners.map(bytesToB64url);
148
+ }
149
+ this.deps.sink.publish(encodeCohortMessage(cert, this.deps.maxMessageBytes));
150
+ this.lastFirstKx = signable.members.slice(0, this.minSigs);
151
+ this.lastPublishedAt = now;
152
+ return cert;
153
+ }
154
+ }
155
+
156
+ function sameOrder(a: readonly string[], b: readonly string[]): boolean {
157
+ if (a.length !== b.length) {
158
+ return false;
159
+ }
160
+ for (let i = 0; i < a.length; i++) {
161
+ if (a[i] !== b[i]) {
162
+ return false;
163
+ }
164
+ }
165
+ return true;
166
+ }
167
+
168
+ /** Build a cohort-side {@link MembershipCertPublisher}. */
169
+ export function createMembershipCertPublisher(deps: MembershipCertPublisherDeps): MembershipCertPublisher {
170
+ return new SigningMembershipCertPublisher(deps);
171
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Cohort-topic substrate — membership-source dispatch.
3
+ *
4
+ * Resolved open question (per `docs/cohort-topic.md` §Membership source): how *committed*
5
+ * membership is obtained differs by tier.
6
+ *
7
+ * - **T0/T1** cohorts serve committed work (transaction commits, chain serving); their membership is
8
+ * anchored in the transaction log's commit certificate. The verifier reads it from there (it never
9
+ * writes the log).
10
+ * - **T2/T3** cohorts (matchmaking, push forwarding) derive membership from current FRET state and
11
+ * verify against FRET's signed `MembershipCertV1` advertisements.
12
+ *
13
+ * Both back-ends present the same {@link IMembershipSource} surface; this router picks between them by
14
+ * tier. db-p2p supplies the two concrete sources (tx-log reader and FRET cert source).
15
+ */
16
+
17
+ import type { IMembershipSource } from "../ports.js";
18
+
19
+ /** Picks the authoritative membership source for a cohort by its tier. */
20
+ export interface IMembershipSourceRouter {
21
+ /** Source for a cohort at `tier`: T0/T1 → committed tx-log, T2/T3 → FRET certs. */
22
+ for(tier: number): IMembershipSource;
23
+ }
24
+
25
+ /** Tiers whose committed membership is anchored in the transaction log. */
26
+ function isCommittedTier(tier: number): boolean {
27
+ return tier === 0 || tier === 1;
28
+ }
29
+
30
+ /**
31
+ * Build a router over the two membership back-ends.
32
+ * @param deps.committed source backed by the transaction-log commit certificate (T0/T1).
33
+ * @param deps.fret source backed by FRET's signed `MembershipCertV1` advertisements (T2/T3).
34
+ */
35
+ export function createMembershipSourceRouter(deps: { committed: IMembershipSource; fret: IMembershipSource }): IMembershipSourceRouter {
36
+ return {
37
+ for(tier: number): IMembershipSource {
38
+ return isCommittedTier(tier) ? deps.committed : deps.fret;
39
+ },
40
+ };
41
+ }