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

Update block docs / additional constructor renamings #3615

Merged
merged 4 commits into from
Aug 27, 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
12 changes: 6 additions & 6 deletions packages/block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ npm install @ethereumjs/block

### Introduction

There are five static factories to instantiate a `Block`:
There are five standalone functions to instantiate a `Block`:

- `Block.fromBlockData(blockData: BlockData = {}, opts?: BlockOptions)`
- `Block.fromRLPSerializedBlock(serialized: Uint8Array, opts?: BlockOptions)`
- `Block.fromValuesArray(values: BlockBytes, opts?: BlockOptions)`
- `Block.fromRPC(blockData: JsonRpcBlock, uncles?: any[], opts?: BlockOptions)`
- `Block.fromJsonRpcProvider(provider: string | EthersProvider, blockTag: string | bigint, opts: BlockOptions)`
- `createBlock(blockData: BlockData = {}, opts?: BlockOptions)`
- `createBlockFromRLPSerializedBlock(serialized: Uint8Array, opts?: BlockOptions)`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this has been forgotten to rename with the simplified name.

Will directly push here, also the code change.

- `createBlockFromBytesArray(values: BlockBytes, opts?: BlockOptions)`
- `createBlockFromRPC(blockParams: JsonRpcBlock, uncles?: any[], opts?: BlockOptions)`
- `createBlockFromJsonRPCProvider(provider: string | EthersProvider, blockTag: string | bigint, opts: BlockOptions)`

For `BlockHeader` instantiation analog factory methods exists, see API docs linked below.

