From a0d89a2018357906bb3c023d6f968e2e4bfa1aba Mon Sep 17 00:00:00 2001 From: Nitin Mittal Date: Fri, 12 Jul 2024 16:04:53 +0400 Subject: [PATCH] fix: block fetching retry --- internal/block_getters/block_getter.ts | 53 +++++++++++++++++--------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/internal/block_getters/block_getter.ts b/internal/block_getters/block_getter.ts index 06c1652..bdb7190 100644 --- a/internal/block_getters/block_getter.ts +++ b/internal/block_getters/block_getter.ts @@ -13,14 +13,14 @@ import { Logger } from "../logger/logger.js"; * * @author - Vibhu Rajeev, Nitin Mittal */ -export class BlockGetter extends BlockFormatter implements IBlockGetter { +export class BlockGetter extends BlockFormatter implements IBlockGetter { /** * @param {Eth} eth - Eth module from web3.js * @param {number} maxRetries - The number of times to retry on errors. * * @constructor */ - constructor(protected eth: Eth, protected maxRetries: number = 0) { + constructor(protected eth: Eth, protected maxRetries: number = 0) { super(); } @@ -46,25 +46,44 @@ export class BlockGetter extends BlockFormatter implements IBlockGetter { * * @throws {Error} - Throws error object on failure. */ - public async getBlockWithTransactionReceipts(blockNumber: number | string): Promise { - const block: BlockTransactionObject = await this.eth.getBlock(blockNumber, true); - Logger.debug(`Fetching transaction receipts for the following block ${block.number}`); + public async getBlockWithTransactionReceipts(blockNumber: number | string, errorCount: number = 0): Promise { + try { + const block: BlockTransactionObject = await this.eth.getBlock(blockNumber, true); + + if (!block) { + throw new BlockProducerError( + "Block producer error", + BlockProducerError.codes.RECEIPT_NOT_FOUND, + false, + `null receipt found for block ${blockNumber}.`, + "remote" + ); + } + + Logger.debug(`Fetching transaction receipts for the following block ${block.number}`); - const transactions: ITransaction[] = []; + const transactions: ITransaction[] = []; - for (const transactionObject of block.transactions) { - transactions.push( - this.formatTransactionObject( - transactionObject as IWeb3Transaction, - await this.getTransactionReceipt(transactionObject.hash) - ) + for (const transactionObject of block.transactions) { + transactions.push( + this.formatTransactionObject( + transactionObject as IWeb3Transaction, + await this.getTransactionReceipt(transactionObject.hash) + ) + ); + } + + return this.formatBlockWithTransactions( + block, + transactions ); - } + } catch (error) { + if (errorCount >= this.maxRetries) { + throw error; + } - return this.formatBlockWithTransactions( - block, - transactions - ); + return this.getBlockWithTransactionReceipts(blockNumber, errorCount + 1); + } } /**