Skip to content

Commit e02f3f4

Browse files
twoethsphilknows
authored andcommitted
fix: avoid toHexString() (#7075)
* fix: avoid toHexString() * fix: use toRootHex and toPubkeyHex where applicable
1 parent fa67e62 commit e02f3f4

File tree

32 files changed

+107
-108
lines changed

32 files changed

+107
-108
lines changed

packages/api/src/beacon/routes/proof.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable @typescript-eslint/naming-convention */
22
import {CompactMultiProof, ProofType} from "@chainsafe/persistent-merkle-tree";
3-
import {ByteListType, ContainerType, fromHexString, toHexString} from "@chainsafe/ssz";
3+
import {ByteListType, ContainerType, fromHexString} from "@chainsafe/ssz";
4+
import {toHex} from "@lodestar/utils";
45
import {ChainForkConfig} from "@lodestar/config";
56
import {ssz} from "@lodestar/types";
67
import {Endpoint, RouteDefinitions, Schema} from "../../utils/index.js";
@@ -45,7 +46,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
4546
url: "/eth/v0/beacon/proof/state/{state_id}",
4647
method: "GET",
4748
req: {
48-
writeReq: ({stateId, descriptor}) => ({params: {state_id: stateId}, query: {format: toHexString(descriptor)}}),
49+
writeReq: ({stateId, descriptor}) => ({params: {state_id: stateId}, query: {format: toHex(descriptor)}}),
4950
parseReq: ({params, query}) => ({stateId: params.state_id, descriptor: fromHexString(query.format)}),
5051
schema: {params: {state_id: Schema.StringRequired}, query: {format: Schema.StringRequired}},
5152
},
@@ -63,7 +64,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
6364
url: "/eth/v0/beacon/proof/block/{block_id}",
6465
method: "GET",
6566
req: {
66-
writeReq: ({blockId, descriptor}) => ({params: {block_id: blockId}, query: {format: toHexString(descriptor)}}),
67+
writeReq: ({blockId, descriptor}) => ({params: {block_id: blockId}, query: {format: toHex(descriptor)}}),
6768
parseReq: ({params, query}) => ({blockId: params.block_id, descriptor: fromHexString(query.format)}),
6869
schema: {params: {block_id: Schema.StringRequired}, query: {format: Schema.StringRequired}},
6970
},

packages/api/src/beacon/routes/validator.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/naming-convention */
2-
import {ContainerType, fromHexString, toHexString, Type, ValueOf} from "@chainsafe/ssz";
2+
import {ContainerType, fromHexString, Type, ValueOf} from "@chainsafe/ssz";
33
import {ChainForkConfig} from "@lodestar/config";
44
import {isForkBlobs, isForkPostElectra} from "@lodestar/params";
55
import {
@@ -20,6 +20,7 @@ import {
2020
Attestation,
2121
sszTypesFor,
2222
} from "@lodestar/types";
23+
import {toHex, toRootHex} from "@lodestar/utils";
2324
import {Endpoint, RouteDefinitions, Schema} from "../../utils/index.js";
2425
import {fromGraffitiHex, toBoolean, toGraffitiHex} from "../../utils/serdes.js";
2526
import {getExecutionForkTypes, toForkName} from "../../utils/fork.js";
@@ -623,7 +624,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
623624
writeReq: ({slot, randaoReveal, graffiti, feeRecipient, builderSelection, strictFeeRecipientCheck}) => ({
624625
params: {slot},
625626
query: {
626-
randao_reveal: toHexString(randaoReveal),
627+
randao_reveal: toHex(randaoReveal),
627628
graffiti: toGraffitiHex(graffiti),
628629
fee_recipient: feeRecipient,
629630
builder_selection: builderSelection,
@@ -674,7 +675,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
674675
}) => ({
675676
params: {slot},
676677
query: {
677-
randao_reveal: toHexString(randaoReveal),
678+
randao_reveal: toHex(randaoReveal),
678679
graffiti: toGraffitiHex(graffiti),
679680
skip_randao_verification: writeSkipRandaoVerification(skipRandaoVerification),
680681
fee_recipient: feeRecipient,
@@ -765,7 +766,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
765766
req: {
766767
writeReq: ({slot, randaoReveal, graffiti}) => ({
767768
params: {slot},
768-
query: {randao_reveal: toHexString(randaoReveal), graffiti: toGraffitiHex(graffiti)},
769+
query: {randao_reveal: toHex(randaoReveal), graffiti: toGraffitiHex(graffiti)},
769770
}),
770771
parseReq: ({params, query}) => ({
771772
slot: params.slot,
@@ -805,7 +806,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
805806
method: "GET",
806807
req: {
807808
writeReq: ({slot, subcommitteeIndex, beaconBlockRoot}) => ({
808-
query: {slot, subcommittee_index: subcommitteeIndex, beacon_block_root: toHexString(beaconBlockRoot)},
809+
query: {slot, subcommittee_index: subcommitteeIndex, beacon_block_root: toRootHex(beaconBlockRoot)},
809810
}),
810811
parseReq: ({query}) => ({
811812
slot: query.slot,
@@ -830,7 +831,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
830831
method: "GET",
831832
req: {
832833
writeReq: ({attestationDataRoot, slot}) => ({
833-
query: {attestation_data_root: toHexString(attestationDataRoot), slot},
834+
query: {attestation_data_root: toRootHex(attestationDataRoot), slot},
834835
}),
835836
parseReq: ({query}) => ({
836837
attestationDataRoot: fromHexString(query.attestation_data_root),
@@ -853,7 +854,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
853854
method: "GET",
854855
req: {
855856
writeReq: ({attestationDataRoot, slot, committeeIndex}) => ({
856-
query: {attestation_data_root: toHexString(attestationDataRoot), slot, committee_index: committeeIndex},
857+
query: {attestation_data_root: toHex(attestationDataRoot), slot, committee_index: committeeIndex},
857858
}),
858859
parseReq: ({query}) => ({
859860
attestationDataRoot: fromHexString(query.attestation_data_root),

packages/api/src/builder/routes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/naming-convention */
2-
import {fromHexString, toHexString} from "@chainsafe/ssz";
2+
import {fromHexString} from "@chainsafe/ssz";
33
import {
44
ssz,
55
bellatrix,
@@ -13,7 +13,7 @@ import {
1313
} from "@lodestar/types";
1414
import {ForkName, isForkBlobs} from "@lodestar/params";
1515
import {ChainForkConfig} from "@lodestar/config";
16-
import {toPubkeyHex} from "@lodestar/utils";
16+
import {toPubkeyHex, toRootHex} from "@lodestar/utils";
1717

1818
import {Endpoint, RouteDefinitions, Schema} from "../utils/index.js";
1919
import {MetaHeader, VersionCodec, VersionMeta} from "../utils/metadata.js";
@@ -106,7 +106,7 @@ export function getDefinitions(config: ChainForkConfig): RouteDefinitions<Endpoi
106106
method: "GET",
107107
req: {
108108
writeReq: ({slot, parentHash, proposerPubkey: proposerPubKey}) => ({
109-
params: {slot, parent_hash: toHexString(parentHash), pubkey: toPubkeyHex(proposerPubKey)},
109+
params: {slot, parent_hash: toRootHex(parentHash), pubkey: toPubkeyHex(proposerPubKey)},
110110
}),
111111
parseReq: ({params}) => ({
112112
slot: params.slot,

packages/api/src/utils/serdes.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {fromHexString, JsonPath, toHexString} from "@chainsafe/ssz";
1+
import {fromHexString, JsonPath} from "@chainsafe/ssz";
2+
import {toHex} from "@lodestar/utils";
23

34
/**
45
* Serialize proof path to JSON.
@@ -82,7 +83,7 @@ export function toGraffitiHex(utf8?: string): string | undefined {
8283
return undefined;
8384
}
8485

85-
const hex = toHexString(new TextEncoder().encode(utf8));
86+
const hex = toHex(new TextEncoder().encode(utf8));
8687

8788
if (hex.length > GRAFFITI_HEX_LENGTH) {
8889
// remove characters from the end if hex string is too long

packages/beacon-node/src/chain/blocks/importBlock.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {toHexString} from "@chainsafe/ssz";
21
import {capella, ssz, altair, BeaconBlock} from "@lodestar/types";
32
import {ForkLightClient, ForkSeq, INTERVALS_PER_SLOT, MAX_SEED_LOOKAHEAD, SLOTS_PER_EPOCH} from "@lodestar/params";
43
import {
@@ -10,7 +9,7 @@ import {
109
} from "@lodestar/state-transition";
1110
import {routes} from "@lodestar/api";
1211
import {ForkChoiceError, ForkChoiceErrorCode, EpochDifference, AncestorStatus} from "@lodestar/fork-choice";
13-
import {isErrorAborted, toRootHex} from "@lodestar/utils";
12+
import {isErrorAborted, toHex, toRootHex} from "@lodestar/utils";
1413
import {ZERO_HASH_HEX} from "../../constants/index.js";
1514
import {toCheckpointHex} from "../stateCache/index.js";
1615
import {isOptimisticBlock} from "../../util/forkChoice.js";
@@ -433,8 +432,8 @@ export async function importBlock(
433432
blockRoot: blockRootHex,
434433
slot: blockSlot,
435434
index,
436-
kzgCommitment: toHexString(kzgCommitment),
437-
versionedHash: toHexString(kzgCommitmentToVersionedHash(kzgCommitment)),
435+
kzgCommitment: toHex(kzgCommitment),
436+
versionedHash: toHex(kzgCommitmentToVersionedHash(kzgCommitment)),
438437
});
439438
}
440439
}

packages/beacon-node/src/chain/opPools/opPool.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {fromHexString, toHexString} from "@chainsafe/ssz";
1+
import {fromHexString} from "@chainsafe/ssz";
22
import {
33
CachedBeaconStateAllForks,
44
computeEpochAtSlot,
@@ -16,7 +16,7 @@ import {
1616
ForkSeq,
1717
MAX_ATTESTER_SLASHINGS_ELECTRA,
1818
} from "@lodestar/params";
19-
import {toRootHex} from "@lodestar/utils";
19+
import {toHex, toRootHex} from "@lodestar/utils";
2020
import {Epoch, phase0, capella, ssz, ValidatorIndex, SignedBeaconBlock, AttesterSlashing} from "@lodestar/types";
2121
import {IBeaconDb} from "../../db/index.js";
2222
import {SignedBLSToExecutionChangeVersioned} from "../../util/types.js";
@@ -88,7 +88,7 @@ export class OpPool {
8888
key: fromHexString(key),
8989
value: value.attesterSlashing,
9090
})),
91-
toHexString
91+
toHex
9292
),
9393
persistDiff(
9494
db.proposerSlashing,

packages/beacon-node/src/chain/stateCache/datastore/file.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "node:path";
2-
import {toHexString, fromHexString} from "@chainsafe/ssz";
2+
import {fromHexString} from "@chainsafe/ssz";
33
import {phase0, ssz} from "@lodestar/types";
4+
import {toHex} from "@lodestar/utils";
45
import {ensureDir, readFile, readFileNames, removeFile, writeIfNotExist} from "../../../util/file.js";
56
import {CPStateDatastore, DatastoreKey} from "./types.js";
67

@@ -28,18 +29,18 @@ export class FileCPStateDatastore implements CPStateDatastore {
2829

2930
async write(cpKey: phase0.Checkpoint, stateBytes: Uint8Array): Promise<DatastoreKey> {
3031
const serializedCheckpoint = ssz.phase0.Checkpoint.serialize(cpKey);
31-
const filePath = path.join(this.folderPath, toHexString(serializedCheckpoint));
32+
const filePath = path.join(this.folderPath, toHex(serializedCheckpoint));
3233
await writeIfNotExist(filePath, stateBytes);
3334
return serializedCheckpoint;
3435
}
3536

3637
async remove(serializedCheckpoint: DatastoreKey): Promise<void> {
37-
const filePath = path.join(this.folderPath, toHexString(serializedCheckpoint));
38+
const filePath = path.join(this.folderPath, toHex(serializedCheckpoint));
3839
await removeFile(filePath);
3940
}
4041

4142
async read(serializedCheckpoint: DatastoreKey): Promise<Uint8Array | null> {
42-
const filePath = path.join(this.folderPath, toHexString(serializedCheckpoint));
43+
const filePath = path.join(this.folderPath, toHex(serializedCheckpoint));
4344
return readFile(filePath);
4445
}
4546

packages/beacon-node/src/chain/stateCache/persistentCheckpointsCache.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {fromHexString, toHexString} from "@chainsafe/ssz";
1+
import {fromHexString} from "@chainsafe/ssz";
22
import {phase0, Epoch, RootHex} from "@lodestar/types";
33
import {CachedBeaconStateAllForks, computeStartSlotAtEpoch, getBlockRootAtSlot} from "@lodestar/state-transition";
4-
import {Logger, MapDef, sleep, toRootHex} from "@lodestar/utils";
4+
import {Logger, MapDef, sleep, toHex, toRootHex} from "@lodestar/utils";
55
import {routes} from "@lodestar/api";
66
import {loadCachedBeaconState} from "@lodestar/state-transition";
77
import {INTERVALS_PER_SLOT} from "@lodestar/params";
@@ -193,7 +193,7 @@ export class PersistentCheckpointStateCache implements CheckpointStateCache {
193193
return stateOrStateBytesData?.clone(opts?.dontTransferCache) ?? null;
194194
}
195195
const {persistedKey, stateBytes} = stateOrStateBytesData;
196-
const logMeta = {persistedKey: toHexString(persistedKey)};
196+
const logMeta = {persistedKey: toHex(persistedKey)};
197197
this.logger.debug("Reload: read state successful", logMeta);
198198
this.metrics?.stateReloadSecFromSlot.observe(this.clock?.secFromSlot(this.clock?.currentSlot ?? 0) ?? 0);
199199
const seedState = this.findSeedStateToReload(cp);
@@ -341,7 +341,7 @@ export class PersistentCheckpointStateCache implements CheckpointStateCache {
341341
this.logger.verbose("Added checkpoint state to memory but a persisted key existed", {
342342
epoch: cp.epoch,
343343
rootHex: cpHex.rootHex,
344-
persistedKey: toHexString(persistedKey),
344+
persistedKey: toHex(persistedKey),
345345
});
346346
} else {
347347
this.cache.set(key, {type: CacheItemType.inMemory, state});
@@ -688,7 +688,7 @@ export class PersistentCheckpointStateCache implements CheckpointStateCache {
688688
stateSlot: state.slot,
689689
rootHex,
690690
epochBoundaryHex,
691-
persistedKey: persistedKey ? toHexString(persistedKey) : "",
691+
persistedKey: persistedKey ? toHex(persistedKey) : "",
692692
};
693693

694694
if (persistedRootHexes.has(rootHex)) {
@@ -716,7 +716,7 @@ export class PersistentCheckpointStateCache implements CheckpointStateCache {
716716
persistCount++;
717717
this.logger.verbose("Pruned checkpoint state from memory and persisted to disk", {
718718
...logMeta,
719-
persistedKey: toHexString(persistedKey),
719+
persistedKey: toHex(persistedKey),
720720
});
721721
}
722722
// overwrite cpKey, this means the state is deleted from memory

packages/beacon-node/src/eth1/provider/eth1Provider.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import {toHexString} from "@chainsafe/ssz";
21
import {phase0} from "@lodestar/types";
32
import {ChainConfig} from "@lodestar/config";
4-
import {fromHex, isErrorAborted, createElapsedTimeTracker, toPrintableUrl} from "@lodestar/utils";
3+
import {fromHex, isErrorAborted, createElapsedTimeTracker, toPrintableUrl, toHex} from "@lodestar/utils";
54
import {Logger} from "@lodestar/logger";
65

76
import {FetchError, isFetchError} from "@lodestar/api";
@@ -72,7 +71,7 @@ export class Eth1Provider implements IEth1Provider {
7271
) {
7372
this.logger = opts.logger;
7473
this.deployBlock = opts.depositContractDeployBlock ?? 0;
75-
this.depositContractAddress = toHexString(config.DEPOSIT_CONTRACT_ADDRESS);
74+
this.depositContractAddress = toHex(config.DEPOSIT_CONTRACT_ADDRESS);
7675

7776
const providerUrls = opts.providerUrls ?? DEFAULT_PROVIDER_URLS;
7877
this.rpc = new JsonRpcHttpClient(providerUrls, {

packages/beacon-node/src/eth1/provider/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {fromHexString, toHexString} from "@chainsafe/ssz";
1+
import {fromHexString} from "@chainsafe/ssz";
22
import {RootHex} from "@lodestar/types";
3-
import {bytesToBigInt, bigIntToBytes} from "@lodestar/utils";
3+
import {bytesToBigInt, bigIntToBytes, toHex} from "@lodestar/utils";
44
import {ErrorParseJson} from "./jsonRpcHttpClient.js";
55

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

35-
return toHexString(bytes);
35+
return toHex(bytes);
3636
}
3737

3838
/**
@@ -100,7 +100,7 @@ export function bytesToQuantity(bytes: Uint8Array): QUANTITY {
100100
* - WRONG: 004200 (must be prefixed 0x)
101101
*/
102102
export function bytesToData(bytes: Uint8Array): DATA {
103-
return toHexString(bytes);
103+
return toHex(bytes);
104104
}
105105

106106
/**

0 commit comments

Comments
 (0)