@serfab/cadre-host 0.12.0 → 1.0.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.
- package/README.md +6 -4
- package/dist/bin/host.js +9 -1
- package/dist/bin/host.js.map +1 -1
- package/dist/donation/__tests__/fake-orchestrator.js +1 -1
- package/dist/donation/__tests__/fake-orchestrator.js.map +1 -1
- package/dist/donation/donation-service.d.ts +7 -2
- package/dist/donation/donation-service.js +16 -2
- package/dist/donation/donation-service.js.map +1 -1
- package/dist/donation/donation-supervisor.d.ts +12 -2
- package/dist/donation/donation-supervisor.js +25 -2
- package/dist/donation/donation-supervisor.js.map +1 -1
- package/dist/donation/types.d.ts +2 -1
- package/dist/donation/types.js.map +1 -1
- package/dist/installer/index.d.ts +9 -1
- package/dist/installer/index.js +5 -1
- package/dist/installer/index.js.map +1 -1
- package/dist/orchestrator/host-process-orchestrator.d.ts +71 -11
- package/dist/orchestrator/host-process-orchestrator.js +173 -75
- package/dist/orchestrator/host-process-orchestrator.js.map +1 -1
- package/dist/orchestrator/port-allocator.d.ts +44 -2
- package/dist/orchestrator/port-allocator.js +63 -5
- package/dist/orchestrator/port-allocator.js.map +1 -1
- package/dist/orchestrator/port-probe.d.ts +15 -0
- package/dist/orchestrator/port-probe.js +22 -0
- package/dist/orchestrator/port-probe.js.map +1 -0
- package/dist/orchestrator/types.d.ts +13 -1
- package/dist/orchestrator/types.js.map +1 -1
- package/dist/server/routes/bootstrap-node-validation.d.ts +115 -0
- package/dist/server/routes/bootstrap-node-validation.js +197 -0
- package/dist/server/routes/bootstrap-node-validation.js.map +1 -0
- package/dist/server/routes/grants.js +8 -25
- package/dist/server/routes/grants.js.map +1 -1
- package/dist/server/routes/provision-request-validation.d.ts +45 -0
- package/dist/server/routes/provision-request-validation.js +97 -0
- package/dist/server/routes/provision-request-validation.js.map +1 -0
- package/dist/server/server.d.ts +4 -1
- package/dist/server/server.js +36 -2
- package/dist/server/server.js.map +1 -1
- package/dist/ui/assets/{index-C-iks41M.js → index-CEoNRwM_.js} +3 -3
- package/dist/ui/assets/{index-C-iks41M.js.map → index-CEoNRwM_.js.map} +1 -1
- package/dist/ui/index.html +1 -1
- package/package.json +5 -4
- package/src/__tests__/orchestrator-env-scrub.test.ts +22 -0
- package/src/__tests__/orchestrator-node-identity.test.ts +4 -4
- package/src/__tests__/orchestrator-owner.test.ts +45 -2
- package/src/__tests__/orchestrator-ports.test.ts +172 -0
- package/src/__tests__/orchestrator.test.ts +1062 -951
- package/src/bin/host.ts +9 -1
- package/src/donation/__tests__/donation-service.test.ts +94 -7
- package/src/donation/__tests__/donation-supervisor.test.ts +50 -12
- package/src/donation/__tests__/fake-orchestrator.test.ts +168 -168
- package/src/donation/__tests__/fake-orchestrator.ts +1 -1
- package/src/donation/donation-service.ts +22 -3
- package/src/donation/donation-supervisor.ts +25 -2
- package/src/donation/types.ts +2 -1
- package/src/installer/__tests__/installer.smoke.test.ts +263 -218
- package/src/installer/index.ts +327 -314
- package/src/orchestrator/host-process-orchestrator.ts +1250 -1138
- package/src/orchestrator/port-allocator.ts +148 -91
- package/src/orchestrator/port-probe.ts +31 -0
- package/src/orchestrator/types.ts +13 -1
- package/src/server/__tests__/bootstrap-node-validation.test.ts +138 -0
- package/src/server/__tests__/grants-route.test.ts +394 -263
- package/src/server/__tests__/nodes-route.test.ts +2 -2
- package/src/server/__tests__/publishers.test.ts +1 -1
- package/src/server/__tests__/status-route.test.ts +1 -1
- package/src/server/routes/bootstrap-node-validation.ts +206 -0
- package/src/server/routes/grants.ts +9 -26
- package/src/server/routes/provision-request-validation.ts +105 -0
- package/src/server/server.ts +157 -122
|
@@ -1,91 +1,148 @@
|
|
|
1
|
-
import type { NodePorts } from './types.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Range-based port tracker. Allocations are in-memory; on orchestrator
|
|
5
|
-
* restart the caller rehydrates the used-set from persisted state via
|
|
6
|
-
* `markUsed(port)` before serving new allocations.
|
|
7
|
-
*/
|
|
8
|
-
export class PortAllocator {
|
|
9
|
-
private readonly usedPorts = new Set<number>();
|
|
10
|
-
|
|
11
|
-
constructor(
|
|
12
|
-
private readonly start: number,
|
|
13
|
-
private readonly end: number,
|
|
14
|
-
) {
|
|
15
|
-
if (!Number.isInteger(start) || !Number.isInteger(end) || start <= 0 || end < start) {
|
|
16
|
-
throw new Error(`Invalid port range: ${start}..${end}`);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
allocate(): number {
|
|
21
|
-
for (let port = this.start; port <= this.end; port++) {
|
|
22
|
-
if (!this.usedPorts.has(port)) {
|
|
23
|
-
this.usedPorts.add(port);
|
|
24
|
-
return port;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
throw new Error(`No available ports in range ${this.start}..${this.end}`);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
release(port: number): void {
|
|
31
|
-
this.usedPorts.delete(port);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
*
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* the
|
|
64
|
-
*
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
for (const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
1
|
+
import type { NodePorts } from './types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Range-based port tracker. Allocations are in-memory; on orchestrator
|
|
5
|
+
* restart the caller rehydrates the used-set from persisted state via
|
|
6
|
+
* `markUsed(port)` before serving new allocations.
|
|
7
|
+
*/
|
|
8
|
+
export class PortAllocator {
|
|
9
|
+
private readonly usedPorts = new Set<number>();
|
|
10
|
+
|
|
11
|
+
constructor(
|
|
12
|
+
private readonly start: number,
|
|
13
|
+
private readonly end: number,
|
|
14
|
+
) {
|
|
15
|
+
if (!Number.isInteger(start) || !Number.isInteger(end) || start <= 0 || end < start) {
|
|
16
|
+
throw new Error(`Invalid port range: ${start}..${end}`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
allocate(): number {
|
|
21
|
+
for (let port = this.start; port <= this.end; port++) {
|
|
22
|
+
if (!this.usedPorts.has(port)) {
|
|
23
|
+
this.usedPorts.add(port);
|
|
24
|
+
return port;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
throw new Error(`No available ports in range ${this.start}..${this.end}`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
release(port: number): void {
|
|
31
|
+
this.usedPorts.delete(port);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Used during restart rehydration to reserve ports already in use. A no-op for a
|
|
36
|
+
* port outside the range, and for a non-integer: a handle persisted by an older
|
|
37
|
+
* build lacks the ports added since, and `undefined` must not enter the used-set.
|
|
38
|
+
*/
|
|
39
|
+
markUsed(port: number): void {
|
|
40
|
+
if (!Number.isInteger(port) || port < this.start || port > this.end) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
this.usedPorts.add(port);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
has(port: number): boolean {
|
|
47
|
+
return this.usedPorts.has(port);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Allocation order for a node's port set. Fixed so that swapping a caller onto
|
|
53
|
+
* {@link allocateNodePorts} does not shift the ports an existing deployment
|
|
54
|
+
* already handed out — which is also why a key added later goes on the end
|
|
55
|
+
* (`ws` came after the first four).
|
|
56
|
+
*/
|
|
57
|
+
const NODE_PORT_KEYS = ['health', 'metrics', 'p2p', 'admin', 'ws'] as const;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Allocate a full NodePorts set atomically — a mid-way failure (an exhausted
|
|
61
|
+
* range) releases everything already taken rather than stranding the ports it
|
|
62
|
+
* got before the throw. `overrides` supply a port instead of allocating one
|
|
63
|
+
* (the owner node's p2p port, which is fixed by NAT config; a re-spawn's
|
|
64
|
+
* previous ports, see {@link reusedNodePorts}).
|
|
65
|
+
*
|
|
66
|
+
* Overrides are reserved FIRST, before any allocation, so an override that
|
|
67
|
+
* happens to sit inside the managed range cannot also be handed out to another
|
|
68
|
+
* key. Reserving is `markUsed`, a documented no-op outside the range — which is
|
|
69
|
+
* the production case for the owner node's libp2p port, and why reserving first
|
|
70
|
+
* leaves every real port assignment unchanged.
|
|
71
|
+
*
|
|
72
|
+
* NOTE: an override is trusted, not checked — `markUsed` does not refuse a port
|
|
73
|
+
* another handle already holds, nor two keys naming the same port. Safe for
|
|
74
|
+
* today's callers: a re-spawn's overrides were released by its own handle drop
|
|
75
|
+
* with no `await` in between, and the owner's `libp2pPort` can only collide if an
|
|
76
|
+
* operator configures it inside the range onto a port a node already holds. If a
|
|
77
|
+
* caller ever passes ports it did not just release, refuse an override that
|
|
78
|
+
* `allocator.has()` or that repeats another key's port.
|
|
79
|
+
*/
|
|
80
|
+
export function allocateNodePorts(
|
|
81
|
+
allocator: PortAllocator,
|
|
82
|
+
overrides?: Partial<NodePorts>,
|
|
83
|
+
): NodePorts {
|
|
84
|
+
const reserved: number[] = [];
|
|
85
|
+
const ports: Partial<NodePorts> = {};
|
|
86
|
+
try {
|
|
87
|
+
for (const key of NODE_PORT_KEYS) {
|
|
88
|
+
const override = overrides?.[key];
|
|
89
|
+
if (override === undefined) continue;
|
|
90
|
+
allocator.markUsed(override);
|
|
91
|
+
reserved.push(override);
|
|
92
|
+
ports[key] = override;
|
|
93
|
+
}
|
|
94
|
+
for (const key of NODE_PORT_KEYS) {
|
|
95
|
+
if (ports[key] !== undefined) continue;
|
|
96
|
+
const port = allocator.allocate();
|
|
97
|
+
reserved.push(port);
|
|
98
|
+
ports[key] = port;
|
|
99
|
+
}
|
|
100
|
+
} catch (err) {
|
|
101
|
+
for (const port of reserved) allocator.release(port);
|
|
102
|
+
throw err;
|
|
103
|
+
}
|
|
104
|
+
return ports as NodePorts;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Hold a node's whole port set against `allocator` — the inverse of
|
|
109
|
+
* {@link releaseNodePorts}. A key the set lacks is skipped (see `markUsed`).
|
|
110
|
+
*/
|
|
111
|
+
export function reserveNodePorts(allocator: PortAllocator, ports: NodePorts): void {
|
|
112
|
+
for (const key of NODE_PORT_KEYS) allocator.markUsed(ports[key]);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Give a node's whole port set back to `allocator`. A key the set lacks releases
|
|
117
|
+
* nothing: deleting a value the used-set never held is a no-op.
|
|
118
|
+
*/
|
|
119
|
+
export function releaseNodePorts(allocator: PortAllocator, ports: NodePorts): void {
|
|
120
|
+
for (const key of NODE_PORT_KEYS) allocator.release(ports[key]);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The ports a re-spawn comes back on: the set the dropped handle held, as
|
|
125
|
+
* {@link allocateNodePorts} overrides. `{}` when nothing was dropped (a first
|
|
126
|
+
* spawn, or a handle lost along with `state.json`), so every port is allocated
|
|
127
|
+
* fresh.
|
|
128
|
+
*
|
|
129
|
+
* Why a re-spawn must not simply take the lowest free ports: a requester with no
|
|
130
|
+
* address of its own (a phone) reaches a lent node only by dialing the address it
|
|
131
|
+
* was given, so a node that comes back on a different port is one that requester
|
|
132
|
+
* can no longer find.
|
|
133
|
+
*
|
|
134
|
+
* A key the dropped handle lacks — `ws` on a handle persisted by an older build —
|
|
135
|
+
* is left out, and so allocated fresh. When more than one handle was dropped (only
|
|
136
|
+
* possible from a `state.json` that already held duplicates), the last one stored
|
|
137
|
+
* wins and the others' ports stay released.
|
|
138
|
+
*/
|
|
139
|
+
export function reusedNodePorts(dropped: readonly { ports: NodePorts }[]): Partial<NodePorts> {
|
|
140
|
+
const reused: Partial<NodePorts> = {};
|
|
141
|
+
const previous = dropped[dropped.length - 1]?.ports;
|
|
142
|
+
if (!previous) return reused;
|
|
143
|
+
for (const key of NODE_PORT_KEYS) {
|
|
144
|
+
const port = previous[key];
|
|
145
|
+
if (Number.isInteger(port)) reused[key] = port;
|
|
146
|
+
}
|
|
147
|
+
return reused;
|
|
148
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createServer } from 'node:net';
|
|
2
|
+
|
|
3
|
+
/** One TCP port a child binds, on the address it binds it to. */
|
|
4
|
+
export interface PortBinding {
|
|
5
|
+
port: number;
|
|
6
|
+
host: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Resolve when `binding` can be bound right now; reject, naming the port and the
|
|
11
|
+
* OS error code, when it cannot. Binds briefly and closes again at once.
|
|
12
|
+
*
|
|
13
|
+
* Any bind error counts as "unavailable", not just `EADDRINUSE`: on Windows a
|
|
14
|
+
* port held with `SO_EXCLUSIVEADDRUSE` (which libuv sets on every TCP bind) can
|
|
15
|
+
* answer `EACCES` instead, and a child launched onto a port that fails either way
|
|
16
|
+
* would die on it.
|
|
17
|
+
*/
|
|
18
|
+
export function assertPortFree(binding: PortBinding): Promise<void> {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
const server = createServer();
|
|
21
|
+
server.once('error', (err: NodeJS.ErrnoException) => {
|
|
22
|
+
reject(new Error(
|
|
23
|
+
`port ${binding.port} on ${binding.host} is unavailable (${err.code ?? err.message})`,
|
|
24
|
+
{ cause: err },
|
|
25
|
+
));
|
|
26
|
+
});
|
|
27
|
+
server.listen({ port: binding.port, host: binding.host }, () => {
|
|
28
|
+
server.close(() => resolve());
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
}
|
|
@@ -42,13 +42,25 @@ export interface HostProcessConfig {
|
|
|
42
42
|
*/
|
|
43
43
|
export type PushCredentialsResolver = () => Promise<PushCredentials | undefined>;
|
|
44
44
|
|
|
45
|
-
/**
|
|
45
|
+
/**
|
|
46
|
+
* Per-child allocated ports. `admin` carries the loopback admin channel (6.6).
|
|
47
|
+
*
|
|
48
|
+
* A handle read back from a `state.json` written by an older build lacks every key
|
|
49
|
+
* added since, whatever this type says — the node-set helpers in `port-allocator.ts`
|
|
50
|
+
* skip a missing key rather than reserving `undefined`.
|
|
51
|
+
*/
|
|
46
52
|
export interface NodePorts {
|
|
47
53
|
health: number;
|
|
48
54
|
metrics: number;
|
|
55
|
+
/** libp2p TCP listener. */
|
|
49
56
|
p2p: number;
|
|
50
57
|
/** Loopback admin-channel port. Bound only by the owner node. */
|
|
51
58
|
admin: number;
|
|
59
|
+
/**
|
|
60
|
+
* libp2p WebSocket listener. The one a phone dials: a phone's node has no TCP
|
|
61
|
+
* transport, so the `p2p` port is unreachable to it.
|
|
62
|
+
*/
|
|
63
|
+
ws: number;
|
|
52
64
|
}
|
|
53
65
|
|
|
54
66
|
/** In-memory record per managed child. */
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The host's `bootstrapNodes` rule — the requester addresses a donated node is
|
|
3
|
+
* started with — exercised directly.
|
|
4
|
+
*
|
|
5
|
+
* This is one half of a **manual tripwire**: cadre-provider carries an identical
|
|
6
|
+
* copy of this rule's per-entry half
|
|
7
|
+
* (`packages/cadre-provider/src/server/bootstrap-node-validation.ts`) and the
|
|
8
|
+
* per-entry rows below are the same rows
|
|
9
|
+
* `packages/cadre-provider/src/server/__tests__/bootstrap-node-validation.test.ts`
|
|
10
|
+
* pins that copy to. The empty-list row differs on purpose: this host accepts an
|
|
11
|
+
* empty list (a phone requester dials its lent node instead), the provider still
|
|
12
|
+
* requires one. Neither suite can observe the other package — nothing here
|
|
13
|
+
* imports cadre-provider's server module — so this does not detect a change made
|
|
14
|
+
* over there; what it does is fail if *this* copy is changed, which is how an
|
|
15
|
+
* editor is landed on the comment pointing at the other one.
|
|
16
|
+
*
|
|
17
|
+
* The route contract (`POST /grants` → 400, nothing provisioned) lives in
|
|
18
|
+
* `grants-route.test.ts`, next to the rest of that route's HTTP behaviour.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { describe, it, expect } from 'vitest';
|
|
22
|
+
|
|
23
|
+
import { validateBootstrapNodes } from '../routes/bootstrap-node-validation.js';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A real, decodable peer id. Placeholder strings like `12D3KooReq` look right and
|
|
27
|
+
* are not: `peerIdFromString` throws `Incorrect length` on them, which is exactly
|
|
28
|
+
* the typo this rule exists to catch.
|
|
29
|
+
*/
|
|
30
|
+
const PEER = '12D3KooWA9hbnKrRnPRSPTRkzXqTHzGE8YpJ3JHZmQ5tGwLRTMmp';
|
|
31
|
+
const GOOD = `/ip4/127.0.0.1/tcp/4001/p2p/${PEER}`;
|
|
32
|
+
/** A second, distinct dialable address, for "which entry was bad" assertions. */
|
|
33
|
+
const GOOD_2 = `/dns4/bootstrap.example/tcp/443/wss/p2p/${PEER}`;
|
|
34
|
+
|
|
35
|
+
describe('validateBootstrapNodes (the host copy of the address rule)', () => {
|
|
36
|
+
function errorOf(value: unknown): string {
|
|
37
|
+
const result = validateBootstrapNodes(value);
|
|
38
|
+
if (!('error' in result)) throw new Error(`expected a rejection, got ${JSON.stringify(result)}`);
|
|
39
|
+
return result.error;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
it('accepts a dialable address and returns it trimmed', () => {
|
|
43
|
+
expect(validateBootstrapNodes([` ${GOOD}\n`])).toEqual({ nodes: [GOOD] });
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('accepts several dialable addresses across transports', () => {
|
|
47
|
+
expect(validateBootstrapNodes([GOOD, GOOD_2])).toEqual({ nodes: [GOOD, GOOD_2] });
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('accepts a relayed address whose relay and target peer ids both decode', () => {
|
|
51
|
+
const relayed = `/ip4/1.2.3.4/tcp/4001/p2p/${PEER}/p2p-circuit/p2p/${PEER}`;
|
|
52
|
+
expect(validateBootstrapNodes([relayed])).toEqual({ nodes: [relayed] });
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// The one row that differs from cadre-provider's table: a requester with no address
|
|
56
|
+
// of its own (a phone) sends none and dials the lent node itself.
|
|
57
|
+
it('treats an absent field and an empty list alike: no addresses, nothing rejected', () => {
|
|
58
|
+
expect(validateBootstrapNodes(undefined)).toEqual({ nodes: [] });
|
|
59
|
+
expect(validateBootstrapNodes([])).toEqual({ nodes: [] });
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('rejects a non-array, and an array with a non-string element', () => {
|
|
63
|
+
expect(errorOf(GOOD)).toBe('bootstrapNodes must be an array of strings');
|
|
64
|
+
expect(errorOf(null)).toBe('bootstrapNodes must be an array of strings');
|
|
65
|
+
expect(errorOf([42])).toBe('bootstrapNodes must be an array of strings');
|
|
66
|
+
expect(errorOf([GOOD, { addr: GOOD }])).toBe('bootstrapNodes must be an array of strings');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('rejects an empty or whitespace-only entry with the blank-value message', () => {
|
|
70
|
+
for (const blank of ['', ' ', '\t\n']) {
|
|
71
|
+
expect(errorOf([blank])).toMatch(/must not be empty or whitespace-only/);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Case 1: `@libp2p/bootstrap` maps `multiaddr()` over the list before filtering,
|
|
76
|
+
// so this one throws during libp2p construction and the child never starts.
|
|
77
|
+
it('rejects a string that is not a multiaddr, naming it', () => {
|
|
78
|
+
for (const bad of ['not-an-address', '42', 'bootstrap.example:4001']) {
|
|
79
|
+
const message = errorOf([bad]);
|
|
80
|
+
expect(message).toMatch(/must be multiaddrs/);
|
|
81
|
+
expect(message).toContain(bad);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// Case 2: the quiet one — parses, then gets filtered out inside the child and
|
|
86
|
+
// the node comes up healthy-looking with zero bootstrap peers.
|
|
87
|
+
it('rejects a valid multiaddr with no /p2p/ component, naming it', () => {
|
|
88
|
+
const message = errorOf(['/ip4/127.0.0.1/tcp/4001']);
|
|
89
|
+
expect(message).toMatch(/must include a \/p2p\/<peerId> component/);
|
|
90
|
+
expect(message).toContain('/ip4/127.0.0.1/tcp/4001');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Case 3: `multiaddr()` does not validate the /p2p/ value; `peerIdFromString`
|
|
94
|
+
// does, and throws inside the child's libp2p construction.
|
|
95
|
+
it('rejects a /p2p/ component whose peer id does not decode, naming the entry and the id', () => {
|
|
96
|
+
const bad = '/ip4/127.0.0.1/tcp/4001/p2p/12D3KooReq';
|
|
97
|
+
const message = errorOf([bad]);
|
|
98
|
+
expect(message).toMatch(/must carry a decodable peer id/);
|
|
99
|
+
expect(message).toContain(bad);
|
|
100
|
+
expect(message).toContain('12D3KooReq');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('rejects a relayed address whose relay peer id does not decode', () => {
|
|
104
|
+
expect(errorOf([`/ip4/1.2.3.4/tcp/4001/p2p/12D3KooReq/p2p-circuit/p2p/${PEER}`]))
|
|
105
|
+
.toMatch(/must carry a decodable peer id/);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// Case 4: parses, names a decodable peer, and survives `@libp2p/bootstrap`'s
|
|
109
|
+
// filter — but names no place, so libp2p finds no transport and the dial fails
|
|
110
|
+
// with no valid addresses. Same visible outcome as case 2.
|
|
111
|
+
it('rejects an address that names only peer ids, with no location to dial', () => {
|
|
112
|
+
for (const bad of [`/p2p/${PEER}`, `/p2p/${PEER}/p2p-circuit/p2p/${PEER}`]) {
|
|
113
|
+
const message = errorOf([bad]);
|
|
114
|
+
expect(message).toMatch(/must name where to reach the peer/);
|
|
115
|
+
expect(message).toContain(bad);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// A partial address is deliberately still accepted: which transports the child
|
|
120
|
+
// can dial is the embedder's choice, so only the total absence of a location is
|
|
121
|
+
// a boundary error.
|
|
122
|
+
it('accepts an address with a location but no transport, leaving that to the child', () => {
|
|
123
|
+
const partial = `/ip4/1.2.3.4/p2p/${PEER}`;
|
|
124
|
+
expect(validateBootstrapNodes([partial])).toEqual({ nodes: [partial] });
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('names the offending entry when only one address in a batch is bad', () => {
|
|
128
|
+
const message = errorOf([GOOD, '/ip4/10.0.0.9/tcp/4001', GOOD_2]);
|
|
129
|
+
expect(message).toContain('/ip4/10.0.0.9/tcp/4001');
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// A requester must not be able to turn one junk string into a megabyte of log line.
|
|
133
|
+
it('caps the echoed value for an over-long rejected address', () => {
|
|
134
|
+
const message = errorOf([`/ip4/1.2.3.4/tcp/${'9'.repeat(5000)}`]);
|
|
135
|
+
expect(message).toContain('chars)');
|
|
136
|
+
expect(message.length).toBeLessThan(400);
|
|
137
|
+
});
|
|
138
|
+
});
|