Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/cyan-dogs-ask.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hardhat": patch
---

Updated `network.createServer` signature to type non-generic chainTypes
6 changes: 6 additions & 0 deletions .changeset/petite-teams-slide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@nomicfoundation/hardhat-errors": patch
"hardhat": patch
---

Added guard against `http` network configs in `network.createServer(...)`
8 changes: 8 additions & 0 deletions v-next/hardhat-errors/src/descriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,14 @@ account, and its parameters are incompatible. You sent both gasPrice and authori

Please double check your transactions' parameters.`,
},
CREATE_SERVER_UNSUPPORTED_NETWORK_TYPE: {
number: 724,
messageTemplate:
'Cannot create a server for network "{networkName}" because it has type "{networkType}". Only "edr-simulated" networks are supported.',
websiteTitle: "Unsupported network type for createServer",
websiteDescription:
"The createServer method only supports 'edr-simulated' networks. HTTP networks cannot be used to create a local JSON-RPC server.",
},
},
SOLIDITY_TESTS: {
BUILD_INFO_NOT_FOUND_FOR_CONTRACT: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,15 @@ export class NetworkManagerImplementation implements NetworkManager {
return networkConnection as NetworkConnection<ChainTypeT>;
}

public async createServer(
networkOrParams: NetworkConnectionParams | string = "default",
public async createServer<
ChainTypeT extends ChainType | string = DefaultChainType,
>(
networkOrParams?: NetworkConnectionParams<ChainTypeT> | string,
_hostname?: string,
port?: number,
Comment thread
kanej marked this conversation as resolved.
): Promise<JsonRpcServer> {
this.#ensureNetworkOrParamsIsNotHttpNetworkConfig(networkOrParams);

const insideDocker = await exists("/.dockerenv");
const hostname = _hostname ?? (insideDocker ? "0.0.0.0" : "127.0.0.1");

Expand Down Expand Up @@ -434,4 +438,27 @@ export class NetworkManagerImplementation implements NetworkManager {

return path;
}

#ensureNetworkOrParamsIsNotHttpNetworkConfig(
networkOrParams?: NetworkConnectionParams<string> | string,
) {
const networkName =
typeof networkOrParams === "string"
? networkOrParams
: networkOrParams?.network ?? this.#defaultNetwork;

const networkConfig = this.#networkConfigs[networkName];

if (networkConfig === undefined || networkConfig.type === "edr-simulated") {
return;
}

throw new HardhatError(
HardhatError.ERRORS.CORE.NETWORK.CREATE_SERVER_UNSUPPORTED_NETWORK_TYPE,
{
networkName,
networkType: networkConfig.type,
},
);
}
}
4 changes: 2 additions & 2 deletions v-next/hardhat/src/types/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export interface NetworkManager {
*
* @return A `JsonRpcServer` instance that can be started with {@link JsonRpcServer.listen}.
*/
createServer(
networkOrParams?: NetworkConnectionParams | string,
createServer<ChainTypeT extends ChainType | string = DefaultChainType>(
networkOrParams?: NetworkConnectionParams<ChainTypeT> | string,
hostname?: string,
port?: number,
): Promise<JsonRpcServer>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,17 +846,55 @@ describe("NetworkManagerImplementation", () => {
});

describe("createServer", function () {
it("should throw an error if the network type is not edr-simulated", async () => {
await assertRejectsWithHardhatError(
networkManager.createServer("localhost"),
HardhatError.ERRORS.CORE.NETWORK.CREATE_SERVER_UNSUPPORTED_NETWORK_TYPE,
{ networkName: "localhost", networkType: "http" },
);
});

it("should throw an error if network parameters specify an http network", async () => {
await assertRejectsWithHardhatError(
networkManager.createServer({ network: "localhost" }),
HardhatError.ERRORS.CORE.NETWORK.CREATE_SERVER_UNSUPPORTED_NETWORK_TYPE,
{ networkName: "localhost", networkType: "http" },
);
});

it("connects to a network and returns a JsonRpcServer that wraps around it", async () => {
const server = await networkManager.createServer(
"edrNetwork",
"127.0.0.1",
);

const { address, port } = await server.listen();

try {
const { provider } = await networkManager.connect({
network: "localhost",
override: { url: `http://${address}:${port}` },
});
await provider.request({ method: "eth_chainId" });
} finally {
await server.close();
}
});

it("connects to a network based on network parameters and returns a JsonRpcServer that wraps around it", async () => {
const server = await networkManager.createServer(
{ network: "edrNetwork", chainType: "op" },
"127.0.0.1",
);

const { address, port } = await server.listen();

try {
const { provider } = await networkManager.connect({
network: "localhost",
override: { url: `http://${address}:${port}` },
});

await provider.request({ method: "eth_chainId" });
} finally {
await server.close();
Expand Down
Loading