( api: ApiPromise, blockArray: number[], overallSpecVer?: number, )
| 394 | } |
| 395 | |
| 396 | export async function fetchBlocksBatches( |
| 397 | api: ApiPromise, |
| 398 | blockArray: number[], |
| 399 | overallSpecVer?: number, |
| 400 | ): Promise<IBlock<BlockContent>[]> { |
| 401 | const blocks = await fetchBlocksArray(api, blockArray); |
| 402 | const blockHashs = blocks.map((b) => b.block.header.hash); |
| 403 | const parentBlockHashs = blocks.map((b) => b.block.header.parentHash); |
| 404 | // If overallSpecVersion passed, we don't need to use api to get runtimeVersions |
| 405 | // wrap block with specVersion |
| 406 | // If specVersion changed, we also not guarantee in this batch contains multiple runtimes, |
| 407 | // therefore we better to fetch runtime over all blocks |
| 408 | const [blockEvents, runtimeVersions] = await Promise.all([ |
| 409 | fetchEventsRange(api, blockHashs), |
| 410 | overallSpecVer !== undefined // note, we need to be careful if spec version is 0 |
| 411 | ? undefined |
| 412 | : fetchRuntimeVersionRange(api, parentBlockHashs), |
| 413 | ]); |
| 414 | |
| 415 | return blocks.map((block, idx) => { |
| 416 | const events = blockEvents[idx]; |
| 417 | const parentSpecVersion = |
| 418 | overallSpecVer ?? runtimeVersions?.[idx].specVersion.toNumber(); |
| 419 | assert(parentSpecVersion !== undefined, 'parentSpecVersion is undefined'); |
| 420 | |
| 421 | const wrappedBlock = wrapBlock(block, events.toArray(), parentSpecVersion); |
| 422 | const wrappedExtrinsics = wrapExtrinsics(wrappedBlock, events); |
| 423 | const wrappedEvents = wrapEvents(wrappedExtrinsics, events, wrappedBlock); |
| 424 | |
| 425 | return { |
| 426 | getHeader: () => substrateBlockToHeader(wrappedBlock), |
| 427 | block: { |
| 428 | block: wrappedBlock, |
| 429 | extrinsics: wrappedExtrinsics, |
| 430 | events: wrappedEvents, |
| 431 | }, |
| 432 | }; |
| 433 | }); |
| 434 | } |
| 435 | |
| 436 | // TODO why is fetchBlocksBatches a breadth first function rather than depth? |
| 437 | export async function fetchLightBlock( |
no test coverage detected