Skip to content

Commit

Permalink
Merge branch 'master' into block-err-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jochem-brouwer committed Sep 30, 2024
2 parents 2f904ab + f1fda27 commit ba79730
Show file tree
Hide file tree
Showing 47 changed files with 166 additions and 239 deletions.
17 changes: 8 additions & 9 deletions packages/block/src/header/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
equalsBytes,
hexToBytes,
toType,
zeros,
} from '@ethereumjs/util'
import { keccak256 } from 'ethereum-cryptography/keccak.js'

Expand Down Expand Up @@ -112,21 +111,21 @@ export class BlockHeader {
const skipValidateConsensusFormat = opts.skipConsensusFormatValidation ?? false

const defaults = {
parentHash: zeros(32),
parentHash: new Uint8Array(32),
uncleHash: KECCAK256_RLP_ARRAY,
coinbase: createZeroAddress(),
stateRoot: zeros(32),
stateRoot: new Uint8Array(32),
transactionsTrie: KECCAK256_RLP,
receiptTrie: KECCAK256_RLP,
logsBloom: zeros(256),
logsBloom: new Uint8Array(256),
difficulty: BIGINT_0,
number: BIGINT_0,
gasLimit: DEFAULT_GAS_LIMIT,
gasUsed: BIGINT_0,
timestamp: BIGINT_0,
extraData: new Uint8Array(0),
mixHash: zeros(32),
nonce: zeros(8),
mixHash: new Uint8Array(32),
nonce: new Uint8Array(8),
}

const parentHash = toType(headerData.parentHash, TypeOutput.Uint8Array) ?? defaults.parentHash
Expand Down Expand Up @@ -167,7 +166,7 @@ export class BlockHeader {
withdrawalsRoot: this.common.isActivatedEIP(4895) ? KECCAK256_RLP : undefined,
blobGasUsed: this.common.isActivatedEIP(4844) ? BIGINT_0 : undefined,
excessBlobGas: this.common.isActivatedEIP(4844) ? BIGINT_0 : undefined,
parentBeaconBlockRoot: this.common.isActivatedEIP(4788) ? zeros(32) : undefined,
parentBeaconBlockRoot: this.common.isActivatedEIP(4788) ? new Uint8Array(32) : undefined,
requestsRoot: this.common.isActivatedEIP(7685) ? KECCAK256_RLP : undefined,
}

Expand Down Expand Up @@ -441,8 +440,8 @@ export class BlockHeader {
)} (cannot exceed 32 bytes length, received ${extraData.length} bytes)`
error = true
}
if (!equalsBytes(nonce, zeros(8))) {
errorMsg += `, nonce: ${bytesToHex(nonce)} (expected: ${bytesToHex(zeros(8))})`
if (!equalsBytes(nonce, new Uint8Array(8))) {
errorMsg += `, nonce: ${bytesToHex(nonce)} (expected: ${bytesToHex(new Uint8Array(8))})`
error = true
}
}
Expand Down
27 changes: 10 additions & 17 deletions packages/block/test/block.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { Common, Goerli, Hardfork, Mainnet, createCustomCommon } from '@ethereumjs/common'
import { RLP } from '@ethereumjs/rlp'
import { createLegacyTx } from '@ethereumjs/tx'
import {
KECCAK256_RLP_ARRAY,
bytesToHex,
equalsBytes,
hexToBytes,
toBytes,
zeros,
} from '@ethereumjs/util'
import { KECCAK256_RLP_ARRAY, bytesToHex, equalsBytes, hexToBytes, toBytes } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

import { genTransactionsTrieRoot } from '../src/helpers.js'
Expand Down Expand Up @@ -76,14 +69,14 @@ describe('[Block]: block functions', () => {
headerArray.push(zero)
}

// mock header data (if set to zeros(0) header throws)
headerArray[0] = zeros(32) // parentHash
headerArray[2] = zeros(20) // coinbase
headerArray[3] = zeros(32) // stateRoot
headerArray[4] = zeros(32) // transactionsTrie
headerArray[5] = zeros(32) // receiptTrie
headerArray[13] = zeros(32) // mixHash
headerArray[14] = zeros(8) // nonce
// mock header data (if set to new Uint8Array() header throws)
headerArray[0] = new Uint8Array(32) // parentHash
headerArray[2] = new Uint8Array(20) // coinbase
headerArray[3] = new Uint8Array(32) // stateRoot
headerArray[4] = new Uint8Array(32) // transactionsTrie
headerArray[5] = new Uint8Array(32) // receiptTrie
headerArray[13] = new Uint8Array(32) // mixHash
headerArray[14] = new Uint8Array(8) // nonce

const valuesArray = <BlockBytes>[headerArray, [], []]

Expand Down Expand Up @@ -265,7 +258,7 @@ describe('[Block]: block functions', () => {
}
}

const zeroRoot = zeros(32)
const zeroRoot = new Uint8Array(32)

// Tx root
block = createBlock({
Expand Down
8 changes: 4 additions & 4 deletions packages/block/test/eip4788block.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { bytesToHex, zeros } from '@ethereumjs/util'
import { bytesToHex } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

import { createBlock, createBlockHeader } from '../src/index.js'
Expand All @@ -13,7 +13,7 @@ describe('EIP4788 header tests', () => {
() => {
createBlockHeader(
{
parentBeaconBlockRoot: zeros(32),
parentBeaconBlockRoot: new Uint8Array(32),
},
{
common: earlyCommon,
Expand Down Expand Up @@ -45,7 +45,7 @@ describe('EIP4788 header tests', () => {
{
excessBlobGas: 0n,
blobGasUsed: 0n,
parentBeaconBlockRoot: zeros(32),
parentBeaconBlockRoot: new Uint8Array(32),
},
{
common,
Expand All @@ -62,7 +62,7 @@ describe('EIP4788 header tests', () => {
)
assert.equal(
block.toJSON().header?.parentBeaconBlockRoot,
bytesToHex(zeros(32)),
bytesToHex(new Uint8Array(32)),
'JSON output includes excessBlobGas',
)
})
Expand Down
9 changes: 4 additions & 5 deletions packages/block/test/eip4895block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
createWithdrawalFromBytesArray,
hexToBytes,
randomBytes,
zeros,
} from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

Expand Down Expand Up @@ -57,7 +56,7 @@ describe('EIP4895 tests', () => {
() => {
createBlockHeader(
{
withdrawalsRoot: zeros(32),
withdrawalsRoot: new Uint8Array(32),
},
{
common: earlyCommon,
Expand All @@ -79,7 +78,7 @@ describe('EIP4895 tests', () => {
assert.doesNotThrow(() => {
createBlockHeader(
{
withdrawalsRoot: zeros(32),
withdrawalsRoot: new Uint8Array(32),
},
{
common,
Expand Down Expand Up @@ -116,7 +115,7 @@ describe('EIP4895 tests', () => {
createBlock(
{
header: {
withdrawalsRoot: zeros(32),
withdrawalsRoot: new Uint8Array(32),
},
withdrawals: [],
},
Expand All @@ -128,7 +127,7 @@ describe('EIP4895 tests', () => {
const block = createBlock(
{
header: {
withdrawalsRoot: zeros(32),
withdrawalsRoot: new Uint8Array(32),
},
withdrawals: [],
},
Expand Down
45 changes: 22 additions & 23 deletions packages/block/test/header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
createZeroAddress,
equalsBytes,
hexToBytes,
zeros,
} from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

Expand All @@ -32,21 +31,21 @@ import type { PrefixedHexString } from '@ethereumjs/util'
describe('[Block]: Header functions', () => {
it('should create with default constructor', () => {
function compareDefaultHeader(header: BlockHeader) {
assert.ok(equalsBytes(header.parentHash, zeros(32)))
assert.ok(equalsBytes(header.parentHash, new Uint8Array(32)))
assert.ok(equalsBytes(header.uncleHash, KECCAK256_RLP_ARRAY))
assert.ok(header.coinbase.equals(createZeroAddress()))
assert.ok(equalsBytes(header.stateRoot, zeros(32)))
assert.ok(equalsBytes(header.stateRoot, new Uint8Array(32)))
assert.ok(equalsBytes(header.transactionsTrie, KECCAK256_RLP))
assert.ok(equalsBytes(header.receiptTrie, KECCAK256_RLP))
assert.ok(equalsBytes(header.logsBloom, zeros(256)))
assert.ok(equalsBytes(header.logsBloom, new Uint8Array(256)))
assert.equal(header.difficulty, BigInt(0))
assert.equal(header.number, BigInt(0))
assert.equal(header.gasLimit, BigInt('0xffffffffffffff'))
assert.equal(header.gasUsed, BigInt(0))
assert.equal(header.timestamp, BigInt(0))
assert.ok(equalsBytes(header.extraData, new Uint8Array(0)))
assert.ok(equalsBytes(header.mixHash, zeros(32)))
assert.ok(equalsBytes(header.nonce, zeros(8)))
assert.ok(equalsBytes(header.mixHash, new Uint8Array(32)))
assert.ok(equalsBytes(header.nonce, new Uint8Array(8)))
}

const header = createBlockHeader()
Expand Down Expand Up @@ -137,14 +136,14 @@ describe('[Block]: Header functions', () => {
headerArray.push(zero)
}

// mock header data (if set to zeros(0) header throws)
headerArray[0] = zeros(32) //parentHash
headerArray[2] = zeros(20) //coinbase
headerArray[3] = zeros(32) //stateRoot
headerArray[4] = zeros(32) //transactionsTrie
headerArray[5] = zeros(32) //receiptTrie
headerArray[13] = zeros(32) // mixHash
headerArray[14] = zeros(8) // nonce
// mock header data (if set to new Uint8Array() header throws)
headerArray[0] = new Uint8Array(32) //parentHash
headerArray[2] = new Uint8Array(20) //coinbase
headerArray[3] = new Uint8Array(32) //stateRoot
headerArray[4] = new Uint8Array(32) //transactionsTrie
headerArray[5] = new Uint8Array(32) //receiptTrie
headerArray[13] = new Uint8Array(32) // mixHash
headerArray[14] = new Uint8Array(8) // nonce

let header = createBlockHeaderFromBytesArray(headerArray, { common })
assert.ok(Object.isFrozen(header), 'block should be frozen by default')
Expand All @@ -159,15 +158,15 @@ describe('[Block]: Header functions', () => {
it('Initialization -> createWithdrawalFromBytesArray() -> error cases', () => {
const headerArray = Array(22).fill(new Uint8Array(0))

// mock header data (if set to zeros(0) header throws)
headerArray[0] = zeros(32) //parentHash
headerArray[2] = zeros(20) //coinbase
headerArray[3] = zeros(32) //stateRoot
headerArray[4] = zeros(32) //transactionsTrie
headerArray[5] = zeros(32) //receiptTrie
headerArray[13] = zeros(32) // mixHash
headerArray[14] = zeros(8) // nonce
headerArray[15] = zeros(4) // bad data
// mock header data (if set to new Uint8Array() header throws)
headerArray[0] = new Uint8Array(32) //parentHash
headerArray[2] = new Uint8Array(20) //coinbase
headerArray[3] = new Uint8Array(32) //stateRoot
headerArray[4] = new Uint8Array(32) //transactionsTrie
headerArray[5] = new Uint8Array(32) //receiptTrie
headerArray[13] = new Uint8Array(32) // mixHash
headerArray[14] = new Uint8Array(8) // nonce
headerArray[15] = new Uint8Array(4) // bad data
try {
createBlockHeaderFromBytesArray(headerArray)
} catch (e: any) {
Expand Down
9 changes: 4 additions & 5 deletions packages/block/test/mergeBlock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
createZeroAddress,
equalsBytes,
hexToBytes,
zeros,
} from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

Expand All @@ -19,21 +18,21 @@ const common = new Common({
})

function validateMergeHeader(header: BlockHeader) {
assert.ok(equalsBytes(header.parentHash, zeros(32)), 'parentHash')
assert.ok(equalsBytes(header.parentHash, new Uint8Array(32)), 'parentHash')
assert.ok(equalsBytes(header.uncleHash, KECCAK256_RLP_ARRAY), 'uncleHash')
assert.ok(header.coinbase.equals(createZeroAddress()), 'coinbase')
assert.ok(equalsBytes(header.stateRoot, zeros(32)), 'stateRoot')
assert.ok(equalsBytes(header.stateRoot, new Uint8Array(32)), 'stateRoot')
assert.ok(equalsBytes(header.transactionsTrie, KECCAK256_RLP), 'transactionsTrie')
assert.ok(equalsBytes(header.receiptTrie, KECCAK256_RLP), 'receiptTrie')
assert.ok(equalsBytes(header.logsBloom, zeros(256)), 'logsBloom')
assert.ok(equalsBytes(header.logsBloom, new Uint8Array(256)), 'logsBloom')
assert.equal(header.difficulty, BigInt(0), 'difficulty')
assert.equal(header.number, BigInt(0), 'number')
assert.equal(header.gasLimit, BigInt('0xffffffffffffff'), 'gasLimit')
assert.equal(header.gasUsed, BigInt(0), 'gasUsed')
assert.equal(header.timestamp, BigInt(0), 'timestamp')
assert.ok(header.extraData.length <= 32, 'extraData')
assert.equal(header.mixHash.length, 32, 'mixHash')
assert.ok(equalsBytes(header.nonce, zeros(8)), 'nonce')
assert.ok(equalsBytes(header.nonce, new Uint8Array(8)), 'nonce')
}

describe('[Header]: Casper PoS / The Merge Functionality', () => {
Expand Down
9 changes: 4 additions & 5 deletions packages/client/src/miner/pendingBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
equalsBytes,
toBytes,
toType,
zeros,
} from '@ethereumjs/util'
import { BuildStatus, buildBlock } from '@ethereumjs/vm'
import { keccak256 } from 'ethereum-cryptography/keccak'
Expand Down Expand Up @@ -121,12 +120,12 @@ export class PendingBlock {
// potentially included in the fcU in future and can be safely added in uniqueness calc
const timestampBuf = bigIntToUnpaddedBytes(toType(timestamp ?? 0, TypeOutput.BigInt))
const gasLimitBuf = bigIntToUnpaddedBytes(gasLimit)
const mixHashBuf = toType(mixHash!, TypeOutput.Uint8Array) ?? zeros(32)
const mixHashBuf = toType(mixHash!, TypeOutput.Uint8Array) ?? new Uint8Array(32)
const parentBeaconBlockRootBuf =
toType(parentBeaconBlockRoot!, TypeOutput.Uint8Array) ?? zeros(32)
const coinbaseBuf = toType(coinbase ?? zeros(20), TypeOutput.Uint8Array)
toType(parentBeaconBlockRoot!, TypeOutput.Uint8Array) ?? new Uint8Array(32)
const coinbaseBuf = toType(coinbase ?? new Uint8Array(20), TypeOutput.Uint8Array)

let withdrawalsBuf = zeros(0)
let withdrawalsBuf = new Uint8Array()

if (withdrawals !== undefined && withdrawals !== null) {
const withdrawalsBufTemp: Uint8Array[] = []
Expand Down
9 changes: 4 additions & 5 deletions packages/client/src/rpc/modules/engine/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
equalsBytes,
hexToBytes,
toBytes,
zeros,
} from '@ethereumjs/util'

import { ExecStatus } from '../../../execution/index.js'
Expand Down Expand Up @@ -71,7 +70,7 @@ import type { Block, ExecutionPayload } from '@ethereumjs/block'
import type { PrefixedHexString } from '@ethereumjs/util'
import type { VM } from '@ethereumjs/vm'

const zeroBlockHash = zeros(32)
const zeroBlockHash = new Uint8Array(32)

/**
* engine_* RPC module
Expand Down Expand Up @@ -486,7 +485,7 @@ export class Engine {
const latestValidHash =
this.chain.blocks.latest !== null
? await validHash(this.chain.blocks.latest.hash(), this.chain, this.chainCache)
: bytesToHex(zeros(32))
: bytesToHex(new Uint8Array(32))
const response = {
status: Status.INVALID,
validationError: this.skeleton.fillStatus.validationError ?? '',
Expand Down Expand Up @@ -555,7 +554,7 @@ export class Engine {
const latestValidHash =
this.chain.blocks.latest !== null
? await validHash(this.chain.blocks.latest.hash(), this.chain, this.chainCache)
: bytesToHex(zeros(32))
: bytesToHex(new Uint8Array(32))
const response = {
status: Status.INVALID,
validationError: this.skeleton.fillStatus.validationError ?? '',
Expand Down Expand Up @@ -978,7 +977,7 @@ export class Engine {
const latestValidHash =
this.chain.blocks.latest !== null
? await validHash(this.chain.blocks.latest.hash(), this.chain, this.chainCache)
: bytesToHex(zeros(32))
: bytesToHex(new Uint8Array(32))
const response = {
payloadStatus: {
status: Status.INVALID,
Expand Down
3 changes: 1 addition & 2 deletions packages/client/src/service/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
formatBigDecimal,
intToBytes,
utf8ToBytes,
zeros,
} from '@ethereumjs/util'

import { short, timeDuration } from '../util/index.js'
Expand Down Expand Up @@ -89,7 +88,7 @@ export const errReorgDenied = new Error('non-forced head reorg denied')
*/
export const errSyncMerged = new Error('sync merged')

const zeroBlockHash = zeros(32)
const zeroBlockHash = new Uint8Array(32)
/**
* The Skeleton chain class helps support beacon sync by accepting head blocks
* while backfill syncing the rest of the chain.
Expand Down
4 changes: 2 additions & 2 deletions packages/common/test/hardforks.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hexToBytes, zeros } from '@ethereumjs/util'
import { hexToBytes } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

import {
Expand Down Expand Up @@ -312,7 +312,7 @@ describe('[Common]: Hardfork logic', () => {
hardfork: Hardfork.Cancun,
mergeForkIdPostMerge: true,
}
const genesisHash = zeros(32)
const genesisHash = new Uint8Array(32)
const zeroCommon = createCommonFromGethGenesis(defaultConfig, gethConfig)

const zeroCommonShanghaiFork = zeroCommon.forkHash(Hardfork.Shanghai, genesisHash)
Expand Down
Loading

0 comments on commit ba79730

Please sign in to comment.