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

Function fetchLightBlock

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

Source from the content-addressed store, hash-verified

435
436// TODO why is fetchBlocksBatches a breadth first function rather than depth?
437export async function fetchLightBlock(
438 api: ApiPromise,
439 height: number,
440): Promise<IBlock<LightBlockContent>> {
441 const blockHash = await api.rpc.chain.getBlockHash(height).catch((e) => {
442 logger.error(`failed to fetch BlockHash ${height}`);
443 throw ApiPromiseConnection.handleError(e);
444 });
445
446 const [header, events, timestamp] = await Promise.all([
447 api.rpc.chain.getHeader(blockHash).catch((e) => {
448 logger.error(
449 `failed to fetch Block Header hash="${blockHash}" height="${height}"`,
450 );
451 throw ApiPromiseConnection.handleError(e);
452 }),
453 api.query.system.events.at(blockHash).catch((e) => {
454 logger.error(`failed to fetch events at block ${blockHash}`);
455 throw ApiPromiseConnection.handleError(e);
456 }),
457 // TODO: Maybe api.query.timestamp.now.at(blockHash) is the only option. If we do use it we need sufficient tests and errors if a chain doesn't support getting the timestamp.
458 (await api.at(blockHash)).query.timestamp.now(),
459 ]);
460
461 const blockHeader: BlockHeader = {
462 block: { header },
463 events: events.toArray(),
464 };
465 return {
466 block: {
467 block: blockHeader,
468 events: events.map((evt, idx) => merge(evt, { idx, block: blockHeader })),
469 },
470 getHeader: () => {
471 return {
472 ...substrateHeaderToHeader(blockHeader.block.header),
473 timestamp: new Date(timestamp.toNumber()),
474 };
475 },
476 };
477}
478
479export async function fetchBlocksBatchesLight(
480 api: ApiPromise,

Callers 1

fetchBlocksBatchesLightFunction · 0.85

Calls 5

substrateHeaderToHeaderFunction · 0.85
getHeaderMethod · 0.80
mapMethod · 0.80
handleErrorMethod · 0.65
errorMethod · 0.45

Tested by

no test coverage detected