@ocap/chain-node 1.32.1 → 1.32.2
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/access-log.d.mts +5 -2
- package/esm/access-log.mjs +5 -3
- package/esm/config-consistency.mjs +38 -2
- package/esm/follower-config.d.mts +3 -2
- package/esm/follower-config.mjs +30 -4
- package/esm/start-chain-node.mjs +56 -10
- package/esm/types.d.mts +5 -0
- package/lib/access-log.cjs +4 -2
- package/lib/access-log.d.cts +5 -2
- package/lib/config-consistency.cjs +38 -2
- package/lib/follower-config.cjs +30 -3
- package/lib/follower-config.d.cts +3 -2
- package/lib/start-chain-node.cjs +54 -8
- package/lib/types.d.cts +5 -0
- package/package.json +16 -16
package/esm/access-log.d.mts
CHANGED
|
@@ -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 };
|
package/esm/access-log.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { resolveClientIp } from "@ocap/util/lib/client-ip";
|
|
1
|
+
import { getTrustedClientIpHeaders, resolveClientIp } from "@ocap/util/lib/client-ip";
|
|
2
2
|
|
|
3
3
|
//#region src/access-log.ts
|
|
4
4
|
const MAX_PATH = 2048;
|
|
@@ -36,7 +36,8 @@ const chunkBytes = (chunk, encoding) => {
|
|
|
36
36
|
return 0;
|
|
37
37
|
};
|
|
38
38
|
/** Express middleware writing one `formatAccessLogLine` per finished response through `logger.info`. */
|
|
39
|
-
function createAccessLogMiddleware(logger) {
|
|
39
|
+
function createAccessLogMiddleware(logger, options = {}) {
|
|
40
|
+
const getHeaders = () => options.env !== void 0 ? getTrustedClientIpHeaders(options.env.CHAIN_CLIENT_IP_HEADERS) : void 0;
|
|
40
41
|
return (req, res, next) => {
|
|
41
42
|
const startedAt = Date.now();
|
|
42
43
|
let bytes = 0;
|
|
@@ -51,13 +52,14 @@ function createAccessLogMiddleware(logger) {
|
|
|
51
52
|
return origEnd.apply(this, args);
|
|
52
53
|
};
|
|
53
54
|
res.once("finish", () => {
|
|
55
|
+
const trustedHeaders = getHeaders();
|
|
54
56
|
logger.info(formatAccessLogLine({
|
|
55
57
|
method: req.method,
|
|
56
58
|
path: req.originalUrl,
|
|
57
59
|
status: res.statusCode,
|
|
58
60
|
durationMs: Date.now() - startedAt,
|
|
59
61
|
bytes,
|
|
60
|
-
clientIp: resolveClientIp(req),
|
|
62
|
+
clientIp: resolveClientIp(req, trustedHeaders !== void 0 ? { headers: trustedHeaders } : void 0),
|
|
61
63
|
host: headerString(req.headers.host),
|
|
62
64
|
cfRay: headerString(req.headers["cf-ray"]),
|
|
63
65
|
userAgent: headerString(req.headers["user-agent"])
|
|
@@ -124,6 +124,43 @@ function diffConsensusConfig(local, upstream) {
|
|
|
124
124
|
}
|
|
125
125
|
const MAX_CONFIG_RESPONSE_BYTES = 8 * 1024 * 1024;
|
|
126
126
|
const GET_CONFIG_QUERY = "{ getConfig { code config } }";
|
|
127
|
+
async function readBoundedResponseText(res, url, maxBytes) {
|
|
128
|
+
const contentLength = res.headers?.get?.("content-length");
|
|
129
|
+
if (contentLength !== null && contentLength !== void 0 && contentLength.trim() !== "") {
|
|
130
|
+
const parsedLen = Number(contentLength);
|
|
131
|
+
if (Number.isFinite(parsedLen) && parsedLen > maxBytes) {
|
|
132
|
+
try {
|
|
133
|
+
await res.body?.cancel();
|
|
134
|
+
} catch {}
|
|
135
|
+
throw new Error(`getConfig from ${url} failed: response larger than ${maxBytes} bytes`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (!res.body || typeof res.body.getReader !== "function") {
|
|
139
|
+
const text$1 = await res.text();
|
|
140
|
+
if (Buffer.byteLength(text$1, "utf8") > maxBytes) throw new Error(`getConfig from ${url} failed: response larger than ${maxBytes} bytes`);
|
|
141
|
+
return text$1;
|
|
142
|
+
}
|
|
143
|
+
const reader = res.body.getReader();
|
|
144
|
+
const decoder = new TextDecoder();
|
|
145
|
+
let totalBytes = 0;
|
|
146
|
+
let text = "";
|
|
147
|
+
for (;;) {
|
|
148
|
+
const { done, value } = await reader.read();
|
|
149
|
+
if (done) break;
|
|
150
|
+
if (value) {
|
|
151
|
+
totalBytes += value.byteLength;
|
|
152
|
+
if (totalBytes > maxBytes) {
|
|
153
|
+
try {
|
|
154
|
+
await reader.cancel();
|
|
155
|
+
} catch {}
|
|
156
|
+
throw new Error(`getConfig from ${url} failed: response larger than ${maxBytes} bytes`);
|
|
157
|
+
}
|
|
158
|
+
text += decoder.decode(value, { stream: true });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
text += decoder.decode();
|
|
162
|
+
return text;
|
|
163
|
+
}
|
|
127
164
|
/** POST `{ getConfig }` to `<upstreamUrl>/api/gql` and return the parsed in-memory config of the upstream. */
|
|
128
165
|
async function fetchUpstreamChainConfig(upstreamUrl, { fetchImpl = fetch, timeoutMs = 1e4 } = {}) {
|
|
129
166
|
const url = `${String(upstreamUrl).trim().replace(/\/+$/, "")}/api/gql`;
|
|
@@ -137,9 +174,8 @@ async function fetchUpstreamChainConfig(upstreamUrl, { fetchImpl = fetch, timeou
|
|
|
137
174
|
redirect: "error",
|
|
138
175
|
signal: AbortSignal.timeout(timeoutMs)
|
|
139
176
|
});
|
|
140
|
-
const text = await res
|
|
177
|
+
const text = await readBoundedResponseText(res, url, MAX_CONFIG_RESPONSE_BYTES);
|
|
141
178
|
if (!res.ok) throw new Error(`getConfig from ${url} failed: HTTP ${res.status}`);
|
|
142
|
-
if (text.length > MAX_CONFIG_RESPONSE_BYTES) throw new Error(`getConfig from ${url} failed: response larger than ${MAX_CONFIG_RESPONSE_BYTES} bytes`);
|
|
143
179
|
const body = JSON.parse(text);
|
|
144
180
|
if (body.errors?.length) throw new Error(`getConfig from ${url} failed: ${body.errors.map((e) => e.message).join("; ")}`);
|
|
145
181
|
const raw = body.data?.getConfig?.config;
|
|
@@ -15,11 +15,12 @@ declare function mergeTransactionOverlay(base: unknown, overlay: unknown): unkno
|
|
|
15
15
|
* variables from env (e.g. CHAIN_TX_BRIDGE_ENABLED): the stored row is not rewritten on a ledger-anchored leader
|
|
16
16
|
* restart, so it still holds the values of the last genesis/config write.
|
|
17
17
|
*/
|
|
18
|
-
declare function rehydrateFollowerChainConfig(storedChain: unknown, profileChainConfig?: Partial<IChainConfig
|
|
18
|
+
declare function rehydrateFollowerChainConfig(storedChain: unknown, profileChainConfig?: Partial<IChainConfig>, extraReservedSymbols?: readonly string[]): IChainConfig;
|
|
19
19
|
/**
|
|
20
20
|
* The config a resolver actually runs with (`OCAPResolver` constructor): validated, with `token.address` set to the
|
|
21
21
|
* genesis token address. This is what the leader's `getConfig` returns and what the follower's replayer reads.
|
|
22
22
|
*/
|
|
23
23
|
declare function toResolverConfig(config: IChainConfig): IChainConfig;
|
|
24
|
+
declare function orphanIndexFromSequence(chainId: string, override?: number): number;
|
|
24
25
|
//#endregion
|
|
25
|
-
export { isCompleteChainConfig, isPlainRecord, mergeTransactionOverlay, rehydrateFollowerChainConfig, toResolverConfig };
|
|
26
|
+
export { isCompleteChainConfig, isPlainRecord, mergeTransactionOverlay, orphanIndexFromSequence, rehydrateFollowerChainConfig, toResolverConfig };
|
package/esm/follower-config.mjs
CHANGED
|
@@ -29,16 +29,24 @@ function mergeTransactionOverlay(base, overlay) {
|
|
|
29
29
|
* variables from env (e.g. CHAIN_TX_BRIDGE_ENABLED): the stored row is not rewritten on a ledger-anchored leader
|
|
30
30
|
* restart, so it still holds the values of the last genesis/config write.
|
|
31
31
|
*/
|
|
32
|
-
function rehydrateFollowerChainConfig(storedChain, profileChainConfig) {
|
|
32
|
+
function rehydrateFollowerChainConfig(storedChain, profileChainConfig, extraReservedSymbols) {
|
|
33
33
|
const storedChainPartial = storedChain;
|
|
34
34
|
const storedChainId = typeof storedChainPartial.chainId === "string" ? storedChainPartial.chainId.trim() : "";
|
|
35
35
|
const baseSymbol = storedChainPartial.token?.symbol || "TBA";
|
|
36
|
-
const reservedSymbols = Array.from(new Set([
|
|
36
|
+
const reservedSymbols = Array.from(new Set([
|
|
37
|
+
baseSymbol,
|
|
38
|
+
...storedChainPartial.reservedSymbols ?? [],
|
|
39
|
+
...profileChainConfig?.reservedSymbols ?? [],
|
|
40
|
+
...extraReservedSymbols ?? []
|
|
41
|
+
].map((s) => String(s || "").trim().toUpperCase()).filter(Boolean)));
|
|
37
42
|
const transactionOverlay = isPlainRecord(profileChainConfig?.transaction) ? profileChainConfig.transaction : void 0;
|
|
43
|
+
const storedTransaction = isPlainRecord(storedChainPartial.transaction) ? { ...storedChainPartial.transaction } : storedChainPartial.transaction;
|
|
44
|
+
if (isPlainRecord(storedTransaction)) delete storedTransaction.maxTxSize;
|
|
45
|
+
const transaction = transactionOverlay ? mergeTransactionOverlay(storedTransaction, transactionOverlay) : storedTransaction;
|
|
38
46
|
return Config.create({
|
|
39
47
|
...storedChain,
|
|
40
48
|
...storedChainId ? { chainId: storedChainId } : {},
|
|
41
|
-
...
|
|
49
|
+
...transaction !== void 0 ? { transaction } : {},
|
|
42
50
|
reservedSymbols
|
|
43
51
|
});
|
|
44
52
|
}
|
|
@@ -52,6 +60,24 @@ function toResolverConfig(config) {
|
|
|
52
60
|
if (tokenItx) validated.token.address = tokenItx.address;
|
|
53
61
|
return validated;
|
|
54
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* First ledger sequence whose orphan entries (finalized, no stateCommitHash, no statedb tx row) the chain's leader
|
|
65
|
+
* indexed. Leaders index the orphans they produce since 1.31.1 (#1387); before that they never did, and a follower
|
|
66
|
+
* index must hold exactly what the leader's holds (#1875). Values read from the leaders' indexdb: the first orphan
|
|
67
|
+
* with an index row, every earlier one without (beta: last unindexed 437972; main: last unindexed 832519). A chain
|
|
68
|
+
* started on 1.31.1+ indexed all of them.
|
|
69
|
+
*/
|
|
70
|
+
const ORPHAN_INDEX_FROM_SEQUENCE = Object.freeze({
|
|
71
|
+
beta: 437983,
|
|
72
|
+
"xenon-2020-01-15": 832598
|
|
73
|
+
});
|
|
74
|
+
function orphanIndexFromSequence(chainId, override) {
|
|
75
|
+
if (override !== void 0) {
|
|
76
|
+
if (!Number.isSafeInteger(override) || override < 1) throw new Error(`orphanIndexFromSequence must be a positive integer, got ${String(override)}`);
|
|
77
|
+
return override;
|
|
78
|
+
}
|
|
79
|
+
return Object.hasOwn(ORPHAN_INDEX_FROM_SEQUENCE, chainId) ? ORPHAN_INDEX_FROM_SEQUENCE[chainId] : 1;
|
|
80
|
+
}
|
|
55
81
|
|
|
56
82
|
//#endregion
|
|
57
|
-
export { isCompleteChainConfig, isPlainRecord, mergeTransactionOverlay, rehydrateFollowerChainConfig, toResolverConfig };
|
|
83
|
+
export { isCompleteChainConfig, isPlainRecord, mergeTransactionOverlay, orphanIndexFromSequence, rehydrateFollowerChainConfig, toResolverConfig };
|
package/esm/start-chain-node.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createAccessLogMiddleware } from "./access-log.mjs";
|
|
2
|
-
import { verifyFollowerConfigAgainstUpstream } from "./config-consistency.mjs";
|
|
3
|
-
import { isCompleteChainConfig, rehydrateFollowerChainConfig } from "./follower-config.mjs";
|
|
2
|
+
import { fetchUpstreamChainConfig, verifyFollowerConfigAgainstUpstream } from "./config-consistency.mjs";
|
|
3
|
+
import { isCompleteChainConfig, orphanIndexFromSequence, rehydrateFollowerChainConfig } from "./follower-config.mjs";
|
|
4
4
|
import { assertValidChainNodeTestHooks } from "./test-hooks-token.mjs";
|
|
5
5
|
import { CustomError } from "@ocap/util/lib/error";
|
|
6
6
|
import { LeaderRouter, Ledger, bytesToHex, canonicalizeMultiCurvePublicKey, createCheckpointSigner, createEnvAnchorPublisher, createEnvColdArchiveReader, createEnvSegmentPublisher, createEnvWitnessRegistry, decodeMultiCurvePublicKey, normalizeDidForCompare, resolveConsensusProposerFromSigner, toHotStuffClusterConfig, verifyDidMatchesMultiCurvePublicKey } from "@arcblock/ledger-core";
|
|
@@ -60,6 +60,22 @@ function assertSafeNonSymlinkDirectory(rawDataDir) {
|
|
|
60
60
|
if (fs.lstatSync(dataDir).isSymbolicLink()) throw new CustomError("SECURITY_VIOLATION", `Refusing symlinked chain node dataDir: ${dataDir}`);
|
|
61
61
|
return dataDir;
|
|
62
62
|
}
|
|
63
|
+
/** Write a small file and fsync it and its directory, so it survives a crash or power loss right after. */
|
|
64
|
+
function writeFileDurably(filePath, content) {
|
|
65
|
+
const fd = fs.openSync(filePath, "w", 384);
|
|
66
|
+
try {
|
|
67
|
+
fs.writeFileSync(fd, content);
|
|
68
|
+
fs.fsyncSync(fd);
|
|
69
|
+
} finally {
|
|
70
|
+
fs.closeSync(fd);
|
|
71
|
+
}
|
|
72
|
+
const dirFd = fs.openSync(path.dirname(filePath), "r");
|
|
73
|
+
try {
|
|
74
|
+
fs.fsyncSync(dirFd);
|
|
75
|
+
} finally {
|
|
76
|
+
fs.closeSync(dirFd);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
63
79
|
function loadOrCreateNodeId(dataDir, explicitNodeId) {
|
|
64
80
|
const nodeIdPath = path.join(dataDir, NODE_ID_FILENAME);
|
|
65
81
|
if (fs.existsSync(nodeIdPath) && fs.lstatSync(nodeIdPath).isSymbolicLink()) throw new CustomError("SECURITY_VIOLATION", `Refusing symlinked .node-id file: ${nodeIdPath}`);
|
|
@@ -179,6 +195,7 @@ async function bootChainNodeOnce(params) {
|
|
|
179
195
|
if ((role.kind === "validator" || role.kind === "hotstuff") && !isCompleteChainConfig(profile.chainConfig)) throw new CustomError("INVALID_CONFIG", `${role.kind} role requires a complete chainConfig`);
|
|
180
196
|
if ((role.kind === "validator" || role.kind === "hotstuff") && !role.signer) throw new CustomError("INVALID_CONFIG", `${role.kind} role requires role.signer`);
|
|
181
197
|
if (role.kind === "hotstuff" && (!Array.isArray(role.validators) || role.validators.length === 0)) throw new CustomError("INVALID_CONFIG", "HotStuff role requires a non-empty validators list");
|
|
198
|
+
if (role.kind === "follower" && role.orphanIndexFromSequence !== void 0 && (!Number.isSafeInteger(role.orphanIndexFromSequence) || role.orphanIndexFromSequence < 1)) throw new CustomError("INVALID_CONFIG", `role.orphanIndexFromSequence must be a positive integer, got ${String(role.orphanIndexFromSequence)}`);
|
|
182
199
|
const logger = testHooks?.logger ?? profile.logger ?? createDefaultLogger("chain-node");
|
|
183
200
|
const variadicLogger = toVariadicLogger(logger);
|
|
184
201
|
const followerExpectedChainId = "expectedChainId" in profile ? profile.expectedChainId : void 0;
|
|
@@ -226,6 +243,7 @@ async function bootChainNodeOnce(params) {
|
|
|
226
243
|
let ledgerCleanupIndex = -1;
|
|
227
244
|
let followerPrep = null;
|
|
228
245
|
const isKeylessFollower = role.kind === "follower" && !profile.storage?.trustedStorageSecretKey?.trim();
|
|
246
|
+
let followerFetchGuard;
|
|
229
247
|
if (role.kind === "follower") {
|
|
230
248
|
if (fs.existsSync(ledgerPath) && fs.statSync(ledgerPath).size > 0) {
|
|
231
249
|
const precheckLedgerStorage = createLedgerStorage();
|
|
@@ -262,6 +280,7 @@ async function bootChainNodeOnce(params) {
|
|
|
262
280
|
init
|
|
263
281
|
}, guardedBootstrapFetch);
|
|
264
282
|
}) : guardedBootstrapFetch;
|
|
283
|
+
followerFetchGuard = bootstrapFetchImpl;
|
|
265
284
|
const explicitExpectedChainId = followerExpectedChainId?.trim() || "";
|
|
266
285
|
const profileChainId = profile.chainConfig?.chainId?.trim() || "";
|
|
267
286
|
if (explicitExpectedChainId && profileChainId && explicitExpectedChainId !== profileChainId) throw new CustomError("SNAPSHOT_CHAIN_ID_MISMATCH", `Follower expectedChainId (${explicitExpectedChainId}) !== profile.chainConfig.chainId (${profileChainId})`);
|
|
@@ -341,7 +360,7 @@ async function bootChainNodeOnce(params) {
|
|
|
341
360
|
const storedChainId = typeof storedChainPartial.chainId === "string" ? storedChainPartial.chainId.trim() : "";
|
|
342
361
|
const overrideChainId = typeof profile.chainConfig?.chainId === "string" ? profile.chainConfig.chainId.trim() : "";
|
|
343
362
|
if (storedChainId && overrideChainId && overrideChainId !== storedChainId) throw new CustomError("SNAPSHOT_CHAIN_ID_MISMATCH", `Follower profile.chainConfig.chainId (${overrideChainId}) !== statedb chain.chainId (${storedChainId})`);
|
|
344
|
-
chainConfig = rehydrateFollowerChainConfig(storedChain, profile.chainConfig);
|
|
363
|
+
chainConfig = rehydrateFollowerChainConfig(storedChain, profile.chainConfig, profile.reservedSymbols);
|
|
345
364
|
} else {
|
|
346
365
|
const baseConfig = profile.chainConfig;
|
|
347
366
|
if (role.kind === "follower") {
|
|
@@ -352,6 +371,7 @@ async function bootChainNodeOnce(params) {
|
|
|
352
371
|
chainConfig = baseConfig;
|
|
353
372
|
}
|
|
354
373
|
if (role.kind === "follower" && followerExpectedChainId && chainConfig.chainId !== followerExpectedChainId) throw new CustomError("SNAPSHOT_CHAIN_ID_MISMATCH", `Rehydrated Follower chainId (${chainConfig.chainId}) !== expectedChainId (${followerExpectedChainId})`);
|
|
374
|
+
const orphanIndexStart = role.kind === "follower" ? orphanIndexFromSequence(chainConfig.chainId, role.orphanIndexFromSequence) : 1;
|
|
355
375
|
const hotstuffValidators = role.kind === "hotstuff" ? role.validators : role.kind === "follower" ? role.hotstuffValidators ?? [] : [];
|
|
356
376
|
const hotstuffQuorumThreshold = role.kind === "hotstuff" ? role.quorumThreshold : role.kind === "follower" ? role.hotstuffQuorumThreshold : void 0;
|
|
357
377
|
const hotstuffClusterConfig = role.kind === "hotstuff" || hotstuffValidators.length > 0 ? toHotStuffClusterConfig(chainConfig.chainId, hotstuffValidators, hotstuffQuorumThreshold) : void 0;
|
|
@@ -520,6 +540,9 @@ async function bootChainNodeOnce(params) {
|
|
|
520
540
|
});
|
|
521
541
|
}
|
|
522
542
|
const indexDbExisted = fs.existsSync(indexdbPath);
|
|
543
|
+
const indexBuildMarkerPath = `${indexdbPath}.build-pending`;
|
|
544
|
+
let followerIndexBuildNeeded = role.kind === "follower" && (Boolean(followerPrep?.bootstrapped) || !indexDbExisted || fs.existsSync(indexBuildMarkerPath));
|
|
545
|
+
if (followerIndexBuildNeeded) writeFileDurably(indexBuildMarkerPath, `${(/* @__PURE__ */ new Date()).toISOString()}\n`);
|
|
523
546
|
const indexdb = new SqliteIndexDB({
|
|
524
547
|
filename: indexdbPath,
|
|
525
548
|
...profile.storage?.indexWalAutoCheckpoint !== void 0 ? { walAutoCheckpoint: profile.storage.indexWalAutoCheckpoint } : {}
|
|
@@ -529,8 +552,16 @@ async function bootChainNodeOnce(params) {
|
|
|
529
552
|
});
|
|
530
553
|
await indexdb.initialize();
|
|
531
554
|
await waitEventEmitterReady(indexdb);
|
|
555
|
+
if (role.kind === "follower" && !followerIndexBuildNeeded) {
|
|
556
|
+
if (await indexdb.tx.count() === 0 && await ledger.getLatestSequence() > 0) {
|
|
557
|
+
logger.warn?.("follower IndexDB has no transactions while the ledger has entries; rebuilding it", { indexdbPath });
|
|
558
|
+
followerIndexBuildNeeded = true;
|
|
559
|
+
writeFileDurably(indexBuildMarkerPath, `${(/* @__PURE__ */ new Date()).toISOString()}\n`);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
532
562
|
let resolverRef = null;
|
|
533
|
-
const
|
|
563
|
+
const explicitAdminEnv = profile.http.adminEnv;
|
|
564
|
+
const adminEnv = explicitAdminEnv ?? {};
|
|
534
565
|
const preferencesManager = new NodePreferencesManager({
|
|
535
566
|
dataDir,
|
|
536
567
|
env: adminEnv,
|
|
@@ -590,7 +621,7 @@ async function bootChainNodeOnce(params) {
|
|
|
590
621
|
mode: profile.configCheck?.mode ?? "off",
|
|
591
622
|
localConfig: resolver.config,
|
|
592
623
|
upstreamUrl: role.upstreamChainUrl ?? role.clusterPeerUrls?.[0] ?? null,
|
|
593
|
-
fetchUpstreamConfig: profile.configCheck?.fetchUpstreamConfig,
|
|
624
|
+
fetchUpstreamConfig: profile.configCheck?.fetchUpstreamConfig ?? ((u) => fetchUpstreamChainConfig(u, followerFetchGuard ? { fetchImpl: followerFetchGuard } : void 0)),
|
|
594
625
|
hasLocalState: !followerPrep?.bootstrapped && await ledger.getLatestSequence() > 0,
|
|
595
626
|
...profile.configCheck?.retryDeadlineMs !== void 0 ? { retryDeadlineMs: profile.configCheck.retryDeadlineMs } : {},
|
|
596
627
|
logger
|
|
@@ -652,9 +683,20 @@ async function bootChainNodeOnce(params) {
|
|
|
652
683
|
triggerSyncOnce: () => syncRef.triggerSyncOnce()
|
|
653
684
|
});
|
|
654
685
|
}
|
|
655
|
-
if (
|
|
656
|
-
|
|
657
|
-
|
|
686
|
+
if (followerIndexBuildNeeded) {
|
|
687
|
+
const buildStart = Date.now();
|
|
688
|
+
logger.info?.("follower initial index build started", {
|
|
689
|
+
indexdbPath,
|
|
690
|
+
resumed: indexDbExisted
|
|
691
|
+
});
|
|
692
|
+
const built = await resolver.rebuildIndex();
|
|
693
|
+
const orphans = await followerSync.replayer.indexExistingOrphanEntries(orphanIndexStart);
|
|
694
|
+
fs.rmSync(indexBuildMarkerPath, { force: true });
|
|
695
|
+
logger.info?.("follower initial index build finished", {
|
|
696
|
+
tables: built?.tables,
|
|
697
|
+
orphanEntries: orphans,
|
|
698
|
+
durationMs: Date.now() - buildStart
|
|
699
|
+
});
|
|
658
700
|
}
|
|
659
701
|
} else if (role.kind === "hotstuff") {
|
|
660
702
|
const hotstuffStateStore = new SqliteHotStuffStateStore(ledgerStorage);
|
|
@@ -724,7 +766,7 @@ async function bootChainNodeOnce(params) {
|
|
|
724
766
|
res.once("close", markDone);
|
|
725
767
|
next();
|
|
726
768
|
});
|
|
727
|
-
if (profile.http.enableAccessLog) app.use(createAccessLogMiddleware(logger));
|
|
769
|
+
if (profile.http.enableAccessLog) app.use(createAccessLogMiddleware(logger, explicitAdminEnv ? { env: explicitAdminEnv } : void 0));
|
|
728
770
|
app.head("/", (_req, res) => {
|
|
729
771
|
res.status(200).send("OK");
|
|
730
772
|
});
|
|
@@ -767,7 +809,10 @@ async function bootChainNodeOnce(params) {
|
|
|
767
809
|
const server = createSocketHandler({
|
|
768
810
|
indexdb,
|
|
769
811
|
app,
|
|
770
|
-
wsOptions: {
|
|
812
|
+
wsOptions: {
|
|
813
|
+
broadcastEventName: `@arcblock/ws:broadcast:${nodeId}:${crypto.randomUUID()}`,
|
|
814
|
+
...explicitAdminEnv ? { env: explicitAdminEnv } : {}
|
|
815
|
+
}
|
|
771
816
|
});
|
|
772
817
|
pushCleanup(async () => {
|
|
773
818
|
isShuttingDown = true;
|
|
@@ -802,6 +847,7 @@ async function bootChainNodeOnce(params) {
|
|
|
802
847
|
});
|
|
803
848
|
app.use("/", createHttpHandler({
|
|
804
849
|
resolver,
|
|
850
|
+
...explicitAdminEnv ? { env: explicitAdminEnv } : {},
|
|
805
851
|
onError: profile.http.onGraphqlError ? (err) => {
|
|
806
852
|
profile.http.onGraphqlError(err);
|
|
807
853
|
} : () => {}
|
package/esm/types.d.mts
CHANGED
|
@@ -86,6 +86,11 @@ interface FollowerRoleConfig {
|
|
|
86
86
|
signer?: ChainNodeSignerConfig;
|
|
87
87
|
/** Optional explicit runtime role (`'follower'` default or `'standby'`) */
|
|
88
88
|
runtimeRole?: 'follower' | 'standby';
|
|
89
|
+
/**
|
|
90
|
+
* First sequence whose orphan ledger entries the initial index build indexes (default: where the chain's leader
|
|
91
|
+
* began indexing them, see `orphanIndexFromSequence`; 1 for chains not listed there).
|
|
92
|
+
*/
|
|
93
|
+
orphanIndexFromSequence?: number;
|
|
89
94
|
}
|
|
90
95
|
interface HotStuffValidatorRoleConfig {
|
|
91
96
|
kind: 'hotstuff';
|
package/lib/access-log.cjs
CHANGED
|
@@ -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"])
|
package/lib/access-log.d.cts
CHANGED
|
@@ -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
|
|
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;
|
package/lib/follower-config.cjs
CHANGED
|
@@ -31,16 +31,24 @@ function mergeTransactionOverlay(base, overlay) {
|
|
|
31
31
|
* variables from env (e.g. CHAIN_TX_BRIDGE_ENABLED): the stored row is not rewritten on a ledger-anchored leader
|
|
32
32
|
* restart, so it still holds the values of the last genesis/config write.
|
|
33
33
|
*/
|
|
34
|
-
function rehydrateFollowerChainConfig(storedChain, profileChainConfig) {
|
|
34
|
+
function rehydrateFollowerChainConfig(storedChain, profileChainConfig, extraReservedSymbols) {
|
|
35
35
|
const storedChainPartial = storedChain;
|
|
36
36
|
const storedChainId = typeof storedChainPartial.chainId === "string" ? storedChainPartial.chainId.trim() : "";
|
|
37
37
|
const baseSymbol = storedChainPartial.token?.symbol || "TBA";
|
|
38
|
-
const reservedSymbols = Array.from(new Set([
|
|
38
|
+
const reservedSymbols = Array.from(new Set([
|
|
39
|
+
baseSymbol,
|
|
40
|
+
...storedChainPartial.reservedSymbols ?? [],
|
|
41
|
+
...profileChainConfig?.reservedSymbols ?? [],
|
|
42
|
+
...extraReservedSymbols ?? []
|
|
43
|
+
].map((s) => String(s || "").trim().toUpperCase()).filter(Boolean)));
|
|
39
44
|
const transactionOverlay = isPlainRecord(profileChainConfig?.transaction) ? profileChainConfig.transaction : void 0;
|
|
45
|
+
const storedTransaction = isPlainRecord(storedChainPartial.transaction) ? { ...storedChainPartial.transaction } : storedChainPartial.transaction;
|
|
46
|
+
if (isPlainRecord(storedTransaction)) delete storedTransaction.maxTxSize;
|
|
47
|
+
const transaction = transactionOverlay ? mergeTransactionOverlay(storedTransaction, transactionOverlay) : storedTransaction;
|
|
40
48
|
return _ocap_config.create({
|
|
41
49
|
...storedChain,
|
|
42
50
|
...storedChainId ? { chainId: storedChainId } : {},
|
|
43
|
-
...
|
|
51
|
+
...transaction !== void 0 ? { transaction } : {},
|
|
44
52
|
reservedSymbols
|
|
45
53
|
});
|
|
46
54
|
}
|
|
@@ -54,10 +62,29 @@ function toResolverConfig(config) {
|
|
|
54
62
|
if (tokenItx) validated.token.address = tokenItx.address;
|
|
55
63
|
return validated;
|
|
56
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* First ledger sequence whose orphan entries (finalized, no stateCommitHash, no statedb tx row) the chain's leader
|
|
67
|
+
* indexed. Leaders index the orphans they produce since 1.31.1 (#1387); before that they never did, and a follower
|
|
68
|
+
* index must hold exactly what the leader's holds (#1875). Values read from the leaders' indexdb: the first orphan
|
|
69
|
+
* with an index row, every earlier one without (beta: last unindexed 437972; main: last unindexed 832519). A chain
|
|
70
|
+
* started on 1.31.1+ indexed all of them.
|
|
71
|
+
*/
|
|
72
|
+
const ORPHAN_INDEX_FROM_SEQUENCE = Object.freeze({
|
|
73
|
+
beta: 437983,
|
|
74
|
+
"xenon-2020-01-15": 832598
|
|
75
|
+
});
|
|
76
|
+
function orphanIndexFromSequence(chainId, override) {
|
|
77
|
+
if (override !== void 0) {
|
|
78
|
+
if (!Number.isSafeInteger(override) || override < 1) throw new Error(`orphanIndexFromSequence must be a positive integer, got ${String(override)}`);
|
|
79
|
+
return override;
|
|
80
|
+
}
|
|
81
|
+
return Object.hasOwn(ORPHAN_INDEX_FROM_SEQUENCE, chainId) ? ORPHAN_INDEX_FROM_SEQUENCE[chainId] : 1;
|
|
82
|
+
}
|
|
57
83
|
|
|
58
84
|
//#endregion
|
|
59
85
|
exports.isCompleteChainConfig = isCompleteChainConfig;
|
|
60
86
|
exports.isPlainRecord = isPlainRecord;
|
|
61
87
|
exports.mergeTransactionOverlay = mergeTransactionOverlay;
|
|
88
|
+
exports.orphanIndexFromSequence = orphanIndexFromSequence;
|
|
62
89
|
exports.rehydrateFollowerChainConfig = rehydrateFollowerChainConfig;
|
|
63
90
|
exports.toResolverConfig = toResolverConfig;
|
|
@@ -15,11 +15,12 @@ declare function mergeTransactionOverlay(base: unknown, overlay: unknown): unkno
|
|
|
15
15
|
* variables from env (e.g. CHAIN_TX_BRIDGE_ENABLED): the stored row is not rewritten on a ledger-anchored leader
|
|
16
16
|
* restart, so it still holds the values of the last genesis/config write.
|
|
17
17
|
*/
|
|
18
|
-
declare function rehydrateFollowerChainConfig(storedChain: unknown, profileChainConfig?: Partial<IChainConfig
|
|
18
|
+
declare function rehydrateFollowerChainConfig(storedChain: unknown, profileChainConfig?: Partial<IChainConfig>, extraReservedSymbols?: readonly string[]): IChainConfig;
|
|
19
19
|
/**
|
|
20
20
|
* The config a resolver actually runs with (`OCAPResolver` constructor): validated, with `token.address` set to the
|
|
21
21
|
* genesis token address. This is what the leader's `getConfig` returns and what the follower's replayer reads.
|
|
22
22
|
*/
|
|
23
23
|
declare function toResolverConfig(config: IChainConfig): IChainConfig;
|
|
24
|
+
declare function orphanIndexFromSequence(chainId: string, override?: number): number;
|
|
24
25
|
//#endregion
|
|
25
|
-
export { isCompleteChainConfig, isPlainRecord, mergeTransactionOverlay, rehydrateFollowerChainConfig, toResolverConfig };
|
|
26
|
+
export { isCompleteChainConfig, isPlainRecord, mergeTransactionOverlay, orphanIndexFromSequence, rehydrateFollowerChainConfig, toResolverConfig };
|
package/lib/start-chain-node.cjs
CHANGED
|
@@ -69,6 +69,22 @@ function assertSafeNonSymlinkDirectory(rawDataDir) {
|
|
|
69
69
|
if (node_fs.default.lstatSync(dataDir).isSymbolicLink()) throw new _ocap_util_lib_error.CustomError("SECURITY_VIOLATION", `Refusing symlinked chain node dataDir: ${dataDir}`);
|
|
70
70
|
return dataDir;
|
|
71
71
|
}
|
|
72
|
+
/** Write a small file and fsync it and its directory, so it survives a crash or power loss right after. */
|
|
73
|
+
function writeFileDurably(filePath, content) {
|
|
74
|
+
const fd = node_fs.default.openSync(filePath, "w", 384);
|
|
75
|
+
try {
|
|
76
|
+
node_fs.default.writeFileSync(fd, content);
|
|
77
|
+
node_fs.default.fsyncSync(fd);
|
|
78
|
+
} finally {
|
|
79
|
+
node_fs.default.closeSync(fd);
|
|
80
|
+
}
|
|
81
|
+
const dirFd = node_fs.default.openSync(node_path.default.dirname(filePath), "r");
|
|
82
|
+
try {
|
|
83
|
+
node_fs.default.fsyncSync(dirFd);
|
|
84
|
+
} finally {
|
|
85
|
+
node_fs.default.closeSync(dirFd);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
72
88
|
function loadOrCreateNodeId(dataDir, explicitNodeId) {
|
|
73
89
|
const nodeIdPath = node_path.default.join(dataDir, NODE_ID_FILENAME);
|
|
74
90
|
if (node_fs.default.existsSync(nodeIdPath) && node_fs.default.lstatSync(nodeIdPath).isSymbolicLink()) throw new _ocap_util_lib_error.CustomError("SECURITY_VIOLATION", `Refusing symlinked .node-id file: ${nodeIdPath}`);
|
|
@@ -188,6 +204,7 @@ async function bootChainNodeOnce(params) {
|
|
|
188
204
|
if ((role.kind === "validator" || role.kind === "hotstuff") && !require_follower_config.isCompleteChainConfig(profile.chainConfig)) throw new _ocap_util_lib_error.CustomError("INVALID_CONFIG", `${role.kind} role requires a complete chainConfig`);
|
|
189
205
|
if ((role.kind === "validator" || role.kind === "hotstuff") && !role.signer) throw new _ocap_util_lib_error.CustomError("INVALID_CONFIG", `${role.kind} role requires role.signer`);
|
|
190
206
|
if (role.kind === "hotstuff" && (!Array.isArray(role.validators) || role.validators.length === 0)) throw new _ocap_util_lib_error.CustomError("INVALID_CONFIG", "HotStuff role requires a non-empty validators list");
|
|
207
|
+
if (role.kind === "follower" && role.orphanIndexFromSequence !== void 0 && (!Number.isSafeInteger(role.orphanIndexFromSequence) || role.orphanIndexFromSequence < 1)) throw new _ocap_util_lib_error.CustomError("INVALID_CONFIG", `role.orphanIndexFromSequence must be a positive integer, got ${String(role.orphanIndexFromSequence)}`);
|
|
191
208
|
const logger = testHooks?.logger ?? profile.logger ?? createDefaultLogger("chain-node");
|
|
192
209
|
const variadicLogger = toVariadicLogger(logger);
|
|
193
210
|
const followerExpectedChainId = "expectedChainId" in profile ? profile.expectedChainId : void 0;
|
|
@@ -235,6 +252,7 @@ async function bootChainNodeOnce(params) {
|
|
|
235
252
|
let ledgerCleanupIndex = -1;
|
|
236
253
|
let followerPrep = null;
|
|
237
254
|
const isKeylessFollower = role.kind === "follower" && !profile.storage?.trustedStorageSecretKey?.trim();
|
|
255
|
+
let followerFetchGuard;
|
|
238
256
|
if (role.kind === "follower") {
|
|
239
257
|
if (node_fs.default.existsSync(ledgerPath) && node_fs.default.statSync(ledgerPath).size > 0) {
|
|
240
258
|
const precheckLedgerStorage = createLedgerStorage();
|
|
@@ -271,6 +289,7 @@ async function bootChainNodeOnce(params) {
|
|
|
271
289
|
init
|
|
272
290
|
}, guardedBootstrapFetch);
|
|
273
291
|
}) : guardedBootstrapFetch;
|
|
292
|
+
followerFetchGuard = bootstrapFetchImpl;
|
|
274
293
|
const explicitExpectedChainId = followerExpectedChainId?.trim() || "";
|
|
275
294
|
const profileChainId = profile.chainConfig?.chainId?.trim() || "";
|
|
276
295
|
if (explicitExpectedChainId && profileChainId && explicitExpectedChainId !== profileChainId) throw new _ocap_util_lib_error.CustomError("SNAPSHOT_CHAIN_ID_MISMATCH", `Follower expectedChainId (${explicitExpectedChainId}) !== profile.chainConfig.chainId (${profileChainId})`);
|
|
@@ -350,7 +369,7 @@ async function bootChainNodeOnce(params) {
|
|
|
350
369
|
const storedChainId = typeof storedChainPartial.chainId === "string" ? storedChainPartial.chainId.trim() : "";
|
|
351
370
|
const overrideChainId = typeof profile.chainConfig?.chainId === "string" ? profile.chainConfig.chainId.trim() : "";
|
|
352
371
|
if (storedChainId && overrideChainId && overrideChainId !== storedChainId) throw new _ocap_util_lib_error.CustomError("SNAPSHOT_CHAIN_ID_MISMATCH", `Follower profile.chainConfig.chainId (${overrideChainId}) !== statedb chain.chainId (${storedChainId})`);
|
|
353
|
-
chainConfig = require_follower_config.rehydrateFollowerChainConfig(storedChain, profile.chainConfig);
|
|
372
|
+
chainConfig = require_follower_config.rehydrateFollowerChainConfig(storedChain, profile.chainConfig, profile.reservedSymbols);
|
|
354
373
|
} else {
|
|
355
374
|
const baseConfig = profile.chainConfig;
|
|
356
375
|
if (role.kind === "follower") {
|
|
@@ -361,6 +380,7 @@ async function bootChainNodeOnce(params) {
|
|
|
361
380
|
chainConfig = baseConfig;
|
|
362
381
|
}
|
|
363
382
|
if (role.kind === "follower" && followerExpectedChainId && chainConfig.chainId !== followerExpectedChainId) throw new _ocap_util_lib_error.CustomError("SNAPSHOT_CHAIN_ID_MISMATCH", `Rehydrated Follower chainId (${chainConfig.chainId}) !== expectedChainId (${followerExpectedChainId})`);
|
|
383
|
+
const orphanIndexStart = role.kind === "follower" ? require_follower_config.orphanIndexFromSequence(chainConfig.chainId, role.orphanIndexFromSequence) : 1;
|
|
364
384
|
const hotstuffValidators = role.kind === "hotstuff" ? role.validators : role.kind === "follower" ? role.hotstuffValidators ?? [] : [];
|
|
365
385
|
const hotstuffQuorumThreshold = role.kind === "hotstuff" ? role.quorumThreshold : role.kind === "follower" ? role.hotstuffQuorumThreshold : void 0;
|
|
366
386
|
const hotstuffClusterConfig = role.kind === "hotstuff" || hotstuffValidators.length > 0 ? (0, _arcblock_ledger_core.toHotStuffClusterConfig)(chainConfig.chainId, hotstuffValidators, hotstuffQuorumThreshold) : void 0;
|
|
@@ -529,6 +549,9 @@ async function bootChainNodeOnce(params) {
|
|
|
529
549
|
});
|
|
530
550
|
}
|
|
531
551
|
const indexDbExisted = node_fs.default.existsSync(indexdbPath);
|
|
552
|
+
const indexBuildMarkerPath = `${indexdbPath}.build-pending`;
|
|
553
|
+
let followerIndexBuildNeeded = role.kind === "follower" && (Boolean(followerPrep?.bootstrapped) || !indexDbExisted || node_fs.default.existsSync(indexBuildMarkerPath));
|
|
554
|
+
if (followerIndexBuildNeeded) writeFileDurably(indexBuildMarkerPath, `${(/* @__PURE__ */ new Date()).toISOString()}\n`);
|
|
532
555
|
const indexdb = new _ocap_indexdb_sqlite.default({
|
|
533
556
|
filename: indexdbPath,
|
|
534
557
|
...profile.storage?.indexWalAutoCheckpoint !== void 0 ? { walAutoCheckpoint: profile.storage.indexWalAutoCheckpoint } : {}
|
|
@@ -538,8 +561,16 @@ async function bootChainNodeOnce(params) {
|
|
|
538
561
|
});
|
|
539
562
|
await indexdb.initialize();
|
|
540
563
|
await waitEventEmitterReady(indexdb);
|
|
564
|
+
if (role.kind === "follower" && !followerIndexBuildNeeded) {
|
|
565
|
+
if (await indexdb.tx.count() === 0 && await ledger.getLatestSequence() > 0) {
|
|
566
|
+
logger.warn?.("follower IndexDB has no transactions while the ledger has entries; rebuilding it", { indexdbPath });
|
|
567
|
+
followerIndexBuildNeeded = true;
|
|
568
|
+
writeFileDurably(indexBuildMarkerPath, `${(/* @__PURE__ */ new Date()).toISOString()}\n`);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
541
571
|
let resolverRef = null;
|
|
542
|
-
const
|
|
572
|
+
const explicitAdminEnv = profile.http.adminEnv;
|
|
573
|
+
const adminEnv = explicitAdminEnv ?? {};
|
|
543
574
|
const preferencesManager = new _ocap_resolver.NodePreferencesManager({
|
|
544
575
|
dataDir,
|
|
545
576
|
env: adminEnv,
|
|
@@ -599,7 +630,7 @@ async function bootChainNodeOnce(params) {
|
|
|
599
630
|
mode: profile.configCheck?.mode ?? "off",
|
|
600
631
|
localConfig: resolver.config,
|
|
601
632
|
upstreamUrl: role.upstreamChainUrl ?? role.clusterPeerUrls?.[0] ?? null,
|
|
602
|
-
fetchUpstreamConfig: profile.configCheck?.fetchUpstreamConfig,
|
|
633
|
+
fetchUpstreamConfig: profile.configCheck?.fetchUpstreamConfig ?? ((u) => require_config_consistency.fetchUpstreamChainConfig(u, followerFetchGuard ? { fetchImpl: followerFetchGuard } : void 0)),
|
|
603
634
|
hasLocalState: !followerPrep?.bootstrapped && await ledger.getLatestSequence() > 0,
|
|
604
635
|
...profile.configCheck?.retryDeadlineMs !== void 0 ? { retryDeadlineMs: profile.configCheck.retryDeadlineMs } : {},
|
|
605
636
|
logger
|
|
@@ -661,9 +692,20 @@ async function bootChainNodeOnce(params) {
|
|
|
661
692
|
triggerSyncOnce: () => syncRef.triggerSyncOnce()
|
|
662
693
|
});
|
|
663
694
|
}
|
|
664
|
-
if (
|
|
665
|
-
|
|
666
|
-
|
|
695
|
+
if (followerIndexBuildNeeded) {
|
|
696
|
+
const buildStart = Date.now();
|
|
697
|
+
logger.info?.("follower initial index build started", {
|
|
698
|
+
indexdbPath,
|
|
699
|
+
resumed: indexDbExisted
|
|
700
|
+
});
|
|
701
|
+
const built = await resolver.rebuildIndex();
|
|
702
|
+
const orphans = await followerSync.replayer.indexExistingOrphanEntries(orphanIndexStart);
|
|
703
|
+
node_fs.default.rmSync(indexBuildMarkerPath, { force: true });
|
|
704
|
+
logger.info?.("follower initial index build finished", {
|
|
705
|
+
tables: built?.tables,
|
|
706
|
+
orphanEntries: orphans,
|
|
707
|
+
durationMs: Date.now() - buildStart
|
|
708
|
+
});
|
|
667
709
|
}
|
|
668
710
|
} else if (role.kind === "hotstuff") {
|
|
669
711
|
const hotstuffStateStore = new _arcblock_ledger_sqlite.SqliteHotStuffStateStore(ledgerStorage);
|
|
@@ -733,7 +775,7 @@ async function bootChainNodeOnce(params) {
|
|
|
733
775
|
res.once("close", markDone);
|
|
734
776
|
next();
|
|
735
777
|
});
|
|
736
|
-
if (profile.http.enableAccessLog) app.use(require_access_log.createAccessLogMiddleware(logger));
|
|
778
|
+
if (profile.http.enableAccessLog) app.use(require_access_log.createAccessLogMiddleware(logger, explicitAdminEnv ? { env: explicitAdminEnv } : void 0));
|
|
737
779
|
app.head("/", (_req, res) => {
|
|
738
780
|
res.status(200).send("OK");
|
|
739
781
|
});
|
|
@@ -776,7 +818,10 @@ async function bootChainNodeOnce(params) {
|
|
|
776
818
|
const server = (0, _ocap_gql.createSocketHandler)({
|
|
777
819
|
indexdb,
|
|
778
820
|
app,
|
|
779
|
-
wsOptions: {
|
|
821
|
+
wsOptions: {
|
|
822
|
+
broadcastEventName: `@arcblock/ws:broadcast:${nodeId}:${node_crypto.default.randomUUID()}`,
|
|
823
|
+
...explicitAdminEnv ? { env: explicitAdminEnv } : {}
|
|
824
|
+
}
|
|
780
825
|
});
|
|
781
826
|
pushCleanup(async () => {
|
|
782
827
|
isShuttingDown = true;
|
|
@@ -811,6 +856,7 @@ async function bootChainNodeOnce(params) {
|
|
|
811
856
|
});
|
|
812
857
|
app.use("/", (0, _ocap_gql.createHttpHandler)({
|
|
813
858
|
resolver,
|
|
859
|
+
...explicitAdminEnv ? { env: explicitAdminEnv } : {},
|
|
814
860
|
onError: profile.http.onGraphqlError ? (err) => {
|
|
815
861
|
profile.http.onGraphqlError(err);
|
|
816
862
|
} : () => {}
|
package/lib/types.d.cts
CHANGED
|
@@ -86,6 +86,11 @@ interface FollowerRoleConfig {
|
|
|
86
86
|
signer?: ChainNodeSignerConfig;
|
|
87
87
|
/** Optional explicit runtime role (`'follower'` default or `'standby'`) */
|
|
88
88
|
runtimeRole?: 'follower' | 'standby';
|
|
89
|
+
/**
|
|
90
|
+
* First sequence whose orphan ledger entries the initial index build indexes (default: where the chain's leader
|
|
91
|
+
* began indexing them, see `orphanIndexFromSequence`; 1 for chains not listed there).
|
|
92
|
+
*/
|
|
93
|
+
orphanIndexFromSequence?: number;
|
|
89
94
|
}
|
|
90
95
|
interface HotStuffValidatorRoleConfig {
|
|
91
96
|
kind: 'hotstuff';
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.32.
|
|
6
|
+
"version": "1.32.2",
|
|
7
7
|
"description": "Unified OCAP chain node runtime assembly (Validator, Standby, Keyless Follower)",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"main": "./lib/index.cjs",
|
|
@@ -54,26 +54,26 @@
|
|
|
54
54
|
"author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
|
|
55
55
|
"license": "MIT",
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@ocap/mcrypto": "1.32.
|
|
58
|
-
"@ocap/message": "1.32.
|
|
57
|
+
"@ocap/mcrypto": "1.32.2",
|
|
58
|
+
"@ocap/message": "1.32.2",
|
|
59
59
|
"@types/express": "^4.17.23",
|
|
60
60
|
"@types/node": "^20.10.0",
|
|
61
61
|
"tsdown": "^0.18.4"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@arcblock/did": "1.32.
|
|
65
|
-
"@arcblock/ledger-core": "1.32.
|
|
66
|
-
"@arcblock/ledger-sqlite": "1.32.
|
|
67
|
-
"@ocap/config": "1.32.
|
|
68
|
-
"@ocap/gql": "1.32.
|
|
69
|
-
"@ocap/indexdb-sqlite": "1.32.
|
|
70
|
-
"@ocap/resolver": "1.32.
|
|
71
|
-
"@ocap/state": "1.32.
|
|
72
|
-
"@ocap/statedb-sqlite": "1.32.
|
|
73
|
-
"@ocap/trusted-storage": "1.32.
|
|
74
|
-
"@ocap/types": "1.32.
|
|
75
|
-
"@ocap/util": "1.32.
|
|
76
|
-
"@ocap/wallet": "1.32.
|
|
64
|
+
"@arcblock/did": "1.32.2",
|
|
65
|
+
"@arcblock/ledger-core": "1.32.2",
|
|
66
|
+
"@arcblock/ledger-sqlite": "1.32.2",
|
|
67
|
+
"@ocap/config": "1.32.2",
|
|
68
|
+
"@ocap/gql": "1.32.2",
|
|
69
|
+
"@ocap/indexdb-sqlite": "1.32.2",
|
|
70
|
+
"@ocap/resolver": "1.32.2",
|
|
71
|
+
"@ocap/state": "1.32.2",
|
|
72
|
+
"@ocap/statedb-sqlite": "1.32.2",
|
|
73
|
+
"@ocap/trusted-storage": "1.32.2",
|
|
74
|
+
"@ocap/types": "1.32.2",
|
|
75
|
+
"@ocap/util": "1.32.2",
|
|
76
|
+
"@ocap/wallet": "1.32.2",
|
|
77
77
|
"cors": "^2.8.5",
|
|
78
78
|
"express": "^4.22.1"
|
|
79
79
|
}
|