Skip to content

Commit

Permalink
Block: rename BlockHeaderFromValuesArray
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottyPoi committed Aug 14, 2024
1 parent 0e62524 commit 9546ae4
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 27 deletions.
6 changes: 3 additions & 3 deletions packages/block/src/block/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
Block,
blockHeaderFromRpc,
createBlockHeader,
createBlockHeaderFromValuesArray,
createBlockHeaderFromBytesArray,
executionPayloadFromBeaconPayload,
} from '../index.js'

Expand Down Expand Up @@ -157,7 +157,7 @@ export function createBlockFromBytesArray(values: BlockBytes, opts?: BlockOption
// 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)
Expand Down Expand Up @@ -222,7 +222,7 @@ export function createBlockFromBytesArray(values: BlockBytes, opts?: BlockOption
uncleOpts.setHardfork = true
}
for (const uncleHeaderData of uhsData ?? []) {
uncleHeaders.push(createBlockHeaderFromValuesArray(uncleHeaderData, uncleOpts))
uncleHeaders.push(createBlockHeaderFromBytesArray(uncleHeaderData, uncleOpts))
}

const withdrawals = (withdrawalBytes as WithdrawalBytes[])
Expand Down
7 changes: 2 additions & 5 deletions packages/block/src/header/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ export function createBlockHeader(headerData: HeaderData = {}, opts: BlockOption
* @param values
* @param opts
*/
export function createBlockHeaderFromValuesArray(
values: BlockHeaderBytes,
opts: BlockOptions = {},
) {
export function createBlockHeaderFromBytesArray(values: BlockHeaderBytes, opts: BlockOptions = {}) {
const headerData = valuesArrayToHeaderData(values)
const { number, baseFeePerGas, excessBlobGas, blobGasUsed, parentBeaconBlockRoot, requestsRoot } =
headerData
Expand Down Expand Up @@ -71,7 +68,7 @@ export function createBlockHeaderFromRLP(
if (!Array.isArray(values)) {
throw new Error('Invalid serialized header input. Must be array')
}
return createBlockHeaderFromValuesArray(values as Uint8Array[], opts)
return createBlockHeaderFromBytesArray(values as Uint8Array[], opts)
}

export function createSealedCliqueBlockHeader(
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 @@ -18,7 +18,7 @@ import {
createBlockFromRLPSerializedBlock,
createBlockHeader,
createBlockHeaderFromRLP,
createBlockHeaderFromValuesArray,
createBlockHeaderFromBytesArray,
} from '../src/index.js'

import * as testData from './testdata/bcBlockGasLimitTest.json'
Expand Down Expand Up @@ -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',
Expand All @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions packages/blockchain/src/db/manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createBlockFromBytesArray, createBlockHeaderFromValuesArray } from '@ethereumjs/block'
import { createBlockFromBytesArray, createBlockHeaderFromBytesArray } from '@ethereumjs/block'
import { RLP } from '@ethereumjs/rlp'
import {
KECCAK256_RLP,
Expand Down Expand Up @@ -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)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/blockchain/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/blockchain/chain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createBlockFromBytesArray, 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'
Expand Down Expand Up @@ -468,7 +468,7 @@ export class Chain {
}
break
}
const header = createBlockHeaderFromValuesArray(h.raw(), {
const header = createBlockHeaderFromBytesArray(h.raw(), {
common: this.config.chainCommon,
setHardfork: true,
})
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/net/protocol/ethprotocol.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createBlockFromBytesArray, createBlockHeaderFromValuesArray } from '@ethereumjs/block'
import { createBlockFromBytesArray, createBlockHeaderFromBytesArray } from '@ethereumjs/block'
import { RLP } from '@ethereumjs/rlp'
import {
BlobEIP4844Transaction,
Expand Down Expand Up @@ -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
}),
],
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/net/protocol/lesprotocol.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createBlockHeaderFromValuesArray } from '@ethereumjs/block'
import { createBlockHeaderFromBytesArray } from '@ethereumjs/block'
import {
BIGINT_0,
bigIntToUnpaddedBytes,
Expand Down Expand Up @@ -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
}),
Expand Down
4 changes: 2 additions & 2 deletions packages/devp2p/examples/peer-communication-les.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createBlockFromBytesArray, 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'
Expand Down Expand Up @@ -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, [
Expand Down
4 changes: 2 additions & 2 deletions packages/devp2p/examples/peer-communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)) {
Expand Down

0 comments on commit 9546ae4

Please sign in to comment.