From 93cacf2d3c83634acbf937ef9133edc8d75de292 Mon Sep 17 00:00:00 2001 From: Paul Noel Date: Wed, 20 Sep 2023 09:08:54 -0500 Subject: [PATCH] cloud_functions: don't alarm governed VAAs --- cloud_functions/src/alarmMissingVaas.ts | 72 ++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/cloud_functions/src/alarmMissingVaas.ts b/cloud_functions/src/alarmMissingVaas.ts index db93f82d..050d85fc 100644 --- a/cloud_functions/src/alarmMissingVaas.ts +++ b/cloud_functions/src/alarmMissingVaas.ts @@ -5,6 +5,35 @@ import { ObservedMessage } from './types'; import { explorerBlock, explorerTx } from '@wormhole-foundation/wormhole-monitor-common'; import { Firestore } from 'firebase-admin/firestore'; +interface EnqueuedVAAResponse { + sequence: string; + releaseTime: number; + notionalValue: string; + txHash: string; +} + +interface Emitter { + emitterAddress: string; + enqueuedVaas: EnqueuedVAAResponse[]; + totalEnqueuedVaas: string; +} + +interface ChainStatus { + availableNotional: string; + chainId: number; + emitters: Emitter[]; +} + +interface GovernedVAA { + chainId: number; + emitterAddress: string; + sequence: string; + txHash: string; +} + +// The key is the vaaKey +type GovernedVAAMap = Map; + export async function alarmMissingVaas(req: any, res: any) { res.set('Access-Control-Allow-Origin', '*'); if (req.method === 'OPTIONS') { @@ -26,6 +55,9 @@ export async function alarmMissingVaas(req: any, res: any) { firestoreVAAs.push(vaa); }); + // Get governed VAAS + const governedVAAs: GovernedVAAMap = await getGovernedVaas(); + // attempting to retrieve missing VAAs... const messages: MissingVaasByChain = await commonGetMissingVaas(); if (messages) { @@ -42,7 +74,7 @@ export async function alarmMissingVaas(req: any, res: any) { const msg: ObservedMessage = msgs.messages[i]; if (msg.timestamp < twoHoursAgo) { let vaaKey: string = `${msg.chain}/${msg.emitter}/${msg.seq}`; - if (!firestoreMap.has(vaaKey)) { + if (!firestoreMap.has(vaaKey) && !governedVAAs.has(vaaKey)) { let firestoreMsg: FirestoreVAA = convert(msg); firestoreMap.set(vaaKey, firestoreMsg); firestoreVAAs.push(firestoreMsg); @@ -64,6 +96,44 @@ export async function alarmMissingVaas(req: any, res: any) { return; } +// This function gets all the enqueued VAAs from he governorStatus collection. +async function getGovernedVaas(): Promise { + const vaas: GovernedVAAMap = new Map(); + // Walk all the guardians and retrieve the enqueued VAAs + const firestore = new Firestore(); + const collection = firestore.collection( + assertEnvironmentVariable('FIRESTORE_GOVERNOR_STATUS_COLLECTION') + ); + const snapshot = await collection.get(); + snapshot.forEach(async (doc) => { + const data = doc.data(); + if (data) { + // data should be a ChainStatus[] + const chains: ChainStatus[] = data.chains; + chains.forEach((chain) => { + // chain should be a ChainStatus + const emitters: Emitter[] = chain.emitters; + emitters.forEach((emitter) => { + // emitter should be an Emitter + const enqueuedVaas: EnqueuedVAAResponse[] = emitter.enqueuedVaas; + enqueuedVaas.forEach((vaa) => { + // vaa should be an EnqueuedVAAResponse + const governedVAA: GovernedVAA = { + chainId: chain.chainId, + emitterAddress: emitter.emitterAddress, + sequence: vaa.sequence, + txHash: vaa.txHash, + }; + const key = `${chain.chainId}/${emitter.emitterAddress}/${vaa.sequence}`; + vaas.set(key, governedVAA); + }); + }); + }); + } + }); + return vaas; +} + // 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.