-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathensure-config-is-synced.test.ts
73 lines (61 loc) · 2.63 KB
/
ensure-config-is-synced.test.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
import axios from "axios";
import {describe, expect, it, vi} from "vitest";
import {BeaconPreset, ForkName} from "../../src/index.js";
import {mainnetPreset} from "../../src/presets/mainnet.js";
import {minimalPreset} from "../../src/presets/minimal.js";
import {loadConfigYaml} from "../yaml.js";
// Not e2e, but slow. Run with e2e tests
/** https://github.com/ethereum/consensus-specs/releases */
const specConfigCommit = "v1.5.0-beta.1";
/**
* Fields that we filter from local config when doing comparison.
* Ideally this should be empty as it is not spec compliant
*/
const ignoredLocalPresetFields: (keyof BeaconPreset)[] = [];
describe("Ensure config is synced", () => {
vi.setConfig({testTimeout: 60 * 1000});
it("mainnet", async () => {
const remotePreset = await downloadRemoteConfig("mainnet", specConfigCommit);
assertCorrectPreset({...mainnetPreset}, remotePreset);
});
it("minimal", async () => {
const remotePreset = await downloadRemoteConfig("minimal", specConfigCommit);
assertCorrectPreset({...minimalPreset}, remotePreset);
});
});
function assertCorrectPreset(localPreset: BeaconPreset, remotePreset: BeaconPreset): void {
const filteredLocalPreset: Partial<BeaconPreset> = Object.keys(localPreset)
.filter((key) => !ignoredLocalPresetFields.includes(key as keyof BeaconPreset))
.reduce(
(acc, key) => {
acc[key as keyof BeaconPreset] = localPreset[key as keyof BeaconPreset];
return acc;
},
{} as Partial<BeaconPreset>
);
// Check each key for better debuggability
for (const key of Object.keys(remotePreset) as (keyof BeaconPreset)[]) {
expect(filteredLocalPreset[key]).toBe(remotePreset[key]);
}
expect(filteredLocalPreset).toEqual(remotePreset);
}
async function downloadRemoteConfig(preset: "mainnet" | "minimal", commit: string): Promise<BeaconPreset> {
const downloadedParams = await Promise.all(
Object.values(ForkName).map((forkName) =>
axios({
url: `https://raw.githubusercontent.com/ethereum/consensus-specs/${commit}/presets/${preset}/${forkName}.yaml`,
timeout: 30 * 1000,
}).then((response) => loadConfigYaml(response.data))
)
);
// Merge all the fetched yamls for the different forks
const beaconPresetRaw: Record<string, unknown> = Object.assign(
...(downloadedParams as unknown as [input: Record<string, unknown>])
);
// As of December 2021 the presets don't include any hex strings
const beaconPreset = {} as BeaconPreset;
for (const key of Object.keys(beaconPresetRaw)) {
beaconPreset[key as keyof BeaconPreset] = parseInt(beaconPresetRaw[key] as string, 10);
}
return beaconPreset;
}