-
Notifications
You must be signed in to change notification settings - Fork 828
Worker threads/signature verification pool tests #4089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
acolytec3
wants to merge
6
commits into
master
Choose a base branch
from
parallelized-sig-verification-experiment
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
65f9c03
sig verification pool test
acolytec3 cc860ec
fix pooling
acolytec3 6d42407
use esm
acolytec3 033f652
benchmark
acolytec3 a6b2eb2
delete unused worker script
acolytec3 8888d90
relocate files and add readme
acolytec3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ecrecover } from '@ethereumjs/util' | ||
import { parentPort } from 'worker_threads' | ||
|
||
interface SignatureTask { | ||
msgHash: Uint8Array | ||
v: bigint | ||
r: Uint8Array | ||
s: Uint8Array | ||
chainId?: bigint | ||
} | ||
|
||
interface SignatureResult { | ||
publicKey: Uint8Array | ||
} | ||
|
||
function processSignature(task: SignatureTask): SignatureResult { | ||
const publicKey = ecrecover(task.msgHash, task.v, task.r, task.s, task.chainId) | ||
return { publicKey } | ||
} | ||
|
||
// Listen for messages from the main thread | ||
parentPort?.on('message', (tasks: SignatureTask[]) => { | ||
const results = tasks.map(processSignature) | ||
parentPort?.postMessage(results) | ||
}) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Worker } from 'worker_threads' | ||
|
||
interface SignatureTask { | ||
msgHash: Uint8Array | ||
v: bigint | ||
r: Uint8Array | ||
s: Uint8Array | ||
chainId?: bigint | ||
} | ||
|
||
interface SignatureResult { | ||
publicKey: Uint8Array | ||
} | ||
|
||
export class SignatureWorkerPool { | ||
private workers: Worker[] = [] | ||
private results: Map<number, SignatureResult> = new Map() | ||
private nextTaskId = 0 | ||
private pendingResults = 0 | ||
|
||
constructor(numWorkers: number = 4) { | ||
const workerCode = ` | ||
import { parentPort } from 'worker_threads' | ||
import { ecrecover } from '@ethereumjs/util' | ||
|
||
parentPort.on('message', (data) => { | ||
const { tasks, taskId } = data | ||
const results = tasks.map(task => { | ||
const publicKey = ecrecover(task.msgHash, task.v, task.r, task.s, task.chainId) | ||
return { publicKey } | ||
}) | ||
parentPort.postMessage({ results, taskId }) | ||
}) | ||
` | ||
|
||
// Initialize workers | ||
for (let i = 0; i < numWorkers; i++) { | ||
const worker = new Worker(workerCode, { eval: true }) | ||
|
||
worker.on('message', (data: { results: SignatureResult[]; taskId: number }) => { | ||
const { results, taskId } = data | ||
results.forEach((result, index) => { | ||
this.results.set(taskId + index, result) | ||
}) | ||
this.pendingResults-- | ||
}) | ||
|
||
this.workers.push(worker) | ||
} | ||
} | ||
|
||
async processBatch(tasks: SignatureTask[]): Promise<Map<number, SignatureResult>> { | ||
// Clear previous results | ||
this.results.clear() | ||
this.nextTaskId = 0 | ||
this.pendingResults = 0 | ||
|
||
// Calculate batch size per worker | ||
const batchSize = Math.ceil(tasks.length / this.workers.length) | ||
|
||
// Process tasks in parallel | ||
for (let i = 0; i < this.workers.length; i++) { | ||
const start = i * batchSize | ||
const end = Math.min(start + batchSize, tasks.length) | ||
if (start >= tasks.length) break | ||
|
||
const batch = tasks.slice(start, end) | ||
this.pendingResults++ | ||
this.workers[i].postMessage({ tasks: batch, taskId: this.nextTaskId }) | ||
this.nextTaskId += batch.length | ||
} | ||
|
||
// Wait for all results | ||
while (this.pendingResults > 0) { | ||
await new Promise((resolve) => setTimeout(resolve, 10)) | ||
} | ||
|
||
return this.results | ||
} | ||
|
||
terminate() { | ||
for (const worker of this.workers) { | ||
worker.terminate() | ||
} | ||
} | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { LegacyTx } from '@ethereumjs/tx' | ||
import { | ||
bigIntToUnpaddedBytes, | ||
bytesToHex, | ||
ecrecover, | ||
equalsBytes, | ||
randomBytes, | ||
} from '@ethereumjs/util' | ||
import { SignatureWorkerPool } from '../../src/worker/signatureWorkerPool.ts' | ||
|
||
async function runBenchmark() { | ||
// Create 10 test transactions | ||
const transactions = Array.from({ length: 2000 }, (_, i) => { | ||
return new LegacyTx({ | ||
nonce: i, | ||
gasPrice: 1000000000n, | ||
gasLimit: 21000n, | ||
to: '0x0000000000000000000000000000000000000000', | ||
value: 1000000000000000000n, | ||
data: '0x', | ||
}) | ||
}) | ||
|
||
// Sign all transactions | ||
const signedTxs = transactions.map((tx) => tx.sign(randomBytes(32))) | ||
|
||
// Precompute all hashes and signature values | ||
const msgHashes = signedTxs.map((tx) => tx.getMessageToVerifySignature()) | ||
const vs = signedTxs.map((tx) => tx.v!) | ||
const rs = signedTxs.map((tx) => bigIntToUnpaddedBytes(tx.r!)) | ||
const ss = signedTxs.map((tx) => bigIntToUnpaddedBytes(tx.s!)) | ||
const chainIds = signedTxs.map((tx) => tx.common.chainId()) | ||
|
||
console.log('Running benchmark...\n') | ||
|
||
// Sequential verification | ||
console.log('Sequential verification:') | ||
const startSeq = performance.now() | ||
|
||
// Store sequential results | ||
const sequentialResults = new Map() | ||
for (let i = 0; i < signedTxs.length; i++) { | ||
const publicKey = ecrecover(msgHashes[i], vs[i], rs[i], ss[i], 1n) | ||
sequentialResults.set(i, publicKey) | ||
} | ||
|
||
const endSeq = performance.now() | ||
console.log(`Time taken: ${endSeq - startSeq}ms\n`) | ||
|
||
// Parallel verification using worker pool | ||
console.log('Parallel verification using worker pool:') | ||
const startPar = performance.now() | ||
|
||
const signatureTasks = msgHashes.map((msgHash, i) => { | ||
return { | ||
msgHash, | ||
v: vs[i], | ||
r: rs[i], | ||
s: ss[i], | ||
chainId: chainIds[i], | ||
} | ||
}) | ||
|
||
const workerPool = new SignatureWorkerPool(4) // Use 4 workers | ||
const parallelResults = await workerPool.processBatch(signatureTasks) | ||
await workerPool.terminate() | ||
|
||
const endPar = performance.now() | ||
console.log(`Time taken: ${endPar - startPar}ms\n`) | ||
|
||
// Print speedup | ||
const speedup = (endSeq - startSeq) / (endPar - startPar) | ||
console.log(`Speedup: ${speedup.toFixed(2)}x`) | ||
|
||
// Print result counts | ||
console.log( | ||
`\nResults count - Sequential: ${sequentialResults.size}, Parallel: ${parallelResults.size}`, | ||
) | ||
|
||
// Verify results match by comparing the stored sequential results with parallel results | ||
console.log('\nVerifying results...') | ||
let allMatch = true | ||
let missingResults = 0 | ||
let mismatchedResults = 0 | ||
|
||
for (let i = 0; i < signedTxs.length; i++) { | ||
const parallelResult = parallelResults.get(i) | ||
const sequentialResult = sequentialResults.get(i) | ||
|
||
if (!parallelResult) { | ||
console.log(`Missing parallel result at index ${i}`) | ||
missingResults++ | ||
allMatch = false | ||
continue | ||
} | ||
|
||
if (!equalsBytes(parallelResult.publicKey, sequentialResult)) { | ||
console.log(`Mismatch at index ${i}`) | ||
console.log(`Sequential: ${sequentialResult}`) | ||
console.log(`Parallel: ${parallelResult.publicKey}`) | ||
mismatchedResults++ | ||
allMatch = false | ||
} | ||
} | ||
|
||
console.log(`\nVerification Summary:`) | ||
console.log(`- All results match: ${allMatch}`) | ||
console.log(`- Missing results: ${missingResults}`) | ||
console.log(`- Mismatched results: ${mismatchedResults}`) | ||
} | ||
|
||
runBenchmark().catch(console.error) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh, there is likely no way around this "string-ification of code"? 😬 Or will there be a more "solid" way to do this later on? Anyhow - even if not - guess the price would be worth it. If the snippets are not getting too extensive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pain point here is how worker threads work. The two ways to invoke worker threads are to pass the stringified code directly or invoke a separate file (e.g.
worker.js
). This issue shows how hard this once you integrate typescript into the equation. The worker thread environment doesn't inherit from the top level application sotsx
doesn't run (or at least not easily). Nor do any of the tricks for having node run typescript directly. I spent a couple of hours thinking this through and my options are to either compile the code to Javascript (thus meaning we havejs
source files in oursrc
directory (which is kinda weird) or I have to go the route suggested in the tsx issue and add this extratsx/cli
step to the thread loading step. Happy to go either way at this point.