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

Function getBlockByHeight

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

Source from the content-addressed store, hash-verified

297 */
298
299export async function getBlockByHeight(
300 api: ApiPromise,
301 height: number,
302): Promise<SignedBlock> {
303 const blockHash = await api.rpc.chain.getBlockHash(height).catch((e) => {
304 logger.error(`failed to fetch BlockHash ${height}`);
305 throw ApiPromiseConnection.handleError(e);
306 });
307
308 const block = await api.rpc.chain.getBlock(blockHash).catch((e) => {
309 logger.error(
310 `failed to fetch Block hash="${blockHash}" height="${height}"${getApiDecodeErrMsg(e.message)}`,
311 );
312 throw ApiPromiseConnection.handleError(e);
313 });
314
315 // validate block is valid
316 if (block.block.header.hash.toHex() !== blockHash.toHex()) {
317 throw new Error(
318 `fetched block header hash ${block.block.header.hash.toHex()} is not match with blockHash ${blockHash.toHex()} at block ${height}. This is likely a problem with the rpc provider.`,
319 );
320 }
321 return block;
322}
323
324export async function getHeaderByHeight(
325 api: ApiPromise,

Callers 3

getBlockTimestampMethod · 0.90
substrate.test.tsFile · 0.90
fetchBlocksArrayFunction · 0.85

Calls 3

getApiDecodeErrMsgFunction · 0.85
handleErrorMethod · 0.65
errorMethod · 0.45

Tested by

no test coverage detected