-
-
Notifications
You must be signed in to change notification settings - Fork 318
/
Copy pathapplyPreset.ts
99 lines (84 loc) · 3.25 KB
/
applyPreset.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// MUST import this file first before anything and not import any Lodestar code.
// eslint-disable-next-line no-restricted-imports
import {hasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/as-sha256.js";
// eslint-disable-next-line no-restricted-imports
import {setHasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/index.js";
// without setting this first, persistent-merkle-tree will use noble instead
setHasher(hasher);
//
// ## Rationale
//
// Lodestar implemented PRESET / CONFIG separation to allow importing types and preset constants directly
// see https://github.com/ChainSafe/lodestar/pull/2585
//
// However this prevents dynamic configuration changes which is exactly what the CLI required before.
// - The dev command can't apply the minimal preset dynamically
// - `--network gnosis` can't apply a different preset dynamically
// - `--network chiado` can't apply a different preset dynamically
//
// Running this file allows us to keep a static export strategy while NOT requiring users to
// set LODESTAR_PRESET manually every time.
// IMPORTANT: only import Lodestar code here which does not import any other Lodestar libraries
import {setActivePreset, presetFromJson, PresetName} from "@lodestar/params/setPreset";
import {readFile} from "./util/file.js";
const network = valueOfArg("network");
const preset = valueOfArg("preset");
const presetFile = valueOfArg("presetFile");
// Apply preset flag if present
if (preset) {
process.env.LODESTAR_PRESET = preset;
}
// If ENV is set overrides, network (otherwise can not override network --dev in mainnet mode)
else if (process.env.LODESTAR_PRESET) {
// break
}
// Translate network to preset
else if (network) {
if (network === "dev") {
process.env.LODESTAR_PRESET = "minimal";
// "c-kzg" has hardcoded the mainnet value, do not use presets
// eslint-disable-next-line @typescript-eslint/naming-convention
setActivePreset(PresetName.minimal, {FIELD_ELEMENTS_PER_BLOB: 4096});
} else if (network === "gnosis" || network === "chiado") {
process.env.LODESTAR_PRESET = "gnosis";
}
}
// If running dev top level command `$ lodestar dev`, apply minimal
else if (process.argv[2] === "dev") {
process.env.LODESTAR_PRESET = "minimal";
process.env.LODESTAR_NETWORK = "dev";
// "c-kzg" has hardcoded the mainnet value, do not use presets
// eslint-disable-next-line @typescript-eslint/naming-convention
setActivePreset(PresetName.minimal, {FIELD_ELEMENTS_PER_BLOB: 4096});
}
if (presetFile) {
// Override the active preset with custom values from file
// Do not modify the preset to use as a base by passing null
setActivePreset(null, presetFromJson(readFile(presetFile) ?? {}));
}
/**
* Valid syntax
* - `--preset minimal`
* - `--preset=minimal`
*/
function valueOfArg(argName: string): string | null {
// Syntax `--preset minimal`
// process.argv = ["--preset", "minimal"];
{
const index = process.argv.indexOf(`--${argName}`);
if (index > -1) {
return process.argv[index + 1] ?? "";
}
}
// Syntax `--preset=minimal`
{
const prefix = `--${argName}=`;
const item = process.argv.find((arg) => arg.startsWith(prefix));
if (item) {
return item.slice(prefix.length);
}
}
return null;
}
// Add empty export to make this a module
export {};