@optimystic/db-core 1.5.0 → 1.6.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 (157) hide show
  1. package/README.md +336 -336
  2. package/dist/src/cluster/structs.d.ts +37 -0
  3. package/dist/src/cluster/structs.d.ts.map +1 -1
  4. package/dist/src/cluster/structs.js +13 -0
  5. package/dist/src/cluster/structs.js.map +1 -1
  6. package/dist/src/collection/collection.d.ts +44 -4
  7. package/dist/src/collection/collection.d.ts.map +1 -1
  8. package/dist/src/collection/collection.js +95 -14
  9. package/dist/src/collection/collection.js.map +1 -1
  10. package/dist/src/log/log.d.ts +41 -8
  11. package/dist/src/log/log.d.ts.map +1 -1
  12. package/dist/src/log/log.js +15 -10
  13. package/dist/src/log/log.js.map +1 -1
  14. package/dist/src/testing/test-transactor.d.ts +36 -0
  15. package/dist/src/testing/test-transactor.d.ts.map +1 -1
  16. package/dist/src/testing/test-transactor.js +49 -0
  17. package/dist/src/testing/test-transactor.js.map +1 -1
  18. package/dist/src/transaction/coordinator.d.ts.map +1 -1
  19. package/dist/src/transaction/coordinator.js +26 -9
  20. package/dist/src/transaction/coordinator.js.map +1 -1
  21. package/package.json +1 -1
  22. package/src/blocks/block-store.ts +31 -31
  23. package/src/blocks/block-types.ts +11 -11
  24. package/src/blocks/index.ts +5 -5
  25. package/src/blocks/structs.ts +17 -17
  26. package/src/btree/btree.ts +872 -872
  27. package/src/btree/independent-trunk.ts +53 -53
  28. package/src/btree/index.ts +5 -5
  29. package/src/btree/key-range.ts +15 -15
  30. package/src/btree/keyset.ts +6 -6
  31. package/src/btree/nodes.ts +25 -25
  32. package/src/btree/path.ts +37 -37
  33. package/src/btree/tree-block.ts +11 -11
  34. package/src/btree/trunk.ts +14 -14
  35. package/src/chain/chain-nodes.ts +26 -26
  36. package/src/chain/chain.ts +349 -349
  37. package/src/chain/index.ts +3 -3
  38. package/src/cluster/i-cluster.ts +6 -6
  39. package/src/cluster/index.ts +3 -3
  40. package/src/cluster/structs.ts +38 -0
  41. package/src/cohort-topic/addressing.ts +120 -120
  42. package/src/cohort-topic/antidos/bootstrap-evidence.ts +106 -106
  43. package/src/cohort-topic/antidos/index.ts +5 -5
  44. package/src/cohort-topic/antidos/rate-limiter.ts +210 -210
  45. package/src/cohort-topic/antidos/replay-guard.ts +146 -146
  46. package/src/cohort-topic/antidos/topic-budget.ts +160 -160
  47. package/src/cohort-topic/antiflood/index.ts +2 -2
  48. package/src/cohort-topic/antiflood/invariants.ts +108 -108
  49. package/src/cohort-topic/antiflood/jitter.ts +117 -117
  50. package/src/cohort-topic/coldstart.ts +237 -237
  51. package/src/cohort-topic/dmax.ts +88 -88
  52. package/src/cohort-topic/gossip/bus.ts +254 -254
  53. package/src/cohort-topic/gossip/index.ts +3 -3
  54. package/src/cohort-topic/gossip/records.ts +45 -45
  55. package/src/cohort-topic/gossip/view.ts +91 -91
  56. package/src/cohort-topic/index.ts +20 -20
  57. package/src/cohort-topic/load/barometer.ts +134 -134
  58. package/src/cohort-topic/load/index.ts +1 -1
  59. package/src/cohort-topic/member-engine.ts +430 -430
  60. package/src/cohort-topic/membership/index.ts +3 -3
  61. package/src/cohort-topic/membership/publisher.ts +163 -163
  62. package/src/cohort-topic/membership/source.ts +41 -41
  63. package/src/cohort-topic/ports.ts +157 -157
  64. package/src/cohort-topic/promotion.ts +477 -477
  65. package/src/cohort-topic/registration/bytes.ts +37 -37
  66. package/src/cohort-topic/registration/handoff.ts +154 -154
  67. package/src/cohort-topic/registration/index.ts +6 -6
  68. package/src/cohort-topic/registration/renewal.ts +495 -495
  69. package/src/cohort-topic/registration/sharding.ts +61 -61
  70. package/src/cohort-topic/registration/store.ts +81 -81
  71. package/src/cohort-topic/registration/types.ts +91 -91
  72. package/src/cohort-topic/ring-hash.ts +50 -50
  73. package/src/cohort-topic/service.ts +416 -416
  74. package/src/cohort-topic/sig/index.ts +2 -2
  75. package/src/cohort-topic/sig/payloads.ts +59 -59
  76. package/src/cohort-topic/sig/threshold.ts +64 -64
  77. package/src/cohort-topic/tiers.ts +74 -74
  78. package/src/cohort-topic/traffic.ts +233 -233
  79. package/src/cohort-topic/walk.ts +326 -326
  80. package/src/cohort-topic/willingness.ts +237 -237
  81. package/src/cohort-topic/wire/index.ts +18 -18
  82. package/src/cohort-topic/wire/payloads.ts +126 -126
  83. package/src/collection/action.ts +48 -48
  84. package/src/collection/collection-type-registry.ts +37 -37
  85. package/src/collection/collection.ts +2203 -2118
  86. package/src/collection/index.ts +4 -4
  87. package/src/collections/diary/index.ts +2 -2
  88. package/src/collections/diary/struct.ts +3 -3
  89. package/src/collections/index.ts +2 -2
  90. package/src/collections/tree/collection-trunk.ts +25 -25
  91. package/src/collections/tree/index.ts +2 -2
  92. package/src/collections/tree/readme.md +55 -55
  93. package/src/log/index.ts +2 -2
  94. package/src/log/log.ts +304 -268
  95. package/src/log/struct.ts +165 -165
  96. package/src/matchmaking/capability-filter.ts +45 -45
  97. package/src/matchmaking/config.ts +98 -98
  98. package/src/matchmaking/index.ts +21 -21
  99. package/src/matchmaking/multi-cohort-seeker.ts +234 -234
  100. package/src/matchmaking/provider.ts +123 -123
  101. package/src/matchmaking/query-eval.ts +105 -105
  102. package/src/matchmaking/seeker-walk.ts +127 -127
  103. package/src/matchmaking/seeker.ts +86 -86
  104. package/src/matchmaking/topic-anchor.ts +90 -90
  105. package/src/matchmaking/voting-quorum.ts +394 -394
  106. package/src/network/i-peer-network.ts +25 -25
  107. package/src/network/repo-protocol.ts +13 -13
  108. package/src/network/types.ts +37 -37
  109. package/src/reactivity/backfill.ts +220 -220
  110. package/src/reactivity/backpressure.ts +191 -191
  111. package/src/reactivity/checkpoint.ts +308 -308
  112. package/src/reactivity/config.ts +172 -172
  113. package/src/reactivity/dedupe.ts +132 -132
  114. package/src/reactivity/forwarder.ts +87 -87
  115. package/src/reactivity/index.ts +34 -34
  116. package/src/reactivity/notification.ts +123 -123
  117. package/src/reactivity/policy.ts +79 -79
  118. package/src/reactivity/push-state.ts +310 -310
  119. package/src/reactivity/recover.ts +153 -153
  120. package/src/reactivity/replay-buffer.ts +141 -141
  121. package/src/reactivity/resume.ts +549 -549
  122. package/src/reactivity/rotation.ts +415 -415
  123. package/src/reactivity/subscriber.ts +132 -132
  124. package/src/reactivity/subscription.ts +66 -66
  125. package/src/reactivity/topic-anchor.ts +71 -71
  126. package/src/reactivity/verify.ts +73 -73
  127. package/src/reactivity/wire-validate.ts +13 -13
  128. package/src/testing/async-wait.ts +65 -65
  129. package/src/testing/index.ts +2 -2
  130. package/src/testing/test-transactor.ts +62 -0
  131. package/src/transaction/actions-engine.ts +68 -68
  132. package/src/transaction/coordinator.ts +28 -11
  133. package/src/transaction/index.ts +52 -52
  134. package/src/transaction/operations-hash.ts +196 -196
  135. package/src/transaction/read-dependency-collector.ts +78 -78
  136. package/src/transaction/session.ts +238 -238
  137. package/src/transaction/transaction.ts +442 -442
  138. package/src/transaction/validator.ts +197 -197
  139. package/src/transactor/block-floors.ts +126 -126
  140. package/src/transactor/index.ts +6 -6
  141. package/src/transactor/network-transactor.ts +1545 -1545
  142. package/src/transform/atomic-proxy.ts +92 -92
  143. package/src/transform/cache-source.ts +370 -370
  144. package/src/transform/helpers.ts +164 -164
  145. package/src/transform/index.ts +7 -7
  146. package/src/transform/struct.ts +21 -21
  147. package/src/utility/actor.ts +62 -62
  148. package/src/utility/backoff.ts +95 -95
  149. package/src/utility/batch-coordinator.ts +191 -191
  150. package/src/utility/ensured.ts +32 -32
  151. package/src/utility/groupby.ts +18 -18
  152. package/src/utility/hash-string.ts +14 -14
  153. package/src/utility/is-record-empty.ts +5 -5
  154. package/src/utility/latches.ts +42 -42
  155. package/src/utility/lru-map.ts +60 -60
  156. package/src/utility/nameof.ts +7 -7
  157. package/src/utility/pending.ts +43 -43
