Skip to content

Commit

Permalink
Fix response formatting and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Jan 6, 2025
1 parent f5b7711 commit 67d4c4b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 91 deletions.
17 changes: 10 additions & 7 deletions packages/portalnetwork/src/client/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class UltralightProvider {
if (!SUPPORTED_METHODS.has(method)) {
throw this.createError(
ERROR_CODES.UNSUPPORTED_METHOD,
`The Provider does not support the requested method`,
`The provider does not support the requested method`,
)
}

Expand Down Expand Up @@ -173,16 +173,19 @@ export class UltralightProvider {
}

private async getTransactionCount(address: string, block: string) {
return this.portal.ETH.getTransactionCount(hexToBytes(address), block)
const txCount = await this.portal.ETH.getTransactionCount(hexToBytes(address), block)
return formatResponse('0x' + (txCount !== undefined ? txCount.toString(16) : ''))
}

private async getCode(codeAddress: string, codeBlock: string) {
return this.portal.ETH.getCode(hexToBytes(codeAddress), codeBlock)
const code = await this.portal.ETH.getCode(hexToBytes(codeAddress), codeBlock)
return formatResponse('0x' + (code !== undefined ? code.toString() : ''))
}

private async getBalance(balanceAddress: string, balanceBlock: bigint) {

const balance = await this.portal.ETH.getBalance(hexToBytes(balanceAddress), balanceBlock)
return formatResponse('0x' + (balance ? balance.toString(16) : '0'))
return formatResponse('0x' + (balance !== undefined ? balance.toString(16) : ''))
}

private async getStorageAt(storageAddress: string, position: string, storageBlock: string) {
Expand All @@ -191,17 +194,17 @@ export class UltralightProvider {
hexToBytes(position),
storageBlock,
)
return formatResponse('0x' + (storage ? storage.toString() : '0'))
return formatResponse('0x' + (storage !== undefined ? storage.toString() : ''))
}

private async call(callObject: any, callBlock: bigint) {
const result = await this.portal.ETH.call(callObject, callBlock)
return formatResponse('0x' + result.toString())
return formatResponse('0x' + (result !== undefined ? result.toString() : ''))
}

private createError(code: number, message: string) {
const error = new Error(message)
;(error as any).code = code
; (error as any).code = code
return error
}
}
169 changes: 85 additions & 84 deletions packages/portalnetwork/test/client/provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,101 +23,102 @@ it('Test provider functionality', async () => {
enr,
privateKey,
},
supportedNetworks: [{ networkId: NetworkId.HistoryNetwork }],
supportedNetworks: [{ networkId: NetworkId.HistoryNetwork }, { networkId: NetworkId.StateNetwork }],
})


// Stub getBlockByHash for unit testing
provider.portal.ETH.getBlockByHash = async (_hash: Uint8Array) => {
return Block.fromBlockData({ header: BlockHeader.fromHeaderData({ number: 2n }) })
}

