Skip to content

Commit cd7b5c3

Browse files
committed
type as hex string
1 parent 4a10e6a commit cd7b5c3

7 files changed

Lines changed: 173 additions & 47 deletions

File tree

packages/cli/src/rpc/modules/beacon.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hexToBytes } from '@ethereumjs/util'
1+
import { hexToBytes, PrefixedHexString } from '@ethereumjs/util'
22
import { RunStatusCode } from '@lodestar/light-client'
33
import { ssz } from '@lodestar/types'
44
import {
@@ -90,12 +90,12 @@ export class beacon {
9090
const period = Number(BigInt(params[0]))
9191
const rangeKey = getBeaconContentKey(
9292
BeaconNetworkContentType.LightClientUpdate,
93-
hexToBytes(computeLightClientKeyFromPeriod(period)),
93+
hexToBytes(computeLightClientKeyFromPeriod(period) as PrefixedHexString),
9494
)
9595
const update = await this._beacon.retrieve(rangeKey)
9696
if (update !== undefined) {
9797
return ssz.capella.LightClientUpdate.toJson(
98-
ssz.capella.LightClientUpdate.deserialize(hexToBytes(update)),
98+
ssz.capella.LightClientUpdate.deserialize(hexToBytes(update as PrefixedHexString)),
9999
)
100100
}
101101
const lookup = new ContentLookup(

packages/cli/src/rpc/modules/eth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class eth {
9191
async getBalance(params: [string, string]) {
9292
const [address, blockTag] = params
9393
try {
94-
const res = await this._client.ETH.getBalance(hexToBytes(address), BigInt(blockTag))
94+
const res = await this._client.ETH.getBalance(hexToBytes(address as PrefixedHexString), BigInt(blockTag))
9595
if (res === undefined) {
9696
throw {
9797
code: INTERNAL_ERROR,
@@ -141,7 +141,7 @@ export class eth {
141141
this._client.logger(
142142
`eth_getBlockByHash request received. blockHash: ${blockHash} includeTransactions: ${includeTransactions}`,
143143
)
144-
const block = await this._client.ETH.getBlockByHash(hexToBytes(blockHash), includeTransactions)
144+
const block = await this._client.ETH.getBlockByHash(hexToBytes(blockHash as PrefixedHexString), includeTransactions)
145145
//@ts-ignore @ethereumjs/block has some weird typing discrepancy
146146
if (block !== undefined) return block
147147
throw new Error('Block not found')

packages/cli/src/rpc/modules/portal.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EntryStatus, distance } from '@chainsafe/discv5'
22
import { ENR } from '@chainsafe/enr'
3-
import { bigIntToHex, bytesToHex, hexToBytes, short } from '@ethereumjs/util'
3+
import { bigIntToHex, bytesToHex, hexToBytes, PrefixedHexString, short } from '@ethereumjs/util'
44
import {
55
ContentLookup,
66
FoundContent,
@@ -735,7 +735,7 @@ export class portal {
735735
const [contentKey] = params
736736
this.logger(`Received historyLocalContent request for ${contentKey}`)
737737

738-
const res = await this._history.findContentLocally(hexToBytes(contentKey))
738+
const res = await this._history.findContentLocally(hexToBytes(contentKey as PrefixedHexString))
739739
this.logger.extend('historyLocalContent')(
740740
`request returned ${res !== undefined ? res.length : 'null'} bytes`,
741741
)
@@ -754,7 +754,7 @@ export class portal {
754754
const [contentKey] = params
755755
this.logger(`Received stateLocalContent request for ${contentKey}`)
756756

757-
const res = await this._state.findContentLocally(hexToBytes(contentKey))
757+
const res = await this._state.findContentLocally(hexToBytes(contentKey as PrefixedHexString))
758758
this.logger.extend('stateLocalContent')(`request returned ${res?.length} bytes`)
759759
this.logger.extend('stateLocalContent')(
760760
`${res !== undefined ? bytesToHex(res) : 'content not found'}`,
@@ -771,7 +771,7 @@ export class portal {
771771
const [contentKey] = params
772772
this.logger.extend('beaconLocalContent')(`Received request for ${contentKey}`)
773773

774-
const content = await this._beacon.findContentLocally(hexToBytes(contentKey))
774+
const content = await this._beacon.findContentLocally(hexToBytes(contentKey as PrefixedHexString))
775775
this.logger.extend('beaconLocalContent')(
776776
`request returned ${content !== undefined ? content.length : 'null'} bytes`,
777777
)
@@ -787,7 +787,7 @@ export class portal {
787787

788788
// portal_*Store
789789
async historyStore(params: [string, string]) {
790-
const [contentKey, content] = params.map((param) => hexToBytes(param))
790+
const [contentKey, content] = params.map((param) => hexToBytes(param as PrefixedHexString))
791791
try {
792792
await this._history.store(contentKey, content)
793793
return true
@@ -798,8 +798,8 @@ export class portal {
798798
async stateStore(params: [string, string]) {
799799
const [contentKey, content] = params
800800
try {
801-
const contentKeyBytes = hexToBytes(contentKey)
802-
await this._state.store(contentKeyBytes, hexToBytes(content))
801+
const contentKeyBytes = hexToBytes(contentKey as PrefixedHexString)
802+
await this._state.store(contentKeyBytes, hexToBytes(content as PrefixedHexString))
803803
this.logger(`stored ${contentKey} in state network db`)
804804
return true
805805
} catch {
@@ -808,7 +808,7 @@ export class portal {
808808
}
809809
}
810810
async beaconStore(params: [string, string]) {
811-
const [contentKey, content] = params.map((param) => hexToBytes(param))
811+
const [contentKey, content] = params.map((param) => hexToBytes(param as PrefixedHexString))
812812
try {
813813
await this._beacon.store(contentKey, content)
814814
return true
@@ -819,7 +819,7 @@ export class portal {
819819
}
820820
// portal_*PutContent
821821
async historyPutContent(params: [string, string]) {
822-
const [contentKey, content] = params.map((param) => hexToBytes(param))
822+
const [contentKey, content] = params.map((param) => hexToBytes(param as PrefixedHexString))
823823
const contentId = this._history.contentKeyToId(contentKey)
824824
const d = distance(contentId, this._client.discv5.enr.nodeId)
825825
let storedLocally = false
@@ -841,7 +841,7 @@ export class portal {
841841
}
842842
}
843843
async statePutContent(params: [string, string]) {
844-
const [contentKey, content] = params.map((param) => hexToBytes(param))
844+
const [contentKey, content] = params.map((param) => hexToBytes(param as PrefixedHexString))
845845
const contentId = this._state.contentKeyToId(contentKey)
846846
const d = distance(contentId, this._client.discv5.enr.nodeId)
847847
let storedLocally = false
@@ -863,7 +863,7 @@ export class portal {
863863
}
864864
}
865865
async beaconPutContent(params: [string, string]) {
866-
const [contentKey, content] = params.map((param) => hexToBytes(param))
866+
const [contentKey, content] = params.map((param) => hexToBytes(param as PrefixedHexString))
867867
const contentId = this._beacon.contentKeyToId(contentKey)
868868
const d = distance(contentId, this._client.discv5.enr.nodeId)
869869
let storedLocally = false
@@ -892,7 +892,7 @@ export class portal {
892892
this.logger.extend('findContent')(
893893
`received request to send request to ${shortId(nodeId)} for contentKey ${contentKey}`,
894894
)
895-
const res = await this._history.sendFindContent(ENR.decodeTxt(enr), hexToBytes(contentKey))
895+
const res = await this._history.sendFindContent(ENR.decodeTxt(enr), hexToBytes(contentKey as PrefixedHexString))
896896
if (res === undefined) {
897897
this.logger.extend('findContent')('request returned undefined')
898898
return undefined
@@ -924,7 +924,7 @@ export class portal {
924924
this.logger.extend('findContent')(
925925
`received request to send request to ${shortId(nodeId)} for contentKey ${contentKey}`,
926926
)
927-
const res = await this._state.sendFindContent(ENR.decodeTxt(enr), hexToBytes(contentKey))
927+
const res = await this._state.sendFindContent(ENR.decodeTxt(enr), hexToBytes(contentKey as PrefixedHexString))
928928
if (res === undefined) {
929929
this.logger.extend('findContent')('request returned type: ENRS')
930930
return { enrs: [] }
@@ -957,7 +957,7 @@ export class portal {
957957
}
958958
}
959959

960-
const res = await this._beacon.sendFindContent(ENR.decodeTxt(enr), hexToBytes(contentKey))
960+
const res = await this._beacon.sendFindContent(ENR.decodeTxt(enr), hexToBytes(contentKey as PrefixedHexString))
961961

962962
if (res === undefined) {
963963
this.logger.extend('findContent')('request returned type: ENRS')
@@ -983,7 +983,7 @@ export class portal {
983983
async historyGetContent(params: [string]) {
984984
const [contentKey] = params
985985
this.logger.extend('historyGetContent')(`request received for ${contentKey}`)
986-
const lookup = new ContentLookup(this._history, hexToBytes(contentKey))
986+
const lookup = new ContentLookup(this._history, hexToBytes(contentKey as PrefixedHexString))
987987
const res = await lookup.startLookup()
988988
if (res === undefined) {
989989
this.logger.extend('historyGetContent')('request returned { enrs: [] }')
@@ -1010,7 +1010,7 @@ export class portal {
10101010
async stateGetContent(params: [string]) {
10111011
const [contentKey] = params
10121012
this.logger.extend('stateGetContent')(`request received for ${contentKey}`)
1013-
const lookup = new ContentLookup(this._state, hexToBytes(contentKey))
1013+
const lookup = new ContentLookup(this._state, hexToBytes(contentKey as PrefixedHexString))
10141014
const res = await lookup.startLookup()
10151015
if (!res) {
10161016
this.logger.extend('stateGetContent')('request returned { enrs: [] }')
@@ -1037,7 +1037,7 @@ export class portal {
10371037
async beaconGetContent(params: [string]) {
10381038
const [contentKey] = params
10391039
this.logger.extend('beaconGetContent')(`request received for ${contentKey}`)
1040-
const lookup = new ContentLookup(this._beacon, hexToBytes(contentKey))
1040+
const lookup = new ContentLookup(this._beacon, hexToBytes(contentKey as PrefixedHexString))
10411041
const res = await lookup.startLookup()
10421042
this.logger.extend('beaconGetContent')(`request returned ${JSON.stringify(res)}`)
10431043
if (!res) {
@@ -1067,7 +1067,7 @@ export class portal {
10671067
async historyTraceGetContent(params: [string]) {
10681068
const [contentKey] = params
10691069
this.logger.extend('historyTraceGetContent')(`request received for ${contentKey}`)
1070-
const lookup = new ContentLookup(this._history, hexToBytes(contentKey), true)
1070+
const lookup = new ContentLookup(this._history, hexToBytes(contentKey as PrefixedHexString), true)
10711071
const res = await lookup.startLookup()
10721072
this.logger.extend('historyTraceGetContent')(`request returned ${JSON.stringify(res)}`)
10731073
if (!res) {
@@ -1096,7 +1096,7 @@ export class portal {
10961096
async beaconTraceGetContent(params: [string]) {
10971097
const [contentKey] = params
10981098
this.logger.extend('beaconTraceGetContent')(`request received for ${contentKey}`)
1099-
const lookup = new ContentLookup(this._history, hexToBytes(contentKey), true)
1099+
const lookup = new ContentLookup(this._history, hexToBytes(contentKey as PrefixedHexString), true)
11001100
const res = await lookup.startLookup()
11011101
this.logger.extend('beaconTraceGetContent')(`request returned ${JSON.stringify(res)}`)
11021102
if (!res) {
@@ -1125,7 +1125,7 @@ export class portal {
11251125
async stateTraceGetContent(params: [string]) {
11261126
const [contentKey] = params
11271127
this.logger.extend('stateTraceGetContent')(`request received for ${contentKey}`)
1128-
const lookup = new ContentLookup(this._history, hexToBytes(contentKey), true)
1128+
const lookup = new ContentLookup(this._history, hexToBytes(contentKey as PrefixedHexString), true)
11291129
const res = await lookup.startLookup()
11301130
this.logger.extend('stateTraceGetContent')(`request returned ${JSON.stringify(res)}`)
11311131
if (!res) {
@@ -1157,8 +1157,8 @@ export class portal {
11571157
params: [string, [string, string][]],
11581158
): Promise<string | ReturnType<typeof this._history.sendOffer>> {
11591159
const [enrHex, contentItems] = params
1160-
const contentKeys = contentItems.map((item) => hexToBytes(item[0]))
1161-
const contentValues = contentItems.map((item) => hexToBytes(item[1]))
1160+
const contentKeys = contentItems.map((item) => hexToBytes(item[0] as PrefixedHexString))
1161+
const contentValues = contentItems.map((item) => hexToBytes(item[1] as PrefixedHexString))
11621162
const enr = ENR.decodeTxt(enrHex)
11631163
const res = await this._history.sendOffer(enr, contentKeys, contentValues)
11641164
if (res === undefined) {
@@ -1170,8 +1170,8 @@ export class portal {
11701170
params: [string, [string, string][]],
11711171
): Promise<string | ReturnType<typeof this._state.sendOffer>> {
11721172
const [enrHex, contentItems] = params
1173-
const contentKeys = contentItems.map((item) => hexToBytes(item[0]))
1174-
const contentValues = contentItems.map((item) => hexToBytes(item[1]))
1173+
const contentKeys = contentItems.map((item) => hexToBytes(item[0] as PrefixedHexString))
1174+
const contentValues = contentItems.map((item) => hexToBytes(item[1] as PrefixedHexString))
11751175
const enr = ENR.decodeTxt(enrHex)
11761176
const res = await this._state.sendOffer(enr, contentKeys, contentValues)
11771177
if (res === undefined) {
@@ -1183,8 +1183,8 @@ export class portal {
11831183
params: [string, [string, string][]],
11841184
): Promise<string | ReturnType<typeof this._beacon.sendOffer>> {
11851185
const [enrHex, contentItems] = params
1186-
const contentKeys = contentItems.map((item) => hexToBytes(item[0]))
1187-
const contentValues = contentItems.map((item) => hexToBytes(item[1]))
1186+
const contentKeys = contentItems.map((item) => hexToBytes(item[0] as PrefixedHexString))
1187+
const contentValues = contentItems.map((item) => hexToBytes(item[1] as PrefixedHexString))
11881188
const enr = ENR.decodeTxt(enrHex)
11891189
const res = await this._beacon.sendOffer(enr, contentKeys, contentValues)
11901190
if (res === undefined) {
@@ -1197,13 +1197,13 @@ export class portal {
11971197
async historyGossip(params: [string, string]) {
11981198
const [contentKey, content] = params
11991199
this.logger(`historyGossip request received for ${contentKey}`)
1200-
const res = await this._history.gossipContent(hexToBytes(contentKey), hexToBytes(content))
1200+
const res = await this._history.gossipContent(hexToBytes(contentKey as PrefixedHexString), hexToBytes(content as PrefixedHexString))
12011201
return res
12021202
}
12031203
async stateGossip(params: [string, string]) {
12041204
const [contentKey, content] = params
12051205
this.logger(`stateGossip request received for ${contentKey}`)
1206-
const res = await this._state.gossipContent(hexToBytes(contentKey), hexToBytes(content))
1206+
const res = await this._state.gossipContent(hexToBytes(contentKey as PrefixedHexString), hexToBytes(content as PrefixedHexString))
12071207
return res
12081208
}
12091209

packages/cli/src/rpc/modules/ultralight.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { bytesToHex, hexToBytes } from '@ethereumjs/util'
1+
import { bytesToHex, hexToBytes, PrefixedHexString } from '@ethereumjs/util'
22
import { HistoryNetworkContentType, NetworkId } from 'portalnetwork'
33

44
import { INTERNAL_ERROR } from '../error-code.js'
@@ -77,7 +77,7 @@ export class ultralight {
7777
`ultralight_addContentToDB request received for ${HistoryNetworkContentType[type]} ${contentKey}`,
7878
)
7979
try {
80-
await this._history!.store(hexToBytes(contentKey), hexToBytes(value))
80+
await this._history!.store(hexToBytes(contentKey as PrefixedHexString), hexToBytes(value as PrefixedHexString))
8181
this.logger(`${type} value for ${contentKey} added to content DB`)
8282
return `${type} value for ${contentKey} added to content DB`
8383
} catch (err: any) {

packages/portalnetwork/src/networks/contentLookup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class ContentLookup {
4141
this.contentTrace = trace
4242
? {
4343
origin: ('0x' + this.network.portal.discv5.enr.nodeId) as PrefixedHexString,
44-
targetId: Array.from(hexToBytes('0x' + this.contentId)) as any,
44+
targetId: Array.from(hexToBytes(`0x${this.contentId}`)) as any,
4545
metadata: {},
4646
}
4747
: undefined

packages/portalnetwork/src/networks/state/genesis.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import {
44
createAccount,
55
equalsBytes,
66
hexToBytes,
7-
parseGethGenesisState,
7+
PrefixedHexString,
88
} from '@ethereumjs/util'
9-
10-
import genesis from './mainnet.json' assert { type: 'json' }
9+
import { GethGenesis, parseGethGenesisState, type AccountState } from '@ethereumjs/common'
10+
import genesis from './mainnet.json' with { type: 'json' }
1111

1212
import type { MPTNode, Proof } from '@ethereumjs/mpt'
13-
import type { AccountState } from '@ethereumjs/util'
1413

1514
const genesisAccounts = () => {
16-
const parsed = parseGethGenesisState(genesis)
15+
const parsed = parseGethGenesisState(genesis as unknown as GethGenesis)
1716
const gState = parsed as Record<string, AccountState>
1817
const accounts: [string, Uint8Array][] = Object.entries(gState).map(([address, [balance]]) => {
1918
return [
@@ -29,9 +28,9 @@ const genesisAccounts = () => {
2928
export const genesisStateTrie = async () => {
3029
const trie = new Trie({ useKeyHashing: true })
3130
for (const account of genesisAccounts()) {
32-
await trie.put(hexToBytes(account[0]), account[1])
31+
await trie.put(hexToBytes(account[0] as PrefixedHexString), account[1])
3332
}
34-
if (equalsBytes(trie.root(), hexToBytes(genesis.genesisStateRoot)) === true) {
33+
if (equalsBytes(trie.root(), hexToBytes(genesis.genesisStateRoot as PrefixedHexString)) === true) {
3534
throw new Error('Invalid genesis state root')
3635
}
3736
return trie

0 commit comments

Comments
 (0)