Skip to content

Commit

Permalink
Initialize runTx() default block with simpler constructor to avoid dr…
Browse files Browse the repository at this point in the history
…awing all txs in
  • Loading branch information
holgerd77 committed Aug 17, 2024
1 parent 78939cb commit 6a803a1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
12 changes: 12 additions & 0 deletions packages/block/src/block/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ export function createBlock(blockData: BlockData = {}, opts?: BlockOptions) {
)
}

/**
* Simple static constructor if only an empty block is needed
* (tree shaking advantages since it does not draw all the tx constructors in)
*
* @param headerData
* @param opts
*/
export function createEmptyBlock(headerData: HeaderData, opts?: BlockOptions) {
const header = createBlockHeader(headerData, opts)
return new Block(header)
}

/**
* Static constructor to create a block from an array of Bytes values
*
Expand Down
4 changes: 4 additions & 0 deletions packages/block/test/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
createBlockFromBytesArray,
createBlockFromRLPSerializedBlock,
createBlockFromRPC,
createEmptyBlock,
paramsBlock,
} from '../src/index.js'

Expand All @@ -46,6 +47,9 @@ describe('[Block]: block functions', () => {
'should use custom parameters provided',
)

const emptyBlock = createEmptyBlock({}, { common })
assert.ok(bytesToHex(emptyBlock.hash()), 'block should initialize')

// test default freeze values
// also test if the options are carried over to the constructor
block = createBlock({})
Expand Down
4 changes: 2 additions & 2 deletions packages/vm/src/runTx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cliqueSigner, createBlock } from '@ethereumjs/block'
import { cliqueSigner, createEmptyBlock } from '@ethereumjs/block'
import { ConsensusType, Hardfork } from '@ethereumjs/common'
import { RLP } from '@ethereumjs/rlp'
import { Blob4844Tx, Capability, isBlob4844Tx } from '@ethereumjs/tx'
Expand Down Expand Up @@ -81,7 +81,7 @@ export async function runTx(vm: VM, opts: RunTxOpts): Promise<RunTxResult> {
}

// create a reasonable default if no block is given
opts.block = opts.block ?? createBlock({}, { common: vm.common })
opts.block = opts.block ?? createEmptyBlock({}, { common: vm.common })

if (opts.skipHardForkValidation !== true) {
// If block and tx don't have a same hardfork, set tx hardfork to block
Expand Down

0 comments on commit 6a803a1

Please sign in to comment.