Skip to content
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

Block: rename constructor createBlock #3549

Merged
merged 6 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/block/examples/1559.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createBlockFromBlockData } from '@ethereumjs/block'
import { createBlock } from '@ethereumjs/block'
import { Chain, Common, Hardfork } from '@ethereumjs/common'
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })

const block = createBlockFromBlockData(
const block = createBlock(
{
header: {
baseFeePerGas: BigInt(10),
Expand All @@ -19,7 +19,7 @@ console.log(Number(block.header.calcNextBaseFee())) // 11

// So for creating a block with a matching base fee in a certain
// chain context you can do:
const blockWithMatchingBaseFee = createBlockFromBlockData(
const blockWithMatchingBaseFee = createBlock(
{
header: {
baseFeePerGas: block.header.calcNextBaseFee(),
Expand Down
4 changes: 2 additions & 2 deletions packages/block/examples/4844.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createBlockFromBlockData } from '@ethereumjs/block'
import { createBlock } from '@ethereumjs/block'
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { create4844BlobTx } from '@ethereumjs/tx'
import { createAddressFromPrivateKey } from '@ethereumjs/util'
Expand All @@ -20,7 +20,7 @@ const main = async () => {
{ common },
)

const block = createBlockFromBlockData(
const block = createBlock(
{
header: {
excessBlobGas: 0n,
Expand Down
4 changes: 2 additions & 2 deletions packages/block/examples/clique.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createBlockFromBlockData } from '@ethereumjs/block'
import { createBlock } from '@ethereumjs/block'
import { Chain, Common, Hardfork } from '@ethereumjs/common'

const common = new Common({ chain: Chain.Goerli, hardfork: Hardfork.Chainstart })

console.log(common.consensusType()) // 'poa'
console.log(common.consensusAlgorithm()) // 'clique'

createBlockFromBlockData({ header: { extraData: new Uint8Array(97) } }, { common })
createBlock({ header: { extraData: new Uint8Array(97) } }, { common })
console.log(`Old Clique Proof-of-Authority block created`)
4 changes: 2 additions & 2 deletions packages/block/examples/pos.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createBlockFromBlockData } from '@ethereumjs/block'
import { createBlock } from '@ethereumjs/block'
import { Chain, Common } from '@ethereumjs/common'

const common = new Common({ chain: Chain.Mainnet })

const block = createBlockFromBlockData(
const block = createBlock(
{
// Provide your block data here or use default values
},
Expand Down
4 changes: 2 additions & 2 deletions packages/block/examples/pow.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createBlockFromBlockData } from '@ethereumjs/block'
import { createBlock } from '@ethereumjs/block'
import { Chain, Common, Hardfork } from '@ethereumjs/common'

const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })

console.log(common.consensusType()) // 'pow'
console.log(common.consensusAlgorithm()) // 'ethash'

createBlockFromBlockData({}, { common })
createBlock({}, { common })
console.log(`Old Proof-of-Work block created`)
4 changes: 2 additions & 2 deletions packages/block/examples/withdrawals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createBlockFromBlockData } from '@ethereumjs/block'
import { createBlock } from '@ethereumjs/block'
import { Chain, Common } from '@ethereumjs/common'
import { Address, hexToBytes } from '@ethereumjs/util'

Expand All @@ -13,7 +13,7 @@ const withdrawal = <WithdrawalData>{
amount: BigInt(1000),
}

const block = createBlockFromBlockData(
const block = createBlock(
{
header: {
withdrawalsRoot: hexToBytes(
Expand Down
4 changes: 2 additions & 2 deletions packages/block/src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { genRequestsTrieRoot, genTransactionsTrieRoot, genWithdrawalsTrieRoot }
import {
BlockHeader,
type createBlockFromBeaconPayloadJson,
type createBlockFromBlockData,
type createBlock,
type createBlockFromExecutionPayload,
type createBlockFromJsonRpcProvider,
type createBlockFromRLPSerializedBlock,
Expand Down Expand Up @@ -55,7 +55,7 @@ import type {
* A block object can be created with one of the following constructor methods
* (separate from the Block class to allow for tree shaking):
*
* - {@link createBlockFromBlockData }
* - {@link createBlock }
* - {@link createBlockFromValuesArray }
* - {@link createBlockFromRLPSerializedBlock }
* - {@link createBlockFromRPC }
Expand Down
4 changes: 2 additions & 2 deletions packages/block/src/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function createHeaderFromRLP(serializedHeaderData: Uint8Array, opts: Bloc
* @param blockData
* @param opts
*/
export function createBlockFromBlockData(blockData: BlockData = {}, opts?: BlockOptions) {
export function createBlock(blockData: BlockData = {}, opts?: BlockOptions) {
const {
header: headerData,
transactions: txsData,
Expand Down Expand Up @@ -475,7 +475,7 @@ export async function createBlockFromExecutionPayload(
}

// we are not setting setHardfork as common is already set to the correct hf
const block = createBlockFromBlockData(
const block = createBlock(
{ header, transactions: txs, withdrawals, executionWitness, requests },
opts,
)
Expand Down
4 changes: 2 additions & 2 deletions packages/block/src/from-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
toType,
} from '@ethereumjs/util'

import { createBlockFromBlockData } from './constructors.js'
import { createBlock } from './constructors.js'
import { blockHeaderFromRpc } from './header-from-rpc.js'

import type { BlockOptions, JsonRpcBlock } from './index.js'
Expand Down Expand Up @@ -64,7 +64,7 @@ export function createBlockFromRpc(
const bytes = hexToBytes(req as PrefixedHexString)
return CLRequestFactory.fromSerializedRequest(bytes)
})
return createBlockFromBlockData(
return createBlock(
{ header, transactions, uncleHeaders, withdrawals: blockParams.withdrawals, requests },
options,
)
Expand Down
57 changes: 27 additions & 30 deletions packages/block/test/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { assert, describe, it } from 'vitest'

import {
createBlockFromBlockData,
createBlock,
createBlockFromRLPSerializedBlock,
createBlockFromValuesArray,
} from '../src/constructors.js'
Expand All @@ -32,12 +32,12 @@ import type { NestedUint8Array, PrefixedHexString } from '@ethereumjs/util'
describe('[Block]: block functions', () => {
it('should test block initialization', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
const genesis = createBlockFromBlockData({}, { common })
const genesis = createBlock({}, { common })
assert.ok(bytesToHex(genesis.hash()), 'block should initialize')

const params = JSON.parse(JSON.stringify(paramsBlock))
params['1']['minGasLimit'] = 3000 // 5000
let block = createBlockFromBlockData({}, { params })
let block = createBlock({}, { params })
assert.equal(
block.common.param('minGasLimit'),
BigInt(3000),
Expand All @@ -46,10 +46,10 @@ describe('[Block]: block functions', () => {

// test default freeze values
// also test if the options are carried over to the constructor
block = createBlockFromBlockData({})
block = createBlock({})
assert.ok(Object.isFrozen(block), 'block should be frozen by default')

block = createBlockFromBlockData({}, { freeze: false })
block = createBlock({}, { freeze: false })
assert.ok(
!Object.isFrozen(block),
'block should not be frozen when freeze deactivated in options',
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('[Block]: block functions', () => {
customChains: customChains as ChainConfig[],
})

let block = createBlockFromBlockData(
let block = createBlock(
{
header: {
number: 12, // Berlin block
Expand All @@ -111,7 +111,7 @@ describe('[Block]: block functions', () => {
)
assert.equal(block.common.hardfork(), Hardfork.Berlin, 'should use setHardfork option')

block = createBlockFromBlockData(
block = createBlock(
{
header: {
number: 20, // Future block
Expand All @@ -125,7 +125,7 @@ describe('[Block]: block functions', () => {
'should use setHardfork option (td > threshold)',
)

block = createBlockFromBlockData(
block = createBlock(
{
header: {
number: 12, // Berlin block,
Expand All @@ -143,26 +143,23 @@ describe('[Block]: block functions', () => {

it('should initialize with undefined parameters without throwing', () => {
assert.doesNotThrow(function () {
createBlockFromBlockData()
createBlock()
})
})

it('should initialize with null parameters without throwing', () => {
const common = new Common({ chain: Chain.Goerli })
const opts = { common }
assert.doesNotThrow(function () {
createBlockFromBlockData({}, opts)
createBlock({}, opts)
})
})

it('should throw when trying to initialize with uncle headers on a PoA network', () => {
const common = new Common({ chain: Chain.Mainnet })
const uncleBlock = createBlockFromBlockData(
{ header: { extraData: new Uint8Array(117) } },
{ common },
)
const uncleBlock = createBlock({ header: { extraData: new Uint8Array(117) } }, { common })
assert.throws(function () {
createBlockFromBlockData({ uncleHeaders: [uncleBlock.header] }, { common })
createBlock({ uncleHeaders: [uncleBlock.header] }, { common })
})
})

Expand Down Expand Up @@ -212,9 +209,9 @@ describe('[Block]: block functions', () => {
gasLimit: 53000,
gasPrice: 7,
})
const blockTest = createBlockFromBlockData({ transactions: [tx] })
const blockTest = createBlock({ transactions: [tx] })
const txTrie = await blockTest.genTxTrie()
const block = createBlockFromBlockData({
const block = createBlock({
header: {
transactionsTrie: txTrie,
},
Expand All @@ -229,7 +226,7 @@ describe('[Block]: block functions', () => {
})

it('should test transaction validation with empty transaction list', async () => {
const block = createBlockFromBlockData({})
const block = createBlock({})
await testTransactionValidation(block)
})

Expand Down Expand Up @@ -264,7 +261,7 @@ describe('[Block]: block functions', () => {
const unsignedTx = createLegacyTx({})
const txRoot = await genTransactionsTrieRoot([unsignedTx])

let block = createBlockFromBlockData({
let block = createBlock({
transactions: [unsignedTx],
header: {
transactionsTrie: txRoot,
Expand All @@ -286,7 +283,7 @@ describe('[Block]: block functions', () => {
const zeroRoot = zeros(32)

// Tx root
block = createBlockFromBlockData({
block = createBlock({
transactions: [unsignedTx],
header: {
transactionsTrie: zeroRoot,
Expand All @@ -295,7 +292,7 @@ describe('[Block]: block functions', () => {
await checkThrowsAsync(block.validateData(false, false), 'invalid transaction trie')

// Withdrawals root
block = createBlockFromBlockData(
block = createBlock(
{
header: {
withdrawalsRoot: zeroRoot,
Expand All @@ -307,7 +304,7 @@ describe('[Block]: block functions', () => {
await checkThrowsAsync(block.validateData(false, false), 'invalid withdrawals trie')

// Uncle root
block = createBlockFromBlockData(
block = createBlock(
{
header: {
uncleHash: zeroRoot,
Expand All @@ -321,17 +318,17 @@ describe('[Block]: block functions', () => {
const common = new Common({ chain: Chain.Mainnet, eips: [6800], hardfork: Hardfork.Cancun })
// Note: `executionWitness: undefined` will still initialize an execution witness in the block
// So, only testing for `null` here
block = createBlockFromBlockData({ executionWitness: null }, { common })
block = createBlock({ executionWitness: null }, { common })
await checkThrowsAsync(
block.validateData(false, false),
'Invalid block: ethereumjs stateless client needs executionWitness',
)
})

it('should test isGenesis (mainnet default)', () => {
const block = createBlockFromBlockData({ header: { number: 1 } })
const block = createBlock({ header: { number: 1 } })
assert.notEqual(block.isGenesis(), true)
const genesisBlock = createBlockFromBlockData({ header: { number: 0 } })
const genesisBlock = createBlock({ header: { number: 0 } })
assert.equal(genesisBlock.isGenesis(), true)
})

Expand Down Expand Up @@ -432,15 +429,15 @@ describe('[Block]: block functions', () => {

it('should set canonical difficulty if I provide a calcDifficultyFromHeader header', () => {
let common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
const genesis = createBlockFromBlockData({}, { common })
const genesis = createBlock({}, { common })

const nextBlockHeaderData = {
number: genesis.header.number + BigInt(1),
timestamp: genesis.header.timestamp + BigInt(10),
}

common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
const blockWithoutDifficultyCalculation = createBlockFromBlockData(
const blockWithoutDifficultyCalculation = createBlock(
{
header: nextBlockHeaderData,
},
Expand All @@ -455,7 +452,7 @@ describe('[Block]: block functions', () => {
)

// test if we set difficulty if we have a "difficulty header" in options; also verify this is equal to reported canonical difficulty.
const blockWithDifficultyCalculation = createBlockFromBlockData(
const blockWithDifficultyCalculation = createBlock(
{
header: nextBlockHeaderData,
},
Expand All @@ -481,7 +478,7 @@ describe('[Block]: block functions', () => {
timestamp: genesis.header.timestamp + BigInt(10),
}

const block_farAhead = createBlockFromBlockData(
const block_farAhead = createBlock(
{
header: noParentHeaderData,
},
Expand All @@ -499,7 +496,7 @@ describe('[Block]: block functions', () => {

it('should be able to initialize shanghai blocks with correct hardfork defaults', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Shanghai })
const block = createBlockFromBlockData({}, { common })
const block = createBlock({}, { common })
assert.equal(block.common.hardfork(), Hardfork.Shanghai, 'hardfork should be set to shanghai')
assert.deepEqual(block.withdrawals, [], 'withdrawals should be set to default empty array')
})
Expand Down
Loading
Loading