@ocap/chain-node 1.32.1 → 1.32.3

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/esm/types.d.mts CHANGED
@@ -1,9 +1,11 @@
1
1
  import { FutureTimestampGuardConfig, TChainConfigCheckMode } from "./standalone-env.mjs";
2
+ import { DidTlsAllowedIssuerPinsInput, DidTlsWireAlgorithm } from "./did-tls.mjs";
2
3
  import { CHAIN_NODE_TEST_HOOKS_TOKEN } from "./test-hooks-token.mjs";
3
4
  import { AnchorPublisher, ICheckpointSignerWallet, IHotStuffRuntimeValidatorMember, ILedgerHealth, INodeRoleConfig, ISegmentArchiveStore, IWriterLeaseConfig, Ledger, LedgerConfig } from "@arcblock/ledger-core";
4
5
  import { SqliteLedgerStorage } from "@arcblock/ledger-sqlite";
5
- import SqliteIndexDB from "@ocap/indexdb-sqlite";
6
6
  import OCAPResolver, { FollowerSyncService, HotStuffNodeRuntime, HotStuffNodeRuntimeOptions, IColdSegmentReader, INodeMaintenanceController, NodePreferencesManager, TFollowerUpstreamFetchKind } from "@ocap/resolver";
7
+ import tls from "node:tls";
8
+ import SqliteIndexDB from "@ocap/indexdb-sqlite";
7
9
  import SqliteStateDB from "@ocap/statedb-sqlite";
8
10
  import { SqliteTrustedStorage } from "@ocap/trusted-storage";
9
11
  import express from "express";
@@ -16,6 +18,8 @@ type ILedgerHealthStatus = ILedgerHealth;
16
18
  type TNodeRetentionMode = INodeRoleConfig['retentionMode'];
17
19
  type ChainNodeSegmentPublisher = NonNullable<LedgerConfig['segmentPublisher']> & {
18
20
  getArchiveStore?: () => ISegmentArchiveStore | undefined;
21
+ /** Present on `BackgroundSegmentPublisher`: DA publisher status, `null` outside DA mode (#2016) */
22
+ getDaStatus?: () => Record<string, unknown> | null;
19
23
  };
