MCPcopy Create free account
hub / github.com/subquery/subql / getHeaderByHeight

Function getHeaderByHeight

packages/node/src/utils/substrate.ts:324–346  ·  view source on GitHub ↗
(
  api: ApiPromise,
  height: number,
)

Source from the content-addressed store, hash-verified

322}
323
324export async function getHeaderByHeight(
325 api: ApiPromise,
326 height: number,
327): Promise<SubstrateHeader> {
328 const blockHash = await api.rpc.chain.getBlockHash(height).catch((e) => {
329 logger.error(`failed to fetch BlockHash ${height}`);
330 throw ApiPromiseConnection.handleError(e);
331 });
332
333 const header = await api.rpc.chain.getHeader(blockHash).catch((e) => {
334 logger.error(
335 `failed to fetch Block Header hash="${blockHash}" height="${height}"`,
336 );
337 throw ApiPromiseConnection.handleError(e);
338 });
339 // validate block is valid
340 if (header.hash.toHex() !== blockHash.toHex()) {
341 throw new Error(
342 `fetched block header hash ${header.hash.toHex()} is not match with blockHash ${blockHash.toHex()} at block ${height}. This is likely a problem with the rpc provider.`,
343 );
344 }
345 return header;
346}
347
348export async function fetchBlocksArray(
349 api: ApiPromise,

Callers 1

fetchHeaderArrayFunction · 0.85

Calls 3

getHeaderMethod · 0.80
handleErrorMethod · 0.65
errorMethod · 0.45

Tested by

no test coverage detected