Skip to content

Commit

Permalink
Fix e2e test fork aware
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Dec 6, 2022
1 parent a53cdf9 commit 76b01f7
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 418 deletions.
25 changes: 19 additions & 6 deletions packages/beacon-node/src/network/reqresp/ReqRespBeaconNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export class ReqRespBeaconNode extends ReqResp implements IReqRespBeaconNode {
private readonly networkEventBus: INetworkEventBus;
private readonly peersData: PeersData;

/** Track registered fork to only send to known protocols */
private currentRegisteredFork: ForkSeq = ForkSeq.phase0;

private readonly config: IBeaconConfig;
protected readonly logger: ILogger;

Expand Down Expand Up @@ -113,6 +116,8 @@ export class ReqRespBeaconNode extends ReqResp implements IReqRespBeaconNode {
}

registerProtocolsAtFork(fork: ForkName): void {
this.currentRegisteredFork = ForkSeq[fork];

const mustSubscribeProtocols = this.getProtocolsAtFork(fork);
const mustSubscribeProtocolIDs = new Set(mustSubscribeProtocols.map((protocol) => this.formatProtocolID(protocol)));

Expand Down Expand Up @@ -158,10 +163,16 @@ export class ReqRespBeaconNode extends ReqResp implements IReqRespBeaconNode {
);
}

async metadata(peerId: PeerId, fork?: ForkName): Promise<allForks.Metadata> {
// Only request V1 if forcing phase0 fork. It's safe to not specify `fork` and let stream negotiation pick the version
const versions = fork === ForkName.phase0 ? [Version.V1] : [Version.V2, Version.V1];
return collectExactOne(this.sendRequest<null, allForks.Metadata>(peerId, ReqRespMethod.Metadata, versions, null));
async metadata(peerId: PeerId): Promise<allForks.Metadata> {
return collectExactOne(
this.sendRequest<null, allForks.Metadata>(
peerId,
ReqRespMethod.Metadata,
// Before altair, prioritize V2. After altair only request V2
this.currentRegisteredFork >= ForkSeq.altair ? [Version.V2] : [(Version.V2, Version.V1)],
null
)
);
}

async beaconBlocksByRange(
Expand All @@ -172,7 +183,8 @@ export class ReqRespBeaconNode extends ReqResp implements IReqRespBeaconNode {
this.sendRequest<phase0.BeaconBlocksByRangeRequest, allForks.SignedBeaconBlock>(
peerId,
ReqRespMethod.BeaconBlocksByRange,
[Version.V2, Version.V1], // Prioritize V2
// Before altair, prioritize V2. After altair only request V2
this.currentRegisteredFork >= ForkSeq.altair ? [Version.V2] : [(Version.V2, Version.V1)],
request
),
request
Expand All @@ -187,7 +199,8 @@ export class ReqRespBeaconNode extends ReqResp implements IReqRespBeaconNode {
this.sendRequest<phase0.BeaconBlocksByRootRequest, allForks.SignedBeaconBlock>(
peerId,
ReqRespMethod.BeaconBlocksByRoot,
[Version.V2, Version.V1], // Prioritize V2
// Before altair, prioritize V2. After altair only request V2
this.currentRegisteredFork >= ForkSeq.altair ? [Version.V2] : [(Version.V2, Version.V1)],
request
),
request.length
Expand Down
3 changes: 1 addition & 2 deletions packages/beacon-node/src/network/reqresp/interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {PeerId} from "@libp2p/interface-peer-id";
import {ForkName} from "@lodestar/params";
import {allForks, altair, phase0} from "@lodestar/types";

export interface IReqRespBeaconNode {
Expand All @@ -8,7 +7,7 @@ export interface IReqRespBeaconNode {
status(peerId: PeerId, request: phase0.Status): Promise<phase0.Status>;
goodbye(peerId: PeerId, request: phase0.Goodbye): Promise<void>;
ping(peerId: PeerId): Promise<phase0.Ping>;
metadata(peerId: PeerId, fork?: ForkName): Promise<allForks.Metadata>;
metadata(peerId: PeerId): Promise<allForks.Metadata>;
beaconBlocksByRange(
peerId: PeerId,
request: phase0.BeaconBlocksByRangeRequest
Expand Down
69 changes: 38 additions & 31 deletions packages/beacon-node/test/e2e/network/reqresp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import {PeerId} from "@libp2p/interface-peer-id";
import {createSecp256k1PeerId} from "@libp2p/peer-id-factory";
import {expect} from "chai";
import {BitArray} from "@chainsafe/ssz";
import {createIBeaconConfig} from "@lodestar/config";
import {createIBeaconConfig, createIChainForkConfig, IChainForkConfig} from "@lodestar/config";
import {chainConfig} from "@lodestar/config/default";
import {ForkName} from "@lodestar/params";
import {
Encoding,
RequestError,
Expand All @@ -13,27 +12,23 @@ import {
HandlerTypeFromMessage,
EncodedPayloadType,
EncodedPayload,
ContextBytesType,
} from "@lodestar/reqresp";
import * as messages from "@lodestar/reqresp/messages";
import {altair, phase0, Root, ssz} from "@lodestar/types";
import {allForks, altair, phase0, Root, ssz} from "@lodestar/types";
import {sleep as _sleep} from "@lodestar/utils";
import {GossipHandlers} from "../../../src/network/gossip/index.js";
import {Network, ReqRespBeaconNodeOpts} from "../../../src/network/index.js";
import {defaultNetworkOptions, INetworkOptions} from "../../../src/network/options.js";
import {ReqRespHandlers} from "../../../src/network/reqresp/handlers/index.js";
import {ReqRespMethod} from "../../../src/network/reqresp/types.js";
import {
blocksToReqRespBlockResponses,
generateEmptyReqRespBlockResponse,
generateEmptySignedBlock,
} from "../../utils/block.js";
import {expectRejectedWithLodestarError} from "../../utils/errors.js";
import {testLogger} from "../../utils/logger.js";
import {MockBeaconChain} from "../../utils/mocks/chain/chain.js";
import {connect, createNode, onPeerConnect} from "../../utils/network.js";
import {generateState} from "../../utils/state.js";
import {StubbedBeaconDb} from "../../utils/stub/index.js";
import {arrToSource, generateEmptySignedBlocks} from "../../unit/network/reqresp/utils.js";
import {arrToSource} from "../../unit/network/reqresp/utils.js";
import {defaultRateLimiterOpts} from "../../../src/network/reqresp/inboundRateLimiter.js";

/* eslint-disable require-yield, @typescript-eslint/naming-convention */
Expand All @@ -55,12 +50,12 @@ describe("network / ReqResp", function () {
};

// Schedule ALTAIR_FORK_EPOCH to trigger registering lightclient ReqResp protocols immediately
const config: typeof chainConfig = {
const config = createIChainForkConfig({
...chainConfig,
ALTAIR_FORK_EPOCH: 0,
};
});

const state = generateState();
const state = generateState({}, config);
const beaconConfig = createIBeaconConfig(config, state.genesisValidatorsRoot);
const chain = new MockBeaconChain({genesisTime: 0, chainId: 0, networkId: BigInt(0), state, config: beaconConfig});
const db = new StubbedBeaconDb();
Expand Down Expand Up @@ -150,18 +145,6 @@ describe("network / ReqResp", function () {
expect(pong.toString()).to.deep.equal(expectedPong.toString(), "Wrong response body");
});

it("should send/receive a metadata message - phase0", async function () {
const [netA, netB] = await createAndConnectPeers();

const metadata: phase0.Metadata = {
seqNumber: netB.metadata.seqNumber,
attnets: netB.metadata.attnets,
};

const receivedMetadata = await netA.reqResp.metadata(netB.peerId, ForkName.phase0);
expect(receivedMetadata).to.deep.equal(metadata, "Wrong response body");
});

it("should send/receive a metadata message - altair", async function () {
const [netA, netB] = await createAndConnectPeers();

Expand Down Expand Up @@ -200,14 +183,16 @@ describe("network / ReqResp", function () {
const req: phase0.BeaconBlocksByRangeRequest = {startSlot: 0, step: 1, count: 2};
const blocks: phase0.SignedBeaconBlock[] = [];
for (let slot = req.startSlot; slot < req.count; slot++) {
const block = generateEmptySignedBlock();
const block = config.getForkTypes(slot).SignedBeaconBlock.defaultValue();
block.message.slot = slot;
blocks.push(block);
}

const [netA, netB] = await createAndConnectPeers({
onBeaconBlocksByRange: async function* () {
yield* arrToSource(blocksToReqRespBlockResponses(blocks));
for (const block of blocks) {
yield wrapBlockAsEncodedPayload(config, block);
}
} as HandlerTypeFromMessage<typeof messages.BeaconBlocksByRange>,
});

Expand Down Expand Up @@ -328,7 +313,11 @@ describe("network / ReqResp", function () {

const [netA, netB] = await createAndConnectPeers({
onBeaconBlocksByRange: async function* onRequest() {
yield* arrToSource(blocksToReqRespBlockResponses(generateEmptySignedBlocks(2)));
for (let slot = 0; slot < 2; slot++) {
const block = config.getForkTypes(slot).SignedBeaconBlock.defaultValue();
block.message.slot = slot;
yield wrapBlockAsEncodedPayload(config, block);
}
throw Error(testErrorMessage);
} as HandlerTypeFromMessage<typeof messages.BeaconBlocksByRange>,
});
Expand All @@ -350,7 +339,7 @@ describe("network / ReqResp", function () {
onBeaconBlocksByRange: async function* onRequest() {
// Wait for too long before sending first response chunk
await sleep(ttfbTimeoutMs * 10);
yield generateEmptyReqRespBlockResponse();
yield config.getForkTypes(0).SignedBeaconBlock.defaultValue();
} as HandlerTypeFromMessage<typeof messages.BeaconBlocksByRange>,
},
{ttfbTimeoutMs}
Expand All @@ -371,10 +360,10 @@ describe("network / ReqResp", function () {
const [netA, netB] = await createAndConnectPeers(
{
onBeaconBlocksByRange: async function* onRequest() {
yield generateEmptyReqRespBlockResponse();
yield getEmptyEncodedPayloadSignedBeaconBlock(config);
// Wait for too long before sending second response chunk
await sleep(respTimeoutMs * 5);
yield generateEmptyReqRespBlockResponse();
yield getEmptyEncodedPayloadSignedBeaconBlock(config);
} as HandlerTypeFromMessage<typeof messages.BeaconBlocksByRange>,
},
{respTimeoutMs}
Expand Down Expand Up @@ -412,7 +401,7 @@ describe("network / ReqResp", function () {
const [netA, netB] = await createAndConnectPeers(
{
onBeaconBlocksByRange: async function* onRequest() {
yield generateEmptyReqRespBlockResponse();
yield getEmptyEncodedPayloadSignedBeaconBlock(config);
await sleep(100000000);
} as HandlerTypeFromMessage<typeof messages.BeaconBlocksByRange>,
},
Expand All @@ -433,3 +422,21 @@ describe("network / ReqResp", function () {
function formatMetadata(method: ReqRespMethod, encoding: Encoding, peer: PeerId): IRequestErrorMetadata {
return {method, encoding, peer: peer.toString()};
}

function getEmptyEncodedPayloadSignedBeaconBlock(config: IChainForkConfig): EncodedPayload<allForks.SignedBeaconBlock> {
return wrapBlockAsEncodedPayload(config, config.getForkTypes(0).SignedBeaconBlock.defaultValue());
}

function wrapBlockAsEncodedPayload(
config: IChainForkConfig,
block: allForks.SignedBeaconBlock
): EncodedPayload<allForks.SignedBeaconBlock> {
return {
type: EncodedPayloadType.bytes,
bytes: config.getForkTypes(block.message.slot).SignedBeaconBlock.serialize(block),
contextBytes: {
type: ContextBytesType.ForkDigest,
forkSlot: block.message.slot,
},
};
}
2 changes: 1 addition & 1 deletion packages/beacon-node/test/spec/presets/epoch_processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as epochFns from "@lodestar/state-transition/epoch";
import {ssz} from "@lodestar/types";
import {createCachedBeaconStateTest} from "../../utils/cachedBeaconState.js";
import {expectEqualBeaconState, inputTypeSszTreeViewDU} from "../utils/expectEqualBeaconState.js";
import {getConfig} from "../utils/getConfig.js";
import {getConfig} from "../../utils/config.js";
import {TestRunnerFn} from "../utils/types.js";
import {assertCorrectProgressiveBalances} from "../config.js";

Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/test/spec/presets/finality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {ForkName} from "@lodestar/params";
import {createCachedBeaconStateTest} from "../../utils/cachedBeaconState.js";
import {expectEqualBeaconState, inputTypeSszTreeViewDU} from "../utils/expectEqualBeaconState.js";
import {shouldVerify, TestRunnerFn} from "../utils/types.js";
import {getConfig} from "../utils/getConfig.js";
import {getConfig} from "../../utils/config.js";
import {assertCorrectProgressiveBalances} from "../config.js";

/* eslint-disable @typescript-eslint/naming-convention */
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/test/spec/presets/fork_choice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {createIBeaconConfig} from "@lodestar/config";
import {BeaconChain, ChainEvent} from "../../../src/chain/index.js";
import {createCachedBeaconStateTest} from "../../utils/cachedBeaconState.js";
import {testLogger} from "../../utils/logger.js";
import {getConfig} from "../utils/getConfig.js";
import {getConfig} from "../../utils/config.js";
import {TestRunnerFn} from "../utils/types.js";
import {Eth1ForBlockProductionDisabled} from "../../../src/eth1/index.js";
import {ExecutionEngineMock} from "../../../src/execution/index.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/test/spec/presets/genesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {ForkName} from "@lodestar/params";

import {expectEqualBeaconState} from "../utils/expectEqualBeaconState.js";
import {TestRunnerFn} from "../utils/types.js";
import {getConfig} from "../utils/getConfig.js";
import {getConfig} from "../../utils/config.js";
// The aim of the genesis tests is to provide a baseline to test genesis-state initialization and test if the
// proposed genesis-validity conditions are working.

Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/test/spec/presets/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {ForkName} from "@lodestar/params";

import {createCachedBeaconStateTest} from "../../utils/cachedBeaconState.js";
import {expectEqualBeaconState, inputTypeSszTreeViewDU} from "../utils/expectEqualBeaconState.js";
import {getConfig} from "../utils/getConfig.js";
import {getConfig} from "../../utils/config.js";
import {BaseSpecTest, shouldVerify, TestRunnerFn} from "../utils/types.js";

/* eslint-disable @typescript-eslint/naming-convention */
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/test/spec/presets/rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {VectorCompositeType} from "@chainsafe/ssz";
import {ssz} from "@lodestar/types";
import {createCachedBeaconStateTest} from "../../utils/cachedBeaconState.js";
import {inputTypeSszTreeViewDU} from "../utils/expectEqualBeaconState.js";
import {getConfig} from "../utils/getConfig.js";
import {getConfig} from "../../utils/config.js";
import {TestRunnerFn} from "../utils/types.js";
import {assertCorrectProgressiveBalances} from "../config.js";

Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/test/spec/presets/sanity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {bnToNum} from "@lodestar/utils";
import {createCachedBeaconStateTest} from "../../utils/cachedBeaconState.js";
import {expectEqualBeaconState, inputTypeSszTreeViewDU} from "../utils/expectEqualBeaconState.js";
import {shouldVerify, TestRunnerFn} from "../utils/types.js";
import {getConfig} from "../utils/getConfig.js";
import {getConfig} from "../../utils/config.js";
import {assertCorrectProgressiveBalances} from "../config.js";

/* eslint-disable @typescript-eslint/naming-convention */
Expand Down
32 changes: 0 additions & 32 deletions packages/beacon-node/test/spec/utils/getConfig.ts

This file was deleted.

5 changes: 3 additions & 2 deletions packages/beacon-node/test/unit/chain/prepareNextSlot.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {expect} from "chai";
import sinon, {SinonStubbedInstance} from "sinon";
import {config} from "@lodestar/config/default";
import {ForkChoice, ProtoBlock} from "@lodestar/fork-choice";
import {WinstonLogger} from "@lodestar/utils";
import {ForkSeq, SLOTS_PER_EPOCH} from "@lodestar/params";
import {ForkName, ForkSeq, SLOTS_PER_EPOCH} from "@lodestar/params";
import {IChainForkConfig} from "@lodestar/config";
import {BeaconChain, ChainEventEmitter} from "../../../src/chain/index.js";
import {IBeaconChain} from "../../../src/chain/interface.js";
Expand All @@ -17,12 +16,14 @@ import {PayloadIdCache} from "../../../src/execution/engine/payloadIdCache.js";
import {ExecutionEngineHttp} from "../../../src/execution/engine/http.js";
import {IExecutionEngine} from "../../../src/execution/engine/interface.js";
import {StubbedChainMutable} from "../../utils/stub/index.js";
import {getConfig} from "../../utils/config.js";

type StubbedChain = StubbedChainMutable<"clock" | "forkChoice" | "emitter" | "regen">;

describe("PrepareNextSlot scheduler", () => {
const sandbox = sinon.createSandbox();
const abortController = new AbortController();
const config = getConfig(ForkName.bellatrix, 0);

let chainStub: StubbedChain;
let scheduler: PrepareNextSlotScheduler;
Expand Down
Loading

0 comments on commit 76b01f7

Please sign in to comment.