|
| 1 | +import { LegacyTx } from '@ethereumjs/tx' |
| 2 | +import { bigIntToUnpaddedBytes, ecrecover, equalsBytes, randomBytes } from '@ethereumjs/util' |
| 3 | +import { SignatureWorkerPool } from '../../src/worker/signatureWorkerPool.ts' |
| 4 | + |
| 5 | +async function runBenchmark() { |
| 6 | + // Create 10 test transactions |
| 7 | + const transactions = Array.from({ length: 1000 }, (_, i) => { |
| 8 | + return new LegacyTx({ |
| 9 | + nonce: i, |
| 10 | + gasPrice: 1000000000n, |
| 11 | + gasLimit: 21000n, |
| 12 | + to: '0x0000000000000000000000000000000000000000', |
| 13 | + value: 1000000000000000000n, |
| 14 | + data: '0x', |
| 15 | + }) |
| 16 | + }) |
| 17 | + |
| 18 | + // Sign all transactions |
| 19 | + const signedTxs = transactions.map((tx) => tx.sign(randomBytes(32))) |
| 20 | + |
| 21 | + // Precompute all hashes and signature values |
| 22 | + const msgHashes = signedTxs.map((tx) => tx.getMessageToVerifySignature()) |
| 23 | + const vs = signedTxs.map((tx) => tx.v!) |
| 24 | + const rs = signedTxs.map((tx) => bigIntToUnpaddedBytes(tx.r!)) |
| 25 | + const ss = signedTxs.map((tx) => bigIntToUnpaddedBytes(tx.s!)) |
| 26 | + const chainIds = signedTxs.map((tx) => tx.common.chainId()) |
| 27 | + |
| 28 | + console.log('Running benchmark...\n') |
| 29 | + |
| 30 | + // Sequential verification |
| 31 | + console.log('Sequential verification:') |
| 32 | + const startSeq = performance.now() |
| 33 | + |
| 34 | + // Store sequential results |
| 35 | + const sequentialResults = new Map() |
| 36 | + for (let i = 0; i < signedTxs.length; i++) { |
| 37 | + const publicKey = ecrecover(msgHashes[i], vs[i], rs[i], ss[i], 1n) |
| 38 | + sequentialResults.set(i, publicKey) |
| 39 | + } |
| 40 | + |
| 41 | + const endSeq = performance.now() |
| 42 | + console.log(`Time taken: ${endSeq - startSeq}ms\n`) |
| 43 | + |
| 44 | + // Parallel verification using worker pool |
| 45 | + console.log('Parallel verification using worker pool:') |
| 46 | + const startPar = performance.now() |
| 47 | + |
| 48 | + const signatureTasks = msgHashes.map((msgHash, i) => { |
| 49 | + return { |
| 50 | + msgHash, |
| 51 | + v: vs[i], |
| 52 | + r: rs[i], |
| 53 | + s: ss[i], |
| 54 | + chainId: chainIds[i], |
| 55 | + } |
| 56 | + }) |
| 57 | + |
| 58 | + const workerPool = new SignatureWorkerPool(4) // Use 4 workers |
| 59 | + const parallelResults = await workerPool.processBatch(signatureTasks) |
| 60 | + await workerPool.terminate() |
| 61 | + |
| 62 | + const endPar = performance.now() |
| 63 | + console.log(`Time taken: ${endPar - startPar}ms\n`) |
| 64 | + |
| 65 | + // Print speedup |
| 66 | + const speedup = (endSeq - startSeq) / (endPar - startPar) |
| 67 | + console.log(`Speedup: ${speedup.toFixed(2)}x`) |
| 68 | + |
| 69 | + // Verify results match by comparing the stored sequential results with parallel results |
| 70 | + console.log('\nVerifying results...') |
| 71 | + let allMatch = true |
| 72 | + for (let i = 0; i < signedTxs.length; i++) { |
| 73 | + const parallelResult = parallelResults.get(i) |
| 74 | + const sequentialResult = sequentialResults.get(i) |
| 75 | + if (parallelResult && !equalsBytes(parallelResult.publicKey, sequentialResult)) { |
| 76 | + console.log(`Mismatch at index ${i}`) |
| 77 | + allMatch = false |
| 78 | + } |
| 79 | + } |
| 80 | + console.log(`All results match: ${allMatch}`) |
| 81 | +} |
| 82 | + |
| 83 | +runBenchmark().catch(console.error) |
0 commit comments