From 0c332452b20b6d7c96be9bae8aac8cafd104dbd1 Mon Sep 17 00:00:00 2001 From: acolytec3 <17355484+acolytec3@users.noreply.github.com> Date: Fri, 25 Oct 2024 11:02:12 -0400 Subject: [PATCH] Switch all bytes hex converters (#661) * switch all bytes hex converters * lint * more lint * run all integration tests --- packages/cli/scripts/beaconBridge.ts | 13 ++- packages/cli/scripts/bootstrapFinder.ts | 13 ++- packages/cli/scripts/bridge.ts | 6 +- packages/cli/scripts/buildAccumulator.ts | 6 +- packages/cli/scripts/findContent.ts | 8 +- packages/cli/scripts/genesisBridge.ts | 23 ++-- packages/cli/scripts/gossipTest.ts | 7 +- .../cli/scripts/recursiveFindContentTest.ts | 14 ++- packages/cli/scripts/seeder.ts | 15 ++- packages/cli/scripts/shareEpochs.ts | 7 +- packages/cli/scripts/stateBridge.ts | 34 +++--- packages/cli/src/rpc/modules/beacon.ts | 8 +- packages/cli/src/rpc/modules/portal.ts | 100 +++++++++--------- packages/cli/src/rpc/validators.ts | 5 +- packages/cli/src/util.ts | 6 +- .../rpc/beacon/getLightClientUpdate.spec.ts | 8 +- packages/portalnetwork/package.json | 2 +- packages/portalnetwork/src/client/client.ts | 19 ++-- packages/portalnetwork/src/client/eth.ts | 5 +- packages/portalnetwork/src/client/provider.ts | 5 +- .../src/networks/beacon/beacon.ts | 19 ++-- .../src/networks/contentLookup.ts | 7 +- .../src/networks/history/history.ts | 17 ++- .../src/networks/history/receiptManager.ts | 4 +- .../src/networks/history/util.ts | 7 +- .../portalnetwork/src/networks/network.ts | 26 +++-- .../portalnetwork/src/networks/networkDB.ts | 28 ++--- .../src/networks/state/genesis.ts | 17 +-- .../src/networks/state/portalTrie.ts | 9 +- .../portalnetwork/src/networks/state/state.ts | 17 +-- .../portalnetwork/src/networks/state/util.ts | 7 +- packages/portalnetwork/src/util/discv5.ts | 1 - packages/portalnetwork/src/util/helpers.ts | 46 ++++---- .../utp/PortalNetworkUtp/ContentRequest.ts | 3 +- .../test/client/eth/ethCall.spec.ts | 14 +-- .../test/client/eth/ethGetBalance.spec.ts | 10 +- .../test/integration/beacon.spec.ts | 15 ++- .../integration/eth/getBlockByNumber.spec.ts | 14 ++- .../test/integration/integration.spec.ts | 7 +- .../test/integration/state.spec.ts | 3 +- .../test/integration/stateGenesis.spec.ts | 26 ++--- .../portalnetwork/test/integration/util.ts | 31 +++--- .../test/networks/beacon/beacon.spec.ts | 5 +- .../networks/beacon/portalSpecTests.spec.ts | 5 +- .../test/networks/beacon/types.spec.ts | 5 +- .../history/blockHeaderByNumber.spec.ts | 9 +- .../test/networks/history/blockIndex.spec.ts | 5 +- .../test/networks/history/contentKeys.spec.ts | 13 ++- .../test/networks/history/headerProof.spec.ts | 25 +++-- .../networks/history/historyNetwork.spec.ts | 5 +- .../networks/history/portalSpecTests.spec.ts | 3 +- .../test/networks/history/types.spec.ts | 20 ++-- .../test/networks/history/util.spec.ts | 11 +- .../networks/state/accountTrieNode.spec.ts | 5 +- .../test/networks/state/content.spec.ts | 81 +++++++------- .../test/networks/state/stateManager.spec.ts | 16 +-- .../portalnetwork/test/wire/types.spec.ts | 20 ++-- .../test/wire/utp/packet.spec.ts | 9 +- .../test/wire/utp/socket.spec.ts | 5 +- .../portalnetwork/test/wire/utp/util.spec.ts | 4 +- 60 files changed, 418 insertions(+), 460 deletions(-) diff --git a/packages/cli/scripts/beaconBridge.ts b/packages/cli/scripts/beaconBridge.ts index b2544befc..86f4261c8 100644 --- a/packages/cli/scripts/beaconBridge.ts +++ b/packages/cli/scripts/beaconBridge.ts @@ -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' @@ -14,7 +14,6 @@ import { LightClientUpdatesByRange, LightClientUpdatesByRangeKey, getBeaconContentKey, - toHexString, } from 'portalnetwork' const { Client } = jayson @@ -82,14 +81,14 @@ 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`, @@ -97,7 +96,7 @@ const main = async () => { ) const res3 = await ultralight.request('portal_beaconStore', [ optimisticUpdateKey, - toHexString( + bytesToHex( concatBytes( capellaForkDigest, ssz.capella.LightClientOptimisticUpdate.serialize(optimisticUpdate), @@ -130,7 +129,7 @@ const main = async () => { ) let res = await ultralight.request('portal_beaconStore', [ optimisticUpdateKey, - toHexString( + bytesToHex( concatBytes( capellaForkDigest, ssz.capella.LightClientOptimisticUpdate.serialize(optimisticUpdate), @@ -152,7 +151,7 @@ const main = async () => { ) res = await ultralight.request('portal_beaconStore', [ finalityUpdateKey, - toHexString( + bytesToHex( concatBytes( capellaForkDigest, ssz.capella.LightClientFinalityUpdate.serialize(finalityUpdate), diff --git a/packages/cli/scripts/bootstrapFinder.ts b/packages/cli/scripts/bootstrapFinder.ts index 2d1172593..2472b3ec6 100644 --- a/packages/cli/scripts/bootstrapFinder.ts +++ b/packages/cli/scripts/bootstrapFinder.ts @@ -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' @@ -12,7 +12,6 @@ import { LightClientUpdatesByRange, LightClientUpdatesByRangeKey, getBeaconContentKey, - toHexString, } from 'portalnetwork' import type { ForkLightClient } from '@lodestar/params' @@ -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! @@ -77,7 +76,7 @@ const main = async () => { BeaconLightClientNetworkContentType.LightClientBootstrap, LightClientBootstrapKey.serialize({ blockHash: hexToBytes(bootstrapRoot) }), ), - toHexString( + bytesToHex( concatBytes( beaconConfig.forkName2ForkDigest(bootstrap.version), ( @@ -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`, @@ -115,7 +114,7 @@ const main = async () => { } const res3 = await ultralights[0].request('portal_beaconStore', [ optimisticUpdateKey, - toHexString( + bytesToHex( concatBytes( beaconConfig.forkName2ForkDigest(optimisticUpdate.version), ( @@ -148,7 +147,7 @@ const main = async () => { ) const res = await ultralights[0].request('portal_beaconStore', [ optimisticUpdateKey, - toHexString( + bytesToHex( concatBytes( beaconConfig.forkName2ForkDigest(optimisticUpdate.version), ( diff --git a/packages/cli/scripts/bridge.ts b/packages/cli/scripts/bridge.ts index 90d7ab4ce..d862cbd50 100644 --- a/packages/cli/scripts/bridge.ts +++ b/packages/cli/scripts/bridge.ts @@ -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 () => { @@ -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)) diff --git a/packages/cli/scripts/buildAccumulator.ts b/packages/cli/scripts/buildAccumulator.ts index 2cc97b0f3..2abd59303 100644 --- a/packages/cli/scripts/buildAccumulator.ts +++ b/packages/cli/scripts/buildAccumulator.ts @@ -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' @@ -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) diff --git a/packages/cli/scripts/findContent.ts b/packages/cli/scripts/findContent.ts index 708c4ef03..749a3fdc7 100644 --- a/packages/cli/scripts/findContent.ts +++ b/packages/cli/scripts/findContent.ts @@ -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' diff --git a/packages/cli/scripts/genesisBridge.ts b/packages/cli/scripts/genesisBridge.ts index 61ff29d1d..18254254c 100644 --- a/packages/cli/scripts/genesisBridge.ts +++ b/packages/cli/scripts/genesisBridge.ts @@ -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' @@ -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') } @@ -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 }) } @@ -58,7 +57,7 @@ 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( @@ -66,9 +65,9 @@ export const generateAccountTrieProofs = async ( 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 @@ -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, @@ -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, diff --git a/packages/cli/scripts/gossipTest.ts b/packages/cli/scripts/gossipTest.ts index ce554eec7..a79261c6a 100644 --- a/packages/cli/scripts/gossipTest.ts +++ b/packages/cli/scripts/gossipTest.ts @@ -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' @@ -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[] = [] diff --git a/packages/cli/scripts/recursiveFindContentTest.ts b/packages/cli/scripts/recursiveFindContentTest.ts index 314d4db05..1801a7a4c 100644 --- a/packages/cli/scripts/recursiveFindContentTest.ts +++ b/packages/cli/scripts/recursiveFindContentTest.ts @@ -1,13 +1,14 @@ 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', ), ), @@ -15,7 +16,7 @@ const testBlocks = [ ), Block.fromRLPSerializedBlock( Buffer.from( - fromHexString( + hexToBytes( '0xf9028ef90217a08faf8b77fedb23eb4d591433ac3643be1764209efa52ac6386e10d1a127e4220a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d493479452bc44d5378309ee2abf1539bf71de1b7d7be3b5a0bd0eaff61d52c20e085cb7a7c60b312c792e0b141c5a00e50fd42f8ae1cfe51da09b763cefd23adf252ba87898f7cb8ccc06a4ebddc6be9032648fd55789d4c0b8a0cbb141d48d01bbbf96fb19adff38fb2a6c5e3de40843472a91067ef4f9eac09fb90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008605afdbcd75fd83030d42832fefd88252088455ee029f98d783010102844765746887676f312e342e32856c696e7578a04ddfa646f9a9ec8507af565631322186e2e06347586c9f137383d745ee8bf5958885808f6bbbb2a835f871f86f822d86850ba43b740083015f9094c197252baf4a4d2974eab91039594f789a8c207c88017a798d89731c00801ca0825c34f6ddfad0c9fe0e2aa75a3bff9bccc21e81a782fb2a454afb4ad4abac70a0106d3942a42839f74bbbf71b6ff8c5b11082af8b0ff2799cb9b8d14b7fcc9e11c0', ), ), @@ -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']) @@ -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]) diff --git a/packages/cli/scripts/seeder.ts b/packages/cli/scripts/seeder.ts index c2b577738..9b675d1ff 100644 --- a/packages/cli/scripts/seeder.ts +++ b/packages/cli/scripts/seeder.ts @@ -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' @@ -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, @@ -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(), diff --git a/packages/cli/scripts/shareEpochs.ts b/packages/cli/scripts/shareEpochs.ts index 1174c8e40..6773a38e6 100644 --- a/packages/cli/scripts/shareEpochs.ts +++ b/packages/cli/scripts/shareEpochs.ts @@ -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 = [] @@ -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) diff --git a/packages/cli/scripts/stateBridge.ts b/packages/cli/scripts/stateBridge.ts index 39ec7cfab..cb6757aff 100644 --- a/packages/cli/scripts/stateBridge.ts +++ b/packages/cli/scripts/stateBridge.ts @@ -1,4 +1,4 @@ -import { fromHexString, toHexString } from '@chainsafe/ssz' +import { bytesToHex, hexToBytes } from '@chainsafe/ssz' import { Block } from '@ethereumjs/block' import { Address } from '@ethereumjs/util' import { Alchemy, Network } from 'alchemy-sdk' @@ -27,7 +27,7 @@ const alchemy = new Alchemy({ const db = new Map() const remember = async (contentKey: Uint8Array, content: Uint8Array) => { - db.set(toHexString(contentKey), content) + db.set(bytesToHex(contentKey), content) } const store = async (contentKey: Uint8Array, content: Uint8Array) => { @@ -36,8 +36,8 @@ const store = async (contentKey: Uint8Array, content: Uint8Array) => { port: workerData.port, }) const stored = await client.request('portal_stateStore', [ - toHexString(contentKey), - toHexString(content), + bytesToHex(contentKey), + bytesToHex(content), ]) parentPort?.postMessage(`stored: ${stored.result}`) } @@ -48,13 +48,13 @@ const index = async (block: Block) => { port: workerData.port, }) await client.request('ultralight_addBlockToHistory', [ - toHexString(block.header.hash()), - toHexString(block.serialize()), + bytesToHex(block.header.hash()), + bytesToHex(block.serialize()), ]) await client.request('ultralight_indexBlock', [ '0x' + block.header.number.toString(16), - toHexString(block.header.hash()), + bytesToHex(block.header.hash()), ]) parentPort?.postMessage(`indexed: ${block.header.number}`) } @@ -64,8 +64,8 @@ const gossip = async (contentKey: Uint8Array, content: Uint8Array) => { port: workerData.port ?? 8545, }) const stored = await client.request('portal_stateGossip', [ - toHexString(contentKey), - toHexString(content), + bytesToHex(contentKey), + bytesToHex(content), ]) parentPort?.postMessage(`gossiped to: ${stored.result} nodes`) } @@ -114,11 +114,11 @@ const generateStateNetworkContent = async () => { : '0x0' + contract.address.slice(2), ), slot: BigInt(p.key), - stateRoot: fromHexString(stateroot), + stateRoot: hexToBytes(stateroot), }) const data = { witnesses: p.proof.map((x: string) => { - return x.length % 2 === 0 ? fromHexString(x) : fromHexString('0x0' + x.slice(2)) + return x.length % 2 === 0 ? hexToBytes(x) : hexToBytes('0x0' + x.slice(2)) }), } const csp = ContractStorageTrieProofType.serialize(data) @@ -139,12 +139,12 @@ const generateStateNetworkContent = async () => { for (const c of contracts) { const accountProof = await alchemy.core.send('eth_getProof', [c.contractAddress, [], number]) const accountProofContent = AccountTrieProofType.serialize({ - witnesses: accountProof.accountProof.map(fromHexString), + witnesses: accountProof.accountProof.map(hexToBytes), }) const accountProofContentKey = getStateNetworkContentKey({ contentType: StateNetworkContentType.AccountTrieProof, address: Address.fromString(c.contractAddress), - stateRoot: fromHexString(stateroot), + stateRoot: hexToBytes(stateroot), }) void toStorage(accountProofContentKey, accountProofContent) const codeHash = accountProof.codeHash @@ -152,9 +152,9 @@ const generateStateNetworkContent = async () => { const bytecodeContentkey = getStateNetworkContentKey({ contentType: StateNetworkContentType.ContractByteCode, address: Address.fromString(c.contractAddress), - codeHash: fromHexString(codeHash), + codeHash: hexToBytes(codeHash), }) - const contractBytecode = ContractByteCodeType.serialize(fromHexString(bytecode)) + const contractBytecode = ContractByteCodeType.serialize(hexToBytes(bytecode)) totalBytes_code += contractBytecode.length totalBytecode++ void toStorage(bytecodeContentkey, contractBytecode) @@ -177,7 +177,7 @@ const generateStateNetworkContent = async () => { return [ x.address, { - witnesses: x.accountProof.map(fromHexString), + witnesses: x.accountProof.map(hexToBytes), }, ] }), @@ -191,7 +191,7 @@ const generateStateNetworkContent = async () => { totalATP++ const contentKey = getStateNetworkContentKey({ address: Address.fromString(add), - stateRoot: fromHexString(stateroot), + stateRoot: hexToBytes(stateroot), contentType: StateNetworkContentType.AccountTrieProof, }) await toStorage(contentKey, content) diff --git a/packages/cli/src/rpc/modules/beacon.ts b/packages/cli/src/rpc/modules/beacon.ts index 6dcbfd4fe..c23e01439 100644 --- a/packages/cli/src/rpc/modules/beacon.ts +++ b/packages/cli/src/rpc/modules/beacon.ts @@ -1,3 +1,4 @@ +import { hexToBytes } from '@ethereumjs/util' import { RunStatusCode } from '@lodestar/light-client' import { ssz } from '@lodestar/types' import { @@ -8,7 +9,6 @@ import { NetworkId, type PortalNetwork, computeLightClientKeyFromPeriod, - fromHexString, getBeaconContentKey, } from 'portalnetwork' @@ -90,12 +90,12 @@ export class beacon { const period = Number(BigInt(params[0])) const rangeKey = getBeaconContentKey( BeaconLightClientNetworkContentType.LightClientUpdate, - fromHexString(computeLightClientKeyFromPeriod(period)), + hexToBytes(computeLightClientKeyFromPeriod(period)), ) const update = await this._beacon.retrieve(rangeKey) if (update !== undefined) { return ssz.capella.LightClientUpdate.toJson( - ssz.capella.LightClientUpdate.deserialize(fromHexString(update)), + ssz.capella.LightClientUpdate.deserialize(hexToBytes(update)), ) } const lookup = new ContentLookup( @@ -109,7 +109,7 @@ export class beacon { if (res !== undefined && 'content' in Object.keys(res)) { return ssz.capella.LightClientUpdate.toJson( //@ts-ignore - ssz.capella.LightClientUpdate.deserialize(fromHexString(res.content)), + ssz.capella.LightClientUpdate.deserialize(hexToBytes(res.content)), ) } } diff --git a/packages/cli/src/rpc/modules/portal.ts b/packages/cli/src/rpc/modules/portal.ts index 1730abc7a..70b8ea6e0 100644 --- a/packages/cli/src/rpc/modules/portal.ts +++ b/packages/cli/src/rpc/modules/portal.ts @@ -1,9 +1,8 @@ import { EntryStatus } from '@chainsafe/discv5' import { ENR } from '@chainsafe/enr' import { BitArray } from '@chainsafe/ssz' -import { hexToBytes, short } from '@ethereumjs/util' +import { bytesToHex, hexToBytes, short } from '@ethereumjs/util' import { - BeaconLightClientNetworkContentType, ContentLookup, ContentMessageType, FoundContent, @@ -12,10 +11,7 @@ import { NodeLookup, PingPongCustomDataType, PortalWireMessageType, - fromHexString, - getBeaconContentKey, shortId, - toHexString, } from 'portalnetwork' import { INVALID_PARAMS } from '../error-code.js' @@ -592,7 +588,7 @@ export class portal { return ( pong && { enrSeq: Number(pong.enrSeq), - dataRadius: toHexString(pong.customPayload), + dataRadius: bytesToHex(pong.customPayload), } ) } @@ -609,7 +605,7 @@ export class portal { return ( pong && { enrSeq: Number(pong.enrSeq), - dataRadius: toHexString(pong.customPayload), + dataRadius: bytesToHex(pong.customPayload), } ) } @@ -626,7 +622,7 @@ export class portal { return ( pong && { enrSeq: Number(pong.enrSeq), - dataRadius: toHexString(pong.customPayload), + dataRadius: bytesToHex(pong.customPayload), } ) } @@ -787,12 +783,12 @@ export class portal { const [contentKey] = params this.logger(`Received historyLocalContent request for ${contentKey}`) - const res = await this._history.findContentLocally(fromHexString(contentKey)) + const res = await this._history.findContentLocally(hexToBytes(contentKey)) this.logger.extend(`historyLocalContent`)( `request returned ${res !== undefined ? res.length : 'null'} bytes`, ) this.logger.extend(`historyLocalContent`)( - `${res !== undefined ? short(toHexString(res)) : 'content not found'}`, + `${res !== undefined ? short(bytesToHex(res)) : 'content not found'}`, ) if (res === undefined) { throw { @@ -800,16 +796,16 @@ export class portal { message: 'no content found', } } - return toHexString(res) + return bytesToHex(res) } async stateLocalContent(params: [string]): Promise { const [contentKey] = params this.logger(`Received stateLocalContent request for ${contentKey}`) - const res = await this._state.findContentLocally(fromHexString(contentKey)) + const res = await this._state.findContentLocally(hexToBytes(contentKey)) this.logger.extend(`stateLocalContent`)(`request returned ${res?.length} bytes`) this.logger.extend(`stateLocalContent`)( - `${res !== undefined ? toHexString(res) : 'content not found'}`, + `${res !== undefined ? bytesToHex(res) : 'content not found'}`, ) if (res === undefined) { throw { @@ -817,20 +813,20 @@ export class portal { message: 'no content found', } } - return toHexString(res) + return bytesToHex(res) } async beaconLocalContent(params: [string]) { const [contentKey] = params this.logger.extend(`beaconLocalContent`)(`Received request for ${contentKey}`) - const content = await this._beacon.findContentLocally(fromHexString(contentKey)) + const content = await this._beacon.findContentLocally(hexToBytes(contentKey)) this.logger.extend(`beaconLocalContent`)( `request returned ${content !== undefined ? content.length : 'null'} bytes`, ) this.logger.extend(`beaconLocalContent`)( - `retrieved content: ${content !== undefined ? short(toHexString(content)) : 'content not found'}`, + `retrieved content: ${content !== undefined ? short(bytesToHex(content)) : 'content not found'}`, ) - if (content !== undefined) return toHexString(content) + if (content !== undefined) return bytesToHex(content) throw { code: -32009, message: 'no content found', @@ -839,7 +835,7 @@ export class portal { // portal_*Store async historyStore(params: [string, string]) { - const [contentKey, content] = params.map((param) => fromHexString(param)) + const [contentKey, content] = params.map((param) => hexToBytes(param)) try { await this._history.store(contentKey, content) return true @@ -850,8 +846,8 @@ export class portal { async stateStore(params: [string, string]) { const [contentKey, content] = params try { - const contentKeyBytes = fromHexString(contentKey) - await this._state.store(contentKeyBytes, fromHexString(content)) + const contentKeyBytes = hexToBytes(contentKey) + await this._state.store(contentKeyBytes, hexToBytes(content)) this.logger(`stored ${contentKey} in state network db`) return true } catch { @@ -860,7 +856,7 @@ export class portal { } } async beaconStore(params: [string, string]) { - const [contentKey, content] = params.map((param) => fromHexString(param)) + const [contentKey, content] = params.map((param) => hexToBytes(param)) try { await this._beacon.store(contentKey, content) return true @@ -883,7 +879,7 @@ export class portal { return '' } } - const res = await this._history.sendFindContent(nodeId, fromHexString(contentKey)) + const res = await this._history.sendFindContent(nodeId, hexToBytes(contentKey)) if (res === undefined) { this.logger.extend('findContent')(`request returned type: ENRS`) return { enrs: [] } @@ -897,7 +893,7 @@ export class portal { returnValue = { enrs: res.enrs.map((v: Uint8Array) => ENR.decode(v).encodeTxt()) } } else { returnValue = { - content: res.content.length > 0 ? toHexString(res.content) : '0x', + content: res.content.length > 0 ? bytesToHex(res.content) : '0x', utpTransfer: res.utp, } } @@ -915,7 +911,7 @@ export class portal { this.logger.extend('findContent')( `received request to send request to ${shortId(nodeId)} for contentKey ${contentKey}`, ) - const res = await this._state.sendFindContent(nodeId, fromHexString(contentKey)) + const res = await this._state.sendFindContent(nodeId, hexToBytes(contentKey)) if (res === undefined) { this.logger.extend('findContent')(`request returned type: ENRS`) return { enrs: [] } @@ -929,7 +925,7 @@ export class portal { returnValue = { enrs: res.enrs.map((v: Uint8Array) => ENR.decode(v).encodeTxt()) } } else { returnValue = { - content: res.content.length > 0 ? toHexString(res.content) : '0x', + content: res.content.length > 0 ? bytesToHex(res.content) : '0x', utpTransfer: res.utp, } } @@ -948,7 +944,7 @@ export class portal { } } - const res = await this._beacon.sendFindContent(nodeId, fromHexString(contentKey)) + const res = await this._beacon.sendFindContent(nodeId, hexToBytes(contentKey)) if (res === undefined) { this.logger.extend('findContent')(`request returned type: ENRS`) @@ -963,7 +959,7 @@ export class portal { returnValue = { enrs: res.enrs.map((v: Uint8Array) => ENR.decode(v).encodeTxt()) } } else { returnValue = { - content: res.content.length > 0 ? toHexString(res.content) : '0x', + content: res.content.length > 0 ? bytesToHex(res.content) : '0x', utpTransfer: res.utp, } } @@ -974,7 +970,7 @@ export class portal { async historyGetContent(params: [string]) { const [contentKey] = params this.logger.extend('historyGetContent')(`request received for ${contentKey}`) - const lookup = new ContentLookup(this._history, fromHexString(contentKey)) + const lookup = new ContentLookup(this._history, hexToBytes(contentKey)) const res = await lookup.startLookup() if (res === undefined) { this.logger.extend('historyGetContent')(`request returned { enrs: [] }`) @@ -982,18 +978,18 @@ export class portal { } if ('enrs' in res) { this.logger.extend('historyGetContent')( - `request returned { enrs: [{${{ enrs: res.enrs.map(toHexString) }}}] }`, + `request returned { enrs: [{${{ enrs: res.enrs.map(bytesToHex) }}}] }`, ) if (res.enrs.length === 0) { throw new Error('No content found') } - return { enrs: res.enrs.map(toHexString) } + return { enrs: res.enrs.map(bytesToHex) } } else { this.logger.extend('historyGetContent')( - `request returned { content: ${short(toHexString(res.content))}, utpTransfer: ${res.utp} }`, + `request returned { content: ${short(bytesToHex(res.content))}, utpTransfer: ${res.utp} }`, ) return { - content: toHexString(res.content), + content: bytesToHex(res.content), utpTransfer: res.utp, } } @@ -1001,7 +997,7 @@ export class portal { async stateGetContent(params: [string]) { const [contentKey] = params this.logger.extend('stateGetContent')(`request received for ${contentKey}`) - const lookup = new ContentLookup(this._state, fromHexString(contentKey)) + const lookup = new ContentLookup(this._state, hexToBytes(contentKey)) const res = await lookup.startLookup() this.logger.extend('stateGetContent')(`request returned ${JSON.stringify(res)}`) if (!res) { @@ -1010,18 +1006,18 @@ export class portal { } if ('enrs' in res) { this.logger.extend('stateGetContent')( - `request returned { enrs: [{${{ enrs: res.enrs.map(toHexString) }}}] }`, + `request returned { enrs: [{${{ enrs: res.enrs.map(bytesToHex) }}}] }`, ) if (res.enrs.length === 0) { throw new Error('No content found') } - return { enrs: res.enrs.map(toHexString) } + return { enrs: res.enrs.map(bytesToHex) } } else { this.logger.extend('stateGetContent')( - `request returned { content: ${toHexString(res.content)}, utpTransfer: ${res.utp} }`, + `request returned { content: ${bytesToHex(res.content)}, utpTransfer: ${res.utp} }`, ) return { - content: toHexString(res.content), + content: bytesToHex(res.content), utpTransfer: res.utp, } } @@ -1029,7 +1025,7 @@ export class portal { async beaconGetContent(params: [string]) { const [contentKey] = params this.logger.extend('beaconGetContent')(`request received for ${contentKey}`) - const lookup = new ContentLookup(this._beacon, fromHexString(contentKey)) + const lookup = new ContentLookup(this._beacon, hexToBytes(contentKey)) const res = await lookup.startLookup() this.logger.extend('beaconGetContent')(`request returned ${JSON.stringify(res)}`) if (!res) { @@ -1038,18 +1034,18 @@ export class portal { } if ('enrs' in res) { this.logger.extend('beaconGetContent')( - `request returned { enrs: [{${{ enrs: res.enrs.map(toHexString) }}}] }`, + `request returned { enrs: [{${{ enrs: res.enrs.map(bytesToHex) }}}] }`, ) if (res.enrs.length === 0) { throw new Error('No content found') } - return { enrs: res.enrs.map(toHexString) } + return { enrs: res.enrs.map(bytesToHex) } } else { this.logger.extend('beaconGetContent')( - `request returned { content: ${toHexString(res.content)}, utpTransfer: ${res.utp} }`, + `request returned { content: ${bytesToHex(res.content)}, utpTransfer: ${res.utp} }`, ) return { - content: toHexString(res.content), + content: bytesToHex(res.content), utpTransfer: res.utp, } } @@ -1072,8 +1068,8 @@ export class portal { } async stateOffer(params: [string, [string, string][]]) { const [enrHex, contentItems] = params - const contentKeys = contentItems.map((item) => fromHexString(item[0])) - const contentValues = contentItems.map((item) => fromHexString(item[1])) + const contentKeys = contentItems.map((item) => hexToBytes(item[0])) + const contentValues = contentItems.map((item) => hexToBytes(item[1])) const enr = ENR.decodeTxt(enrHex) if (this._state.routingTable.getWithPending(enr.nodeId)?.value === undefined) { const res = await this._state.sendPing(enr) @@ -1101,14 +1097,14 @@ export class portal { // portal_*SendOffer async historySendOffer(params: [string, string[]]) { const [dstId, contentKeys] = params - const keys = contentKeys.map((key) => fromHexString(key)) + const keys = contentKeys.map((key) => hexToBytes(key)) const res = await this._history.sendOffer(dstId, keys) const enr = this._history.routingTable.getWithPending(dstId)?.value return res && enr && '0x' + enr.seq.toString(16) } async stateSendOffer(params: [string, string[]]) { const [dstId, contentKeys] = params - const keys = contentKeys.map((key) => fromHexString(key)) + const keys = contentKeys.map((key) => hexToBytes(key)) const res = await this._state.sendOffer(dstId, keys) const enr = this._state.routingTable.getWithPending(dstId)?.value return res && enr && '0x' + enr.seq.toString(16) @@ -1151,35 +1147,35 @@ export class portal { async historyGossip(params: [string, string]) { const [contentKey, content] = params this.logger(`historyGossip request received for ${contentKey}`) - const res = await this._history.gossipContent(fromHexString(contentKey), fromHexString(content)) + const res = await this._history.gossipContent(hexToBytes(contentKey), hexToBytes(content)) return res } async stateGossip(params: [string, string]) { const [contentKey, content] = params this.logger(`stateGossip request received for ${contentKey}`) - const res = await this._state.gossipContent(fromHexString(contentKey), fromHexString(content)) + const res = await this._state.gossipContent(hexToBytes(contentKey), hexToBytes(content)) return res } // other async historySendFindContent(params: [string, string]) { const [nodeId, contentKey] = params - const res = await this._history.sendFindContent(nodeId, fromHexString(contentKey)) + const res = await this._history.sendFindContent(nodeId, hexToBytes(contentKey)) const enr = this._history.routingTable.getWithPending(nodeId)?.value return res && enr && '0x' + enr.seq.toString(16) } async beaconSendFindContent(params: [string, string]) { const [nodeId, contentKey] = params console.log(nodeId) - const res = await this._beacon.sendFindContent(nodeId, fromHexString(contentKey)) - if (res !== undefined && 'content' in res) return toHexString(res.content as Uint8Array) + const res = await this._beacon.sendFindContent(nodeId, hexToBytes(contentKey)) + if (res !== undefined && 'content' in res) return bytesToHex(res.content as Uint8Array) return '0x' } async historySendContent(params: [string, string]) { const [nodeId, content] = params const payload = ContentMessageType.serialize({ selector: 1, - value: fromHexString(content), + value: hexToBytes(content), }) const enr = this._history.routingTable.getWithPending(nodeId)?.value void this.sendPortalNetworkResponse( diff --git a/packages/cli/src/rpc/validators.ts b/packages/cli/src/rpc/validators.ts index 41562b738..8dfbee7a9 100644 --- a/packages/cli/src/rpc/validators.ts +++ b/packages/cli/src/rpc/validators.ts @@ -1,4 +1,5 @@ -import { HistoryNetworkContentType, NetworkId, toHexString } from 'portalnetwork' +import { bytesToHex } from '@ethereumjs/util' +import { HistoryNetworkContentType, NetworkId } from 'portalnetwork' import { isValidEnr } from '../util.js' @@ -106,7 +107,7 @@ export const validators = { get dstId() { return (params: any[], index: number) => { if (params[index]['raw'] !== undefined) { - params[index] = toHexString(Uint8Array.from(params[index]['raw'])).slice(2) + params[index] = bytesToHex(Uint8Array.from(params[index]['raw'])).slice(2) } if (typeof params[index] !== 'string') { return { diff --git a/packages/cli/src/util.ts b/packages/cli/src/util.ts index 3aa7b583c..523fd8dc8 100644 --- a/packages/cli/src/util.ts +++ b/packages/cli/src/util.ts @@ -1,13 +1,13 @@ import { BlockHeader } from '@ethereumjs/block' import { RLP } from '@ethereumjs/rlp' import { TransactionFactory } from '@ethereumjs/tx' +import { bytesToHex } from '@ethereumjs/util' import { BlockBodyContentType, BlockHeaderWithProof, EpochAccumulator, sszReceiptType, sszUnclesType, - toHexString, } from 'portalnetwork' import type { Enr } from './rpc/schema/types.js' @@ -76,7 +76,7 @@ export const toJSON = (contentKey: Uint8Array, res: Uint8Array) => { const proof = blockHeaderWithProof.proof.selector === 0 ? [] - : (blockHeaderWithProof.proof.value as Uint8Array[])?.map((p) => toHexString(p)) + : (blockHeaderWithProof.proof.value as Uint8Array[])?.map((p) => bytesToHex(p)) content = { header, proof } break } @@ -85,7 +85,7 @@ export const toJSON = (contentKey: Uint8Array, res: Uint8Array) => { const transactions = blockBody.allTransactions.map((tx) => TransactionFactory.fromSerializedData(tx).toJSON(), ) - const unclesRlp = toHexString(sszUnclesType.deserialize(blockBody.sszUncles)) + const unclesRlp = bytesToHex(sszUnclesType.deserialize(blockBody.sszUncles)) content = { transactions, uncles: { diff --git a/packages/cli/test/rpc/beacon/getLightClientUpdate.spec.ts b/packages/cli/test/rpc/beacon/getLightClientUpdate.spec.ts index 42f0e602f..8b6e772b2 100644 --- a/packages/cli/test/rpc/beacon/getLightClientUpdate.spec.ts +++ b/packages/cli/test/rpc/beacon/getLightClientUpdate.spec.ts @@ -1,12 +1,10 @@ -import { bytesToHex } from '@ethereumjs/util' +import { bytesToHex, hexToBytes } from '@ethereumjs/util' import { computeSyncPeriodAtSlot } from '@lodestar/light-client/utils' import { ssz } from '@lodestar/types' import { BeaconLightClientNetworkContentType, computeLightClientKeyFromPeriod, - fromHexString, getBeaconContentKey, - toHexString, } from 'portalnetwork' import { assert, describe, it } from 'vitest' @@ -18,13 +16,13 @@ describe(`${method} tests`, () => { const rangeJson = require('./range.json')[0] const rangeKey = getBeaconContentKey( BeaconLightClientNetworkContentType.LightClientUpdate, - fromHexString( + hexToBytes( computeLightClientKeyFromPeriod( computeSyncPeriodAtSlot(Number(rangeJson.data.attested_header.beacon.slot)), ), ), ) - const rangeHex = toHexString( + const rangeHex = bytesToHex( ssz.capella.LightClientUpdate.serialize( ssz.capella.LightClientUpdate.fromJson(rangeJson.data), ), diff --git a/packages/portalnetwork/package.json b/packages/portalnetwork/package.json index 50b4bbcba..468f41100 100644 --- a/packages/portalnetwork/package.json +++ b/packages/portalnetwork/package.json @@ -12,7 +12,7 @@ "build": "tsc", "docs": "typedoc src/index.ts", "test": "npx vitest run test/* -c=./vitest.config.unit.ts", - "test:integration": "npx vitest run ./test/integration/*.spec.ts -c=./vitest.config.integration.ts", + "test:integration": "npx vitest run ./test/integration/ -c=./vitest.config.integration.ts", "typecheck": "tsc -p ./tsconfig.types.json", "coverage": "c8 npm run test", "coverage:all": "c8 npm run test:all", diff --git a/packages/portalnetwork/src/client/client.ts b/packages/portalnetwork/src/client/client.ts index 5790beebb..cddde547b 100644 --- a/packages/portalnetwork/src/client/client.ts +++ b/packages/portalnetwork/src/client/client.ts @@ -1,7 +1,6 @@ import { Discv5 } from '@chainsafe/discv5' import { SignableENR } from '@chainsafe/enr' -import { toHexString } from '@chainsafe/ssz' -import { hexToBytes } from '@ethereumjs/util' +import { bytesToHex, hexToBytes } from '@ethereumjs/util' import { keys } from '@libp2p/crypto' import { multiaddr } from '@multiformats/multiaddr' import debug from 'debug' @@ -378,12 +377,12 @@ export class PortalNetwork extends (EventEmitter as { new (): PortalNetworkEvent { type: 'put', key: 'privateKey', - value: toHexString(this.discv5.enr.privateKey!), + value: bytesToHex(this.discv5.enr.privateKey!), }, { type: 'put', key: 'publicKey', - value: toHexString(this.discv5.enr.publicKey!), + value: bytesToHex(this.discv5.enr.publicKey!), }, { type: 'put', @@ -398,15 +397,13 @@ export class PortalNetwork extends (EventEmitter as { new (): PortalNetworkEvent private onTalkReq = async (src: INodeAddress, sourceId: ENR | null, message: ITalkReqMessage) => { this.metrics?.totalBytesReceived.inc(message.request.length) - if (toHexString(message.protocol) === NetworkId.UTPNetwork) { + if (bytesToHex(message.protocol) === NetworkId.UTPNetwork) { await this.handleUTP(src, src.nodeId, message, message.request) return } - const network = this.networks.get(toHexString(message.protocol) as NetworkId) + const network = this.networks.get(bytesToHex(message.protocol) as NetworkId) if (!network) { - this.logger( - `Received TALKREQ message on unsupported network ${toHexString(message.protocol)}`, - ) + this.logger(`Received TALKREQ message on unsupported network ${bytesToHex(message.protocol)}`) await this.sendPortalNetworkResponse(src, message.id, new Uint8Array()) return @@ -480,7 +477,7 @@ export class PortalNetwork extends (EventEmitter as { new (): PortalNetworkEvent hexToBytes(messageNetwork), ) this.eventLog && - this.emit('SendTalkReq', nodeAddr.nodeId, toHexString(res), toHexString(payload)) + this.emit('SendTalkReq', nodeAddr.nodeId, bytesToHex(res), bytesToHex(payload)) return res } catch (err: any) { if (networkId === NetworkId.UTPNetwork) { @@ -497,7 +494,7 @@ export class PortalNetwork extends (EventEmitter as { new (): PortalNetworkEvent payload: Uint8Array, ) => { this.eventLog && - this.emit('SendTalkResp', src.nodeId, requestId.toString(16), toHexString(payload)) + this.emit('SendTalkResp', src.nodeId, requestId.toString(16), bytesToHex(payload)) await this.discv5.sendTalkResp(src, requestId, payload) } } diff --git a/packages/portalnetwork/src/client/eth.ts b/packages/portalnetwork/src/client/eth.ts index 93cda47f6..bb8b094af 100644 --- a/packages/portalnetwork/src/client/eth.ts +++ b/packages/portalnetwork/src/client/eth.ts @@ -1,7 +1,6 @@ -import { fromHexString } from '@chainsafe/ssz' import { Common } from '@ethereumjs/common' import { EVM } from '@ethereumjs/evm' -import { Address, TypeOutput, bytesToHex, toType } from '@ethereumjs/util' +import { Address, TypeOutput, bytesToHex, hexToBytes, toType } from '@ethereumjs/util' import { keccak256 } from 'ethereum-cryptography/keccak.js' import { @@ -209,7 +208,7 @@ export class ETH { gasLimit: toType(gasLimit, TypeOutput.BigInt), gasPrice: toType(gasPrice, TypeOutput.BigInt), value: toType(value, TypeOutput.BigInt), - data: data !== undefined ? fromHexString(data) : undefined, + data: data !== undefined ? hexToBytes(data) : undefined, } const res = (await evm.runCall(runCallOpts)).execResult.returnValue return bytesToHex(res) diff --git a/packages/portalnetwork/src/client/provider.ts b/packages/portalnetwork/src/client/provider.ts index 72b3b3074..518925148 100644 --- a/packages/portalnetwork/src/client/provider.ts +++ b/packages/portalnetwork/src/client/provider.ts @@ -1,9 +1,8 @@ -import { hexToBytes } from '@ethereumjs/util' +import { bytesToHex, hexToBytes } from '@ethereumjs/util' import { ethers } from 'ethers' import { addRLPSerializedBlock } from '../networks/index.js' import { NetworkId } from '../networks/types.js' -import { toHexString } from '../util/discv5.js' import { blockFromRpc, ethJsBlockToEthersBlock, @@ -92,7 +91,7 @@ export class UltralightProvider extends ethers.JsonRpcProvider { const ethJSBlock = blockFromRpc(block) await addRLPSerializedBlock( - toHexString(ethJSBlock.serialize()), + bytesToHex(ethJSBlock.serialize()), block.hash, this.historyNetwork, [], // I'm too lazy to fix this right now diff --git a/packages/portalnetwork/src/networks/beacon/beacon.ts b/packages/portalnetwork/src/networks/beacon/beacon.ts index c2cf6a4e1..2ff918fd5 100644 --- a/packages/portalnetwork/src/networks/beacon/beacon.ts +++ b/packages/portalnetwork/src/networks/beacon/beacon.ts @@ -1,5 +1,4 @@ import { ProofType } from '@chainsafe/persistent-merkle-tree' -import { toHexString } from '@chainsafe/ssz' import { bytesToHex, bytesToInt, @@ -201,7 +200,7 @@ export class BeaconLightClientNetwork extends BaseNetwork { update.slice(4), ) as LightClientUpdate roots.push( - toHexString(ssz.phase0.BeaconBlockHeader.hashTreeRoot(decoded.finalizedHeader.beacon)), + bytesToHex(ssz.phase0.BeaconBlockHeader.hashTreeRoot(decoded.finalizedHeader.beacon)), ) } this.bootstrapFinder.set(nodeId, roots) @@ -569,7 +568,7 @@ export class BeaconLightClientNetwork extends BaseNetwork { this.portal.metrics?.contentMessagesSent.inc() this.logger( - `Received FindContent request for contentKey: ${toHexString( + `Received FindContent request for contentKey: ${bytesToHex( decodedContentMessage.contentKey, )}`, ) @@ -579,9 +578,9 @@ export class BeaconLightClientNetwork extends BaseNetwork { } else if (value !== undefined && value.length < MAX_PACKET_SIZE) { this.logger( 'Found value for requested content ' + - toHexString(decodedContentMessage.contentKey) + + bytesToHex(decodedContentMessage.contentKey) + ' ' + - toHexString(value.slice(0, 10)) + + bytesToHex(value.slice(0, 10)) + `...`, ) const payload = ContentMessageType.serialize({ @@ -639,7 +638,7 @@ export class BeaconLightClientNetwork extends BaseNetwork { // optimistic update and this ensures we don't accidentally store multiple await this.put( hexToBytes(intToHex(BeaconLightClientNetworkContentType.LightClientOptimisticUpdate)), - toHexString(value), + bytesToHex(value), ) break case BeaconLightClientNetworkContentType.LightClientFinalityUpdate: @@ -647,7 +646,7 @@ export class BeaconLightClientNetwork extends BaseNetwork { // finality update and this ensures we don't accidentally store multiple await this.put( hexToBytes(intToHex(BeaconLightClientNetworkContentType.LightClientFinalityUpdate)), - toHexString(value), + bytesToHex(value), ) break case BeaconLightClientNetworkContentType.HistoricalSummaries: { @@ -688,7 +687,7 @@ export class BeaconLightClientNetwork extends BaseNetwork { // We store the HistoricalSummaries object by content type since we should only ever have one (most up to date) await this.put( hexToBytes(intToHex(BeaconLightClientNetworkContentType.HistoricalSummaries)), - toHexString(value), + bytesToHex(value), ) // Store the Historical Summaries data in memory so can be accessed easily by the History Network @@ -699,7 +698,7 @@ export class BeaconLightClientNetwork extends BaseNetwork { break } default: - await this.put(contentKey, toHexString(value)) + await this.put(contentKey, bytesToHex(value)) } this.logger( @@ -967,7 +966,7 @@ export class BeaconLightClientNetwork extends BaseNetwork { if (offerAccepted) { this.logger.extend('OFFER')(`Accepting an OFFER`) const desiredKeys = msg.contentKeys.filter((k, i) => contentIds[i] === true) - this.logger(toHexString(msg.contentKeys[0])) + this.logger(bytesToHex(msg.contentKeys[0])) await this.sendAccept(src, requestId, contentIds, desiredKeys) } else { this.logger.extend('OFFER')(`Declining an OFFER since no interesting content`) diff --git a/packages/portalnetwork/src/networks/contentLookup.ts b/packages/portalnetwork/src/networks/contentLookup.ts index 4d4d2e812..e7c3fb029 100644 --- a/packages/portalnetwork/src/networks/contentLookup.ts +++ b/packages/portalnetwork/src/networks/contentLookup.ts @@ -1,7 +1,6 @@ import { distance } from '@chainsafe/discv5' import { ENR } from '@chainsafe/enr' -import { toHexString } from '@chainsafe/ssz' -import { short } from '@ethereumjs/util' +import { bytesToHex, short } from '@ethereumjs/util' import { Heap } from 'heap-js' import { serializedContentKeyToContentId, shortId } from '../util/index.js' @@ -55,7 +54,7 @@ export class ContentLookup { public startLookup = async (): Promise => { // Don't support content lookups for networks that don't implement it (i.e. Canonical Indices) if (!this.network.sendFindContent) return - this.logger(`starting recursive content lookup for ${toHexString(this.contentKey)}`) + this.logger(`starting recursive content lookup for ${bytesToHex(this.contentKey)}`) this.network.portal.metrics?.totalContentLookups.inc() try { // Try to find content locally first @@ -122,7 +121,7 @@ export class ContentLookup { if ('content' in res) { this.finished = true // findContent returned data sought - this.logger(`received content corresponding to ${shortId(toHexString(this.contentKey))}`) + this.logger(`received content corresponding to ${shortId(bytesToHex(this.contentKey))}`) // Mark content offered to peer that sent it to us (so we don't try to offer it to them) this.network.routingTable.contentKeyKnownToPeer(peer.nodeId, this.contentKey) this.network.portal.metrics?.successfulContentLookups.inc() diff --git a/packages/portalnetwork/src/networks/history/history.ts b/packages/portalnetwork/src/networks/history/history.ts index 017bea062..c014edcfe 100644 --- a/packages/portalnetwork/src/networks/history/history.ts +++ b/packages/portalnetwork/src/networks/history/history.ts @@ -14,7 +14,6 @@ import { reassembleBlock, saveReceipts, shortId, - toHexString, } from '../../index.js' import { BaseNetwork } from '../network.js' import { NetworkId } from '../types.js' @@ -164,7 +163,7 @@ export class HistoryNetwork extends BaseNetwork { } else { verifyPreMergeHeaderProof( proof.value, - toHexString(header.hash()), + bytesToHex(header.hash()), validation.blockNumber, ) } @@ -185,7 +184,7 @@ export class HistoryNetwork extends BaseNetwork { } } } - await this.indexBlockHash(header.number, toHexString(header.hash())) + await this.indexBlockHash(header.number, bytesToHex(header.hash())) return header.hash() } @@ -284,8 +283,8 @@ export class HistoryNetwork extends BaseNetwork { switch (contentType) { case HistoryNetworkContentType.BlockHeader: { try { - await this.validateHeader(value, { blockHash: toHexString(keyOpt) }) - await this.put(contentKey, toHexString(value)) + await this.validateHeader(value, { blockHash: bytesToHex(keyOpt) }) + await this.put(contentKey, bytesToHex(value)) } catch (err) { this.logger(`Error validating header: ${(err as any).message}`) } @@ -298,7 +297,7 @@ export class HistoryNetwork extends BaseNetwork { case HistoryNetworkContentType.Receipt: { try { sszReceiptsListType.deserialize(value) - await this.put(contentKey, toHexString(value)) + await this.put(contentKey, bytesToHex(value)) } catch (err: any) { this.logger(`Received invalid bytes as receipt data for ${bytesToHex(keyOpt)}`) return @@ -311,7 +310,7 @@ export class HistoryNetwork extends BaseNetwork { const blockHash = await this.validateHeader(value, { blockNumber }) // Store block header using 0x00 key type const hashKey = getContentKey(HistoryNetworkContentType.BlockHeader, blockHash) - await this.put(hashKey, toHexString(value)) + await this.put(hashKey, bytesToHex(value)) // Switch keyOpt to blockNumber for gossip purposes keyOpt = blockNumber } catch (err) { @@ -361,7 +360,7 @@ export class HistoryNetwork extends BaseNetwork { } const bodyContentKey = getContentKey(HistoryNetworkContentType.BlockBody, hashKey) if (block instanceof Block) { - await this.put(bodyContentKey, toHexString(bodyBytes)) + await this.put(bodyContentKey, bytesToHex(bodyBytes)) // TODO: Decide when and if to build and store receipts. // Doing this here caused a bottleneck when same receipt is gossiped via uTP at the same time. // if (block.transactions.length > 0) { @@ -370,7 +369,7 @@ export class HistoryNetwork extends BaseNetwork { } else { this.logger(`Could not verify block content`) this.logger(`Adding anyway for testing...`) - await this.put(bodyContentKey, toHexString(bodyBytes)) + await this.put(bodyContentKey, bytesToHex(bodyBytes)) // TODO: Decide what to do here. We shouldn't be storing block bodies without a corresponding header // as it's against spec return diff --git a/packages/portalnetwork/src/networks/history/receiptManager.ts b/packages/portalnetwork/src/networks/history/receiptManager.ts index 4263d5a6d..d3742c5af 100644 --- a/packages/portalnetwork/src/networks/history/receiptManager.ts +++ b/packages/portalnetwork/src/networks/history/receiptManager.ts @@ -1,8 +1,8 @@ -import { toHexString } from '@chainsafe/ssz' import * as RLP from '@ethereumjs/rlp' import { bigIntToBytes, bytesToBigInt, + bytesToHex, bytesToInt, equalsBytes, hexToBytes, @@ -125,7 +125,7 @@ export async function getLogs( const returnedLogs: GetLogsReturn = [] let returnedLogsSize = 0 for (const block of blocks) { - const receipts = await getReceipts(toHexString(block!.hash())) + const receipts = await getReceipts(bytesToHex(block!.hash())) if (receipts.length === 0) continue let logs: GetLogsReturn = [] let logIndex = 0 diff --git a/packages/portalnetwork/src/networks/history/util.ts b/packages/portalnetwork/src/networks/history/util.ts index cb25d8247..2df93efa3 100644 --- a/packages/portalnetwork/src/networks/history/util.ts +++ b/packages/portalnetwork/src/networks/history/util.ts @@ -1,9 +1,8 @@ import { digest } from '@chainsafe/as-sha256' import { ProofType, createProof } from '@chainsafe/persistent-merkle-tree' -import { fromHexString, toHexString } from '@chainsafe/ssz' import { Block, BlockHeader } from '@ethereumjs/block' import { RLP as rlp } from '@ethereumjs/rlp' -import { bytesToUnprefixedHex, equalsBytes, hexToBytes } from '@ethereumjs/util' +import { bytesToHex, bytesToUnprefixedHex, equalsBytes, hexToBytes } from '@ethereumjs/util' import { ssz } from '@lodestar/types' import { historicalEpochs } from './data/epochHashes.js' @@ -210,7 +209,7 @@ export const addRLPSerializedBlock = async ( network: HistoryNetwork, witnesses: Witnesses, ) => { - const block = Block.fromRLPSerializedBlock(fromHexString(rlpHex), { + const block = Block.fromRLPSerializedBlock(hexToBytes(rlpHex), { setHardfork: true, }) const header = block.header @@ -232,7 +231,7 @@ export const addRLPSerializedBlock = async ( header: header.serialize(), proof: { selector: 0, value: null }, }) - await network.indexBlockHash(header.number, toHexString(header.hash())) + await network.indexBlockHash(header.number, bytesToHex(header.hash())) await network.store(headerKey, headerProof) } diff --git a/packages/portalnetwork/src/networks/network.ts b/packages/portalnetwork/src/networks/network.ts index 51a74a548..6bdaac78c 100644 --- a/packages/portalnetwork/src/networks/network.ts +++ b/packages/portalnetwork/src/networks/network.ts @@ -1,8 +1,14 @@ import { digest } from '@chainsafe/as-sha256' import { EntryStatus, distance } from '@chainsafe/discv5' import { ENR } from '@chainsafe/enr' -import { BitArray, fromHexString, toHexString } from '@chainsafe/ssz' -import { bytesToInt, bytesToUnprefixedHex, concatBytes, hexToBytes } from '@ethereumjs/util' +import { BitArray } from '@chainsafe/ssz' +import { + bytesToHex, + bytesToInt, + bytesToUnprefixedHex, + concatBytes, + hexToBytes, +} from '@ethereumjs/util' import { EventEmitter } from 'events' import { @@ -150,7 +156,7 @@ export abstract class BaseNetwork extends EventEmitter { size = await this.db.size() } for (const [key, val] of toDelete) { - void this.gossipContent(fromHexString(key), fromHexString(val)) + void this.gossipContent(hexToBytes(key), hexToBytes(val)) } } catch (err: any) { this.logger(`Error pruning content: ${err.message}`) @@ -516,7 +522,7 @@ export abstract class BaseNetwork extends EventEmitter { const d = distance(cid, this.enr.nodeId) if (d >= this.nodeRadius) { this.logger.extend('OFFER')( - `Content key: ${toHexString(msg.contentKeys[x])} is outside radius.\nd=${d}\nr=${this.nodeRadius}`, + `Content key: ${bytesToHex(msg.contentKeys[x])} is outside radius.\nd=${d}\nr=${this.nodeRadius}`, ) continue } @@ -534,7 +540,7 @@ export abstract class BaseNetwork extends EventEmitter { if (offerAccepted) { this.logger(`Accepting an OFFER`) const desiredKeys = msg.contentKeys.filter((k, i) => contentIds[i] === true) - this.logger(toHexString(msg.contentKeys[0])) + this.logger(bytesToHex(msg.contentKeys[0])) for (const k of desiredKeys) { this.streamingKey(k) } @@ -623,7 +629,7 @@ export abstract class BaseNetwork extends EventEmitter { this.portal.metrics?.contentMessagesSent.inc() this.logger( - `Received FindContent request for contentKey: ${toHexString( + `Received FindContent request for contentKey: ${bytesToHex( decodedContentMessage.contentKey, )}`, ) @@ -635,9 +641,9 @@ export abstract class BaseNetwork extends EventEmitter { } else if (value instanceof Uint8Array && value.length < MAX_PACKET_SIZE) { this.logger( 'Found value for requested content ' + - toHexString(decodedContentMessage.contentKey) + + bytesToHex(decodedContentMessage.contentKey) + ' ' + - toHexString(value.slice(0, 10)) + + bytesToHex(value.slice(0, 10)) + `...`, ) const payload = ContentMessageType.serialize({ @@ -854,7 +860,7 @@ export abstract class BaseNetwork extends EventEmitter { const offered = await Promise.allSettled( peers.map(async (peer) => { this.logger.extend(`gossipContent`)( - `Offering ${toHexString(contentKey)} to ${shortId(peer.nodeId)}`, + `Offering ${bytesToHex(contentKey)} to ${shortId(peer.nodeId)}`, ) const res = await this.sendMessage(peer, payload, this.networkId) return [peer, res] @@ -871,7 +877,7 @@ export abstract class BaseNetwork extends EventEmitter { const msg = decoded.value as AcceptMessage if (msg.contentKeys.get(0) === true) { this.logger.extend(`gossipContent`)( - `${toHexString(contentKey)} accepted by ${shortId(peer.nodeId)}`, + `${bytesToHex(contentKey)} accepted by ${shortId(peer.nodeId)}`, ) accepted++ this.logger.extend(`gossipContent`)(`accepted: ${accepted}`) diff --git a/packages/portalnetwork/src/networks/networkDB.ts b/packages/portalnetwork/src/networks/networkDB.ts index 990961724..95e510d9e 100644 --- a/packages/portalnetwork/src/networks/networkDB.ts +++ b/packages/portalnetwork/src/networks/networkDB.ts @@ -1,6 +1,6 @@ import { distance } from '@chainsafe/discv5' -import { ContainerType, UintBigintType, fromHexString, toHexString } from '@chainsafe/ssz' -import { padToEven } from '@ethereumjs/util' +import { ContainerType, UintBigintType } from '@chainsafe/ssz' +import { bytesToHex, hexToBytes, padToEven } from '@ethereumjs/util' import debug from 'debug' import fs from 'fs' import { MemoryLevel } from 'memory-level' @@ -38,7 +38,7 @@ export class NetworkDB { this.contentId = contentId ?? function (contentKey: Uint8Array) { - return toHexString(contentKey) + return bytesToHex(contentKey) } this.maxStorage = maxStorage ?? 1024 } @@ -64,10 +64,10 @@ export class NetworkDB { */ async put(key: string | Uint8Array, val: string | Uint8Array) { if (key instanceof Uint8Array) { - key = toHexString(key) + key = bytesToHex(key) } if (val instanceof Uint8Array) { - val = toHexString(val) + val = bytesToHex(val) } if (!key.startsWith('0x')) throw new Error('Key must be 0x prefixed hex string') if (!val.startsWith('0x')) throw new Error('Key must be 0x prefixed hex string') @@ -77,7 +77,7 @@ export class NetworkDB { this.logger(`Error putting content in DB: ${err.toString()}`) } this.streaming.delete(key) - this.logger(`Put ${key} in DB. Size=${fromHexString(padToEven(val)).length} bytes`) + this.logger(`Put ${key} in DB. Size=${hexToBytes(padToEven(val)).length} bytes`) } /** * Get a value from the database by key. @@ -86,7 +86,7 @@ export class NetworkDB { */ async get(key: string | Uint8Array) { if (key instanceof Uint8Array) { - key = toHexString(key) + key = bytesToHex(key) } // this.streaming is a Set of contentKeys currently streaming over uTP // the timeout is a safety measure to prevent the while loop from running indefinitely in case of a uTP stream failure @@ -102,7 +102,7 @@ export class NetworkDB { this.logger(`Getting ${key} from DB`) const val = await this.db.get(key) this.logger( - `Got ${key} from DB with key: ${key}. Size=${fromHexString(padToEven(val)).length} bytes`, + `Got ${key} from DB with key: ${key}. Size=${hexToBytes(padToEven(val)).length} bytes`, ) clearTimeout(timeout) return val @@ -113,7 +113,7 @@ export class NetworkDB { */ async del(key: string | Uint8Array): Promise { if (key instanceof Uint8Array) { - key = toHexString(key) + key = bytesToHex(key) } await this.db.del(key) } @@ -133,8 +133,8 @@ export class NetworkDB { const _db = this.db as MemoryLevel let size = 0 for await (const [key, value] of _db.iterator()) { - size += fromHexString('0x' + padToEven(key.slice(2))).length - size += fromHexString(value).length + size += hexToBytes('0x' + padToEven(key.slice(2))).length + size += hexToBytes(value).length } return size } @@ -157,7 +157,7 @@ export class NetworkDB { */ addToStreaming(key: string | Uint8Array): void { if (key instanceof Uint8Array) { - key = toHexString(key) + key = bytesToHex(key) } this.logger(`Adding ${key} to streaming`) this.streaming.add(key) @@ -173,7 +173,7 @@ export class NetworkDB { const toDelete: [string, string][] = [] for await (const [key, value] of this.db.iterator()) { // Calculate distance between node and content - const d = distance(this.nodeId, this.contentId(fromHexString(key))) + const d = distance(this.nodeId, this.contentId(hexToBytes(key))) // If content is out of radius -- delete content if (d > radius) { // Before deleting BlockHeaderWithProof (0x00) -- Check if BlockHeaderByNumber contentKey is in radius @@ -192,7 +192,7 @@ export class NetworkDB { // If BOTH content keys are out of radius -- delete BlockHeaderWithProof. Add both keys to delete list (for gossip) if (numberDistance > radius) { - toDelete.push([toHexString(numberKey), value]) + toDelete.push([bytesToHex(numberKey), value]) toDelete.push([key, value]) await this.db.del(key) } diff --git a/packages/portalnetwork/src/networks/state/genesis.ts b/packages/portalnetwork/src/networks/state/genesis.ts index d3011cf18..2b3d1b89d 100644 --- a/packages/portalnetwork/src/networks/state/genesis.ts +++ b/packages/portalnetwork/src/networks/state/genesis.ts @@ -1,6 +1,11 @@ -import { fromHexString, toHexString } from '@chainsafe/ssz' import { LeafNode, Trie } from '@ethereumjs/trie' -import { Account, equalsBytes, parseGethGenesisState } from '@ethereumjs/util' +import { + Account, + bytesToHex, + equalsBytes, + hexToBytes, + parseGethGenesisState, +} from '@ethereumjs/util' import genesis from './mainnet.json' assert { type: 'json' } @@ -24,9 +29,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]) } - if (!equalsBytes(trie.root(), fromHexString(genesis.genesisStateRoot))) { + if (!equalsBytes(trie.root(), hexToBytes(genesis.genesisStateRoot))) { throw new Error('Invalid genesis state root') } return trie @@ -49,7 +54,7 @@ export const generateAccountTrieProofs = async (): Promise<{ ...(await trie.findPath(nodeHash)).stack.map((node) => node.serialize()), node.serialize(), ] - return [toHexString(nodeHash), { path, proof }] + return [bytesToHex(nodeHash), { path, proof }] }), ) const proofs = await Promise.all( @@ -57,7 +62,7 @@ export const generateAccountTrieProofs = async (): Promise<{ const nodeHash = trie['hash'](node.serialize()) const nodePath = await trie.findPath(nodeHash) const proof = [...nodePath.stack.map((node) => node.serialize()), node.serialize()] - const content = [toHexString(nodeHash), { path, proof }] + const content = [bytesToHex(nodeHash), { path, proof }] return content }), ) diff --git a/packages/portalnetwork/src/networks/state/portalTrie.ts b/packages/portalnetwork/src/networks/state/portalTrie.ts index e143b0367..907667bb9 100644 --- a/packages/portalnetwork/src/networks/state/portalTrie.ts +++ b/packages/portalnetwork/src/networks/state/portalTrie.ts @@ -1,4 +1,3 @@ -import { toHexString } from '@chainsafe/ssz' import { BranchNode, LeafNode, Trie } from '@ethereumjs/trie' import { bytesToUnprefixedHex } from '@ethereumjs/util' import { bytesToHex } from 'ethereum-cryptography/utils' @@ -30,7 +29,7 @@ export class PortalTrie { const keyobj = AccountTrieNodeContentKey.decode(key) if (request === undefined || !('content' in request)) { throw new Error( - `network doesn't have node [${unpackNibbles(keyobj.path)}]${toHexString(keyobj.nodeHash)}`, + `network doesn't have node [${unpackNibbles(keyobj.path)}]${bytesToHex(keyobj.nodeHash)}`, ) } const node = AccountTrieNodeRetrieval.deserialize(request.content).node @@ -42,7 +41,7 @@ export class PortalTrie { const keyobj = StorageTrieNodeContentKey.decode(key) if (request === undefined || !('content' in request)) { throw new Error( - `network doesn't have node [${unpackNibbles(keyobj.path)}]${toHexString(keyobj.nodeHash)}`, + `network doesn't have node [${unpackNibbles(keyobj.path)}]${bytesToHex(keyobj.nodeHash)}`, ) } const node = StorageTrieNodeRetrieval.deserialize(request.content).node @@ -65,7 +64,7 @@ export class PortalTrie { const lookup = new ContentLookup(this.state, rootNodeKey) const request = await lookup.startLookup() if (request === undefined || !('content' in request)) { - this.logger(`network doesn't have root node ${toHexString(stateroot)}`) + this.logger(`network doesn't have root node ${bytesToHex(stateroot)}`) return undefined } const node = AccountTrieNodeRetrieval.deserialize(request.content).node @@ -147,7 +146,7 @@ export class PortalTrie { const lookup = new ContentLookup(this.state, rootNodeKey) const request = await lookup.startLookup() if (request === undefined || !('content' in request)) { - throw new Error(`network doesn't have root node ${toHexString(storageRoot)}`) + throw new Error(`network doesn't have root node ${bytesToHex(storageRoot)}`) } const node = AccountTrieNodeRetrieval.deserialize(request.content).node this.logger.extend('findPath')(`RootNode found: (${node.length} bytes)`) diff --git a/packages/portalnetwork/src/networks/state/state.ts b/packages/portalnetwork/src/networks/state/state.ts index 08fd21ed2..be297ab16 100644 --- a/packages/portalnetwork/src/networks/state/state.ts +++ b/packages/portalnetwork/src/networks/state/state.ts @@ -1,8 +1,13 @@ import { distance } from '@chainsafe/discv5' import { ENR } from '@chainsafe/enr' -import { fromHexString, toHexString } from '@chainsafe/ssz' import { BranchNode, ExtensionNode, Trie, decodeNode } from '@ethereumjs/trie' -import { bytesToHex, bytesToInt, bytesToUnprefixedHex, equalsBytes } from '@ethereumjs/util' +import { + bytesToHex, + bytesToInt, + bytesToUnprefixedHex, + equalsBytes, + hexToBytes, +} from '@ethereumjs/util' import debug from 'debug' import { shortId } from '../../util/util.js' @@ -118,7 +123,7 @@ export class StateNetwork extends BaseNetwork { } case FoundContent.CONTENT: this.logger.extend(`FOUNDCONTENT`)( - `received ${StateNetworkContentType[contentType]} content corresponding to ${toHexString(key)}`, + `received ${StateNetworkContentType[contentType]} content corresponding to ${bytesToHex(key)}`, ) try { await this.store(key, decoded.value as Uint8Array, false) @@ -143,7 +148,7 @@ export class StateNetwork extends BaseNetwork { public findContentLocally = async (contentKey: Uint8Array): Promise => { try { const value = await this.db.get(contentKey) - return value !== undefined ? fromHexString(value) : undefined + return value !== undefined ? hexToBytes(value) : undefined } catch { return undefined } @@ -237,7 +242,7 @@ export class StateNetwork extends BaseNetwork { interested.push({ contentKey, dbContent }) this.manager.trie.db.local.set(bytesToUnprefixedHex(nodeHash), bytesToHex(contentKey)) } else { - notInterested.push({ contentKey, nodeHash: toHexString(nodeHash) }) + notInterested.push({ contentKey, nodeHash: bytesToHex(nodeHash) }) } curRlp = nodes.pop() @@ -339,7 +344,7 @@ export class StateNetwork extends BaseNetwork { interested.push({ contentKey, dbContent }) this.manager.trie.db.local.set(bytesToUnprefixedHex(nodeHash), bytesToHex(contentKey)) } else { - notInterested.push({ contentKey, nodeHash: toHexString(nodeHash) }) + notInterested.push({ contentKey, nodeHash: bytesToHex(nodeHash) }) } curRlp = nodes.pop() diff --git a/packages/portalnetwork/src/networks/state/util.ts b/packages/portalnetwork/src/networks/state/util.ts index c50830bf8..28d3ec534 100644 --- a/packages/portalnetwork/src/networks/state/util.ts +++ b/packages/portalnetwork/src/networks/state/util.ts @@ -1,8 +1,7 @@ import { digest as sha256 } from '@chainsafe/as-sha256' import { distance } from '@chainsafe/discv5' -import { fromHexString, toHexString } from '@chainsafe/ssz' import { BranchNode, ExtensionNode, decodeNode } from '@ethereumjs/trie' -import { bytesToUnprefixedHex, equalsBytes } from '@ethereumjs/util' +import { bytesToHex, bytesToUnprefixedHex, equalsBytes, hexToBytes } from '@ethereumjs/util' import { packNibbles, unpackNibbles } from './nibbleEncoding.js' import { @@ -104,7 +103,7 @@ export class StateNetworkContentId { export function wrapDBContent(contentKey: Uint8Array, dbContent: string) { const keytype = keyType(contentKey) - const dbBytes = fromHexString('0x' + dbContent) + const dbBytes = hexToBytes('0x' + dbContent) const wrapped = keytype === StateNetworkContentType.AccountTrieNode ? AccountTrieNodeRetrieval.serialize({ @@ -117,7 +116,7 @@ export function wrapDBContent(contentKey: Uint8Array, dbContent: string) { : ContractRetrieval.serialize({ code: dbBytes, }) - return toHexString(wrapped) + return bytesToHex(wrapped) } export function calculateAddressRange( diff --git a/packages/portalnetwork/src/util/discv5.ts b/packages/portalnetwork/src/util/discv5.ts index c0aa60cfd..7401beade 100644 --- a/packages/portalnetwork/src/util/discv5.ts +++ b/packages/portalnetwork/src/util/discv5.ts @@ -1,2 +1 @@ export { distance, IDiscv5CreateOptions, log2Distance } from '@chainsafe/discv5' -export { fromHexString, toHexString } from '@chainsafe/ssz' diff --git a/packages/portalnetwork/src/util/helpers.ts b/packages/portalnetwork/src/util/helpers.ts index c63095c79..4692c217b 100644 --- a/packages/portalnetwork/src/util/helpers.ts +++ b/packages/portalnetwork/src/util/helpers.ts @@ -5,8 +5,6 @@ import { VM } from '@ethereumjs/vm' import debug from 'debug' import { ethers } from 'ethers' -import { toHexString } from './index.js' - import type { Log, TxReceiptType } from '../networks/index.js' import type { BlockOptions, JsonRpcBlock, Block as ethJsBlock } from '@ethereumjs/block' import type { @@ -42,13 +40,13 @@ export async function getBlockReceipts( return new ethers.Log( { blockNumber: Number(block.header.number), - blockHash: toHexString(block.header.hash()), + blockHash: bytesToHex(block.header.hash()), transactionIndex: idx, removed: false, - address: toHexString(log[0]), - data: toHexString(log[2]), - topics: log[1].map((l) => toHexString(l)), - transactionHash: toHexString(block.transactions[idx].hash()), + address: bytesToHex(log[0]), + data: bytesToHex(log[2]), + topics: log[1].map((l) => bytesToHex(l)), + transactionHash: bytesToHex(block.transactions[idx].hash()), index: i, }, provider, @@ -62,12 +60,12 @@ export async function getBlockReceipts( index: idx, root: (r as PreByzantiumTxReceipt).stateRoot !== undefined - ? toHexString((r as PreByzantiumTxReceipt).stateRoot) + ? bytesToHex((r as PreByzantiumTxReceipt).stateRoot) : null, gasUsed: block.header.gasUsed, - logsBloom: toHexString(block.header.logsBloom), - blockHash: toHexString(block.hash()), - hash: toHexString(block.transactions[idx].hash()), + logsBloom: bytesToHex(block.header.logsBloom), + blockHash: bytesToHex(block.hash()), + hash: bytesToHex(block.transactions[idx].hash()), cumulativeGasUsed: r.cumulativeBlockGasUsed, logs, blockNumber: Number(block.header.number), @@ -108,16 +106,16 @@ export const ethJsBlockToEthersBlock = ( return new ethers.Block( { - hash: toHexString(block.hash()), - parentHash: toHexString(block.header.parentHash), + hash: bytesToHex(block.hash()), + parentHash: bytesToHex(block.header.parentHash), number: Number(block.header.number), timestamp: Number(block.header.timestamp), - nonce: toHexString(block.header.nonce), + nonce: bytesToHex(block.header.nonce), difficulty: block.header.difficulty, gasLimit: block.header.gasLimit, miner: block.header.coinbase.toString(), gasUsed: block.header.gasUsed, - extraData: toHexString(block.header.extraData), + extraData: bytesToHex(block.header.extraData), transactions: block.transactions.map((tx) => bytesToHex(tx.hash())), baseFeePerGas: block.header.baseFeePerGas ?? null, }, @@ -143,7 +141,7 @@ export const ethJsBlockToEthersBlockWithTxs = async ( for (const [_idx, tx] of Object.entries(block.transactions)) { const normedTx: ethers.TransactionResponse = new ethers.TransactionResponse( { - hash: toHexString(tx.hash()), + hash: bytesToHex(tx.hash()), signature: ethers.Signature.from({ r: tx.r?.toString() ?? '', s: tx.s?.toString() ?? '', @@ -154,7 +152,7 @@ export const ethJsBlockToEthersBlockWithTxs = async ( nonce: Number(tx.nonce), chainId: 1n, gasLimit: tx.gasLimit, - data: toHexString(tx.data), + data: bytesToHex(tx.data), value: tx.value, gasPrice: tx.type === 0 ? (tx as LegacyTransaction).gasPrice : 0n, maxFeePerGas: tx.type === 2 ? (tx as FeeMarketEIP1559Transaction).maxFeePerGas : null, @@ -173,21 +171,21 @@ export const ethJsBlockToEthersBlockWithTxs = async ( txns.push(normedTx) } return { - hash: toHexString(block.hash()), + hash: bytesToHex(block.hash()), transactions: txns, - parentHash: toHexString(block.header.parentHash), + parentHash: bytesToHex(block.header.parentHash), number: Number(block.header.number), timestamp: Number(block.header.timestamp), - nonce: toHexString(block.header.nonce), + nonce: bytesToHex(block.header.nonce), difficulty: block.header.difficulty, gasLimit: block.header.gasLimit, miner: block.header.coinbase.toString(), gasUsed: block.header.gasUsed, - extraData: toHexString(block.header.extraData), + extraData: bytesToHex(block.header.extraData), _difficulty: block.header.difficulty, - sha3Uncles: toHexString(block.header.uncleHash), - uncleHeaders: block.uncleHeaders.map((uncle) => toHexString(uncle.hash())), - stateRoot: toHexString(block.header.stateRoot), + sha3Uncles: bytesToHex(block.header.uncleHash), + uncleHeaders: block.uncleHeaders.map((uncle) => bytesToHex(uncle.hash())), + stateRoot: bytesToHex(block.header.stateRoot), transactionCount: txns.length, } } diff --git a/packages/portalnetwork/src/wire/utp/PortalNetworkUtp/ContentRequest.ts b/packages/portalnetwork/src/wire/utp/PortalNetworkUtp/ContentRequest.ts index 754887513..5db5f3c4a 100644 --- a/packages/portalnetwork/src/wire/utp/PortalNetworkUtp/ContentRequest.ts +++ b/packages/portalnetwork/src/wire/utp/PortalNetworkUtp/ContentRequest.ts @@ -9,7 +9,6 @@ import { PacketType, StateNetwork, bitmap, - toHexString, } from '../../../index.js' import { RequestCode } from './types.js' @@ -269,7 +268,7 @@ export class AcceptReadRequest extends ContentReadRequest { while (this.socket.reader!.contents.length > 0) { const key = this.contentKeys.shift()! const value = this.socket.reader!.contents.shift()! - this.logger(`Storing: ${toHexString(key)}. ${this.contentKeys.length} still streaming.`) + this.logger(`Storing: ${bytesToHex(key)}. ${this.contentKeys.length} still streaming.`) await this.returnContent([value], [key]) } } diff --git a/packages/portalnetwork/test/client/eth/ethCall.spec.ts b/packages/portalnetwork/test/client/eth/ethCall.spec.ts index 906065606..a4dab81b5 100644 --- a/packages/portalnetwork/test/client/eth/ethCall.spec.ts +++ b/packages/portalnetwork/test/client/eth/ethCall.spec.ts @@ -1,5 +1,5 @@ // import { SignableENR } from '@chainsafe/discv5' -// import { fromHexString, toHexString } from '@chainsafe/ssz' +// import { hexToBytes, bytesToHex } from '@chainsafe/ssz' // import { Block } from '@ethereumjs/block' // import { Trie } from '@ethereumjs/trie' // import { Account, Address, bytesToInt, bytesToUtf8, randomBytes } from '@ethereumjs/util' @@ -20,7 +20,7 @@ import { describe } from 'vitest' // const privateKeys = [ // '0x0a2700250802122102273097673a2948af93317235d2f02ad9cf3b79a34eeb37720c5f19e09f11783c12250802122102273097673a2948af93317235d2f02ad9cf3b79a34eeb37720c5f19e09f11783c1a2408021220aae0fff4ac28fdcdf14ee8ecb591c7f1bc78651206d86afe16479a63d9cb73bd', // ] -// const id1 = await createFromProtobuf(fromHexString(privateKeys[0])) +// const id1 = await createFromProtobuf(hexToBytes(privateKeys[0])) // const enr1 = SignableENR.createFromPeerId(id1) // const initMa = multiaddr(`/ip4/127.0.0.1/udp/0`) @@ -41,7 +41,7 @@ describe.skip('ethCall', () => { // // } // const greeterBytecode = // '0x608060405234801561000f575f80fd5b5060043610610034575f3560e01c80638da5cb5b14610038578063cfae321714610056575b5f80fd5b610040610074565b60405161004d9190610118565b60405180910390f35b61005e61009c565b60405161006b91906101bb565b60405180910390f35b5f60015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b60606040518060400160405280600581526020017f68656c6c6f000000000000000000000000000000000000000000000000000000815250905090565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f610102826100d9565b9050919050565b610112816100f8565b82525050565b5f60208201905061012b5f830184610109565b92915050565b5f81519050919050565b5f82825260208201905092915050565b5f5b8381101561016857808201518184015260208101905061014d565b5f8484015250505050565b5f601f19601f8301169050919050565b5f61018d82610131565b610197818561013b565b93506101a781856020860161014b565b6101b081610173565b840191505092915050565b5f6020820190508181035f8301526101d38184610183565b90509291505056fea2646970667358221220945519e237b301b5baf64c20c2a39b6a8b300541470b28b0e6cfbc1568dc6f3364736f6c63430008160033' - // const byteCode = fromHexString(greeterBytecode) + // const byteCode = hexToBytes(greeterBytecode) // const node = await PortalNetwork.create({ // transport: TransportLayer.NODE, // supportedNetworks: [NetworkId.StateNetwork, NetworkId.HistoryNetwork], @@ -83,17 +83,17 @@ describe.skip('ethCall', () => { // { setHardfork: true }, // ) // await addRLPSerializedBlock( - // toHexString(block.serialize()), - // toHexString(block.header.hash()), + // bytesToHex(block.serialize()), + // bytesToHex(block.header.hash()), // history, // ) - // await history.indexBlockHash(block.header.number, toHexString(block.header.hash())) + // await history.indexBlockHash(block.header.number, bytesToHex(block.header.hash())) // const greeterInput = '0xcfae3217' // const tx: RpcTx = { // to: address.toString(), // data: greeterInput, // } - // const res = fromHexString(await node.ETH.ethCall(tx, 15537394n)) + // const res = hexToBytes(await node.ETH.ethCall(tx, 15537394n)) // // Decode offset in `returnValue` for start of Solidity return value // const offset = bytesToInt(res.slice(0, 32)) // // First 32 bytes of return value are length of returned value diff --git a/packages/portalnetwork/test/client/eth/ethGetBalance.spec.ts b/packages/portalnetwork/test/client/eth/ethGetBalance.spec.ts index d20fa3c8f..95d88a645 100644 --- a/packages/portalnetwork/test/client/eth/ethGetBalance.spec.ts +++ b/packages/portalnetwork/test/client/eth/ethGetBalance.spec.ts @@ -5,8 +5,8 @@ import { describe } from 'vitest' // NetworkId, // PortalNetwork, // addRLPSerializedBlock, -// fromHexString, -// toHexString, +// hexToBytes, +// bytesToHex, // } from '../../../src/index.js' // import type { HistoryNetwork, StateNetwork } from '../../../src' @@ -21,7 +21,7 @@ describe.skip('ethGetBalance using HistoryNetwork and StateNetwork', async () => // const block = Block.fromRPC(testBlockData[blockNumber], undefined, { // setHardfork: true, // }) - // await addRLPSerializedBlock(toHexString(block.serialize()), toHexString(block.hash()), history) + // await addRLPSerializedBlock(bytesToHex(block.serialize()), bytesToHex(block.hash()), history) // const stored = await ultralight.ETH.getBlockByNumber(BigInt(blockNumber), true) // it(`should store block ${blockNumber}`, async () => { // assert.isDefined(stored) @@ -33,8 +33,8 @@ describe.skip('ethGetBalance using HistoryNetwork and StateNetwork', async () => // const contentKeys = Object.keys(block) // it(`should store ${contentKeys.length} pieces of content by key (block: ${blocksMeta[idx].blockNumber})`, async () => { // for await (const key of contentKeys) { - // const keyBytes = fromHexString(key) - // const storing = await state.stateDB.storeContent(keyBytes, fromHexString(block[key])) + // const keyBytes = hexToBytes(key) + // const storing = await state.stateDB.storeContent(keyBytes, hexToBytes(block[key])) // assert.isTrue(storing) // } // }) diff --git a/packages/portalnetwork/test/integration/beacon.spec.ts b/packages/portalnetwork/test/integration/beacon.spec.ts index 5524f2363..cb5f47a64 100644 --- a/packages/portalnetwork/test/integration/beacon.spec.ts +++ b/packages/portalnetwork/test/integration/beacon.spec.ts @@ -1,5 +1,5 @@ import { SignableENR } from '@chainsafe/enr' -import { bigIntToHex, concatBytes, hexToBytes, intToHex } from '@ethereumjs/util' +import { bigIntToHex, bytesToHex, concatBytes, hexToBytes, intToHex } from '@ethereumjs/util' import { keys } from '@libp2p/crypto' import { RunStatusCode } from '@lodestar/light-client' import { computeSyncPeriodAtSlot } from '@lodestar/light-client/utils' @@ -22,7 +22,6 @@ import { PortalNetwork, TransportLayer, getBeaconContentKey, - toHexString, } from '../../src/index.js' import type { BeaconLightClientNetwork } from '../../src/index.js' @@ -91,7 +90,7 @@ describe('Find Content tests', () => { const content = await network2.findContentLocally(hexToBytes(bootstrap.content_key)) assert.notOk(content === undefined, 'should retrieve content for bootstrap key') assert.equal( - toHexString(content!), + bytesToHex(content!), bootstrap.content_value, 'retrieved correct content for bootstrap', ) @@ -174,7 +173,7 @@ describe('Find Content tests', () => { ), ) assert.equal( - toHexString(res!['content']), + bytesToHex(res!['content']), optimisticUpdate.content_value, 'retrieved content for optimistic update from network', ) @@ -187,7 +186,7 @@ describe('Find Content tests', () => { assert.notOk(content === undefined, 'should retrieve content for optimistic update key') assert.equal( - toHexString(content!), + bytesToHex(content!), optimisticUpdate.content_value, 'retrieved correct content for optimistic update from local storage', ) @@ -244,7 +243,7 @@ describe('Find Content tests', () => { ) assert.equal( - toHexString(res!['content'] as Uint8Array), + bytesToHex(res!['content'] as Uint8Array), updatesByRange.content_value, 'retrieved content for light client updates by range from network', ) @@ -254,7 +253,7 @@ describe('Find Content tests', () => { 'should retrieve content for Light Client Update by Range key', ) assert.equal( - toHexString(content!), + bytesToHex(content!), updatesByRange.content_value, 'retrieved correct content for Light Client Update by Range from local storage', ) @@ -367,7 +366,7 @@ describe('OFFER/ACCEPT tests', () => { assert.notOk(content === undefined, 'should retrieve content for optimistic update key') assert.equal( - toHexString(content!), + bytesToHex(content!), optimisticUpdate.content_value, 'retrieved correct content for optimistic update from local storage', ) diff --git a/packages/portalnetwork/test/integration/eth/getBlockByNumber.spec.ts b/packages/portalnetwork/test/integration/eth/getBlockByNumber.spec.ts index 533704d54..e79e65723 100644 --- a/packages/portalnetwork/test/integration/eth/getBlockByNumber.spec.ts +++ b/packages/portalnetwork/test/integration/eth/getBlockByNumber.spec.ts @@ -1,6 +1,6 @@ import { SignableENR } from '@chainsafe/enr' import { Block } from '@ethereumjs/block' -import { concatBytes, hexToBytes } from '@ethereumjs/util' +import { bytesToHex, concatBytes, hexToBytes } from '@ethereumjs/util' import { privateKeyFromProtobuf } from '@libp2p/crypto/keys' import { RunStatusCode } from '@lodestar/light-client' import { ForkName } from '@lodestar/params' @@ -17,10 +17,8 @@ import { PortalNetwork, TransportLayer, addRLPSerializedBlock, - fromHexString, generatePreMergeHeaderProof, getBeaconContentKey, - toHexString, } from '../../../src/index.js' import type { BeaconLightClientNetwork, HistoryNetwork } from '../../../src/index.js' @@ -79,7 +77,7 @@ describe( const blockRlp = block1000.raw const blockHash = block1000.hash - const proof = await generatePreMergeHeaderProof(1000n, fromHexString(epoch)) + const proof = await generatePreMergeHeaderProof(1000n, hexToBytes(epoch)) await addRLPSerializedBlock(blockRlp, blockHash, network1, proof) await network1.sendPing(network2?.enr!.toENR()) const retrieved = await node2.ETH.getBlockByNumber(1000, false) @@ -155,14 +153,14 @@ describe('should find a block using "latest" and "finalized"', async () => { const finalizedBlockjson = require('../testdata/finalizedBlock.json') const finalizedBlock = Block.fromRPC(finalizedBlockjson.result, [], { setHardfork: true }) await addRLPSerializedBlock( - toHexString(headBlockblock.serialize()), - toHexString(headBlockblock.hash()), + bytesToHex(headBlockblock.serialize()), + bytesToHex(headBlockblock.hash()), history, [], ) await addRLPSerializedBlock( - toHexString(finalizedBlock.serialize()), - toHexString(finalizedBlock.hash()), + bytesToHex(finalizedBlock.serialize()), + bytesToHex(finalizedBlock.hash()), history, [], ) diff --git a/packages/portalnetwork/test/integration/integration.spec.ts b/packages/portalnetwork/test/integration/integration.spec.ts index 321b7713f..3e9c8c7a7 100644 --- a/packages/portalnetwork/test/integration/integration.spec.ts +++ b/packages/portalnetwork/test/integration/integration.spec.ts @@ -17,7 +17,6 @@ import { TransportLayer, addRLPSerializedBlock, getContentKey, - toHexString, } from '../../src/index.js' import type { HistoryNetwork } from '../../src/index.js' @@ -187,7 +186,7 @@ describe('FindContent', async () => { it('should find local with getBlockByNumber', () => { assert.equal( - toHexString(byNumber!.hash()), + bytesToHex(byNumber!.hash()), testBlockData[29].blockHash, 'retrieved expected header', ) @@ -218,7 +217,7 @@ describe('FindContent', async () => { void node2.stop() it('should find content', () => { assert.equal( - toHexString(header.hash()), + bytesToHex(header.hash()), testBlockData[29].blockHash, 'retrieved expected header', ) @@ -275,7 +274,7 @@ describe('eth_getBlockByHash', async () => { void node2.stop() it('should find content', () => { assert.equal( - toHexString(retrieved!.hash()), + bytesToHex(retrieved!.hash()), testBlockData[29].blockHash, 'retrieved expected header', ) diff --git a/packages/portalnetwork/test/integration/state.spec.ts b/packages/portalnetwork/test/integration/state.spec.ts index 4e1eeb386..4d475d917 100644 --- a/packages/portalnetwork/test/integration/state.spec.ts +++ b/packages/portalnetwork/test/integration/state.spec.ts @@ -11,7 +11,6 @@ import { NetworkId, PortalNetwork, TransportLayer, - fromHexString, } from '../../src/index.js' import samples from '../networks/state/testdata/accountNodeSamples.json' @@ -26,7 +25,7 @@ const privateKeys = [ const sample = samples[0] const [key, value] = sample as [string, object] const content = Uint8Array.from(Object.values(value)) -const contentKey = fromHexString(key) +const contentKey = hexToBytes(key) const deserialized = AccountTrieNodeOffer.deserialize(content) const { proof } = deserialized diff --git a/packages/portalnetwork/test/integration/stateGenesis.spec.ts b/packages/portalnetwork/test/integration/stateGenesis.spec.ts index 4bdf2e03a..9aa22efed 100644 --- a/packages/portalnetwork/test/integration/stateGenesis.spec.ts +++ b/packages/portalnetwork/test/integration/stateGenesis.spec.ts @@ -1,4 +1,4 @@ -import { fromHexString, toHexString } from '@chainsafe/ssz' +import { bytesToHex, hexToBytes } from '@chainsafe/ssz' import { Block } from '@ethereumjs/block' import { BranchNode, Trie } from '@ethereumjs/trie' import { Account, bytesToUnprefixedHex, padToEven } from '@ethereumjs/util' @@ -24,19 +24,19 @@ describe('genesisContent', async () => { let invalid = 0 for (const leaf of result.leafNodeContent) { const [key, value] = leaf - const contentKey = AccountTrieNodeContentKey.decode(fromHexString(key)) + const contentKey = AccountTrieNodeContentKey.decode(hexToBytes(key)) const deserialized = AccountTrieNodeOffer.deserialize(value) const r = t['hash'](deserialized.proof[0]) const l = t['hash'](deserialized.proof[deserialized.proof.length - 1]) try { assert.deepEqual( deserialized.blockHash, - fromHexString('0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3'), + hexToBytes('0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3'), ) assert.deepEqual(contentKey.nodeHash, l) assert.deepEqual( r, - fromHexString('0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544'), + hexToBytes('0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544'), ) valid++ } catch { @@ -52,12 +52,12 @@ describe('genesisContent', async () => { it('should have no invalid proofs for all accounts', async () => { expect(invalid).toEqual(0) }) - t.root(fromHexString('0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544')) + t.root(hexToBytes('0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544')) let fails = 0 let pass = 0 for await (const [add, bal] of Object.entries(mainnet.alloc)) { const address = '0x' + padToEven(add) - const accountVal = await t.get(fromHexString(address)) + const accountVal = await t.get(hexToBytes(address)) try { const account = Account.fromRlpSerializedAccount(accountVal!) expect(account.balance).toEqual(BigInt(bal.balance)) @@ -80,7 +80,7 @@ describe('genesisDevnet', async () => { const sortedNodeHashByClient = await populateGenesisDB(trie, networks) const hasRoot = Object.entries(sortedNodeHashByClient) .filter(([_, nodeHashes]) => - nodeHashes.map((h) => toHexString(h)).includes(toHexString(trie.root())), + nodeHashes.map((h) => bytesToHex(h)).includes(bytesToHex(trie.root())), ) .map(([nodeId, _]) => nodeId) it('should store root in some clients', () => { @@ -90,7 +90,7 @@ describe('genesisDevnet', async () => { const storedTrieNodes = Object.entries(sortedNodeHashByClient) .map(([_, trieNodes]) => { - return trieNodes.map((node) => toHexString(node)) + return trieNodes.map((node) => bytesToHex(node)) }) .flat() const uniqueStoredTrieNodes = Array.from(new Set(storedTrieNodes)) @@ -149,7 +149,7 @@ describe('execute Block 1', async () => { const sortedNodeHashByClient = await populateGenesisDB(trie, networks) const hasRoot = Object.entries(sortedNodeHashByClient) .filter(([_, nodeHashes]) => - nodeHashes.map((h) => toHexString(h)).includes(toHexString(trie.root())), + nodeHashes.map((h) => bytesToHex(h)).includes(bytesToHex(trie.root())), ) .map(([nodeId, _]) => nodeId) it('should store root in some clients', () => { @@ -159,7 +159,7 @@ describe('execute Block 1', async () => { const storedTrieNodes = Object.entries(sortedNodeHashByClient) .map(([_, trieNodes]) => { - return trieNodes.map((node) => toHexString(node)) + return trieNodes.map((node) => bytesToHex(node)) }) .flat() const uniqueStoredTrieNodes = Array.from(new Set(storedTrieNodes)) @@ -176,7 +176,7 @@ describe('execute Block 1', async () => { }) const block1raw = rawBlocks.block1raw - const block = Block.fromRLPSerializedBlock(fromHexString(block1raw), { setHardfork: true }) + const block = Block.fromRLPSerializedBlock(hexToBytes(block1raw), { setHardfork: true }) const runResult = await vm.runBlock({ block, @@ -220,8 +220,8 @@ describe('execute Block 1', async () => { it('should calculate new state root', async () => { assert.equal( - toHexString(prooftrie.root()), - toHexString(block.header.stateRoot), + bytesToHex(prooftrie.root()), + bytesToHex(block.header.stateRoot), 'successfully updates state from block', ) }) diff --git a/packages/portalnetwork/test/integration/util.ts b/packages/portalnetwork/test/integration/util.ts index 856b79431..dd95c46d1 100644 --- a/packages/portalnetwork/test/integration/util.ts +++ b/packages/portalnetwork/test/integration/util.ts @@ -1,11 +1,10 @@ import { distance } from '@chainsafe/discv5' import { SignableENR } from '@chainsafe/enr' -import { fromHexString, toHexString } from '@chainsafe/ssz' import { Chain, Common, Hardfork } from '@ethereumjs/common' import { getGenesis } from '@ethereumjs/genesis' import { DefaultStateManager } from '@ethereumjs/statemanager' import { ExtensionNode, Trie, decodeNode } from '@ethereumjs/trie' -import { bytesToUnprefixedHex, hexToBytes, padToEven } from '@ethereumjs/util' +import { bytesToHex, bytesToUnprefixedHex, hexToBytes, padToEven } from '@ethereumjs/util' import { VM } from '@ethereumjs/vm' import { privateKeyFromProtobuf } from '@libp2p/crypto/keys' import { multiaddr } from '@multiformats/multiaddr' @@ -95,19 +94,19 @@ export const genesisContent = async ( const nodeBytes = node.serialize() const node_hash = trie['hash'](nodeBytes) const nibbles = key.map((n) => n.toString(16)) as TNibble[] - nodePaths[toHexString(node_hash)] = { nodeBytes, nibbles } + nodePaths[bytesToHex(node_hash)] = { nodeBytes, nibbles } }) const proofs = await Promise.all( Object.keys(mainnet.alloc).map(async (add) => { const address = '0x' + padToEven(add) - const leafproof = await trie.createProof(fromHexString(address)) + const leafproof = await trie.createProof(hexToBytes(address)) return { address, leafproof } }), ) const leafProofs: [Uint8Array, { nibbles: string[]; proof: Uint8Array[] }][] = proofs.map( ({ address, leafproof }) => { - const addressPath = bytesToUnprefixedHex(trie['hash'](fromHexString(address))).split('') + const addressPath = bytesToUnprefixedHex(trie['hash'](hexToBytes(address))).split('') const nodePath: string[] = [] for (const p of leafproof.slice(0, -1)) { const node = decodeNode(p) @@ -128,7 +127,7 @@ export const genesisContent = async ( const allProofs: Record = {} for (const [_, { proof }] of leafProofs) { for (const [idx, n] of proof.slice(0, -1).entries()) { - const hash = toHexString(trie['hash'](n)) + const hash = bytesToHex(trie['hash'](n)) if (hash in allProofs) continue const nodeProof = proof.slice(0, idx + 1) const nodeNibbles: TNibble[] = nodePaths[hash].nibbles @@ -142,11 +141,9 @@ export const genesisContent = async ( nodeHash, path, } - const contentKey = toHexString(AccountTrieNodeContentKey.encode(key)) + const contentKey = bytesToHex(AccountTrieNodeContentKey.encode(key)) const content = AccountTrieNodeOffer.serialize({ - blockHash: fromHexString( - '0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3', - ), + blockHash: hexToBytes('0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3'), proof, }) return [contentKey, content] @@ -156,14 +153,12 @@ export const genesisContent = async ( ([nodeHash, { nibbles, proof }]) => { const path = packNibbles(nibbles 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: fromHexString( - '0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3', - ), + blockHash: hexToBytes('0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3'), proof, }) return [contentKey, content] @@ -188,11 +183,11 @@ export const sortedById = ( ) const allContent = [...leafNodeContent, ...trieNodeContent] for (const [contentKey] of allContent) { - const contentId = new Trie({ useKeyHashing: true })['hash'](fromHexString(contentKey)) + const contentId = new Trie({ useKeyHashing: true })['hash'](hexToBytes(contentKey)) for (const { nodeId, radius } of clients) { const d = distance(nodeId, bytesToUnprefixedHex(contentId)) if (d < radius) { - const { nodeHash } = AccountTrieNodeContentKey.decode(fromHexString(contentKey)) + const { nodeHash } = AccountTrieNodeContentKey.decode(hexToBytes(contentKey)) trieNodes[nodeId].push(nodeHash) } } @@ -214,7 +209,7 @@ export const populateGenesisDB = async (trie: Trie, networks: StateNetwork[]) => for (const nodeHash of nodeHashes) { await db.put( bytesToUnprefixedHex(nodeHash), - bytesToUnprefixedHex(nodePaths[toHexString(nodeHash)].nodeBytes), + bytesToUnprefixedHex(nodePaths[bytesToHex(nodeHash)].nodeBytes), ) } } diff --git a/packages/portalnetwork/test/networks/beacon/beacon.spec.ts b/packages/portalnetwork/test/networks/beacon/beacon.spec.ts index 3f097b13e..b9dec2712 100644 --- a/packages/portalnetwork/test/networks/beacon/beacon.spec.ts +++ b/packages/portalnetwork/test/networks/beacon/beacon.spec.ts @@ -1,5 +1,4 @@ import { SignableENR } from '@chainsafe/enr' -import { toHexString } from '@chainsafe/ssz' import { bytesToHex, concatBytes, hexToBytes, randomBytes } from '@ethereumjs/util' import { keys } from '@libp2p/crypto' import { ssz } from '@lodestar/types' @@ -172,7 +171,7 @@ describe('API tests', async () => { 6684738, 'put the correct update in the correct position in the range', ) - assert.equal(toHexString(range!), updatesByRange.content_value) + assert.equal(bytesToHex(range!), updatesByRange.content_value) expect(gossipSpy).toHaveBeenCalledTimes(0) // verifies that we don't gossip individual LightClientUpdates vi.clearAllMocks() }) @@ -185,7 +184,7 @@ describe('API tests', async () => { hexToBytes(updatesByRange.content_key).slice(1), ) assert.equal( - toHexString(reconstructedRange).slice(0, 20), + bytesToHex(reconstructedRange).slice(0, 20), updatesByRange.content_value.slice(0, 20), 'stored and reconstructed a LightClientUpdatesByRange object', ) diff --git a/packages/portalnetwork/test/networks/beacon/portalSpecTests.spec.ts b/packages/portalnetwork/test/networks/beacon/portalSpecTests.spec.ts index 063949646..cc3e5ed59 100644 --- a/packages/portalnetwork/test/networks/beacon/portalSpecTests.spec.ts +++ b/packages/portalnetwork/test/networks/beacon/portalSpecTests.spec.ts @@ -1,5 +1,4 @@ -import { toHexString } from '@chainsafe/ssz' -import { hexToBytes } from '@ethereumjs/util' +import { bytesToHex, hexToBytes } from '@ethereumjs/util' import { createBeaconConfig, defaultChainConfig } from '@lodestar/config' import { genesisData } from '@lodestar/config/networks' import { ssz } from '@lodestar/types' @@ -92,7 +91,7 @@ describe('portal network spec test vectors', () => { it('deserializes bootstrap key', () => { assert.equal( - toHexString(LightClientBootstrapKey.deserialize(bootstrapKey).blockHash), + bytesToHex(LightClientBootstrapKey.deserialize(bootstrapKey).blockHash), '0xbd9f42d9a42d972bdaf4dee84e5b419dd432b52867258acb7bcc7f567b6e3af1', 'deserialized light client bootstrap key', ) diff --git a/packages/portalnetwork/test/networks/beacon/types.spec.ts b/packages/portalnetwork/test/networks/beacon/types.spec.ts index bdfc5a83c..cee899f9e 100644 --- a/packages/portalnetwork/test/networks/beacon/types.spec.ts +++ b/packages/portalnetwork/test/networks/beacon/types.spec.ts @@ -1,5 +1,4 @@ -import { toHexString } from '@chainsafe/ssz' -import { hexToBytes } from '@ethereumjs/util' +import { bytesToHex, hexToBytes } from '@ethereumjs/util' import { createBeaconConfig, defaultChainConfig } from '@lodestar/config' import { genesisData } from '@lodestar/config/networks' import { ssz } from '@lodestar/types' @@ -131,7 +130,7 @@ describe('Beacon network type tests using portal network spec test vectors', () it('deserializes bootstrap key', () => { assert.equal( - toHexString(LightClientBootstrapKey.deserialize(bootstrapKey).blockHash), + bytesToHex(LightClientBootstrapKey.deserialize(bootstrapKey).blockHash), '0xbd9f42d9a42d972bdaf4dee84e5b419dd432b52867258acb7bcc7f567b6e3af1', 'deserialized light client bootstrap key', ) diff --git a/packages/portalnetwork/test/networks/history/blockHeaderByNumber.spec.ts b/packages/portalnetwork/test/networks/history/blockHeaderByNumber.spec.ts index 8d1b708a8..5fc7d8c64 100644 --- a/packages/portalnetwork/test/networks/history/blockHeaderByNumber.spec.ts +++ b/packages/portalnetwork/test/networks/history/blockHeaderByNumber.spec.ts @@ -10,14 +10,13 @@ import { NetworkId, PortalNetwork, decodeHistoryNetworkContentKey, - fromHexString, } from '../../../src/index.js' import testdata from './testData/headerWithProof.json' describe('Retrieve Block Header By Number', async () => { - const HWP1000001 = fromHexString(testdata[1000001].content_value) - const HWP1000002 = fromHexString(testdata[1000002].content_value) + const HWP1000001 = hexToBytes(testdata[1000001].content_value) + const HWP1000002 = hexToBytes(testdata[1000002].content_value) const header100001 = BlockHeaderWithProof.deserialize(HWP1000001).header const header100002 = BlockHeaderWithProof.deserialize(HWP1000002).header @@ -58,11 +57,11 @@ describe('Retrieve Block Header By Number', async () => { }) it('Should retrieve block header by hash', async () => { - const header = await history.getBlockHeaderFromDB({ blockHash: fromHexString(hash100001) }) + const header = await history.getBlockHeaderFromDB({ blockHash: hexToBytes(hash100001) }) assert.deepEqual(header, header100001) }) it('Should retrieve block header by hash', async () => { - const header = await history.getBlockHeaderFromDB({ blockHash: fromHexString(hash100002) }) + const header = await history.getBlockHeaderFromDB({ blockHash: hexToBytes(hash100002) }) assert.deepEqual(header, header100002) }) diff --git a/packages/portalnetwork/test/networks/history/blockIndex.spec.ts b/packages/portalnetwork/test/networks/history/blockIndex.spec.ts index ccf984a80..e7f69b623 100644 --- a/packages/portalnetwork/test/networks/history/blockIndex.spec.ts +++ b/packages/portalnetwork/test/networks/history/blockIndex.spec.ts @@ -1,4 +1,3 @@ -import { fromHexString } from '@chainsafe/ssz' import { hexToBytes } from '@ethereumjs/util' import { assert, describe, it } from 'vitest' @@ -24,8 +23,8 @@ describe('BlockIndex', async () => { supportedNetworks: [{ networkId: NetworkId.HistoryNetwork }], }) const history = ultralight.networks.get(NetworkId.HistoryNetwork) as HistoryNetwork - const headerKey = getContentKey(HistoryNetworkContentType.BlockHeader, fromHexString(hash)) - await history.store(headerKey, fromHexString(headerWithProof)) + const headerKey = getContentKey(HistoryNetworkContentType.BlockHeader, hexToBytes(hash)) + await history.store(headerKey, hexToBytes(headerWithProof)) const stored = await history.get(headerWithProofkey) it('should store block header', () => { diff --git a/packages/portalnetwork/test/networks/history/contentKeys.spec.ts b/packages/portalnetwork/test/networks/history/contentKeys.spec.ts index 451e16aa0..1ee30a2f9 100644 --- a/packages/portalnetwork/test/networks/history/contentKeys.spec.ts +++ b/packages/portalnetwork/test/networks/history/contentKeys.spec.ts @@ -5,7 +5,6 @@ import { assert, describe, it } from 'vitest' import { HistoryNetworkContentType, decodeHistoryNetworkContentKey, - fromHexString, getContentId, getContentKey, } from '../../../src/index.js' @@ -17,12 +16,12 @@ describe('ContentKey and ContentId', () => { it('block header', () => { const { blockHash, contentKeyHex, contentIdHex } = testVectors.blockHeader const contentKey = getContentKey(HistoryNetworkContentType.BlockHeader, hexToBytes(blockHash)) - const contentId = getContentId(HistoryNetworkContentType.BlockHeader, fromHexString(blockHash)) + const contentId = getContentId(HistoryNetworkContentType.BlockHeader, hexToBytes(blockHash)) const decoded = decodeHistoryNetworkContentKey(contentKey) assert.equal(bytesToHex(contentKey), contentKeyHex, 'encoded content key') assert.equal(contentId, contentIdHex, 'encoded content id') - assert.deepEqual(decoded.keyOpt, fromHexString(blockHash), 'decoded hash from content key') + assert.deepEqual(decoded.keyOpt, hexToBytes(blockHash), 'decoded hash from content key') assert.equal( decoded.contentType, HistoryNetworkContentType.BlockHeader, @@ -32,12 +31,12 @@ describe('ContentKey and ContentId', () => { it('block body', () => { const { blockHash, contentKeyHex, contentIdHex } = testVectors.blockBody const contentKey = getContentKey(HistoryNetworkContentType.BlockBody, hexToBytes(blockHash)) - const contentId = getContentId(HistoryNetworkContentType.BlockBody, fromHexString(blockHash)) + const contentId = getContentId(HistoryNetworkContentType.BlockBody, hexToBytes(blockHash)) const decoded = decodeHistoryNetworkContentKey(contentKey) assert.equal(bytesToHex(contentKey), contentKeyHex, 'encoded content key') assert.equal(contentId, contentIdHex, 'encoded content id') - assert.deepEqual(decoded.keyOpt, fromHexString(blockHash), 'decoded hash from content key') + assert.deepEqual(decoded.keyOpt, hexToBytes(blockHash), 'decoded hash from content key') assert.equal( decoded.contentType, HistoryNetworkContentType.BlockBody, @@ -47,12 +46,12 @@ describe('ContentKey and ContentId', () => { it('receipts', () => { const { blockHash, contentKeyHex, contentIdHex } = testVectors.receipts const contentKey = getContentKey(HistoryNetworkContentType.Receipt, hexToBytes(blockHash)) - const contentId = getContentId(HistoryNetworkContentType.Receipt, fromHexString(blockHash)) + const contentId = getContentId(HistoryNetworkContentType.Receipt, hexToBytes(blockHash)) const decoded = decodeHistoryNetworkContentKey(contentKey) assert.equal(bytesToHex(contentKey), contentKeyHex, 'encoded content key') assert.equal(contentId, contentIdHex, 'encoded content id') - assert.deepEqual(decoded.keyOpt, fromHexString(blockHash), 'decoded hash from content key') + assert.deepEqual(decoded.keyOpt, hexToBytes(blockHash), 'decoded hash from content key') assert.equal( decoded.contentType, HistoryNetworkContentType.Receipt, diff --git a/packages/portalnetwork/test/networks/history/headerProof.spec.ts b/packages/portalnetwork/test/networks/history/headerProof.spec.ts index 4124e91f4..498b53cb3 100644 --- a/packages/portalnetwork/test/networks/history/headerProof.spec.ts +++ b/packages/portalnetwork/test/networks/history/headerProof.spec.ts @@ -1,7 +1,6 @@ import { ProofType, createProof } from '@chainsafe/persistent-merkle-tree' -import { toHexString } from '@chainsafe/ssz' import { BlockHeader } from '@ethereumjs/block' -import { hexToBytes } from '@ethereumjs/util' +import { bytesToHex, hexToBytes } from '@ethereumjs/util' import { createChainForkConfig } from '@lodestar/config' import { ssz } from '@lodestar/types' import { readFileSync } from 'fs' @@ -56,19 +55,19 @@ describe('Pre-Merge Header Record Proof tests', () => { it('Test Data is valid', () => { assert.equal(historicalEpochs.length, 1897, 'Accumulator contains 1897 historical epochs') assert.equal( - toHexString(EpochAccumulator.hashTreeRoot(epoch)), - toHexString(historicalEpochs[0]), + bytesToHex(EpochAccumulator.hashTreeRoot(epoch)), + bytesToHex(historicalEpochs[0]), 'Header Accumulator contains hash tree root of stored Epoch Accumulator', ) const hashes = [...epoch.values()].map((headerRecord) => { - return toHexString(headerRecord.blockHash) + return bytesToHex(headerRecord.blockHash) }) assert.equal( - toHexString(header.hash()), + bytesToHex(header.hash()), block1000.hash, 'Successfully created BlockHeader from stored bytes', ) - assert.ok(hashes.includes(toHexString(header.hash())), 'Header is a part of EpochAccumulator') + assert.ok(hashes.includes(bytesToHex(header.hash())), 'Header is a part of EpochAccumulator') }) it('Epoch Accumulator can create proof for header record.', () => { @@ -89,15 +88,15 @@ describe('Pre-Merge Header Record Proof tests', () => { ) assert.deepEqual( { - blockHash: toHexString(headerRecord.blockHash), + blockHash: bytesToHex(headerRecord.blockHash), totalDifficulty: headerRecord.totalDifficulty, }, headerRecord1000, 'HeaderRecord found located in Epoch Accumulator Tree by gIndex', ) assert.equal( - toHexString(headerRecord.blockHash), - toHexString(header.hash()), + bytesToHex(headerRecord.blockHash), + bytesToHex(header.hash()), 'HeadeRecord blockHash matches blockHeader', ) @@ -106,7 +105,7 @@ describe('Pre-Merge Header Record Proof tests', () => { gindex: gIndex, }) as SingleProof assert.equal( - toHexString(proof.leaf), + bytesToHex(proof.leaf), headerRecord1000.blockHash, 'Successfully created a Proof for Header Record', ) @@ -133,8 +132,8 @@ describe('Pre-Merge Header Record Proof tests', () => { const leaf = reconstructedEpoch.get(1000) assert.ok(true, 'SSZ Tree has a leaf at the expected index') assert.equal( - toHexString(leaf.hashTreeRoot()), - toHexString(HeaderRecordType.hashTreeRoot(headerRecord)), + bytesToHex(leaf.hashTreeRoot()), + bytesToHex(HeaderRecordType.hashTreeRoot(headerRecord)), 'Leaf contains correct Header Record', ) } catch { diff --git a/packages/portalnetwork/test/networks/history/historyNetwork.spec.ts b/packages/portalnetwork/test/networks/history/historyNetwork.spec.ts index 32676778f..013162243 100644 --- a/packages/portalnetwork/test/networks/history/historyNetwork.spec.ts +++ b/packages/portalnetwork/test/networks/history/historyNetwork.spec.ts @@ -2,7 +2,7 @@ import { EntryStatus } from '@chainsafe/discv5' import { ENR } from '@chainsafe/enr' import { Block, type BlockBytes, BlockHeader } from '@ethereumjs/block' import * as RLP from '@ethereumjs/rlp' -import { concatBytes, hexToBytes } from '@ethereumjs/util' +import { bytesToHex, concatBytes, hexToBytes } from '@ethereumjs/util' import { readFileSync } from 'fs' import { createRequire } from 'module' import * as td from 'testdouble' @@ -21,7 +21,6 @@ import { getContentKey, reassembleBlock, sszEncodeBlockBody, - toHexString, } from '../../../src/index.js' import type { HistoryNetwork } from '../../../src/index.js' @@ -204,7 +203,7 @@ describe('Header Tests', async () => { }) try { const res = network.validateHeader(serializedHeaderWithProof, { - blockHash: toHexString(header.hash()), + blockHash: bytesToHex(header.hash()), }) assert.ok(res, 'validated post-merge proof') } catch (err: any) { diff --git a/packages/portalnetwork/test/networks/history/portalSpecTests.spec.ts b/packages/portalnetwork/test/networks/history/portalSpecTests.spec.ts index 5e3347440..43e3d28aa 100644 --- a/packages/portalnetwork/test/networks/history/portalSpecTests.spec.ts +++ b/packages/portalnetwork/test/networks/history/portalSpecTests.spec.ts @@ -12,7 +12,6 @@ import { EpochAccumulator, HistoricalHashesAccumulator, decodeHistoryNetworkContentKey, - fromHexString, } from '../../../src/index.js' describe('Accumulator spec tests', () => { @@ -51,7 +50,7 @@ describe('block body tests', () => { }, ), ) as any - const key = decodeHistoryNetworkContentKey(fromHexString(testVector.content_key)) + const key = decodeHistoryNetworkContentKey(hexToBytes(testVector.content_key)) assert.equal(key.contentType, 1) const body = BlockBodyContentType.deserialize(hexToBytes(testVector.content_value)) const tx0Hash = bytesToHex( diff --git a/packages/portalnetwork/test/networks/history/types.spec.ts b/packages/portalnetwork/test/networks/history/types.spec.ts index cd10c71cf..f3ebe77d2 100644 --- a/packages/portalnetwork/test/networks/history/types.spec.ts +++ b/packages/portalnetwork/test/networks/history/types.spec.ts @@ -1,5 +1,5 @@ import { ProofType } from '@chainsafe/persistent-merkle-tree' -import { ContainerType, UintBigintType, fromHexString, toHexString } from '@chainsafe/ssz' +import { ContainerType, UintBigintType } from '@chainsafe/ssz' import { BlockHeader } from '@ethereumjs/block' import { bytesToHex, concatBytes, hexToBytes, randomBytes } from '@ethereumjs/util' import { readFileSync } from 'fs' @@ -30,9 +30,9 @@ describe('History Subnetwork contentKey serialization/deserialization', () => { let encodedKey = ContentKeyType.serialize( concatBytes(Uint8Array.from([HistoryNetworkContentType.BlockHeader]), hexToBytes(blockHash)), ) - let contentId = getContentId(HistoryNetworkContentType.BlockHeader, fromHexString(blockHash)) + let contentId = getContentId(HistoryNetworkContentType.BlockHeader, hexToBytes(blockHash)) assert.equal( - toHexString(encodedKey), + bytesToHex(encodedKey), '0x00d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d', 'blockheader content key equals expected output', ) @@ -45,9 +45,9 @@ describe('History Subnetwork contentKey serialization/deserialization', () => { encodedKey = ContentKeyType.serialize( concatBytes(Uint8Array.from([HistoryNetworkContentType.BlockBody]), hexToBytes(blockHash)), ) - contentId = getContentId(HistoryNetworkContentType.BlockBody, fromHexString(blockHash)) + contentId = getContentId(HistoryNetworkContentType.BlockBody, hexToBytes(blockHash)) assert.equal( - toHexString(encodedKey), + bytesToHex(encodedKey), '0x01d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d', 'blockbody content key equals expected output', ) @@ -60,9 +60,9 @@ describe('History Subnetwork contentKey serialization/deserialization', () => { encodedKey = ContentKeyType.serialize( concatBytes(Uint8Array.from([HistoryNetworkContentType.Receipt]), hexToBytes(blockHash)), ) - contentId = getContentId(HistoryNetworkContentType.Receipt, fromHexString(blockHash)) + contentId = getContentId(HistoryNetworkContentType.Receipt, hexToBytes(blockHash)) assert.equal( - toHexString(encodedKey), + bytesToHex(encodedKey), '0x02d1c390624d3bd4e409a61a858e5dcc5517729a9170d014a6c96530d64dd8621d', 'receipt content key equals expected output', ) @@ -275,11 +275,11 @@ describe('Header With Proof serialization/deserialization tests', async () => { hexToBytes(epochHash), ) it('should validate', async () => { - assert.ok(proof, `proof is valid: ${toHexString(proof.hashTreeRoot())}`) + assert.ok(proof, `proof is valid: ${bytesToHex(proof.hashTreeRoot())}`) }) it('should match epoch hash', async () => { assert.equal( - toHexString(EpochAccumulator.hashTreeRoot(actual_Epoch)), + bytesToHex(EpochAccumulator.hashTreeRoot(actual_Epoch)), epochHash, 'stored epoch hash matches valid epoch', ) @@ -310,7 +310,7 @@ describe('Header With Proof serialization/deserialization tests', async () => { ) assert.ok( history.validateHeader(serializedBlock1, { - blockHash: toHexString(deserializedHeader.hash()), + blockHash: bytesToHex(deserializedHeader.hash()), }), ) }) diff --git a/packages/portalnetwork/test/networks/history/util.spec.ts b/packages/portalnetwork/test/networks/history/util.spec.ts index 2f7639926..b4ac89c50 100644 --- a/packages/portalnetwork/test/networks/history/util.spec.ts +++ b/packages/portalnetwork/test/networks/history/util.spec.ts @@ -1,4 +1,3 @@ -import { fromHexString, toHexString } from '@chainsafe/ssz' import { Block, BlockHeader } from '@ethereumjs/block' import { Common, Hardfork } from '@ethereumjs/common' import { KECCAK256_RLP, bytesToHex, concatBytes, hexToBytes } from '@ethereumjs/util' @@ -28,7 +27,7 @@ describe('utility functions', () => { ) it('contentId functions', () => { assert.equal( - getContentId(HistoryNetworkContentType.BlockHeader, fromHexString(block1Hash)), + getContentId(HistoryNetworkContentType.BlockHeader, hexToBytes(block1Hash)), serializedContentKeyToContentId(block1headerContentKey), 'produced same content id', ) @@ -51,24 +50,24 @@ describe('utility functions', () => { }) it('epochRootByBlocknumber', () => { assert.equal( - toHexString(epochRootByBlocknumber(1000n)!), + bytesToHex(epochRootByBlocknumber(1000n)!), '0x5ec1ffb8c3b146f42606c74ced973dc16ec5a107c0345858c343fc94780b4218', 'epochRootByBlocknumber returned correct epoch root', ) assert.equal( - toHexString(epochRootByIndex(0)!), + bytesToHex(epochRootByIndex(0)!), '0x5ec1ffb8c3b146f42606c74ced973dc16ec5a107c0345858c343fc94780b4218', 'epochRootByIndex returned correct epoch root', ) }) it('epochRootByIndex', () => { assert.equal( - toHexString(epochRootByBlocknumber(9192n)!), + bytesToHex(epochRootByBlocknumber(9192n)!), '0xa5364e9a9bc513c4601f0d62e6b46dbdedf3200bbfae54d6350f46f2c7a01938', 'epochRootByBlocknumber returned correct epoch root', ) assert.equal( - toHexString(epochRootByIndex(1)!), + bytesToHex(epochRootByIndex(1)!), '0xa5364e9a9bc513c4601f0d62e6b46dbdedf3200bbfae54d6350f46f2c7a01938', 'epochRootByIndex returned correct epoch root', ) diff --git a/packages/portalnetwork/test/networks/state/accountTrieNode.spec.ts b/packages/portalnetwork/test/networks/state/accountTrieNode.spec.ts index 118390d6c..879ccf159 100644 --- a/packages/portalnetwork/test/networks/state/accountTrieNode.spec.ts +++ b/packages/portalnetwork/test/networks/state/accountTrieNode.spec.ts @@ -10,7 +10,6 @@ import { PortalNetwork, StateNetworkContentId, distance, - fromHexString, } from '../../../src/index.js' import samples from './testdata/accountNodeSamples.json' @@ -21,7 +20,7 @@ describe('samples', () => { const _samples = samples as [string, object][] for (const [key, value] of _samples) { const contentBytes = Uint8Array.from(Object.values(value)) - const contentKeyBytes = fromHexString(key) + const contentKeyBytes = hexToBytes(key) const contentKey = AccountTrieNodeContentKey.decode(contentKeyBytes) it('should decode sample key', () => { expect(contentKey.path).toBeDefined() @@ -59,7 +58,7 @@ describe('StateNetwork AccountTrieNode Gossip', async () => { const sample = samples.slice(-1)[0] const [key, value] = sample as [string, object] const contentBytes = Uint8Array.from(Object.values(value)) - const contentKeyBytes = fromHexString(key) + const contentKeyBytes = hexToBytes(key) const contentKey = AccountTrieNodeContentKey.decode(contentKeyBytes) const content = AccountTrieNodeOffer.deserialize(contentBytes) const { path } = contentKey diff --git a/packages/portalnetwork/test/networks/state/content.spec.ts b/packages/portalnetwork/test/networks/state/content.spec.ts index 29e46845d..631e06566 100644 --- a/packages/portalnetwork/test/networks/state/content.spec.ts +++ b/packages/portalnetwork/test/networks/state/content.spec.ts @@ -1,3 +1,4 @@ +import { bytesToHex, hexToBytes } from '@ethereumjs/util' import { keccak256 } from 'ethereum-cryptography/keccak.js' import { readFileSync } from 'fs' import yaml from 'js-yaml' @@ -14,9 +15,7 @@ import { StateNetworkContentId, StorageTrieNodeContentKey, StorageTrieNodeOffer, - fromHexString, packNibbles, - toHexString, } from '../../../src/index.js' describe('Account Trie Node', async () => { @@ -44,25 +43,25 @@ describe('Account Trie Node', async () => { ) as any it('serializes a content key', () => { const contentKey = AccountTrieNodeContentKey.encode({ - nodeHash: fromHexString(keyData.node_hash), + nodeHash: hexToBytes(keyData.node_hash), path: packNibbles(keyData.path.map((x) => x.toString(16))), }) - assert.equal(keyData.content_key, toHexString(contentKey)) + assert.equal(keyData.content_key, bytesToHex(contentKey)) }) it('decodes a content key', () => { - const decoded = AccountTrieNodeContentKey.decode(fromHexString(keyData.content_key)) - assert.equal(toHexString(decoded.nodeHash), keyData.node_hash) + const decoded = AccountTrieNodeContentKey.decode(hexToBytes(keyData.content_key)) + assert.equal(bytesToHex(decoded.nodeHash), keyData.node_hash) assert.deepEqual(decoded, { - nodeHash: fromHexString(keyData.node_hash), + nodeHash: hexToBytes(keyData.node_hash), path: packNibbles(keyData.path.map((x) => x.toString(16))), }) }) it('creates the correct content id', () => { const contentId = StateNetworkContentId.fromKeyObj({ - nodeHash: fromHexString(keyData.node_hash), + nodeHash: hexToBytes(keyData.node_hash), path: packNibbles(keyData.path.map((x) => x.toString(16))), }) - assert.equal(keyData.content_id, toHexString(contentId)) + assert.equal(keyData.content_id, bytesToHex(contentId)) }) const nodeWithProofData: AccountTrieNodeData = yaml.load( @@ -78,10 +77,10 @@ describe('Account Trie Node', async () => { ) as any it('serializes a content value', () => { const serialized = AccountTrieNodeOffer.serialize({ - blockHash: fromHexString(nodeWithProofData.block_hash), - proof: nodeWithProofData.proof.map((x) => fromHexString(x)), + blockHash: hexToBytes(nodeWithProofData.block_hash), + proof: nodeWithProofData.proof.map((x) => hexToBytes(x)), }) - assert.equal(nodeWithProofData.content_value, toHexString(serialized)) + assert.equal(nodeWithProofData.content_value, bytesToHex(serialized)) }) const nodeData = yaml.load( readFileSync( @@ -96,9 +95,9 @@ describe('Account Trie Node', async () => { ) as any it('serializes a content value without proof', () => { const serialized = AccountTrieNodeRetrieval.serialize({ - node: fromHexString(nodeData.trie_node), + node: hexToBytes(nodeData.trie_node), }) - assert.equal(nodeData.content_value, toHexString(serialized)) + assert.equal(nodeData.content_value, bytesToHex(serialized)) }) }) @@ -121,27 +120,27 @@ describe('Contract ByteCode', async () => { ), ) as any it('serializes a content key', () => { - const addressHash = keccak256(fromHexString(keyData.address)) + const addressHash = keccak256(hexToBytes(keyData.address)) const contentKey = ContractCodeContentKey.encode({ addressHash, - codeHash: fromHexString(keyData.code_hash), + codeHash: hexToBytes(keyData.code_hash), }) - assert.equal(keyData.content_key, toHexString(contentKey)) + assert.equal(keyData.content_key, bytesToHex(contentKey)) const contentId = StateNetworkContentId.fromKeyObj({ addressHash, - codeHash: fromHexString(keyData.code_hash), + codeHash: hexToBytes(keyData.code_hash), }) - assert.equal(keyData.content_id, toHexString(contentId)) + assert.equal(keyData.content_id, bytesToHex(contentId)) }) it('decodes a content key', () => { - const decoded = ContractCodeContentKey.decode(fromHexString(keyData.content_key)) + const decoded = ContractCodeContentKey.decode(hexToBytes(keyData.content_key)) assert.equal( - toHexString(decoded.addressHash), - toHexString(keccak256(fromHexString(keyData.address))), + bytesToHex(decoded.addressHash), + bytesToHex(keccak256(hexToBytes(keyData.address))), ) assert.deepEqual(decoded, { - addressHash: keccak256(fromHexString(keyData.address)), - codeHash: fromHexString(keyData.code_hash), + addressHash: keccak256(hexToBytes(keyData.address)), + codeHash: hexToBytes(keyData.code_hash), }) }) const contractByteCodeWithProofData = yaml.load( @@ -157,13 +156,11 @@ describe('Contract ByteCode', async () => { ) as any it('serializes a content value', () => { const serialized = ContractCodeOffer.serialize({ - accountProof: contractByteCodeWithProofData.account_proof.map((x: string) => - fromHexString(x), - ), - blockHash: fromHexString(contractByteCodeWithProofData.block_hash), - code: fromHexString(contractByteCodeWithProofData.bytecode), + accountProof: contractByteCodeWithProofData.account_proof.map((x: string) => hexToBytes(x)), + blockHash: hexToBytes(contractByteCodeWithProofData.block_hash), + code: hexToBytes(contractByteCodeWithProofData.bytecode), }) - assert.equal(contractByteCodeWithProofData.content_value, toHexString(serialized)) + assert.equal(contractByteCodeWithProofData.content_value, bytesToHex(serialized)) }) const contractByteCodeData = yaml.load( readFileSync( @@ -178,9 +175,9 @@ describe('Contract ByteCode', async () => { ) as any it('serializes a content value without proof', () => { const serialized = ContractRetrieval.serialize({ - code: fromHexString(contractByteCodeData.bytecode), + code: hexToBytes(contractByteCodeData.bytecode), }) - assert.equal(contractByteCodeData.content_value, toHexString(serialized)) + assert.equal(contractByteCodeData.content_value, bytesToHex(serialized)) }) }) @@ -197,21 +194,21 @@ describe('Storage Trie Node', async () => { ), ) as any it('serializes a content key', () => { - const addressHash = keccak256(fromHexString(storageTrieNodeKeyData.address)) + const addressHash = keccak256(hexToBytes(storageTrieNodeKeyData.address)) const contentKey = StorageTrieNodeContentKey.encode({ addressHash, - nodeHash: fromHexString(storageTrieNodeKeyData.node_hash), + nodeHash: hexToBytes(storageTrieNodeKeyData.node_hash), path: packNibbles(storageTrieNodeKeyData.path.map((x: number) => x.toString(16))), }) - assert.equal(storageTrieNodeKeyData.content_key, toHexString(contentKey)) + assert.equal(storageTrieNodeKeyData.content_key, bytesToHex(contentKey)) }) it('finds a content id', () => { const contentId = StateNetworkContentId.fromKeyObj({ - addressHash: keccak256(fromHexString(storageTrieNodeKeyData.address)), - nodeHash: fromHexString(storageTrieNodeKeyData.node_hash), + addressHash: keccak256(hexToBytes(storageTrieNodeKeyData.address)), + nodeHash: hexToBytes(storageTrieNodeKeyData.node_hash), path: packNibbles(storageTrieNodeKeyData.path.map((x: number) => x.toString(16))), }) - assert.equal(storageTrieNodeKeyData.content_id, toHexString(contentId)) + assert.equal(storageTrieNodeKeyData.content_id, bytesToHex(contentId)) }) const storageTrieNodeData = yaml.load( readFileSync( @@ -226,10 +223,10 @@ describe('Storage Trie Node', async () => { ) as any it('serializes a content value', () => { const serialized = StorageTrieNodeOffer.serialize({ - accountProof: storageTrieNodeData.account_proof.map((x: string) => fromHexString(x)), - blockHash: fromHexString(storageTrieNodeData.block_hash), - storageProof: storageTrieNodeData.storage_proof.map((x: string) => fromHexString(x)), + accountProof: storageTrieNodeData.account_proof.map((x: string) => hexToBytes(x)), + blockHash: hexToBytes(storageTrieNodeData.block_hash), + storageProof: storageTrieNodeData.storage_proof.map((x: string) => hexToBytes(x)), }) - assert.equal(storageTrieNodeData.content_value, toHexString(serialized)) + assert.equal(storageTrieNodeData.content_value, bytesToHex(serialized)) }) }) diff --git a/packages/portalnetwork/test/networks/state/stateManager.spec.ts b/packages/portalnetwork/test/networks/state/stateManager.spec.ts index b9157fe9b..b30da0520 100644 --- a/packages/portalnetwork/test/networks/state/stateManager.spec.ts +++ b/packages/portalnetwork/test/networks/state/stateManager.spec.ts @@ -1,5 +1,5 @@ // import { SignableENR } from '@chainsafe/discv5/enr' -// import { fromHexString, toHexString } from '@chainsafe/ssz' +// import { hexToBytes, bytesToHex } from '@chainsafe/ssz' // import { EVM } from '@ethereumjs/evm' // import { Trie } from '@ethereumjs/trie' // import { @@ -82,7 +82,7 @@ describe.skip('UltralightStateManager', () => { // // } // const greeterBytecode = // '0x608060405234801561000f575f80fd5b5060043610610034575f3560e01c80638da5cb5b14610038578063cfae321714610056575b5f80fd5b610040610074565b60405161004d9190610118565b60405180910390f35b61005e61009c565b60405161006b91906101bb565b60405180910390f35b5f60015f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b60606040518060400160405280600581526020017f68656c6c6f000000000000000000000000000000000000000000000000000000815250905090565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f610102826100d9565b9050919050565b610112816100f8565b82525050565b5f60208201905061012b5f830184610109565b92915050565b5f81519050919050565b5f82825260208201905092915050565b5f5b8381101561016857808201518184015260208101905061014d565b5f8484015250505050565b5f601f19601f8301169050919050565b5f61018d82610131565b610197818561013b565b93506101a781856020860161014b565b6101b081610173565b840191505092915050565b5f6020820190508181035f8301526101d38184610183565b90509291505056fea2646970667358221220945519e237b301b5baf64c20c2a39b6a8b300541470b28b0e6cfbc1568dc6f3364736f6c63430008160033' - // const byteCode = fromHexString(greeterBytecode) + // const byteCode = hexToBytes(greeterBytecode) // const node = await PortalNetwork.create({ // transport: TransportLayer.NODE, // supportedNetworks: [NetworkId.StateNetwork], @@ -124,7 +124,7 @@ describe.skip('UltralightStateManager', () => { // assert.deepEqual(gotCode, byteCode, 'retrieved contract code from state network') // const greeterInput = '0xcfae3217' // const evm = new EVM({ stateManager: usm }) - // const res = (await evm.runCall({ data: fromHexString(greeterInput), to: address })).execResult + // const res = (await evm.runCall({ data: hexToBytes(greeterInput), to: address })).execResult // .returnValue // // Decode offset in `returnValue` for start of Solidity return value // const offset = bytesToInt(res.slice(0, 32)) @@ -157,7 +157,7 @@ describe.skip('UltralightStateManager', () => { // const network = node.networks.get(NetworkId.StateNetwork) as StateNetwork // const usm = new UltralightStateManager(network) // const storageTrie = new Trie({ useKeyHashing: true }) - // await storageTrie.put(fromHexString(cstp.slot), fromHexString(cstp.value)) + // await storageTrie.put(hexToBytes(cstp.slot), hexToBytes(cstp.value)) // const pk = randomBytes(32) // const address = Address.fromPrivateKey(pk) // const account = Account.fromAccountData({ @@ -168,7 +168,7 @@ describe.skip('UltralightStateManager', () => { // const trie = new Trie({ useKeyHashing: true }) // await trie.put(address.toBytes(), account.serialize()) // const proof = await trie.createProof(address.toBytes()) - // const storageProof = await storageTrie.createProof(fromHexString(cstp.slot)) + // const storageProof = await storageTrie.createProof(hexToBytes(cstp.slot)) // const content = AccountTrieProofType.serialize({ // witnesses: proof, // }) @@ -183,9 +183,9 @@ describe.skip('UltralightStateManager', () => { // storageContent, // ) // await usm.setStateRoot(trie.root()) - // const res = await usm.getContractStorage(address, fromHexString(cstp.slot)) + // const res = await usm.getContractStorage(address, hexToBytes(cstp.slot)) // assert.equal( - // toHexString(res), + // bytesToHex(res), // cstp.value, // 'successfully retrieved storage slot with state manager', // ) @@ -245,7 +245,7 @@ describe.skip('UltralightStateManager', () => { // assert.deepEqual({}, clearedStorage, 'storage cache should be empty after clear') // await usm.putContractCode(vitalikDotEth, hexToBytes('0x6000')) // assert.equal( - // toHexString(await usm.getContractCode(vitalikDotEth)), + // bytesToHex(await usm.getContractCode(vitalikDotEth)), // '0x6000', // 'contract code was found in cache', // ) diff --git a/packages/portalnetwork/test/wire/types.spec.ts b/packages/portalnetwork/test/wire/types.spec.ts index 7a902caf9..8b4614121 100644 --- a/packages/portalnetwork/test/wire/types.spec.ts +++ b/packages/portalnetwork/test/wire/types.spec.ts @@ -1,6 +1,6 @@ import { ENR } from '@chainsafe/enr' -import { BitArray, toHexString } from '@chainsafe/ssz' -import { concatBytes, hexToBytes } from '@ethereumjs/util' +import { BitArray } from '@chainsafe/ssz' +import { bytesToHex, concatBytes, hexToBytes } from '@ethereumjs/util' import { assert, describe, it } from 'vitest' import { @@ -26,7 +26,7 @@ describe('message encoding should match test vectors', () => { }) testVector = '0x0001000000000000000c000000feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' - assert.equal(toHexString(payload), testVector, 'ping message encoded correctly') + assert.equal(bytesToHex(payload), testVector, 'ping message encoded correctly') }) // Validate FINDNODES message encoding @@ -38,7 +38,7 @@ describe('message encoding should match test vectors', () => { value: { distances }, }) testVector = '0x02040000000001ff00' - assert.equal(toHexString(payload), testVector, 'findNodes message encoded correctly') + assert.equal(bytesToHex(payload), testVector, 'findNodes message encoded correctly') }) it('should encode PONG message correctly', () => { @@ -50,7 +50,7 @@ describe('message encoding should match test vectors', () => { }, }) testVector = '0x030105000000' - assert.equal(toHexString(payload), testVector, 'nodes message with no ENRs encoded correctly') + assert.equal(bytesToHex(payload), testVector, 'nodes message with no ENRs encoded correctly') }) // Validate NODES message encoding const enr1 = @@ -70,7 +70,7 @@ describe('message encoding should match test vectors', () => { }) testVector = '0x030105000000080000007f000000f875b8401ce2991c64993d7c84c29a00bdc871917551c7d330fca2dd0d69c706596dc655448f030b98a77d4001fd46ae0112ce26d613c5a6a02a81a6223cd0c4edaa53280182696482763489736563703235366b31a103ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd3138f875b840d7f1c39e376297f81d7297758c64cb37dcc5c3beea9f57f7ce9695d7d5a67553417d719539d6ae4b445946de4d99e680eb8063f29485b555d45b7df16a1850130182696482763489736563703235366b31a1030e2cb74241c0c4fc8e8166f1a79a05d5b0dd95813a74b094529f317d5c39d235' - assert.equal(toHexString(payload), testVector, 'nodes message with 2 ENRs encoded correctly') + assert.equal(bytesToHex(payload), testVector, 'nodes message with 2 ENRs encoded correctly') }) // Validate FINDCONTENT message encoding @@ -82,7 +82,7 @@ describe('message encoding should match test vectors', () => { value: { contentKey }, }) testVector = '0x0404000000706f7274616c' - assert.equal(toHexString(payload), testVector, 'find content message encoded correctly') + assert.equal(bytesToHex(payload), testVector, 'find content message encoded correctly') // Validate CONTENT message encoding // TODO: Update CONTENT encoding to use PortalMessageWireType.serialize @@ -94,7 +94,7 @@ describe('message encoding should match test vectors', () => { const contentMessagePayload = ContentMessageType.serialize({ selector: 0, value: connectionId }) payload = concatBytes(Uint8Array.from([MessageCodes.CONTENT]), contentMessagePayload) testVector = '0x05000102' - assert.equal(toHexString(payload), testVector, 'content message encodes correctly') + assert.equal(bytesToHex(payload), testVector, 'content message encodes correctly') }) // Validate OFFER message encoding @@ -105,7 +105,7 @@ describe('message encoding should match test vectors', () => { value: { contentKeys }, }) testVector = '0x060400000004000000010203' - assert.equal(toHexString(payload), testVector, 'offer message encodes correctly') + assert.equal(bytesToHex(payload), testVector, 'offer message encodes correctly') }) // Validate ACCEPT message encoding @@ -120,6 +120,6 @@ describe('message encoding should match test vectors', () => { }, }) testVector = '0x070102060000000101' - assert.equal(toHexString(payload), testVector, 'accept message encodes correctly') + assert.equal(bytesToHex(payload), testVector, 'accept message encodes correctly') }) }) diff --git a/packages/portalnetwork/test/wire/utp/packet.spec.ts b/packages/portalnetwork/test/wire/utp/packet.spec.ts index 014351f0b..5336161ef 100644 --- a/packages/portalnetwork/test/wire/utp/packet.spec.ts +++ b/packages/portalnetwork/test/wire/utp/packet.spec.ts @@ -1,5 +1,4 @@ -import { toHexString } from '@chainsafe/ssz' -import { hexToBytes } from '@ethereumjs/util' +import { bytesToHex, hexToBytes } from '@ethereumjs/util' import { assert, describe, it } from 'vitest' import { Packet, PacketType } from '../../../src/index.js' @@ -163,7 +162,7 @@ export function encodingTest( ) } const encodedPacket = testPacket.encode() - assert.equal(toHexString(encodedPacket), expectedResult, 'Packet encoding test passed') + assert.equal(bytesToHex(encodedPacket), expectedResult, 'Packet encoding test passed') const testHeader = testPacket.header const decodedPacket = Packet.fromBuffer(Buffer.from(expectedResult.slice(2), 'hex')) const decodedHeader = decodedPacket.header @@ -191,8 +190,8 @@ export function encodingTest( // } if (packetType === PacketType.ST_DATA) { assert.equal( - toHexString(Buffer.from(testData.payload!)), - toHexString(Buffer.from(Uint8Array.from(decodedPacket.payload!))), + bytesToHex(Buffer.from(testData.payload!)), + bytesToHex(Buffer.from(Uint8Array.from(decodedPacket.payload!))), `Successfully encoded and decoded DATA Packet payload.`, ) } diff --git a/packages/portalnetwork/test/wire/utp/socket.spec.ts b/packages/portalnetwork/test/wire/utp/socket.spec.ts index ec6172126..3d490ee95 100644 --- a/packages/portalnetwork/test/wire/utp/socket.spec.ts +++ b/packages/portalnetwork/test/wire/utp/socket.spec.ts @@ -1,4 +1,4 @@ -import { hexToBytes } from '@ethereumjs/util' +import { bytesToHex, hexToBytes } from '@ethereumjs/util' import { randomBytes } from 'crypto' import debug from 'debug' import { assert, describe, it } from 'vitest' @@ -12,7 +12,6 @@ import { PortalNetwork, PortalNetworkUTP, UtpSocketType, - toHexString, } from '../../../src/index.js' import { createUtpSocket } from '../../../src/wire/utp/Socket/index.js' @@ -230,7 +229,7 @@ describe('createPacket()', async () => { assert.equal(write_data.header.seqNr, write.getSeqNr() - 1, 'Packet seqNr correctly set') assert.equal(write_data.header.ackNr, write.ackNr, 'Packet ackNr correctly set') assert.equal( - toHexString(write_data.payload!), + bytesToHex(write_data.payload!), '0x1234', 'DATA Packet payload correctly set to undefined', ) diff --git a/packages/portalnetwork/test/wire/utp/util.spec.ts b/packages/portalnetwork/test/wire/utp/util.spec.ts index a713196f9..76db61e8f 100644 --- a/packages/portalnetwork/test/wire/utp/util.spec.ts +++ b/packages/portalnetwork/test/wire/utp/util.spec.ts @@ -1,4 +1,4 @@ -import { toHexString } from '@chainsafe/ssz' +import { bytesToHex } from '@ethereumjs/util' import { randomBytes } from 'crypto' import { assert, describe, it } from 'vitest' @@ -67,7 +67,7 @@ describe('uTP utils tests', () => { assert.deepEqual( contents[0], decoded[0], - `first item matches, "${toHexString(contents[0]).slice(0, 5)}..." === "${toHexString( + `first item matches, "${bytesToHex(contents[0]).slice(0, 5)}..." === "${bytesToHex( decoded[0], ).slice(0, 5)}..."`, )