Skip to content

Commit

Permalink
retry in case of reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcgroul committed Jul 22, 2024
1 parent e455558 commit 292df3c
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions substrate/substrate-data-raw/src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,6 @@ export class RpcDataSource {
}
}

async getFinalizedHead(best: string): Promise<string> {
if (this.finalityConfirmation == null) {
return this.rpc.getFinalizedHead()
} else {
let header = await this.rpc.getBlockHeader(best)
assert(header)
let height = qty2Int(header.number) - this.finalityConfirmation
let hash = await this.rpc.getBlockHash(height)
assert(hash)
return hash
}
}

async *getFinalizedBlocks(requests: RangeRequestList<DataRequest>, stopOnHead?: boolean): AsyncIterable<Batch<BlockData>> {
assertRangeList(requests.map(req => req.range))

Expand Down Expand Up @@ -193,7 +180,27 @@ export class RpcDataSource {
while (!isEnd()) {
let head = await headSrc.call()
if (head === prev) continue
let finalizedHead = await this.getFinalizedHead(head)
let finalizedHead: string
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
}
}
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
}
await this.handleNewHeads({
best: {hash: head},
finalized: {hash: finalizedHead}
Expand Down

0 comments on commit 292df3c

Please sign in to comment.