diff --git a/package.json b/package.json index a19c21c2b1..63c2c562a5 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "prepublishOnly": "cd scripts && ts-node filter-shrinkwrap.ts \"$(lerna list --parseable --scope ganache)\"/npm-shrinkwrap.json", "postpublish": "git restore \"$(lerna list --parseable --scope ganache)\"/npm-shrinkwrap.json", "start": "lerna exec --loglevel=silent --scope ganache -- npm run start --silent -- ", - "test": "lerna exec --concurrency 1 -- npm run test", + "test": "lerna exec -- npm run test", "tsc": "tsc --build src", "tsc.clean": "npx lerna exec -- npx shx rm -rf lib dist typings" }, diff --git a/src/chains/ethereum/ethereum/package-lock.json b/src/chains/ethereum/ethereum/package-lock.json index 834f0202dc..afd4d45ba6 100644 --- a/src/chains/ethereum/ethereum/package-lock.json +++ b/src/chains/ethereum/ethereum/package-lock.json @@ -4911,6 +4911,12 @@ "pkg-dir": "^4.1.0" } }, + "find-open-port": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/find-open-port/-/find-open-port-2.0.3.tgz", + "integrity": "sha512-lcYIpPHWNksWSSgUxe+gAq8LqG7wCpkhohhTVG0E0c5KT1Lrpb5cY7bJ+fO49Dax1TkAw5paKAoBSTgYwnBKrA==", + "dev": true + }, "find-replace": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/find-replace/-/find-replace-3.0.0.tgz", diff --git a/src/chains/ethereum/ethereum/package.json b/src/chains/ethereum/ethereum/package.json index 0bcae0a502..7693d79f50 100644 --- a/src/chains/ethereum/ethereum/package.json +++ b/src/chains/ethereum/ethereum/package.json @@ -102,6 +102,7 @@ "cheerio": "1.0.0-rc.3", "cross-env": "7.0.3", "fast-check": "3.0.1", + "find-open-port": "2.0.3", "fs-extra": "9.0.1", "local-web-server": "4.2.1", "memdown": "6.1.1", diff --git a/src/chains/ethereum/ethereum/tests/forking/forking.test.ts b/src/chains/ethereum/ethereum/tests/forking/forking.test.ts index 802f14d876..1461009370 100644 --- a/src/chains/ethereum/ethereum/tests/forking/forking.test.ts +++ b/src/chains/ethereum/ethereum/tests/forking/forking.test.ts @@ -17,6 +17,7 @@ import { import compile from "../helpers/compile"; import path from "path"; import { CodedError } from "@ganache/ethereum-utils"; +import { findPort } from "find-open-port"; async function deployContract( remoteProvider: EthereumProvider, @@ -53,9 +54,8 @@ async function deployContract( methods }; } -const PORT = 9999; -describe("forking", function () { +describe("forking", async function () { this.timeout(10000); const NETWORK_ID = 1234; @@ -63,6 +63,7 @@ describe("forking", function () { let remoteServer: Server; let remoteProvider: EthereumProvider; let remoteAccounts: string[]; + let remotePort: number; beforeEach("start remote chain", async () => { remoteServer = ganache.server({ @@ -72,7 +73,8 @@ describe("forking", function () { }); remoteProvider = remoteServer.provider as unknown as EthereumProvider; remoteAccounts = Object.keys(remoteProvider.getInitialAccounts()); - await remoteServer.listen(PORT); + remotePort = await findPort(); + await remoteServer.listen(remotePort); }); afterEach(async () => { @@ -111,10 +113,11 @@ describe("forking", function () { totalDifficulty: "0x0", transactions: [] }; - const port = 9988; + let port: number; let junk: any; let server: http.Server; beforeEach("start mock http server", async () => { + port = await findPort(); // mock a server so we can send bad requests back at ganache server = http.createServer((req, res) => { let body = ""; @@ -187,7 +190,7 @@ describe("forking", function () { it("throws on invalid provider", async () => { await assert.rejects( () => - startLocalChain(PORT, { + startLocalChain(remotePort, { provider: { request: "not a function" } as any, disableCache: true }), @@ -200,7 +203,7 @@ describe("forking", function () { beforeEach( "start up localProvider fork with remoteProvider", async () => { - const provider = await startLocalChain(PORT, { + const provider = await startLocalChain(remotePort, { provider: remoteProvider as any, disableCache: true }); @@ -272,7 +275,7 @@ describe("forking", function () { return (send as any).apply(remoteProvider, args); }; - const provider = await startLocalChain(PORT, { + const provider = await startLocalChain(remotePort, { provider: remoteProvider as any, disableCache: true }); @@ -340,7 +343,7 @@ describe("forking", function () { describe("initial state", () => { it("should get the Network ID of the forked chain", async () => { - const { localProvider } = await startLocalChain(PORT, { + const { localProvider } = await startLocalChain(remotePort, { disableCache: true }); @@ -363,7 +366,7 @@ describe("forking", function () { ); assert.strictEqual(remoteBlockNumber, 10); const localStartBlockNum = blocks / 2; - const { localProvider } = await startLocalChain(PORT, { + const { localProvider } = await startLocalChain(remotePort, { blockNumber: localStartBlockNum, disableCache: true }); @@ -388,7 +391,7 @@ describe("forking", function () { describe("block number", () => { let localProvider: EthereumProvider; beforeEach("start local chain", async () => { - ({ localProvider } = await startLocalChain(PORT, { + ({ localProvider } = await startLocalChain(remotePort, { disableCache: true })); }); @@ -410,7 +413,7 @@ describe("forking", function () { }); beforeEach("start local chain", async () => { - ({ localProvider } = await startLocalChain(PORT, { + ({ localProvider } = await startLocalChain(remotePort, { disableCache: true })); }); @@ -437,7 +440,7 @@ describe("forking", function () { }); beforeEach("start local chain", async () => { - ({ localProvider, localAccounts } = await startLocalChain(PORT, { + ({ localProvider, localAccounts } = await startLocalChain(remotePort, { disableCache: true })); }); @@ -641,7 +644,7 @@ describe("forking", function () { }); it("should fetch contract code from the remote chain via the local chain", async () => { - const { localProvider } = await startLocalChain(PORT, { + const { localProvider } = await startLocalChain(remotePort, { disableCache: true }); const { blockNumbersWithCode, blockNumbersWithoutCode } = @@ -671,7 +674,7 @@ describe("forking", function () { }); it("should fetch initial contract data from the remote chain via the local chain", async () => { - const { localProvider } = await startLocalChain(PORT, { + const { localProvider } = await startLocalChain(remotePort, { disableCache: true }); const { blockNum, blockNumbersWithCode, blockNumbersWithoutCode } = @@ -720,7 +723,7 @@ describe("forking", function () { }); it("should fetch changed contract data from the remote chain via the local chain", async () => { - const { localProvider } = await startLocalChain(PORT, { + const { localProvider } = await startLocalChain(remotePort, { disableCache: true }); const { blockNum, blockNumbersWithCode, blockNumbersWithoutCode } = @@ -829,7 +832,7 @@ describe("forking", function () { initialValue: number, snapshotValues: number[] ) { - const { localProvider } = await startLocalChain(PORT, { + const { localProvider } = await startLocalChain(remotePort, { disableCache: true }); const subId = await localProvider.send("eth_subscribe", ["newHeads"]); @@ -925,7 +928,7 @@ describe("forking", function () { describe("gas estimation", () => { it("should not affect live state", async () => { - const { localProvider } = await startLocalChain(PORT, { + const { localProvider } = await startLocalChain(remotePort, { disableCache: true }); const blockNum = await getBlockNumber(localProvider); @@ -974,7 +977,7 @@ describe("forking", function () { await remoteProvider.once("message"); await remoteProvider.send("eth_unsubscribe", [subId]); - ({ localProvider } = await startLocalChain(PORT)); + ({ localProvider } = await startLocalChain(remotePort)); }); it("ensure local block's latest matches remote block's latest (with transaction)", async () => { @@ -1223,7 +1226,7 @@ describe("forking", function () { KNOWN_NETWORKS.forEach(network => { describe(network, () => { beforeEach("set up network provider", async () => { - const provider = await startLocalChain(PORT, { + const provider = await startLocalChain(null, { network, disableCache: true }); diff --git a/src/chains/ethereum/ethereum/tests/forking/helpers.ts b/src/chains/ethereum/ethereum/tests/forking/helpers.ts index 6be1324cfe..80e133d687 100644 --- a/src/chains/ethereum/ethereum/tests/forking/helpers.ts +++ b/src/chains/ethereum/ethereum/tests/forking/helpers.ts @@ -53,7 +53,7 @@ export const updateRemotesAccountNonces = async ( }; export const startLocalChain = async ( - port: number, + port?: number, options?: EthereumProviderOptions["fork"] ) => { const fork: EthereumProviderOptions["fork"] = { ...options }; diff --git a/src/chains/filecoin/filecoin/package-lock.json b/src/chains/filecoin/filecoin/package-lock.json index 2787192bea..382f2593ae 100644 --- a/src/chains/filecoin/filecoin/package-lock.json +++ b/src/chains/filecoin/filecoin/package-lock.json @@ -3388,6 +3388,12 @@ "pkg-dir": "^4.1.0" } }, + "find-open-port": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/find-open-port/-/find-open-port-2.0.3.tgz", + "integrity": "sha512-lcYIpPHWNksWSSgUxe+gAq8LqG7wCpkhohhTVG0E0c5KT1Lrpb5cY7bJ+fO49Dax1TkAw5paKAoBSTgYwnBKrA==", + "dev": true + }, "find-up": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", diff --git a/src/chains/filecoin/filecoin/package.json b/src/chains/filecoin/filecoin/package.json index ba49a560d5..377b1b511b 100644 --- a/src/chains/filecoin/filecoin/package.json +++ b/src/chains/filecoin/filecoin/package.json @@ -80,6 +80,7 @@ "deep-equal": "2.0.3", "emittery": "0.10.0", "encoding-down": "7.1.0", + "find-open-port": "2.0.3", "ipfs-http-client": "48.1.3", "levelup": "5.1.1", "lodash.clonedeep": "4.5.0", diff --git a/src/chains/filecoin/filecoin/tests/api/filecoin/subscriptions-ws.test.ts b/src/chains/filecoin/filecoin/tests/api/filecoin/subscriptions-ws.test.ts index 510bfe82b2..527557108d 100644 --- a/src/chains/filecoin/filecoin/tests/api/filecoin/subscriptions-ws.test.ts +++ b/src/chains/filecoin/filecoin/tests/api/filecoin/subscriptions-ws.test.ts @@ -8,10 +8,12 @@ describe("api", () => { describe("filecoin", () => { let server: Server<"filecoin">; let ws: WebSocket; - const port = 7778; // Use a different port than the default, to test it works + let port: number; before(async () => { - server = await getServer(port); + const serverDetails = await getServer(); + server = serverDetails.server; + port = serverDetails.port; ws = new WebSocket(`ws://localhost:${port}/rpc/v0`); await new Promise((resolve, reject) => { diff --git a/src/chains/filecoin/filecoin/tests/helpers/getServer.ts b/src/chains/filecoin/filecoin/tests/helpers/getServer.ts index 8afcf75a8b..32d487dc1a 100644 --- a/src/chains/filecoin/filecoin/tests/helpers/getServer.ts +++ b/src/chains/filecoin/filecoin/tests/helpers/getServer.ts @@ -1,14 +1,17 @@ import { FilecoinFlavorName } from "../../../../../packages/flavors"; import Server from "../../../../../packages/core/src/server"; +import { findPort } from "find-open-port"; -const getServer = async (port: number) => { +const getServer = async () => { + const port = await findPort(); + const ipfsPort = await findPort(); const server = new Server({ flavor: FilecoinFlavorName, server: { ws: true }, chain: { - ipfsPort: 5002 // Use a different port than the default, to test it works + ipfsPort }, logging: { logger: { @@ -17,7 +20,7 @@ const getServer = async (port: number) => { } }); await server.listen(port); - return server; + return { server, port }; }; export default getServer; diff --git a/src/packages/core/package-lock.json b/src/packages/core/package-lock.json index 783e051aca..01808b8587 100644 --- a/src/packages/core/package-lock.json +++ b/src/packages/core/package-lock.json @@ -950,6 +950,12 @@ "pkg-dir": "^4.1.0" } }, + "find-open-port": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/find-open-port/-/find-open-port-2.0.3.tgz", + "integrity": "sha512-lcYIpPHWNksWSSgUxe+gAq8LqG7wCpkhohhTVG0E0c5KT1Lrpb5cY7bJ+fO49Dax1TkAw5paKAoBSTgYwnBKrA==", + "dev": true + }, "find-up": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz", diff --git a/src/packages/core/package.json b/src/packages/core/package.json index 95c845f295..6f841a7a6f 100644 --- a/src/packages/core/package.json +++ b/src/packages/core/package.json @@ -61,6 +61,7 @@ "@types/mocha": "9.0.0", "@types/superagent": "4.1.10", "cross-env": "7.0.3", + "find-open-port": "2.0.3", "mocha": "9.1.3", "nyc": "15.1.0", "superagent": "6.1.0", diff --git a/src/packages/core/tests/server.test.ts b/src/packages/core/tests/server.test.ts index 09cab25b13..1257da21eb 100644 --- a/src/packages/core/tests/server.test.ts +++ b/src/packages/core/tests/server.test.ts @@ -26,12 +26,13 @@ import { NetworkInterfaceInfoIPv6, networkInterfaces } from "os"; +import { findPort } from "find-open-port"; + const chunkSize = 1024 * 1024; const IS_WINDOWS = process.platform === "win32"; describe("server", () => { - const port = 5234; const networkId = 1234; const jsonRpcJson: any = { jsonrpc: "2.0", @@ -43,6 +44,7 @@ describe("server", () => { log: (_message: string) => {} }; let s: Server; + let port: number; const defaultOptions = { chain: { @@ -52,7 +54,9 @@ describe("server", () => { logger } }; - + before(async () => { + port = await findPort(); + }); async function setup( options: ServerOptions = defaultOptions, host: string | null = null