@serfab/cadre-core 1.10.0 → 1.11.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 (61) hide show
  1. package/dist/cadre-node.d.ts +0 -27
  2. package/dist/cadre-node.js +2 -94
  3. package/dist/cadre-node.js.map +1 -1
  4. package/dist/control-schema.js +1066 -1066
  5. package/dist/diagnostics/connection-path.d.ts +1 -18
  6. package/dist/diagnostics/connection-path.js +1 -20
  7. package/dist/diagnostics/connection-path.js.map +1 -1
  8. package/dist/identity-key.d.ts +0 -30
  9. package/dist/identity-key.js +7 -40
  10. package/dist/identity-key.js.map +1 -1
  11. package/dist/index.d.ts +3 -2
  12. package/dist/index.js +6 -4
  13. package/dist/index.js.map +1 -1
  14. package/dist/relay-stun.d.ts +24 -0
  15. package/dist/relay-stun.js +51 -0
  16. package/dist/relay-stun.js.map +1 -0
  17. package/dist/seed-bootstrap.js +4 -4
  18. package/dist/strand-membership-writer.js +32 -32
  19. package/package.json +2 -2
  20. package/src/cadre-node.ts +8654 -8753
  21. package/src/control-authorization.ts +117 -117
  22. package/src/control-cohort.ts +128 -128
  23. package/src/control-formation-recorder.ts +353 -353
  24. package/src/control-read-retry.ts +216 -216
  25. package/src/control-schema.ts +1078 -1078
  26. package/src/control-write-retry.ts +567 -567
  27. package/src/diagnostics/connection-path.ts +1 -29
  28. package/src/formation-approval.ts +610 -610
  29. package/src/hibernation-manager.ts +638 -638
  30. package/src/identity-key.ts +7 -62
  31. package/src/index.ts +644 -647
  32. package/src/invitation-bootstrap.ts +40 -40
  33. package/src/joined-strand-store.ts +498 -498
  34. package/src/link-budget.ts +684 -684
  35. package/src/membership-connection-gater.ts +570 -570
  36. package/src/node-local-snapshot.ts +298 -298
  37. package/src/peer-dial.ts +315 -315
  38. package/src/peer-join-backfill.ts +710 -710
  39. package/src/pending-join-runner.ts +735 -735
  40. package/src/relay-reservation.ts +1121 -1121
  41. package/src/relay-server.ts +98 -98
  42. package/src/relay-stun.ts +58 -0
  43. package/src/seed-bootstrap.ts +1448 -1448
  44. package/src/strand-addr-protocol.ts +547 -547
  45. package/src/strand-database.ts +287 -287
  46. package/src/strand-first-sync-gate.ts +391 -391
  47. package/src/strand-formation-deadlines.ts +227 -227
  48. package/src/strand-formation-manager.ts +725 -725
  49. package/src/strand-fret-addrs.ts +145 -145
  50. package/src/strand-instance-manager.ts +1789 -1789
  51. package/src/strand-membership-writer.ts +1974 -1974
  52. package/src/strand-network-state-file.ts +40 -40
  53. package/src/strand-network-state.ts +243 -243
  54. package/src/strand-revocation-enforcer.ts +654 -654
  55. package/src/strand-solicitation.ts +532 -532
  56. package/src/strand-wake-protocol.ts +401 -401
  57. package/src/types.ts +2222 -2222
  58. package/dist/diagnostics/webrtc-turn-tracker.d.ts +0 -50
  59. package/dist/diagnostics/webrtc-turn-tracker.js +0 -196
  60. package/dist/diagnostics/webrtc-turn-tracker.js.map +0 -1
  61. package/src/diagnostics/webrtc-turn-tracker.ts +0 -222
