Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use random ports in p2p_client tests #8624

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions yarn-project/foundation/src/testing/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './test_data.js';
export * from './snapshot_serializer.js';
export * from './port_allocator.js';
31 changes: 31 additions & 0 deletions yarn-project/foundation/src/testing/port_allocator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import net from 'net';

/**
* Get a random port that is free to use.
* Returns undefined if it fails to get a port.
*
* @attribution: adapted from https://stackoverflow.com/a/71178451
*
* @returns a random port that is free to use.
*/
export function getRandomPort(): Promise<number | undefined> {
return new Promise(resolve => {
const server = net.createServer();
server.listen(0, () => {
const address = server.address();
if (address && typeof address === 'object' && 'port' in address) {
const port = address.port;
server.close(() => {
resolve(port);
});
} else {
server.close(() => {
resolve(undefined);
});
}
});
server.on('error', () => {
resolve(undefined);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type ClientProtocolCircuitVerifier, type WorldStateSynchronizer, mockTx
import { EthAddress } from '@aztec/circuits.js';
import { createDebugLogger } from '@aztec/foundation/log';
import { sleep } from '@aztec/foundation/sleep';
import { getRandomPort } from '@aztec/foundation/testing';
import { type AztecKVStore } from '@aztec/kv-store';
import { type DataStoreConfig, openTmpStore } from '@aztec/kv-store/utils';

Expand All @@ -27,7 +28,7 @@ type Mockify<T> = {

const TEST_TIMEOUT = 80000;

const BOOT_NODE_UDP_PORT = 40400;
const DEFAULT_BOOT_NODE_UDP_PORT = 40400;
async function createBootstrapNode(port: number) {
const peerId = await createLibP2PPeerId();
const bootstrapNode = new BootstrapNode();
Expand Down Expand Up @@ -61,10 +62,12 @@ describe('Req Resp p2p client integration', () => {
let kvStore: AztecKVStore;
let worldStateSynchronizer: WorldStateSynchronizer;
let proofVerifier: ClientProtocolCircuitVerifier;
let bootNodePort: number;
const logger = createDebugLogger('p2p-client-integration-test');

const makeBootstrapNode = async (): Promise<[BootstrapNode, string]> => {
const bootstrapNode = await createBootstrapNode(BOOT_NODE_UDP_PORT);
bootNodePort = (await getRandomPort()) || DEFAULT_BOOT_NODE_UDP_PORT;
const bootstrapNode = await createBootstrapNode(bootNodePort);
const enr = bootstrapNode.getENR().encodeTxt();
return [bootstrapNode, enr];
};
Expand All @@ -74,8 +77,9 @@ describe('Req Resp p2p client integration', () => {
const peerIdPrivateKeys = generatePeerIdPrivateKeys(numberOfPeers);
for (let i = 0; i < numberOfPeers; i++) {
// Note these bindings are important
const addr = `127.0.0.1:${i + 1 + BOOT_NODE_UDP_PORT}`;
const listenAddr = `0.0.0.0:${i + 1 + BOOT_NODE_UDP_PORT}`;
const port = (await getRandomPort()) || bootNodePort + i + 1;
const addr = `127.0.0.1:${port}`;
const listenAddr = `0.0.0.0:${port}`;
const config: P2PConfig & DataStoreConfig = {
...getP2PDefaultConfig(),
p2pEnabled: true,
Expand Down
Loading