Skip to content

Commit ade08b3

Browse files
committed
Add implementation to runBlock
1 parent 033f652 commit ade08b3

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

packages/vm/src/runBlock.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
equalsBytes,
2323
hexToBytes,
2424
intToBytes,
25+
publicToAddress,
2526
setLengthLeft,
2627
short,
2728
unprefixedHexToBytes,
@@ -33,6 +34,7 @@ import { Bloom } from './bloom/index.ts'
3334
import { emitEVMProfile } from './emitEVMProfile.ts'
3435
import { runTx } from './index.ts'
3536
import { accumulateRequests } from './requests.ts'
37+
import { SignatureWorkerPool } from './worker/signatureWorkerPool.ts'
3638

3739
import type { Block } from '@ethereumjs/block'
3840
import type { Common } from '@ethereumjs/common'
@@ -659,6 +661,37 @@ async function applyTransactions(vm: VM, block: Block, opts: RunBlockOpts) {
659661
const receipts: TxReceipt[] = []
660662
const txResults: RunTxResult[] = []
661663

664+
// Batch verify signatures if more than 100 transactions
665+
if (block.transactions.length > 1) {
666+
const workerPool = new SignatureWorkerPool()
667+
const tasks = block.transactions.map((tx) => {
668+
const msgHash = tx.getHashedMessageToSign()
669+
if (Array.isArray(msgHash)) {
670+
throw new Error('Transaction message hash cannot be an array')
671+
}
672+
return {
673+
msgHash,
674+
v: tx.v!,
675+
r: bigIntToBytes(tx.r!),
676+
s: bigIntToBytes(tx.s!),
677+
chainId: tx.common.chainId(),
678+
}
679+
})
680+
681+
const results = await workerPool.processBatch(tasks)
682+
683+
// Override getSenderAddress for each transaction
684+
block.transactions.forEach((tx, index) => {
685+
const result = results.get(index)
686+
if (result !== undefined) {
687+
tx.getSenderAddress = () => new Address(publicToAddress(result.publicKey))
688+
}
689+
})
690+
691+
// Clean up worker pool
692+
workerPool.terminate()
693+
}
694+
662695
/*
663696
* Process transactions
664697
*/

packages/vm/src/worker/signatureWorkerPool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export class SignatureWorkerPool {
2020

2121
constructor(numWorkers: number = 4) {
2222
const workerCode = `
23-
import { parentPort } from 'worker_threads'
24-
import { ecrecover } from '@ethereumjs/util'
23+
const { parentPort } = require('worker_threads')
24+
const { ecrecover } = require('@ethereumjs/util')
2525
2626
parentPort.on('message', (data) => {
2727
const { tasks, taskId } = data

packages/vm/test/api/runBlock.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ describe('runBlock() -> tx types', async () => {
683683
to: defaultAuthAddr,
684684
value: BIGINT_1,
685685
},
686-
{ common },
686+
{ common, freeze: false },
687687
).sign(defaultSenderPkey)
688688
const tx2 = createEOACode7702Tx(
689689
{
@@ -695,13 +695,13 @@ describe('runBlock() -> tx types', async () => {
695695
value: BIGINT_1,
696696
nonce: 1,
697697
},
698-
{ common },
698+
{ common, freeze: false },
699699
).sign(defaultSenderPkey)
700700
const block = createBlock(
701701
{
702702
transactions: [tx1, tx2],
703703
},
704-
{ common, setHardfork: false, skipConsensusFormatValidation: true },
704+
{ common, setHardfork: false, skipConsensusFormatValidation: true, freeze: false },
705705
)
706706

707707
await runBlock(vm, { block, skipBlockValidation: true, generate: true })

0 commit comments

Comments
 (0)