Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into eof-fuzz
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Aug 7, 2024
2 parents 198c917 + c00859b commit 238c47b
Show file tree
Hide file tree
Showing 57 changed files with 293 additions and 266 deletions.
4 changes: 2 additions & 2 deletions packages/block/examples/simple.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createHeader } from '@ethereumjs/block'
import { createBlockHeader } from '@ethereumjs/block'
import { bytesToHex } from '@ethereumjs/util'

import type { HeaderData } from '@ethereumjs/block'
Expand All @@ -9,5 +9,5 @@ const headerData: HeaderData = {
gasLimit: 8000000,
timestamp: 1562422144,
}
const header = createHeader(headerData)
const header = createBlockHeader(headerData)
console.log(`Created block header with hash=${bytesToHex(header.hash())}`)
24 changes: 15 additions & 9 deletions packages/block/src/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import type {
* @param headerData
* @param opts
*/
export function createHeader(headerData: HeaderData = {}, opts: BlockOptions = {}) {
export function createBlockHeader(headerData: HeaderData = {}, opts: BlockOptions = {}) {
return new BlockHeader(headerData, opts)
}

Expand All @@ -72,11 +72,14 @@ export function createHeader(headerData: HeaderData = {}, opts: BlockOptions = {
* @param values
* @param opts
*/
export function createHeaderFromValuesArray(values: BlockHeaderBytes, opts: BlockOptions = {}) {
export function createBlockHeaderFromValuesArray(
values: BlockHeaderBytes,
opts: BlockOptions = {},
) {
const headerData = valuesArrayToHeaderData(values)
const { number, baseFeePerGas, excessBlobGas, blobGasUsed, parentBeaconBlockRoot, requestsRoot } =
headerData
const header = createHeader(headerData, opts)
const header = createBlockHeader(headerData, opts)
if (header.common.isActivatedEIP(1559) && baseFeePerGas === undefined) {
const eip1559ActivationBlock = bigIntToBytes(header.common.eipBlock(1559)!)
if (
Expand Down Expand Up @@ -109,12 +112,15 @@ export function createHeaderFromValuesArray(values: BlockHeaderBytes, opts: Bloc
* @param serializedHeaderData
* @param opts
*/
export function createHeaderFromRLP(serializedHeaderData: Uint8Array, opts: BlockOptions = {}) {
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 createHeaderFromValuesArray(values as Uint8Array[], opts)
return createBlockHeaderFromValuesArray(values as Uint8Array[], opts)
}

/**
Expand All @@ -133,7 +139,7 @@ export function createBlock(blockData: BlockData = {}, opts?: BlockOptions) {
requests: clRequests,
} = blockData

const header = createHeader(headerData, opts)
const header = createBlockHeader(headerData, opts)

// parse transactions
const transactions = []
Expand All @@ -160,7 +166,7 @@ export function createBlock(blockData: BlockData = {}, opts?: BlockOptions) {
uncleOpts.setHardfork = true
}
for (const uhData of uhsData ?? []) {
const uh = createHeader(uhData, uncleOpts)
const uh = createBlockHeader(uhData, uncleOpts)
uncleHeaders.push(uh)
}

Expand Down Expand Up @@ -194,7 +200,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 = createHeaderFromValuesArray(headerData, opts)
const header = createBlockHeaderFromValuesArray(headerData, opts)

// conditional assignment of rest of values and splicing them out from the valuesTail
const withdrawalBytes = header.common.isActivatedEIP(4895)
Expand Down Expand Up @@ -259,7 +265,7 @@ export function createBlockFromValuesArray(values: BlockBytes, opts?: BlockOptio
uncleOpts.setHardfork = true
}
for (const uncleHeaderData of uhsData ?? []) {
uncleHeaders.push(createHeaderFromValuesArray(uncleHeaderData, uncleOpts))
uncleHeaders.push(createBlockHeaderFromValuesArray(uncleHeaderData, uncleOpts))
}

const withdrawals = (withdrawalBytes as WithdrawalBytes[])
Expand Down
2 changes: 1 addition & 1 deletion packages/block/src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class BlockHeader {
* This constructor takes the values, validates them, assigns them and freezes the object.
*
* @deprecated Use the public static factory methods to assist in creating a Header object from
* varying data types. For a default empty header, use {@link createHeader}.
* varying data types. For a default empty header, use {@link createBlockHeader}.
*
*/
constructor(headerData: HeaderData, opts: BlockOptions = {}) {
Expand Down
14 changes: 7 additions & 7 deletions packages/block/test/clique.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Common, Goerli, Hardfork } from '@ethereumjs/common'
import { Address, createZeroAddress, hexToBytes } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

import { createHeader } from '../src/constructors.js'
import { createBlockHeader } from '../src/constructors.js'

describe('[Header]: Clique PoA Functionality', () => {
const common = new Common({ chain: Goerli, hardfork: Hardfork.Chainstart })

it('Header Data', () => {
let header = createHeader({ number: 1 })
let header = createBlockHeader({ number: 1 })
assert.throws(
() => {
header.cliqueIsEpochTransition()
Expand All @@ -18,13 +18,13 @@ describe('[Header]: Clique PoA Functionality', () => {
'cliqueIsEpochTransition() -> should throw on PoW networks',
)

header = createHeader({ extraData: new Uint8Array(97) }, { common })
header = createBlockHeader({ extraData: new Uint8Array(97) }, { common })
assert.ok(
header.cliqueIsEpochTransition(),
'cliqueIsEpochTransition() -> should indicate an epoch transition for the genesis block',
)

header = createHeader({ number: 1, extraData: new Uint8Array(97) }, { common })
header = createBlockHeader({ number: 1, extraData: new Uint8Array(97) }, { common })
assert.notOk(
header.cliqueIsEpochTransition(),
'cliqueIsEpochTransition() -> should correctly identify a non-epoch block',
Expand All @@ -48,7 +48,7 @@ describe('[Header]: Clique PoA Functionality', () => {
'cliqueEpochTransitionSigners() -> should throw on non-epch block',
)

header = createHeader({ number: 60000, extraData: new Uint8Array(137) }, { common })
header = createBlockHeader({ number: 60000, extraData: new Uint8Array(137) }, { common })
assert.ok(
header.cliqueIsEpochTransition(),
'cliqueIsEpochTransition() -> should correctly identify an epoch block',
Expand Down Expand Up @@ -89,7 +89,7 @@ describe('[Header]: Clique PoA Functionality', () => {
it('Signing', () => {
const cliqueSigner = A.privateKey

let header = createHeader(
let header = createBlockHeader(
{ number: 1, extraData: new Uint8Array(97) },
{ common, freeze: false, cliqueSigner },
)
Expand All @@ -98,7 +98,7 @@ describe('[Header]: Clique PoA Functionality', () => {
assert.ok(header.cliqueVerifySignature([A.address]), 'should verify signature')
assert.ok(header.cliqueSigner().equals(A.address), 'should recover the correct signer address')

header = createHeader({ extraData: new Uint8Array(97) }, { common })
header = createBlockHeader({ extraData: new Uint8Array(97) }, { common })
assert.ok(
header.cliqueSigner().equals(createZeroAddress()),
'should return zero address on default block',
Expand Down
34 changes: 17 additions & 17 deletions packages/block/test/eip1559block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { create1559FeeMarketTx } from '@ethereumjs/tx'
import { hexToBytes } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

import { createBlock, createHeader } from '../src/constructors.js'
import { createBlock, createBlockHeader } from '../src/constructors.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'
Expand Down Expand Up @@ -36,7 +36,7 @@ describe('EIP1559 tests', () => {
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Istanbul })
assert.throws(
() => {
createHeader(
createBlockHeader(
{
number: BigInt(1),
parentHash: genesis.hash(),
Expand All @@ -57,7 +57,7 @@ describe('EIP1559 tests', () => {

it('Header -> genericFormatValidation checks', async () => {
try {
createHeader(
createBlockHeader(
{
number: BigInt(1),
parentHash: genesis.hash(),
Expand All @@ -81,7 +81,7 @@ describe('EIP1559 tests', () => {
}

try {
const header = createHeader(
const header = createBlockHeader(
{
number: BigInt(1),
parentHash: genesis.hash(),
Expand Down Expand Up @@ -127,7 +127,7 @@ describe('EIP1559 tests', () => {

it('Header -> validate()', async () => {
try {
createHeader(
createBlockHeader(
{
baseFeePerGas: BigInt(1000),
number: BigInt(1),
Expand Down Expand Up @@ -182,7 +182,7 @@ describe('EIP1559 tests', () => {

it('Header -> validate() -> gas usage', async () => {
try {
createHeader(
createBlockHeader(
{
number: BigInt(1),
parentHash: genesis.hash(),
Expand All @@ -205,7 +205,7 @@ describe('EIP1559 tests', () => {
})

it('Header -> validate() -> gas usage', async () => {
createHeader(
createBlockHeader(
{
number: BigInt(1),
parentHash: genesis.hash(),
Expand Down Expand Up @@ -241,7 +241,7 @@ describe('EIP1559 tests', () => {

it('Header -> validate() -> gasLimit -> success cases', async () => {
let parentGasLimit = genesis.header.gasLimit * BigInt(2)
createHeader(
createBlockHeader(
{
number: BigInt(1),
parentHash: genesis.hash(),
Expand All @@ -257,7 +257,7 @@ describe('EIP1559 tests', () => {

assert.ok(true, 'should not throw if gas limit is between bounds (HF transition block)')

createHeader(
createBlockHeader(
{
number: BigInt(1),
parentHash: genesis.hash(),
Expand All @@ -274,7 +274,7 @@ describe('EIP1559 tests', () => {
assert.ok(true, 'should not throw if gas limit is between bounds (HF transition block)')

parentGasLimit = block1.header.gasLimit
createHeader(
createBlockHeader(
{
number: BigInt(2),
parentHash: block1.hash(),
Expand All @@ -290,7 +290,7 @@ describe('EIP1559 tests', () => {

assert.ok(true, 'should not throw if gas limit is between bounds (post-HF transition block)')

createHeader(
createBlockHeader(
{
number: BigInt(2),
parentHash: block1.hash(),
Expand All @@ -309,7 +309,7 @@ describe('EIP1559 tests', () => {

it('Header -> validateGasLimit() -> error cases', async () => {
let parentGasLimit = genesis.header.gasLimit * BigInt(2)
let header = createHeader(
let header = createBlockHeader(
{
number: BigInt(1),
parentHash: genesis.hash(),
Expand All @@ -333,7 +333,7 @@ describe('EIP1559 tests', () => {
}

parentGasLimit = block1.header.gasLimit
header = createHeader(
header = createBlockHeader(
{
number: BigInt(2),
parentHash: block1.hash(),
Expand All @@ -359,7 +359,7 @@ describe('EIP1559 tests', () => {

it('Header -> validateGasLimit() -> error cases', async () => {
let parentGasLimit = genesis.header.gasLimit * BigInt(2)
let header = createHeader(
let header = createBlockHeader(
{
number: BigInt(1),
parentHash: genesis.hash(),
Expand All @@ -383,7 +383,7 @@ describe('EIP1559 tests', () => {
}

parentGasLimit = block1.header.gasLimit
header = createHeader(
header = createBlockHeader(
{
number: BigInt(2),
parentHash: block1.hash(),
Expand Down Expand Up @@ -452,7 +452,7 @@ describe('EIP1559 tests', () => {
it('Header -> calcNextBaseFee()', () => {
for (let index = 0; index < eip1559BaseFee.length; index++) {
const item = eip1559BaseFee[index]
const result = createHeader(
const result = createBlockHeader(
{
baseFeePerGas: BigInt(item.parentBaseFee),
gasUsed: BigInt(item.parentGasUsed),
Expand All @@ -466,7 +466,7 @@ describe('EIP1559 tests', () => {
})

it('Header -> toJSON()', () => {
const header = createHeader(
const header = createBlockHeader(
{
number: BigInt(3),
parentHash: genesis.hash(),
Expand Down
10 changes: 5 additions & 5 deletions packages/block/test/eip4788block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, createHeader } from '../src/constructors.js'
import { createBlock, createBlockHeader } from '../src/constructors.js'

describe('EIP4788 header tests', () => {
it('should work', () => {
Expand All @@ -11,7 +11,7 @@ describe('EIP4788 header tests', () => {

assert.throws(
() => {
createHeader(
createBlockHeader(
{
parentBeaconBlockRoot: zeros(32),
},
Expand All @@ -27,7 +27,7 @@ describe('EIP4788 header tests', () => {

assert.throws(
() => {
createHeader(
createBlockHeader(
{
blobGasUsed: 1n,
},
Expand All @@ -41,7 +41,7 @@ describe('EIP4788 header tests', () => {
'should throw when setting blobGasUsed with EIP4844 not being activated',
)
assert.doesNotThrow(() => {
createHeader(
createBlockHeader(
{
excessBlobGas: 0n,
blobGasUsed: 0n,
Expand All @@ -56,7 +56,7 @@ describe('EIP4788 header tests', () => {

const block = createBlock(
{
header: createHeader({}, { common }),
header: createBlockHeader({}, { common }),
},
{ common, skipConsensusFormatValidation: true },
)
Expand Down
Loading

0 comments on commit 238c47b

Please sign in to comment.