Skip to content

Commit

Permalink
fix getBlockHash & add chain_getHead alias (#441)
Browse files Browse the repository at this point in the history
* add chain_getHead alias

* support multiple hash fetch

* handle null
  • Loading branch information
ermalkaleci authored Oct 14, 2023
1 parent d319a35 commit 6962e7b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
20 changes: 12 additions & 8 deletions packages/chopsticks/src/rpc/substrate/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ const processHeader = (header: Header) => {

/**
* @param context
* @param params - [`blockNumber`]
* @param params - [`blockNumber` | `blockNumber[]` | null]
*
* @return Block hash
* @return Block hash | hash[] | null
*/
export const chain_getBlockHash: Handler<[number], HexString> = async (context, [blockNumber]) => {
const block = await context.chain.getBlockAt(blockNumber)
if (!block) {
throw new ResponseError(1, `Block #${blockNumber} not found`)
}
return block.hash
export const chain_getBlockHash: Handler<[number | number[] | 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),
)
return Array.isArray(blockNumber) ? hashes : hashes[0]
}

/**
Expand Down Expand Up @@ -103,6 +106,7 @@ export const chain_unsubscribeNewHead: Handler<[string], void> = async (_context
unsubscribe(subid)
}

export const chain_getHead = chain_getBlockHash
export const chain_subscribeNewHeads = chain_subscribeNewHead
export const chain_unsubscribeNewHeads = chain_unsubscribeNewHead
export const chain_unsubscribeFinalizedHeads = chain_unsubscribeNewHead
4 changes: 2 additions & 2 deletions packages/core/src/blockchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ export class Blockchain {
/**
* Get block by number.
*/
async getBlockAt(number?: number): Promise<Block | undefined> {
if (number === undefined) {
async getBlockAt(number?: number | null): Promise<Block | undefined> {
if (number === null || number === undefined) {
return this.head
}
if (number > this.#head.number) {
Expand Down
10 changes: 10 additions & 0 deletions packages/e2e/src/chain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ describe('chain rpc', () => {
expectHex(await api.rpc.chain.getBlockHash(0)).toMatch(hash0)
expectHex(await api.rpc.chain.getBlockHash(1000)).toMatch(hash1000)

expect(await api.rpc('chain_getHead')).toEqual(hashHead)
expect(await api.rpc('chain_getBlockHash', null)).toEqual(hashHead)
expect(await api.rpc('chain_getBlockHash', undefined)).toEqual(hashHead)
expect(await api.rpc('chain_getBlockHash', [null])).toEqual(expect.arrayContaining([hashHead]))
expect(await api.rpc('chain_getBlockHash', [undefined])).toEqual(expect.arrayContaining([hashHead]))
expect(await api.rpc('chain_getBlockHash', [0, 1000])).toEqual(expect.arrayContaining([hash0, hash1000]))
expect(await api.rpc('chain_getBlockHash', [0, undefined, null])).toEqual(
expect.arrayContaining([hash0, hashHead, hashHead]),
)

expectJson(await api.rpc.chain.getHeader()).toMatchSnapshot()
expectJson(await api.rpc.chain.getHeader(hashHead)).toMatchSnapshot()
expectJson(await api.rpc.chain.getHeader(hash0)).toMatchSnapshot()
Expand Down

0 comments on commit 6962e7b

Please sign in to comment.