Skip to content

Commit abc60d4

Browse files
committed
fix: guard against creating a server with a network config type of http
Match the behaviour of `npx hardhat node` and throw if a network config with a type other than `edr-simulated` is used.
1 parent 5910545 commit abc60d4

4 files changed

Lines changed: 57 additions & 0 deletions

File tree

.changeset/petite-teams-slide.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@nomicfoundation/hardhat-errors": patch
3+
"hardhat": patch
4+
---
5+
6+
Added guard against `http` network configs in `network.createServer(...)`

v-next/hardhat-errors/src/descriptors.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,14 @@ account, and its parameters are incompatible. You sent both gasPrice and authori
10501050
10511051
Please double check your transactions' parameters.`,
10521052
},
1053+
CREATE_SERVER_UNSUPPORTED_NETWORK_TYPE: {
1054+
number: 724,
1055+
messageTemplate:
1056+
'Cannot create a server for network "{networkName}" because it has type "{networkType}". Only "edr-simulated" networks are supported.',
1057+
websiteTitle: "Unsupported network type for createServer",
1058+
websiteDescription:
1059+
"The createServer method only supports 'edr-simulated' networks. HTTP networks cannot be used to create a local JSON-RPC server.",
1060+
},
10531061
},
10541062
SOLIDITY_TESTS: {
10551063
BUILD_INFO_NOT_FOUND_FOR_CONTRACT: {

v-next/hardhat/src/internal/builtin-plugins/network-manager/network-manager.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ export class NetworkManagerImplementation implements NetworkManager {
117117
_hostname?: string,
118118
port?: number,
119119
): Promise<JsonRpcServer> {
120+
this.#ensureNetworkOrParamsIsNotHttpNetworkConfig(networkOrParams);
121+
120122
const insideDocker = await exists("/.dockerenv");
121123
const hostname = _hostname ?? (insideDocker ? "0.0.0.0" : "127.0.0.1");
122124

@@ -436,4 +438,27 @@ export class NetworkManagerImplementation implements NetworkManager {
436438

437439
return path;
438440
}
441+
442+
#ensureNetworkOrParamsIsNotHttpNetworkConfig(
443+
networkOrParams: NetworkConnectionParams<string> | string,
444+
) {
445+
const networkName =
446+
typeof networkOrParams === "string"
447+
? networkOrParams
448+
: networkOrParams.network ?? this.#defaultNetwork;
449+
450+
const networkConfig = this.#networkConfigs[networkName];
451+
452+
if (networkConfig === undefined || networkConfig.type === "edr-simulated") {
453+
return;
454+
}
455+
456+
throw new HardhatError(
457+
HardhatError.ERRORS.CORE.NETWORK.CREATE_SERVER_UNSUPPORTED_NETWORK_TYPE,
458+
{
459+
networkName,
460+
networkType: networkConfig.type,
461+
},
462+
);
463+
}
439464
}

v-next/hardhat/test/internal/builtin-plugins/network-manager/network-manager.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,24 @@ describe("NetworkManagerImplementation", () => {
846846
});
847847

848848
describe("createServer", function () {
849+
it("should throw an error if the network type is not edr-simulated", async () => {
850+
await assertRejectsWithHardhatError(
851+
networkManager.createServer("localhost"),
852+
HardhatError.ERRORS.CORE.NETWORK
853+
.CREATE_SERVER_UNSUPPORTED_NETWORK_TYPE,
854+
{ networkName: "localhost", networkType: "http" },
855+
);
856+
});
857+
858+
it("should throw an error if network parameters specify an http network", async () => {
859+
await assertRejectsWithHardhatError(
860+
networkManager.createServer({ network: "localhost" }),
861+
HardhatError.ERRORS.CORE.NETWORK
862+
.CREATE_SERVER_UNSUPPORTED_NETWORK_TYPE,
863+
{ networkName: "localhost", networkType: "http" },
864+
);
865+
});
866+
849867
it("connects to a network and returns a JsonRpcServer that wraps around it", async () => {
850868
const server = await networkManager.createServer(
851869
"edrNetwork",

0 commit comments

Comments
 (0)