Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: avoid fromHexString #7080

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/api/src/beacon/routes/proof.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {CompactMultiProof, ProofType} from "@chainsafe/persistent-merkle-tree";
import {ByteListType, ContainerType, fromHexString} from "@chainsafe/ssz";
import {toHex} from "@lodestar/utils";
import {ByteListType, ContainerType} from "@chainsafe/ssz";
import {fromHex, 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 @@ -47,7 +47,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
method: "GET",
req: {
writeReq: ({stateId, descriptor}) => ({params: {state_id: stateId}, query: {format: toHex(descriptor)}}),
parseReq: ({params, query}) => ({stateId: params.state_id, descriptor: fromHexString(query.format)}),
parseReq: ({params, query}) => ({stateId: params.state_id, descriptor: fromHex(query.format)}),
schema: {params: {state_id: Schema.StringRequired}, query: {format: Schema.StringRequired}},
},
resp: {
Expand All @@ -65,7 +65,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
method: "GET",
req: {
writeReq: ({blockId, descriptor}) => ({params: {block_id: blockId}, query: {format: toHex(descriptor)}}),
parseReq: ({params, query}) => ({blockId: params.block_id, descriptor: fromHexString(query.format)}),
parseReq: ({params, query}) => ({blockId: params.block_id, descriptor: fromHex(query.format)}),
schema: {params: {block_id: Schema.StringRequired}, query: {format: Schema.StringRequired}},
},
resp: {
Expand Down
16 changes: 8 additions & 8 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, Type, ValueOf} from "@chainsafe/ssz";
import {ContainerType, Type, ValueOf} from "@chainsafe/ssz";
import {ChainForkConfig} from "@lodestar/config";
import {isForkBlobs, isForkPostElectra} from "@lodestar/params";
import {
Expand All @@ -20,7 +20,7 @@ import {
Attestation,
sszTypesFor,
} from "@lodestar/types";
import {toHex, toRootHex} from "@lodestar/utils";
import {fromHex, 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 @@ -633,7 +633,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
}),
parseReq: ({params, query}) => ({
slot: params.slot,
randaoReveal: fromHexString(query.randao_reveal),
randaoReveal: fromHex(query.randao_reveal),
graffiti: fromGraffitiHex(query.graffiti),
feeRecipient: query.fee_recipient,
builderSelection: query.builder_selection as BuilderSelection,
Expand Down Expand Up @@ -687,7 +687,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
}),
parseReq: ({params, query}) => ({
slot: params.slot,
randaoReveal: fromHexString(query.randao_reveal),
randaoReveal: fromHex(query.randao_reveal),
graffiti: fromGraffitiHex(query.graffiti),
skipRandaoVerification: parseSkipRandaoVerification(query.skip_randao_verification),
feeRecipient: query.fee_recipient,
Expand Down Expand Up @@ -770,7 +770,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
}),
parseReq: ({params, query}) => ({
slot: params.slot,
randaoReveal: fromHexString(query.randao_reveal),
randaoReveal: fromHex(query.randao_reveal),
graffiti: fromGraffitiHex(query.graffiti),
}),
schema: {
Expand Down Expand Up @@ -811,7 +811,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
parseReq: ({query}) => ({
slot: query.slot,
subcommitteeIndex: query.subcommittee_index,
beaconBlockRoot: fromHexString(query.beacon_block_root),
beaconBlockRoot: fromHex(query.beacon_block_root),
}),
schema: {
query: {
Expand All @@ -834,7 +834,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
query: {attestation_data_root: toRootHex(attestationDataRoot), slot},
}),
parseReq: ({query}) => ({
attestationDataRoot: fromHexString(query.attestation_data_root),
attestationDataRoot: fromHex(query.attestation_data_root),
slot: query.slot,
}),
schema: {
Expand All @@ -857,7 +857,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
query: {attestation_data_root: toHex(attestationDataRoot), slot, committee_index: committeeIndex},
}),
parseReq: ({query}) => ({
attestationDataRoot: fromHexString(query.attestation_data_root),
attestationDataRoot: fromHex(query.attestation_data_root),
slot: query.slot,
committeeIndex: query.committee_index,
}),
Expand Down
7 changes: 3 additions & 4 deletions packages/api/src/builder/routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {fromHexString} from "@chainsafe/ssz";
import {
ssz,
bellatrix,
Expand All @@ -13,7 +12,7 @@ import {
} from "@lodestar/types";
import {ForkName, isForkBlobs} from "@lodestar/params";
import {ChainForkConfig} from "@lodestar/config";
import {toPubkeyHex, toRootHex} from "@lodestar/utils";
import {fromHex, 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 @@ -110,8 +109,8 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
}),
parseReq: ({params}) => ({
slot: params.slot,
parentHash: fromHexString(params.parent_hash),
proposerPubkey: fromHexString(params.pubkey),
parentHash: fromHex(params.parent_hash),
proposerPubkey: fromHex(params.pubkey),
}),
schema: {
params: {slot: Schema.UintRequired, parent_hash: Schema.StringRequired, pubkey: Schema.StringRequired},
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/utils/serdes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {fromHexString, JsonPath} from "@chainsafe/ssz";
import {toHex} from "@lodestar/utils";
import {JsonPath} from "@chainsafe/ssz";
import {fromHex, toHex} from "@lodestar/utils";

/**
* Serialize proof path to JSON.
Expand Down Expand Up @@ -103,7 +103,7 @@ export function fromGraffitiHex(hex?: string): string | undefined {
return undefined;
}
try {
return new TextDecoder("utf8").decode(fromHexString(hex));
return new TextDecoder("utf8").decode(fromHex(hex));
} catch {
// allow malformed graffiti hex string
return hex;
Expand Down
9 changes: 4 additions & 5 deletions packages/beacon-node/src/chain/archiver/archiveBlocks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {fromHexString} from "@chainsafe/ssz";
import {Epoch, Slot, RootHex} from "@lodestar/types";
import {IForkChoice} from "@lodestar/fork-choice";
import {Logger, toRootHex} from "@lodestar/utils";
import {Logger, fromHex, toRootHex} from "@lodestar/utils";
import {ForkSeq, SLOTS_PER_EPOCH} from "@lodestar/params";
import {computeEpochAtSlot, computeStartSlotAtEpoch} from "@lodestar/state-transition";
import {KeyValue} from "@lodestar/db";
Expand Down Expand Up @@ -48,7 +47,7 @@ export async function archiveBlocks(

const finalizedCanonicalBlockRoots: BlockRootSlot[] = finalizedCanonicalBlocks.map((block) => ({
slot: block.slot,
root: fromHexString(block.blockRoot),
root: fromHex(block.blockRoot),
}));

if (finalizedCanonicalBlockRoots.length > 0) {
Expand All @@ -68,7 +67,7 @@ export async function archiveBlocks(
// deleteNonCanonicalBlocks
// loop through forkchoice single time

const nonCanonicalBlockRoots = finalizedNonCanonicalBlocks.map((summary) => fromHexString(summary.blockRoot));
const nonCanonicalBlockRoots = finalizedNonCanonicalBlocks.map((summary) => fromHex(summary.blockRoot));
if (nonCanonicalBlockRoots.length > 0) {
await db.block.batchDelete(nonCanonicalBlockRoots);
logger.verbose("Deleted non canonical blocks from hot DB", {
Expand Down Expand Up @@ -144,7 +143,7 @@ async function migrateBlocksFromHotToColdDb(db: IBeaconDb, blocks: BlockRootSlot
value: blockBuffer,
slot: block.slot,
blockRoot: block.root,
// TODO: Benchmark if faster to slice Buffer or fromHexString()
// TODO: Benchmark if faster to slice Buffer or fromHex()
parentRoot: getParentRootFromSignedBlock(blockBuffer),
};
})
Expand Down
16 changes: 8 additions & 8 deletions packages/beacon-node/src/chain/chain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "node:path";
import {CompositeTypeAny, fromHexString, TreeView, Type} from "@chainsafe/ssz";
import {CompositeTypeAny, TreeView, Type} from "@chainsafe/ssz";
import {
BeaconStateAllForks,
CachedBeaconStateAllForks,
Expand Down Expand Up @@ -35,7 +35,7 @@ import {
} from "@lodestar/types";
import {CheckpointWithHex, ExecutionStatus, IForkChoice, ProtoBlock, UpdateHeadOpt} from "@lodestar/fork-choice";
import {ProcessShutdownCallback} from "@lodestar/validator";
import {Logger, gweiToWei, isErrorAborted, pruneSetToMax, sleep, toRootHex} from "@lodestar/utils";
import {Logger, fromHex, gweiToWei, isErrorAborted, pruneSetToMax, sleep, toRootHex} from "@lodestar/utils";
import {ForkSeq, GENESIS_SLOT, SLOTS_PER_EPOCH} from "@lodestar/params";

import {GENESIS_EPOCH, ZERO_HASH} from "../constants/index.js";
Expand Down Expand Up @@ -521,7 +521,7 @@ export class BeaconChain implements IBeaconChain {
};
}

const data = await this.db.stateArchive.getByRoot(fromHexString(stateRoot));
const data = await this.db.stateArchive.getByRoot(fromHex(stateRoot));
return data && {state: data, executionOptimistic: false, finalized: true};
}

Expand Down Expand Up @@ -568,7 +568,7 @@ export class BeaconChain implements IBeaconChain {
// Unfinalized slot, attempt to find in fork-choice
const block = this.forkChoice.getCanonicalBlockAtSlot(slot);
if (block) {
const data = await this.db.block.get(fromHexString(block.blockRoot));
const data = await this.db.block.get(fromHex(block.blockRoot));
if (data) {
return {block: data, executionOptimistic: isOptimisticBlock(block), finalized: false};
}
Expand All @@ -587,15 +587,15 @@ export class BeaconChain implements IBeaconChain {
): Promise<{block: SignedBeaconBlock; executionOptimistic: boolean; finalized: boolean} | null> {
const block = this.forkChoice.getBlockHex(root);
if (block) {
const data = await this.db.block.get(fromHexString(root));
const data = await this.db.block.get(fromHex(root));
if (data) {
return {block: data, executionOptimistic: isOptimisticBlock(block), finalized: false};
}
// If block is not found in hot db, try cold db since there could be an archive cycle happening
// TODO: Add a lock to the archiver to have deterministic behavior on where are blocks
}

const data = await this.db.blockArchive.getByRoot(fromHexString(root));
const data = await this.db.blockArchive.getByRoot(fromHex(root));
return data && {block: data, executionOptimistic: false, finalized: true};
}

Expand Down Expand Up @@ -768,7 +768,7 @@ export class BeaconChain implements IBeaconChain {
finalizedRoot: finalizedCheckpoint.epoch === GENESIS_EPOCH ? ZERO_HASH : finalizedCheckpoint.root,
finalizedEpoch: finalizedCheckpoint.epoch,
// TODO: PERFORMANCE: Memoize to prevent re-computing every time
headRoot: fromHexString(head.blockRoot),
headRoot: fromHex(head.blockRoot),
headSlot: head.slot,
};
}
Expand Down Expand Up @@ -1120,7 +1120,7 @@ export class BeaconChain implements IBeaconChain {
// TODO: Improve using regen here
const {blockRoot, stateRoot, slot} = this.forkChoice.getHead();
const headState = this.regen.getStateSync(stateRoot);
const headBlock = await this.db.block.get(fromHexString(blockRoot));
const headBlock = await this.db.block.get(fromHex(blockRoot));
if (headBlock == null) {
throw Error(`Head block ${slot} ${headBlock} is not available in database`);
}
Expand Down
5 changes: 2 additions & 3 deletions packages/beacon-node/src/chain/opPools/opPool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {fromHexString} from "@chainsafe/ssz";
import {
CachedBeaconStateAllForks,
computeEpochAtSlot,
Expand All @@ -16,7 +15,7 @@ import {
ForkSeq,
MAX_ATTESTER_SLASHINGS_ELECTRA,
} from "@lodestar/params";
import {toHex, toRootHex} from "@lodestar/utils";
import {fromHex, 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 @@ -85,7 +84,7 @@ export class OpPool {
persistDiff(
db.attesterSlashing,
Array.from(this.attesterSlashings.entries()).map(([key, value]) => ({
key: fromHexString(key),
key: fromHex(key),
value: value.attesterSlashing,
})),
toHex
Expand Down
5 changes: 2 additions & 3 deletions packages/beacon-node/src/chain/regen/regen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {fromHexString} from "@chainsafe/ssz";
import {phase0, Slot, RootHex, BeaconBlock, SignedBeaconBlock} from "@lodestar/types";
import {
CachedBeaconStateAllForks,
Expand All @@ -11,7 +10,7 @@ import {
StateHashTreeRootSource,
} from "@lodestar/state-transition";
import {IForkChoice, ProtoBlock} from "@lodestar/fork-choice";
import {Logger, toRootHex} from "@lodestar/utils";
import {Logger, fromHex, toRootHex} from "@lodestar/utils";
import {SLOTS_PER_EPOCH} from "@lodestar/params";
import {ChainForkConfig} from "@lodestar/config";
import {Metrics} from "../../metrics/index.js";
Expand Down Expand Up @@ -216,7 +215,7 @@ export class StateRegenerator implements IStateRegeneratorInternal {
const protoBlocksAsc = blocksToReplay.reverse();
for (const [i, protoBlock] of protoBlocksAsc.entries()) {
replaySlots[i] = protoBlock.slot;
blockPromises[i] = this.modules.db.block.get(fromHexString(protoBlock.blockRoot));
blockPromises[i] = this.modules.db.block.get(fromHex(protoBlock.blockRoot));
}

const logCtx = {stateRoot, replaySlots: replaySlots.join(",")};
Expand Down
5 changes: 2 additions & 3 deletions packages/beacon-node/src/chain/stateCache/datastore/file.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from "node:path";
import {fromHexString} from "@chainsafe/ssz";
import {phase0, ssz} from "@lodestar/types";
import {toHex} from "@lodestar/utils";
import {fromHex, toHex} from "@lodestar/utils";
import {ensureDir, readFile, readFileNames, removeFile, writeIfNotExist} from "../../../util/file.js";
import {CPStateDatastore, DatastoreKey} from "./types.js";

Expand Down Expand Up @@ -48,6 +47,6 @@ export class FileCPStateDatastore implements CPStateDatastore {
const fileNames = await readFileNames(this.folderPath);
return fileNames
.filter((fileName) => fileName.startsWith("0x") && fileName.length === CHECKPOINT_FILE_NAME_LENGTH)
.map((fileName) => fromHexString(fileName));
.map((fileName) => fromHex(fileName));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {fromHexString} from "@chainsafe/ssz";
import {phase0, Epoch, RootHex} from "@lodestar/types";
import {CachedBeaconStateAllForks, computeStartSlotAtEpoch, getBlockRootAtSlot} from "@lodestar/state-transition";
import {Logger, MapDef, sleep, toHex, toRootHex} from "@lodestar/utils";
import {Logger, MapDef, fromHex, 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 @@ -657,7 +656,7 @@ export class PersistentCheckpointStateCache implements CheckpointStateCache {
let persistCount = 0;
const epochBoundarySlot = computeStartSlotAtEpoch(epoch);
const epochBoundaryRoot =
epochBoundarySlot === state.slot ? fromHexString(blockRootHex) : getBlockRootAtSlot(state, epochBoundarySlot);
epochBoundarySlot === state.slot ? fromHex(blockRootHex) : getBlockRootAtSlot(state, epochBoundarySlot);
const epochBoundaryHex = toRootHex(epochBoundaryRoot);
const prevEpochRoot = toRootHex(getBlockRootAtSlot(state, epochBoundarySlot - 1));

Expand Down Expand Up @@ -698,7 +697,7 @@ export class PersistentCheckpointStateCache implements CheckpointStateCache {
} else {
// persist and do not update epochIndex
this.metrics?.statePersistSecFromSlot.observe(this.clock?.secFromSlot(this.clock?.currentSlot ?? 0) ?? 0);
const cpPersist = {epoch: epoch, root: fromHexString(rootHex)};
const cpPersist = {epoch: epoch, root: fromHex(rootHex)};
// It's not sustainable to allocate ~240MB for each state every epoch, so we use buffer pool to reuse the memory.
// As monitored on holesky as of Jan 2024:
// - This does not increase heap allocation while gc time is the same
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/src/eth1/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {fromHexString} from "@chainsafe/ssz";
import {CachedBeaconStateAllForks} from "@lodestar/state-transition";
import {Root} from "@lodestar/types";
import {fromHex} from "@lodestar/utils";
import {IEth1ForBlockProduction, Eth1DataAndDeposits, IEth1Provider, PowMergeBlock, TDProgress} from "./interface.js";
import {Eth1DepositDataTracker, Eth1DepositDataTrackerModules} from "./eth1DepositDataTracker.js";
import {Eth1MergeBlockTracker, Eth1MergeBlockTrackerModules} from "./eth1MergeBlockTracker.js";
Expand Down Expand Up @@ -92,7 +92,7 @@ export class Eth1ForBlockProduction implements IEth1ForBlockProduction {

async getTerminalPowBlock(): Promise<Root | null> {
const block = await this.eth1MergeBlockTracker.getTerminalPowBlock();
return block && fromHexString(block.blockHash);
return block && fromHex(block.blockHash);
}

getPowBlock(powBlockHash: string): Promise<PowMergeBlock | null> {
Expand Down
5 changes: 2 additions & 3 deletions packages/beacon-node/src/eth1/provider/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {fromHexString} from "@chainsafe/ssz";
import {RootHex} from "@lodestar/types";
import {bytesToBigInt, bigIntToBytes, toHex} from "@lodestar/utils";
import {bytesToBigInt, bigIntToBytes, toHex, fromHex} 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 @@ -108,7 +107,7 @@ export function bytesToData(bytes: Uint8Array): DATA {
*/
export function dataToBytes(hex: DATA, fixedLength: number | null): Uint8Array {
try {
const bytes = fromHexString(hex);
const bytes = fromHex(hex);
if (fixedLength != null && bytes.length !== fixedLength) {
throw Error(`Wrong data length ${bytes.length} expected ${fixedLength}`);
}
Expand Down
10 changes: 5 additions & 5 deletions packages/beacon-node/src/eth1/utils/depositContract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Interface} from "@ethersproject/abi";
import {fromHexString} from "@chainsafe/ssz";
import {phase0, ssz} from "@lodestar/types";
import {fromHex} from "@lodestar/utils";

const depositEventFragment =
"event DepositEvent(bytes pubkey, bytes withdrawal_credentials, bytes amount, bytes signature, bytes index)";
Expand All @@ -23,15 +23,15 @@ export function parseDepositLog(log: {blockNumber: number; data: string; topics:
blockNumber: log.blockNumber,
index: parseHexNumLittleEndian(values.index),
depositData: {
pubkey: fromHexString(values.pubkey),
withdrawalCredentials: fromHexString(values.withdrawal_credentials),
pubkey: fromHex(values.pubkey),
withdrawalCredentials: fromHex(values.withdrawal_credentials),
amount: parseHexNumLittleEndian(values.amount),
signature: fromHexString(values.signature),
signature: fromHex(values.signature),
},
};
}

function parseHexNumLittleEndian(hex: string): number {
// Can't use parseInt() because amount is a hex string in little endian
return ssz.UintNum64.deserialize(fromHexString(hex));
return ssz.UintNum64.deserialize(fromHex(hex));
}
Loading
Loading