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

Switch all bytes hex converters #661

Merged
merged 4 commits into from
Oct 25, 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
13 changes: 6 additions & 7 deletions packages/cli/scripts/beaconBridge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { concatBytes, hexToBytes } from '@ethereumjs/util'
import { bytesToHex, concatBytes, hexToBytes } from '@ethereumjs/util'
import { createBeaconConfig, defaultChainConfig } from '@lodestar/config'
import { genesisData } from '@lodestar/config/networks'
import { computeSyncPeriodAtSlot } from '@lodestar/light-client/utils'
Expand All @@ -14,7 +14,6 @@ import {
LightClientUpdatesByRange,
LightClientUpdatesByRangeKey,
getBeaconContentKey,
toHexString,
} from 'portalnetwork'

const { Client } = jayson
Expand Down Expand Up @@ -82,22 +81,22 @@ const main = async () => {
BeaconLightClientNetworkContentType.LightClientBootstrap,
LightClientBootstrapKey.serialize({ blockHash: hexToBytes(bootstrapRoot) }),
),
toHexString(
bytesToHex(
concatBytes(capellaForkDigest, ssz.capella.LightClientBootstrap.serialize(bootstrap)),
),
])
console.log('Pushed bootstrap into Portal Network', res)
const res2 = await ultralight.request('portal_beaconStore', [
rangeKey,
toHexString(serializedRange),
bytesToHex(serializedRange),
])
console.log(
`Pushed light client updates for range ${oldPeriod}-${currentPeriod} into Portal Network`,
res2,
)
const res3 = await ultralight.request('portal_beaconStore', [
optimisticUpdateKey,
toHexString(
bytesToHex(
concatBytes(
capellaForkDigest,
ssz.capella.LightClientOptimisticUpdate.serialize(optimisticUpdate),
Expand Down Expand Up @@ -130,7 +129,7 @@ const main = async () => {
)
let res = await ultralight.request('portal_beaconStore', [
optimisticUpdateKey,
toHexString(
bytesToHex(
concatBytes(
capellaForkDigest,
ssz.capella.LightClientOptimisticUpdate.serialize(optimisticUpdate),
Expand All @@ -152,7 +151,7 @@ const main = async () => {
)
res = await ultralight.request('portal_beaconStore', [
finalityUpdateKey,
toHexString(
bytesToHex(
concatBytes(
capellaForkDigest,
ssz.capella.LightClientFinalityUpdate.serialize(finalityUpdate),
Expand Down
13 changes: 6 additions & 7 deletions packages/cli/scripts/bootstrapFinder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { concatBytes, hexToBytes } from '@ethereumjs/util'
import { bytesToHex, concatBytes, hexToBytes } from '@ethereumjs/util'
import { getClient } from '@lodestar/api'
import { createBeaconConfig, defaultChainConfig } from '@lodestar/config'
import { genesisData } from '@lodestar/config/networks'
Expand All @@ -12,7 +12,6 @@ import {
LightClientUpdatesByRange,
LightClientUpdatesByRangeKey,
getBeaconContentKey,
toHexString,
} from 'portalnetwork'

import type { ForkLightClient } from '@lodestar/params'
Expand Down Expand Up @@ -68,7 +67,7 @@ const main = async () => {
for (let x = 0; x < 4; x++) {
const bootstrapSlot = updatesByRange.response![x].data.finalizedHeader.beacon.slot

const bootstrapRoot = toHexString(
const bootstrapRoot = bytesToHex(
(await api.beacon.getBlockRoot(bootstrapSlot)).response!.data.root,
)
const bootstrap = (await api.lightclient.getBootstrap(bootstrapRoot)).response!
Expand All @@ -77,7 +76,7 @@ const main = async () => {
BeaconLightClientNetworkContentType.LightClientBootstrap,
LightClientBootstrapKey.serialize({ blockHash: hexToBytes(bootstrapRoot) }),
),
toHexString(
bytesToHex(
concatBytes(
beaconConfig.forkName2ForkDigest(bootstrap.version),
(
Expand All @@ -96,7 +95,7 @@ const main = async () => {
}

for (let x = 0; x < 10; x++) {
await ultralights[x].request('portal_beaconStore', [rangeKey, toHexString(serializedRange)])
await ultralights[x].request('portal_beaconStore', [rangeKey, bytesToHex(serializedRange)])
}
console.log(
`Seeded light client updates for range ${oldPeriod}-${oldPeriod + 4} into Portal Network`,
Expand All @@ -115,7 +114,7 @@ const main = async () => {
}
const res3 = await ultralights[0].request('portal_beaconStore', [
optimisticUpdateKey,
toHexString(
bytesToHex(
concatBytes(
beaconConfig.forkName2ForkDigest(optimisticUpdate.version),
(
Expand Down Expand Up @@ -148,7 +147,7 @@ const main = async () => {
)
const res = await ultralights[0].request('portal_beaconStore', [
optimisticUpdateKey,
toHexString(
bytesToHex(
concatBytes(
beaconConfig.forkName2ForkDigest(optimisticUpdate.version),
(
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/scripts/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Block } from '@ethereumjs/block'
import { bytesToHex } from '@ethereumjs/util'
import jayson from 'jayson/promise/index.js'
import { toHexString } from 'portalnetwork'

// Bridge node script expects a url string corresponding to an execution node serving the Ethereum JSON-RPC API
const main = async () => {
Expand All @@ -21,8 +21,8 @@ const main = async () => {
)
const block = Block.fromRPC(res.result, [], { setHardfork: true })
const portRes = await portal.request('ultralight_addBlockToHistory', [
toHexString(block.hash()),
toHexString(block.serialize()),
bytesToHex(block.hash()),
bytesToHex(block.serialize()),
])
console.log(portRes.result)
await new Promise((resolve) => setTimeout(resolve, 12000))
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/scripts/buildAccumulator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BlockHeader } from '@ethereumjs/block'
import { bytesToHex, hexToBytes } from '@ethereumjs/util'
import jayson from 'jayson/promise/index.js'
import { fromHexString, toHexString } from 'portalnetwork'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'

Expand All @@ -20,11 +20,11 @@ const main = async () => {

for (let x = 1; x < args.blockHeight; x++) {
const web3res = await web3.request('debug_getHeaderRlp', [x])
const header = BlockHeader.fromRLPSerializedHeader(Buffer.from(fromHexString(web3res.result)), {
const header = BlockHeader.fromRLPSerializedHeader(Buffer.from(hexToBytes(web3res.result)), {
setHardfork: true,
})
const res2 = await ultralight.request('portal_addBlockHeaderToHistory', [
toHexString(header.hash()),
bytesToHex(header.hash()),
web3res.result,
])
console.log(x, res2)
Expand Down
8 changes: 2 additions & 6 deletions packages/cli/scripts/findContent.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { bytesToHex, hexToBytes } from '@ethereumjs/util'
import { hexToBytes } from '@ethereumjs/util'
import jayson from 'jayson/promise/index.js'
import {
HistoryNetworkContentType,
decodeHistoryNetworkContentKey,
getContentKey,
} from 'portalnetwork'
import { decodeHistoryNetworkContentKey } from 'portalnetwork'

import type { HttpClient } from 'jayson/promise/index.js'

Expand Down
23 changes: 11 additions & 12 deletions packages/cli/scripts/genesisBridge.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { fromHexString, toHexString } from '@chainsafe/ssz'
import { Blockchain } from '@ethereumjs/blockchain'
import { Common } from '@ethereumjs/common'
import { LeafNode, Trie } from '@ethereumjs/trie'
import { Account, parseGethGenesisState } from '@ethereumjs/util'
import { Account, bytesToHex, hexToBytes, parseGethGenesisState } from '@ethereumjs/util'
import { readFileSync, writeFileSync } from 'fs'
import { AccountTrieNodeContentKey, AccountTrieNodeOffer, tightlyPackNibbles } from 'portalnetwork'

Expand All @@ -29,9 +28,9 @@ const genesisAccounts = () => {
export const genesisStateTrie = async () => {
const trie = new Trie({ useKeyHashing: true })
for (const account of genesisAccounts()) {
await trie.put(fromHexString(account[0]), account[1])
await trie.put(hexToBytes(account[0]), account[1])
}
const root = toHexString(trie.root())
const root = bytesToHex(trie.root())
if (root !== genesis.genesisStateRoot) {
throw new Error('Invalid genesis state root')
}
Expand All @@ -49,7 +48,7 @@ export const generateAccountTrieProofs = async (
const leafNodes: [TrieNode, number[]][] = []
await trie.walkAllNodes(async (node, key) => {
node instanceof LeafNode ? leafNodes.push([node, key]) : nodes.push([node, key])
if (toHexString(trie['hash'](node.serialize())) === genesis.genesisStateRoot) {
if (bytesToHex(trie['hash'](node.serialize())) === genesis.genesisStateRoot) {
console.log(`ROOT NODE WALKING`)
console.log({ nodes, leafNodes })
}
Expand All @@ -58,17 +57,17 @@ export const generateAccountTrieProofs = async (
leafNodes.map(async ([node, path]) => {
const nodeHash = trie['hash'](node.serialize())
const proof = (await trie.findPath(nodeHash)).stack.map((node) => node.serialize())
return [toHexString(nodeHash), { path, proof }]
return [bytesToHex(nodeHash), { path, proof }]
}),
)
const proofs = await Promise.all(
nodes.map(async ([node, path]) => {
const nodeHash = trie['hash'](node.serialize())
const nodePath = await trie.findPath(nodeHash)
const proof = nodePath.stack.map((node) => node.serialize())
const content = [toHexString(nodeHash), { path, proof }]
const content = [bytesToHex(nodeHash), { path, proof }]
// console.log(content)
if (toHexString(nodeHash) === genesis.genesisStateRoot) {
if (bytesToHex(nodeHash) === genesis.genesisStateRoot) {
console.log('GENESIS PROOF:', content)
}
return content
Expand All @@ -95,10 +94,10 @@ const _index = async () => {
const leafNodeContent: [string, Uint8Array][] = Object.keys(leafProofs).map((nodeHash) => {
const path = tightlyPackNibbles(leafProofs[nodeHash].path as TNibble[])
const key: TAccountTrieNodeKey = {
nodeHash: fromHexString(nodeHash),
nodeHash: hexToBytes(nodeHash),
path,
}
const contentKey = toHexString(AccountTrieNodeContentKey.encode(key))
const contentKey = bytesToHex(AccountTrieNodeContentKey.encode(key))
const content = AccountTrieNodeOffer.serialize({
blockHash: genesisBlock.hash(),
proof: leafProofs[nodeHash].proof,
Expand All @@ -109,10 +108,10 @@ const _index = async () => {
try {
const path = tightlyPackNibbles(nonLeafProofs[nodeHash].path as TNibble[])
const key: TAccountTrieNodeKey = {
nodeHash: fromHexString(nodeHash),
nodeHash: hexToBytes(nodeHash),
path,
}
const contentKey = toHexString(AccountTrieNodeContentKey.encode(key))
const contentKey = bytesToHex(AccountTrieNodeContentKey.encode(key))
const content = AccountTrieNodeOffer.serialize({
blockHash: genesisBlock.hash(),
proof: nonLeafProofs[nodeHash].proof,
Expand Down
7 changes: 3 additions & 4 deletions packages/cli/scripts/gossipTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { hexToBytes } from '@ethereumjs/util'
import jayson from 'jayson/promise/index.js'
import { BlockHeaderWithProof, fromHexString } from 'portalnetwork'
import { BlockHeaderWithProof } from 'portalnetwork'

import type { HttpClient } from 'jayson/promise/index.js'

Expand All @@ -14,9 +15,7 @@ const blockBodyContent_value = '0x0800000008000000c0'
const { Client } = jayson

const gossip = async () => {
const blockHeaderContent = BlockHeaderWithProof.deserialize(
fromHexString(blockHeaderContent_value),
)
const blockHeaderContent = BlockHeaderWithProof.deserialize(hexToBytes(blockHeaderContent_value))
console.log(blockHeaderContent)

const ultralights: HttpClient[] = []
Expand Down
14 changes: 6 additions & 8 deletions packages/cli/scripts/recursiveFindContentTest.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { Block } from '@ethereumjs/block'
import { bytesToHex, hexToBytes } from '@ethereumjs/util'
import jayson from 'jayson/promise/index.js'
import { HistoryNetworkContentType, fromHexString, getContentKey, toHexString } from 'portalnetwork'
import { HistoryNetworkContentType, getContentKey } from 'portalnetwork'

import type { HttpClient } from 'jayson/promise/index.js'

const testBlocks = [
Block.fromRLPSerializedBlock(
Buffer.from(
fromHexString(
hexToBytes(
'0xf9028df90217a013ced9eaa49a522d4e7dcf80a739a57dbf08f4ce5efc4edbac86a66d8010f693a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d493479452bc44d5378309ee2abf1539bf71de1b7d7be3b5a0ac4ba3fe45d38b28e2af093024e112851a0f3c72bf1d02b306506e93cd39e26da068d722d467154a4570a7d759cd6b08792c4a1cb994261196b99735222b513bd9a00db8f50b32f1ec33d2546b4aa485defeae3a4e88d5f90fdcccadd6dff516e4b9b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008605af25e8b8e583030d41832fefd88252088455ee029798d783010102844765746887676f312e342e32856c696e7578a0ee8523229bf562950f30ad5a85be3fabc3f19926ee479826d54d4f5f2728c245880a0fb916fd59aad0f870f86e822d85850ba43b740083015f90947c5080988c6d91d090c23d54740f856c69450b29874b04c0f2616400801ba09aaf0e60d53dfb7c34ed51991bd350b8e021185ccc070b4264e209d16df5dc08a03565399bd97800b6d0e9959cd0920702039642b85b37a799391181e0610d6ba9c0',
),
),
{ setHardfork: true },
),
Block.fromRLPSerializedBlock(
Buffer.from(
fromHexString(
hexToBytes(
'0xf9028ef90217a08faf8b77fedb23eb4d591433ac3643be1764209efa52ac6386e10d1a127e4220a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d493479452bc44d5378309ee2abf1539bf71de1b7d7be3b5a0bd0eaff61d52c20e085cb7a7c60b312c792e0b141c5a00e50fd42f8ae1cfe51da09b763cefd23adf252ba87898f7cb8ccc06a4ebddc6be9032648fd55789d4c0b8a0cbb141d48d01bbbf96fb19adff38fb2a6c5e3de40843472a91067ef4f9eac09fb90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008605afdbcd75fd83030d42832fefd88252088455ee029f98d783010102844765746887676f312e342e32856c696e7578a04ddfa646f9a9ec8507af565631322186e2e06347586c9f137383d745ee8bf5958885808f6bbbb2a835f871f86f822d86850ba43b740083015f9094c197252baf4a4d2974eab91039594f789a8c207c88017a798d89731c00801ca0825c34f6ddfad0c9fe0e2aa75a3bff9bccc21e81a782fb2a454afb4ad4abac70a0106d3942a42839f74bbbf71b6ff8c5b11082af8b0ff2799cb9b8d14b7fcc9e11c0',
),
),
Expand All @@ -41,10 +42,7 @@ const recursiveFindContent = async () => {

const headerKey = getContentKey(HistoryNetworkContentType.BlockHeader, testBlocks[0].hash())
const header = testBlocks[0].header.serialize()
const store = await ultralights[0].request('portal_historyStore', [
headerKey,
toHexString(header),
])
const store = await ultralights[0].request('portal_historyStore', [headerKey, bytesToHex(header)])
store.result !== undefined || console.log('store fail')
for (const enr of enrs.slice(0, 9)) {
const ping = await ultralights[9].request('portal_historyPing', [enr, '0x00'])
Expand All @@ -56,7 +54,7 @@ const recursiveFindContent = async () => {
const find = await ultralights[8].request('portal_historyRecursiveFindContent', [headerKey])
console.log(
'RecursiveFindContent',
Buffer.from(fromHexString(find.result)).equals(header) ? 'pass' : 'fail',
Buffer.from(hexToBytes(find.result)).equals(header) ? 'pass' : 'fail',
)
if (find.result !== undefined) {
await ultralights[8].request('portal_historyStore', [headerKey, find.result])
Expand Down
15 changes: 6 additions & 9 deletions packages/cli/scripts/seeder.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Block } from '@ethereumjs/block'
import { hexToBytes } from '@ethereumjs/util'
import { bytesToHex, hexToBytes } from '@ethereumjs/util'
import { readFileSync } from 'fs'
import jayson from 'jayson/promise/index.js'
import { createRequire } from 'module'
import { HistoryNetworkContentType, fromHexString, getContentKey, toHexString } from 'portalnetwork'
import { HistoryNetworkContentType, getContentKey } from 'portalnetwork'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'

Expand Down Expand Up @@ -86,10 +86,7 @@ const main = async () => {
}
console.log(`ok ${method} test`)
}
const epochKey = getContentKey(
HistoryNetworkContentType.EpochAccumulator,
fromHexString(epoch.hash),
)
const epochKey = getContentKey(HistoryNetworkContentType.EpochAccumulator, hexToBytes(epoch.hash))
let res = await clientInfo.ultralight.client.request('ultralight_addContentToDB', [
epochKey,
epoch.serialized,
Expand Down Expand Up @@ -170,15 +167,15 @@ const main = async () => {
await testRes([clients[0]], 'portal_historyOffer', [
[
clientInfo.peer1.enr,
getContentKey(HistoryNetworkContentType.BlockHeader, fromHexString(block[0])),
getContentKey(HistoryNetworkContentType.BlockHeader, hexToBytes(block[0])),
[
[
toHexString(
bytesToHex(
Block.fromRLPSerializedBlock(hexToBytes((block[1] as any).rlp), {
setHardfork: true,
}).header.serialize(),
),
toHexString(
bytesToHex(
Block.fromRLPSerializedBlock(hexToBytes((block[1] as any).rlp), {
setHardfork: true,
}).header.serialize(),
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/scripts/shareEpochs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Block } from '@ethereumjs/block'
import { bytesToHex } from '@ethereumjs/util'
import jayson from 'jayson/promise/index.js'
import { NetworkId, toHexString } from 'portalnetwork'
import { NetworkId } from 'portalnetwork'

// async function testnet(number: number): Promise<[HttpClient[], string[]]> {
// const bootnodes = []
Expand Down Expand Up @@ -37,13 +38,13 @@ const main = async () => {
const block = await web3.request('debug_getBlockRlp', [i])
const blockhash = Block.fromRLPSerializedBlock(block.result).hash()
const res1 = await builder.request('portal_addBlockToHistory', [
toHexString(blockhash),
bytesToHex(blockhash),
block.result,
])
console.log(i + 1, res1)
// const bn = Math.floor(Math.random() * bootnodes.length)
// const res2 = await bootnodes[bn].request('portal_addBlockToHistory', [
// toHexString(blockhash),
// bytesToHex(blockhash),
// block.result,
// ])
// console.log((i + 1),bn, res2)
Expand Down
Loading
Loading