Skip to content

Commit

Permalink
fix: add requestId to data fetcher request
Browse files Browse the repository at this point in the history
  • Loading branch information
vasyl-ivanchuk committed Mar 5, 2024
1 parent 417b044 commit 6ff5c99
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
22 changes: 10 additions & 12 deletions packages/worker/src/block/block.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,23 @@ export class BlockProcessor {
if (!allBlocksExist) {
// We don't need to handle this potential revert as these blocks are not in DB yet,
// try again later once these blocks are present in blockchain again.
this.logger.warn(
"Not all the requested blocks from the next blocks to process range exist in blockchain, likely revert has happened",
{
lastDbBlockNumber,
}
);
this.logger.warn({
message:
"Not all the requested blocks from the next blocks to process range exist in blockchain, likely revert has happened",
lastDbBlockNumber,
});
return false;
}
const isBlocksLinkingValid = validateBlocksLinking(blocksToProcess);
if (!isBlocksLinkingValid) {
// We don't need to handle this revert as these blocks are not in DB yet,
// we just need to wait for blockchain to complete this revert before inserting these blocks.
// This is very unlikely to ever happen.
this.logger.warn(
"Some of the requested blocks from the next blocks to process range have invalid link to previous block, likely revert has happened",
{
lastDbBlockNumber: lastDbBlockNumber,
}
);
this.logger.warn({
message:
"Some of the requested blocks from the next blocks to process range have invalid link to previous block, likely revert has happened",
lastDbBlockNumber: lastDbBlockNumber,
});
return false;
}

Expand Down
1 change: 1 addition & 0 deletions packages/worker/src/block/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class BlockService extends Worker {
this.logger.error(`Error on processing next block range, waiting ${nextIterationDelay} ms to retry`, error.stack);
}
if (nextIterationDelay) {
this.logger.debug(`Waiting for ${nextIterationDelay}ms`);
await waitFor(() => !this.currentProcessPromise, nextIterationDelay);
}
if (!this.currentProcessPromise) {
Expand Down
5 changes: 3 additions & 2 deletions packages/worker/src/block/block.watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ export class BlockWatcher implements OnModuleInit, OnModuleDestroy {
}

private async getBlockInfoFromBlockChain(blockNumber: number): Promise<BlockData> {
this.logger.debug({ message: "Getting block from the blockchain", blockNumber });
const requestId = Math.random();
this.logger.debug({ message: "Getting block from the blockchain", blockNumber, requestId });

const stopGetBlockInfoDurationMetric = this.getBlockInfoDurationMetric.startTimer();
const blockData = await this.dataFetchService.getBlockData(blockNumber);
const blockData = await this.dataFetchService.getBlockData(blockNumber, requestId);
stopGetBlockInfoDurationMetric();

return blockData;
Expand Down
7 changes: 4 additions & 3 deletions packages/worker/src/dataFetcher/dataFetcher.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ export class DataFetcherService {
this.httpAgent = new Agent({ keepAlive });
}

public async getBlockData(blockNumber: number): Promise<BlockData> {
const blocksData = await this.getBlocksData(blockNumber, blockNumber);
public async getBlockData(blockNumber: number, requestId?: number): Promise<BlockData> {
const blocksData = await this.getBlocksData(blockNumber, blockNumber, requestId);
return blocksData[0];
}

private async getBlocksData(from: number, to: number): Promise<BlockData[]> {
private async getBlocksData(from: number, to: number, requestId?: number): Promise<BlockData[]> {
const queryString = new URLSearchParams({
from: from.toString(),
to: to.toString(),
Expand All @@ -45,6 +45,7 @@ export class DataFetcherService {
code: error.code,
status: error.response?.status,
response: error.response?.data,
requestId,
});
throw error;
})
Expand Down

0 comments on commit 6ff5c99

Please sign in to comment.