provider.portal.ETH.getBlockByNumber = async (blockNumber: number | bigint | "latest" | "finalized") => {
return Block.fromBlockData({
header: BlockHeader.fromHeaderData({
number: typeof blockNumber === 'string' ? 0n : blockNumber,
return Block.fromBlockData({
header: BlockHeader.fromHeaderData({
number: typeof blockNumber === 'string' ? 0n : blockNumber,

})
})
}

provider.portal.ETH.getTransactionCount = async (_address: Uint8Array) => {
return BigInt('0x5')
}

provider.portal.ETH.getCode = async (_address: Uint8Array) => {
return new Uint8Array(Buffer.from('60806040', 'hex'))
}

provider.portal.ETH.getBalance = async (_address: Uint8Array) => {
return 1000000000000000000n
}

provider.portal.ETH.getStorageAt = async (_address: Uint8Array, _position: Uint8Array) => {
const result = new Uint8Array(32)
result[30] = 0x00
result[31] = 0x01
return Buffer.from(result).toString('hex')
}

provider.portal.ETH.call = async (_txObject: any) => {
return Buffer.from([0x00, 0x01]).toString('hex')
}

const blockByHash = await provider.request({
method: 'eth_getBlockByHash',
params: ['0x123', false]
}) as { result: { number: string } }
expect(blockByHash.result.number).toBe('0x2')

const blockByNumber = await provider.request({
method: 'eth_getBlockByNumber',
params: [100, false]
}) as { result: { number: string } }
expect(blockByNumber.result.number).toBe('0x64')

const balance = await provider.request({
method: 'eth_getBalance',
params: ['0x3DC00AaD844393c110b61aED5849b7c82104e748', '0x0']
}) as { result: string }
expect(balance.result).toBe('0xde0b6b3a7640000')

const storage = await provider.request({
method: 'eth_getStorageAt',
params: [
'0x1234567890123456789012345678901234567890',
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x64'
]
}) as { result: string }
expect(storage.result).toBe('0x' + '00'.repeat(30) + '0001')

const call = await provider.request({
method: 'eth_call',
params: [{
to: '0x1234567890123456789012345678901234567890',
data: '0x70a08231000000000000000000000000'
}, '0x64']
}) as { result: string }
expect(call.result).toBe('0x0001')

await expect(provider.request({
method: 'eth_unsupportedMethod',
params: []
})).rejects.toThrow()

await expect(provider.request({
method: 'eth_getBlockByHash',
params: ['0x123']
})).resolves.toEqual({
error: {
code: -32602,
message: 'Invalid params for eth_getBlockByHash'
},
id: null,
jsonrpc: '2.0',
})
}

provider.portal.ETH.getTransactionCount = async (_address: Uint8Array) => {
return BigInt('0x5')
}

provider.portal.ETH.getCode = async (_address: Uint8Array) => {
return new Uint8Array(Buffer.from('60806040', 'hex'))
}

provider.portal.ETH.getBalance = async (_address: Uint8Array) => {
return 1000000000000000000n
}

provider.portal.ETH.getStorageAt = async (_address: Uint8Array, _position: Uint8Array) => {
const result = new Uint8Array(32)
result[30] = 0x00
result[31] = 0x01
return Buffer.from(result).toString('hex')
}

provider.portal.ETH.call = async (_txObject: any) => {
return Buffer.from([0x00, 0x01]).toString('hex')
}

const blockByHash = await provider.request({
method: 'eth_getBlockByHash',
params: ['0x123', false]
}) as { result: { number: string } }
expect(blockByHash.result.number).toBe('0x2')

const blockByNumber = await provider.request({
method: 'eth_getBlockByNumber',
params: [100, false]
}) as { result: { number: string } }
expect(blockByNumber.result.number).toBe('0x64')

const balance = await provider.request({
method: 'eth_getBalance',
params: ['0x3DC00AaD844393c110b61aED5849b7c82104e748', '0x0']
}) as { result: string }
expect(balance.result).toBe('0xde0b6b3a7640000')


const storage = await provider.request({
method: 'eth_getStorageAt',
params: [
'0x1234567890123456789012345678901234567890',
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x64'
]
}) as { result: string }
expect(storage.result).toBe('0x' + '00'.repeat(30) + '0001')

const call = await provider.request({
method: 'eth_call',
params: [{
to: '0x1234567890123456789012345678901234567890',
data: '0x70a08231000000000000000000000000'
}, '0x64']
}) as { result: string }
expect(call.result).toBe('0x0001')

await expect(provider.request({
method: 'eth_unsupportedMethod',
params: []
})).rejects.toThrow()

await expect(provider.request({
method: 'eth_getBlockByHash',
params: ['0x123']
})).resolves.toEqual({
error: {
code: -32602,
message: 'Invalid params for eth_getBlockByHash'
},
id: null,
jsonrpc: '2.0',
})

await (provider as any).portal.stop()
await provider.portal.stop()

})

0 comments on commit 67d4c4b

Please sign in to comment.