Skip to content

Commit

Permalink
watcher: handle near missing blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
panoel committed Oct 31, 2023
1 parent 5a42151 commit ba00fc7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
2 changes: 2 additions & 0 deletions watcher/src/utils/near.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const getNearProvider = async (rpc: string): Promise<Provider> => {
return provider;
};

// This function can definitely throw.
export async function getTimestampByBlock(
provider: Provider,
blockHeight: number
Expand All @@ -32,6 +33,7 @@ export async function getTimestampByBlock(
return block.header.timestamp;
}

// This function can definitely throw.
export async function fetchBlockByBlockId(
provider: Provider,
blockHeight: BlockId
Expand Down
57 changes: 46 additions & 11 deletions watcher/src/watchers/NearArchiveWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
isWormholePublishEventLog,
} from '../utils/near';
import { Watcher } from './Watcher';
import { sleep } from '@wormhole-foundation/wormhole-monitor-common';

export class NearArchiveWatcher extends Watcher {
provider: Provider | null = null;
Expand All @@ -39,22 +38,58 @@ export class NearArchiveWatcher extends Watcher {
}

async getMessagesForBlocks(fromBlock: number, toBlock: number): Promise<VaasByBlock> {
// assume toBlock was retrieved from getFinalizedBlockNumber and is finalized
this.logger.info(`fetching info for blocks ${fromBlock} to ${toBlock}`);
const origFromBlock = fromBlock;
const origToBlock = toBlock;
this.logger.info(`fetching info for blocks ${origFromBlock} to ${origToBlock}`);
const provider = await this.getProvider();
const fromBlockTimestamp: number = await getTimestampByBlock(provider, fromBlock);
// Do the following in a while loop until a fromBlock has been found.
let fromBlockTimestamp: number = 0;
let done: boolean = false;
while (!done) {
try {
fromBlockTimestamp = await getTimestampByBlock(provider, fromBlock);
done = true;
} catch (e) {
// Logging this to help with troubleshooting.
this.logger.debug(e);
this.logger.error('getMessagesForBlocks(): Error fetching from block', fromBlock);
fromBlock++;
if (fromBlock > toBlock) {
this.logger.error(
`Unable to fetch timestamp for fromBlock in range ${origFromBlock} - ${origToBlock}`
);
throw new Error(
`Unable to fetch timestamp for fromBlock in range ${origFromBlock} - ${origToBlock}`
);
}
}
}
if (fromBlockTimestamp === 0) {
this.logger.error(`Unable to fetch timestamp for block ${fromBlock}`);
this.logger.error(`Unable to fetch timestamp for fromBlock ${fromBlock}`);
throw new Error(`Unable to fetch timestamp for fromBlock ${fromBlock}`);
}
let toBlockInfo: BlockResult = {} as BlockResult;
try {
toBlockInfo = await fetchBlockByBlockId(provider, toBlock);
} catch (e) {
// Logging this to help with troubleshooting.
this.logger.error('getMessagesForBlocks(): Error fetching block', e);
throw e;
done = false;
while (!done) {
try {
toBlockInfo = await fetchBlockByBlockId(provider, toBlock);
done = true;
} catch (e) {
// Logging this to help with troubleshooting.
this.logger.debug(e);
this.logger.error('getMessagesForBlocks(): Error fetching toBlock', toBlock);
toBlock--;
if (toBlock < fromBlock) {
this.logger.error(
`Unable to fetch block info for toBlock in range ${origFromBlock} - ${origToBlock}`
);
throw new Error(
`Unable to fetch block info for toBlock in range ${origFromBlock} - ${origToBlock}`
);
}
}
}
this.logger.info(`Actual block range: ${fromBlock} - ${toBlock}`);
const transactions: Transaction[] = await getTransactionsByAccountId(
CONTRACTS.MAINNET.near.core,
this.maximumBatchSize,
Expand Down

0 comments on commit ba00fc7

Please sign in to comment.