From 8881334330f7943b0375105277ae8c0dd64b5585 Mon Sep 17 00:00:00 2001 From: Cayman Date: Mon, 11 Apr 2022 14:10:42 -0500 Subject: [PATCH] Use @chainsafe/threads --- package.json | 2 +- test/unit/multithread/naive/index.ts | 20 ++++++++++++++++++-- test/unit/multithread/naive/worker.ts | 2 +- yarn.lock | 24 ++++++++++++------------ 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 5253e7d..dd3ca12 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,7 @@ "@chainsafe/blst": "^0.2.0", "@chainsafe/eslint-plugin-node": "^11.2.3", "@chainsafe/lodestar-spec-test-util": "^0.18.0", + "@chainsafe/threads": "^1.9.0", "@types/chai": "^4.2.9", "@types/mocha": "^8.0.4", "@types/randombytes": "^2.0.0", @@ -94,7 +95,6 @@ "nyc": "^15.0.0", "prettier": "^2.1.2", "resolve-typescript-plugin": "^1.1.5", - "threads": "^1.7.0", "ts-loader": "^8.3.0", "ts-node": "^10.7.0", "typescript": "^4.6.3", diff --git a/test/unit/multithread/naive/index.ts b/test/unit/multithread/naive/index.ts index 609926b..5530464 100644 --- a/test/unit/multithread/naive/index.ts +++ b/test/unit/multithread/naive/index.ts @@ -1,4 +1,4 @@ -import {spawn, Pool, Worker, Thread} from "threads"; +import {spawn, Pool, Worker, Thread} from "@chainsafe/threads"; import {Implementation, PointFormat} from "../../../../src/index.js"; import {PublicKey, Signature} from "../../../../src/types.js"; import {WorkerApi} from "./worker.js"; @@ -7,6 +7,11 @@ type ThreadType = { [K in keyof WorkerApi]: (...args: Parameters) => Promise>; }; +import path from "path"; +import {fileURLToPath} from "url"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); + export class BlsMultiThreadNaive { impl: Implementation; pool: Pool; @@ -18,7 +23,18 @@ export class BlsMultiThreadNaive { // THe worker is not able to deserialize from uncompressed // `Error: err _wrapDeserialize` this.format = impl === "blst-native" ? PointFormat.uncompressed : PointFormat.compressed; - this.pool = Pool(() => (spawn(new Worker("./worker")) as any) as Promise, workerCount); + this.pool = Pool( + () => + (spawn( + // There is still an annoyance dealing with ESM imports here: + // threads.js attempts to require.resolve any files passed to Worker, and + // the esm module resolver requires the .js extension, even though the .js file does not actually exist. + // The solution for now: + // Pass in the script path as an absolute path and suppress threads.js default behavior when importing + new Worker(path.join(__dirname, "./worker.js"), {suppressResolveScript: true, suppressTranspileTS: true}) + ) as any) as Promise, + workerCount + ); } async destroy(): Promise { diff --git a/test/unit/multithread/naive/worker.ts b/test/unit/multithread/naive/worker.ts index ec9fa8b..68bc4d0 100644 --- a/test/unit/multithread/naive/worker.ts +++ b/test/unit/multithread/naive/worker.ts @@ -1,4 +1,4 @@ -import {expose} from "threads/worker"; +import {expose} from "@chainsafe/threads/worker"; import {bls, init, CoordType, Implementation} from "../../../../src/index.js"; export type WorkerApi = typeof workerApi; diff --git a/yarn.lock b/yarn.lock index d05ceed..fcd6a54 100644 --- a/yarn.lock +++ b/yarn.lock @@ -266,6 +266,18 @@ "@chainsafe/persistent-merkle-tree" "^0.3.0" case "^1.6.3" +"@chainsafe/threads@^1.9.0": + version "1.9.0" + resolved "https://registry.yarnpkg.com/@chainsafe/threads/-/threads-1.9.0.tgz#5ab07b1c150ad6ffc2325f344cde549a96b53802" + integrity sha512-TFSj5CWwi2o0Z3qBdrROdR+mJ23reBm+e0iPaifn657AxNtatdHNV9l6DiNwOKHTxTxKTm1daVuWW5TP0dJGDw== + dependencies: + callsites "^3.1.0" + debug "^4.2.0" + is-observable "^2.1.0" + observable-fns "^0.6.1" + optionalDependencies: + tiny-worker ">= 2" + "@cspotcode/source-map-consumer@0.8.0": version "0.8.0" resolved "https://registry.yarnpkg.com/@cspotcode/source-map-consumer/-/source-map-consumer-0.8.0.tgz#33bf4b7b39c178821606f669bbc447a6a629786b" @@ -6100,18 +6112,6 @@ text-table@^0.2.0: resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4" integrity sha1-f17oI66AUgfACvLfSoTsP8+lcLQ= -threads@^1.7.0: - version "1.7.0" - resolved "https://registry.yarnpkg.com/threads/-/threads-1.7.0.tgz#d9e9627bfc1ef22ada3b733c2e7558bbe78e589c" - integrity sha512-Mx5NBSHX3sQYR6iI9VYbgHKBLisyB+xROCBGjjWm1O9wb9vfLxdaGtmT/KCjUqMsSNW6nERzCW3T6H43LqjDZQ== - dependencies: - callsites "^3.1.0" - debug "^4.2.0" - is-observable "^2.1.0" - observable-fns "^0.6.1" - optionalDependencies: - tiny-worker ">= 2" - through2@^2.0.0: version "2.0.5" resolved "https://registry.yarnpkg.com/through2/-/through2-2.0.5.tgz#01c1e39eb31d07cb7d03a96a70823260b23132cd"