Skip to content

Commit

Permalink
Update the clock util
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarhussain committed Mar 15, 2024
1 parent 594cb88 commit c0887de
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
14 changes: 14 additions & 0 deletions packages/beacon-node/test/mocks/clock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import EventEmitter from "node:events";
import {Mocked, vi} from "vitest";
import {computeEpochAtSlot} from "@lodestar/state-transition";
import {Epoch, Slot} from "@lodestar/types";
import {IClock} from "../../src/util/clock.js";
Expand Down Expand Up @@ -62,3 +63,16 @@ export class ClockStopped extends EventEmitter implements IClock {
this.slot = slot;
}
}

export function getMockedClock(): Mocked<IClock> {
return {
get currentSlot() {
return 0;
},
get currentEpoch() {
return 0;
},
currentSlotWithGossipDisparity: undefined,
isCurrentSlotGivenGossipDisparity: vi.fn(),
} as unknown as Mocked<IClock>;
}
18 changes: 3 additions & 15 deletions packages/beacon-node/test/mocks/mockedBeaconChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {Clock} from "../../src/util/clock.js";
import {QueuedStateRegenerator} from "../../src/chain/regen/index.js";
import {ShufflingCache} from "../../src/chain/shufflingCache.js";
import {getMockedLogger} from "./loggerMock.js";
import {getMockedClock} from "./clock.js";

export type MockedBeaconChain = Mocked<BeaconChain> & {
logger: Mocked<Logger>;
Expand Down Expand Up @@ -105,25 +106,12 @@ vi.mock("../../src/chain/chain.js", async (importActual) => {
const BeaconChain = vi.fn().mockImplementation(({clock, genesisTime, config}: MockedBeaconChainOptions) => {
const logger = getMockedLogger();

const clk =
clock === "real"
? new Clock({config, genesisTime: 0, signal: new AbortController().signal})
: {
get currentSlot() {
return 0;
},
get currentEpoch() {
return 0;
},
currentSlotWithGossipDisparity: undefined,
isCurrentSlotGivenGossipDisparity: vi.fn(),
};

return {
config,
opts: {},
genesisTime,
clock: clk,
clock:
clock === "real" ? new Clock({config, genesisTime, signal: new AbortController().signal}) : getMockedClock(),
forkChoice: getMockedForkChoice(),
executionEngine: {
notifyForkchoiceUpdate: vi.fn(),
Expand Down

0 comments on commit c0887de

Please sign in to comment.