@@ -1,145 +1,145 @@
1
- /**
2
- * Reading the signed address records a running strand node's FRET routing table holds,
3
- * as per-peer addresses ready for that node's own address book (`peer-addr-book.ts`).
4
- *
5
- * Why a strand node needs this. libp2p's peerStore hides every address one hour after
6
- * it was first observed (the `NOTE:` on `mergePeerAddrs`), and FRET does not re-apply
7
- * the records it holds: it hands a record to the peerStore when a neighbour's snapshot
8
- * delivers it or a saved table is imported, and the peerStore refuses a record whose
9
- * sequence number it already has. So a strand node that has been up for more than an
10
- * hour would have no address for a peer whose connection then drops, although FRET
11
- * still holds that peer's record. `CadreNode.refreshStrandPeerAddrs` re-merges the
12
- * records on every pass, which is what keeps another party's strand nodes dialable;
13
- * own-party siblings are re-resolved separately, over the strand-addr RPC.
14
- *
15
- * How long a record is available here is FRET's rule, not this module's: while FRET
16
- * holds the entry, and at most 14 days after the record was last confirmed (a peer
17
- * connected at export time counts as confirmed then).
18
- *
19
- * Every record is opened and verified here rather than taken on FRET's word. A record
20
- * is self-certifying, the table is reached through a structural type rather than an
21
- * interface this package depends on, and the address book write is attributed from what
22
- * the record proves, not from the id the table filed it under.
23
- */
24
-
25
- import debug from 'debug';
26
- import { PeerRecord, RecordEnvelope } from '@libp2p/peer-record';
27
- import { peerIdFromPublicKey } from '@libp2p/peer-id';
28
- import { multiaddr, type Multiaddr } from '@multiformats/multiaddr';
29
- import { fromString as uint8ArrayFromString } from 'uint8arrays';
30
- import { withTrailingPeerId } from './peer-record.js';
31
-
32
- const log = debug('sereus:cadre:strand-fret-addrs');
33
-
34
- /** The slice of FRET's exported routing table read here (`SerializedTable` in `p2p-fret`). */
35
- interface FretTableSlice {
36
- entries: ReadonlyArray<{
37
- id: string;
38
- /** `envelope` is the base64url of a marshaled libp2p record envelope. */
39
- addressRecord?: { envelope: string };
40
- }>;
41
- }
42
-
43
- /** The slice of FRET's libp2p service called here. */
44
- interface FretTableSource {
45
- exportTable(): FretTableSlice;
46
- }
47
-
48
- /**
49
- * The slice of a strand's libp2p node this module touches. Structurally satisfied by
50
- * `Libp2p`; db-p2p registers FRET as `services.fret`.
51
- */
52
- export interface FretAddrHost {
53
- peerId: { toString(): string };
54
- services?: Record<string, unknown>;
55
- }
56
-
57
- /** What one read of a strand node's FRET table yields. */
58
- export interface FretPeerAddrs {
59
- /** Peer id → the addresses its record lists, each bound to that peer. Table order. */
60
- peers: Map<string, Multiaddr[]>;
61
- /**
62
- * Records left out because they did not decode, did not verify, or were not signed
63
- * by and about the peer the table filed them under.
64
- */
65
- rejected: number;
66
- }
67
-
68
- /**
69
- * The addresses of every peer `host`'s FRET table holds a usable signed record for,
70
- * `host` itself excluded. A node with no FRET service (a db-p2p build without it, a
71
- * test double) reads as an empty table. One bad record never costs the others: it is
72
- * counted in `rejected` and skipped.
73
- *
74
- * Throws only what `exportTable()` throws, which a caller treats like any other
75
- * failure of a pass over a node that may be mid-teardown.
76
- */
77
- export async function strandFretPeerAddrs(host: FretAddrHost): Promise<FretPeerAddrs> {
78
- const result: FretPeerAddrs = { peers: new Map(), rejected: 0 };
79
- const fret = host.services?.fret;
80
- if (!isFretTableSource(fret)) {
81
- return result;
82
- }
83
- const selfId = host.peerId.toString();
84
- for (const entry of fret.exportTable().entries) {
85
- if (entry.id === selfId || entry.addressRecord === undefined) {
86
- continue;
87
- }
88
- const addrs = await verifiedRecordAddrs(entry.id, entry.addressRecord.envelope);
89
- if (addrs === null) {
90
- result.rejected++;
91
- } else if (addrs.length > 0) {
92
- result.peers.set(entry.id, addrs);
93
- }
94
- }
95
- return result;
96
- }
97
-
98
- function isFretTableSource(service: unknown): service is FretTableSource {
99
- return typeof service === 'object' && service !== null
100
- && typeof (service as { exportTable?: unknown }).exportTable === 'function';
101
- }
102
-
103
- /**
104
- * The addresses one record lists, bound to `peerId`; null when the record must not be
105
- * used. `openAndCertify` proves only that the envelope's own key signed it, so the
106
- * signer and the peer the payload describes are each held against `peerId` here:
107
- * without that, any peer could sign a record rewriting another peer's addresses.
108
- */
109
- async function verifiedRecordAddrs(peerId: string, envelope: string): Promise<Multiaddr[] | null> {
110
- try {
111
- const opened = await RecordEnvelope.openAndCertify(uint8ArrayFromString(envelope, 'base64url'), PeerRecord.DOMAIN);
112
- const record = PeerRecord.createFromProtobuf(opened.payload);
113
- const signer = peerIdFromPublicKey(opened.publicKey).toString();
114
- const subject = record.peerId.toString();
115
- if (signer !== peerId || subject !== peerId) {
116
- log('skipping the address record filed under %s: signed by %s about %s', peerId, signer, subject);
117
- return null;
118
- }
119
- return bindToPeer(record.multiaddrs.map((addr) => addr.toString()), peerId);
120
- } catch (error) {
121
- log('skipping the address record filed under %s: %o', peerId, error);
122
- return null;
123
- }
124
- }
125
-
126
- /**
127
- * A record names its peer once, in `peerId`, so its addresses carry no trailing
128
- * `/p2p/<peerId>` and a relay-only peer's circuit address ends at the relay hop
129
- * (`…/p2p/<relay>/p2p-circuit`). Each is given the suffix that makes it a complete
130
- * dial target; one already terminating in a different peer id does not reach this peer
131
- * and is dropped.
132
- *
133
- * Takes strings because `@libp2p/peer-record` carries its own `@multiformats/multiaddr`
134
- * copy, a structurally different type from the one `withTrailingPeerId` takes.
135
- */
136
- function bindToPeer(addrs: string[], peerId: string): Multiaddr[] {
137
- const bound: Multiaddr[] = [];
138
- for (const addr of addrs) {
139
- const withPeer = withTrailingPeerId(multiaddr(addr), peerId);
140
- if (withPeer !== null) {
141
- bound.push(withPeer);
142
- }
143
- }
144
- return bound;
145
- }
1
+ /**
2
+ * Reading the signed address records a running strand node's FRET routing table holds,
3
+ * as per-peer addresses ready for that node's own address book (`peer-addr-book.ts`).
4
+ *
5
+ * Why a strand node needs this. libp2p's peerStore hides every address one hour after
6
+ * it was first observed (the `NOTE:` on `mergePeerAddrs`), and FRET does not re-apply
7
+ * the records it holds: it hands a record to the peerStore when a neighbour's snapshot
8
+ * delivers it or a saved table is imported, and the peerStore refuses a record whose
9
+ * sequence number it already has. So a strand node that has been up for more than an
10
+ * hour would have no address for a peer whose connection then drops, although FRET
11
+ * still holds that peer's record. `CadreNode.refreshStrandPeerAddrs` re-merges the
12
+ * records on every pass, which is what keeps another party's strand nodes dialable;
13
+ * own-party siblings are re-resolved separately, over the strand-addr RPC.
14
+ *
15
+ * How long a record is available here is FRET's rule, not this module's: while FRET
16
+ * holds the entry, and at most 14 days after the record was last confirmed (a peer
17
+ * connected at export time counts as confirmed then).
18
+ *
19
+ * Every record is opened and verified here rather than taken on FRET's word. A record
20
+ * is self-certifying, the table is reached through a structural type rather than an
21
+ * interface this package depends on, and the address book write is attributed from what
22
+ * the record proves, not from the id the table filed it under.
23
+ */
24
+
25
+ import debug from 'debug';
26
+ import { PeerRecord, RecordEnvelope } from '@libp2p/peer-record';
27
+ import { peerIdFromPublicKey } from '@libp2p/peer-id';
28
+ import { multiaddr, type Multiaddr } from '@multiformats/multiaddr';
29
+ import { fromString as uint8ArrayFromString } from 'uint8arrays';
30
+ import { withTrailingPeerId } from './peer-record.js';
31
+
32
+ const log = debug('sereus:cadre:strand-fret-addrs');
33
+
34
+ /** The slice of FRET's exported routing table read here (`SerializedTable` in `p2p-fret`). */
35
+ interface FretTableSlice {
36
+ entries: ReadonlyArray<{
37
+ id: string;
38
+ /** `envelope` is the base64url of a marshaled libp2p record envelope. */
39
+ addressRecord?: { envelope: string };
40
+ }>;
41
+ }
42
+
43
+ /** The slice of FRET's libp2p service called here. */
44
+ interface FretTableSource {
45
+ exportTable(): FretTableSlice;
46
+ }
47
+
48
+ /**
49
+ * The slice of a strand's libp2p node this module touches. Structurally satisfied by
50
+ * `Libp2p`; db-p2p registers FRET as `services.fret`.
51
+ */
52
+ export interface FretAddrHost {
53
+ peerId: { toString(): string };
54
+ services?: Record<string, unknown>;
55
+ }
56
+
57
+ /** What one read of a strand node's FRET table yields. */
58
+ export interface FretPeerAddrs {
59
+ /** Peer id → the addresses its record lists, each bound to that peer. Table order. */
60
+ peers: Map<string, Multiaddr[]>;
61
+ /**
62
+ * Records left out because they did not decode, did not verify, or were not signed
63
+ * by and about the peer the table filed them under.
64
+ */
65
+ rejected: number;
66
+ }
67
+
68
+ /**
69
+ * The addresses of every peer `host`'s FRET table holds a usable signed record for,
70
+ * `host` itself excluded. A node with no FRET service (a db-p2p build without it, a
71
+ * test double) reads as an empty table. One bad record never costs the others: it is
72
+ * counted in `rejected` and skipped.
73
+ *
74
+ * Throws only what `exportTable()` throws, which a caller treats like any other
75
+ * failure of a pass over a node that may be mid-teardown.
76
+ */
77
+ export async function strandFretPeerAddrs(host: FretAddrHost): Promise<FretPeerAddrs> {
78
+ const result: FretPeerAddrs = { peers: new Map(), rejected: 0 };
79
+ const fret = host.services?.fret;
80
+ if (!isFretTableSource(fret)) {
81
+ return result;
82
+ }
83
+ const selfId = host.peerId.toString();
84
+ for (const entry of fret.exportTable().entries) {
85
+ if (entry.id === selfId || entry.addressRecord === undefined) {
86
+ continue;
87
+ }
88
+ const addrs = await verifiedRecordAddrs(entry.id, entry.addressRecord.envelope);
89
+ if (addrs === null) {
90
+ result.rejected++;
91
+ } else if (addrs.length > 0) {
92
+ result.peers.set(entry.id, addrs);
93
+ }
94
+ }
95
+ return result;
96
+ }
97
+
98
+ function isFretTableSource(service: unknown): service is FretTableSource {
99
+ return typeof service === 'object' && service !== null
100
+ && typeof (service as { exportTable?: unknown }).exportTable === 'function';
101
+ }
102
+
103
+ /**
104
+ * The addresses one record lists, bound to `peerId`; null when the record must not be
105
+ * used. `openAndCertify` proves only that the envelope's own key signed it, so the
106
+ * signer and the peer the payload describes are each held against `peerId` here:
107
+ * without that, any peer could sign a record rewriting another peer's addresses.
108
+ */
109
+ async function verifiedRecordAddrs(peerId: string, envelope: string): Promise<Multiaddr[] | null> {
110
+ try {
111
+ const opened = await RecordEnvelope.openAndCertify(uint8ArrayFromString(envelope, 'base64url'), PeerRecord.DOMAIN);
112
+ const record = PeerRecord.createFromProtobuf(opened.payload);
113
+ const signer = peerIdFromPublicKey(opened.publicKey).toString();
114
+ const subject = record.peerId.toString();
115
+ if (signer !== peerId || subject !== peerId) {
116
+ log('skipping the address record filed under %s: signed by %s about %s', peerId, signer, subject);
117
+ return null;
118
+ }
119
+ return bindToPeer(record.multiaddrs.map((addr) => addr.toString()), peerId);
120
+ } catch (error) {
121
+ log('skipping the address record filed under %s: %o', peerId, error);
122
+ return null;
123
+ }
124
+ }
125
+
126
+ /**
127
+ * A record names its peer once, in `peerId`, so its addresses carry no trailing
128
+ * `/p2p/<peerId>` and a relay-only peer's circuit address ends at the relay hop
129
+ * (`…/p2p/<relay>/p2p-circuit`). Each is given the suffix that makes it a complete
130
+ * dial target; one already terminating in a different peer id does not reach this peer
131
+ * and is dropped.
132
+ *
133
+ * Takes strings because `@libp2p/peer-record` carries its own `@multiformats/multiaddr`
134
+ * copy, a structurally different type from the one `withTrailingPeerId` takes.
135
+ */
136
+ function bindToPeer(addrs: string[], peerId: string): Multiaddr[] {
137
+ const bound: Multiaddr[] = [];
138
+ for (const addr of addrs) {
139
+ const withPeer = withTrailingPeerId(multiaddr(addr), peerId);
140
+ if (withPeer !== null) {
141
+ bound.push(withPeer);
142
+ }
143
+ }
144
+ return bound;
145
+ }