Skip to content

Commit

Permalink
fix: avoid toHexString() (#7075)
Browse files Browse the repository at this point in the history
* fix: avoid toHexString()

* fix: use toRootHex and toPubkeyHex where applicable
  • Loading branch information
twoeths authored Sep 12, 2024
1 parent 6c1e335 commit 9f4bf50
Show file tree
Hide file tree
Showing 32 changed files with 107 additions and 108 deletions.
7 changes: 4 additions & 3 deletions packages/api/src/beacon/routes/proof.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {CompactMultiProof, ProofType} from "@chainsafe/persistent-merkle-tree";
import {ByteListType, ContainerType, fromHexString, toHexString} from "@chainsafe/ssz";
import {ByteListType, ContainerType, fromHexString} from "@chainsafe/ssz";
import {toHex} from "@lodestar/utils";
import {ChainForkConfig} from "@lodestar/config";
import {ssz} from "@lodestar/types";
import {Endpoint, RouteDefinitions, Schema} from "../../utils/index.js";
Expand Down Expand Up @@ -45,7 +46,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
url: "/eth/v0/beacon/proof/state/{state_id}",
method: "GET",
req: {
writeReq: ({stateId, descriptor}) => ({params: {state_id: stateId}, query: {format: toHexString(descriptor)}}),
writeReq: ({stateId, descriptor}) => ({params: {state_id: stateId}, query: {format: toHex(descriptor)}}),
parseReq: ({params, query}) => ({stateId: params.state_id, descriptor: fromHexString(query.format)}),
schema: {params: {state_id: Schema.StringRequired}, query: {format: Schema.StringRequired}},
},
Expand All @@ -63,7 +64,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
url: "/eth/v0/beacon/proof/block/{block_id}",
method: "GET",
req: {
writeReq: ({blockId, descriptor}) => ({params: {block_id: blockId}, query: {format: toHexString(descriptor)}}),
writeReq: ({blockId, descriptor}) => ({params: {block_id: blockId}, query: {format: toHex(descriptor)}}),
parseReq: ({params, query}) => ({blockId: params.block_id, descriptor: fromHexString(query.format)}),
schema: {params: {block_id: Schema.StringRequired}, query: {format: Schema.StringRequired}},
},
Expand Down
15 changes: 8 additions & 7 deletions packages/api/src/beacon/routes/validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {ContainerType, fromHexString, toHexString, Type, ValueOf} from "@chainsafe/ssz";
import {ContainerType, fromHexString, Type, ValueOf} from "@chainsafe/ssz";
import {ChainForkConfig} from "@lodestar/config";
import {isForkBlobs, isForkPostElectra} from "@lodestar/params";
import {
Expand All @@ -20,6 +20,7 @@ import {
Attestation,
sszTypesFor,
} from "@lodestar/types";
import {toHex, toRootHex} from "@lodestar/utils";
import {Endpoint, RouteDefinitions, Schema} from "../../utils/index.js";
import {fromGraffitiHex, toBoolean, toGraffitiHex} from "../../utils/serdes.js";
import {getExecutionForkTypes, toForkName} from "../../utils/fork.js";
Expand Down Expand Up @@ -623,7 +624,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
writeReq: ({slot, randaoReveal, graffiti, feeRecipient, builderSelection, strictFeeRecipientCheck}) => ({
params: {slot},
query: {
randao_reveal: toHexString(randaoReveal),
randao_reveal: toHex(randaoReveal),
graffiti: toGraffitiHex(graffiti),
fee_recipient: feeRecipient,
builder_selection: builderSelection,
Expand Down Expand Up @@ -674,7 +675,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
}) => ({
params: {slot},
query: {
randao_reveal: toHexString(randaoReveal),
randao_reveal: toHex(randaoReveal),
graffiti: toGraffitiHex(graffiti),
skip_randao_verification: writeSkipRandaoVerification(skipRandaoVerification),
fee_recipient: feeRecipient,
Expand Down Expand Up @@ -765,7 +766,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
req: {
writeReq: ({slot, randaoReveal, graffiti}) => ({
params: {slot},
query: {randao_reveal: toHexString(randaoReveal), graffiti: toGraffitiHex(graffiti)},
query: {randao_reveal: toHex(randaoReveal), graffiti: toGraffitiHex(graffiti)},
}),
parseReq: ({params, query}) => ({
slot: params.slot,
Expand Down Expand Up @@ -805,7 +806,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
method: "GET",
req: {
writeReq: ({slot, subcommitteeIndex, beaconBlockRoot}) => ({
query: {slot, subcommittee_index: subcommitteeIndex, beacon_block_root: toHexString(beaconBlockRoot)},
query: {slot, subcommittee_index: subcommitteeIndex, beacon_block_root: toRootHex(beaconBlockRoot)},
}),
parseReq: ({query}) => ({
slot: query.slot,
Expand All @@ -830,7 +831,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
method: "GET",
req: {
writeReq: ({attestationDataRoot, slot}) => ({
query: {attestation_data_root: toHexString(attestationDataRoot), slot},
query: {attestation_data_root: toRootHex(attestationDataRoot), slot},
}),
parseReq: ({query}) => ({
attestationDataRoot: fromHexString(query.attestation_data_root),
Expand All @@ -853,7 +854,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
method: "GET",
req: {
writeReq: ({attestationDataRoot, slot, committeeIndex}) => ({
query: {attestation_data_root: toHexString(attestationDataRoot), slot, committee_index: committeeIndex},
query: {attestation_data_root: toHex(attestationDataRoot), slot, committee_index: committeeIndex},
}),
parseReq: ({query}) => ({
attestationDataRoot: fromHexString(query.attestation_data_root),
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/builder/routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {fromHexString, toHexString} from "@chainsafe/ssz";
import {fromHexString} from "@chainsafe/ssz";
import {
ssz,
bellatrix,
Expand All @@ -13,7 +13,7 @@ import {
} from "@lodestar/types";
import {ForkName, isForkBlobs} from "@lodestar/params";
import {ChainForkConfig} from "@lodestar/config";
import {toPubkeyHex} from "@lodestar/utils";
import {toPubkeyHex, toRootHex} from "@lodestar/utils";

import {Endpoint, RouteDefinitions, Schema} from "../utils/index.js";
import {MetaHeader, VersionCodec, VersionMeta} from "../utils/metadata.js";
Expand Down Expand Up @@ -106,7 +106,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
method: "GET",
req: {
writeReq: ({slot, parentHash, proposerPubkey: proposerPubKey}) => ({
params: {slot, parent_hash: toHexString(parentHash), pubkey: toPubkeyHex(proposerPubKey)},
params: {slot, parent_hash: toRootHex(parentHash), pubkey: toPubkeyHex(proposerPubKey)},
}),
parseReq: ({params}) => ({
slot: params.slot,
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/utils/serdes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {fromHexString, JsonPath, toHexString} from "@chainsafe/ssz";
import {fromHexString, JsonPath} from "@chainsafe/ssz";
import {toHex} from "@lodestar/utils";

/**
* Serialize proof path to JSON.
Expand Down Expand Up @@ -82,7 +83,7 @@ export function toGraffitiHex(utf8?: string): string | undefined {
return undefined;
}

const hex = toHexString(new TextEncoder().encode(utf8));
const hex = toHex(new TextEncoder().encode(utf8));

if (hex.length > GRAFFITI_HEX_LENGTH) {
// remove characters from the end if hex string is too long
Expand Down
7 changes: 3 additions & 4 deletions packages/beacon-node/src/chain/blocks/importBlock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {toHexString} from "@chainsafe/ssz";
import {capella, ssz, altair, BeaconBlock} from "@lodestar/types";
import {ForkLightClient, ForkSeq, INTERVALS_PER_SLOT, MAX_SEED_LOOKAHEAD, SLOTS_PER_EPOCH} from "@lodestar/params";
import {
Expand All @@ -10,7 +9,7 @@ import {
} from "@lodestar/state-transition";
import {routes} from "@lodestar/api";
import {ForkChoiceError, ForkChoiceErrorCode, EpochDifference, AncestorStatus} from "@lodestar/fork-choice";
import {isErrorAborted, toRootHex} from "@lodestar/utils";
import {isErrorAborted, toHex, toRootHex} from "@lodestar/utils";
import {ZERO_HASH_HEX} from "../../constants/index.js";
import {toCheckpointHex} from "../stateCache/index.js";
import {isOptimisticBlock} from "../../util/forkChoice.js";
Expand Down Expand Up @@ -433,8 +432,8 @@ export async function importBlock(
blockRoot: blockRootHex,
slot: blockSlot,
index,
kzgCommitment: toHexString(kzgCommitment),
versionedHash: toHexString(kzgCommitmentToVersionedHash(kzgCommitment)),
kzgCommitment: toHex(kzgCommitment),
versionedHash: toHex(kzgCommitmentToVersionedHash(kzgCommitment)),
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/beacon-node/src/chain/opPools/opPool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {fromHexString, toHexString} from "@chainsafe/ssz";
import {fromHexString} from "@chainsafe/ssz";
import {
CachedBeaconStateAllForks,
computeEpochAtSlot,
Expand All @@ -16,7 +16,7 @@ import {
ForkSeq,
MAX_ATTESTER_SLASHINGS_ELECTRA,
} from "@lodestar/params";
import {toRootHex} from "@lodestar/utils";
import {toHex, toRootHex} from "@lodestar/utils";
import {Epoch, phase0, capella, ssz, ValidatorIndex, SignedBeaconBlock, AttesterSlashing} from "@lodestar/types";
import {IBeaconDb} from "../../db/index.js";
import {SignedBLSToExecutionChangeVersioned} from "../../util/types.js";
Expand Down Expand Up @@ -88,7 +88,7 @@ export class OpPool {
key: fromHexString(key),
value: value.attesterSlashing,
})),
toHexString
toHex
),
persistDiff(
db.proposerSlashing,
Expand Down
9 changes: 5 additions & 4 deletions packages/beacon-node/src/chain/stateCache/datastore/file.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from "node:path";
import {toHexString, fromHexString} from "@chainsafe/ssz";
import {fromHexString} from "@chainsafe/ssz";
import {phase0, ssz} from "@lodestar/types";
import {toHex} from "@lodestar/utils";
import {ensureDir, readFile, readFileNames, removeFile, writeIfNotExist} from "../../../util/file.js";
import {CPStateDatastore, DatastoreKey} from "./types.js";

Expand Down Expand Up @@ -28,18 +29,18 @@ export class FileCPStateDatastore implements CPStateDatastore {

async write(cpKey: phase0.Checkpoint, stateBytes: Uint8Array): Promise<DatastoreKey> {
const serializedCheckpoint = ssz.phase0.Checkpoint.serialize(cpKey);
const filePath = path.join(this.folderPath, toHexString(serializedCheckpoint));
const filePath = path.join(this.folderPath, toHex(serializedCheckpoint));
await writeIfNotExist(filePath, stateBytes);
return serializedCheckpoint;
}

async remove(serializedCheckpoint: DatastoreKey): Promise<void> {
const filePath = path.join(this.folderPath, toHexString(serializedCheckpoint));
const filePath = path.join(this.folderPath, toHex(serializedCheckpoint));
await removeFile(filePath);
}

async read(serializedCheckpoint: DatastoreKey): Promise<Uint8Array | null> {
const filePath = path.join(this.folderPath, toHexString(serializedCheckpoint));
const filePath = path.join(this.folderPath, toHex(serializedCheckpoint));
return readFile(filePath);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {fromHexString, toHexString} from "@chainsafe/ssz";
import {fromHexString} from "@chainsafe/ssz";
import {phase0, Epoch, RootHex} from "@lodestar/types";
import {CachedBeaconStateAllForks, computeStartSlotAtEpoch, getBlockRootAtSlot} from "@lodestar/state-transition";
import {Logger, MapDef, sleep, toRootHex} from "@lodestar/utils";
import {Logger, MapDef, sleep, toHex, toRootHex} from "@lodestar/utils";
import {routes} from "@lodestar/api";
import {loadCachedBeaconState} from "@lodestar/state-transition";
import {INTERVALS_PER_SLOT} from "@lodestar/params";
Expand Down Expand Up @@ -193,7 +193,7 @@ export class PersistentCheckpointStateCache implements CheckpointStateCache {
return stateOrStateBytesData?.clone(opts?.dontTransferCache) ?? null;
}
const {persistedKey, stateBytes} = stateOrStateBytesData;
const logMeta = {persistedKey: toHexString(persistedKey)};
const logMeta = {persistedKey: toHex(persistedKey)};
this.logger.debug("Reload: read state successful", logMeta);
this.metrics?.stateReloadSecFromSlot.observe(this.clock?.secFromSlot(this.clock?.currentSlot ?? 0) ?? 0);
const seedState = this.findSeedStateToReload(cp);
Expand Down Expand Up @@ -341,7 +341,7 @@ export class PersistentCheckpointStateCache implements CheckpointStateCache {
this.logger.verbose("Added checkpoint state to memory but a persisted key existed", {
epoch: cp.epoch,
rootHex: cpHex.rootHex,
persistedKey: toHexString(persistedKey),
persistedKey: toHex(persistedKey),
});
} else {
this.cache.set(key, {type: CacheItemType.inMemory, state});
Expand Down Expand Up @@ -688,7 +688,7 @@ export class PersistentCheckpointStateCache implements CheckpointStateCache {
stateSlot: state.slot,
rootHex,
epochBoundaryHex,
persistedKey: persistedKey ? toHexString(persistedKey) : "",
persistedKey: persistedKey ? toHex(persistedKey) : "",
};

if (persistedRootHexes.has(rootHex)) {
Expand Down Expand Up @@ -716,7 +716,7 @@ export class PersistentCheckpointStateCache implements CheckpointStateCache {
persistCount++;
this.logger.verbose("Pruned checkpoint state from memory and persisted to disk", {
...logMeta,
persistedKey: toHexString(persistedKey),
persistedKey: toHex(persistedKey),
});
}
// overwrite cpKey, this means the state is deleted from memory
Expand Down
5 changes: 2 additions & 3 deletions packages/beacon-node/src/eth1/provider/eth1Provider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {toHexString} from "@chainsafe/ssz";
import {phase0} from "@lodestar/types";
import {ChainConfig} from "@lodestar/config";
import {fromHex, isErrorAborted, createElapsedTimeTracker, toPrintableUrl} from "@lodestar/utils";
import {fromHex, isErrorAborted, createElapsedTimeTracker, toPrintableUrl, toHex} from "@lodestar/utils";
import {Logger} from "@lodestar/logger";

import {FetchError, isFetchError} from "@lodestar/api";
Expand Down Expand Up @@ -72,7 +71,7 @@ export class Eth1Provider implements IEth1Provider {
) {
this.logger = opts.logger;
this.deployBlock = opts.depositContractDeployBlock ?? 0;
this.depositContractAddress = toHexString(config.DEPOSIT_CONTRACT_ADDRESS);
this.depositContractAddress = toHex(config.DEPOSIT_CONTRACT_ADDRESS);

const providerUrls = opts.providerUrls ?? DEFAULT_PROVIDER_URLS;
this.rpc = new JsonRpcHttpClient(providerUrls, {
Expand Down
8 changes: 4 additions & 4 deletions packages/beacon-node/src/eth1/provider/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {fromHexString, toHexString} from "@chainsafe/ssz";
import {fromHexString} from "@chainsafe/ssz";
import {RootHex} from "@lodestar/types";
import {bytesToBigInt, bigIntToBytes} from "@lodestar/utils";
import {bytesToBigInt, bigIntToBytes, toHex} from "@lodestar/utils";
import {ErrorParseJson} from "./jsonRpcHttpClient.js";

/** QUANTITY as defined in ethereum execution layer JSON RPC https://eth.wiki/json-rpc/API */
Expand Down Expand Up @@ -32,7 +32,7 @@ export function bytesToHex(bytes: Uint8Array): string {
return "0x" + bytes[0].toString(16);
}

return toHexString(bytes);
return toHex(bytes);
}

/**
Expand Down Expand Up @@ -100,7 +100,7 @@ export function bytesToQuantity(bytes: Uint8Array): QUANTITY {
* - WRONG: 004200 (must be prefixed 0x)
*/
export function bytesToData(bytes: Uint8Array): DATA {
return toHexString(bytes);
return toHex(bytes);
}

/**
Expand Down
1 change: 0 additions & 1 deletion packages/beacon-node/src/eth1/utils/eth1Vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ function getKeysWithMaxValue<T>(map: Map<T, number>): T[] {
* ✓ pickEth1Vote - max votes 37.89912 ops/s 26.38583 ms/op - 29 runs 1.27 s
*/
function getEth1DataKey(eth1Data: phase0.Eth1Data): string {
// return toHexString(ssz.phase0.Eth1Data.hashTreeRoot(eth1Data));
return fastSerializeEth1Data(eth1Data);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {toHexString} from "@chainsafe/ssz";
import {ForkDigest, Root, Slot, phase0, ssz} from "@lodestar/types";
import {toRootHex} from "@lodestar/utils";
import {toHex, toRootHex} from "@lodestar/utils";

// TODO: Why this value? (From Lighthouse)
const FUTURE_SLOT_TOLERANCE = 1;
Expand Down Expand Up @@ -79,7 +78,7 @@ export function isZeroRoot(root: Root): boolean {
export function renderIrrelevantPeerType(type: IrrelevantPeerType): string {
switch (type.code) {
case IrrelevantPeerCode.INCOMPATIBLE_FORKS:
return `INCOMPATIBLE_FORKS ours: ${toHexString(type.ours)} theirs: ${toHexString(type.theirs)}`;
return `INCOMPATIBLE_FORKS ours: ${toHex(type.ours)} theirs: ${toHex(type.theirs)}`;
case IrrelevantPeerCode.DIFFERENT_CLOCKS:
return `DIFFERENT_CLOCKS slotDiff: ${type.slotDiff}`;
case IrrelevantPeerCode.DIFFERENT_FINALIZED:
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/cmds/validator/keymanager/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import crypto from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import {toHexString} from "@chainsafe/ssz";
import {RestApiServer, RestApiServerOpts, RestApiServerModules} from "@lodestar/beacon-node";
import {KeymanagerApiMethods, registerRoutes} from "@lodestar/api/keymanager/server";
import {ChainForkConfig} from "@lodestar/config";
import {toHex} from "@lodestar/utils";
import {writeFile600Perm} from "../../../util/index.js";

export type KeymanagerRestApiServerOpts = RestApiServerOpts & {
Expand Down Expand Up @@ -50,7 +50,7 @@ export class KeymanagerRestApiServer extends RestApiServer {

if (opts.isAuthEnabled) {
// Generate a new token if token file does not exist or file do exist, but is empty
bearerToken = readFileIfExists(apiTokenPath) ?? `api-token-${toHexString(crypto.randomBytes(32))}`;
bearerToken = readFileIfExists(apiTokenPath) ?? `api-token-${toHex(crypto.randomBytes(32))}`;
writeFile600Perm(apiTokenPath, bearerToken, {encoding: "utf8"});
}

Expand Down
1 change: 1 addition & 0 deletions packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"dependencies": {
"@chainsafe/ssz": "^0.17.1",
"@lodestar/params": "^1.21.0",
"@lodestar/utils": "^1.21.0",
"@lodestar/types": "^1.21.0"
}
}
5 changes: 3 additions & 2 deletions packages/config/src/chainConfig/json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {fromHexString, toHexString} from "@chainsafe/ssz";
import {fromHexString} from "@chainsafe/ssz";
import {toHex} from "@lodestar/utils";
import {ChainConfig, chainConfigTypes, SpecValue, SpecValueTypeName} from "./types.js";

const MAX_UINT64_JSON = "18446744073709551615";
Expand Down Expand Up @@ -69,7 +70,7 @@ export function serializeSpecValue(value: SpecValue, typeName: SpecValueTypeName
if (!(value instanceof Uint8Array)) {
throw Error(`Invalid value ${value.toString()} expected Uint8Array`);
}
return toHexString(value);
return toHex(value);

case "string":
if (typeof value !== "string") {
Expand Down
4 changes: 2 additions & 2 deletions packages/config/src/genesisConfig/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {toHexString} from "@chainsafe/ssz";
import {ForkName, SLOTS_PER_EPOCH, DOMAIN_VOLUNTARY_EXIT} from "@lodestar/params";
import {DomainType, ForkDigest, phase0, Root, Slot, ssz, Version} from "@lodestar/types";
import {toHex} from "@lodestar/utils";
import {ChainForkConfig} from "../beaconConfig.js";
import {ForkDigestHex, CachedGenesis} from "./types.js";
export type {ForkDigestContext} from "./types.js";
Expand Down Expand Up @@ -139,7 +139,7 @@ function computeForkDataRoot(currentVersion: Version, genesisValidatorsRoot: Roo
}

function toHexStringNoPrefix(hex: string | Uint8Array): string {
return strip0xPrefix(typeof hex === "string" ? hex : toHexString(hex));
return strip0xPrefix(typeof hex === "string" ? hex : toHex(hex));
}

function strip0xPrefix(hex: string): string {
Expand Down
Loading

0 comments on commit 9f4bf50

Please sign in to comment.