Skip to content

Commit

Permalink
cloud_functions/src: add snoozing
Browse files Browse the repository at this point in the history
  • Loading branch information
panoel committed Sep 8, 2023
1 parent 988b630 commit 9ff71ed
Showing 1 changed file with 82 additions and 5 deletions.
87 changes: 82 additions & 5 deletions cloud_functions/src/alarmMissingVaas.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { CHAIN_ID_TO_NAME, ChainId, ChainName } from '@certusone/wormhole-sdk';
import { MissingVaasByChain, commonGetMissingVaas } from './getMissingVaas';
import { formatAndSendToSlack } from './utils';
import { assertEnvironmentVariable, formatAndSendToSlack } from './utils';
import { ObservedMessage } from './types';
import { explorerBlock, explorerTx } from '@wormhole-foundation/wormhole-monitor-common';
import { Firestore } from 'firebase-admin/firestore';

export async function alarmMissingVaas(req: any, res: any) {
res.set('Access-Control-Allow-Origin', '*');
Expand All @@ -14,21 +15,39 @@ export async function alarmMissingVaas(req: any, res: any) {
res.status(204).send('');
return;
}
let firestoreVAAs: FirestoreVAA[] = [];
try {
// Get the current VAAs in the firestore holding area that we want to keep there.
// The key is the vaaKey
let firestoreMap: Map<string, FirestoreVAA> = await getAndProcessFirestore();

// Pre fill out firestoreVAAS with the VAAs we know we want to keep.
firestoreMap.forEach((vaa) => {
firestoreVAAs.push(vaa);
});

// attempting to retrieve missing VAAs...
const messages: MissingVaasByChain = await commonGetMissingVaas();
if (messages) {
const now = new Date();
now.setHours(now.getHours() - 2);
const twoHoursAgo = now.toISOString();
const thePast = now;
thePast.setHours(now.getHours() - 2);
const twoHoursAgo = thePast.toISOString();
for (const chain of Object.keys(messages)) {
const chainId = chain as unknown as ChainId;
const msgs = messages[chainId];
if (msgs && msgs.messages) {
for (let i = 0; i < msgs.messages.length; i++) {
// Check the timestamp and only send messages that are older than 2 hours
if (msgs.messages[i].timestamp < twoHoursAgo) {
await formatAndSendToSlack(formatMessage(msgs.messages[i]));
const msg: ObservedMessage = msgs.messages[i];
if (msg.timestamp < twoHoursAgo) {
let vaaKey: string = `${msg.chain}/${msg.emitter}/${msg.seq}`;
if (!firestoreMap.has(vaaKey)) {
let firestoreMsg: FirestoreVAA = convert(msg);
firestoreMap.set(vaaKey, firestoreMsg);
firestoreVAAs.push(firestoreMsg);
await formatAndSendToSlack(formatMessage(msg));
}
}
}
} else {
Expand All @@ -40,10 +59,59 @@ export async function alarmMissingVaas(req: any, res: any) {
console.log('could not get missing VAAs', e);
res.sendStatus(500);
}
await updateFirestore(firestoreVAAs);
res.status(200).send('successfully alarmed missing VAAS');
return;
}

// This function gets all the VAAs in the firestore table,
// checks the timestamp (keeping any that are less than 2 hours old),
// and returns a map of those VAAs.
async function getAndProcessFirestore(): Promise<Map<string, FirestoreVAA>> {
// Get VAAs in the firestore holding area.
const firestore = new Firestore();
const collection = firestore.collection(
assertEnvironmentVariable('FIRESTORE_ALARM_MISSING_VAAS_COLLECTION')
);
let current = new Map<string, FirestoreVAA>();
await collection
.doc('VAAs')
.get()
.then((doc) => {
if (doc.exists) {
const data = doc.data();
if (data) {
console.log('Document data:', data);
// TODO: Figure out how to process the VAAs and put into a map, if < 2 hours old
}
}
})
.catch((error) => {
console.log('Error getting document:', error);
});
return current;
}

async function updateFirestore(vaas: FirestoreVAA[]): Promise<void> {
const firestore = new Firestore();
const collection = firestore.collection(
assertEnvironmentVariable('FIRESTORE_ALARM_MISSING_VAAS_COLLECTION')
);
const doc = collection.doc('VAAs');
await doc.set({ VAAs: vaas });
}

function convert(msg: ObservedMessage): FirestoreVAA {
return {
chain: msg.chain.toString(),
txHash: msg.txHash,
vaaKey: `${msg.chain}/${msg.emitter}/${msg.seq}`,
block: msg.block.toString(),
blockTS: msg.timestamp,
noticedTS: new Date().toISOString(),
};
}

function formatMessage(msg: ObservedMessage): string {
const cName: string = CHAIN_ID_TO_NAME[msg.chain as ChainId] as ChainName;
// const vaaKeyUrl: string = `https://wormhole.com/explorer/?emitterChain=${msg.chain}&emitterAddress=${msg.emitter}&sequence=${msg.seq}`;
Expand All @@ -53,3 +121,12 @@ function formatMessage(msg: ObservedMessage): string {
const formattedMsg = `*Chain:* ${cName}(${msg.chain})\n*TxHash:* <${txHashUrl}|${msg.txHash}>\n*VAA Key:* <${vaaKeyUrl}|${msg.chain}/${msg.emitter}/${msg.seq}> \n*Block:* <${blockUrl}|${msg.block}> \n*Timestamp:* ${msg.timestamp}`;
return formattedMsg;
}

type FirestoreVAA = {
chain: string;
txHash: string;
vaaKey: string;
block: string;
blockTS: string;
noticedTS: string;
};

0 comments on commit 9ff71ed

Please sign in to comment.