-
-
Notifications
You must be signed in to change notification settings - Fork 444
Expand file tree
/
Copy pathsyncCommittee.test.ts
More file actions
69 lines (63 loc) · 2.73 KB
/
syncCommittee.test.ts
File metadata and controls
69 lines (63 loc) · 2.73 KB
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
import {MockedObject, afterEach, beforeAll, beforeEach, describe, expect, it, vi} from "vitest";
import {SecretKey} from "@chainsafe/lodestar-z/blst";
import {toHexString} from "@chainsafe/ssz";
import {config} from "@lodestar/config/default";
import {altair} from "@lodestar/types";
import {SyncCommitteeMessagePool} from "../../../../src/chain/opPools/index.js";
import {Clock} from "../../../../src/util/clock.js";
vi.mock("../../../../src/util/clock.js");
describe("chain / opPools / SyncCommitteeMessagePool", () => {
let cache: SyncCommitteeMessagePool;
const subcommitteeIndex = 2;
const indexInSubcommittee = 3;
const beaconBlockRoot = Buffer.alloc(32, 1);
const slot = 10;
let syncCommittee: altair.SyncCommitteeMessage;
let clockStub: MockedObject<Clock>;
const cutOffTime = 1;
beforeAll(async () => {
const sk = SecretKey.fromBytes(Buffer.alloc(32, 1));
syncCommittee = {
slot,
beaconBlockRoot,
validatorIndex: 2000,
signature: sk.sign(beaconBlockRoot).toBytes(),
};
});
beforeEach(() => {
clockStub = vi.mocked(new Clock({} as any));
cache = new SyncCommitteeMessagePool(config, clockStub, cutOffTime);
cache.add(subcommitteeIndex, syncCommittee, indexInSubcommittee);
});
afterEach(() => {
vi.clearAllTimers();
vi.clearAllMocks();
});
it("should propagate SyncCommitteeContribution", () => {
clockStub.msFromSlot.mockReturnValue(0);
let contribution = cache.getContribution(subcommitteeIndex, syncCommittee.slot, syncCommittee.beaconBlockRoot);
expect(contribution).not.toBeNull();
const newSecretKey = SecretKey.fromBytes(Buffer.alloc(32, 2));
const newSyncCommittee: altair.SyncCommitteeMessage = {
slot: syncCommittee.slot,
beaconBlockRoot,
// different validatorIndex
validatorIndex: syncCommittee.validatorIndex + 1,
signature: newSecretKey.sign(beaconBlockRoot).toBytes(),
};
const newIndicesInSubSyncCommittee = [1];
cache.add(subcommitteeIndex, newSyncCommittee, newIndicesInSubSyncCommittee[0]);
contribution = cache.getContribution(subcommitteeIndex, syncCommittee.slot, syncCommittee.beaconBlockRoot);
expect(contribution).not.toBeNull();
if (contribution) {
expect(contribution.slot).toBe(syncCommittee.slot);
expect(toHexString(contribution.beaconBlockRoot)).toBe(toHexString(syncCommittee.beaconBlockRoot));
expect(contribution.subcommitteeIndex).toBe(subcommitteeIndex);
const newIndices = [...newIndicesInSubSyncCommittee, indexInSubcommittee];
const aggregationBits = contribution.aggregationBits;
for (let index = 0; index < aggregationBits.bitLen; index++) {
expect(aggregationBits.get(index)).toBe(newIndices.includes(index));
}
}
});
});