Skip to content

Commit

Permalink
cloud_functions: use getNetwork()
Browse files Browse the repository at this point in the history
  • Loading branch information
panoel committed Jun 7, 2024
1 parent 53a55a7 commit af5b937
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
2 changes: 1 addition & 1 deletion cloud_functions/src/alarmMissingVaas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ async function getAndProcessFirestore(): Promise<Map<string, FirestoreVAA>> {
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);
}
Expand Down
4 changes: 2 additions & 2 deletions cloud_functions/src/getMissingVaas.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -17,7 +17,7 @@ export type MissingVaasByChain = {
export async function commonGetMissingVaas(): Promise<MissingVaasByChain> {
// 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);
Expand Down
32 changes: 27 additions & 5 deletions common/src/consts.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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',
Expand Down
28 changes: 14 additions & 14 deletions dashboard/src/components/Monitor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -78,7 +78,7 @@ function BlockDetail({ chain, message }: { chain: string; message: ObservedMessa
gutterBottom
>
<IconButton
href={explorerTx(network, toChainId(Number(chain)), message.txHash)}
href={explorerTx(network, Number(chain) as ChainId, message.txHash)}
target="_blank"
size="small"
sx={inlineIconButtonSx}
Expand Down Expand Up @@ -106,7 +106,7 @@ function BlockDetail({ chain, message }: { chain: string; message: ObservedMessa
<Typography gutterBottom>
Block {message.block}{' '}
<IconButton
href={explorerBlock(network, toChainId(Number(chain)), message.block.toString())}
href={explorerBlock(network, Number(chain) as ChainId, message.block.toString())}
target="_blank"
size="small"
sx={inlineIconButtonSx}
Expand Down Expand Up @@ -240,7 +240,7 @@ function ReobserveCodeContent({ misses }: { misses: MissesByChain }) {
.map(([chain, info]) => {
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
Expand Down Expand Up @@ -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(
Expand All @@ -309,7 +309,7 @@ function Misses({
<CollapsibleSection
key={chain}
defaultExpanded={false}
header={`${chainIdToChain.get(toChainId(Number(chain)))} (${chain}) - ${
header={`${chainIdToChain.get(Number(chain) as ChainId)} (${chain}) - ${
filteredMisses.length
}`}
>
Expand Down Expand Up @@ -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(
Expand All @@ -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]);
Expand Down Expand Up @@ -499,14 +499,14 @@ function Monitor({ governorInfo }: { governorInfo?: CloudGovernorInfo | null })
header={
<div>
<Typography variant="h5" sx={{ mb: 0.5 }}>
{chainIdToChain.get(toChainId(Number(chain)))} ({chain})
{chainIdToChain.get(Number(chain) as ChainId)} ({chain})
</Typography>
<Typography variant="body2" sx={{ mb: 0.5 }}>
Last Indexed Block - {lastBlock.split('/')[0]}
{' - '}
{new Date(lastBlock.split('/')[1]).toLocaleString()}
</Typography>
{messageCounts?.[toChainId(Number(chain))] ? (
{messageCounts?.[Number(chain) as ChainId] ? (
<Typography
component="div"
sx={{
Expand All @@ -516,12 +516,12 @@ function Monitor({ governorInfo }: { governorInfo?: CloudGovernorInfo | null })
>
<Box sx={missingBlockSx} />
&nbsp;={' '}
{messageCounts?.[toChainId(Number(chain))]?.numMessagesWithoutVaas}
{messageCounts?.[Number(chain) as ChainId]?.numMessagesWithoutVaas}
&nbsp;&nbsp;
<Box sx={doneBlockSx} />
&nbsp;={' '}
{(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)}
</Typography>
) : null}
Expand Down
4 changes: 2 additions & 2 deletions watcher/scripts/solanaMissedMessageAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit af5b937

Please sign in to comment.