From 80d73bb1c426493a159429cbdca0108ea3eaf16d Mon Sep 17 00:00:00 2001 From: acolytec3 <17355484+acolytec3@users.noreply.github.com> Date: Sat, 28 Dec 2024 07:23:08 -0500 Subject: [PATCH] Config updates and formatting fixes --- packages/cli/scripts/testProvider.ts | 50 ++++--- packages/portalnetwork/src/client/provider.ts | 141 +++++++++++------- 2 files changed, 120 insertions(+), 71 deletions(-) diff --git a/packages/cli/scripts/testProvider.ts b/packages/cli/scripts/testProvider.ts index 57bf25fce..e4917c08c 100644 --- a/packages/cli/scripts/testProvider.ts +++ b/packages/cli/scripts/testProvider.ts @@ -3,26 +3,38 @@ import { UltralightProvider } from 'portalnetwork' const testBlockHash = '0x95b0950557cbc3e6647766adb719f80f7c7d192f4429b6026cdbd2cbe6a64294' async function main() { - try { + const provider = await UltralightProvider.create({ + bootnodes: [ + 'enr:-Jy4QIs2pCyiKna9YWnAF0zgf7bT0GzlAGoF8MEKFJOExmtofBIqzm71zDvmzRiiLkxaEJcs_Amr7XIhLI74k1rtlXICY5Z0IDAuMS4xLWFscGhhLjEtMTEwZjUwgmlkgnY0gmlwhKEjVaWJc2VjcDI1NmsxoQLSC_nhF1iRwsCw0n3J4jRjqoaRxtKgsEe5a-Dz7y0JloN1ZHCCIyg', + 'enr:-Jy4QKSLYMpku9F0Ebk84zhIhwTkmn80UnYvE4Z4sOcLukASIcofrGdXVLAUPVHh8oPCfnEOZm1W1gcAxB9kV2FJywkCY5Z0IDAuMS4xLWFscGhhLjEtMTEwZjUwgmlkgnY0gmlwhJO2oc6Jc2VjcDI1NmsxoQLMSGVlxXL62N3sPtaV-n_TbZFCEM5AR7RDyIwOadbQK4N1ZHCCIyg', + 'enr:-Jy4QH4_H4cW--ejWDl_W7ngXw2m31MM2GT8_1ZgECnfWxMzZTiZKvHDgkmwUS_l2aqHHU54Q7hcFSPz6VGzkUjOqkcCY5Z0IDAuMS4xLWFscGhhLjEtMTEwZjUwgmlkgnY0gmlwhJ31OTWJc2VjcDI1Nmsx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0vzJEB2IEd5RtVdPUYZUbeg4f0LMradgwpyIhYUeSxz2Tfa8DgmlkgnY0gmlwhKRc9_OJc2VjcDI1NmsxoQJd4NAVKOXfbdxyjSOUJzmA4rjtg43EDeEJu1f8YRhb_4N1ZHCCE4o', + 'enr:-IS4QGG6moBhLW1oXz84NaKEHaRcim64qzFn1hAG80yQyVGNLoKqzJe887kEjthr7rJCNlt6vdVMKMNoUC9OCeNK-EMDgmlkgnY0gmlwhKRc9-KJc2VjcDI1NmsxoQLJhXByb3LmxHQaqgLDtIGUmpANXaBbFw3ybZWzGqb9-IN1ZHCCE4k', + 'enr:-IS4QA5hpJikeDFf1DD1_Le6_ylgrLGpdwn3SRaneGu9hY2HUI7peHep0f28UUMzbC0PvlWjN8zSfnqMG07WVcCyBhADgmlkgnY0gmlwhKRc9-KJc2VjcDI1NmsxoQJMpHmGj1xSP1O-Mffk_jYIHVcg6tY5_CjmWVg1gJEsPIN1ZHCCE4o', + ], + bindAddress: '0.0.0.0', + }) + console.log('Provider created:', provider.portal.discv5.enr.nodeId) + await provider.portal.start() + console.log('portal started') - const provider = await UltralightProvider.create({ - bootnodes: [ - 'enr:-Jy4QIs2pCyiKna9YWnAF0zgf7bT0GzlAGoF8MEKFJOExmtofBIqzm71zDvmzRiiLkxaEJcs_Amr7XIhLI74k1rtlXICY5Z0IDAuMS4xLWFscGhhLjEtMTEwZjUwgmlkgnY0gmlwhKEjVaWJc2VjcDI1NmsxoQLSC_nhF1iRwsCw0n3J4jRjqoaRxtKgsEe5a-Dz7y0JloN1ZHCCIyg', - 'enr:-Jy4QKSLYMpku9F0Ebk84zhIhwTkmn80UnYvE4Z4sOcLukASIcofrGdXVLAUPVHh8oPCfnEOZm1W1gcAxB9kV2FJywkCY5Z0IDAuMS4xLWFscGhhLjEtMTEwZjUwgmlkgnY0gmlwhJO2oc6Jc2VjcDI1NmsxoQLMSGVlxXL62N3sPtaV-n_TbZFCEM5AR7RDyIwOadbQK4N1ZHCCIyg', - 'enr:-Jy4QH4_H4cW--ejWDl_W7ngXw2m31MM2GT8_1ZgECnfWxMzZTiZKvHDgkmwUS_l2aqHHU54Q7hcFSPz6VGzkUjOqkcCY5Z0IDAuMS4xLWFscGhhLjEtMTEwZjUwgmlkgnY0gmlwhJ31OTWJc2VjcDI1NmsxoQPC0eRkjRajDiETr_DRa5N5VJRm-ttCWDoO1QAMMCg5pIN1ZHCCIyg' - ] - }) -console.log('Provider created:', provider) - - const block = await provider.request({ - method: 'eth_getBlockByHash', - params: [testBlockHash, false] - }) - - console.log('Block retrieved:', block) - } catch (error) { - console.error('Error while fetching block by hash:', error) + while (provider.portal.network()['0x500b']?.routingTable.values().length === 0) { + console.log('Waiting for network to start...') + await new Promise((resolve) => setTimeout(resolve, 1000)) } + const block = await provider.request({ + method: 'eth_getBlockByHash', + params: [testBlockHash, false], + }) + + console.log('Block retrieved:', block) + process.exit(0) } -main().catch((err) => console.error(err)) +main() + .catch((err) => console.error(err)) diff --git a/packages/portalnetwork/src/client/provider.ts b/packages/portalnetwork/src/client/provider.ts index beea66f7b..4356a545b 100644 --- a/packages/portalnetwork/src/client/provider.ts +++ b/packages/portalnetwork/src/client/provider.ts @@ -1,18 +1,14 @@ -import { PortalNetwork } from './client' +import { PortalNetwork } from './client.js' -import type { HistoryNetwork } from '../networks/index.js' -import { NetworkId } from '../networks/types.js' -import { PortalNetworkOpts } from './types' +import type { PortalNetworkOpts } from './types' import { hexToBytes } from '@ethereumjs/util' - const ERROR_CODES = { UNSUPPORTED_METHOD: 4200, INVALID_PARAMS: -32602, - INTERNAL_ERROR: -32603 + INTERNAL_ERROR: -32603, } - const SUPPORTED_METHODS = new Set([ 'eth_getBlockByHash', 'eth_getBlockByNumber', @@ -20,7 +16,7 @@ const SUPPORTED_METHODS = new Set([ 'eth_getCode', 'eth_getBalance', 'eth_getStorageAt', - 'eth_call' + 'eth_call', ]) interface RequestArguments { @@ -29,14 +25,10 @@ interface RequestArguments { } export class UltralightProvider { - public portal: PortalNetwork - public historyNetwork: HistoryNetwork constructor(portal: PortalNetwork) { - this.portal = portal - this.historyNetwork = portal.networks.get(NetworkId.HistoryNetwork) as HistoryNetwork } static async create(opts: Partial): Promise { @@ -45,103 +37,148 @@ export class UltralightProvider { } async request({ method, params = [] }: RequestArguments): Promise { - if (!SUPPORTED_METHODS.has(method)) { - throw this.createError(ERROR_CODES.UNSUPPORTED_METHOD, `The Provider does not support the requested method`) + throw this.createError( + ERROR_CODES.UNSUPPORTED_METHOD, + `The Provider does not support the requested method`, + ) } try { switch (method) { - case 'eth_getBlockByHash': - if (params.length !== 2) throw this.createError(ERROR_CODES.INVALID_PARAMS, 'Invalid params for eth_getBlockByHash') + case 'eth_getBlockByHash': { + if (params.length !== 2) + throw this.createError( + ERROR_CODES.INVALID_PARAMS, + 'Invalid params for eth_getBlockByHash', + ) const [blockHash, fullTx] = params return await this.getBlockByHash(hexToBytes(blockHash as string), fullTx as boolean) + } + + case 'eth_getBlockByNumber': { + if (params.length !== 2) + throw this.createError( + ERROR_CODES.INVALID_PARAMS, + 'Invalid params for eth_getBlockByNumber', + ) - case 'eth_getBlockByNumber': - if (params.length !== 2) throw this.createError(ERROR_CODES.INVALID_PARAMS, 'Invalid params for eth_getBlockByNumber') const [blockNumber, includeTx] = params - if (typeof blockNumber !== 'number' && typeof blockNumber !== 'bigint' && blockNumber !== 'latest' && blockNumber !== 'finalized') { - throw this.createError(ERROR_CODES.INVALID_PARAMS, `Invalid block number: ${blockNumber}`) + if ( + typeof blockNumber !== 'number' && + typeof blockNumber !== 'bigint' && + blockNumber !== 'latest' && + blockNumber !== 'finalized' + ) { + throw this.createError( + ERROR_CODES.INVALID_PARAMS, + `Invalid block number: ${blockNumber}`, + ) } return await this.getBlockByNumber(blockNumber, includeTx as boolean) + } - case 'eth_getTransactionCount': - if (params.length !== 2) throw this.createError(ERROR_CODES.INVALID_PARAMS, 'Invalid params for eth_getTransactionCount') + case 'eth_getTransactionCount': { + if (params.length !== 2) + throw this.createError( + ERROR_CODES.INVALID_PARAMS, + 'Invalid params for eth_getTransactionCount', + ) const [address, block] = params return await this.getTransactionCount(address as Uint8Array, block as string) + } - case 'eth_getCode': - if (params.length !== 2) throw this.createError(ERROR_CODES.INVALID_PARAMS, 'Invalid params for eth_getCode') + case 'eth_getCode': { + if (params.length !== 2) + throw this.createError(ERROR_CODES.INVALID_PARAMS, 'Invalid params for eth_getCode') const [codeAddress, codeBlock] = params return await this.getCode(codeAddress as Uint8Array, codeBlock as string) + } - case 'eth_getBalance': - if (params.length !== 2) throw this.createError(ERROR_CODES.INVALID_PARAMS, 'Invalid params for eth_getBalance') + case 'eth_getBalance': { + if (params.length !== 2) + throw this.createError(ERROR_CODES.INVALID_PARAMS, 'Invalid params for eth_getBalance') const [balanceAddress, balanceBlock] = params return await this.getBalance(balanceAddress as Uint8Array, balanceBlock as bigint) + } - case 'eth_getStorageAt': - if (params.length !== 3) throw this.createError(ERROR_CODES.INVALID_PARAMS, 'Invalid params for eth_getStorageAt') + case 'eth_getStorageAt': { + if (params.length !== 3) + throw this.createError( + ERROR_CODES.INVALID_PARAMS, + 'Invalid params for eth_getStorageAt', + ) const [storageAddress, position, storageBlock] = params - return await this.getStorageAt(storageAddress as Uint8Array, position as Uint8Array, storageBlock as string) + return await this.getStorageAt( + storageAddress as Uint8Array, + position as Uint8Array, + storageBlock as string, + ) + } - case 'eth_call': - if (params.length !== 2) throw this.createError(ERROR_CODES.INVALID_PARAMS, 'Invalid params for eth_call') + case 'eth_call': { + if (params.length !== 2) + throw this.createError(ERROR_CODES.INVALID_PARAMS, 'Invalid params for eth_call') const [callObject, callBlock] = params return await this.call(callObject as any, callBlock as bigint) + } default: - throw this.createError(ERROR_CODES.UNSUPPORTED_METHOD, `The Provider does not support the requested method`) + throw this.createError( + ERROR_CODES.UNSUPPORTED_METHOD, + `The Provider does not support the requested method`, + ) } } catch (error: any) { return { jsonrpc: '2.0', id: null, error: { - code: error.code || -32603, - message: error.message || 'Internal error', - } + code: error.code ?? ERROR_CODES.INTERNAL_ERROR, + message: error.message ?? 'Internal error', + }, } } } private async getBlockByHash(blockHash: Uint8Array, fullTx: boolean) { - - const response = await this.portal.ETH.getBlockByHash(blockHash, fullTx) - return response -} + const response = await this.portal.ETH.getBlockByHash(blockHash, fullTx) + return response + } private async getBlockByNumber(blockNumber: string | number | bigint, includeTx: boolean) { - if (typeof blockNumber === 'string') { if (blockNumber === 'latest' || blockNumber === 'finalized') { - return await this.portal.ETH.getBlockByNumber(blockNumber, includeTx) - } - return await this.portal.ETH.getBlockByNumber(BigInt(blockNumber), includeTx) + return this.portal.ETH.getBlockByNumber(blockNumber, includeTx) + } + return this.portal.ETH.getBlockByNumber(BigInt(blockNumber), includeTx) } - return await this.portal.ETH.getBlockByNumber(blockNumber, includeTx) + return this.portal.ETH.getBlockByNumber(blockNumber, includeTx) } - private async getTransactionCount(address: Uint8Array, block: string) { - return await this.portal.ETH.getTransactionCount(address, block) + return this.portal.ETH.getTransactionCount(address, block) } private async getCode(codeAddress: Uint8Array, codeBlock: string) { - return await this.portal.ETH.getCode(codeAddress, codeBlock) + return this.portal.ETH.getCode(codeAddress, codeBlock) } private async getBalance(balanceAddress: Uint8Array, balanceBlock: bigint) { - return await this.portal.ETH.getBalance(balanceAddress, balanceBlock) + return this.portal.ETH.getBalance(balanceAddress, balanceBlock) } - private async getStorageAt(storageAddress: Uint8Array, position: Uint8Array, storageBlock: string) { - return await this.portal.ETH.getStorageAt(storageAddress, position, storageBlock) + private async getStorageAt( + storageAddress: Uint8Array, + position: Uint8Array, + storageBlock: string, + ) { + return this.portal.ETH.getStorageAt(storageAddress, position, storageBlock) } private async call(callObject: any, callBlock: bigint) { - return await this.portal.ETH.call(callObject, callBlock) + return this.portal.ETH.call(callObject, callBlock) } private createError(code: number, message: string) { @@ -149,4 +186,4 @@ export class UltralightProvider { ;(error as any).code = code return error } -} \ No newline at end of file +}