-
-
Notifications
You must be signed in to change notification settings - Fork 458
Expand file tree
/
Copy pathmockedBeaconChain.ts
More file actions
199 lines (180 loc) · 6.64 KB
/
mockedBeaconChain.ts
File metadata and controls
199 lines (180 loc) · 6.64 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import {PubkeyIndexMap} from "@chainsafe/pubkey-index-map";
import {ChainForkConfig} from "@lodestar/config";
import {config as defaultConfig} from "@lodestar/config/default";
import {EpochDifference, ForkChoice, ProtoBlock} from "@lodestar/fork-choice";
import {Logger} from "@lodestar/utils";
import {Mock, Mocked, vi} from "vitest";
import {BeaconProposerCache} from "../../src/chain/beaconProposerCache.js";
import {BeaconChain} from "../../src/chain/chain.js";
import {ChainEventEmitter} from "../../src/chain/emitter.js";
import {LightClientServer} from "../../src/chain/lightClient/index.js";
import {AggregatedAttestationPool, OpPool, SyncContributionAndProofPool} from "../../src/chain/opPools/index.js";
import {QueuedStateRegenerator} from "../../src/chain/regen/index.js";
import {ShufflingCache} from "../../src/chain/shufflingCache.js";
import {Eth1ForBlockProduction} from "../../src/eth1/index.js";
import {ExecutionBuilderHttp} from "../../src/execution/builder/http.js";
import {ExecutionEngineHttp} from "../../src/execution/engine/index.js";
import {Clock} from "../../src/util/clock.js";
import {getMockedClock} from "./clock.js";
import {getMockedLogger} from "./loggerMock.js";
export type MockedBeaconChain = Mocked<BeaconChain> & {
logger: Mocked<Logger>;
getHeadState: Mock;
forkChoice: MockedForkChoice;
executionEngine: Mocked<ExecutionEngineHttp>;
executionBuilder: Mocked<ExecutionBuilderHttp>;
eth1: Mocked<Eth1ForBlockProduction>;
opPool: Mocked<OpPool>;
aggregatedAttestationPool: Mocked<AggregatedAttestationPool>;
syncContributionAndProofPool: Mocked<SyncContributionAndProofPool>;
beaconProposerCache: Mocked<BeaconProposerCache>;
shufflingCache: Mocked<ShufflingCache>;
regen: Mocked<QueuedStateRegenerator>;
bls: {
verifySignatureSets: Mock<() => boolean>;
verifySignatureSetsSameMessage: Mock<() => boolean>;
close: Mock;
canAcceptWork: Mock<() => boolean>;
};
lightClientServer: Mocked<LightClientServer>;
};
vi.mock("@lodestar/fork-choice", async (importActual) => {
const mod = await importActual<typeof import("@lodestar/fork-choice")>();
const ForkChoice = vi.fn().mockImplementation(() => {
return {
updateTime: vi.fn(),
getJustifiedBlock: vi.fn(),
getFinalizedBlock: vi.fn(),
getHead: vi.fn(),
getHeadRoot: vi.fn(),
getDependentRoot: vi.fn(),
getBlockHex: vi.fn(),
getBlock: vi.fn(),
getAllAncestorBlocks: vi.fn(),
getAllNonAncestorBlocks: vi.fn(),
iterateAncestorBlocks: vi.fn(),
getBlockSummariesByParentRoot: vi.fn(),
getCanonicalBlockAtSlot: vi.fn(),
getFinalizedCheckpoint: vi.fn(),
hasBlockHex: vi.fn(),
getBlockSummariesAtSlot: vi.fn(),
};
});
return {
...mod,
ForkChoice,
};
});
vi.mock("../../src/chain/regen/index.js");
vi.mock("../../src/eth1/index.js");
vi.mock("../../src/chain/beaconProposerCache.js");
vi.mock("../../src/chain/shufflingCache.js");
vi.mock("../../src/chain/lightClient/index.js");
vi.mock("../../src/chain/opPools/index.js", async (importActual) => {
const mod = await importActual<typeof import("../../src/chain/opPools/index.js")>();
const OpPool = vi.fn().mockImplementation(() => {
return {
hasSeenBlsToExecutionChange: vi.fn(),
hasSeenVoluntaryExit: vi.fn(),
hasSeenProposerSlashing: vi.fn(),
hasSeenAttesterSlashing: vi.fn(),
getSlashingsAndExits: vi.fn(),
};
});
const AggregatedAttestationPool = vi.fn().mockImplementation(() => {
return {
getAttestationsForBlock: vi.fn(),
};
});
const SyncContributionAndProofPool = vi.fn().mockImplementation(() => {
return {
getAggregate: vi.fn(),
};
});
return {
...mod,
OpPool,
AggregatedAttestationPool,
SyncContributionAndProofPool,
};
});
vi.mock("../../src/chain/chain.js", async (importActual) => {
const mod = await importActual<typeof import("../../src/chain/chain.js")>();
const BeaconChain = vi.fn().mockImplementation(({clock, genesisTime, config}: MockedBeaconChainOptions) => {
const logger = getMockedLogger();
return {
config,
opts: {},
genesisTime,
clock:
clock === "real" ? new Clock({config, genesisTime, signal: new AbortController().signal}) : getMockedClock(),
forkChoice: getMockedForkChoice(),
executionEngine: {
notifyForkchoiceUpdate: vi.fn(),
getPayload: vi.fn(),
getClientVersion: vi.fn(),
},
executionBuilder: {},
// @ts-expect-error
eth1: new Eth1ForBlockProduction(),
opPool: new OpPool(),
aggregatedAttestationPool: new AggregatedAttestationPool(config),
syncContributionAndProofPool: new SyncContributionAndProofPool(),
// @ts-expect-error
beaconProposerCache: new BeaconProposerCache(),
shufflingCache: new ShufflingCache(),
pubkey2index: new PubkeyIndexMap(),
index2pubkey: [],
produceCommonBlockBody: vi.fn(),
getProposerHead: vi.fn(),
produceBlock: vi.fn(),
produceBlindedBlock: vi.fn(),
getCanonicalBlockAtSlot: vi.fn(),
recomputeForkChoiceHead: vi.fn(),
predictProposerHead: vi.fn(),
getHeadStateAtCurrentEpoch: vi.fn(),
getHeadState: vi.fn(),
getStateBySlot: vi.fn(),
updateBuilderStatus: vi.fn(),
processBlock: vi.fn(),
regenStateForAttestationVerification: vi.fn(),
close: vi.fn(),
logger,
regen: new QueuedStateRegenerator({} as any),
lightClientServer: new LightClientServer({} as any, {} as any),
bls: {
verifySignatureSets: vi.fn().mockResolvedValue(true),
verifySignatureSetsSameMessage: vi.fn().mockResolvedValue([true]),
close: vi.fn().mockResolvedValue(true),
canAcceptWork: vi.fn().mockReturnValue(true),
},
emitter: new ChainEventEmitter(),
};
});
return {
...mod,
BeaconChain,
};
});
export type MockedBeaconChainOptions = {
clock: "real" | "fake";
genesisTime: number;
config: ChainForkConfig;
};
export function getMockedBeaconChain(opts?: Partial<MockedBeaconChainOptions>): MockedBeaconChain {
const {clock, genesisTime, config} = opts ?? {};
// @ts-expect-error
return new BeaconChain({
clock: clock ?? "fake",
genesisTime: genesisTime ?? 0,
config: config ?? defaultConfig,
}) as MockedBeaconChain;
}
export type MockedForkChoice = Mocked<ForkChoice>;
export function getMockedForkChoice(): MockedForkChoice {
// ForkChoice package is mocked globally
return vi.mocked(new ForkChoice({} as any, {} as any, {} as any, {} as any));
}
// To avoid loading the package in test while mocked, exporting frequently used types and constants
export type {ProtoBlock};
export {EpochDifference};