@@ -1,2 +1,2 @@
1
- export * from "./payloads.js";
2
- export * from "./threshold.js";
1
+ export * from "./payloads.js";
2
+ export * from "./threshold.js";
@@ -1,59 +1,59 @@
1
- /**
2
- * Cohort-topic substrate — canonical threshold-signature payloads.
3
- *
4
- * A threshold signature is only verifiable if signer and verifier agree on the exact bytes signed.
5
- * These builders produce that deterministic byte image for each threshold-signed message
6
- * (`MembershipCertV1`, `PromotionNoticeV1`, `DemotionNoticeV1`, `ChildLinkV1`), covering exactly the
7
- * semantic fields per `docs/cohort-topic.md` §Wire formats — never the `thresholdSig`/`signers` envelope.
8
- *
9
- * Determinism comes from encoding an explicitly-ordered JSON array (array order is stable, unlike
10
- * object key order) as UTF-8. The publisher signs this image; the verifier recomputes and checks it.
11
- *
12
- * The notice images carry `cohortCoord` (the served coord the deciding cohort sits at) so the coord the
13
- * receiver routes by is bound into the signed bytes — rewriting it to hijack a sibling cohort breaks
14
- * verification. It is inserted **just before the trailing `cohortEpoch`**, deliberately keeping `cohortEpoch`
15
- * the LAST element: the `/sign` endorser (`handleSignRequest`) reads a notice's embedded epoch positionally
16
- * as `image[image.length - 1]`, so `cohortEpoch` must stay last. This is a hard, coordinated change to the
17
- * canonical signed bytes, acceptable pre-release (no deployed nodes speak this protocol, so there is no
18
- * version to negotiate).
19
- */
20
-
21
- import type { ChildLinkV1, DemotionNoticeV1, MembershipCertV1, PromotionNoticeV1 } from "../wire/types.js";
22
-
23
- const utf8 = new TextEncoder();
24
-
25
- /** Fields of a `MembershipCertV1` covered by its threshold signature (doc: cohortCoord, cohortEpoch, members, stabilizedAt). */
26
- export type MembershipCertSignable = Pick<MembershipCertV1, "cohortCoord" | "cohortEpoch" | "members" | "stabilizedAt">;
27
-
28
- /** Canonical signed byte image of a membership certificate. */
29
- export function membershipCertSigningPayload(cert: MembershipCertSignable): Uint8Array {
30
- return utf8.encode(JSON.stringify(["MembershipCertV1", cert.cohortCoord, cert.cohortEpoch, cert.members, cert.stabilizedAt]));
31
- }
32
-
33
- /** Fields of a `PromotionNoticeV1` covered by its threshold signature. */
34
- export type PromotionSignable = Pick<PromotionNoticeV1, "topicId" | "fromTier" | "toTier" | "effectiveAt" | "cohortEpoch" | "cohortCoord">;
35
-
36
- /** Canonical signed byte image of a promotion notice. `cohortEpoch` stays last (the `/sign` endorser reads it positionally). */
37
- export function promotionNoticeSigningPayload(n: PromotionSignable): Uint8Array {
38
- return utf8.encode(JSON.stringify(["PromotionNoticeV1", n.topicId, n.fromTier, n.toTier, n.effectiveAt, n.cohortCoord, n.cohortEpoch]));
39
- }
40
-
41
- /** Fields of a `DemotionNoticeV1` covered by its threshold signature. */
42
- export type DemotionSignable = Pick<DemotionNoticeV1, "topicId" | "tier" | "parentCohortCoord" | "effectiveAt" | "cohortEpoch" | "cohortCoord">;
43
-
44
- /** Canonical signed byte image of a demotion notice. `cohortEpoch` stays last (the `/sign` endorser reads it positionally). */
45
- export function demotionNoticeSigningPayload(n: DemotionSignable): Uint8Array {
46
- return utf8.encode(JSON.stringify(["DemotionNoticeV1", n.topicId, n.tier, n.parentCohortCoord, n.effectiveAt, n.cohortCoord, n.cohortEpoch]));
47
- }
48
-
49
- /** Fields of a `ChildLinkV1` covered by the child cohort's threshold signature. */
50
- export type ChildLinkSignable = Pick<ChildLinkV1, "topicId" | "childCohortCoord" | "childParticipantCoord" | "childTier" | "tier" | "effectiveAt" | "cohortEpoch">;
51
-
52
- /**
53
- * Canonical signed byte image of a child-link frame. `cohortEpoch` stays **last** so the `/sign` endorser
54
- * (`handleSignRequest`) reads the embedded epoch positionally as `image[image.length - 1]`, exactly as it
55
- * does for a promotion / demotion notice.
56
- */
57
- export function childLinkSigningPayload(n: ChildLinkSignable): Uint8Array {
58
- return utf8.encode(JSON.stringify(["ChildLinkV1", n.topicId, n.childCohortCoord, n.childParticipantCoord, n.childTier, n.tier, n.effectiveAt, n.cohortEpoch]));
59
- }
1
+ /**
2
+ * Cohort-topic substrate — canonical threshold-signature payloads.
3
+ *
4
+ * A threshold signature is only verifiable if signer and verifier agree on the exact bytes signed.
5
+ * These builders produce that deterministic byte image for each threshold-signed message
6
+ * (`MembershipCertV1`, `PromotionNoticeV1`, `DemotionNoticeV1`, `ChildLinkV1`), covering exactly the
7
+ * semantic fields per `docs/cohort-topic.md` §Wire formats — never the `thresholdSig`/`signers` envelope.
8
+ *
9
+ * Determinism comes from encoding an explicitly-ordered JSON array (array order is stable, unlike
10
+ * object key order) as UTF-8. The publisher signs this image; the verifier recomputes and checks it.
11
+ *
12
+ * The notice images carry `cohortCoord` (the served coord the deciding cohort sits at) so the coord the
13
+ * receiver routes by is bound into the signed bytes — rewriting it to hijack a sibling cohort breaks
14
+ * verification. It is inserted **just before the trailing `cohortEpoch`**, deliberately keeping `cohortEpoch`
15
+ * the LAST element: the `/sign` endorser (`handleSignRequest`) reads a notice's embedded epoch positionally
16
+ * as `image[image.length - 1]`, so `cohortEpoch` must stay last. This is a hard, coordinated change to the
17
+ * canonical signed bytes, acceptable pre-release (no deployed nodes speak this protocol, so there is no
18
+ * version to negotiate).
19
+ */
20
+
21
+ import type { ChildLinkV1, DemotionNoticeV1, MembershipCertV1, PromotionNoticeV1 } from "../wire/types.js";
22
+
23
+ const utf8 = new TextEncoder();
24
+
25
+ /** Fields of a `MembershipCertV1` covered by its threshold signature (doc: cohortCoord, cohortEpoch, members, stabilizedAt). */
26
+ export type MembershipCertSignable = Pick<MembershipCertV1, "cohortCoord" | "cohortEpoch" | "members" | "stabilizedAt">;
27
+
28
+ /** Canonical signed byte image of a membership certificate. */
29
+ export function membershipCertSigningPayload(cert: MembershipCertSignable): Uint8Array {
30
+ return utf8.encode(JSON.stringify(["MembershipCertV1", cert.cohortCoord, cert.cohortEpoch, cert.members, cert.stabilizedAt]));
31
+ }
32
+
33
+ /** Fields of a `PromotionNoticeV1` covered by its threshold signature. */
34
+ export type PromotionSignable = Pick<PromotionNoticeV1, "topicId" | "fromTier" | "toTier" | "effectiveAt" | "cohortEpoch" | "cohortCoord">;
35
+
36
+ /** Canonical signed byte image of a promotion notice. `cohortEpoch` stays last (the `/sign` endorser reads it positionally). */
37
+ export function promotionNoticeSigningPayload(n: PromotionSignable): Uint8Array {
38
+ return utf8.encode(JSON.stringify(["PromotionNoticeV1", n.topicId, n.fromTier, n.toTier, n.effectiveAt, n.cohortCoord, n.cohortEpoch]));
39
+ }
40
+
41
+ /** Fields of a `DemotionNoticeV1` covered by its threshold signature. */
42
+ export type DemotionSignable = Pick<DemotionNoticeV1, "topicId" | "tier" | "parentCohortCoord" | "effectiveAt" | "cohortEpoch" | "cohortCoord">;
43
+
44
+ /** Canonical signed byte image of a demotion notice. `cohortEpoch` stays last (the `/sign` endorser reads it positionally). */
45
+ export function demotionNoticeSigningPayload(n: DemotionSignable): Uint8Array {
46
+ return utf8.encode(JSON.stringify(["DemotionNoticeV1", n.topicId, n.tier, n.parentCohortCoord, n.effectiveAt, n.cohortCoord, n.cohortEpoch]));
47
+ }
48
+
49
+ /** Fields of a `ChildLinkV1` covered by the child cohort's threshold signature. */
50
+ export type ChildLinkSignable = Pick<ChildLinkV1, "topicId" | "childCohortCoord" | "childParticipantCoord" | "childTier" | "tier" | "effectiveAt" | "cohortEpoch">;
51
+
52
+ /**
53
+ * Canonical signed byte image of a child-link frame. `cohortEpoch` stays **last** so the `/sign` endorser
54
+ * (`handleSignRequest`) reads the embedded epoch positionally as `image[image.length - 1]`, exactly as it
55
+ * does for a promotion / demotion notice.
56
+ */
57
+ export function childLinkSigningPayload(n: ChildLinkSignable): Uint8Array {
58
+ return utf8.encode(JSON.stringify(["ChildLinkV1", n.topicId, n.childCohortCoord, n.childParticipantCoord, n.childTier, n.tier, n.effectiveAt, n.cohortEpoch]));
59
+ }
@@ -1,64 +1,64 @@
1
- /**
2
- * Cohort-topic substrate — `k − x` threshold signing and verification.
3
- *
4
- * Per `docs/cohort-topic.md` §FRET integration (L659) and §Membership snapshots: the cohort
5
- * threshold-signs `PromotionNoticeV1` / `DemotionNoticeV1` / `MembershipCertV1` with `minSigs`
6
- * (= `k − x`, default 14) signers. This module is db-core logic over the {@link ICohortThresholdCrypto}
7
- * port; db-p2p binds the port to FRET's `minSigs` cohort-signature assembly — db-core never imports
8
- * FRET, and the underlying scheme is reused unchanged.
9
- *
10
- * {@link CohortSigner.verifyThreshold} layers the membership check db-core owns on top of the raw
11
- * crypto: the `signers` must be a distinct `≥ minSigs` subset of the certificate's `members`, *and*
12
- * the signature must verify against the payload. Either failure → not verified.
13
- */
14
-
15
- import type { ICohortThresholdCrypto } from "../ports.js";
16
- import { bytesToB64url } from "../wire/codec.js";
17
- import type { MembershipCertV1 } from "../wire/types.js";
18
-
19
- /** Default cohort-signature threshold, `k − x` (see §Configuration). */
20
- export const DEFAULT_MIN_SIGS = 14;
21
-
22
- /** Threshold signer/verifier over the injected cohort crypto. Peer ids are raw bytes. */
23
- export interface CohortSigner {
24
- /** Assemble a cohort threshold signature over `payload` (collects `minSigs` signers). */
25
- thresholdSign(payload: Uint8Array): Promise<{ thresholdSig: Uint8Array; signers: Uint8Array[] }>;
26
- /**
27
- * Verify a threshold-signed message: `signers` are a distinct `≥ minSigs` subset of
28
- * `cert.members` and `sig` is a valid cohort signature over `payload`.
29
- */
30
- verifyThreshold(payload: Uint8Array, sig: Uint8Array, signers: readonly Uint8Array[], cert: MembershipCertV1, minSigs: number): boolean;
31
- }
32
-
33
- class CryptoCohortSigner implements CohortSigner {
34
- constructor(private readonly crypto: ICohortThresholdCrypto, private readonly minSigs: number) {}
35
-
36
- thresholdSign(payload: Uint8Array): Promise<{ thresholdSig: Uint8Array; signers: Uint8Array[] }> {
37
- return this.crypto.assemble(payload, this.minSigs);
38
- }
39
-
40
- verifyThreshold(payload: Uint8Array, sig: Uint8Array, signers: readonly Uint8Array[], cert: MembershipCertV1, minSigs: number): boolean {
41
- if (signers.length < minSigs) {
42
- return false;
43
- }
44
- // Members are base64url on the wire; compare signers in that same canonical form.
45
- const memberSet = new Set(cert.members);
46
- const seen = new Set<string>();
47
- for (const signer of signers) {
48
- const key = bytesToB64url(signer);
49
- if (seen.has(key)) {
50
- return false; // a duplicated signer cannot pad the count toward minSigs
51
- }
52
- seen.add(key);
53
- if (!memberSet.has(key)) {
54
- return false; // signer is not a member of the attested cohort
55
- }
56
- }
57
- return this.crypto.verify(payload, sig, signers);
58
- }
59
- }
60
-
61
- /** Build a {@link CohortSigner} over the FRET-backed (in db-p2p) threshold crypto. */
62
- export function createCohortSigner(crypto: ICohortThresholdCrypto, minSigs: number = DEFAULT_MIN_SIGS): CohortSigner {
63
- return new CryptoCohortSigner(crypto, minSigs);
64
- }
1
+ /**
2
+ * Cohort-topic substrate — `k − x` threshold signing and verification.
3
+ *
4
+ * Per `docs/cohort-topic.md` §FRET integration (L659) and §Membership snapshots: the cohort
5
+ * threshold-signs `PromotionNoticeV1` / `DemotionNoticeV1` / `MembershipCertV1` with `minSigs`
6
+ * (= `k − x`, default 14) signers. This module is db-core logic over the {@link ICohortThresholdCrypto}
7
+ * port; db-p2p binds the port to FRET's `minSigs` cohort-signature assembly — db-core never imports
8
+ * FRET, and the underlying scheme is reused unchanged.
9
+ *
10
+ * {@link CohortSigner.verifyThreshold} layers the membership check db-core owns on top of the raw
11
+ * crypto: the `signers` must be a distinct `≥ minSigs` subset of the certificate's `members`, *and*
12
+ * the signature must verify against the payload. Either failure → not verified.
13
+ */
14
+
15
+ import type { ICohortThresholdCrypto } from "../ports.js";
16
+ import { bytesToB64url } from "../wire/codec.js";
17
+ import type { MembershipCertV1 } from "../wire/types.js";
18
+
19
+ /** Default cohort-signature threshold, `k − x` (see §Configuration). */
20
+ export const DEFAULT_MIN_SIGS = 14;
21
+
22
+ /** Threshold signer/verifier over the injected cohort crypto. Peer ids are raw bytes. */
23
+ export interface CohortSigner {
24
+ /** Assemble a cohort threshold signature over `payload` (collects `minSigs` signers). */
25
+ thresholdSign(payload: Uint8Array): Promise<{ thresholdSig: Uint8Array; signers: Uint8Array[] }>;
26
+ /**
27
+ * Verify a threshold-signed message: `signers` are a distinct `≥ minSigs` subset of
28
+ * `cert.members` and `sig` is a valid cohort signature over `payload`.
29
+ */
30
+ verifyThreshold(payload: Uint8Array, sig: Uint8Array, signers: readonly Uint8Array[], cert: MembershipCertV1, minSigs: number): boolean;
31
+ }
32
+
33
+ class CryptoCohortSigner implements CohortSigner {
34
+ constructor(private readonly crypto: ICohortThresholdCrypto, private readonly minSigs: number) {}
35
+
36
+ thresholdSign(payload: Uint8Array): Promise<{ thresholdSig: Uint8Array; signers: Uint8Array[] }> {
37
+ return this.crypto.assemble(payload, this.minSigs);
38
+ }
39
+
40
+ verifyThreshold(payload: Uint8Array, sig: Uint8Array, signers: readonly Uint8Array[], cert: MembershipCertV1, minSigs: number): boolean {
41
+ if (signers.length < minSigs) {
42
+ return false;
43
+ }
44
+ // Members are base64url on the wire; compare signers in that same canonical form.
45
+ const memberSet = new Set(cert.members);
46
+ const seen = new Set<string>();
47
+ for (const signer of signers) {
48
+ const key = bytesToB64url(signer);
49
+ if (seen.has(key)) {
50
+ return false; // a duplicated signer cannot pad the count toward minSigs
51
+ }
52
+ seen.add(key);
53
+ if (!memberSet.has(key)) {
54
+ return false; // signer is not a member of the attested cohort
55
+ }
56
+ }
57
+ return this.crypto.verify(payload, sig, signers);
58
+ }
59
+ }
60
+
61
+ /** Build a {@link CohortSigner} over the FRET-backed (in db-p2p) threshold crypto. */
62
+ export function createCohortSigner(crypto: ICohortThresholdCrypto, minSigs: number = DEFAULT_MIN_SIGS): CohortSigner {
63
+ return new CryptoCohortSigner(crypto, minSigs);
64
+ }
@@ -1,74 +1,74 @@
1
- /**
2
- * Cohort-topic substrate — tier ladder (T0–T3) and node profiles (Edge / Core).
3
- *
4
- * Transcribed from `docs/cohort-topic.md` §Tier ladder. This module encodes only the **static**
5
- * tier classes, the Edge/Core profiles, and the per-node override surface. The decline-policy
6
- * *behavior* (T0 never declined while serving, T3 declined freely) and the Edge runtime overrides
7
- * (`ttl = 60s`, `ping_interval = 20s`, sticky-cached backups) are willingness/TTL concerns owned by
8
- * later tickets — only the profile *flags* (which tiers a node is willing to forward) live here.
9
- */
10
-
11
- /**
12
- * System-wide capacity tier, in priority order (T0 essential … T3 luxury).
13
- *
14
- * Modeled as a frozen const-object + value-union type rather than a TS `enum`: db-core's test
15
- * runner uses Node's native type-stripping, which rejects `enum` (non-erasable syntax). The object
16
- * carries the named members (`Tier.T0`), the type alias carries the value union (`0 | 1 | 2 | 3`).
17
- *
18
- * - **T0 — essential**: transaction commit, block production, threshold-sig contribution.
19
- * - **T1 — correctness-supporting**: chain serving, replay-window storage, partition heal.
20
- * - **T2 — functional**: matchmaking/voting directories, capability discovery, capacity gossip.
21
- * - **T3 — luxury**: reactivity push forwarding, anticipatory warm-up, optional delta payloads.
22
- */
23
- export const Tier = { T0: 0, T1: 1, T2: 2, T3: 3 } as const;
24
- export type Tier = (typeof Tier)[keyof typeof Tier];
25
-
26
- /** All four tiers, ascending — useful for iteration and override validation. */
27
- export const ALL_TIERS: readonly Tier[] = [Tier.T0, Tier.T1, Tier.T2, Tier.T3];
28
-
29
- /**
30
- * A node's static tier classification. `willingTiers` is the set of tiers this node will take on
31
- * *forwarder* roles for; cohort assembly under FRET is tier-blind, so this is the per-node gate the
32
- * willingness check consults. Edge: `{T0, T1}`; Core: `{T0, T1, T2, T3}` (operator-narrowable).
33
- */
34
- export interface NodeProfile {
35
- readonly kind: "edge" | "core";
36
- readonly willingTiers: ReadonlySet<Tier>;
37
- }
38
-
39
- /** Per-node operator override surface. Currently the willing-tier set; widened by later tickets. */
40
- export interface NodeProfileOverrides {
41
- /**
42
- * Restrict the willing-tier set. Tiers outside the profile's base set are ignored (Edge can never
43
- * be widened to T2/T3 via override — that is a hardware/policy class, not a knob).
44
- */
45
- willingTiers?: Iterable<Tier>;
46
- }
47
-
48
- const EDGE_BASE_TIERS: readonly Tier[] = [Tier.T0, Tier.T1];
49
- const CORE_BASE_TIERS: readonly Tier[] = ALL_TIERS;
50
-
51
- /** Intersect a requested override set with the profile's base tiers (override can only narrow). */
52
- function resolveWillingTiers(base: readonly Tier[], overrides?: NodeProfileOverrides): ReadonlySet<Tier> {
53
- if (!overrides?.willingTiers) {
54
- return new Set(base);
55
- }
56
- const requested = new Set(overrides.willingTiers);
57
- return new Set(base.filter((t) => requested.has(t)));
58
- }
59
-
60
- /**
61
- * Edge profile (mobile / browser / IoT): forwards T0 + T1 only. T2/T3 willingness is permanently
62
- * off and an operator override cannot turn it on. An override may further narrow (e.g. to `{T0}`).
63
- */
64
- export function edgeProfile(overrides?: NodeProfileOverrides): NodeProfile {
65
- return { kind: "edge", willingTiers: resolveWillingTiers(EDGE_BASE_TIERS, overrides) };
66
- }
67
-
68
- /**
69
- * Core profile (servers / fixed infrastructure): forwards T0–T3 by default. An operator override may
70
- * restrict it to a subset (e.g. `{T0, T1, T2}` to shed reactivity-push duty).
71
- */
72
- export function coreProfile(overrides?: NodeProfileOverrides): NodeProfile {
73
- return { kind: "core", willingTiers: resolveWillingTiers(CORE_BASE_TIERS, overrides) };
74
- }
1
+ /**
2
+ * Cohort-topic substrate — tier ladder (T0–T3) and node profiles (Edge / Core).
3
+ *
4
+ * Transcribed from `docs/cohort-topic.md` §Tier ladder. This module encodes only the **static**
5
+ * tier classes, the Edge/Core profiles, and the per-node override surface. The decline-policy
6
+ * *behavior* (T0 never declined while serving, T3 declined freely) and the Edge runtime overrides
7
+ * (`ttl = 60s`, `ping_interval = 20s`, sticky-cached backups) are willingness/TTL concerns owned by
8
+ * later tickets — only the profile *flags* (which tiers a node is willing to forward) live here.
9
+ */
10
+
11
+ /**
12
+ * System-wide capacity tier, in priority order (T0 essential … T3 luxury).
13
+ *
14
+ * Modeled as a frozen const-object + value-union type rather than a TS `enum`: db-core's test
15
+ * runner uses Node's native type-stripping, which rejects `enum` (non-erasable syntax). The object
16
+ * carries the named members (`Tier.T0`), the type alias carries the value union (`0 | 1 | 2 | 3`).
17
+ *
18
+ * - **T0 — essential**: transaction commit, block production, threshold-sig contribution.
19
+ * - **T1 — correctness-supporting**: chain serving, replay-window storage, partition heal.
20
+ * - **T2 — functional**: matchmaking/voting directories, capability discovery, capacity gossip.
21
+ * - **T3 — luxury**: reactivity push forwarding, anticipatory warm-up, optional delta payloads.
22
+ */
23
+ export const Tier = { T0: 0, T1: 1, T2: 2, T3: 3 } as const;
24
+ export type Tier = (typeof Tier)[keyof typeof Tier];
25
+
26
+ /** All four tiers, ascending — useful for iteration and override validation. */
27
+ export const ALL_TIERS: readonly Tier[] = [Tier.T0, Tier.T1, Tier.T2, Tier.T3];
28
+
29
+ /**
30
+ * A node's static tier classification. `willingTiers` is the set of tiers this node will take on
31
+ * *forwarder* roles for; cohort assembly under FRET is tier-blind, so this is the per-node gate the
32
+ * willingness check consults. Edge: `{T0, T1}`; Core: `{T0, T1, T2, T3}` (operator-narrowable).
33
+ */
34
+ export interface NodeProfile {
35
+ readonly kind: "edge" | "core";
36
+ readonly willingTiers: ReadonlySet<Tier>;
37
+ }
38
+
39
+ /** Per-node operator override surface. Currently the willing-tier set; widened by later tickets. */
40
+ export interface NodeProfileOverrides {
41
+ /**
42
+ * Restrict the willing-tier set. Tiers outside the profile's base set are ignored (Edge can never
43
+ * be widened to T2/T3 via override — that is a hardware/policy class, not a knob).
44
+ */
45
+ willingTiers?: Iterable<Tier>;
46
+ }
47
+
48
+ const EDGE_BASE_TIERS: readonly Tier[] = [Tier.T0, Tier.T1];
49
+ const CORE_BASE_TIERS: readonly Tier[] = ALL_TIERS;
50
+
51
+ /** Intersect a requested override set with the profile's base tiers (override can only narrow). */
52
+ function resolveWillingTiers(base: readonly Tier[], overrides?: NodeProfileOverrides): ReadonlySet<Tier> {
53
+ if (!overrides?.willingTiers) {
54
+ return new Set(base);
55
+ }
56
+ const requested = new Set(overrides.willingTiers);
57
+ return new Set(base.filter((t) => requested.has(t)));
58
+ }
59
+
60
+ /**
61
+ * Edge profile (mobile / browser / IoT): forwards T0 + T1 only. T2/T3 willingness is permanently
62
+ * off and an operator override cannot turn it on. An override may further narrow (e.g. to `{T0}`).
63
+ */
64
+ export function edgeProfile(overrides?: NodeProfileOverrides): NodeProfile {
65
+ return { kind: "edge", willingTiers: resolveWillingTiers(EDGE_BASE_TIERS, overrides) };
66
+ }
67
+
68
+ /**
69
+ * Core profile (servers / fixed infrastructure): forwards T0–T3 by default. An operator override may
70
+ * restrict it to a subset (e.g. `{T0, T1, T2}` to shed reactivity-push duty).
71
+ */
72
+ export function coreProfile(overrides?: NodeProfileOverrides): NodeProfile {
73
+ return { kind: "core", willingTiers: resolveWillingTiers(CORE_BASE_TIERS, overrides) };
74
+ }