20
24
  interface TrustedAccountConfig {
21
25
  address: string;
@@ -64,12 +68,25 @@ interface FollowerRoleConfig {
64
68
  clusterPeerUrls?: string[];
65
69
  snapshotBaseUrl?: string | null;
66
70
  snapshotPrefix?: string;
71
+ /**
72
+ * `FOLLOWER_BOOTSTRAP_SOURCE` (#2017): absent or `snapshot` = start from the sanitized snapshot (the default); `da` =
73
+ * start a fresh data dir from the G0 of the DA archive described by `da`, then follow its segments, then the hot tail.
74
+ */
75
+ bootstrapSource?: INodeRoleConfig['bootstrapSource'];
76
+ /** The pinned DA archive (`DA_BASE_URL`, `DA_GENESIS_MANIFEST_HASH`, ...); required with `bootstrapSource: 'da'` */
77
+ da?: INodeRoleConfig['da'];
78
+ /** `DA_BASE_URL` in snapshot mode (#2018): the DA archive a fresh snapshot bootstrap is cross-checked against */
79
+ daCrossCheck?: INodeRoleConfig['daCrossCheck'];
80
+ /** `DA_BASE_URL` set but incomplete in snapshot mode (#2018): enforced only when the node would bootstrap */
81
+ daCrossCheckIncomplete?: INodeRoleConfig['daCrossCheckIncomplete'];
67
82
  trustedProposer?: string | null;
68
83
  trustedProposerPublicKey?: string | null;
69
84
  hotstuffValidators?: IHotStuffRuntimeValidatorMember[];
70
85
  hotstuffQuorumThreshold?: number;
71
86
  /** Whether `127.0.0.1` / RFC1918 private peers are allowed (`false` by default for SSRF safety) */
72
87
  allowPrivatePeers?: boolean;
88
+ /** Optional local `afs-mesh` loopback egress gateway (`http://127.0.0.1:<port>` or `http://[::1]:<port>`, #2025) */
89
+ meshEgressUrl?: string;
73
90
  /** Reject cascaded keyless/unsigned follower upstreams with `UPSTREAM_CASCADED_FORBIDDEN` */
74
91
  disallowKeylessCascadedUpstream?: boolean;
75
92
  allowUnauthenticatedBootstrap?: boolean;
@@ -86,6 +103,11 @@ interface FollowerRoleConfig {
86
103
  signer?: ChainNodeSignerConfig;
87
104
  /** Optional explicit runtime role (`'follower'` default or `'standby'`) */
88
105
  runtimeRole?: 'follower' | 'standby';
106
+ /**
107
+ * First sequence whose orphan ledger entries the initial index build indexes (default: where the chain's leader
108
+ * began indexing them, see `orphanIndexFromSequence`; 1 for chains not listed there).
109
+ */
110
+ orphanIndexFromSequence?: number;
89
111
  }
90
112
  interface HotStuffValidatorRoleConfig {
91
113
  kind: 'hotstuff';
@@ -97,6 +119,8 @@ interface HotStuffValidatorRoleConfig {
97
119
  allowPublicConsensusBind?: boolean;
98
120
  writePolicy?: TNodeWritePolicy;
99
121
  allowPrivatePeers?: boolean;
122
+ /** Optional local `afs-mesh` loopback egress gateway (`http://127.0.0.1:<port>` or `http://[::1]:<port>`, #2025) */
123
+ meshEgressUrl?: string;
100
124
  retentionMode?: TNodeRetentionMode;
101
125
  hotWindowEntries?: number;
102
126
  hotWindowCheckpoints?: number;
@@ -117,11 +141,47 @@ interface ChainAppHookContext {
117
141
  preferencesManager: NodePreferencesManager;
118
142
  maintenanceController: INodeMaintenanceController;
119
143
  }
144
+ interface ChainHttpTlsConfig {
145
+ enabled?: boolean;
146
+ /** TLS wire key/signature algorithm (`'p256'` universal BoringSSL/OpenSSL default with Ed25519 `DIDBindingProof`, or `'ed25519'` on OpenSSL) */
147
+ wireAlgorithm?: DidTlsWireAlgorithm;
148
+ /** Optional Ed25519 DID seed / secretKey for server cert synthesis (defaults to node signer / `CHAIN_SK`) */
149
+ identitySecretKey?: string;
150
+ /** Optional explicit W3C or OCAP server DID (`did:abt:z...` or `z...`) */
151
+ subjectDid?: string;
152
+ /** Optional PEM certificate file override (`lstatSync` non-symlink verified) */
153
+ certFile?: string;
154
+ /** Optional PEM private key file override (`lstatSync` non-symlink verified) */
155
+ keyFile?: string;
156
+ /** Require client mTLS certificate (`true` by default whenever `enabled: true`) */
157
+ requireClientCert?: boolean;
158
+ /** Fail-closed pinned 32-byte Ed25519 issuer public keys (hex, `0x` hex, multibase `z...`, or `<did>=<pk>`) */
159
+ allowedIssuerPublicKeys?: DidTlsAllowedIssuerPinsInput;
160
+ /** Optional allowlist of permitted client subject DIDs */
161
+ allowedClientDids?: readonly string[];
162
+ /** Optional DNS SubjectAltName entries for synthesized server certificate */
163
+ sanDnsNames?: readonly string[];
164
+ /** Optional IPv4 / IPv6 SubjectAltName entries for synthesized server certificate */
165
+ sanIpAddresses?: readonly string[];
166
+ /** Synthesized server certificate validity duration in days (`1..3650`, default `365`) */
167
+ validityDays?: number;
168
+ /** TLS handshake timeout in milliseconds (default `10_000`) */
169
+ handshakeTimeoutMs?: number;
170
+ /** Allowed clock skew in seconds for client cert verification (default `60`) */
171
+ clockSkewSeconds?: number;
172
+ }
173
+ interface ReloadedTlsCredentialsInfo {
174
+ reloaded: boolean;
175
+ subjectDid: string | null;
176
+ spkiSha256Hex: string | null;
177
+ }
120
178
  interface ChainProfileBase {
121
179
  /** Optional explicit nodeId (otherwise loaded/persisted in `<dataDir>/.node-id`) */
122
180
  nodeId?: string;
123
181
  /** Exclusive node data directory (`statedb.sqlite`, `ledger.sqlite`, `indexdb.sqlite`, `trusted.sqlite`, `.node-id`) */
124
182
  dataDir: string;
183
+ /** Optional local `afs-mesh` loopback egress gateway (`http://127.0.0.1:<port>` or `http://[::1]:<port>`, #2025) */
184
+ meshEgressUrl?: string;
125
185
  http: {
126
186
  /** Default `'127.0.0.1'`; pass `'0.0.0.0'` explicitly to listen on all interfaces */
127
187
  host?: string;
@@ -137,6 +197,8 @@ interface ChainProfileBase {
137
197
  onGraphqlError?: (err: Error & {
138
198
  originalError?: Error;
139
199
  }) => void;
200
+ /** Optional zero-CA DID-TLS / mTLS listener configuration (#2025) */
201
+ tls?: ChainHttpTlsConfig;
140
202
  };
141
203
  storage?: {
142
204
  statedbPath?: string;
@@ -165,6 +227,8 @@ interface ChainProfileBase {
165
227
  coldArchiveReader?: IColdSegmentReader | null;
166
228
  anchorPublisher?: AnchorPublisher | null;
167
229
  env?: Record<string, string | undefined>;
230
+ /** Test seam: the `fetch` the env-built segment publisher uses (e.g. a fake R2). Unset in production. */
231
+ fetchImpl?: typeof fetch;
168
232
  };
169
233
  genesisTime?: string;
170
234
  reservedSymbols?: string[];
@@ -296,15 +360,17 @@ interface RunningChainNode {
296
360
  readonly indexdb: SqliteIndexDB;
297
361
  readonly trustedStorage: SqliteTrustedStorage;
298
362
  readonly server: http.Server;
363
+ readonly tlsServer?: tls.Server;
299
364
  readonly followerSync?: FollowerSyncService;
300
365
  readonly hotstuffRuntime?: HotStuffNodeRuntime;
301
366
  readonly preferencesManager: NodePreferencesManager;
302
367
  readonly maintenanceController: INodeMaintenanceController;
303
368
  ledgerHealth(): Promise<ILedgerHealthStatus>;
369
+ reloadTlsCredentials(): Promise<ReloadedTlsCredentialsInfo>;
304
370
  advanceClock?(seconds: number): number;
305
371
  restart(nextProfile?: Partial<ChainProfile>): Promise<RunningChainNode>;
306
372
  stop(): Promise<void>;
307
373
  }
308
374
  type ChainNodeHandle = RunningChainNode;
309
375
  //#endregion
310
- export { ChainAppHookContext, ChainNodeHandle, ChainNodeProfile, ChainNodeRoleConfig, ChainNodeSegmentPublisher, ChainNodeSignerConfig, ChainNodeTestHooks, ChainNodeTestHooksInput, ChainProfile, ChainProfileBase, FollowerRoleConfig, FollowerSyncTestController, FollowerUpstreamInterceptorContext, HotStuffValidatorRoleConfig, ILedgerHealthStatus, ILedgerLeaseOptions, PublicKeyOnlyValidatorSignerConfig, RunningChainNode, StartChainNodeOptions, TNodeRetentionMode, TrustedAccountConfig, ValidatorRoleConfig, ValidatorSignerConfig };
376
+ export { ChainAppHookContext, ChainHttpTlsConfig, ChainNodeHandle, ChainNodeProfile, ChainNodeRoleConfig, ChainNodeSegmentPublisher, ChainNodeSignerConfig, ChainNodeTestHooks, ChainNodeTestHooksInput, ChainProfile, ChainProfileBase, FollowerRoleConfig, FollowerSyncTestController, FollowerUpstreamInterceptorContext, HotStuffValidatorRoleConfig, ILedgerHealthStatus, ILedgerLeaseOptions, PublicKeyOnlyValidatorSignerConfig, ReloadedTlsCredentialsInfo, RunningChainNode, StartChainNodeOptions, TNodeRetentionMode, TrustedAccountConfig, ValidatorRoleConfig, ValidatorSignerConfig };
@@ -37,7 +37,8 @@ const chunkBytes = (chunk, encoding) => {
37
37
  return 0;
38
38
  };
39
39
  /** Express middleware writing one `formatAccessLogLine` per finished response through `logger.info`. */
40
- function createAccessLogMiddleware(logger) {
40
+ function createAccessLogMiddleware(logger, options = {}) {
41
+ const getHeaders = () => options.env !== void 0 ? (0, _ocap_util_lib_client_ip.getTrustedClientIpHeaders)(options.env.CHAIN_CLIENT_IP_HEADERS) : void 0;
41
42
  return (req, res, next) => {
42
43
  const startedAt = Date.now();
43
44
  let bytes = 0;
@@ -52,13 +53,14 @@ function createAccessLogMiddleware(logger) {
52
53
  return origEnd.apply(this, args);
53
54
  };
54
55
  res.once("finish", () => {
56
+ const trustedHeaders = getHeaders();
55
57
  logger.info(formatAccessLogLine({
56
58
  method: req.method,
57
59
  path: req.originalUrl,
58
60
  status: res.statusCode,
59
61
  durationMs: Date.now() - startedAt,
60
62
  bytes,
61
- clientIp: (0, _ocap_util_lib_client_ip.resolveClientIp)(req),
63
+ clientIp: (0, _ocap_util_lib_client_ip.resolveClientIp)(req, trustedHeaders !== void 0 ? { headers: trustedHeaders } : void 0),
62
64
  host: headerString(req.headers.host),
63
65
  cfRay: headerString(req.headers["cf-ray"]),
64
66
  userAgent: headerString(req.headers["user-agent"])
@@ -26,9 +26,12 @@ interface AccessLogFields {
26
26
  userAgent?: string;
27
27
  }
28
28
  declare function formatAccessLogLine(f: AccessLogFields): string;
29
+ interface AccessLogMiddlewareOptions {
30
+ env?: Record<string, string | undefined>;
31
+ }
29
32
  /** Express middleware writing one `formatAccessLogLine` per finished response through `logger.info`. */
30
33
  declare function createAccessLogMiddleware(logger: {
31
34
  info: (msg: string) => void;
32
- }): (req: Request, res: Response, next: NextFunction) => void;
35
+ }, options?: AccessLogMiddlewareOptions): (req: Request, res: Response, next: NextFunction) => void;
33
36
  //#endregion
34
- export { AccessLogFields, createAccessLogMiddleware, formatAccessLogLine };
37
+ export { AccessLogFields, AccessLogMiddlewareOptions, createAccessLogMiddleware, formatAccessLogLine };
@@ -126,6 +126,43 @@ function diffConsensusConfig(local, upstream) {
126
126
  }
127
127
  const MAX_CONFIG_RESPONSE_BYTES = 8 * 1024 * 1024;
128
128
  const GET_CONFIG_QUERY = "{ getConfig { code config } }";
129
+ async function readBoundedResponseText(res, url, maxBytes) {
130
+ const contentLength = res.headers?.get?.("content-length");
131
+ if (contentLength !== null && contentLength !== void 0 && contentLength.trim() !== "") {
132
+ const parsedLen = Number(contentLength);
133
+ if (Number.isFinite(parsedLen) && parsedLen > maxBytes) {
134
+ try {
135
+ await res.body?.cancel();
136
+ } catch {}
137
+ throw new Error(`getConfig from ${url} failed: response larger than ${maxBytes} bytes`);
138
+ }
139
+ }
140
+ if (!res.body || typeof res.body.getReader !== "function") {
141
+ const text$1 = await res.text();
142
+ if (Buffer.byteLength(text$1, "utf8") > maxBytes) throw new Error(`getConfig from ${url} failed: response larger than ${maxBytes} bytes`);
143
+ return text$1;
144
+ }
145
+ const reader = res.body.getReader();
146
+ const decoder = new TextDecoder();
147
+ let totalBytes = 0;
148
+ let text = "";
149
+ for (;;) {
150
+ const { done, value } = await reader.read();
151
+ if (done) break;
152
+ if (value) {
153
+ totalBytes += value.byteLength;
154
+ if (totalBytes > maxBytes) {
155
+ try {
156
+ await reader.cancel();
157
+ } catch {}
158
+ throw new Error(`getConfig from ${url} failed: response larger than ${maxBytes} bytes`);
159
+ }
160
+ text += decoder.decode(value, { stream: true });
161
+ }
162
+ }
163
+ text += decoder.decode();
164
+ return text;
165
+ }
129
166
  /** POST `{ getConfig }` to `<upstreamUrl>/api/gql` and return the parsed in-memory config of the upstream. */
130
167
  async function fetchUpstreamChainConfig(upstreamUrl, { fetchImpl = fetch, timeoutMs = 1e4 } = {}) {
131
168
  const url = `${String(upstreamUrl).trim().replace(/\/+$/, "")}/api/gql`;
@@ -139,9 +176,8 @@ async function fetchUpstreamChainConfig(upstreamUrl, { fetchImpl = fetch, timeou
139
176
  redirect: "error",
140
177
  signal: AbortSignal.timeout(timeoutMs)
141
178
  });
142
- const text = await res.text();
179
+ const text = await readBoundedResponseText(res, url, MAX_CONFIG_RESPONSE_BYTES);
143
180
  if (!res.ok) throw new Error(`getConfig from ${url} failed: HTTP ${res.status}`);
144
- if (text.length > MAX_CONFIG_RESPONSE_BYTES) throw new Error(`getConfig from ${url} failed: response larger than ${MAX_CONFIG_RESPONSE_BYTES} bytes`);
145
181
  const body = JSON.parse(text);
146
182
  if (body.errors?.length) throw new Error(`getConfig from ${url} failed: ${body.errors.map((e) => e.message).join("; ")}`);
147
183
  const raw = body.data?.getConfig?.config;