From ef74fefe846ed07bd513b1c5d13edf7d84fad0df Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 18 Aug 2020 13:05:34 +0200 Subject: [PATCH] Fall back on query when header from subscription is too high --- packages/stargate/src/queries/queryclient.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/stargate/src/queries/queryclient.ts b/packages/stargate/src/queries/queryclient.ts index d5052478..10b7723b 100644 --- a/packages/stargate/src/queries/queryclient.ts +++ b/packages/stargate/src/queries/queryclient.ts @@ -230,7 +230,7 @@ export class QueryClient { } const searchHeight = height + 1; - let nextHeader: Header; + let nextHeader: Header | undefined; let headersSubscription: Stream | undefined; try { headersSubscription = this.tmClient.subscribeNewBlockHeader(); @@ -239,9 +239,14 @@ export class QueryClient { } if (headersSubscription) { - // get the header for height+1 - nextHeader = await firstEvent(headersSubscription); // TODO: fall back on polling if this returns a too high header - } else { + const firstHeader = await firstEvent(headersSubscription); + // The first header we get might not be n+1 but n+2 or even higher. In such cases we fall back on a query. + if (firstHeader.height === searchHeight) { + nextHeader = firstHeader; + } + } + + if (!nextHeader) { // start from current height to avoid backend error for minHeight in the future let header = (await this.tmClient.blockchain(height, searchHeight)).blockMetas .map((meta) => meta.header)