Expand Down
4 changes: 2 additions & 2 deletions packages/block/src/block/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
type createBlock,
type createBlockFromExecutionPayload,
type createBlockFromJsonRpcProvider,
type createBlockFromRLPSerializedBlock,
type createBlockFromRLP,
type createBlockFromRPC,
type createBlockFromBytesArray,
} from '../index.js'
Expand All @@ -54,7 +54,7 @@ import type {
*
* - {@link createBlock }
* - {@link createBlockFromBytesArray }
* - {@link createBlockFromRLPSerializedBlock }
* - {@link createBlockFromRLP }
* - {@link createBlockFromRPC }
* - {@link createBlockFromJsonRpcProvider }
* - {@link createBlockFromExecutionPayload }
Expand Down
8 changes: 4 additions & 4 deletions packages/block/src/block/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import { generateCliqueBlockExtraData } from '../consensus/clique.js'
import { genRequestsTrieRoot, genTransactionsTrieRoot, genWithdrawalsTrieRoot } from '../helpers.js'
import {
Block,
blockHeaderFromRpc,
createBlockHeader,
createBlockHeaderFromBytesArray,
createBlockHeaderFromRPC,
executionPayloadFromBeaconPayload,
} from '../index.js'

Expand Down Expand Up @@ -260,7 +260,7 @@ export function createBlockFromBytesArray(values: BlockBytes, opts?: BlockOption
* @param serialized
* @param opts
*/
export function createBlockFromRLPSerializedBlock(serialized: Uint8Array, opts?: BlockOptions) {
export function createBlockFromRLP(serialized: Uint8Array, opts?: BlockOptions) {
const values = RLP.decode(Uint8Array.from(serialized)) as BlockBytes

if (!Array.isArray(values)) {
Expand All @@ -282,7 +282,7 @@ export function createBlockFromRPC(
uncles: any[] = [],
options?: BlockOptions,
) {
const header = blockHeaderFromRpc(blockParams, options)
const header = createBlockHeaderFromRPC(blockParams, options)

const transactions: TypedTransaction[] = []
const opts = { common: header.common }
Expand All @@ -292,7 +292,7 @@ export function createBlockFromRPC(
transactions.push(tx)
}

const uncleHeaders = uncles.map((uh) => blockHeaderFromRpc(uh, options))
const uncleHeaders = uncles.map((uh) => createBlockHeaderFromRPC(uh, options))

const requests = blockParams.requests?.map((req) => {
const bytes = hexToBytes(req as PrefixedHexString)
Expand Down
2 changes: 1 addition & 1 deletion packages/block/src/header/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function createSealedCliqueBlockHeader(
* @param blockParams - Ethereum JSON RPC of block (eth_getBlockByNumber)
* @param options - An object describing the blockchain
*/
export function blockHeaderFromRpc(blockParams: JsonRpcBlock, options?: BlockOptions) {
export function createBlockHeaderFromRPC(blockParams: JsonRpcBlock, options?: BlockOptions) {
const {
parentHash,
sha3Uncles,
Expand Down
26 changes: 13 additions & 13 deletions packages/block/test/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
type JsonRpcBlock,
createBlock,
createBlockFromBytesArray,
createBlockFromRLPSerializedBlock,
createBlockFromRLP,
createBlockFromRPC,
createEmptyBlock,
paramsBlock,
Expand Down Expand Up @@ -62,10 +62,10 @@ describe('[Block]: block functions', () => {
)

const rlpBlock = block.serialize()
block = createBlockFromRLPSerializedBlock(rlpBlock)
block = createBlockFromRLP(rlpBlock)
assert.ok(Object.isFrozen(block), 'block should be frozen by default')

block = createBlockFromRLPSerializedBlock(rlpBlock, { freeze: false })
block = createBlockFromRLP(rlpBlock, { freeze: false })
assert.ok(
!Object.isFrozen(block),
'block should not be frozen when freeze deactivated in options',
Expand Down Expand Up @@ -156,7 +156,7 @@ describe('[Block]: block functions', () => {
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Istanbul })
const blockRlp = hexToBytes(testDataPreLondon.default.blocks[0].rlp as PrefixedHexString)
try {
createBlockFromRLPSerializedBlock(blockRlp, { common })
createBlockFromRLP(blockRlp, { common })
assert.ok(true, 'should pass')
} catch (error: any) {
assert.fail('should not throw')
Expand All @@ -182,7 +182,7 @@ describe('[Block]: block functions', () => {
it('should test transaction validation - invalid tx trie', async () => {
const blockRlp = hexToBytes(testDataPreLondon.default.blocks[0].rlp as PrefixedHexString)
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })
const block = createBlockFromRLPSerializedBlock(blockRlp, { common, freeze: false })
const block = createBlockFromRLP(blockRlp, { common, freeze: false })
await testTransactionValidation(block)
;(block.header as any).transactionsTrie = new Uint8Array(32)
try {
Expand Down Expand Up @@ -222,7 +222,7 @@ describe('[Block]: block functions', () => {
it('should test transaction validation with legacy tx in london', async () => {
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })
const blockRlp = hexToBytes(testDataPreLondon.default.blocks[0].rlp as PrefixedHexString)
const block = createBlockFromRLPSerializedBlock(blockRlp, { common, freeze: false })
const block = createBlockFromRLP(blockRlp, { common, freeze: false })
await testTransactionValidation(block)
;(block.transactions[0] as any).gasPrice = BigInt(0)
const result = block.getTransactionsValidationErrors()
Expand All @@ -235,7 +235,7 @@ describe('[Block]: block functions', () => {
it('should test uncles hash validation', async () => {
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Istanbul })
const blockRlp = hexToBytes(testDataPreLondon2.default.blocks[2].rlp as PrefixedHexString)
const block = createBlockFromRLPSerializedBlock(blockRlp, { common, freeze: false })
const block = createBlockFromRLP(blockRlp, { common, freeze: false })
assert.equal(block.uncleHashIsValid(), true)
;(block.header as any).uncleHash = new Uint8Array(32)
try {
Expand Down Expand Up @@ -325,15 +325,15 @@ describe('[Block]: block functions', () => {
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Chainstart })
const rlp = hexToBytes(`0x${testDataGenesis.default.test.genesis_rlp_hex}`)
const hash = hexToBytes(`0x${testDataGenesis.default.test.genesis_hash}`)
const block = createBlockFromRLPSerializedBlock(rlp, { common })
const block = createBlockFromRLP(rlp, { common })
assert.ok(equalsBytes(block.hash(), hash), 'genesis hash match')
})

it('should test hash() method (mainnet default)', () => {
let common = new Common({ chain: Mainnet, hardfork: Hardfork.Chainstart })
const rlp = hexToBytes(`0x${testDataGenesis.default.test.genesis_rlp_hex}`)
const hash = hexToBytes(`0x${testDataGenesis.default.test.genesis_hash}`)
let block = createBlockFromRLPSerializedBlock(rlp, { common })
let block = createBlockFromRLP(rlp, { common })
assert.ok(equalsBytes(block.hash(), hash), 'genesis hash match')

common = new Common({
Expand All @@ -345,14 +345,14 @@ describe('[Block]: block functions', () => {
},
},
})
block = createBlockFromRLPSerializedBlock(rlp, { common })
block = createBlockFromRLP(rlp, { common })
assert.deepEqual(block.hash(), new Uint8Array([1]), 'custom crypto applied on hash() method')
})

it('should error on invalid params', () => {
assert.throws(
() => {
createBlockFromRLPSerializedBlock('1' as any)
createBlockFromRLP('1' as any)
},
undefined,
undefined,
Expand All @@ -370,7 +370,7 @@ describe('[Block]: block functions', () => {

it('should return the same block data from raw()', () => {
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Istanbul })
const block = createBlockFromRLPSerializedBlock(
const block = createBlockFromRLP(
toBytes(testDataPreLondon2.default.blocks[2].rlp as PrefixedHexString),
{
common,
Expand All @@ -382,7 +382,7 @@ describe('[Block]: block functions', () => {

it('should test toJSON', () => {
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Istanbul })
const block = createBlockFromRLPSerializedBlock(
const block = createBlockFromRLP(
toBytes(testDataPreLondon2.default.blocks[2].rlp as PrefixedHexString),
{
common,
Expand Down
4 changes: 2 additions & 2 deletions packages/block/test/eip4895block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { assert, describe, it } from 'vitest'

import { genWithdrawalsTrieRoot } from '../src/helpers.js'
import { createBlock, createBlockFromRLPSerializedBlock, createBlockHeader } from '../src/index.js'
import { createBlock, createBlockFromRLP, createBlockHeader } from '../src/index.js'

import type { WithdrawalBytes, WithdrawalData } from '@ethereumjs/util'

Expand Down Expand Up @@ -224,7 +224,7 @@ describe('EIP4895 tests', () => {
// throw check if withdrawals array is not provided in the rlp
assert.throws(
() => {
createBlockFromRLPSerializedBlock(rlpWithoutWithdrawals, { common })
createBlockFromRLP(rlpWithoutWithdrawals, { common })
},
undefined,
undefined,
Expand Down
8 changes: 4 additions & 4 deletions packages/block/test/from-rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { bytesToHex, equalsBytes, hexToBytes, randomBytes } from '@ethereumjs/ut
import { assert, describe, it } from 'vitest'

import {
blockHeaderFromRpc,
createBlockFromJsonRpcProvider,
createBlockFromRPC,
createBlockHeaderFromRPC,
} from '../src/index.js'

import * as alchemy14151203 from './testdata/alchemy14151203.json'
Expand Down Expand Up @@ -35,7 +35,7 @@ describe('[fromRPC]: block #2924874', () => {
})

it('should create a block header with the correct hash', () => {
const block = blockHeaderFromRpc(blockData as JsonRpcBlock, { common })
const block = createBlockHeaderFromRPC(blockData as JsonRpcBlock, { common })
const hash = hexToBytes(blockData.hash as PrefixedHexString)
assert.ok(equalsBytes(block.hash(), hash))
})
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('[fromRPC]:', () => {

it('should create a block header with the correct hash when EIP-4896 withdrawals are present', () => {
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Shanghai })
const block = blockHeaderFromRpc(blockDataWithWithdrawals as JsonRpcBlock, { common })
const block = createBlockHeaderFromRPC(blockDataWithWithdrawals as JsonRpcBlock, { common })
const hash = blockDataWithWithdrawals.hash
assert.equal(bytesToHex(block.hash()), hash)
})
Expand Down Expand Up @@ -168,7 +168,7 @@ describe('[fromRPC] - Alchemy/Infura API block responses', () => {

it('should correctly parse a cancun block over rpc', () => {
const common = new Common({ chain: Goerli, hardfork: Hardfork.Cancun })
const block = blockHeaderFromRpc(infuraGoerliBlock10536893 as JsonRpcBlock, { common }) // cspell:disable-line
const block = createBlockHeaderFromRPC(infuraGoerliBlock10536893 as JsonRpcBlock, { common }) // cspell:disable-line
const hash = hexToBytes(infuraGoerliBlock10536893.hash as PrefixedHexString)
assert.ok(equalsBytes(block.hash(), hash))
})
Expand Down
10 changes: 5 additions & 5 deletions packages/block/test/header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { assert, describe, it } from 'vitest'
import {
Block,
createBlock,
createBlockFromRLPSerializedBlock,
createBlockFromRLP,
createBlockHeader,
createBlockHeaderFromBytesArray,
createBlockHeaderFromRLP,
Expand Down Expand Up @@ -344,7 +344,7 @@ describe('[Block]: Header functions', () => {
const blockchain = new Mockchain()

const genesisRlp = toBytes(testDataPreLondon.genesisRLP)
const block = createBlockFromRLPSerializedBlock(genesisRlp, { common })
const block = createBlockFromRLP(genesisRlp, { common })
await blockchain.putBlock(block)

headerData.number = 1
Expand Down Expand Up @@ -424,7 +424,7 @@ describe('[Block]: Header functions', () => {
const cliqueSigner = hexToBytes(
'64bf9cc30328b0e42387b3c82c614e6386259136235e20c1357bd11cdee86993'
)
const poaBlock = createBlockFromRLPSerializedBlock(genesisRlp, { common, cliqueSigner })
const poaBlock = createBlockFromRLP(genesisRlp, { common, cliqueSigner })
await poaBlockchain.putBlock(poaBlock)

header = createBlockHeader(headerData, { common, cliqueSigner })
Expand Down Expand Up @@ -456,12 +456,12 @@ describe('[Block]: Header functions', () => {
bcBlockGasLimitTestData[key as keyof typeof bcBlockGasLimitTestData]
.genesisRLP as PrefixedHexString,
)
const parentBlock = createBlockFromRLPSerializedBlock(genesisRlp, { common })
const parentBlock = createBlockFromRLP(genesisRlp, { common })
const blockRlp = hexToBytes(
bcBlockGasLimitTestData[key as keyof typeof bcBlockGasLimitTestData].blocks[0]
.rlp as PrefixedHexString,
)
const block = createBlockFromRLPSerializedBlock(blockRlp, { common })
const block = createBlockFromRLP(blockRlp, { common })
assert.doesNotThrow(() => block.validateGasLimit(parentBlock))
}
})
Expand Down
6 changes: 3 additions & 3 deletions packages/blockchain/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
createBlock,
createBlockFromRLPSerializedBlock,
createBlockFromRLP,
createBlockHeader,
createBlockHeaderFromBytesArray,
} from '@ethereumjs/block'
Expand Down Expand Up @@ -580,15 +580,15 @@ describe('blockchain test', () => {
it('should add block with body', async () => {
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Istanbul })
const genesisRlp = hexToBytes(testDataPreLondon.genesisRLP as PrefixedHexString)
const genesisBlock = createBlockFromRLPSerializedBlock(genesisRlp, { common })
const genesisBlock = createBlockFromRLP(genesisRlp, { common })
const blockchain = await createBlockchain({
validateBlocks: true,
validateConsensus: false,
genesisBlock,
})

const blockRlp = hexToBytes(testDataPreLondon.blocks[0].rlp as PrefixedHexString)
const block = createBlockFromRLPSerializedBlock(blockRlp, { common })
const block = createBlockFromRLP(blockRlp, { common })
await blockchain.putBlock(block)
})

Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/service/skeleton.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createBlockFromRLPSerializedBlock } from '@ethereumjs/block'
import { createBlockFromRLP } from '@ethereumjs/block'
import { RLP } from '@ethereumjs/rlp'
import {
BIGINT_0,
Expand Down Expand Up @@ -1385,7 +1385,7 @@ export class Skeleton extends MetaDBManager {
const common = this.config.chainCommon.copy()
common.setHardfork(hardfork)

const block = createBlockFromRLPSerializedBlock(blockRLP, {
const block = createBlockFromRLP(blockRLP, {
common,
})
return block
Expand Down
4 changes: 1 addition & 3 deletions packages/client/src/util/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ const main = async () => {
const common = new Common({ chain: '${execution.config.execCommon.chainName()}', hardfork: '${
execution.hardfork
}' })
const block = createBlockFromRLPSerializedBlock(hexToBytes('${bytesToHex(
block.serialize(),
)}'), { common })
const block = createBlockFromRLP(hexToBytes('${bytesToHex(block.serialize())}'), { common })

const stateDB = new Level('${execution.config.getDataDirectory(DataDirectory.State)}')
const trie = new Trie({ db: stateDB, useKeyHashing: true })
Expand Down
8 changes: 4 additions & 4 deletions packages/client/test/rpc/engine/kaustinen6.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
BlockHeader,
createBlockFromExecutionPayload,
createBlockFromRLPSerializedBlock,
createBlockFromRLP,
executionPayloadFromBeaconPayload,
} from '@ethereumjs/block'
import { hexToBytes } from '@ethereumjs/util'
Expand Down Expand Up @@ -213,21 +213,21 @@ async function loadGethVectors(vectorsDirPath: string, opts: { common: Common })
},
}
const block0RlpHex = readFileSync(`${vectorsDirPath}/block0.rlp.hex`, 'utf8').trim()
const block0 = createBlockFromRLPSerializedBlock(hexToBytes(`0x${block0RlpHex}`), {
const block0 = createBlockFromRLP(hexToBytes(`0x${block0RlpHex}`), {
...opts,
executionWitness: executionWitness0,
})
const _block0Payload = block0.toExecutionPayload()

const block1RlpHex = readFileSync(`${vectorsDirPath}/block1.rlp.hex`, 'utf8').trim()
const block1 = createBlockFromRLPSerializedBlock(hexToBytes(`0x${block1RlpHex}`), {
const block1 = createBlockFromRLP(hexToBytes(`0x${block1RlpHex}`), {
...opts,
executionWitness: executionWitness1,
})
const block1Payload = block1.toExecutionPayload()

const block2RlpHex = readFileSync(`${vectorsDirPath}/block2.rlp.hex`, 'utf8').trim()
const block2 = createBlockFromRLPSerializedBlock(hexToBytes(`0x${block2RlpHex}`), {
const block2 = createBlockFromRLP(hexToBytes(`0x${block2RlpHex}`), {
...opts,
executionWitness: executionWitness2,
})
Expand Down
Loading
Loading