From 1c3b0698d514720520f6ddcfcd929fd02854b7f4 Mon Sep 17 00:00:00 2001 From: belopash Date: Tue, 23 Jul 2024 17:55:49 +0500 Subject: [PATCH] retry both rpc calls --- .../substrate-data-raw/src/datasource.ts | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/substrate/substrate-data-raw/src/datasource.ts b/substrate/substrate-data-raw/src/datasource.ts index 7f26555ed..471617f55 100644 --- a/substrate/substrate-data-raw/src/datasource.ts +++ b/substrate/substrate-data-raw/src/datasource.ts @@ -180,26 +180,24 @@ export class RpcDataSource { while (!isEnd()) { let head = await headSrc.call() if (head === prev) continue - let finalizedHead: string + + let finalizedHead: string | null = null if (this.finalityConfirmation == null) { finalizedHead = await this.rpc.getFinalizedHead() } else { - let header: BlockHeader | null = null - let attempts = 0 - while (attempts <= 5) { - header = await this.rpc.getBlockHeader(head) - if (header == null) { - head = await this.rpc.getHead() - attempts += 1 - } else { - break + let attempt = 0 + while (attempt < 5) { + let header = await this.rpc.getBlockHeader(head) + if (header != null) { + let finalizedHeight = qty2Int(header.number) - this.finalityConfirmation + finalizedHead = await this.rpc.getBlockHash(finalizedHeight) + if (finalizedHead != null) break } + + head = await this.rpc.getHead() + attempt += 1 } - assert(header, 'cannot determine head of the chain') - let height = qty2Int(header.number) - this.finalityConfirmation - let hash = await this.rpc.getBlockHash(height) - assert(hash) - finalizedHead = hash + assert(finalizedHead != null, `failed to get finalized head after ${attempt} attempts`) } await this.handleNewHeads({ best: {hash: head},