Skip to content

Commit

Permalink
clean up parameter typing and config
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Dec 30, 2024
1 parent 56d2f9a commit 9c53e5c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 40 deletions.
46 changes: 26 additions & 20 deletions packages/cli/scripts/testProvider.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import { UltralightProvider } from '../../portalnetwork/src/client/provider'
import { NetworkId } from '../../portalnetwork/src/networks/types'

const testBlockHash = '0x95b0950557cbc3e6647766adb719f80f7c7d192f4429b6026cdbd2cbe6a64294'
const testContract = '0x6b175474e89094c44da98b954eedeac495271d0f'
const testStorage = '0x0000000000000000000000000000000000000000000000000000000000000000'
const historicalBlock = 31591
const historicalBlock = 1048576

async function findHistoricalAccount(provider: UltralightProvider, blockNumber: number): Promise<string | null> {
async function findHistoricalAccount(
provider: UltralightProvider,
blockNumber: number,
): Promise<string | null> {
try {
const block: any = await provider.request({
method: 'eth_getBlockByNumber',
params: [blockNumber, true]
params: [blockNumber, true],
})

if (block && block.result && block.result.transactions && block.result.transactions.length > 0) {

if (
block &&
block.result &&
block.result.transactions &&
block.result.transactions.length > 0
) {
const contractCreation = block.result.transactions.find((tx: any) => !tx.to)
if (contractCreation) {
console.log('Found contract creation transaction')
Expand Down Expand Up @@ -46,6 +54,10 @@ async function main() {
'enr:-IS4QA5hpJikeDFf1DD1_Le6_ylgrLGpdwn3SRaneGu9hY2HUI7peHep0f28UUMzbC0PvlWjN8zSfnqMG07WVcCyBhADgmlkgnY0gmlwhKRc9-KJc2VjcDI1NmsxoQJMpHmGj1xSP1O-Mffk_jYIHVcg6tY5_CjmWVg1gJEsPIN1ZHCCE4o',
],
bindAddress: '0.0.0.0',
supportedNetworks: [
{ networkId: NetworkId.HistoryNetwork, maxStorage: 1024 },
{ networkId: NetworkId.StateNetwork, maxStorage: 1024 },
],
})
console.log('Provider created:', provider.portal.discv5.enr.nodeId)
await provider.portal.start()
Expand All @@ -67,7 +79,7 @@ async function main() {
console.log('Testing eth_getBlockByNumber...')
const blockByNumber = await provider.request({
method: 'eth_getBlockByNumber',
params: [100, false]
params: [historicalBlock, false],
})

console.log('Block by number retrieved:', blockByNumber)
Expand All @@ -78,42 +90,38 @@ async function main() {
if (!testAddress) {
console.error('Could not find a historical account to test with')
}

console.log(`Found historical address: ${testAddress}`)

console.log('Testing eth_getTransactionCount...')
const transactionCount = await provider.request({
method: 'eth_getTransactionCount',
params: [testAddress, '31591'],
params: [testAddress, historicalBlock],
})

console.log('Transaction count:', transactionCount)


console.log('Testing eth_getCode...')
const code = await provider.request({
method: 'eth_getCode',
params: [testContract, '100']
params: [testContract, '100'],
})
console.log('Contract code retrieved:', code)


console.log('Testing eth_getBalance...')
const balance = await provider.request({
method: 'eth_getBalance',
params: [testAddress, '31591']
params: [testAddress, historicalBlock],
})
console.log('Account balance:', balance)


console.log('Testing eth_getStorageAt...')
const storage = await provider.request({
method: 'eth_getStorageAt',
params: [testContract, testStorage, '31591']
params: [testContract, testStorage, historicalBlock],
})
console.log('Storage value:', storage)


console.log('Testing eth_call...')
const callData = {
to: testAddress,
Expand All @@ -122,15 +130,14 @@ async function main() {
}
const callResult = await provider.request({
method: 'eth_call',
params: [callData, 31591]
params: [callData, historicalBlock],
})
console.log('Contract call result:', callResult)


try {
await provider.request({
method: 'eth_unsupportedMethod',
params: []
params: [],
})
} catch (error) {
console.log('Expected error for unsupported method:', error.message)
Expand All @@ -139,5 +146,4 @@ async function main() {
process.exit(0)
}

main()
.catch((err) => console.error(err))
main().catch((err) => console.error(err))
38 changes: 18 additions & 20 deletions packages/portalnetwork/src/client/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PortalNetwork } from './client.js'
import type { PortalNetworkOpts } from './types'
import { hexToBytes } from '@ethereumjs/util'

import { formatBlockResponse } from '../util/helpers'
import { formatBlockResponse } from '../util/helpers.js'

const ERROR_CODES = {
UNSUPPORTED_METHOD: 4200,
Expand Down Expand Up @@ -59,7 +59,6 @@ export class UltralightProvider {
}

case 'eth_getBlockByNumber': {
console.log('inside eth_getBlockByNumber')
if (params.length !== 2)
throw this.createError(
ERROR_CODES.INVALID_PARAMS,
Expand Down Expand Up @@ -88,21 +87,21 @@ export class UltralightProvider {
'Invalid params for eth_getTransactionCount',
)
const [address, block] = params
return await this.getTransactionCount(address as Uint8Array, block as string)
return await this.getTransactionCount(address as string, block as string)
}

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)
return await this.getCode(codeAddress as string, codeBlock as string)
}

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)
return await this.getBalance(balanceAddress as string, balanceBlock as bigint)
}

case 'eth_getStorageAt': {
Expand All @@ -113,8 +112,8 @@ export class UltralightProvider {
)
const [storageAddress, position, storageBlock] = params
return await this.getStorageAt(
storageAddress as Uint8Array,
position as Uint8Array,
storageAddress as string,
position as string,
storageBlock as string,
)
}
Expand Down Expand Up @@ -154,7 +153,6 @@ export class UltralightProvider {
}

private async getBlockByNumber(blockNumber: string | number | bigint, includeTx: boolean) {

let block

if (typeof blockNumber === 'string') {
Expand All @@ -174,24 +172,24 @@ export class UltralightProvider {
return formatBlockResponse(block, includeTx)
}

private async getTransactionCount(address: Uint8Array, block: string) {
return this.portal.ETH.getTransactionCount(address, block)
private async getTransactionCount(address: string, block: string) {
return this.portal.ETH.getTransactionCount(hexToBytes(address), block)
}

private async getCode(codeAddress: Uint8Array, codeBlock: string) {
return this.portal.ETH.getCode(codeAddress, codeBlock)
private async getCode(codeAddress: string, codeBlock: string) {
return this.portal.ETH.getCode(hexToBytes(codeAddress), codeBlock)
}

private async getBalance(balanceAddress: Uint8Array, balanceBlock: bigint) {
return this.portal.ETH.getBalance(balanceAddress, balanceBlock)
private async getBalance(balanceAddress: string, balanceBlock: bigint) {
return this.portal.ETH.getBalance(hexToBytes(balanceAddress), balanceBlock)
}

private async getStorageAt(
storageAddress: Uint8Array,
position: Uint8Array,
storageBlock: string,
) {
return this.portal.ETH.getStorageAt(storageAddress, position, storageBlock)
private async getStorageAt(storageAddress: string, position: string, storageBlock: string) {
return this.portal.ETH.getStorageAt(
hexToBytes(storageAddress),
hexToBytes(position),
storageBlock,
)
}

private async call(callObject: any, callBlock: bigint) {
Expand Down

0 comments on commit 9c53e5c

Please sign in to comment.