Skip to content

Commit

Permalink
fix the types/test
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Aug 9, 2024
1 parent cf0e7e8 commit dcdb14c
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 24 deletions.
2 changes: 2 additions & 0 deletions packages/beacon-node/test/e2e/network/mdns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {generateState, zeroProtoBlock} from "../../utils/state.js";
import {testLogger} from "../../utils/logger.js";
import {GossipHandlers} from "../../../src/network/gossip/index.js";
import {memoOnce} from "../../utils/cache.js";
import {computeNodeId} from "../../../src/network/subnets/index.js";

let port = 9000;
const mu = "/ip4/127.0.0.1/tcp/0";
Expand Down Expand Up @@ -98,6 +99,7 @@ describe.skip("mdns", function () {
const network = await Network.init({
...modules,
...(await createNetworkModules(mu, peerId, {...opts, mdns: true})),
nodeId: computeNodeId(peerId),
logger,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {generateState} from "../../../utils/state.js";
import {waitForEvent} from "../../../utils/events/resolver.js";
import {testLogger} from "../../../utils/logger.js";
import {getValidPeerId} from "../../../utils/peer.js";
import {IAttnetsService} from "../../../../src/network/subnets/index.js";
import {IAttnetsService, computeNodeId} from "../../../../src/network/subnets/index.js";
import {Clock} from "../../../../src/util/clock.js";
import {LocalStatusCache} from "../../../../src/network/statusCache.js";

Expand Down Expand Up @@ -82,6 +82,7 @@ describe("network / peers / PeerManager", function () {
syncnetsService: mockSubnetsService,
gossip: {getScore: () => 0, scoreParams: {decayInterval: 1000}} as unknown as Eth2Gossipsub,
peersData: new PeersData(),
nodeId: computeNodeId(peerId1),
},
{
targetPeers: 30,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {rimraf} from "rimraf";
import {describe, it, expect, beforeEach, afterEach, beforeAll} from "vitest";
import {ssz} from "@lodestar/types";
import {ByteVectorType} from "@chainsafe/ssz";
import {ssz, electra} from "@lodestar/types";
import {createChainForkConfig} from "@lodestar/config";
import {LevelDbController} from "@lodestar/db";
import {NUMBER_OF_COLUMNS} from "@lodestar/params";
Expand Down Expand Up @@ -68,15 +69,18 @@ describe("block archive repository", function () {
(ssz.electra.Cell.fixedSize + ssz.deneb.KZGCommitment.fixedSize + ssz.deneb.KZGProof.fixedSize);

const numColumns = NUMBER_OF_COLUMNS;
const custodyColumns = new Uint8Array(numColumns);
const blobsLen = (singedBlock.message as electra.BeaconBlock).body.blobKzgCommitments.length;

// const dataColumnsSize = ssz.electra.DataColumnSidecar.minSize + blobsLen * (ssz.electra.Cell.fixedSize + ssz.deneb.KZGCommitment.fixedSize + ssz.deneb.KZGProof.fixedSize);

// const dataColumnsLen = blockInput.blockData;
const writeData = {
blockRoot,
slot,
numColumns,
blobsLen,
columnsSize,
custodyColumns,
dataColumnSidecars,
dataColumnsIndex: new ByteVectorType(NUMBER_OF_COLUMNS),
dataColumnSidecars: ssz.electra.DataColumnSidecars,
};

await dataColumnRepo.add(writeData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe("beaconBlocksMaybeBlobsByRange", () => {
sendBlobSidecarsByRange: async () => blobSidecars,
} as Partial<INetwork> as INetwork;

const response = await beaconBlocksMaybeBlobsByRange(config, network, peerId, rangeRequest, 0);
const response = await beaconBlocksMaybeBlobsByRange(config, network, peerId, rangeRequest, 0, null);
expect(response).toEqual(expectedResponse);
});
});
Expand Down
8 changes: 4 additions & 4 deletions packages/beacon-node/test/unit/sync/range/batch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe("sync / range / batch", () => {
// retry download: AwaitingDownload -> Downloading
// downloadingSuccess: Downloading -> AwaitingProcessing
batch.startDownloading(peer);
batch.downloadingSuccess(blocksDownloaded);
batch.downloadingSuccess({blocks: blocksDownloaded, pendingDataColumns: null});
expect(batch.state.status).toBe(BatchStatus.AwaitingProcessing);

// startProcessing: AwaitingProcessing -> Processing
Expand All @@ -57,7 +57,7 @@ describe("sync / range / batch", () => {
// retry download + processing: AwaitingDownload -> Downloading -> AwaitingProcessing -> Processing
// processingSuccess: Processing -> AwaitingValidation
batch.startDownloading(peer);
batch.downloadingSuccess(blocksDownloaded);
batch.downloadingSuccess({blocks: blocksDownloaded, pendingDataColumns: null});
batch.startProcessing();
batch.processingSuccess();
expect(batch.state.status).toBe(BatchStatus.AwaitingValidation);
Expand All @@ -68,7 +68,7 @@ describe("sync / range / batch", () => {

// retry download + processing + validation: AwaitingDownload -> Downloading -> AwaitingProcessing -> Processing -> AwaitingValidation
batch.startDownloading(peer);
batch.downloadingSuccess(blocksDownloaded);
batch.downloadingSuccess({blocks: blocksDownloaded, pendingDataColumns: null});
batch.startProcessing();
batch.processingSuccess();
expect(batch.state.status).toBe(BatchStatus.AwaitingValidation);
Expand All @@ -79,7 +79,7 @@ describe("sync / range / batch", () => {
const batch = new Batch(startEpoch, config);

expectThrowsLodestarError(
() => batch.downloadingSuccess(blocksDownloaded),
() => batch.downloadingSuccess({blocks: blocksDownloaded, pendingDataColumns: []}),
new BatchError({
code: BatchErrorCode.WRONG_STATUS,
startEpoch,
Expand Down
24 changes: 16 additions & 8 deletions packages/beacon-node/test/unit/sync/range/chain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ describe("sync / range / chain", () => {
}
};

const downloadBeaconBlocksByRange: SyncChainFns["downloadBeaconBlocksByRange"] = async (peerId, request) => {
const downloadBeaconBlocksByRange: SyncChainFns["downloadBeaconBlocksByRange"] = async (
peer,
request,
partialDownload
) => {
const blocks: BlockInput[] = [];
for (let i = request.startSlot; i < request.startSlot + request.count; i += request.step) {
if (skippedSlots?.has(i)) {
Expand All @@ -94,7 +98,7 @@ describe("sync / range / chain", () => {
)
);
}
return blocks;
return {blocks, pendingDataColumns: null};
};

const target: ChainTarget = {slot: computeStartSlotAtEpoch(targetEpoch), root: ZERO_HASH};
Expand All @@ -111,7 +115,7 @@ describe("sync / range / chain", () => {
);

const peers = [peer];
for (const peer of peers) initialSync.addPeer(peer, target);
for (const peer of peers) initialSync.addPeer(peer, target, []);

initialSync.startSyncing(startEpoch);
});
Expand All @@ -124,7 +128,11 @@ describe("sync / range / chain", () => {
const peers = [peer];

const processChainSegment: SyncChainFns["processChainSegment"] = async () => {};
const downloadBeaconBlocksByRange: SyncChainFns["downloadBeaconBlocksByRange"] = async (peer, request) => {
const downloadBeaconBlocksByRange: SyncChainFns["downloadBeaconBlocksByRange"] = async (
peer,
request,
partialDownload
) => {
const blocks: BlockInput[] = [];
for (let i = request.startSlot; i < request.startSlot + request.count; i += request.step) {
blocks.push(
Expand All @@ -139,7 +147,7 @@ describe("sync / range / chain", () => {
)
);
}
return blocks;
return {blocks, pendingDataColumns: null};
};

const target: ChainTarget = {slot: computeStartSlotAtEpoch(targetEpoch), root: ZERO_HASH};
Expand All @@ -157,7 +165,7 @@ describe("sync / range / chain", () => {

// Add peers after some time
setTimeout(() => {
for (const peer of peers) initialSync.addPeer(peer, target);
for (const peer of peers) initialSync.addPeer(peer, target, []);
}, 20);

initialSync.startSyncing(startEpoch);
Expand All @@ -181,9 +189,9 @@ function logSyncChainFns(logger: Logger, fns: SyncChainFns): SyncChainFns {
logger.debug("mock processChainSegment", {blocks: blocks.map((b) => b.block.message.slot).join(",")});
return fns.processChainSegment(blocks, syncType);
},
downloadBeaconBlocksByRange(peer, request) {
downloadBeaconBlocksByRange(peer, request, partialDownload) {
logger.debug("mock downloadBeaconBlocksByRange", request);
return fns.downloadBeaconBlocksByRange(peer, request);
return fns.downloadBeaconBlocksByRange(peer, request, null);
},
reportPeer(peer, action, actionName) {
logger.debug("mock reportPeer", {peer: peer.toString(), action, actionName});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ describe("sync / range / batches", () => {
batch.startDownloading(peer);
if (status === BatchStatus.Downloading) return batch;

batch.downloadingSuccess([]);
batch.downloadingSuccess({blocks: [], pendingDataColumns: []});
if (status === BatchStatus.AwaitingProcessing) return batch;

batch.startProcessing();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {config} from "@lodestar/config/default";
import {Batch} from "../../../../../src/sync/range/batch.js";
import {ChainPeersBalancer} from "../../../../../src/sync/range/utils/peerBalancer.js";
import {getRandPeerIdStr} from "../../../../utils/peer.js";
import {PeerIdStr} from "../../../../../src/util/peerId.js";

describe("sync / range / peerBalancer", () => {
it("bestPeerToRetryBatch", async () => {
Expand All @@ -14,14 +15,16 @@ describe("sync / range / peerBalancer", () => {
const batch0 = new Batch(0, config);
const batch1 = new Batch(1, config);

const custodyColumn = new Map<PeerIdStr, {custodyColumns: number[]}>();

// Batch zero has a failedDownloadAttempt with peer0
batch0.startDownloading(peer1);
batch0.downloadingError();

// peer2 is busy downloading batch1
batch1.startDownloading(peer2);

const peerBalancer = new ChainPeersBalancer([peer1, peer2, peer3], [batch0, batch1]);
const peerBalancer = new ChainPeersBalancer([peer1, peer2, peer3], custodyColumn, [batch0, batch1]);

expect(peerBalancer.bestPeerToRetryBatch(batch0)).toBe(peer3);

Expand All @@ -41,11 +44,13 @@ describe("sync / range / peerBalancer", () => {
const batch0 = new Batch(0, config);
const batch1 = new Batch(1, config);

const custodyColumn = new Map<PeerIdStr, {custodyColumns: number[]}>();

// peer1 and peer2 are busy downloading
batch0.startDownloading(peer1);
batch1.startDownloading(peer2);

const peerBalancer = new ChainPeersBalancer([peer1, peer2, peer3, peer4], [batch0, batch1]);
const peerBalancer = new ChainPeersBalancer([peer1, peer2, peer3, peer4], custodyColumn, [batch0, batch1]);

const idlePeers = peerBalancer.idlePeers();

Expand Down
7 changes: 5 additions & 2 deletions packages/beacon-node/test/utils/networkWithMockDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {GossipHandlers, Network, NetworkInitModules, getReqRespHandlers} from ".
import {NetworkOptions, defaultNetworkOptions} from "../../src/network/options.js";
import {GetReqRespHandlerFn} from "../../src/network/reqresp/types.js";
import {getMockedBeaconDb} from "../mocks/mockedBeaconDb.js";
import {computeNodeId} from "../../src/network/subnets/index.js";
import {createCachedBeaconStateTest} from "./cachedBeaconState.js";
import {ClockStatic} from "./clock.js";
import {testLogger} from "./logger.js";
Expand Down Expand Up @@ -72,7 +73,7 @@ export async function getNetworkForTest(
}
);

const modules: Omit<NetworkInitModules, "opts" | "peerId" | "logger"> = {
const modules: Omit<NetworkInitModules, "opts" | "peerId" | "logger" | "nodeId"> = {
config: beaconConfig,
chain,
db,
Expand All @@ -81,9 +82,11 @@ export async function getNetworkForTest(
metrics: null,
};

const peerId = await createSecp256k1PeerId();
const network = await Network.init({
...modules,
peerId: await createSecp256k1PeerId(),
peerId,
nodeId: computeNodeId(peerId),
opts: {
...defaultNetworkOptions,
maxPeers: 1,
Expand Down

0 comments on commit dcdb14c

Please sign in to comment.