Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

fix: make port 0 bind to any available port #4070

Merged
merged 12 commits into from
Jan 31, 2023
Merged

Conversation

davidmurdoch
Copy link
Member

@davidmurdoch davidmurdoch commented Dec 20, 2022

Specifying a port of 0 is a way to tell a server that you want the Operating System to let you bind to any open port. Ganache claimed to support binding to port 0, but would rudely yell at you if you tried: Port should be >= 0 and < 65536. Received 0 🤦! This PR rights our wrongs.

Using port 0 is useful when you don't care which port you bind to, you just want an open port.

You can use port 0 in a few ways:

On the cli:

$ ganache --port 0

RPC Listening on 127.0.0.1:35006

In detach mode:

$$\color{#8b949e}\textsf{note: port 0 binding in detach mode doesn't yet provide a scripting friendly way of returning the port}$$

$ ganache --detach --port 0
frozen_caramel_doughnut

$ ganache instances list
┌───────┬─────────────────────────┬──────────┬─────────┬─────────────────┬────────┐
│   PID │ Name                    │ Flavor   │ Version │ Host            │ Uptime │
├───────┼─────────────────────────┼──────────┼─────────┼─────────────────┼────────┤
│ 58078 │ frozen_caramel_doughnut │ ethereum │ 7.7.3   │ 127.0.0.1:35006 │    14s │
└───────┴─────────────────────────┴──────────┴─────────┴─────────────────┴────────┘

and programmatically:

import ganache from "ganache";

const server = ganache.server();
await server.listen(0);
console.log("Listening on port: ", server.address().port);

Fixes #2643 and #3805

@davidmurdoch davidmurdoch marked this pull request as ready for review December 21, 2022 18:31
@davidmurdoch davidmurdoch requested review from jeffsmale90, MicaiahReid and tenthirtyone and removed request for jeffsmale90 December 21, 2022 18:31
@davidmurdoch davidmurdoch changed the title fix: allow port 0 to bind to any available port fix: make port 0 bind to any available port Dec 21, 2022
src/packages/core/src/server.ts Show resolved Hide resolved
src/packages/cli/src/cli.ts Show resolved Hide resolved
src/packages/cli/src/detach.ts Outdated Show resolved Hide resolved
src/packages/cli/src/detach.ts Outdated Show resolved Hide resolved
src/packages/core/src/server.ts Show resolved Hide resolved
src/packages/core/tests/server.test.ts Outdated Show resolved Hide resolved
@davidmurdoch davidmurdoch changed the title fix: make port 0 bind to any available port is this a fix or a feat?: make port 0 bind to any available port Jan 6, 2023
Copy link
Contributor

@MicaiahReid MicaiahReid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a fix because we claimed to support binding to port 0.

src/packages/cli/src/detach.ts Show resolved Hide resolved
src/packages/cli/src/cli.ts Show resolved Hide resolved
src/packages/core/tests/server.test.ts Show resolved Hide resolved
@davidmurdoch davidmurdoch changed the title is this a fix or a feat?: make port 0 bind to any available port fix: make port 0 bind to any available port Jan 10, 2023
@davidmurdoch davidmurdoch merged commit a952ae7 into develop Jan 31, 2023
@davidmurdoch davidmurdoch deleted the support-port-0 branch January 31, 2023 21:42
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support binding to port 0
3 participants