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

watcher: handle near missing blocks #149

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
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
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