From 9546ae46e98ab5b7cdbdd33402fa4a34c1f9c969 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Tue, 13 Aug 2024 18:15:30 -0600 Subject: [PATCH] Block: rename BlockHeaderFromValuesArray --- packages/block/src/block/constructors.ts | 6 +++--- packages/block/src/header/constructors.ts | 7 ++----- packages/block/test/header.spec.ts | 10 +++++----- packages/blockchain/src/db/manager.ts | 4 ++-- packages/blockchain/test/index.spec.ts | 4 ++-- packages/client/src/blockchain/chain.ts | 4 ++-- packages/client/src/net/protocol/ethprotocol.ts | 4 ++-- packages/client/src/net/protocol/lesprotocol.ts | 4 ++-- packages/devp2p/examples/peer-communication-les.ts | 4 ++-- packages/devp2p/examples/peer-communication.ts | 4 ++-- 10 files changed, 24 insertions(+), 27 deletions(-) diff --git a/packages/block/src/block/constructors.ts b/packages/block/src/block/constructors.ts index 984384494b..2a50ee08d0 100644 --- a/packages/block/src/block/constructors.ts +++ b/packages/block/src/block/constructors.ts @@ -33,7 +33,7 @@ import { Block, blockHeaderFromRpc, createBlockHeader, - createBlockHeaderFromValuesArray, + createBlockHeaderFromBytesArray, executionPayloadFromBeaconPayload, } from '../index.js' @@ -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) @@ -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[]) diff --git a/packages/block/src/header/constructors.ts b/packages/block/src/header/constructors.ts index bbaab74fae..5add94da8e 100644 --- a/packages/block/src/header/constructors.ts +++ b/packages/block/src/header/constructors.ts @@ -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 @@ -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( diff --git a/packages/block/test/header.spec.ts b/packages/block/test/header.spec.ts index 0c7cd0a2c9..d61b15fb18 100644 --- a/packages/block/test/header.spec.ts +++ b/packages/block/test/header.spec.ts @@ -18,7 +18,7 @@ import { createBlockFromRLPSerializedBlock, createBlockHeader, createBlockHeaderFromRLP, - createBlockHeaderFromValuesArray, + createBlockHeaderFromBytesArray, } from '../src/index.js' import * as testData from './testdata/bcBlockGasLimitTest.json' @@ -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', @@ -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') diff --git a/packages/blockchain/src/db/manager.ts b/packages/blockchain/src/db/manager.ts index 13ba33ca03..bca9cee717 100644 --- a/packages/blockchain/src/db/manager.ts +++ b/packages/blockchain/src/db/manager.ts @@ -1,4 +1,4 @@ -import { createBlockFromBytesArray, createBlockHeaderFromValuesArray } from '@ethereumjs/block' +import { createBlockFromBytesArray, createBlockHeaderFromBytesArray } from '@ethereumjs/block' import { RLP } from '@ethereumjs/rlp' import { KECCAK256_RLP, @@ -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) } /** diff --git a/packages/blockchain/test/index.spec.ts b/packages/blockchain/test/index.spec.ts index f195591334..508358cd5a 100644 --- a/packages/blockchain/test/index.spec.ts +++ b/packages/blockchain/test/index.spec.ts @@ -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' @@ -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) diff --git a/packages/client/src/blockchain/chain.ts b/packages/client/src/blockchain/chain.ts index b03723e17b..af99f4aa63 100644 --- a/packages/client/src/blockchain/chain.ts +++ b/packages/client/src/blockchain/chain.ts @@ -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' @@ -468,7 +468,7 @@ export class Chain { } break } - const header = createBlockHeaderFromValuesArray(h.raw(), { + const header = createBlockHeaderFromBytesArray(h.raw(), { common: this.config.chainCommon, setHardfork: true, }) diff --git a/packages/client/src/net/protocol/ethprotocol.ts b/packages/client/src/net/protocol/ethprotocol.ts index 7f11890fb4..f5b3ad703c 100644 --- a/packages/client/src/net/protocol/ethprotocol.ts +++ b/packages/client/src/net/protocol/ethprotocol.ts @@ -1,4 +1,4 @@ -import { createBlockFromBytesArray, createBlockHeaderFromValuesArray } from '@ethereumjs/block' +import { createBlockFromBytesArray, createBlockHeaderFromBytesArray } from '@ethereumjs/block' import { RLP } from '@ethereumjs/rlp' import { BlobEIP4844Transaction, @@ -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 }), ], diff --git a/packages/client/src/net/protocol/lesprotocol.ts b/packages/client/src/net/protocol/lesprotocol.ts index b906eaf48c..7235951898 100644 --- a/packages/client/src/net/protocol/lesprotocol.ts +++ b/packages/client/src/net/protocol/lesprotocol.ts @@ -1,4 +1,4 @@ -import { createBlockHeaderFromValuesArray } from '@ethereumjs/block' +import { createBlockHeaderFromBytesArray } from '@ethereumjs/block' import { BIGINT_0, bigIntToUnpaddedBytes, @@ -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 }), diff --git a/packages/devp2p/examples/peer-communication-les.ts b/packages/devp2p/examples/peer-communication-les.ts index f7c4bbe018..8aeb9be843 100644 --- a/packages/devp2p/examples/peer-communication-les.ts +++ b/packages/devp2p/examples/peer-communication-les.ts @@ -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' @@ -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, [ diff --git a/packages/devp2p/examples/peer-communication.ts b/packages/devp2p/examples/peer-communication.ts index fecf9d3c0d..e98808331c 100644 --- a/packages/devp2p/examples/peer-communication.ts +++ b/packages/devp2p/examples/peer-communication.ts @@ -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) @@ -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)) {