Skip to content

Commit

Permalink
RPC chain_getBlockHash accepts hexnumber (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci committed Apr 3, 2024
1 parent 1da013d commit 6bf1ac4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
15 changes: 8 additions & 7 deletions packages/core/src/rpc/substrate/chain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Header as CodecHeader } from '@polkadot/types/interfaces'
import { HexString } from '@polkadot/util/types'
import { hexToNumber, isHex } from '@polkadot/util'

import { Handler, ResponseError } from '../shared.js'
import type { Header } from '../../index.js'
Expand All @@ -22,14 +23,14 @@ const processHeader = ({ parentHash, number, stateRoot, extrinsicsRoot, digest }
*
* @return Block hash | hash[] | null
*/
export const chain_getBlockHash: Handler<[number | number[] | null], HexString | (HexString | null)[] | null> = async (
context,
[blockNumber],
) => {
export const chain_getBlockHash: Handler<
[number | HexString | number[] | HexString[] | null],
HexString | (HexString | null)[] | null
> = async (context, [blockNumber]) => {
const numbers = Array.isArray(blockNumber) ? blockNumber : [blockNumber]
const hashes = await Promise.all(numbers.map((n) => context.chain.getBlockAt(n))).then((blocks) =>
blocks.map((b) => b?.hash || null),
)
const hashes = await Promise.all(
numbers.map((n) => (isHex(n) ? hexToNumber(n) : n)).map((n) => context.chain.getBlockAt(n)),
).then((blocks) => blocks.map((b) => b?.hash || null))
return Array.isArray(blockNumber) ? hashes : hashes[0]
}

Expand Down
3 changes: 3 additions & 0 deletions packages/e2e/src/chain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ describe('chain rpc', () => {
expect(await api.rpc('chain_getBlockHash', [0, undefined, null])).toEqual(
expect.arrayContaining([hash0, hashHead, hashHead]),
)
expect(await api.rpc('chain_getBlockHash', '0x03e8')).toEqual(hash1000)
expect(await api.rpc('chain_getBlockHash', ['0x03e8'])).toEqual(expect.arrayContaining([hash1000]))
expect(await api.rpc('chain_getBlockHash', ['0x03e8', null])).toEqual(expect.arrayContaining([hash1000, hashHead]))

await check(api.rpc.chain.getHeader()).toMatchSnapshot()
await check(api.rpc.chain.getHeader(hashHead)).toMatchSnapshot()
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e/src/migration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Migration', async () => {
const { api, dev, teardown } = await setupContext({
wasmOverride: new URL('../blobs/shibuya-118.wasm', import.meta.url).pathname,
blockNumber: 5335600,
endpoint: 'wss://shibuya-rpc.dwellir.com',
endpoint: ['wss://shibuya-rpc.dwellir.com', 'wss://rpc.shibuya.astar.network'],
db: !process.env.RUN_TESTS_WITHOUT_DB ? 'e2e-tests-db.sqlite' : undefined,
timeout: 400_000,
})
Expand Down

0 comments on commit 6bf1ac4

Please sign in to comment.