diff --git a/packages/beacon-node/test/e2e/network/gossipsub.test.ts b/packages/beacon-node/test/e2e/network/gossipsub.test.ts index 2ecf50473381..0ffab720598c 100644 --- a/packages/beacon-node/test/e2e/network/gossipsub.test.ts +++ b/packages/beacon-node/test/e2e/network/gossipsub.test.ts @@ -5,7 +5,8 @@ import {computeStartSlotAtEpoch} from "@lodestar/state-transition"; import {ssz} from "@lodestar/types"; import {Network} from "../../../src/network/index.js"; import {GossipType, GossipHandlers, GossipHandlerParamGeneric} from "../../../src/network/gossip/index.js"; -import {connect, onPeerConnect, getNetworkForTest} from "../../utils/network.js"; +import {getNetworkForTest} from "../../utils/networkWithMockDb.js"; +import {connect, onPeerConnect} from "../../utils/network.js"; describe( "gossipsub / main thread", diff --git a/packages/beacon-node/test/e2e/network/network.test.ts b/packages/beacon-node/test/e2e/network/network.test.ts index 28b11db43e9a..40bf6b7e14af 100644 --- a/packages/beacon-node/test/e2e/network/network.test.ts +++ b/packages/beacon-node/test/e2e/network/network.test.ts @@ -5,7 +5,8 @@ import {phase0} from "@lodestar/types"; import {sleep} from "@lodestar/utils"; import {Network, NetworkEvent, ReqRespMethod} from "../../../src/network/index.js"; import {GoodByeReasonCode} from "../../../src/constants/index.js"; -import {connect, disconnect, onPeerConnect, onPeerDisconnect, getNetworkForTest} from "../../utils/network.js"; +import {connect, disconnect, onPeerConnect, onPeerDisconnect} from "../../utils/network.js"; +import {getNetworkForTest} from "../../utils/networkWithMockDb.js"; import {getValidPeerId} from "../../utils/peer.js"; describe( diff --git a/packages/beacon-node/test/e2e/network/peers/peerManager.test.ts b/packages/beacon-node/test/e2e/network/peers/peerManager.test.ts index 36d5c7a4d489..8eb0ecdd1ba4 100644 --- a/packages/beacon-node/test/e2e/network/peers/peerManager.test.ts +++ b/packages/beacon-node/test/e2e/network/peers/peerManager.test.ts @@ -11,7 +11,7 @@ import {PeerRpcScoreStore, PeerManager, IReqRespBeaconNodePeerManager} from "../ import {Eth2Gossipsub, getConnectionsMap, NetworkEvent, NetworkEventBus} from "../../../../src/network/index.js"; import {PeersData} from "../../../../src/network/peers/peersData.js"; import {createNode} from "../../../utils/network.js"; -import {getAttnets, getSyncnets} from "../../../utils/networkData.js"; +import {getAttnets, getSyncnets} from "../../../utils/network.js"; import {generateState} from "../../../utils/state.js"; import {waitForEvent} from "../../../utils/events/resolver.js"; import {testLogger} from "../../../utils/logger.js"; diff --git a/packages/beacon-node/test/e2e/network/reqresp.test.ts b/packages/beacon-node/test/e2e/network/reqresp.test.ts index 37055654a127..855dc7046203 100644 --- a/packages/beacon-node/test/e2e/network/reqresp.test.ts +++ b/packages/beacon-node/test/e2e/network/reqresp.test.ts @@ -7,7 +7,8 @@ import {allForks, altair, phase0, Root, ssz} from "@lodestar/types"; import {sleep as _sleep} from "@lodestar/utils"; import {Network, ReqRespBeaconNodeOpts} from "../../../src/network/index.js"; import {expectRejectedWithLodestarError} from "../../utils/errors.js"; -import {connect, getNetworkForTest, getPeerIdOf, onPeerConnect} from "../../utils/network.js"; +import {connect, getPeerIdOf, onPeerConnect} from "../../utils/network.js"; +import {getNetworkForTest} from "../../utils/networkWithMockDb.js"; import {arrToSource} from "../../unit/network/reqresp/utils.js"; import {GetReqRespHandlerFn, ReqRespMethod} from "../../../src/network/reqresp/types.js"; import {PeerIdStr} from "../../../src/util/peerId.js"; diff --git a/packages/beacon-node/test/e2e/sync/unknownBlockSync.test.ts b/packages/beacon-node/test/e2e/sync/unknownBlockSync.test.ts index a51beaf7b961..0ee20a32f0f0 100644 --- a/packages/beacon-node/test/e2e/sync/unknownBlockSync.test.ts +++ b/packages/beacon-node/test/e2e/sync/unknownBlockSync.test.ts @@ -1,4 +1,4 @@ -import {describe, it, afterEach} from "vitest"; +import {describe, it, afterEach, vi} from "vitest"; import {fromHexString} from "@chainsafe/ssz"; import {ChainConfig} from "@lodestar/config"; import {phase0} from "@lodestar/types"; diff --git a/packages/beacon-node/test/perf/network/peers/util/prioritizePeers.test.ts b/packages/beacon-node/test/perf/network/peers/util/prioritizePeers.test.ts index 737ec2da81cd..a0d648a161ea 100644 --- a/packages/beacon-node/test/perf/network/peers/util/prioritizePeers.test.ts +++ b/packages/beacon-node/test/perf/network/peers/util/prioritizePeers.test.ts @@ -5,7 +5,7 @@ import {ATTESTATION_SUBNET_COUNT, SYNC_COMMITTEE_SUBNET_COUNT} from "@lodestar/p import {altair, phase0} from "@lodestar/types"; import {defaultNetworkOptions} from "../../../../../src/network/options.js"; import {prioritizePeers, RequestedSubnet} from "../../../../../src/network/peers/utils/index.js"; -import {getAttnets, getSyncnets} from "../../../../utils/networkData.js"; +import {getAttnets, getSyncnets} from "../../../../utils/network.js"; describe("prioritizePeers", () => { const seedPeers: {id: PeerId; attnets: phase0.AttestationSubnets; syncnets: altair.SyncSubnets; score: number}[] = []; diff --git a/packages/beacon-node/test/unit/network/peers/priorization.test.ts b/packages/beacon-node/test/unit/network/peers/priorization.test.ts index bf04e24e1687..3ec770051275 100644 --- a/packages/beacon-node/test/unit/network/peers/priorization.test.ts +++ b/packages/beacon-node/test/unit/network/peers/priorization.test.ts @@ -9,7 +9,7 @@ import { PrioritizePeersOpts, sortPeersToPrune, } from "../../../../src/network/peers/utils/prioritizePeers.js"; -import {getAttnets, getSyncnets} from "../../../utils/networkData.js"; +import {getAttnets, getSyncnets} from "../../../utils/network.js"; import {RequestedSubnet} from "../../../../src/network/peers/utils/index.js"; type Result = ReturnType; diff --git a/packages/beacon-node/test/utils/network.ts b/packages/beacon-node/test/utils/network.ts index 0fc661b6d30a..56c831b269f5 100644 --- a/packages/beacon-node/test/utils/network.ts +++ b/packages/beacon-node/test/utils/network.ts @@ -1,28 +1,12 @@ +import {BitArray} from "@chainsafe/ssz"; import {PeerId} from "@libp2p/interface"; import {createSecp256k1PeerId} from "@libp2p/peer-id-factory"; -import {ssz} from "@lodestar/types"; -import {ChainForkConfig, createBeaconConfig} from "@lodestar/config"; -import { - GossipHandlers, - INetwork, - Network, - NetworkEvent, - NetworkInitModules, - getReqRespHandlers, -} from "../../src/network/index.js"; -import {createNodeJsLibp2p} from "../../src/network/libp2p/index.js"; +import {ATTESTATION_SUBNET_COUNT, SYNC_COMMITTEE_SUBNET_COUNT} from "@lodestar/params"; +import {INetwork, Network, NetworkEvent} from "../../src/network/index.js"; import {Libp2p} from "../../src/network/interface.js"; -import {GetReqRespHandlerFn} from "../../src/network/reqresp/types.js"; -import {Eth1ForBlockProductionDisabled} from "../../src/eth1/index.js"; -import {defaultNetworkOptions, NetworkOptions} from "../../src/network/options.js"; -import {BeaconChain} from "../../src/chain/chain.js"; -import {ExecutionEngineDisabled} from "../../src/execution/index.js"; +import {createNodeJsLibp2p} from "../../src/network/libp2p/index.js"; +import {NetworkOptions, defaultNetworkOptions} from "../../src/network/options.js"; import {PeerIdStr} from "../../src/util/peerId.js"; -import {getMockedBeaconDb} from "../mocks/mockedBeaconDb.js"; -import {testLogger} from "./logger.js"; -import {generateState} from "./state.js"; -import {ClockStatic} from "./clock.js"; -import {createCachedBeaconStateTest} from "./cachedBeaconState.js"; export async function createNode(multiaddr: string, inPeerId?: PeerId): Promise { const peerId = inPeerId || (await createSecp256k1PeerId()); @@ -40,101 +24,6 @@ export async function createNetworkModules( }; } -export type NetworkForTestOpts = { - startSlot?: number; - opts?: Partial; - gossipHandlersPartial?: Partial; - getReqRespHandler?: GetReqRespHandlerFn; -}; - -export async function getNetworkForTest( - loggerId: string, - config: ChainForkConfig, - opts: NetworkForTestOpts -): Promise<[network: Network, closeAll: () => Promise]> { - const logger = testLogger(loggerId); - const startSlot = opts.startSlot ?? 0; - - const block = ssz.phase0.SignedBeaconBlock.defaultValue(); - const state = generateState( - { - slot: startSlot, - finalizedCheckpoint: { - epoch: 0, - root: ssz.phase0.BeaconBlock.hashTreeRoot(block.message), - }, - }, - config - ); - - const beaconConfig = createBeaconConfig(config, state.genesisValidatorsRoot); - const db = getMockedBeaconDb(); - - const chain = new BeaconChain( - { - safeSlotsToImportOptimistically: 0, - archiveStateEpochFrequency: 0, - suggestedFeeRecipient: "", - blsVerifyAllMainThread: true, - disableOnBlockError: true, - disableArchiveOnCheckpoint: true, - disableLightClientServerOnImportBlockHead: true, - disablePrepareNextSlot: true, - minSameMessageSignatureSetsToBatch: 32, - }, - { - config: beaconConfig, - db, - logger, - // eslint-disable-next-line @typescript-eslint/no-empty-function - processShutdownCallback: () => {}, - // set genesis time so that we are at ALTAIR_FORK_EPOCH - // mock timer does not work on worker thread - clock: new ClockStatic(startSlot, Math.floor(Date.now() / 1000) - startSlot * beaconConfig.SECONDS_PER_SLOT), - metrics: null, - anchorState: createCachedBeaconStateTest(state, beaconConfig), - eth1: new Eth1ForBlockProductionDisabled(), - executionEngine: new ExecutionEngineDisabled(), - } - ); - - const modules: Omit = { - config: beaconConfig, - chain, - db, - getReqRespHandler: opts.getReqRespHandler ?? getReqRespHandlers({db, chain}), - gossipHandlers: opts.gossipHandlersPartial as GossipHandlers, - metrics: null, - }; - - const network = await Network.init({ - ...modules, - peerId: await createSecp256k1PeerId(), - opts: { - ...defaultNetworkOptions, - maxPeers: 1, - targetPeers: 1, - bootMultiaddrs: [], - localMultiaddrs: ["/ip4/127.0.0.1/tcp/0"], - discv5FirstQueryDelayMs: 0, - discv5: null, - skipParamsLog: true, - // Disable rate limiting - rateLimitMultiplier: 0, - ...opts.opts, - }, - logger, - }); - - return [ - network, - async function closeAll() { - await network.close(); - await chain.close(); - }, - ]; -} - export async function getPeerIdOf(net: INetwork): Promise { return (await net.getNetworkIdentity()).peerId; } @@ -162,3 +51,25 @@ export function onPeerConnect(network: Network): Promise { export function onPeerDisconnect(network: Network): Promise { return new Promise((resolve) => network.events.on(NetworkEvent.peerDisconnected, () => resolve())); } + +/** + * Generate valid filled attnets BitVector + */ +export function getAttnets(subnetIds: number[] = []): BitArray { + const attnets = BitArray.fromBitLen(ATTESTATION_SUBNET_COUNT); + for (const subnetId of subnetIds) { + attnets.set(subnetId, true); + } + return attnets; +} + +/** + * Generate valid filled syncnets BitVector + */ +export function getSyncnets(subnetIds: number[] = []): BitArray { + const syncnets = BitArray.fromBitLen(SYNC_COMMITTEE_SUBNET_COUNT); + for (const subnetId of subnetIds) { + syncnets.set(subnetId, true); + } + return syncnets; +} diff --git a/packages/beacon-node/test/utils/networkData.ts b/packages/beacon-node/test/utils/networkData.ts deleted file mode 100644 index fe04a2a91855..000000000000 --- a/packages/beacon-node/test/utils/networkData.ts +++ /dev/null @@ -1,24 +0,0 @@ -import {BitArray} from "@chainsafe/ssz"; -import {ATTESTATION_SUBNET_COUNT, SYNC_COMMITTEE_SUBNET_COUNT} from "@lodestar/params"; - -/** - * Generate valid filled attnets BitVector - */ -export function getAttnets(subnetIds: number[] = []): BitArray { - const attnets = BitArray.fromBitLen(ATTESTATION_SUBNET_COUNT); - for (const subnetId of subnetIds) { - attnets.set(subnetId, true); - } - return attnets; -} - -/** - * Generate valid filled syncnets BitVector - */ -export function getSyncnets(subnetIds: number[] = []): BitArray { - const syncnets = BitArray.fromBitLen(SYNC_COMMITTEE_SUBNET_COUNT); - for (const subnetId of subnetIds) { - syncnets.set(subnetId, true); - } - return syncnets; -} diff --git a/packages/beacon-node/test/utils/networkWithMockDb.ts b/packages/beacon-node/test/utils/networkWithMockDb.ts new file mode 100644 index 000000000000..3eea47892a0f --- /dev/null +++ b/packages/beacon-node/test/utils/networkWithMockDb.ts @@ -0,0 +1,109 @@ +import {createSecp256k1PeerId} from "@libp2p/peer-id-factory"; +import {ChainForkConfig, createBeaconConfig} from "@lodestar/config"; +import {ssz} from "@lodestar/types"; +import {BeaconChain} from "../../src/chain/chain.js"; +import {Eth1ForBlockProductionDisabled} from "../../src/eth1/index.js"; +import {ExecutionEngineDisabled} from "../../src/execution/index.js"; +import {GossipHandlers, Network, NetworkInitModules, getReqRespHandlers} from "../../src/network/index.js"; +import {NetworkOptions, defaultNetworkOptions} from "../../src/network/options.js"; +import {GetReqRespHandlerFn} from "../../src/network/reqresp/types.js"; +import {getMockedBeaconDb} from "../mocks/mockedBeaconDb.js"; +import {createCachedBeaconStateTest} from "./cachedBeaconState.js"; +import {ClockStatic} from "./clock.js"; +import {testLogger} from "./logger.js"; +import {generateState} from "./state.js"; + +export type NetworkForTestOpts = { + startSlot?: number; + opts?: Partial; + gossipHandlersPartial?: Partial; + getReqRespHandler?: GetReqRespHandlerFn; +}; + +export async function getNetworkForTest( + loggerId: string, + config: ChainForkConfig, + opts: NetworkForTestOpts +): Promise<[network: Network, closeAll: () => Promise]> { + const logger = testLogger(loggerId); + const startSlot = opts.startSlot ?? 0; + + const block = ssz.phase0.SignedBeaconBlock.defaultValue(); + const state = generateState( + { + slot: startSlot, + finalizedCheckpoint: { + epoch: 0, + root: ssz.phase0.BeaconBlock.hashTreeRoot(block.message), + }, + }, + config + ); + + const beaconConfig = createBeaconConfig(config, state.genesisValidatorsRoot); + const db = getMockedBeaconDb(); + + const chain = new BeaconChain( + { + safeSlotsToImportOptimistically: 0, + archiveStateEpochFrequency: 0, + suggestedFeeRecipient: "", + blsVerifyAllMainThread: true, + disableOnBlockError: true, + disableArchiveOnCheckpoint: true, + disableLightClientServerOnImportBlockHead: true, + disablePrepareNextSlot: true, + minSameMessageSignatureSetsToBatch: 32, + }, + { + config: beaconConfig, + db, + logger, + // eslint-disable-next-line @typescript-eslint/no-empty-function + processShutdownCallback: () => {}, + // set genesis time so that we are at ALTAIR_FORK_EPOCH + // mock timer does not work on worker thread + clock: new ClockStatic(startSlot, Math.floor(Date.now() / 1000) - startSlot * beaconConfig.SECONDS_PER_SLOT), + metrics: null, + anchorState: createCachedBeaconStateTest(state, beaconConfig), + eth1: new Eth1ForBlockProductionDisabled(), + executionEngine: new ExecutionEngineDisabled(), + } + ); + + const modules: Omit = { + config: beaconConfig, + chain, + db, + getReqRespHandler: opts.getReqRespHandler ?? getReqRespHandlers({db, chain}), + gossipHandlers: opts.gossipHandlersPartial as GossipHandlers, + metrics: null, + }; + + const network = await Network.init({ + ...modules, + peerId: await createSecp256k1PeerId(), + opts: { + ...defaultNetworkOptions, + maxPeers: 1, + targetPeers: 1, + bootMultiaddrs: [], + localMultiaddrs: ["/ip4/127.0.0.1/tcp/0"], + discv5FirstQueryDelayMs: 0, + discv5: null, + skipParamsLog: true, + // Disable rate limiting + rateLimitMultiplier: 0, + ...opts.opts, + }, + logger, + }); + + return [ + network, + async function closeAll() { + await network.close(); + await chain.close(); + }, + ]; +}