@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,423 @@
1
+ /**
2
+ * Cohort-topic substrate — TTL renewal and failover.
3
+ *
4
+ * Per `docs/cohort-topic.md` §TTL and renewal and §Failure modes:
5
+ *
6
+ * - Participant pings `primary` every `ttl/3`. Success → primary touches `lastPing` and gossips it.
7
+ * - Three consecutive ping failures → participant promotes `backups[0]` via a re-attach RPC: a renew
8
+ * carrying a **signed `reattach` flag** (no full re-registration). The backup accepts when it holds
9
+ * the record locally *and* is a computed backup under the current epoch — re-stamping `primary` to
10
+ * itself and serving subsequent plain pings via an epoch-scoped failover override (the unchanged
11
+ * `cohortEpoch` still names the dead node as the computed primary). A re-attach answered
12
+ * `primary_moved` means a real rotation moved primary to a live member: the participant adopts that
13
+ * payload instead of promoting the contacted backup (ignoring a reply that points back at the dead
14
+ * primary). **Resolved (GROUNDING):** the participant's `cohortEpoch` hint refreshes lazily — on the
15
+ * *next* ping/renewal after failover, not eagerly at failover time.
16
+ * - All of primary + backups fail → participant re-runs lookup from `d_max` (walk ticket, injected).
17
+ * - Cohort-side: evict where `now − lastPing > ttl`; eviction is gossiped so members converge.
18
+ *
19
+ * Both sides take their transport/gossip by injection so storage + sharding + TTL stay unit-testable
20
+ * in isolation; db-core never imports FRET or libp2p here.
21
+ */
22
+
23
+ import { b64urlToBytes } from "../wire/codec.js";
24
+ import type { RenewReplyV1, RenewV1 } from "../wire/types.js";
25
+ import { bytesEqual, bytesKey, recordKey } from "./bytes.js";
26
+ import type { SlotAssigner } from "./sharding.js";
27
+ import { MAX_PING_FAILURES, pingIntervalMs } from "./types.js";
28
+ import type { RegistrationRecord, RegistrationStore } from "./types.js";
29
+
30
+ // --- participant side ---
31
+
32
+ /** Participant-side TTL renewal: periodic ping with 3-fail backup promotion. */
33
+ export interface RenewalParticipant {
34
+ /** Run one ping cycle: ping the current primary, count failures, promote/relookup as needed. */
35
+ pingLoop(): Promise<void>;
36
+ /** Re-attach to `target` (a backup), carrying the existing record; resolves with the reply. */
37
+ reattach(target: Uint8Array): Promise<RenewReplyV1>;
38
+ /**
39
+ * Best-effort remote tombstone: send a signed withdraw renew to the current primary so the cohort
40
+ * frees the registration immediately rather than holding it for a full TTL. Swallows any transport
41
+ * failure (TTL expiry remains the fallback) and does NOT touch the failure counters or trigger
42
+ * failover — withdraw is one-shot and fire-and-forget.
43
+ */
44
+ withdraw(): Promise<void>;
45
+ /** `ttl/3` cadence the scheduler should call {@link pingLoop} at. */
46
+ readonly pingIntervalMs: number;
47
+ /** The participant's current view of its registration (primary/backups). */
48
+ readonly record: RegistrationRecord;
49
+ /**
50
+ * Cached cohort-epoch hint (subscriber-side `cohortHint`). Deliberately *not* refreshed at
51
+ * failover; a `primary_moved` reply on the next ping refreshes it lazily.
52
+ */
53
+ readonly cohortEpochHint: Uint8Array | undefined;
54
+ }
55
+
56
+ /** The unsigned body of a renew/re-attach RPC; the injected signer turns it into a {@link RenewV1}. */
57
+ export type UnsignedRenew = Omit<RenewV1, "signature">;
58
+
59
+ /** Transport the participant drives; supplied by db-p2p (FRET dial / RouteAndMaybeAct underneath). */
60
+ export interface RenewalParticipantTransport {
61
+ /** Send a renew/re-attach to `target`; resolves with the reply, rejects on RPC failure. */
62
+ send(target: Uint8Array, msg: RenewV1): Promise<RenewReplyV1>;
63
+ /** Primary + all backups unreachable: re-run lookup from `d_max` (walk ticket owns the body). */
64
+ relookup(): Promise<void>;
65
+ }
66
+
67
+ export interface RenewalParticipantDeps {
68
+ transport: RenewalParticipantTransport;
69
+ /** Monotonic-ish wall clock in unix ms (injected for deterministic tests). */
70
+ clock: () => number;
71
+ /** Signs the renew body; db-p2p supplies the (async) peer-key signature. */
72
+ sign: (body: UnsignedRenew) => Promise<string>;
73
+ /** Correlation id matching the original RegisterV1, base64url. */
74
+ correlationId: string;
75
+ /** Initial cohort-epoch hint from the registration reply (refreshed lazily thereafter). */
76
+ initialCohortEpoch?: Uint8Array;
77
+ }
78
+
79
+ class TtlRenewalParticipant implements RenewalParticipant {
80
+ private consecutiveFailures = 0;
81
+ private current: RegistrationRecord;
82
+ private epochHint: Uint8Array | undefined;
83
+
84
+ constructor(initial: RegistrationRecord, private readonly deps: RenewalParticipantDeps) {
85
+ this.current = initial;
86
+ this.epochHint = deps.initialCohortEpoch;
87
+ }
88
+
89
+ get record(): RegistrationRecord {
90
+ return this.current;
91
+ }
92
+
93
+ get cohortEpochHint(): Uint8Array | undefined {
94
+ return this.epochHint;
95
+ }
96
+
97
+ get pingIntervalMs(): number {
98
+ return pingIntervalMs(this.current.ttl);
99
+ }
100
+
101
+ async pingLoop(): Promise<void> {
102
+ const reply = await this.trySend(this.current.primary);
103
+ if (reply !== undefined) {
104
+ this.onPingSuccess(reply);
105
+ return;
106
+ }
107
+ this.consecutiveFailures++;
108
+ if (this.consecutiveFailures < MAX_PING_FAILURES) {
109
+ return;
110
+ }
111
+ await this.failover();
112
+ }
113
+
114
+ async reattach(target: Uint8Array): Promise<RenewReplyV1> {
115
+ return this.deps.transport.send(target, await this.buildRenew({ reattach: true }));
116
+ }
117
+
118
+ async withdraw(): Promise<void> {
119
+ // Fire-and-forget signed tombstone to the current primary. A send failure is swallowed — the
120
+ // record TTL-expires as it does today (the local-half fallback) — and crucially we never bump
121
+ // `consecutiveFailures` or fail over: the participant is leaving, not recovering.
122
+ try {
123
+ await this.deps.transport.send(this.current.primary, await this.buildRenew({ withdraw: true }));
124
+ } catch {
125
+ /* best-effort; the cohort TTL bounds the leak */
126
+ }
127
+ }
128
+
129
+ /** Send a renew, mapping an RPC rejection to `undefined` (a counted failure). */
130
+ private async trySend(target: Uint8Array): Promise<RenewReplyV1 | undefined> {
131
+ try {
132
+ return await this.deps.transport.send(target, await this.buildRenew({}));
133
+ } catch {
134
+ return undefined;
135
+ }
136
+ }
137
+
138
+ private onPingSuccess(reply: RenewReplyV1): void {
139
+ this.consecutiveFailures = 0;
140
+ if (reply.result === "primary_moved") {
141
+ // Lazy cohortEpoch refresh lands here: a move discovered on a normal ping updates the hint.
142
+ this.applyPrimaryMoved(reply);
143
+ }
144
+ }
145
+
146
+ /**
147
+ * 3-fail path: re-attach to each backup in turn (a signed `reattach=true` renew). An `ok` means the
148
+ * backup accepted the crash-failover promotion → promote it locally. A `primary_moved` means a real
149
+ * rotation moved primary to a different live member → adopt that payload (not the contacted backup),
150
+ * **unless** it points back at the just-failed primary (the bounce guard) — then keep trying. All
151
+ * backups exhausted → re-run lookup from `d_max`.
152
+ */
153
+ private async failover(): Promise<void> {
154
+ const failedPrimary = this.current.primary;
155
+ for (const backup of this.current.backups) {
156
+ const reply = await this.tryReattach(backup);
157
+ if (reply === undefined) {
158
+ continue;
159
+ }
160
+ if (reply.result === "ok") {
161
+ this.promote(backup);
162
+ return;
163
+ }
164
+ if (reply.result === "primary_moved") {
165
+ // A genuine rotation: adopt the rotated assignment — but never re-adopt the dead primary
166
+ // (the defensive guard against the exact bounce crash-failover is meant to fix).
167
+ if (reply.newPrimary !== undefined && bytesEqual(b64(reply.newPrimary), failedPrimary)) {
168
+ continue;
169
+ }
170
+ this.applyPrimaryMoved(reply);
171
+ // We now have a fresh, live primary to ping — clear the strike count so the next single
172
+ // transient failure doesn't immediately re-failover (matching `promote` and the
173
+ // normal-ping `primary_moved` path, which both reset here).
174
+ this.consecutiveFailures = 0;
175
+ return;
176
+ }
177
+ // unknown_registration (replication lag) or anything else: try the next backup.
178
+ }
179
+ await this.deps.transport.relookup();
180
+ // relookup is a terminal recovery action (it owns re-establishing the registration out of
181
+ // band); reset the counter so a still-dead primary backs off to one relookup per
182
+ // MAX_PING_FAILURES cycles rather than re-running the d_max walk on every subsequent ping.
183
+ this.consecutiveFailures = 0;
184
+ }
185
+
186
+ private async tryReattach(target: Uint8Array): Promise<RenewReplyV1 | undefined> {
187
+ try {
188
+ return await this.reattach(target);
189
+ } catch {
190
+ return undefined;
191
+ }
192
+ }
193
+
194
+ /**
195
+ * Promote `target` (a backup) to primary. The `cohortEpoch` hint is deliberately *not* refreshed
196
+ * here — per the resolved open question it refreshes lazily on the next ping/renewal.
197
+ */
198
+ private promote(target: Uint8Array): void {
199
+ const backups = this.current.backups.filter((b) => !bytesEqual(b, target));
200
+ this.current = { ...this.current, primary: target, backups, lastPing: this.deps.clock() };
201
+ this.consecutiveFailures = 0;
202
+ }
203
+
204
+ private applyPrimaryMoved(reply: RenewReplyV1): void {
205
+ const patch: Partial<RegistrationRecord> = { lastPing: this.deps.clock() };
206
+ if (reply.newPrimary !== undefined) {
207
+ patch.primary = b64(reply.newPrimary);
208
+ }
209
+ if (reply.newBackups !== undefined) {
210
+ patch.backups = reply.newBackups.map(b64);
211
+ }
212
+ if (reply.cohortEpoch !== undefined) {
213
+ // Lazy refresh: the epoch hint catches up here, on the ping that discovers the move.
214
+ this.epochHint = b64(reply.cohortEpoch);
215
+ }
216
+ this.current = { ...this.current, ...patch };
217
+ }
218
+
219
+ /**
220
+ * Build a signed renew. The `reattach=true` (crash-failover) / `withdraw=true` (leave tombstone)
221
+ * flags are carried inside the signed body so the accepting member can trust the attestation; a plain
222
+ * ping omits both fields entirely (a stray renew can never silently usurp a live primary or evict a
223
+ * registration). Each flag is set only when true, matching the current plain-ping wire shape.
224
+ */
225
+ private async buildRenew(opts: { reattach?: boolean; withdraw?: boolean }): Promise<RenewV1> {
226
+ const body: UnsignedRenew = {
227
+ v: 1,
228
+ topicId: bytesKey(this.current.topicId),
229
+ participantId: bytesKey(this.current.participantId),
230
+ correlationId: this.deps.correlationId,
231
+ timestamp: this.deps.clock(),
232
+ };
233
+ if (opts.reattach === true) {
234
+ body.reattach = true;
235
+ }
236
+ if (opts.withdraw === true) {
237
+ body.withdraw = true;
238
+ }
239
+ return { ...body, signature: await this.deps.sign(body) };
240
+ }
241
+ }
242
+
243
+ /** Build a {@link RenewalParticipant} around an initial record. */
244
+ export function createRenewalParticipant(initial: RegistrationRecord, deps: RenewalParticipantDeps): RenewalParticipant {
245
+ return new TtlRenewalParticipant(initial, deps);
246
+ }
247
+
248
+ // --- cohort side ---
249
+
250
+ /** Emits cohort gossip so members converge on the active set after a touch or eviction. */
251
+ export interface RenewalGossip {
252
+ /** Gossip a `lastPing` touch for `rec`. */
253
+ touch(rec: RegistrationRecord): void;
254
+ /** Gossip an eviction for `rec`. */
255
+ evicted(rec: RegistrationRecord): void;
256
+ }
257
+
258
+ export interface RenewalCohortSideDeps {
259
+ store: RegistrationStore;
260
+ /** This member's own peer id. */
261
+ self: Uint8Array;
262
+ /** Deterministic slot assignment, shared with the handoff. */
263
+ slots: SlotAssigner;
264
+ /** Current cohort snapshot — db-p2p supplies it from the membership source. */
265
+ cohort: () => { members: readonly Uint8Array[]; cohortEpoch: Uint8Array };
266
+ gossip: RenewalGossip;
267
+ /**
268
+ * Optional dual-serve predicate, wired to the rotation handoff's `isServing`. During a handoff
269
+ * the previous primary is no longer the computed primary but must keep serving until it is acked;
270
+ * when this returns `true` `onRenew` touches and serves instead of replying `primary_moved`.
271
+ */
272
+ isServing?: (topicId: Uint8Array, participantId: Uint8Array) => boolean;
273
+ /**
274
+ * Optional participant peer-key signature verifier (db-p2p binds it to the peer-sig primitive over
275
+ * {@link import("../wire/payloads.js").renewSigningPayload}). It gates the two privileged
276
+ * participant-attested paths: a `reattach` promotion and a `withdraw` eviction. A renew on either path
277
+ * whose signature does not verify against the claimed `participantId` must never act — a stray/MITM'd
278
+ * ping cannot usurp a live primary or evict someone else's registration (§TTL and renewal). Absent →
279
+ * the gate is skipped (unit tests run without peer crypto); plain pings are never verified here (they
280
+ * only touch `lastPing`).
281
+ */
282
+ verifyParticipantSig?: (renew: RenewV1) => boolean;
283
+ }
284
+
285
+ /** Cohort-side TTL handling: touch on renew, redirect on rotation, sweep stale records. */
286
+ export interface RenewalCohortSide {
287
+ /** Handle an inbound renew. Touches the record, or replies `primary_moved` if this member is no longer primary. */
288
+ onRenew(msg: RenewV1, now: number): RenewReplyV1;
289
+ /** Evict every stale record and gossip each eviction; returns the evicted set. */
290
+ sweepStale(now: number): readonly RegistrationRecord[];
291
+ }
292
+
293
+ class StoreRenewalCohortSide implements RenewalCohortSide {
294
+ /**
295
+ * Epoch-scoped crash-failover overrides: `recordKey → cohortEpoch under which this member accepted a
296
+ * promotion`. A matching entry makes a *subsequent plain ping* serve here even though the computed
297
+ * primary is still the dead node, so the migrated participant stops bouncing. Cleared on epoch change
298
+ * (the next rotation handoff reasserts the deterministic assignment). Sibling to the rotation
299
+ * `isServing` dual-serve exception — distinct state, OR-ed into the serve decision.
300
+ */
301
+ private readonly failoverServing = new Map<string, Uint8Array>();
302
+
303
+ constructor(private readonly deps: RenewalCohortSideDeps) {}
304
+
305
+ onRenew(msg: RenewV1, now: number): RenewReplyV1 {
306
+ const topicId = b64(msg.topicId);
307
+ const participantId = b64(msg.participantId);
308
+ const rec = this.deps.store.getByParticipant(topicId, participantId);
309
+ if (rec === undefined) {
310
+ return { v: 1, result: "unknown_registration" };
311
+ }
312
+ const key = recordKey(topicId, participantId);
313
+
314
+ if (msg.withdraw === true) {
315
+ // Signed leave attestation. A forged/missing signature must never evict someone else's
316
+ // registration → ignore, revealing nothing (`unknown_registration`, the same opaque answer a
317
+ // non-existent record gets). The gate is absent in key-less unit mode, matching reattach.
318
+ // `withdraw` is checked before the slot/primary computation: a withdraw needs no slot or
319
+ // primary check (any holder evicts its replica), and it takes precedence over a (malformed)
320
+ // co-set `reattach` — a record being withdrawn is gone regardless of a promotion request.
321
+ if (this.deps.verifyParticipantSig?.(msg) === false) {
322
+ return { v: 1, result: "unknown_registration" };
323
+ }
324
+ this.deps.store.delete(topicId, participantId);
325
+ this.failoverServing.delete(key); // mirror sweepStale: drop any crash-failover override
326
+ this.deps.gossip.evicted(rec);
327
+ return { v: 1, result: "withdrawn" };
328
+ }
329
+
330
+ const { members, cohortEpoch } = this.deps.cohort();
331
+ const { primary, backups } = this.deps.slots.assignSlots(participantId, cohortEpoch, members);
332
+ const self = this.deps.self;
333
+
334
+ if (msg.reattach === true) {
335
+ // Crash-failover promotion request (participant attests primary unreachable). The signed
336
+ // `reattach` flag is what a backup trusts to promote itself, so a missing/forged signature
337
+ // must never escalate: fall through to a plain redirect (never promote).
338
+ if (this.deps.verifyParticipantSig?.(msg) === false) {
339
+ return this.primaryMoved(primary, backups, cohortEpoch);
340
+ }
341
+ if (bytesEqual(primary, self)) {
342
+ // A rotation already made this member the computed primary; serve, no override needed.
343
+ return this.touchAndServe(rec, now);
344
+ }
345
+ if (backups.some((b) => bytesEqual(b, self))) {
346
+ // Legitimate backup takeover: re-stamp primary, gossip the new assignment, and record an
347
+ // epoch-scoped override so subsequent plain pings keep being served here.
348
+ const restamped: RegistrationRecord = {
349
+ ...rec,
350
+ primary: self,
351
+ backups: rec.backups.filter((b) => !bytesEqual(b, self)),
352
+ lastPing: now,
353
+ };
354
+ this.deps.store.put(restamped);
355
+ this.failoverServing.set(key, cohortEpoch);
356
+ this.deps.gossip.touch(restamped);
357
+ return { v: 1, result: "ok" };
358
+ }
359
+ if (this.deps.isServing?.(topicId, participantId) === true) {
360
+ // Rotation dual-serve already covers this record here.
361
+ return this.touchAndServe(rec, now);
362
+ }
363
+ // Not a valid takeover target (stale participant view): redirect, do not promote.
364
+ return this.primaryMoved(primary, backups, cohortEpoch);
365
+ }
366
+
367
+ // Plain ping.
368
+ const isComputedPrimary = bytesEqual(primary, self);
369
+ const override = this.failoverServing.get(key);
370
+ const overrideMatches = override !== undefined && bytesEqual(override, cohortEpoch);
371
+ // Housekeeping: drop a now-redundant override (this member is the computed primary again) or a
372
+ // stale one tagged under a prior epoch (the rotation handoff governs across the epoch change).
373
+ if (override !== undefined && (isComputedPrimary || !overrideMatches)) {
374
+ this.failoverServing.delete(key);
375
+ }
376
+ const serving = isComputedPrimary || this.deps.isServing?.(topicId, participantId) === true || overrideMatches;
377
+ if (serving) {
378
+ return this.touchAndServe(rec, now);
379
+ }
380
+ return this.primaryMoved(primary, backups, cohortEpoch);
381
+ }
382
+
383
+ /** Existing touch path: stamp `lastPing`, gossip the touch, reply `ok`. */
384
+ private touchAndServe(rec: RegistrationRecord, now: number): RenewReplyV1 {
385
+ const touched: RegistrationRecord = { ...rec, lastPing: now };
386
+ this.deps.store.put(touched);
387
+ this.deps.gossip.touch(touched);
388
+ return { v: 1, result: "ok" };
389
+ }
390
+
391
+ private primaryMoved(primary: Uint8Array, backups: readonly Uint8Array[], cohortEpoch: Uint8Array): RenewReplyV1 {
392
+ return {
393
+ v: 1,
394
+ result: "primary_moved",
395
+ newPrimary: bytesKey(primary),
396
+ newBackups: backups.map(bytesKey),
397
+ cohortEpoch: bytesKey(cohortEpoch),
398
+ };
399
+ }
400
+
401
+ sweepStale(now: number): readonly RegistrationRecord[] {
402
+ const evicted = this.deps.store.evictStale(now);
403
+ for (const rec of evicted) {
404
+ // Drop any crash-failover override for an evicted record. Otherwise it leaks (the record gets
405
+ // no more pings, so the plain-ping housekeeping that would clear it never runs) and — if the
406
+ // same `(topic, participant)` re-registers under the unchanged epoch — could wrongly make a
407
+ // non-primary member keep serving via the stale override.
408
+ this.failoverServing.delete(recordKey(rec.topicId, rec.participantId));
409
+ this.deps.gossip.evicted(rec);
410
+ }
411
+ return evicted;
412
+ }
413
+ }
414
+
415
+ /** Build a {@link RenewalCohortSide}. */
416
+ export function createRenewalCohortSide(deps: RenewalCohortSideDeps): RenewalCohortSide {
417
+ return new StoreRenewalCohortSide(deps);
418
+ }
419
+
420
+ /** Decode a base64url wire field to bytes (the canonical byte form on the wire). */
421
+ function b64(s: string): Uint8Array {
422
+ return b64urlToBytes(s);
423
+ }
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Cohort-topic substrate — deterministic primary/backup sharding.
3
+ *
4
+ * Transcribed from `docs/cohort-topic.md` §Primary and backup sharding:
5
+ *
6
+ * ```
7
+ * order(cohortMembers) = sort(cohortMembers, by PeerId ascending)
8
+ * slot(participantId, cohortEpoch) = H(participantId ‖ cohortEpoch) mod k
9
+ * primary = order[slot]
10
+ * backups = order[slot+1 .. slot+2 (mod k)]
11
+ * ```
12
+ *
13
+ * Deterministic given `(participantId, cohortEpoch, members)`; shards delivery load roughly evenly
14
+ * across the `~k` members. `H` is the injected {@link IRingHash} (db-core's own SHA-256 — **not** a
15
+ * FRET import), reused so coords stay byte-compatible with the rest of the substrate.
16
+ */
17
+
18
+ import type { IRingHash } from "../ports.js";
19
+ import { compareBytes, concatBytes } from "./bytes.js";
20
+
21
+ /** Deterministic slot assignment over a fixed cohort snapshot. */
22
+ export interface SlotAssigner {
23
+ /**
24
+ * `(primary, backups)` for `participantId` under `cohortEpoch` and `cohortMembers`.
25
+ * Backups are the 1..2 members following the primary in ascending order, wrapping mod `k`.
26
+ */
27
+ assignSlots(participantId: Uint8Array, cohortEpoch: Uint8Array, cohortMembers: readonly Uint8Array[]): { primary: Uint8Array; backups: Uint8Array[] };
28
+ }
29
+
30
+ /** Number of warm-failover backups (capped by available members). */
31
+ const MAX_BACKUPS = 2;
32
+
33
+ /** `coord mod k` over the full digest, MSB-first, without bigint allocation. */
34
+ function modK(coord: Uint8Array, k: number): number {
35
+ let acc = 0;
36
+ for (let i = 0; i < coord.length; i++) {
37
+ acc = (acc * 256 + coord[i]!) % k;
38
+ }
39
+ return acc;
40
+ }
41
+
42
+ /** Build a {@link SlotAssigner} bound to a hash. */
43
+ export function createSlotAssigner(hash: IRingHash): SlotAssigner {
44
+ return {
45
+ assignSlots(participantId: Uint8Array, cohortEpoch: Uint8Array, cohortMembers: readonly Uint8Array[]): { primary: Uint8Array; backups: Uint8Array[] } {
46
+ const k = cohortMembers.length;
47
+ if (k === 0) {
48
+ throw new RangeError("assignSlots requires a non-empty cohort");
49
+ }
50
+ const order = [...cohortMembers].sort(compareBytes);
51
+ const slot = modK(hash.H(concatBytes(participantId, cohortEpoch)), k);
52
+ const primary = order[slot]!;
53
+ const backups: Uint8Array[] = [];
54
+ const nBackups = Math.min(MAX_BACKUPS, k - 1);
55
+ for (let i = 1; i <= nBackups; i++) {
56
+ backups.push(order[(slot + i) % k]!);
57
+ }
58
+ return { primary, backups };
59
+ },
60
+ };
61
+ }
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Cohort-topic substrate — in-memory registration store.
3
+ *
4
+ * Per `docs/cohort-topic.md` §Registration mechanics. Records are doubly indexed: an outer map
5
+ * keyed by topic, each holding an inner map keyed by participant. This gives O(1)
6
+ * {@link RegistrationStore.getByParticipant} / {@link RegistrationStore.delete} and O(participants)
7
+ * {@link RegistrationStore.listByTopic} without a secondary index to keep in sync. The store is
8
+ * local soft state; cross-member replication runs over cohort gossip in a later ticket.
9
+ */
10
+
11
+ import { bytesKey } from "./bytes.js";
12
+ import type { RegistrationRecord, RegistrationStore } from "./types.js";
13
+
14
+ class InMemoryRegistrationStore implements RegistrationStore {
15
+ /** topicKey → (participantKey → record). Inner maps are pruned when they empty. */
16
+ private readonly byTopic = new Map<string, Map<string, RegistrationRecord>>();
17
+
18
+ put(rec: RegistrationRecord): void {
19
+ const tk = bytesKey(rec.topicId);
20
+ let inner = this.byTopic.get(tk);
21
+ if (inner === undefined) {
22
+ inner = new Map<string, RegistrationRecord>();
23
+ this.byTopic.set(tk, inner);
24
+ }
25
+ inner.set(bytesKey(rec.participantId), rec);
26
+ }
27
+
28
+ getByParticipant(topicId: Uint8Array, participantId: Uint8Array): RegistrationRecord | undefined {
29
+ return this.byTopic.get(bytesKey(topicId))?.get(bytesKey(participantId));
30
+ }
31
+
32
+ listByTopic(topicId: Uint8Array): readonly RegistrationRecord[] {
33
+ const inner = this.byTopic.get(bytesKey(topicId));
34
+ return inner === undefined ? [] : [...inner.values()];
35
+ }
36
+
37
+ listAll(): readonly RegistrationRecord[] {
38
+ const out: RegistrationRecord[] = [];
39
+ for (const inner of this.byTopic.values()) {
40
+ for (const rec of inner.values()) {
41
+ out.push(rec);
42
+ }
43
+ }
44
+ return out;
45
+ }
46
+
47
+ delete(topicId: Uint8Array, participantId: Uint8Array): void {
48
+ const tk = bytesKey(topicId);
49
+ const inner = this.byTopic.get(tk);
50
+ if (inner === undefined) return;
51
+ inner.delete(bytesKey(participantId));
52
+ if (inner.size === 0) {
53
+ this.byTopic.delete(tk);
54
+ }
55
+ }
56
+
57
+ directParticipants(topicId: Uint8Array): number {
58
+ return this.byTopic.get(bytesKey(topicId))?.size ?? 0;
59
+ }
60
+
61
+ evictStale(now: number): readonly RegistrationRecord[] {
62
+ const evicted: RegistrationRecord[] = [];
63
+ for (const [tk, inner] of this.byTopic) {
64
+ for (const [pk, rec] of inner) {
65
+ if (now - rec.lastPing > rec.ttl) {
66
+ evicted.push(rec);
67
+ inner.delete(pk);
68
+ }
69
+ }
70
+ if (inner.size === 0) {
71
+ this.byTopic.delete(tk);
72
+ }
73
+ }
74
+ return evicted;
75
+ }
76
+ }
77
+
78
+ /** Construct an empty {@link RegistrationStore}. */
79
+ export function createRegistrationStore(): RegistrationStore {
80
+ return new InMemoryRegistrationStore();
81
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Cohort-topic substrate — registration record store, types and constants.
3
+ *
4
+ * Transcribed from `docs/cohort-topic.md` §Registration mechanics and §Primary and backup
5
+ * sharding. This module owns the **local**, transport-agnostic shapes: the soft-state record a
6
+ * cohort member holds per participant, the in-memory store interface over those records, and the
7
+ * TTL constants. Cross-member replication (cohort gossip) is layered on top by db-p2p and the
8
+ * bridge ticket; db-core never imports FRET or libp2p here.
9
+ *
10
+ * Peer ids are the opaque byte-array references the cohort-topic substrate uses throughout — the
11
+ * same representation as {@link import("../ports.js").PeerRef}`.id` and
12
+ * {@link import("../ports.js").RingCoord} (raw `Uint8Array`, not the structural
13
+ * {@link import("../../network/types.js").PeerId}). The wire layer carries them as base64url
14
+ * strings; this store works in raw bytes and the renewal/handoff bridges translate at the wire
15
+ * boundary.
16
+ */
17
+
18
+ /**
19
+ * Soft-state registration a cohort member holds per participant. Replicated across the `~k`
20
+ * members by cohort gossip; only {@link RegistrationRecord.primary} serves, with
21
+ * {@link RegistrationRecord.backups} watching for warm failover.
22
+ */
23
+ export interface RegistrationRecord {
24
+ /** Topic id, 32 bytes. */
25
+ topicId: Uint8Array;
26
+ /** Registering participant. */
27
+ participantId: Uint8Array;
28
+ /** Tier this registration sits at (0..3). */
29
+ tier: number;
30
+ /** Cohort member assigned to serve this participant. */
31
+ primary: Uint8Array;
32
+ /** 1..2 warm-failover cohort members. */
33
+ backups: Uint8Array[];
34
+ /** Unix ms the registration first attached. */
35
+ attachedAt: number;
36
+ /** Unix ms of the most recent successful ping/touch. */
37
+ lastPing: number;
38
+ /** Lifetime in ms; record is stale once `now − lastPing > ttl`. */
39
+ ttl: number;
40
+ /** Opaque application-defined per-registration state; the layer never interprets it. */
41
+ appState?: Uint8Array;
42
+ }
43
+
44
+ /**
45
+ * In-memory registration store, indexed for both per-participant lookup and per-topic listing.
46
+ * This ticket owns the **local** store and its indexes; the gossip layer and TTL loop call the
47
+ * deterministic functions over it.
48
+ */
49
+ export interface RegistrationStore {
50
+ /** Insert or replace the record for `(topicId, participantId)`. */
51
+ put(rec: RegistrationRecord): void;
52
+ /** Record for `(topicId, participantId)`, or `undefined`. */
53
+ getByParticipant(topicId: Uint8Array, participantId: Uint8Array): RegistrationRecord | undefined;
54
+ /** All records held for `topicId` (empty if none). */
55
+ listByTopic(topicId: Uint8Array): readonly RegistrationRecord[];
56
+ /** Every record across all topics — used by the rotation handoff inventory pass. */
57
+ listAll(): readonly RegistrationRecord[];
58
+ /** Remove the record for `(topicId, participantId)`. */
59
+ delete(topicId: Uint8Array, participantId: Uint8Array): void;
60
+ /** Stock count of direct participants for `topicId` (drives promotion). */
61
+ directParticipants(topicId: Uint8Array): number;
62
+ /** Remove and return every record where `now − lastPing > ttl`. */
63
+ evictStale(now: number): readonly RegistrationRecord[];
64
+ }
65
+
66
+ /** Core-tier default registration TTL (ms). */
67
+ export const DEFAULT_TTL_MS = 90_000;
68
+ /** Edge-tier default registration TTL (ms). */
69
+ export const EDGE_TTL_MS = 60_000;
70
+ /** Consecutive ping failures before a participant promotes `backups[0]`. */
71
+ export const MAX_PING_FAILURES = 3;
72
+
73
+ /** `ping_interval = ttl / 3` (default 30s Core, 20s Edge), floored to whole ms. */
74
+ export function pingIntervalMs(ttl: number): number {
75
+ return Math.floor(ttl / 3);
76
+ }