From af5b937848a9dede08d6e6e70ed19bbca2825b87 Mon Sep 17 00:00:00 2001 From: Paul Noel Date: Thu, 6 Jun 2024 15:00:42 -0500 Subject: [PATCH] cloud_functions: use getNetwork() --- cloud_functions/src/alarmMissingVaas.ts | 2 +- cloud_functions/src/getMissingVaas.ts | 4 +-- common/src/consts.ts | 32 ++++++++++++++++--- dashboard/src/components/Monitor.tsx | 28 ++++++++-------- .../scripts/solanaMissedMessageAccounts.ts | 4 +-- 5 files changed, 46 insertions(+), 24 deletions(-) diff --git a/cloud_functions/src/alarmMissingVaas.ts b/cloud_functions/src/alarmMissingVaas.ts index 0f3c74ee..dce966a1 100644 --- a/cloud_functions/src/alarmMissingVaas.ts +++ b/cloud_functions/src/alarmMissingVaas.ts @@ -214,7 +214,7 @@ async function getAndProcessFirestore(): Promise> { const vaas: FirestoreVAA[] = data.VAAs; vaas.forEach((vaa) => { // vaa.chain is guaranteed to be a string representation of a number e.g. "34" - if (vaa.noticedTS > getMissThreshold(now, toChainId(Number(vaa.chain)))) { + if (vaa.noticedTS > getMissThreshold(now, vaa.chain)) { // console.log('keeping VAA in firestore', vaa.vaaKey); current.set(vaa.vaaKey, vaa); } diff --git a/cloud_functions/src/getMissingVaas.ts b/cloud_functions/src/getMissingVaas.ts index d3168cfd..ad7dc20f 100644 --- a/cloud_functions/src/getMissingVaas.ts +++ b/cloud_functions/src/getMissingVaas.ts @@ -1,7 +1,7 @@ import { Storage } from '@google-cloud/storage'; import { ObservedMessage } from './types'; -import { assertEnvironmentVariable } from './utils'; import { ChainId } from '@wormhole-foundation/sdk-base'; +import { getNetwork } from '@wormhole-foundation/wormhole-monitor-common'; // Read from cloud storage const storage = new Storage(); @@ -17,7 +17,7 @@ export type MissingVaasByChain = { export async function commonGetMissingVaas(): Promise { // The ID of your GCS bucket let bucketName: string = 'wormhole-observed-blocks-cache'; - if (assertEnvironmentVariable('NETWORK') === 'TESTNET') { + if (getNetwork() === 'Testnet') { bucketName = 'wormhole-observed-blocks-cache-testnet'; } const cacheBucket = storage.bucket(bucketName); diff --git a/common/src/consts.ts b/common/src/consts.ts index e503a55c..2d72d4a7 100644 --- a/common/src/consts.ts +++ b/common/src/consts.ts @@ -1,4 +1,11 @@ -import { Chain, ChainId, Network, chainToChainId, toChain, toChainId } from '@wormhole-foundation/sdk-base'; +import { + Chain, + ChainId, + Network, + chainToChainId, + toChain, + toChainId, +} from '@wormhole-foundation/sdk-base'; export type Mode = 'vaa' | 'ntt'; @@ -98,14 +105,29 @@ export const INITIAL_NTT_DEPLOYMENT_BLOCK_BY_NETWORK_AND_CHAIN: { ['Devnet']: {}, }; -export function getMissThreshold(date: Date, chainId: ChainId): string { - const missThresholdInMins = chainId === toChainId("Scroll") ? 120 : MISS_THRESHOLD_IN_MINS_DEFAULT; - const missDate = new Date(date); +export function getMissThreshold(date: Date, chainish: number | string | Chain | ChainId): string { + // We would like chainish to really be a ChainId. + let missThresholdInMins: number; + try { + let chainId: ChainId; + if (typeof chainish === 'string' && !Number.isNaN(Number(chainish))) { + // e.g. Handle '1' + chainId = toChainId(Number(chainish)); + } else { + // At this point we either have a number, a non-number string, a Chain, or a ChainId + chainId = toChainId(chainish); + } + missThresholdInMins = chainId === toChainId('Scroll') ? 120 : MISS_THRESHOLD_IN_MINS_DEFAULT; + } catch (e) { + // If we can't get the chainId, we'll use the default value. + missThresholdInMins = MISS_THRESHOLD_IN_MINS_DEFAULT; + } + const missDate = date; missDate.setMinutes(missDate.getMinutes() - missThresholdInMins); return missDate.toISOString(); } - export const TOKEN_BRIDGE_EMITTERS: { [key in Chain]?: string } = { +export const TOKEN_BRIDGE_EMITTERS: { [key in Chain]?: string } = { Solana: 'ec7372995d5cc8732397fb0ad35c0121e0eaa90d26f828a534cab54391b3a4f5', Ethereum: '0000000000000000000000003ee18b2214aff97000d974cf647e7c347e8fa585', Terra: '0000000000000000000000007cf7b764e38a0a5e967972c1df77d432510564e2', diff --git a/dashboard/src/components/Monitor.tsx b/dashboard/src/components/Monitor.tsx index 91e6872b..648e46ed 100644 --- a/dashboard/src/components/Monitor.tsx +++ b/dashboard/src/components/Monitor.tsx @@ -16,13 +16,13 @@ import { Tooltip, Typography, } from '@mui/material'; -import { chainIdToChain, toChainId } from '@wormhole-foundation/sdk-base'; +import { ChainId, chainIdToChain } from '@wormhole-foundation/sdk-base'; import { MISS_THRESHOLD_LABEL, explorerBlock, explorerTx, explorerVaa, - getMissThreshold + getMissThreshold, } from '@wormhole-foundation/wormhole-monitor-common'; import axios from 'axios'; import { useCallback, useEffect, useMemo, useState } from 'react'; @@ -78,7 +78,7 @@ function BlockDetail({ chain, message }: { chain: string; message: ObservedMessa gutterBottom > Block {message.block}{' '} { const filteredMisses = showAllMisses ? info.messages - : info.messages.filter((message) => message.timestamp < getMissThreshold(now, toChainId(Number(chain)))); + : info.messages.filter((message) => message.timestamp < getMissThreshold(now, chain)); return filteredMisses.length === 0 ? null : filteredMisses @@ -295,7 +295,7 @@ function Misses({ const filteredMisses = showAllMisses ? info.messages : info.messages - .filter((message) => message.timestamp < getMissThreshold(now, toChainId(Number(chain)))) + .filter((message) => message.timestamp < getMissThreshold(now, chain)) .filter( (message) => !governorInfo?.enqueuedVAAs.some( @@ -309,7 +309,7 @@ function Misses({ @@ -385,7 +385,7 @@ function Monitor({ governorInfo }: { governorInfo?: CloudGovernorInfo | null }) const filteredMisses = showAllMisses ? info.messages : info.messages - .filter((message) => message.timestamp < getMissThreshold(now, toChainId(Number(chain)))) + .filter((message) => message.timestamp < getMissThreshold(now, chain)) .filter( (message) => !governorInfo?.enqueuedVAAs.some( @@ -397,7 +397,7 @@ function Monitor({ governorInfo }: { governorInfo?: CloudGovernorInfo | null }) ); return filteredMisses.length === 0 ? counts - : { ...counts, [toChainId(Number(chain))]: filteredMisses.length }; + : { ...counts, [Number(chain) as ChainId]: filteredMisses.length }; }, {}) : {}; }, [governorInfo?.enqueuedVAAs, misses, showAllMisses]); @@ -499,14 +499,14 @@ function Monitor({ governorInfo }: { governorInfo?: CloudGovernorInfo | null }) header={
- {chainIdToChain.get(toChainId(Number(chain)))} ({chain}) + {chainIdToChain.get(Number(chain) as ChainId)} ({chain}) Last Indexed Block - {lastBlock.split('/')[0]} {' - '} {new Date(lastBlock.split('/')[1]).toLocaleString()} - {messageCounts?.[toChainId(Number(chain))] ? ( + {messageCounts?.[Number(chain) as ChainId] ? (  ={' '} - {messageCounts?.[toChainId(Number(chain))]?.numMessagesWithoutVaas} + {messageCounts?.[Number(chain) as ChainId]?.numMessagesWithoutVaas}     ={' '} - {(messageCounts?.[toChainId(Number(chain))]?.numTotalMessages || 0) - - (messageCounts?.[toChainId(Number(chain))]?.numMessagesWithoutVaas || + {(messageCounts?.[Number(chain) as ChainId]?.numTotalMessages || 0) - + (messageCounts?.[Number(chain) as ChainId]?.numMessagesWithoutVaas || 0)} ) : null} diff --git a/watcher/scripts/solanaMissedMessageAccounts.ts b/watcher/scripts/solanaMissedMessageAccounts.ts index f57e10aa..e2b4b2c7 100644 --- a/watcher/scripts/solanaMissedMessageAccounts.ts +++ b/watcher/scripts/solanaMissedMessageAccounts.ts @@ -10,13 +10,13 @@ import { normalizeCompileInstruction, } from '@wormhole-foundation/wormhole-monitor-common/src/solana'; import { getMissThreshold } from '@wormhole-foundation/wormhole-monitor-common'; -import { contracts, toChainId } from '@wormhole-foundation/sdk-base'; +import { contracts } from '@wormhole-foundation/sdk-base'; // This script finds the message accounts which correspond to solana misses (async () => { const now = new Date(); - const missThreshold = getMissThreshold(now, toChainId('Solana')) + const missThreshold = getMissThreshold(now, 'Solana'); let log = ora('Fetching Solana misses').start(); try { const response = await axios.get(