Use @chainsafe/threads

This commit is contained in:
Cayman 2022-04-11 14:10:42 -05:00
parent 0056e13ee5
commit 8881334330
No known key found for this signature in database
GPG Key ID: 54B21AEC3C53E1F5
4 changed files with 32 additions and 16 deletions

View File

@ -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",

View File

@ -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<WorkerApi[K]>) => Promise<ReturnType<WorkerApi[K]>>;
};
import path from "path";
import {fileURLToPath} from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
export class BlsMultiThreadNaive {
impl: Implementation;
pool: Pool<Thread & ThreadType>;
@ -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<Thread & ThreadType>, 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<Thread & ThreadType>,
workerCount
);
}
async destroy(): Promise<void> {

View File

@ -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;

View File

@ -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"