diff --git a/packages/block/src/block.ts b/packages/block/src/block/block.ts similarity index 98% rename from packages/block/src/block.ts rename to packages/block/src/block/block.ts index 1c3cb22faf..a5521a7d35 100644 --- a/packages/block/src/block.ts +++ b/packages/block/src/block/block.ts @@ -12,8 +12,6 @@ import { } from '@ethereumjs/util' import { keccak256 } from 'ethereum-cryptography/keccak.js' -import { genRequestsTrieRoot, genTransactionsTrieRoot, genWithdrawalsTrieRoot } from './helpers.js' - /* eslint-disable */ // This is to allow for a proper and linked collection of constructors for the class header. // For tree shaking/code size this should be no problem since types go away on transpilation. @@ -21,6 +19,9 @@ import { genRequestsTrieRoot, genTransactionsTrieRoot, genWithdrawalsTrieRoot } // See: https://github.com/microsoft/TypeScript/issues/47558 // (situation will eventually improve on Typescript and/or Eslint update) import { + genRequestsTrieRoot, + genTransactionsTrieRoot, + genWithdrawalsTrieRoot, BlockHeader, type createBlockFromBeaconPayloadJson, type createBlock, @@ -28,10 +29,10 @@ import { type createBlockFromJsonRpcProvider, type createBlockFromRLPSerializedBlock, type createBlockFromRPC, - type createBlockFromValuesArray, -} from './index.js' + type createBlockFromBytesArray, +} from '../index.js' /* eslint-enable */ -import type { BlockBytes, BlockOptions, ExecutionPayload, JsonBlock } from './types.js' +import type { BlockBytes, BlockOptions, ExecutionPayload, JsonBlock } from '../types.js' import type { Common } from '@ethereumjs/common' import type { FeeMarketEIP1559Transaction, @@ -56,7 +57,7 @@ import type { * (separate from the Block class to allow for tree shaking): * * - {@link createBlock } - * - {@link createBlockFromValuesArray } + * - {@link createBlockFromBytesArray } * - {@link createBlockFromRLPSerializedBlock } * - {@link createBlockFromRPC } * - {@link createBlockFromJsonRpcProvider } diff --git a/packages/block/src/constructors.ts b/packages/block/src/block/constructors.ts similarity index 78% rename from packages/block/src/constructors.ts rename to packages/block/src/block/constructors.ts index 4f346e1308..2a50ee08d0 100644 --- a/packages/block/src/constructors.ts +++ b/packages/block/src/block/constructors.ts @@ -10,9 +10,9 @@ import { CLRequestFactory, ConsolidationRequest, DepositRequest, + TypeOutput, Withdrawal, WithdrawalRequest, - bigIntToBytes, bigIntToHex, bytesToHex, bytesToUtf8, @@ -22,24 +22,25 @@ import { hexToBytes, intToHex, isHexString, + setLengthLeft, + toBytes, + toType, } from '@ethereumjs/util' -import { generateCliqueBlockExtraData } from './consensus/clique.js' -import { createBlockFromRpc } from './from-rpc.js' +import { generateCliqueBlockExtraData } from '../consensus/clique.js' +import { genRequestsTrieRoot, genTransactionsTrieRoot, genWithdrawalsTrieRoot } from '../helpers.js' import { - genRequestsTrieRoot, - genTransactionsTrieRoot, - genWithdrawalsTrieRoot, - valuesArrayToHeaderData, -} from './helpers.js' - -import { Block, BlockHeader, executionPayloadFromBeaconPayload } from './index.js' - -import type { BeaconPayloadJson } from './from-beacon-payload.js' + Block, + blockHeaderFromRpc, + createBlockHeader, + createBlockHeaderFromBytesArray, + executionPayloadFromBeaconPayload, +} from '../index.js' + +import type { BeaconPayloadJson } from '../from-beacon-payload.js' import type { BlockBytes, BlockData, - BlockHeaderBytes, BlockOptions, ExecutionPayload, ExecutionWitnessBytes, @@ -47,7 +48,8 @@ import type { JsonRpcBlock, RequestsBytes, WithdrawalsBytes, -} from './types.js' +} from '../types.js' +import type { TypedTransaction } from '@ethereumjs/tx' import type { CLRequest, CLRequestType, @@ -57,71 +59,25 @@ import type { WithdrawalBytes, } from '@ethereumjs/util' -/** - * Static constructor to create a block header from a header data dictionary - * - * @param headerData - * @param opts - */ -export function createBlockHeader(headerData: HeaderData = {}, opts: BlockOptions = {}) { - return new BlockHeader(headerData, opts) -} +export function normalizeTxParams(_txParams: any) { + const txParams = Object.assign({}, _txParams) -/** - * Static constructor to create a block header from an array of Bytes values - * - * @param values - * @param opts - */ -export function createBlockHeaderFromValuesArray( - values: BlockHeaderBytes, - opts: BlockOptions = {}, -) { - const headerData = valuesArrayToHeaderData(values) - const { number, baseFeePerGas, excessBlobGas, blobGasUsed, parentBeaconBlockRoot, requestsRoot } = - headerData - const header = createBlockHeader(headerData, opts) - if (header.common.isActivatedEIP(1559) && baseFeePerGas === undefined) { - const eip1559ActivationBlock = bigIntToBytes(header.common.eipBlock(1559)!) - if ( - eip1559ActivationBlock !== undefined && - equalsBytes(eip1559ActivationBlock, number as Uint8Array) - ) { - throw new Error('invalid header. baseFeePerGas should be provided') - } - } - if (header.common.isActivatedEIP(4844)) { - if (excessBlobGas === undefined) { - throw new Error('invalid header. excessBlobGas should be provided') - } else if (blobGasUsed === undefined) { - throw new Error('invalid header. blobGasUsed should be provided') - } - } - if (header.common.isActivatedEIP(4788) && parentBeaconBlockRoot === undefined) { - throw new Error('invalid header. parentBeaconBlockRoot should be provided') - } + txParams.gasLimit = toType(txParams.gasLimit ?? txParams.gas, TypeOutput.BigInt) + txParams.data = txParams.data === undefined ? txParams.input : txParams.data - if (header.common.isActivatedEIP(7685) && requestsRoot === undefined) { - throw new Error('invalid header. requestsRoot should be provided') - } - return header -} + // check and convert gasPrice and value params + txParams.gasPrice = txParams.gasPrice !== undefined ? BigInt(txParams.gasPrice) : undefined + txParams.value = txParams.value !== undefined ? BigInt(txParams.value) : undefined -/** - * Static constructor to create a block header from a RLP-serialized header - * - * @param serializedHeaderData - * @param opts - */ -export function createBlockHeaderFromRLP( - serializedHeaderData: Uint8Array, - opts: BlockOptions = {}, -) { - const values = RLP.decode(serializedHeaderData) - if (!Array.isArray(values)) { - throw new Error('Invalid serialized header input. Must be array') - } - return createBlockHeaderFromValuesArray(values as Uint8Array[], opts) + // strict byte length checking + txParams.to = + txParams.to !== null && txParams.to !== undefined + ? setLengthLeft(toBytes(txParams.to), 20) + : null + + txParams.v = toType(txParams.v, TypeOutput.BigInt) + + return txParams } /** @@ -193,7 +149,7 @@ export function createBlock(blockData: BlockData = {}, opts?: BlockOptions) { * @param values * @param opts */ -export function createBlockFromValuesArray(values: BlockBytes, opts?: BlockOptions) { +export function createBlockFromBytesArray(values: BlockBytes, opts?: BlockOptions) { if (values.length > 5) { throw new Error(`invalid More values=${values.length} than expected were received (at most 5)`) } @@ -201,7 +157,7 @@ export function createBlockFromValuesArray(values: BlockBytes, opts?: BlockOptio // First try to load header so that we can use its common (in case of setHardfork being activated) // to correctly make checks on the hardforks const [headerData, txsData, uhsData, ...valuesTail] = values - const header = createBlockHeaderFromValuesArray(headerData, opts) + const header = createBlockHeaderFromBytesArray(headerData, opts) // conditional assignment of rest of values and splicing them out from the valuesTail const withdrawalBytes = header.common.isActivatedEIP(4895) @@ -266,7 +222,7 @@ export function createBlockFromValuesArray(values: BlockBytes, opts?: BlockOptio uncleOpts.setHardfork = true } for (const uncleHeaderData of uhsData ?? []) { - uncleHeaders.push(createBlockHeaderFromValuesArray(uncleHeaderData, uncleOpts)) + uncleHeaders.push(createBlockHeaderFromBytesArray(uncleHeaderData, uncleOpts)) } const withdrawals = (withdrawalBytes as WithdrawalBytes[]) @@ -323,7 +279,7 @@ export function createBlockFromRLPSerializedBlock(serialized: Uint8Array, opts?: throw new Error('Invalid serialized block input. Must be array') } - return createBlockFromValuesArray(values, opts) + return createBlockFromBytesArray(values, opts) } /** @@ -333,8 +289,31 @@ export function createBlockFromRLPSerializedBlock(serialized: Uint8Array, opts?: * @param uncles - Optional list of Ethereum JSON RPC of uncles (eth_getUncleByBlockHashAndIndex) * @param opts - An object describing the blockchain */ -export function createBlockFromRPC(blockData: JsonRpcBlock, uncles?: any[], opts?: BlockOptions) { - return createBlockFromRpc(blockData, uncles, opts) +export function createBlockFromRPC( + blockParams: JsonRpcBlock, + uncles: any[] = [], + options?: BlockOptions, +) { + const header = blockHeaderFromRpc(blockParams, options) + + const transactions: TypedTransaction[] = [] + const opts = { common: header.common } + for (const _txParams of blockParams.transactions ?? []) { + const txParams = normalizeTxParams(_txParams) + const tx = createTxFromTxData(txParams, opts) + transactions.push(tx) + } + + const uncleHeaders = uncles.map((uh) => blockHeaderFromRpc(uh, options)) + + const requests = blockParams.requests?.map((req) => { + const bytes = hexToBytes(req as PrefixedHexString) + return CLRequestFactory.fromSerializedRequest(bytes) + }) + return createBlock( + { header, transactions, uncleHeaders, withdrawals: blockParams.withdrawals, requests }, + options, + ) } /** @@ -395,7 +374,7 @@ export const createBlockFromJsonRpcProvider = async ( } } - return createBlockFromRpc(blockData, uncleHeaders, opts) + return createBlockFromRPC(blockData, uncleHeaders, opts) } /** @@ -540,22 +519,3 @@ export function createSealedCliqueBlock( } return sealedCliqueBlock } - -export function createSealedCliqueBlockHeader( - headerData: HeaderData = {}, - cliqueSigner: Uint8Array, - opts: BlockOptions = {}, -): BlockHeader { - const sealedCliqueBlockHeader = new BlockHeader(headerData, { - ...opts, - ...{ skipConsensusFormatValidation: true }, - }) - ;(sealedCliqueBlockHeader.extraData as any) = generateCliqueBlockExtraData( - sealedCliqueBlockHeader, - cliqueSigner, - ) - if (opts.skipConsensusFormatValidation === false) - // We need to validate the consensus format here since we skipped it when constructing the block header - sealedCliqueBlockHeader['_consensusFormatValidation']() - return sealedCliqueBlockHeader -} diff --git a/packages/block/src/block/index.ts b/packages/block/src/block/index.ts new file mode 100644 index 0000000000..2209b04cea --- /dev/null +++ b/packages/block/src/block/index.ts @@ -0,0 +1,2 @@ +export * from './block.js' +export * from './constructors.js' diff --git a/packages/block/src/from-rpc.ts b/packages/block/src/from-rpc.ts deleted file mode 100644 index ee162df098..0000000000 --- a/packages/block/src/from-rpc.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { type TypedTransaction, createTxFromTxData } from '@ethereumjs/tx' -import { - CLRequestFactory, - TypeOutput, - hexToBytes, - setLengthLeft, - toBytes, - toType, -} from '@ethereumjs/util' - -import { createBlock } from './constructors.js' -import { blockHeaderFromRpc } from './header-from-rpc.js' - -import type { BlockOptions, JsonRpcBlock } from './index.js' -import type { PrefixedHexString } from '@ethereumjs/util' - -function normalizeTxParams(_txParams: any) { - const txParams = Object.assign({}, _txParams) - - txParams.gasLimit = toType(txParams.gasLimit ?? txParams.gas, TypeOutput.BigInt) - txParams.data = txParams.data === undefined ? txParams.input : txParams.data - - // check and convert gasPrice and value params - txParams.gasPrice = txParams.gasPrice !== undefined ? BigInt(txParams.gasPrice) : undefined - txParams.value = txParams.value !== undefined ? BigInt(txParams.value) : undefined - - // strict byte length checking - txParams.to = - txParams.to !== null && txParams.to !== undefined - ? setLengthLeft(toBytes(txParams.to), 20) - : null - - txParams.v = toType(txParams.v, TypeOutput.BigInt) - - return txParams -} - -/** - * Creates a new block object from Ethereum JSON RPC. - * - * @param blockParams - Ethereum JSON RPC of block (eth_getBlockByNumber) - * @param uncles - Optional list of Ethereum JSON RPC of uncles (eth_getUncleByBlockHashAndIndex) - * @param options - An object describing the blockchain - * @deprecated - */ -export function createBlockFromRpc( - blockParams: JsonRpcBlock, - uncles: any[] = [], - options?: BlockOptions, -) { - const header = blockHeaderFromRpc(blockParams, options) - - const transactions: TypedTransaction[] = [] - const opts = { common: header.common } - for (const _txParams of blockParams.transactions ?? []) { - const txParams = normalizeTxParams(_txParams) - const tx = createTxFromTxData(txParams, opts) - transactions.push(tx) - } - - const uncleHeaders = uncles.map((uh) => blockHeaderFromRpc(uh, options)) - - const requests = blockParams.requests?.map((req) => { - const bytes = hexToBytes(req as PrefixedHexString) - return CLRequestFactory.fromSerializedRequest(bytes) - }) - return createBlock( - { header, transactions, uncleHeaders, withdrawals: blockParams.withdrawals, requests }, - options, - ) -} diff --git a/packages/block/src/header-from-rpc.ts b/packages/block/src/header-from-rpc.ts deleted file mode 100644 index 94b5b63659..0000000000 --- a/packages/block/src/header-from-rpc.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { numberToHex } from './helpers.js' - -import { BlockHeader } from './index.js' - -import type { BlockOptions, JsonRpcBlock } from './types.js' - -/** - * Creates a new block header object from Ethereum JSON RPC. - * - * @param blockParams - Ethereum JSON RPC of block (eth_getBlockByNumber) - * @param options - An object describing the blockchain - */ -export function blockHeaderFromRpc(blockParams: JsonRpcBlock, options?: BlockOptions) { - const { - parentHash, - sha3Uncles, - miner, - stateRoot, - transactionsRoot, - receiptsRoot, - logsBloom, - difficulty, - number, - gasLimit, - gasUsed, - timestamp, - extraData, - mixHash, - nonce, - baseFeePerGas, - withdrawalsRoot, - blobGasUsed, - excessBlobGas, - parentBeaconBlockRoot, - requestsRoot, - } = blockParams - - const blockHeader = new BlockHeader( - { - parentHash, - uncleHash: sha3Uncles, - coinbase: miner, - stateRoot, - transactionsTrie: transactionsRoot, - receiptTrie: receiptsRoot, - logsBloom, - difficulty: numberToHex(difficulty), - number, - gasLimit, - gasUsed, - timestamp, - extraData, - mixHash, - nonce, - baseFeePerGas, - withdrawalsRoot, - blobGasUsed, - excessBlobGas, - parentBeaconBlockRoot, - requestsRoot, - }, - options, - ) - - return blockHeader -} diff --git a/packages/block/src/header/constructors.ts b/packages/block/src/header/constructors.ts new file mode 100644 index 0000000000..6d32b39350 --- /dev/null +++ b/packages/block/src/header/constructors.ts @@ -0,0 +1,152 @@ +import { RLP } from '@ethereumjs/rlp' +import { bigIntToBytes, equalsBytes } from '@ethereumjs/util' + +import { generateCliqueBlockExtraData } from '../consensus/clique.js' +import { numberToHex, valuesArrayToHeaderData } from '../helpers.js' +import { BlockHeader } from '../index.js' + +import type { BlockHeaderBytes, BlockOptions, HeaderData, JsonRpcBlock } from '../types.js' + +/** + * Static constructor to create a block header from a header data dictionary + * + * @param headerData + * @param opts + */ +export function createBlockHeader(headerData: HeaderData = {}, opts: BlockOptions = {}) { + return new BlockHeader(headerData, opts) +} + +/** + * Static constructor to create a block header from an array of bytes values + * + * @param values + * @param opts + */ +export function createBlockHeaderFromBytesArray(values: BlockHeaderBytes, opts: BlockOptions = {}) { + const headerData = valuesArrayToHeaderData(values) + const { number, baseFeePerGas, excessBlobGas, blobGasUsed, parentBeaconBlockRoot, requestsRoot } = + headerData + const header = createBlockHeader(headerData, opts) + if (header.common.isActivatedEIP(1559) && baseFeePerGas === undefined) { + const eip1559ActivationBlock = bigIntToBytes(header.common.eipBlock(1559)!) + if ( + eip1559ActivationBlock !== undefined && + equalsBytes(eip1559ActivationBlock, number as Uint8Array) + ) { + throw new Error('invalid header. baseFeePerGas should be provided') + } + } + if (header.common.isActivatedEIP(4844)) { + if (excessBlobGas === undefined) { + throw new Error('invalid header. excessBlobGas should be provided') + } else if (blobGasUsed === undefined) { + throw new Error('invalid header. blobGasUsed should be provided') + } + } + if (header.common.isActivatedEIP(4788) && parentBeaconBlockRoot === undefined) { + throw new Error('invalid header. parentBeaconBlockRoot should be provided') + } + + if (header.common.isActivatedEIP(7685) && requestsRoot === undefined) { + throw new Error('invalid header. requestsRoot should be provided') + } + return header +} + +/** + * Static constructor to create a block header from a RLP-serialized header + * + * @param serializedHeaderData + * @param opts + */ +export function createBlockHeaderFromRLP( + serializedHeaderData: Uint8Array, + opts: BlockOptions = {}, +) { + const values = RLP.decode(serializedHeaderData) + if (!Array.isArray(values)) { + throw new Error('Invalid serialized header input. Must be array') + } + return createBlockHeaderFromBytesArray(values as Uint8Array[], opts) +} + +export function createSealedCliqueBlockHeader( + headerData: HeaderData = {}, + cliqueSigner: Uint8Array, + opts: BlockOptions = {}, +): BlockHeader { + const sealedCliqueBlockHeader = new BlockHeader(headerData, { + ...opts, + ...{ skipConsensusFormatValidation: true }, + }) + ;(sealedCliqueBlockHeader.extraData as any) = generateCliqueBlockExtraData( + sealedCliqueBlockHeader, + cliqueSigner, + ) + if (opts.skipConsensusFormatValidation === false) + // We need to validate the consensus format here since we skipped it when constructing the block header + sealedCliqueBlockHeader['_consensusFormatValidation']() + return sealedCliqueBlockHeader +} + +/** + * Creates a new block header object from Ethereum JSON RPC. + * + * @param blockParams - Ethereum JSON RPC of block (eth_getBlockByNumber) + * @param options - An object describing the blockchain + */ +export function blockHeaderFromRpc(blockParams: JsonRpcBlock, options?: BlockOptions) { + const { + parentHash, + sha3Uncles, + miner, + stateRoot, + transactionsRoot, + receiptsRoot, + logsBloom, + difficulty, + number, + gasLimit, + gasUsed, + timestamp, + extraData, + mixHash, + nonce, + baseFeePerGas, + withdrawalsRoot, + blobGasUsed, + excessBlobGas, + parentBeaconBlockRoot, + requestsRoot, + } = blockParams + + const blockHeader = new BlockHeader( + { + parentHash, + uncleHash: sha3Uncles, + coinbase: miner, + stateRoot, + transactionsTrie: transactionsRoot, + receiptTrie: receiptsRoot, + logsBloom, + difficulty: numberToHex(difficulty), + number, + gasLimit, + gasUsed, + timestamp, + extraData, + mixHash, + nonce, + baseFeePerGas, + withdrawalsRoot, + blobGasUsed, + excessBlobGas, + parentBeaconBlockRoot, + requestsRoot, + }, + options, + ) + + return blockHeader +} diff --git a/packages/block/src/header.ts b/packages/block/src/header/header.ts similarity index 99% rename from packages/block/src/header.ts rename to packages/block/src/header/header.ts index eed3e04c24..c3cdeb9123 100644 --- a/packages/block/src/header.ts +++ b/packages/block/src/header/header.ts @@ -25,11 +25,11 @@ import { CLIQUE_EXTRA_SEAL, CLIQUE_EXTRA_VANITY, cliqueIsEpochTransition, -} from './consensus/clique.js' -import { fakeExponential } from './helpers.js' -import { paramsBlock } from './params.js' +} from '../consensus/clique.js' +import { fakeExponential } from '../helpers.js' +import { paramsBlock } from '../params.js' -import type { BlockHeaderBytes, BlockOptions, HeaderData, JsonHeader } from './types.js' +import type { BlockHeaderBytes, BlockOptions, HeaderData, JsonHeader } from '../types.js' interface HeaderCache { hash: Uint8Array | undefined diff --git a/packages/block/src/header/index.ts b/packages/block/src/header/index.ts new file mode 100644 index 0000000000..3d53654058 --- /dev/null +++ b/packages/block/src/header/index.ts @@ -0,0 +1,2 @@ +export * from './constructors.js' +export * from './header.js' diff --git a/packages/block/src/index.ts b/packages/block/src/index.ts index 4ea497e515..50379f3719 100644 --- a/packages/block/src/index.ts +++ b/packages/block/src/index.ts @@ -1,8 +1,8 @@ -export { Block } from './block.js' +export { Block } from './block/block.js' +export * from './block/index.js' export * from './consensus/index.js' -export * from './constructors.js' export { type BeaconPayloadJson, executionPayloadFromBeaconPayload } from './from-beacon-payload.js' -export { BlockHeader } from './header.js' +export * from './header/index.js' export { genRequestsTrieRoot, genTransactionsTrieRoot, diff --git a/packages/block/src/types.ts b/packages/block/src/types.ts index 27d15a95b2..abcb550709 100644 --- a/packages/block/src/types.ts +++ b/packages/block/src/types.ts @@ -1,4 +1,4 @@ -import type { BlockHeader } from './header.js' +import type { BlockHeader } from './index.js' import type { Common, ParamsDict } from '@ethereumjs/common' import type { JsonRpcTx, JsonTx, TransactionType, TxData } from '@ethereumjs/tx' import type { diff --git a/packages/block/test/block.spec.ts b/packages/block/test/block.spec.ts index e3c6ac19fd..e75feda9dc 100644 --- a/packages/block/test/block.spec.ts +++ b/packages/block/test/block.spec.ts @@ -11,14 +11,17 @@ import { } from '@ethereumjs/util' import { assert, describe, it } from 'vitest' +import { genTransactionsTrieRoot } from '../src/helpers.js' import { + type Block, + type BlockBytes, + type JsonRpcBlock, createBlock, + createBlockFromBytesArray, createBlockFromRLPSerializedBlock, - createBlockFromValuesArray, -} from '../src/constructors.js' -import { createBlockFromRpc } from '../src/from-rpc.js' -import { genTransactionsTrieRoot } from '../src/helpers.js' -import { type Block, type BlockBytes, type JsonRpcBlock, paramsBlock } from '../src/index.js' + createBlockFromRPC, + paramsBlock, +} from '../src/index.js' import * as testDataGenesis from './testdata/genesishashestest.json' import * as testDataFromRpcGoerli from './testdata/testdata-from-rpc-goerli.json' @@ -81,10 +84,10 @@ describe('[Block]: block functions', () => { const valuesArray = [headerArray, [], []] - block = createBlockFromValuesArray(valuesArray, { common }) + block = createBlockFromBytesArray(valuesArray, { common }) assert.ok(Object.isFrozen(block), 'block should be frozen by default') - block = createBlockFromValuesArray(valuesArray, { common, freeze: false }) + block = createBlockFromBytesArray(valuesArray, { common, freeze: false }) assert.ok( !Object.isFrozen(block), 'block should not be frozen when freeze deactivated in options', @@ -160,7 +163,7 @@ describe('[Block]: block functions', () => { const common = new Common({ chain: Goerli, hardfork: Hardfork.Chainstart }) try { - createBlockFromRpc(testDataFromRpcGoerli.default as JsonRpcBlock, [], { common }) + createBlockFromRPC(testDataFromRpcGoerli.default as JsonRpcBlock, [], { common }) assert.ok(true, 'does not throw') } catch (error: any) { assert.fail('error thrown') @@ -353,7 +356,7 @@ describe('[Block]: block functions', () => { ) assert.throws( () => { - createBlockFromValuesArray([1, 2, 3, 4] as any) + createBlockFromBytesArray([1, 2, 3, 4] as any) }, undefined, undefined, @@ -369,7 +372,7 @@ describe('[Block]: block functions', () => { common, }, ) - const createBlockFromRaw = createBlockFromValuesArray(block.raw(), { common }) + const createBlockFromRaw = createBlockFromBytesArray(block.raw(), { common }) assert.ok(equalsBytes(block.hash(), createBlockFromRaw.hash())) }) @@ -394,7 +397,7 @@ describe('[Block]: block functions', () => { const common = new Common({ chain: Mainnet, hardfork: Hardfork.Dao }) assert.throws( function () { - createBlockFromValuesArray(blockData as BlockBytes, { common }) + createBlockFromBytesArray(blockData as BlockBytes, { common }) }, /extraData should be 'dao-hard-fork/, undefined, @@ -405,7 +408,7 @@ describe('[Block]: block functions', () => { blockData[0][12] = hexToBytes('0x64616f2d686172642d666f726b') assert.doesNotThrow(function () { - createBlockFromValuesArray(blockData as BlockBytes, { common }) + createBlockFromBytesArray(blockData as BlockBytes, { common }) }, 'should not throw on DAO HF block with correct extra data') }) diff --git a/packages/block/test/clique.spec.ts b/packages/block/test/clique.spec.ts index 23ad345b01..4f55dcac8d 100644 --- a/packages/block/test/clique.spec.ts +++ b/packages/block/test/clique.spec.ts @@ -2,7 +2,6 @@ import { Common, Goerli, Hardfork } from '@ethereumjs/common' import { Address, createZeroAddress, hexToBytes } from '@ethereumjs/util' import { assert, describe, it } from 'vitest' -import { createBlockHeader, createSealedCliqueBlockHeader } from '../src/constructors.js' import { cliqueEpochTransitionSigners, cliqueExtraSeal, @@ -10,6 +9,8 @@ import { cliqueIsEpochTransition, cliqueSigner, cliqueVerifySignature, + createBlockHeader, + createSealedCliqueBlockHeader, } from '../src/index.js' describe('[Header]: Clique PoA Functionality', () => { diff --git a/packages/block/test/difficulty.spec.ts b/packages/block/test/difficulty.spec.ts index 40fcb27512..3bb4afe830 100644 --- a/packages/block/test/difficulty.spec.ts +++ b/packages/block/test/difficulty.spec.ts @@ -11,8 +11,7 @@ import * as difficultyEIP2384_random_to20M from '../../ethereum-tests/Difficulty import * as difficultyFrontier from '../../ethereum-tests/DifficultyTests/dfFrontier/difficultyFrontier.json' import * as difficultyGrayGlacier from '../../ethereum-tests/DifficultyTests/dfGrayGlacier/difficultyGrayGlacier.json' import * as difficultyHomestead from '../../ethereum-tests/DifficultyTests/dfHomestead/difficultyHomestead.json' -import { createBlock } from '../src/constructors.js' -import { type Block, ethashCanonicalDifficulty } from '../src/index.js' +import { type Block, createBlock, ethashCanonicalDifficulty } from '../src/index.js' function runDifficultyTests(test: any, parentBlock: Block, block: Block, msg: string) { const dif = ethashCanonicalDifficulty(block, parentBlock) diff --git a/packages/block/test/eip1559block.spec.ts b/packages/block/test/eip1559block.spec.ts index b01ccbeb29..4182e35ddb 100644 --- a/packages/block/test/eip1559block.spec.ts +++ b/packages/block/test/eip1559block.spec.ts @@ -3,7 +3,7 @@ import { create1559FeeMarketTx } from '@ethereumjs/tx' import { hexToBytes } from '@ethereumjs/util' import { assert, describe, it } from 'vitest' -import { createBlock, createBlockHeader } from '../src/constructors.js' +import { createBlock, createBlockHeader } from '../src/index.js' // Test data from Besu (retrieved via Discord) // Older version at https://github.com/abdelhamidbakhta/besu/blob/bf54b6c0b40d3015fc85ff9b078fbc26592d80c0/ethereum/core/src/test/resources/org/hyperledger/besu/ethereum/core/fees/basefee-test.json import { paramsBlock } from '../src/params.js' diff --git a/packages/block/test/eip4788block.spec.ts b/packages/block/test/eip4788block.spec.ts index bb4bf0f0e4..3100c08c76 100644 --- a/packages/block/test/eip4788block.spec.ts +++ b/packages/block/test/eip4788block.spec.ts @@ -2,7 +2,7 @@ import { Common, Hardfork, Mainnet } from '@ethereumjs/common' import { bytesToHex, zeros } from '@ethereumjs/util' import { assert, describe, it } from 'vitest' -import { createBlock, createBlockHeader } from '../src/constructors.js' +import { createBlock, createBlockHeader } from '../src/index.js' describe('EIP4788 header tests', () => { it('should work', () => { diff --git a/packages/block/test/eip4844block.spec.ts b/packages/block/test/eip4844block.spec.ts index d3c5c6542b..6d99306213 100644 --- a/packages/block/test/eip4844block.spec.ts +++ b/packages/block/test/eip4844block.spec.ts @@ -9,8 +9,8 @@ import { import { loadKZG } from 'kzg-wasm' import { assert, beforeAll, describe, it } from 'vitest' -import { createBlock, createBlockHeader } from '../src/constructors.js' import { fakeExponential, getNumBlobs } from '../src/helpers.js' +import { createBlock, createBlockHeader } from '../src/index.js' import { paramsBlock } from '../src/params.js' import gethGenesis from './testdata/4844-hardfork.json' diff --git a/packages/block/test/eip4895block.spec.ts b/packages/block/test/eip4895block.spec.ts index 60aa4b4cd8..4be6264c65 100644 --- a/packages/block/test/eip4895block.spec.ts +++ b/packages/block/test/eip4895block.spec.ts @@ -10,12 +10,8 @@ import { } from '@ethereumjs/util' import { assert, describe, it } from 'vitest' -import { - createBlock, - createBlockFromRLPSerializedBlock, - createBlockHeader, -} from '../src/constructors.js' import { genWithdrawalsTrieRoot } from '../src/helpers.js' +import { createBlock, createBlockFromRLPSerializedBlock, createBlockHeader } from '../src/index.js' import type { WithdrawalBytes, WithdrawalData } from '@ethereumjs/util' diff --git a/packages/block/test/eip7685block.spec.ts b/packages/block/test/eip7685block.spec.ts index 14aa9759e1..928e2db558 100644 --- a/packages/block/test/eip7685block.spec.ts +++ b/packages/block/test/eip7685block.spec.ts @@ -8,14 +8,14 @@ import { } from '@ethereumjs/util' import { assert, describe, expect, it } from 'vitest' +import { genRequestsTrieRoot } from '../src/helpers.js' import { + Block, createBlock, + createBlockFromBytesArray, createBlockFromRPC, - createBlockFromValuesArray, createBlockHeader, -} from '../src/constructors.js' -import { genRequestsTrieRoot } from '../src/helpers.js' -import { Block } from '../src/index.js' +} from '../src/index.js' import type { CLRequest, CLRequestType } from '@ethereumjs/util' @@ -111,7 +111,7 @@ describe('7685 tests', () => { describe('fromValuesArray tests', () => { it('should construct a block with empty requests root', () => { - const block = createBlockFromValuesArray( + const block = createBlockFromBytesArray( [createBlockHeader({}, { common }).raw(), [], [], [], []], { common, @@ -127,7 +127,7 @@ describe('fromValuesArray tests', () => { const requestsRoot = await genRequestsTrieRoot(requests) const serializedRequests = [request1.serialize(), request2.serialize(), request3.serialize()] - const block = createBlockFromValuesArray( + const block = createBlockFromBytesArray( [createBlockHeader({ requestsRoot }, { common }).raw(), [], [], [], serializedRequests], { common, @@ -147,7 +147,7 @@ describe('fromRPC tests', () => { const requestsRoot = await genRequestsTrieRoot(requests) const serializedRequests = [request1.serialize(), request2.serialize(), request3.serialize()] - const block = createBlockFromValuesArray( + const block = createBlockFromBytesArray( [createBlockHeader({ requestsRoot }, { common }).raw(), [], [], [], serializedRequests], { common, diff --git a/packages/block/test/from-beacon-payload.spec.ts b/packages/block/test/from-beacon-payload.spec.ts index 1851897b14..1d78598650 100644 --- a/packages/block/test/from-beacon-payload.spec.ts +++ b/packages/block/test/from-beacon-payload.spec.ts @@ -3,7 +3,7 @@ import { loadKZG } from 'kzg-wasm' import { assert, beforeAll, describe, it } from 'vitest' import * as shardingJson from '../../client/test/sim/configs/4844-devnet.json' -import { createBlockFromBeaconPayloadJson, createBlockHeader } from '../src/constructors.js' +import { createBlockFromBeaconPayloadJson, createBlockHeader } from '../src/index.js' import * as payloadKaustinen from './testdata/payload-kaustinen.json' import * as payload87335 from './testdata/payload-slot-87335.json' diff --git a/packages/block/test/from-rpc.spec.ts b/packages/block/test/from-rpc.spec.ts index cd3b2ad5a4..4970a59a79 100644 --- a/packages/block/test/from-rpc.spec.ts +++ b/packages/block/test/from-rpc.spec.ts @@ -2,9 +2,11 @@ import { Common, Goerli, Hardfork, Mainnet } from '@ethereumjs/common' import { bytesToHex, equalsBytes, hexToBytes, randomBytes } from '@ethereumjs/util' import { assert, describe, it } from 'vitest' -import { createBlockFromJsonRpcProvider } from '../src/constructors.js' -import { createBlockFromRpc } from '../src/from-rpc.js' -import { blockHeaderFromRpc } from '../src/header-from-rpc.js' +import { + blockHeaderFromRpc, + createBlockFromJsonRpcProvider, + createBlockFromRPC, +} from '../src/index.js' import * as alchemy14151203 from './testdata/alchemy14151203.json' import * as infuraGoerliBlock10536893 from './testdata/infura-goerli-block-10536893.json' @@ -27,7 +29,7 @@ describe('[fromRPC]: block #2924874', () => { const common = new Common({ chain: Mainnet, hardfork: Hardfork.Istanbul }) it('should create a block with transactions with valid signatures', () => { - const block = createBlockFromRpc(blockData as JsonRpcBlock, [], { common }) + const block = createBlockFromRPC(blockData, [], { common }) const allValid = block.transactions.every((tx) => tx.verifySignature()) assert.equal(allValid, true, 'all transaction signatures are valid') }) @@ -45,7 +47,7 @@ describe('[fromRPC]:', () => { const valueAsIntegerString = '1' const blockDataTransactionValueAsInteger = blockData blockDataTransactionValueAsInteger.transactions[0].value = valueAsIntegerString - const createBlockFromTransactionValueAsInteger = createBlockFromRpc( + const createBlockFromTransactionValueAsInteger = createBlockFromRPC( blockDataTransactionValueAsInteger as JsonRpcBlock, undefined, { common }, @@ -61,7 +63,7 @@ describe('[fromRPC]:', () => { const gasPriceAsIntegerString = '1' const blockDataTransactionGasPriceAsInteger = blockData blockDataTransactionGasPriceAsInteger.transactions[0].gasPrice = gasPriceAsIntegerString - const createBlockFromTransactionGasPriceAsInteger = createBlockFromRpc( + const createBlockFromTransactionGasPriceAsInteger = createBlockFromRPC( blockDataTransactionGasPriceAsInteger as JsonRpcBlock, undefined, { common }, @@ -76,7 +78,7 @@ describe('[fromRPC]:', () => { it('should create a block given json data that includes a difficulty parameter of type integer string', () => { const common = new Common({ chain: Mainnet, hardfork: Hardfork.London }) - const blockDifficultyAsInteger = createBlockFromRpc( + const blockDifficultyAsInteger = createBlockFromRPC( blockDataDifficultyAsInteger as JsonRpcBlock, undefined, { @@ -91,7 +93,7 @@ describe('[fromRPC]:', () => { it('should create a block from london hardfork', () => { const common = new Common({ chain: Goerli, hardfork: Hardfork.London }) - const block = createBlockFromRpc(testDataFromRpcGoerliLondon as JsonRpcBlock, [], { common }) + const block = createBlockFromRPC(testDataFromRpcGoerliLondon as JsonRpcBlock, [], { common }) assert.equal( `0x${block.header.baseFeePerGas?.toString(16)}`, testDataFromRpcGoerliLondon.baseFeePerGas, @@ -101,7 +103,7 @@ describe('[fromRPC]:', () => { it('should create a block with uncles', () => { const common = new Common({ chain: Mainnet, hardfork: Hardfork.Istanbul }) - const block = createBlockFromRpc(blockDataWithUncles as JsonRpcBlock, [uncleBlockData], { + const block = createBlockFromRPC(blockDataWithUncles as JsonRpcBlock, [uncleBlockData], { common, }) assert.ok(block.uncleHashIsValid()) @@ -109,7 +111,7 @@ describe('[fromRPC]:', () => { it('should create a block with EIP-4896 withdrawals', () => { const common = new Common({ chain: Mainnet, hardfork: Hardfork.Shanghai }) - const block = createBlockFromRpc(blockDataWithWithdrawals as JsonRpcBlock, [], { common }) + const block = createBlockFromRPC(blockDataWithWithdrawals as JsonRpcBlock, [], { common }) assert.ok(block.withdrawalsTrieIsValid()) }) @@ -124,13 +126,13 @@ describe('[fromRPC]:', () => { describe('[fromRPC] - Alchemy/Infura API block responses', () => { it('should create pre merge block from Alchemy API response to eth_getBlockByHash', () => { const common = new Common({ chain: Mainnet, hardfork: Hardfork.London }) - const block = createBlockFromRpc(alchemy14151203 as JsonRpcBlock, [], { common }) + const block = createBlockFromRPC(alchemy14151203 as JsonRpcBlock, [], { common }) assert.equal(bytesToHex(block.hash()), alchemy14151203.hash) }) it('should create pre and post merge blocks from Infura API responses to eth_getBlockByHash and eth_getBlockByNumber', () => { const common = new Common({ chain: Mainnet }) - let block = createBlockFromRpc(infura2000004woTxs as JsonRpcBlock, [], { + let block = createBlockFromRPC(infura2000004woTxs as JsonRpcBlock, [], { common, setHardfork: true, }) @@ -139,13 +141,13 @@ describe('[fromRPC] - Alchemy/Infura API block responses', () => { infura2000004woTxs.hash, 'created premerge block w/o txns', ) - block = createBlockFromRpc(infura2000004wTxs as JsonRpcBlock, [], { common, setHardfork: true }) + block = createBlockFromRPC(infura2000004wTxs as JsonRpcBlock, [], { common, setHardfork: true }) assert.equal( bytesToHex(block.hash()), infura2000004wTxs.hash, 'created premerge block with txns', ) - block = createBlockFromRpc(infura15571241woTxs as JsonRpcBlock, [], { + block = createBlockFromRPC(infura15571241woTxs as JsonRpcBlock, [], { common, setHardfork: true, }) @@ -155,7 +157,7 @@ describe('[fromRPC] - Alchemy/Infura API block responses', () => { 'created post merge block without txns', ) - block = createBlockFromRpc(infura15571241wTxs as JsonRpcBlock, [], { + block = createBlockFromRPC(infura15571241wTxs as JsonRpcBlock, [], { common, setHardfork: true, }) diff --git a/packages/block/test/header.spec.ts b/packages/block/test/header.spec.ts index ec658db5a0..a56e3c41f7 100644 --- a/packages/block/test/header.spec.ts +++ b/packages/block/test/header.spec.ts @@ -13,19 +13,19 @@ import { import { assert, describe, it } from 'vitest' import { + Block, createBlock, createBlockFromRLPSerializedBlock, createBlockHeader, + createBlockHeaderFromBytesArray, createBlockHeaderFromRLP, - createBlockHeaderFromValuesArray, -} from '../src/constructors.js' -import { Block } from '../src/index.js' +} from '../src/index.js' import * as testData from './testdata/bcBlockGasLimitTest.json' import * as blocksGoerli from './testdata/blocks_goerli.json' import * as blocksMainnet from './testdata/blocks_mainnet.json' -import type { BlockHeader } from '../src/header.js' +import type { BlockHeader } from '../src/index.js' import type { CliqueConfig } from '@ethereumjs/common' import type { PrefixedHexString } from '@ethereumjs/util' @@ -146,10 +146,10 @@ describe('[Block]: Header functions', () => { headerArray[13] = zeros(32) // mixHash headerArray[14] = zeros(8) // nonce - let header = createBlockHeaderFromValuesArray(headerArray, { common }) + let header = createBlockHeaderFromBytesArray(headerArray, { common }) assert.ok(Object.isFrozen(header), 'block should be frozen by default') - header = createBlockHeaderFromValuesArray(headerArray, { common, freeze: false }) + header = createBlockHeaderFromBytesArray(headerArray, { common, freeze: false }) assert.ok( !Object.isFrozen(header), 'block should not be frozen when freeze deactivated in options', @@ -169,14 +169,14 @@ describe('[Block]: Header functions', () => { headerArray[14] = zeros(8) // nonce headerArray[15] = zeros(4) // bad data try { - createBlockHeaderFromValuesArray(headerArray) + createBlockHeaderFromBytesArray(headerArray) } catch (e: any) { const expectedError = 'invalid header. More values than expected were received' assert.ok(e.message.includes(expectedError), 'should throw on more values than expected') } try { - createBlockHeaderFromValuesArray(headerArray.slice(0, 5)) + createBlockHeaderFromBytesArray(headerArray.slice(0, 5)) } catch (e: any) { const expectedError = 'invalid header. Less values than expected were received' assert.ok(e.message.includes(expectedError), 'should throw on less values than expected') diff --git a/packages/block/test/mergeBlock.spec.ts b/packages/block/test/mergeBlock.spec.ts index 638a0d2995..ab323cf499 100644 --- a/packages/block/test/mergeBlock.spec.ts +++ b/packages/block/test/mergeBlock.spec.ts @@ -9,10 +9,9 @@ import { } from '@ethereumjs/util' import { assert, describe, it } from 'vitest' -import { Block } from '../src/block.js' -import { createBlock, createBlockHeader } from '../src/constructors.js' +import { Block, createBlock, createBlockHeader } from '../src/index.js' -import type { BlockHeader } from '../src/header.js' +import type { BlockHeader } from '../src/index.js' const common = new Common({ chain: Mainnet, diff --git a/packages/blockchain/src/db/manager.ts b/packages/blockchain/src/db/manager.ts index c0864c1cd0..bca9cee717 100644 --- a/packages/blockchain/src/db/manager.ts +++ b/packages/blockchain/src/db/manager.ts @@ -1,4 +1,4 @@ -import { createBlockFromValuesArray, createBlockHeaderFromValuesArray } from '@ethereumjs/block' +import { createBlockFromBytesArray, createBlockHeaderFromBytesArray } from '@ethereumjs/block' import { RLP } from '@ethereumjs/rlp' import { KECCAK256_RLP, @@ -141,7 +141,7 @@ export class DBManager { const blockData = [header.raw(), ...body] as BlockBytes const opts: BlockOptions = { common: this.common, setHardfork: true } - return createBlockFromValuesArray(blockData, opts) + return createBlockFromBytesArray(blockData, opts) } /** @@ -160,7 +160,7 @@ export class DBManager { const headerValues = RLP.decode(encodedHeader) const opts: BlockOptions = { common: this.common, setHardfork: true } - return createBlockHeaderFromValuesArray(headerValues as Uint8Array[], opts) + return createBlockHeaderFromBytesArray(headerValues as Uint8Array[], opts) } /** diff --git a/packages/blockchain/test/index.spec.ts b/packages/blockchain/test/index.spec.ts index f195591334..508358cd5a 100644 --- a/packages/blockchain/test/index.spec.ts +++ b/packages/blockchain/test/index.spec.ts @@ -2,7 +2,7 @@ import { createBlock, createBlockFromRLPSerializedBlock, createBlockHeader, - createBlockHeaderFromValuesArray, + createBlockHeaderFromBytesArray, } from '@ethereumjs/block' import { Common, Goerli, Hardfork, Holesky, Mainnet, Sepolia } from '@ethereumjs/common' import { MapDB, bytesToHex, equalsBytes, hexToBytes, utf8ToBytes } from '@ethereumjs/util' @@ -531,7 +531,7 @@ describe('blockchain test', () => { const block2HeaderValuesArray = blocks[2].header.raw() block2HeaderValuesArray[1] = new Uint8Array(32) - const block2Header = createBlockHeaderFromValuesArray(block2HeaderValuesArray, { + const block2Header = createBlockHeaderFromBytesArray(block2HeaderValuesArray, { common: blocks[2].common, }) await blockchain.putHeader(block2Header) diff --git a/packages/client/bin/cli.ts b/packages/client/bin/cli.ts index 1bd8c5e2e0..5d7a6a4885 100755 --- a/packages/client/bin/cli.ts +++ b/packages/client/bin/cli.ts @@ -1,6 +1,6 @@ #!/usr/bin/env node -import { createBlockFromValuesArray } from '@ethereumjs/block' +import { createBlockFromBytesArray } from '@ethereumjs/block' import { CliqueConsensus, createBlockchain } from '@ethereumjs/blockchain' import { Chain, @@ -685,7 +685,7 @@ async function startClient( let buf = RLP.decode(blockRlp, true) while (buf.data?.length > 0 || buf.remainder?.length > 0) { try { - const block = createBlockFromValuesArray(buf.data as BlockBytes, { + const block = createBlockFromBytesArray(buf.data as BlockBytes, { common: config.chainCommon, setHardfork: true, }) diff --git a/packages/client/src/blockchain/chain.ts b/packages/client/src/blockchain/chain.ts index b3280f44b9..af99f4aa63 100644 --- a/packages/client/src/blockchain/chain.ts +++ b/packages/client/src/blockchain/chain.ts @@ -1,4 +1,4 @@ -import { createBlockFromValuesArray, createBlockHeaderFromValuesArray } from '@ethereumjs/block' +import { createBlockFromBytesArray, createBlockHeaderFromBytesArray } from '@ethereumjs/block' import { CliqueConsensus, createBlockchain } from '@ethereumjs/blockchain' import { ConsensusAlgorithm, Hardfork } from '@ethereumjs/common' import { BIGINT_0, equalsBytes } from '@ethereumjs/util' @@ -418,7 +418,7 @@ export class Chain { await this.blockchain.consensus?.setup({ blockchain: this.blockchain }) } - const block = createBlockFromValuesArray(b.raw(), { + const block = createBlockFromBytesArray(b.raw(), { common: this.config.chainCommon, setHardfork: true, }) @@ -468,7 +468,7 @@ export class Chain { } break } - const header = createBlockHeaderFromValuesArray(h.raw(), { + const header = createBlockHeaderFromBytesArray(h.raw(), { common: this.config.chainCommon, setHardfork: true, }) diff --git a/packages/client/src/net/protocol/ethprotocol.ts b/packages/client/src/net/protocol/ethprotocol.ts index 1dd3539313..f5b3ad703c 100644 --- a/packages/client/src/net/protocol/ethprotocol.ts +++ b/packages/client/src/net/protocol/ethprotocol.ts @@ -1,4 +1,4 @@ -import { createBlockFromValuesArray, createBlockHeaderFromValuesArray } from '@ethereumjs/block' +import { createBlockFromBytesArray, createBlockHeaderFromBytesArray } from '@ethereumjs/block' import { RLP } from '@ethereumjs/rlp' import { BlobEIP4844Transaction, @@ -168,7 +168,7 @@ export class EthProtocol extends Protocol { bytesToBigInt(reqId), headers.map((h) => { const common = this.config.chainCommon - const header = createBlockHeaderFromValuesArray(h, { common, setHardfork: true }) + const header = createBlockHeaderFromBytesArray(h, { common, setHardfork: true }) return header }), ], @@ -200,7 +200,7 @@ export class EthProtocol extends Protocol { code: 0x07, encode: ([block, td]: [Block, bigint]) => [block.raw(), bigIntToUnpaddedBytes(td)], decode: ([block, td]: [BlockBytes, Uint8Array]) => [ - createBlockFromValuesArray(block, { + createBlockFromBytesArray(block, { common: this.config.chainCommon, setHardfork: true, }), diff --git a/packages/client/src/net/protocol/lesprotocol.ts b/packages/client/src/net/protocol/lesprotocol.ts index b906eaf48c..7235951898 100644 --- a/packages/client/src/net/protocol/lesprotocol.ts +++ b/packages/client/src/net/protocol/lesprotocol.ts @@ -1,4 +1,4 @@ -import { createBlockHeaderFromValuesArray } from '@ethereumjs/block' +import { createBlockHeaderFromBytesArray } from '@ethereumjs/block' import { BIGINT_0, bigIntToUnpaddedBytes, @@ -109,7 +109,7 @@ export class LesProtocol extends Protocol { reqId: bytesToBigInt(reqId), bv: bytesToBigInt(bv), headers: headers.map((h: BlockHeaderBytes) => - createBlockHeaderFromValuesArray(h, { + createBlockHeaderFromBytesArray(h, { setHardfork: true, common: this.config.chainCommon, // eslint-disable-line no-invalid-this }), diff --git a/packages/client/src/sync/fetcher/blockfetcher.ts b/packages/client/src/sync/fetcher/blockfetcher.ts index 732cc36ba7..30b302027b 100644 --- a/packages/client/src/sync/fetcher/blockfetcher.ts +++ b/packages/client/src/sync/fetcher/blockfetcher.ts @@ -1,4 +1,4 @@ -import { createBlockFromValuesArray } from '@ethereumjs/block' +import { createBlockFromBytesArray } from '@ethereumjs/block' import { KECCAK256_RLP, KECCAK256_RLP_ARRAY, equalsBytes } from '@ethereumjs/util' import { Event } from '../../types.js' @@ -91,7 +91,7 @@ export class BlockFetcher extends BlockFetcherBase { values.push(withdrawalsData) } // Supply the common from the corresponding block header already set on correct fork - const block = createBlockFromValuesArray(values, { common: headers[i].common }) + const block = createBlockFromBytesArray(values, { common: headers[i].common }) // Only validate the data integrity // Upon putting blocks into blockchain (for BlockFetcher), `validateData` is called again // In ReverseBlockFetcher we do not need to validate the entire block, since CL diff --git a/packages/common/test/utils.spec.ts b/packages/common/test/utils.spec.ts index 01bdc1704f..7dff0e0a39 100644 --- a/packages/common/test/utils.spec.ts +++ b/packages/common/test/utils.spec.ts @@ -1,8 +1,8 @@ import { assert, describe, it } from 'vitest' import { Mainnet } from '../src/chains.js' -import { createCommonFromGethGenesis } from '../src/constructors.js' import { Hardfork } from '../src/enums.js' +import { createCommonFromGethGenesis } from '../src/index.js' import { parseGethGenesis } from '../src/utils.js' import * as invalidSpuriousDragonJSON from './data/geth-genesis/invalid-spurious-dragon.json' diff --git a/packages/devp2p/examples/peer-communication-les.ts b/packages/devp2p/examples/peer-communication-les.ts index 1b40d26149..8aeb9be843 100644 --- a/packages/devp2p/examples/peer-communication-les.ts +++ b/packages/devp2p/examples/peer-communication-les.ts @@ -1,4 +1,4 @@ -import { createBlockFromValuesArray, createBlockHeaderFromValuesArray } from '@ethereumjs/block' +import { createBlockFromBytesArray, createBlockHeaderFromBytesArray } from '@ethereumjs/block' import { Common, Hardfork, Mainnet } from '@ethereumjs/common' import * as devp2p from '@ethereumjs/devp2p' import { bytesToHex, bytesToInt, hexToBytes, intToBytes, randomBytes } from '@ethereumjs/util' @@ -109,7 +109,7 @@ rlpx.events.on('peer:added', (peer) => { ) break } - const header = createBlockHeaderFromValuesArray(payload[2][0], { common }) + const header = createBlockHeaderFromBytesArray(payload[2][0], { common }) setTimeout(() => { les.sendMessage(devp2p.LES.MESSAGE_CODES.GET_BLOCK_BODIES, [ @@ -132,7 +132,7 @@ rlpx.events.on('peer:added', (peer) => { const header2 = requests.bodies.shift() const txs = payload[2][0][0] const uncleHeaders = payload[2][0][1] - const block = createBlockFromValuesArray([header2.raw(), txs, uncleHeaders], { common }) + const block = createBlockFromBytesArray([header2.raw(), txs, uncleHeaders], { common }) const isValid = await isValidBlock(block) let isValidPayload = false if (isValid) { diff --git a/packages/devp2p/examples/peer-communication.ts b/packages/devp2p/examples/peer-communication.ts index a3600b15c4..e98808331c 100644 --- a/packages/devp2p/examples/peer-communication.ts +++ b/packages/devp2p/examples/peer-communication.ts @@ -1,4 +1,4 @@ -import { BlockHeader, createBlockFromValuesArray } from '@ethereumjs/block' +import { BlockHeader, createBlockFromBytesArray } from '@ethereumjs/block' import { Common, Hardfork, Mainnet } from '@ethereumjs/common' import * as devp2p from '@ethereumjs/devp2p' import { RLP } from '@ethereumjs/rlp' @@ -174,7 +174,7 @@ rlpx.events.on('peer:added', (peer) => { } const expectedHash = CHECK_BLOCK - const header = createBlockHeaderFromValuesArray(payload[1][0], { common }) + const header = createBlockHeaderFromBytesArray(payload[1][0], { common }) if (bytesToUnprefixedHex(header.hash()) === expectedHash) { console.log(`${addr} verified to be on the same side of the ${CHECK_BLOCK_TITLE}`) clearTimeout(forkDrop) @@ -189,7 +189,7 @@ rlpx.events.on('peer:added', (peer) => { } let isValidPayload = false - const header = createBlockHeaderFromValuesArray(payload[1][0], { common }) + const header = createBlockHeaderFromBytesArray(payload[1][0], { common }) while (requests.headers.length > 0) { const blockHash = requests.headers.shift() if (equalsBytes(header.hash(), blockHash)) { @@ -238,7 +238,7 @@ rlpx.events.on('peer:added', (peer) => { const header = requests.bodies.shift() const txs = payload[1][0][0] const uncleHeaders = payload[1][0][1] - const block = createBlockFromValuesArray([header.raw(), txs, uncleHeaders], { common }) + const block = createBlockFromBytesArray([header.raw(), txs, uncleHeaders], { common }) const isValid = await isValidBlock(block) if (isValid) { isValidPayload = true @@ -257,7 +257,7 @@ rlpx.events.on('peer:added', (peer) => { case devp2p.ETH.MESSAGE_CODES.NEW_BLOCK: { if (!forkVerified) break - const newBlock = createBlockFromValuesArray(payload[0], { common }) + const newBlock = createBlockFromBytesArray(payload[0], { common }) const isValidNewBlock = await isValidBlock(newBlock) if (isValidNewBlock) onNewBlock(newBlock, peer) diff --git a/packages/ethash/test/block.spec.ts b/packages/ethash/test/block.spec.ts index 006401a4b0..a39807ed27 100644 --- a/packages/ethash/test/block.spec.ts +++ b/packages/ethash/test/block.spec.ts @@ -1,7 +1,7 @@ import { createBlock, + createBlockFromBytesArray, createBlockFromRLPSerializedBlock, - createBlockFromValuesArray, } from '@ethereumjs/block' import { Common, Hardfork, Mainnet } from '@ethereumjs/common' import { RLP } from '@ethereumjs/rlp' @@ -35,7 +35,7 @@ describe('Verify POW for valid and invalid blocks', () => { // Put correct amount of extraData in block extraData field so block can be deserialized const values = RLP.decode(Uint8Array.from(invalidRlp)) as BlockBytes values[0][12] = new Uint8Array(32) - const invalidBlock = createBlockFromValuesArray(values, { common }) + const invalidBlock = createBlockFromBytesArray(values, { common }) const invalidBlockResult = await e.verifyPOW(invalidBlock) assert.ok(!invalidBlockResult, 'should be invalid') diff --git a/packages/tx/src/1559/tx.ts b/packages/tx/src/1559/tx.ts index 974cb5a837..370fd9efd2 100644 --- a/packages/tx/src/1559/tx.ts +++ b/packages/tx/src/1559/tx.ts @@ -141,7 +141,7 @@ export class FeeMarketEIP1559Transaction extends BaseTransaction * Returns a Uint8Array Array of the raw Bytes of this transaction, in order. * * Use {@link BaseTransaction.serialize} to add a transaction to a block - * with {@link createBlockFromValuesArray}. + * with {@link createBlockFromBytesArray}. * * For an unsigned tx this method uses the empty Bytes values for the * signature parameters `v`, `r` and `s` for encoding. For an EIP-155 compliant diff --git a/packages/tx/src/legacy/tx.ts b/packages/tx/src/legacy/tx.ts index 9496f51c6d..b814fe764b 100644 --- a/packages/tx/src/legacy/tx.ts +++ b/packages/tx/src/legacy/tx.ts @@ -107,7 +107,7 @@ export class LegacyTransaction extends BaseTransaction { * Format: `[nonce, gasPrice, gasLimit, to, value, data, v, r, s]` * * For legacy txs this is also the correct format to add transactions - * to a block with {@link createBlockFromValuesArray} (use the `serialize()` method + * to a block with {@link createBlockFromBytesArray} (use the `serialize()` method * for typed txs). * * For an unsigned tx this method returns the empty Bytes values diff --git a/packages/vm/test/api/runBlock.spec.ts b/packages/vm/test/api/runBlock.spec.ts index e5a52109a9..e9a2113fc5 100644 --- a/packages/vm/test/api/runBlock.spec.ts +++ b/packages/vm/test/api/runBlock.spec.ts @@ -1,7 +1,7 @@ import { createBlock, + createBlockFromBytesArray, createBlockFromRLPSerializedBlock, - createBlockFromValuesArray, createSealedCliqueBlock, } from '@ethereumjs/block' import { Common, Goerli, Hardfork, Mainnet, createCustomCommon } from '@ethereumjs/common' @@ -317,7 +317,7 @@ describe('runBlock() -> runtime behavior', async () => { ) as NestedUint8Array // edit extra data of this block to "dao-hard-fork" block1[0][12] = utf8ToBytes('dao-hard-fork') - const block = createBlockFromValuesArray(block1 as BlockBytes, { common }) + const block = createBlockFromBytesArray(block1 as BlockBytes, { common }) await setupPreConditions(vm.stateManager, testData) // fill two original DAO child-contracts with funds and the recovery account with funds in order to verify that the balance gets summed correctly