Skip to content

Commit

Permalink
Fix e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarhussain committed Jan 19, 2024
1 parent 2efa29c commit f4159ac
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 147 deletions.
3 changes: 2 additions & 1 deletion packages/beacon-node/test/e2e/network/gossipsub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion packages/beacon-node/test/e2e/network/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 2 additions & 1 deletion packages/beacon-node/test/e2e/network/reqresp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof prioritizePeers>;
Expand Down
143 changes: 27 additions & 116 deletions packages/beacon-node/test/utils/network.ts
Original file line number Diff line number Diff line change
@@ -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<Libp2p> {
const peerId = inPeerId || (await createSecp256k1PeerId());
Expand All @@ -40,101 +24,6 @@ export async function createNetworkModules(
};
}

export type NetworkForTestOpts = {
startSlot?: number;
opts?: Partial<NetworkOptions>;
gossipHandlersPartial?: Partial<GossipHandlers>;
getReqRespHandler?: GetReqRespHandlerFn;
};

export async function getNetworkForTest(
loggerId: string,
config: ChainForkConfig,
opts: NetworkForTestOpts
): Promise<[network: Network, closeAll: () => Promise<void>]> {
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<NetworkInitModules, "opts" | "peerId" | "logger"> = {
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<PeerIdStr> {
return (await net.getNetworkIdentity()).peerId;
}
Expand Down Expand Up @@ -162,3 +51,25 @@ export function onPeerConnect(network: Network): Promise<void> {
export function onPeerDisconnect(network: Network): Promise<void> {
return new Promise<void>((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;
}
24 changes: 0 additions & 24 deletions packages/beacon-node/test/utils/networkData.ts

This file was deleted.

109 changes: 109 additions & 0 deletions packages/beacon-node/test/utils/networkWithMockDb.ts
Original file line number Diff line number Diff line change
@@ -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<NetworkOptions>;
gossipHandlersPartial?: Partial<GossipHandlers>;
getReqRespHandler?: GetReqRespHandlerFn;
};

export async function getNetworkForTest(
loggerId: string,
config: ChainForkConfig,
opts: NetworkForTestOpts
): Promise<[network: Network, closeAll: () => Promise<void>]> {
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<NetworkInitModules, "opts" | "peerId" | "logger"> = {
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();
},
];
}

0 comments on commit f4159ac

Please sign in to comment.