From a7a517e035b3500a32cf17d7304e0e2221cbf8e5 Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Tue, 21 Jan 2025 21:12:20 +0100 Subject: [PATCH] Client: fix version logging and hive block imports (#3840) * client: correctly find package.json file to extract version * client: fix info comment with double 0xs * blockchain: ensure correct empty request hash for prague --- packages/blockchain/src/blockchain.ts | 4 ++-- packages/blockchain/src/helpers.ts | 20 +--------------- packages/client/bin/cli.ts | 2 +- packages/client/src/util/index.ts | 33 +++++++++++++++++++++++---- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/packages/blockchain/src/blockchain.ts b/packages/blockchain/src/blockchain.ts index 9b03af1a3b..e5cf6c9c85 100644 --- a/packages/blockchain/src/blockchain.ts +++ b/packages/blockchain/src/blockchain.ts @@ -7,6 +7,7 @@ import { KECCAK256_RLP, Lock, MapDB, + SHA256_NULL, bigIntToHex, bytesToHex, bytesToUnprefixedHex, @@ -26,7 +27,6 @@ import { } from './db/helpers.js' import { DBManager } from './db/manager.js' import { DBTarget } from './db/operation.js' -import { SHA256_EMPTY_RH } from './helpers.js' import type { BlockchainEvent, @@ -1320,7 +1320,7 @@ export class Blockchain implements BlockchainInterface { number: 0, stateRoot, withdrawalsRoot: common.isActivatedEIP(4895) ? KECCAK256_RLP : undefined, - requestsHash: common.isActivatedEIP(7685) ? SHA256_EMPTY_RH : undefined, + requestsHash: common.isActivatedEIP(7685) ? SHA256_NULL : undefined, } if (common.consensusType() === 'poa') { if (common.genesis().extraData) { diff --git a/packages/blockchain/src/helpers.ts b/packages/blockchain/src/helpers.ts index d3c0272b03..9c8676d42f 100644 --- a/packages/blockchain/src/helpers.ts +++ b/packages/blockchain/src/helpers.ts @@ -1,6 +1,6 @@ import { ChainGenesis } from '@ethereumjs/common' import { genesisMPTStateRoot } from '@ethereumjs/mpt' -import { type GenesisState, hexToBytes } from '@ethereumjs/util' +import { type GenesisState } from '@ethereumjs/util' import type { Chain, Common } from '@ethereumjs/common' @@ -40,21 +40,3 @@ export async function getGenesisStateRoot(chainId: Chain, common: Common): Promi const chainGenesis = ChainGenesis[chainId] return chainGenesis !== undefined ? chainGenesis.stateRoot : genGenesisStateRoot({}, common) } - -/* -The code below calculates the empty requests hash as of devnet-4 for EIP 7685 -Note: it is not possible to calculate this directly in the blockchain package, -this introduces the `ethereum-cryptography` dependency. - -// Used to calculate the empty requests hash -const z0 = sha256(new Uint8Array([0])) -const z1 = sha256(new Uint8Array([1])) -const z2 = sha256(new Uint8Array([2])) - -export const SHA256_EMPTY_RH = sha256(concatBytes(z0, z1, z2)) - -*/ - -export const SHA256_EMPTY_RH = hexToBytes( - '0x6036c41849da9c076ed79654d434017387a88fb833c2856b32e18218b3341c5f', -) diff --git a/packages/client/bin/cli.ts b/packages/client/bin/cli.ts index 230cea85f5..0fbf8d3271 100755 --- a/packages/client/bin/cli.ts +++ b/packages/client/bin/cli.ts @@ -236,7 +236,7 @@ async function startClient( blocks.push(block) buf = RLP.decode(buf.remainder, true) config.logger.info( - `Preloading block hash=0x${short(bytesToHex(block.header.hash()))} number=${ + `Preloading block hash=${short(bytesToHex(block.header.hash()))} number=${ block.header.number }`, ) diff --git a/packages/client/src/util/index.ts b/packages/client/src/util/index.ts index 71cad44bd0..de076c6471 100644 --- a/packages/client/src/util/index.ts +++ b/packages/client/src/util/index.ts @@ -2,9 +2,9 @@ * @module util */ import { bytesToHex } from '@ethereumjs/util' -import { readFileSync } from 'fs' +import { existsSync, readFileSync } from 'fs' import { platform } from 'os' -import { dirname } from 'path' +import { dirname, join as joinPath } from 'path' import { fileURLToPath } from 'url' export * from './inclineClient.js' @@ -24,8 +24,33 @@ export function short(bytes: Uint8Array | string): string { return str } -export function getPackageJSON(): { version: string } { - return JSON.parse(readFileSync(__dirname + '/../../package.json', 'utf-8')) +export function getPackageJSON() { + // Find the package.json by checking the current directory and then + // moving up a directory each time until package.json is found, + // or we are at the root directory. + let currentDir = __dirname + + // eslint-disable-next-line no-constant-condition + while (true) { + const packageJsonPath = joinPath(currentDir, 'package.json') + if (existsSync(packageJsonPath)) { + // Read package.json contents + const parsedJSON = JSON.parse(readFileSync(packageJsonPath, 'utf8')) + + // Verify that the package.json contains the version + if (parsedJSON.version !== undefined) { + return parsedJSON + } + // If it does not contain the version, then keep moving to upper directories until a version is found + } + const parentDir = dirname(currentDir) + // If we've reached the root directory, stop searching + if (parentDir === currentDir) { + // No package.json found + return {} + } + currentDir = parentDir // Move up one directory + } } export function getClientVersion() {