Skip to content

Commit

Permalink
Merge pull request #47 from 0xPolygon/block-getter
Browse files Browse the repository at this point in the history
fix: block fetching retry
  • Loading branch information
nitinmittal23 committed Jul 12, 2024
2 parents d4e7350 + a0d89a2 commit 74eb556
Showing 1 changed file with 36 additions and 17 deletions.
53 changes: 36 additions & 17 deletions internal/block_getters/block_getter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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<IBlock> {
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<IBlock> {
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);
}
}

/**
Expand Down

0 comments on commit 74eb556

Please sign in to comment.