Skip to content

Commit

Permalink
chore: added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeluard committed Feb 5, 2024
1 parent e16a6e5 commit a9df592
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions packages/beacon-node/test/e2e/network/gossipsub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,70 @@ function runTests({useWorker}: {useWorker: boolean}): void {
);
});

it("Publish and receive an attesterSlashing", async function () {
let onAttesterSlashingChange: (payload: Uint8Array) => void;
const onAttesterSlashingChangePromise = new Promise<Uint8Array>((resolve) => (onAttesterSlashingChange = resolve));

const {netA, netB} = await mockModules({
[GossipType.attester_slashing]: async ({gossipData}: GossipHandlerParamGeneric<GossipType.attester_slashing>) => {
onAttesterSlashingChange(gossipData.serializedData);
},
});

await Promise.all([onPeerConnect(netA), onPeerConnect(netB), connect(netA, netB)]);
expect(netA.getConnectedPeerCount()).toBe(1);
expect(netB.getConnectedPeerCount()).toBe(1);

await netA.subscribeGossipCoreTopics();
await netB.subscribeGossipCoreTopics();

// Wait to have a peer connected to a topic
while (!netA.closed) {
await sleep(500);
if (await hasSomeMeshPeer(netA)) {
break;
}
}

const attesterSlashing = ssz.phase0.AttesterSlashing.defaultValue();
await netA.publishAttesterSlashing(attesterSlashing);

const received = await onAttesterSlashingChangePromise;
expect(Buffer.from(received)).toEqual(Buffer.from(ssz.phase0.AttesterSlashing.serialize(attesterSlashing)));
});

it("Publish and receive a proposerSlashing", async function () {
let onProposerSlashingChange: (payload: Uint8Array) => void;
const onProposerSlashingChangePromise = new Promise<Uint8Array>((resolve) => (onProposerSlashingChange = resolve));

const {netA, netB} = await mockModules({
[GossipType.proposer_slashing]: async ({gossipData}: GossipHandlerParamGeneric<GossipType.proposer_slashing>) => {
onProposerSlashingChange(gossipData.serializedData);
},
});

await Promise.all([onPeerConnect(netA), onPeerConnect(netB), connect(netA, netB)]);
expect(netA.getConnectedPeerCount()).toBe(1);
expect(netB.getConnectedPeerCount()).toBe(1);

await netA.subscribeGossipCoreTopics();
await netB.subscribeGossipCoreTopics();

// Wait to have a peer connected to a topic
while (!netA.closed) {
await sleep(500);
if (await hasSomeMeshPeer(netA)) {
break;
}
}

const proposerSlashing = ssz.phase0.ProposerSlashing.defaultValue();
await netA.publishProposerSlashing(proposerSlashing);

const received = await onProposerSlashingChangePromise;
expect(Buffer.from(received)).toEqual(Buffer.from(ssz.phase0.ProposerSlashing.serialize(proposerSlashing)));
});

it("Publish and receive a LightClientOptimisticUpdate", async function () {
let onLightClientOptimisticUpdate: (ou: Uint8Array) => void;
const onLightClientOptimisticUpdatePromise = new Promise<Uint8Array>(
Expand Down

0 comments on commit a9df592

Please sign in to comment.