/
endpoint.test.ts
107 lines (93 loc) 路 3.98 KB
/
endpoint.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
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
100
101
102
103
104
105
106
107
import chaiAsPromised from "chai-as-promised";
import chai, {expect} from "chai";
import {createIBeaconConfig, IChainConfig} from "@lodestar/config";
import {chainConfig as chainConfigDef} from "@lodestar/config/default";
import {getClient} from "@lodestar/api";
import {toHexString} from "@chainsafe/ssz";
import {LogLevel, testLogger, TestLoggerOpts} from "../../../../../utils/logger.js";
import {getDevBeaconNode} from "../../../../../utils/node/beacon.js";
import {getAndInitDevValidators} from "../../../../../utils/node/validator.js";
chai.use(chaiAsPromised);
/* eslint-disable @typescript-eslint/naming-convention */
describe("lodestar / api / impl / state", function () {
const SECONDS_PER_SLOT = 2;
const ALTAIR_FORK_EPOCH = 0;
const restPort = 9596;
const chainConfig: IChainConfig = {...chainConfigDef, SECONDS_PER_SLOT, ALTAIR_FORK_EPOCH};
const genesisValidatorsRoot = Buffer.alloc(32, 0xaa);
const config = createIBeaconConfig(chainConfig, genesisValidatorsRoot);
const testLoggerOpts: TestLoggerOpts = {logLevel: LogLevel.info};
const loggerNodeA = testLogger("Node-A", testLoggerOpts);
describe("eth/v1/beacon/states/{status_id}/validators", function () {
this.timeout("10 min");
const testParams: Pick<IChainConfig, "SECONDS_PER_SLOT"> = {
SECONDS_PER_SLOT: 2,
};
const afterEachCallbacks: (() => Promise<unknown> | void)[] = [];
afterEach(async () => {
while (afterEachCallbacks.length > 0) {
const callback = afterEachCallbacks.pop();
if (callback) await callback();
}
});
it("should return all validators when getStateValidators called without filters", async function () {
const validatorCount = 2;
const bn = await getDevBeaconNode({
params: testParams,
options: {
sync: {isSingleNode: true},
api: {rest: {enabled: true, port: restPort}},
chain: {blsVerifyAllMainThread: true},
},
validatorCount,
logger: loggerNodeA,
});
afterEachCallbacks.push(() => bn.close());
const {validators} = await getAndInitDevValidators({
node: bn,
validatorsPerClient: validatorCount,
validatorClientCount: 1,
startIndex: 0,
useRestApi: false,
testLoggerOpts,
});
afterEachCallbacks.push(() => Promise.all(validators.map((validator) => validator.close())));
const client = getClient({baseUrl: `http://127.0.0.1:${restPort}`}, {config}).beacon;
const response = await client.getStateValidators("head");
expect(response.data.length).to.be.equal(validatorCount);
expect(response.data[0].index).to.be.equal(0);
expect(response.data[1].index).to.be.equal(1);
});
it("should return filtered validators when getStateValidators called with filters", async function () {
const validatorCount = 2;
const filterPubKey =
"0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c";
const bn = await getDevBeaconNode({
params: testParams,
options: {
sync: {isSingleNode: true},
api: {rest: {enabled: true, port: restPort}},
chain: {blsVerifyAllMainThread: true},
},
validatorCount,
logger: loggerNodeA,
});
afterEachCallbacks.push(() => bn.close());
const {validators} = await getAndInitDevValidators({
node: bn,
validatorsPerClient: validatorCount,
validatorClientCount: 1,
startIndex: 0,
useRestApi: false,
testLoggerOpts,
});
afterEachCallbacks.push(() => Promise.all(validators.map((validator) => validator.close())));
const client = getClient({baseUrl: `http://127.0.0.1:${restPort}`}, {config}).beacon;
const response = await client.getStateValidators("head", {
id: [filterPubKey],
});
expect(response.data.length).to.be.equal(1);
expect(toHexString(response.data[0].validator.pubkey)).to.be.equal(filterPubKey);
});
});
});