Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: block fetching retry #47

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading